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 1 commit
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
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.8"
- "2.5"
Expand Down Expand Up @@ -68,21 +66,21 @@ before_install:
fi
- |
# Configure mocha for testing
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
if node_version_lt '0.12'; then npm_use_module 'mocha' '2.5.3'
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3'
elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0'
elif node_version_lt '8.0' ; then npm_use_module 'mocha' '6.2.2'
fi
- |
# Configure nyc for coverage
if node_version_lt '0.10'; then npm_remove_module_re '^nyc$'
if node_version_lt '0.12'; then npm_remove_module_re '^nyc$'
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
elif node_version_lt '4.0' ; then npm_use_module 'nyc' '10.3.2'
elif node_version_lt '6.0' ; then npm_use_module 'nyc' '11.9.0'
elif node_version_lt '8.0' ; then npm_use_module 'nyc' '14.1.1'
fi
- |
# Configure supertest for http calls
if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0'
if node_version_lt '0.12'; then npm_use_module 'supertest' '1.1.0'
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
elif node_version_lt '4.0' ; then npm_use_module 'supertest' '2.0.0'
elif node_version_lt '6.0' ; then npm_use_module 'supertest' '3.4.2'
fi
Expand Down
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
80 changes: 43 additions & 37 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,48 @@ function read (req, res, next, parse, debug, options) {

// read body
debug('read body')
getBody(stream, opts, function (error, body) {
if (error) {
new Promise(function (resolve, reject) {
kjarmicki marked this conversation as resolved.
Show resolved Hide resolved
getBody(stream, opts, function (error, body) {
if (error) {
return reject(error)
}
resolve(body)
})
})
.then(function (body) {
// verify
if (verify) {
try {
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err, {
body: body,
type: err.type || 'entity.verify.failed'
}))
return
}
}

// parse
var str = body
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err) {
next(createError(400, err, {
body: str,
type: err.type || 'entity.parse.failed'
}))
return
}

next()
})
.catch(function (error) {
var _error

if (error.type === 'encoding.unsupported') {
Expand All @@ -94,41 +134,7 @@ function read (req, res, next, parse, debug, options) {
onFinished(req, function onfinished () {
next(createError(400, _error))
})
return
}

// verify
if (verify) {
try {
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err, {
body: body,
type: err.type || 'entity.verify.failed'
}))
return
}
}

// parse
var str = body
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err) {
next(createError(400, err, {
body: str,
type: err.type || 'entity.parse.failed'
}))
return
}

next()
})
})
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"index.js"
],
"engines": {
"node": ">= 0.8"
"node": ">= 0.12"
},
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
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 = true
} 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