diff --git a/README.md b/README.md index 28c7080..81653b9 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ const service = require('restana')({ - `allowUnsafeRegex`: If `TRUE`, potentially catastrophic exponential-time regular expressions are disabled. Default value: `FALSE` - `maxParamLength`: Defines the custom length for parameters in parametric (standard, regex and multi) routes. Default value: `100` - `defaultRoute`: Default route handler when no route match occurs. Default value: `((req, res) => res.send(404))` +- `disableResponseEvent`: If `TRUE`, there won't be `response` events triggered on the `res` object. Default value: `FALSE` #### Example usage: ```js diff --git a/index.js b/index.js index 29220e0..dd66792 100644 --- a/index.js +++ b/index.js @@ -64,6 +64,11 @@ module.exports = (options = {}) => { // the "restana" service interface const app = { + /** + * Application configuration options reference + */ + options, + /** * Register global middleware * @@ -147,7 +152,7 @@ module.exports = (options = {}) => { * @param {Object} res Response object */ handle: (req, res) => { - res.send = exts.response.send(req, res) + res.send = exts.response.send(options, req, res) if (middlewares.length > 0) { // call route middlewares and route handler next([ diff --git a/libs/response-extensions.js b/libs/response-extensions.js index 180e47a..d6eb1f4 100644 --- a/libs/response-extensions.js +++ b/libs/response-extensions.js @@ -1,9 +1,12 @@ /** * The friendly 'res.send' method - * * No comments needed ;) + * + * @param {Object} options Application configuration options + * @param {Object} req Request object + * @param {Object} res Response object */ -module.exports.send = (req, res) => (data = 200, code = 200, headers = null, cb = () => {}) => { +module.exports.send = (options, req, res) => (data = 200, code = 200, headers = null, cb = () => {}) => { if (headers !== null) { // attach custom headers on the response Object.keys(headers).forEach((key) => { @@ -35,7 +38,7 @@ module.exports.send = (req, res) => (data = 200, code = 200, headers = null, cb data, code } - res.emit('response', params) + if (options.disableResponseEvent !== true) { res.emit('response', params) } if (typeof data === 'object') { // transparently setting the 'content-type' header if JSON diff --git a/package-lock.json b/package-lock.json index 144f22f..7612399 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "restana", - "version": "2.9.0", + "version": "2.10.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index dfcd6c0..0119841 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "restana", - "version": "2.9.0", + "version": "2.10.0", "description": "Super fast and minimalist web framework for building REST micro-services.", "main": "index.js", "scripts": { diff --git a/performance/restana-minimal.js b/performance/restana-minimal.js index e13650a..35a85d1 100644 --- a/performance/restana-minimal.js +++ b/performance/restana-minimal.js @@ -1,4 +1,6 @@ -const service = require('./../index')({}) +const service = require('./../index')({ + disableResponseEvent: true +}) service.get('/hi', (req, res) => { res.send('Hello World!') diff --git a/specs/disable-response-event.test.js b/specs/disable-response-event.test.js new file mode 100644 index 0000000..ef214a7 --- /dev/null +++ b/specs/disable-response-event.test.js @@ -0,0 +1,39 @@ +/* global describe, it */ +const request = require('supertest') +const expect = require('chai').expect + +describe('Disable Response Event', () => { + let server + const service = require('../index')({ + disableResponseEvent: true + }) + service.use((req, res, next) => { + const now = new Date().getTime() + + res.on('response', (e) => { + e.res.setHeader('x-response-time', new Date().getTime() - now) + }) + + return next() + }) + service.get('/hello', (req, res) => { + res.send(200) + }) + + it('should start the service with "disableResponseEvent: TRUE"', async () => { + server = await service.start(~~process.env.PORT) + }) + + it('should GET 200 on /hello and no response header', async () => { + await request(server) + .get('/hello') + .expect(200) + .then((response) => { + expect(response.headers['x-response-time']).to.equal(undefined) + }) + }) + + it('should successfully terminate the service', async () => { + await service.close() + }) +})