Skip to content

Commit

Permalink
Merge c9c197f into e62c3db
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed May 21, 2018
2 parents e62c3db + c9c197f commit 8aff836
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 56 deletions.
6 changes: 2 additions & 4 deletions .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"
4 changes: 2 additions & 2 deletions lib/any.js
Expand Up @@ -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 || {};

Expand All @@ -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;
};
26 changes: 10 additions & 16 deletions lib/form.js
Expand Up @@ -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 || {};
Expand All @@ -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;
}
};
42 changes: 18 additions & 24 deletions lib/json.js
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions lib/text.js
Expand Up @@ -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);

Expand All @@ -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;
};

0 comments on commit 8aff836

Please sign in to comment.