Skip to content

Commit

Permalink
adding disableResponseEvent config support
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Mar 10, 2019
1 parent a50bd60 commit 9005403
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ module.exports = (options = {}) => {

// the "restana" service interface
const app = {
/**
* Application configuration options reference
*/
options,

/**
* Register global middleware
*
Expand Down Expand Up @@ -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([
Expand Down
9 changes: 6 additions & 3 deletions libs/response-extensions.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion performance/restana-minimal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const service = require('./../index')({})
const service = require('./../index')({
disableResponseEvent: true
})

service.get('/hi', (req, res) => {
res.send('Hello World!')
Expand Down
39 changes: 39 additions & 0 deletions specs/disable-response-event.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 9005403

Please sign in to comment.