diff --git a/lib/gateway/context.js b/lib/gateway/context.js index d442878b2..c88343bf1 100644 --- a/lib/gateway/context.js +++ b/lib/gateway/context.js @@ -1,7 +1,13 @@ const vm = require('vm'); - -function EgContextBase () {} - +const uuid62 = require('uuid-base62'); +function EgContextBase () { + this.requestID = uuid62.v4(); +} +Object.defineProperty(EgContextBase.prototype, 'egContext', { + get () { + return this; + } +}); EgContextBase.prototype.evaluateAsTemplateString = function (expression) { return this.run('`' + expression + '`', this); }; @@ -10,8 +16,12 @@ EgContextBase.prototype.match = function (expression) { // TODO: now it is Specifically converting to bool, but it may be needed to extend to support // strings like 'yes', 'true', }; -EgContextBase.prototype.run = function (code) { - return vm.runInNewContext(code, { egContext: this, req: this.req, res: this.res }); +EgContextBase.prototype.run = function (code, ctx) { + // Note: It must be some standard object like `this` + // Do not construct new object each time + // In that case it will have to build new contextified object + // So subsequent calls will take longer time then agains some already contextified object + return vm.runInNewContext(code, ctx || this); }; module.exports = EgContextBase; diff --git a/lib/policies/log/log.js b/lib/policies/log/log.js index 76719ce1f..5f3299a64 100644 --- a/lib/policies/log/log.js +++ b/lib/policies/log/log.js @@ -1,11 +1,8 @@ -'use strict'; - const logger = require('./winston-logger'); -const ConfigurationError = require('../../errors').ConfigurationError; module.exports = function (params) { if (!params || !params.message) { - throw new ConfigurationError('Log middleware requires "message" param'); + throw new Error('Log middleware requires "message" param'); } return function (req, res, next) { diff --git a/test/policies/log/log.test.js b/test/policies/log/log.test.js index 6d396cadf..c492468d8 100644 --- a/test/policies/log/log.test.js +++ b/test/policies/log/log.test.js @@ -4,7 +4,7 @@ const logger = require('../../../lib/policies/log/winston-logger'); const sinon = require('sinon'); const assert = require('assert'); -describe('logging policy', () => { +describe('@log policy', () => { const res = { test: 'text' }; @@ -30,6 +30,28 @@ describe('logging policy', () => { assert.equal(logger.info.getCall(0).args[0], '/test GET text'); assert.ok(next.calledOnce); }); + it('should log requestID', () => { + const next = sinon.spy(); + const logMiddleware = logPolicy({ + // eslint-disable-next-line no-template-curly-in-string + message: '${requestID}' + }); + + logMiddleware(req, {}, next); + assert.ok(logger.info.getCall(0).args[0].length > 10); + assert.ok(next.calledOnce); + }); + it('should log egContext.requestID', () => { + const next = sinon.spy(); + const logMiddleware = logPolicy({ + // eslint-disable-next-line no-template-curly-in-string + message: '${egContext.requestID}' + }); + + logMiddleware(req, {}, next); + assert.ok(logger.info.getCall(0).args[0].length > 10); + assert.ok(next.calledOnce); + }); it('should fail to access global context', () => { const next = sinon.spy(); const logMiddleware = logPolicy({