From c9c197f4dda0f3ce0d66002d11fe18da04290f2e Mon Sep 17 00:00:00 2001 From: popomore Date: Mon, 21 May 2018 14:16:44 +0800 Subject: [PATCH] feat: use async function --- .travis.yml | 6 ++---- lib/any.js | 4 ++-- lib/form.js | 26 ++++++++++---------------- lib/json.js | 42 ++++++++++++++++++------------------------ lib/text.js | 14 ++++---------- 5 files changed, 36 insertions(+), 56 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6065018..b9b5841 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,7 @@ sudo: false language: node_js node_js: - - "0.12" - - "4" - - "5" - - "6" + - "8" + - "10" script: "npm run test-cov" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" diff --git a/lib/any.js b/lib/any.js index 4798897..f2df691 100644 --- a/lib/any.js +++ b/lib/any.js @@ -25,7 +25,7 @@ var textTypes = ['text']; * @api public */ -module.exports = function(req, opts){ +module.exports = async function(req, opts){ req = req.req || req; opts = opts || {}; @@ -46,5 +46,5 @@ module.exports = function(req, opts){ var message = type ? 'Unsupported content-type: ' + type : 'Missing content-type'; var err = new Error(message); err.status = 415; - return Promise.reject(err); + throw err; }; diff --git a/lib/form.js b/lib/form.js index 1d85c4e..7462c77 100644 --- a/lib/form.js +++ b/lib/form.js @@ -20,7 +20,7 @@ var utils = require('./utils'); * @api public */ -module.exports = function(req, opts){ +module.exports = async function(req, opts){ req = req.req || req; opts = utils.clone(opts); var queryString = opts.queryString || {}; @@ -36,19 +36,13 @@ module.exports = function(req, opts){ opts.limit = opts.limit || '56kb'; opts.qs = opts.qs || qs; - // raw-body returns a Promise when no callback is specified - return Promise.resolve() - .then(function() { - return raw(inflate(req), opts); - }) - .then(function(str){ - try { - var parsed = opts.qs.parse(str, queryString); - return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed; - } catch (err) { - err.status = 400; - err.body = str; - throw err; - } - }); + const str = await raw(inflate(req), opts); + try { + var parsed = opts.qs.parse(str, queryString); + return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed; + } catch (err) { + err.status = 400; + err.body = str; + throw err; + } }; diff --git a/lib/json.js b/lib/json.js index 7bc8f12..4dc48f4 100644 --- a/lib/json.js +++ b/lib/json.js @@ -3,13 +3,13 @@ * Module dependencies. */ -var raw = require('raw-body'); -var inflate = require('inflation'); -var utils = require('./utils'); +const raw = require('raw-body'); +const inflate = require('inflation'); +const utils = require('./utils'); // Allowed whitespace is defined in RFC 7159 // http://www.rfc-editor.org/rfc/rfc7159.txt -var strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/; +const strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/; /** * Return a Promise which parses json requests. @@ -23,33 +23,27 @@ var strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/; * @api public */ -module.exports = function(req, opts){ +module.exports = async function(req, opts){ req = req.req || req; opts = utils.clone(opts); // defaults - var len = req.headers['content-length']; - var encoding = req.headers['content-encoding'] || 'identity'; + let len = req.headers['content-length']; + const encoding = req.headers['content-encoding'] || 'identity'; if (len && encoding === 'identity') opts.length = len = ~~len; opts.encoding = opts.encoding || 'utf8'; opts.limit = opts.limit || '1mb'; - var strict = opts.strict !== false; - - // raw-body returns a promise when no callback is specified - return Promise.resolve() - .then(function() { - return raw(inflate(req), opts); - }) - .then(function(str) { - try { - var parsed = parse(str); - return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed; - } catch (err) { - err.status = 400; - err.body = str; - throw err; - } - }); + const strict = opts.strict !== false; + + const str = await raw(inflate(req), opts); + try { + const parsed = parse(str); + return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed; + } catch (err) { + err.status = 400; + err.body = str; + throw err; + } function parse(str){ if (!strict) return str ? JSON.parse(str) : str; diff --git a/lib/text.js b/lib/text.js index 1e46cd6..7c41300 100644 --- a/lib/text.js +++ b/lib/text.js @@ -18,7 +18,7 @@ var utils = require('./utils'); * @api public */ -module.exports = function(req, opts){ +module.exports = async function(req, opts){ req = req.req || req; opts = utils.clone(opts); @@ -29,13 +29,7 @@ module.exports = function(req, opts){ opts.encoding = opts.encoding === undefined ? 'utf8': opts.encoding; opts.limit = opts.limit || '1mb'; - // raw-body returns a Promise when no callback is specified - return Promise.resolve() - .then(function() { - return raw(inflate(req), opts); - }) - .then(str => { - // ensure return the same format with json / form - return opts.returnRawBody ? { parsed: str, raw: str } : str; - }); + const str = await raw(inflate(req), opts); + // ensure return the same format with json / form + return opts.returnRawBody ? { parsed: str, raw: str } : str; };