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 #404

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
55 changes: 33 additions & 22 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,27 @@ function read (req, res, next, parse, debug, options) {
}

// read body
debug('read body')
getBody(stream, opts, function (error, body) {
if (error) {
var _error

if (error.type === 'encoding.unsupported') {
// echo back charset
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
charset: encoding.toLowerCase(),
type: 'charset.unsupported'
})
} else {
// set status code on error
_error = createError(400, error)
}

// read off entire request
stream.resume()
onFinished(req, function onfinished () {
next(createError(400, _error))
function readErrorHandler (error) {
var _error
if (error.type === 'encoding.unsupported') {
// echo back charset
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
charset: encoding.toLowerCase(),
type: 'charset.unsupported'
})
return
} else {
// set status code on error
_error = createError(400, error)
}

// read off entire request
stream.resume()
onFinished(req, function onfinished () {
next(createError(400, _error))
})
}

function readSuccessHandler (body) {
// verify
if (verify) {
try {
Expand Down Expand Up @@ -128,7 +125,21 @@ function read (req, res, next, parse, debug, options) {
}

next()
})
}

debug('read body')
if (typeof global.Promise === 'function') {
getBody(stream, opts)
.then(readSuccessHandler)
.catch(readErrorHandler)
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
} else {
getBody(stream, opts, function (error, body) {
if (error) {
return readErrorHandler(error)
}
readSuccessHandler(body)
})
}
}

/**
Expand Down
32 changes: 32 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 testAsyncHooks = false
try {
var asyncHooks = require('async_hooks')
testAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'
} catch (ignored) {
}

var bodyParser = require('..')

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

if (testAsyncHooks) {
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
it('should work with async hooks', 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)
})
}

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