diff --git a/.travis.yml b/.travis.yml index bb7b66a3..1a38cdc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: node_js node_js: + - "0.8" + - "0.10" - "0.12" - "1.8" - "2.5" @@ -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 diff --git a/lib/read.js b/lib/read.js index c414d96a..453f90f6 100644 --- a/lib/read.js +++ b/lib/read.js @@ -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) }) + } } /** diff --git a/package.json b/package.json index ad1ff2e4..1ea75517 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "index.js" ], "engines": { - "node": ">= 0.12" + "node": ">= 0.8" }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .",