Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it work with Async Hooks #414

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ unreleased
- deps: [email protected]
* deps: [email protected]
* deps: type-is@~1.6.18
* Make it work with Async Hooks

1.19.0 / 2019-04-25
===================
Expand Down
22 changes: 18 additions & 4 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var iconv = require('iconv-lite')
var onFinished = require('on-finished')
var zlib = require('zlib')

var asyncHooks
try {
asyncHooks = require('async_hooks')
} catch (ignored) {
}

/**
* Module exports.
*/
Expand Down Expand Up @@ -74,7 +80,7 @@ function read (req, res, next, parse, debug, options) {

// read body
debug('read body')
getBody(stream, opts, function (error, body) {
getBody(stream, opts, preserveAsyncContext('GetBody', function (error, body) {
dougwilson marked this conversation as resolved.
Show resolved Hide resolved
if (error) {
var _error

Expand All @@ -91,9 +97,9 @@ function read (req, res, next, parse, debug, options) {

// read off entire request
stream.resume()
onFinished(req, function onfinished () {
onFinished(req, preserveAsyncContext('OnFinished', function onfinished () {
dougwilson marked this conversation as resolved.
Show resolved Hide resolved
next(createError(400, _error))
})
}))
return
}

Expand Down Expand Up @@ -128,7 +134,7 @@ function read (req, res, next, parse, debug, options) {
}

next()
})
}))
}

/**
Expand Down Expand Up @@ -179,3 +185,11 @@ function contentstream (req, debug, inflate) {

return stream
}

function preserveAsyncContext (asyncResourceType, fn) {
if (!asyncHooks) {
return fn
}
var asyncResource = new asyncHooks.AsyncResource(asyncResourceType)
return asyncResource.runInAsyncScope.bind(asyncResource, fn, null)
}
58 changes: 58 additions & 0 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ var http = require('http')
var methods = require('methods')
var request = require('supertest')

var describeWhenAsyncHooksAreSupported = describe.skip
try {
var asyncHooks = require('async_hooks')
describeWhenAsyncHooksAreSupported = typeof asyncHooks.AsyncLocalStorage === 'function' ? describe : describe.skip
} catch (ignored) {
}

var bodyParser = require('..')

describe('bodyParser()', function () {
Expand Down Expand Up @@ -51,6 +58,57 @@ describe('bodyParser()', function () {
.expect(200, '{"user":"tobi"}', done)
})

describeWhenAsyncHooksAreSupported('used with async hooks', function () {
it('should maintain context when parsing was successful', function (done) {
var _bodyParser = bodyParser()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()

var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(asyncLocalStorage.getStore()))
})
})
})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"contextMaintained":true}', done)
})

it('should maintain context when parsing has failed', function (done) {
var _bodyParser = bodyParser.text()
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()

var server = http.createServer(function (req, res) {
const store = {
contextMaintained: true
}
asyncLocalStorage.run(store, function () {
_bodyParser(req, res, function (err) {
if (!err) {
res.statusCode = 500
res.end('parsing was expeced to fail, but it succeeded')
}
res.end(JSON.stringify(asyncLocalStorage.getStore()))
})
})
})

request(server)
.post('/')
.set('Content-Type', 'text/plain; charset=x-fake')
.send('user is tobi')
.expect(200, '{"contextMaintained":true}', done)
})
})

describe('http methods', function () {
before(function () {
var _bodyParser = bodyParser()
Expand Down