Skip to content

Commit

Permalink
Make Async Hooks support backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarmicki committed Jul 10, 2020
1 parent 5809ad8 commit a28f49a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.8"
- "2.5"
Expand Down Expand Up @@ -66,21 +68,21 @@ before_install:
fi
- |
# Configure mocha for testing
if node_version_lt '0.12'; then npm_use_module 'mocha' '2.5.3'
if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3'
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.12'; then npm_remove_module_re '^nyc$'
if node_version_lt '0.10'; then npm_remove_module_re '^nyc$'
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.12'; then npm_use_module 'supertest' '1.1.0'
if node_version_lt '0.10'; then npm_use_module 'supertest' '1.1.0'
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
109 changes: 57 additions & 52 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,68 +73,73 @@ function read (req, res, next, parse, debug, options) {
}

// read body
debug('read body')
new Promise(function (resolve, reject) {
getBody(stream, opts, function (error, body) {
if (error) {
return reject(error)
}
resolve(body)
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'
})
} else {
// set status code on error
_error = createError(400, error)
}

// read off entire request
stream.resume()
onFinished(req, function onfinished () {
next(createError(400, _error))
})
})
.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
function readSuccessHandler (body) {
// verify
if (verify) {
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(400, err, {
body: str,
type: err.type || 'entity.parse.failed'
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()
}

next()
})
.catch(function (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)
debug('read body')
if (typeof global.Promise === 'function') {
getBody(stream, opts)
.then(readSuccessHandler)
.catch(readErrorHandler)
} else {
getBody(stream, opts, function (error, body) {
if (error) {
return readErrorHandler(error)
}

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

/**
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.12"
"node": ">= 0.8"
},
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
Expand Down
2 changes: 1 addition & 1 deletion test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var request = require('supertest')
var testAsyncHooks = false
try {
var asyncHooks = require('async_hooks')
testAsyncHooks = true
testAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'
} catch (ignored) {
}

Expand Down

0 comments on commit a28f49a

Please sign in to comment.