http-body-parser
Advanced tools
Comparing version 1.1.5 to 1.1.6
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _querystring = require('querystring'); | ||
@@ -16,10 +14,4 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var FormParser = function () { | ||
function FormParser(body, headers) { | ||
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 56 * 1024; | ||
_classCallCheck(this, FormParser); | ||
class FormParser { | ||
constructor(body, headers, limit = 56 * 1024) { | ||
this.body = body; | ||
@@ -30,20 +22,12 @@ this.headers = headers; | ||
_createClass(FormParser, [{ | ||
key: 'parse', | ||
value: function parse() { | ||
return _querystring2.default.parse(this.body.toString(this.headers['content-encoding'] || 'utf8')); | ||
} | ||
}], [{ | ||
key: 'getTypes', | ||
value: function getTypes() { | ||
var extendsTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
parse() { | ||
return _querystring2.default.parse(this.body.toString(this.headers['content-encoding'] || 'utf8')); | ||
} | ||
var originTypes = new Array('application/x-www-form-urlencoded'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
}]); | ||
static getTypes(extendsTypes = []) { | ||
const originTypes = new Array('application/x-www-form-urlencoded'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
} | ||
return FormParser; | ||
}(); | ||
exports.default = FormParser; |
157
lib/index.js
@@ -31,109 +31,56 @@ 'use strict'; | ||
exports.koa = function () { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(ctx, next) { | ||
var parserFactory, body, parser; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
// new parserFactory | ||
parserFactory = new _ParserFactory2.default(ctx.req, options.enableTypes); | ||
// add parser to factory | ||
exports.koa = (options = {}) => (() => { | ||
var _ref = _asyncToGenerator(function* (ctx, next) { | ||
// new parserFactory | ||
const parserFactory = new _ParserFactory2.default(ctx.req, options.enableTypes); | ||
// add parser to factory | ||
parserFactory.addParser('json', _JsonParser2.default, options.json); | ||
parserFactory.addParser('form', _FormParser2.default, options.form); | ||
parserFactory.addParser('text', _TextParser2.default, options.text); | ||
parserFactory.addParser('multipart', _MultipartParser2.default, options.multipart); | ||
parserFactory.addParser('stream', _StreamParser2.default, options.stream); | ||
// get request body | ||
const body = yield parserFactory.getBody(); | ||
// parse body | ||
const parser = parserFactory.getEnableParser(body); | ||
if (parser) { | ||
ctx.request.rawBody = body; | ||
ctx.request.body = parser.parse(); | ||
} | ||
yield next(); | ||
}); | ||
parserFactory.addParser('json', _JsonParser2.default, options.json); | ||
parserFactory.addParser('form', _FormParser2.default, options.form); | ||
parserFactory.addParser('text', _TextParser2.default, options.text); | ||
parserFactory.addParser('multipart', _MultipartParser2.default, options.multipart); | ||
parserFactory.addParser('stream', _StreamParser2.default, options.stream); | ||
// get request body | ||
_context.next = 8; | ||
return parserFactory.getBody(); | ||
return function (_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
})(); | ||
case 8: | ||
body = _context.sent; | ||
exports.express = (options = {}) => (() => { | ||
var _ref2 = _asyncToGenerator(function* (req, res, next) { | ||
try { | ||
// new parserFactory | ||
const parserFactory = new _ParserFactory2.default(req, options.enableTypes); | ||
// add parser to factory | ||
parserFactory.addParser('json', _JsonParser2.default, options.json); | ||
parserFactory.addParser('form', _FormParser2.default, options.form); | ||
parserFactory.addParser('text', _TextParser2.default, options.text); | ||
parserFactory.addParser('multipart', _MultipartParser2.default, options.multipart); | ||
parserFactory.addParser('stream', _StreamParser2.default, options.stream); | ||
// get request body | ||
const body = yield parserFactory.getBody(); | ||
// parse body | ||
const parser = parserFactory.getEnableParser(body); | ||
if (parser) { | ||
req.rawBody = body; | ||
req.body = parser.parse(); | ||
} | ||
} catch (e) { | ||
return next(e); | ||
} | ||
yield next(); | ||
}); | ||
// parse body | ||
parser = parserFactory.getEnableParser(body); | ||
if (parser) { | ||
ctx.request.rawBody = body; | ||
ctx.request.body = parser.parse(); | ||
} | ||
_context.next = 13; | ||
return next(); | ||
case 13: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
})); | ||
return function (_x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
}; | ||
exports.express = function () { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function () { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(req, res, next) { | ||
var parserFactory, body, parser; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.prev = 0; | ||
// new parserFactory | ||
parserFactory = new _ParserFactory2.default(req, options.enableTypes); | ||
// add parser to factory | ||
parserFactory.addParser('json', _JsonParser2.default, options.json); | ||
parserFactory.addParser('form', _FormParser2.default, options.form); | ||
parserFactory.addParser('text', _TextParser2.default, options.text); | ||
parserFactory.addParser('multipart', _MultipartParser2.default, options.multipart); | ||
parserFactory.addParser('stream', _StreamParser2.default, options.stream); | ||
// get request body | ||
_context2.next = 9; | ||
return parserFactory.getBody(); | ||
case 9: | ||
body = _context2.sent; | ||
// parse body | ||
parser = parserFactory.getEnableParser(body); | ||
if (parser) { | ||
req.rawBody = body; | ||
req.body = parser.parse(); | ||
} | ||
_context2.next = 17; | ||
break; | ||
case 14: | ||
_context2.prev = 14; | ||
_context2.t0 = _context2['catch'](0); | ||
return _context2.abrupt('return', next(_context2.t0)); | ||
case 17: | ||
_context2.next = 19; | ||
return next(); | ||
case 19: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, undefined, [[0, 14]]); | ||
})); | ||
return function (_x5, _x6, _x7) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
}; | ||
return function (_x3, _x4, _x5) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
})(); |
@@ -6,13 +6,4 @@ 'use strict'; | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var JsonParser = function () { | ||
function JsonParser(body, headers) { | ||
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1024 * 1024; | ||
_classCallCheck(this, JsonParser); | ||
class JsonParser { | ||
constructor(body, headers, limit = 1024 * 1024) { | ||
this.body = body; | ||
@@ -23,20 +14,12 @@ this.headers = headers; | ||
_createClass(JsonParser, [{ | ||
key: 'parse', | ||
value: function parse() { | ||
return JSON.parse(this.body.toString(this.headers['content-encoding'] || 'utf8')); | ||
} | ||
}], [{ | ||
key: 'getTypes', | ||
value: function getTypes() { | ||
var extendsTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
parse() { | ||
return JSON.parse(this.body.toString(this.headers['content-encoding'] || 'utf8')); | ||
} | ||
var originTypes = ['application/json', 'application/json-patch+json', 'application/vnd.api+json', 'application/csp-report']; | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
}]); | ||
static getTypes(extendsTypes = []) { | ||
const originTypes = ['application/json', 'application/json-patch+json', 'application/vnd.api+json', 'application/csp-report']; | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
} | ||
return JsonParser; | ||
}(); | ||
exports.default = JsonParser; |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _fs = require('fs'); | ||
@@ -28,11 +26,4 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var MultipartParser = function () { | ||
function MultipartParser(body, headers) { | ||
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1024 * 1024; | ||
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _os2.default.tmpdir(); | ||
_classCallCheck(this, MultipartParser); | ||
class MultipartParser { | ||
constructor(body, headers, limit = 1024 * 1024, path = _os2.default.tmpdir()) { | ||
this.body = body; | ||
@@ -42,58 +33,50 @@ this.headers = headers; | ||
this.path = path; | ||
this.boundary = Buffer.from('--' + headers['content-type'].split(';').pop().replace('boundary=', '').trim()); | ||
this.boundary = Buffer.from(`--${headers['content-type'].split(';').pop().replace('boundary=', '').trim()}`); | ||
} | ||
_createClass(MultipartParser, [{ | ||
key: 'parse', | ||
value: function parse() { | ||
var begin = 0; | ||
var body = {}; | ||
while (begin < this.body.length) { | ||
var end = this.body.indexOf(this.boundary, begin + this.boundary.length); | ||
if (end === -1) { | ||
break; | ||
} | ||
var fieldStart = begin + this.boundary.length + 2; | ||
var fieldEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), fieldStart); | ||
var field = this.body.slice(fieldStart, fieldEnd); | ||
var nameMatch = field.toString().match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); | ||
var filenameMatch = field.toString().match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i); | ||
if (!nameMatch) { | ||
break; | ||
} | ||
if (!filenameMatch) { | ||
var valueStart = fieldEnd + 2 + 2; | ||
var valueEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), valueStart); | ||
var value = this.body.slice(valueStart, valueEnd); | ||
body[nameMatch[2]] = value.toString(); | ||
} else { | ||
var _valueStart = this.body.indexOf(Buffer.from([0x0d, 0x0a]), fieldEnd + 2) + 2 + 2; | ||
var _valueEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), _valueStart); | ||
var _value = this.body.slice(_valueStart, _valueEnd); | ||
body[nameMatch[2]] = { | ||
name: filenameMatch[2], | ||
path: _path2.default.join(this.path, filenameMatch[2]), | ||
mimetype: null, | ||
size: _value.length, | ||
sha256: _crypto2.default.createHash('sha256').update(_value).digest('hex') | ||
}; | ||
_fs2.default.writeFileSync(_path2.default.join(this.path, filenameMatch[2]), _value); | ||
} | ||
begin = end; | ||
parse() { | ||
let begin = 0; | ||
const body = {}; | ||
while (begin < this.body.length) { | ||
const end = this.body.indexOf(this.boundary, begin + this.boundary.length); | ||
if (end === -1) { | ||
break; | ||
} | ||
return body; | ||
const fieldStart = begin + this.boundary.length + 2; | ||
const fieldEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), fieldStart); | ||
const field = this.body.slice(fieldStart, fieldEnd); | ||
const nameMatch = field.toString().match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); | ||
const filenameMatch = field.toString().match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i); | ||
if (!nameMatch) { | ||
break; | ||
} | ||
if (!filenameMatch) { | ||
const valueStart = fieldEnd + 2 + 2; | ||
const valueEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), valueStart); | ||
const value = this.body.slice(valueStart, valueEnd); | ||
body[nameMatch[2]] = value.toString(); | ||
} else { | ||
const valueStart = this.body.indexOf(Buffer.from([0x0d, 0x0a]), fieldEnd + 2) + 2 + 2; | ||
const valueEnd = this.body.indexOf(Buffer.from([0x0d, 0x0a]), valueStart); | ||
const value = this.body.slice(valueStart, valueEnd); | ||
body[nameMatch[2]] = { | ||
name: filenameMatch[2], | ||
path: _path2.default.join(this.path, filenameMatch[2]), | ||
mimetype: null, | ||
size: value.length, | ||
sha256: _crypto2.default.createHash('sha256').update(value).digest('hex') | ||
}; | ||
_fs2.default.writeFileSync(_path2.default.join(this.path, filenameMatch[2]), value); | ||
} | ||
begin = end; | ||
} | ||
}], [{ | ||
key: 'getTypes', | ||
value: function getTypes() { | ||
var extendsTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
return body; | ||
} | ||
var originTypes = new Array('multipart/form-data'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
}]); | ||
static getTypes(extendsTypes = []) { | ||
const originTypes = new Array('multipart/form-data'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
} | ||
return MultipartParser; | ||
}(); | ||
exports.default = MultipartParser; |
@@ -7,6 +7,2 @@ 'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _TypeIs = require('./TypeIs'); | ||
@@ -18,11 +14,4 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var ParserFactory = function () { | ||
function ParserFactory(req) { | ||
var enableTypes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['json', 'form', 'text', 'multipart', 'stream']; | ||
var parsers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
_classCallCheck(this, ParserFactory); | ||
class ParserFactory { | ||
constructor(req, enableTypes = ['json', 'form', 'text', 'multipart', 'stream'], parsers = {}) { | ||
this.req = req; | ||
@@ -33,53 +22,34 @@ this.enableTypes = enableTypes; | ||
_createClass(ParserFactory, [{ | ||
key: 'addParser', | ||
value: function addParser(name, _Parser) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
addParser(name, _Parser, options = {}) { | ||
const Parser = _Parser; | ||
Parser.options = options; | ||
this.parsers[name] = Parser; | ||
} | ||
var Parser = _Parser; | ||
Parser.options = options; | ||
this.parsers[name] = Parser; | ||
} | ||
}, { | ||
key: 'getBody', | ||
value: function getBody() { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
var data = []; | ||
_this.req.on('data', function (chunk) { | ||
if (data.length + chunk.length > _this.limit) { | ||
throw new Error('invalid content length'); | ||
} | ||
data.push(chunk); | ||
}); | ||
_this.req.on('end', function () { | ||
return resolve(Buffer.concat(data)); | ||
}); | ||
getBody() { | ||
return new Promise(resolve => { | ||
const data = []; | ||
this.req.on('data', chunk => { | ||
if (data.length + chunk.length > this.limit) { | ||
throw new Error('invalid content length'); | ||
} | ||
data.push(chunk); | ||
}); | ||
} | ||
}, { | ||
key: 'getEnableParser', | ||
value: function getEnableParser(body) { | ||
var _this2 = this; | ||
this.req.on('end', () => resolve(Buffer.concat(data))); | ||
}); | ||
} | ||
var parser = null; | ||
Object.entries(this.parsers).forEach(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
name = _ref2[0], | ||
Parser = _ref2[1]; | ||
if (_this2.enableTypes.includes(name)) { | ||
if ((0, _TypeIs2.default)(_this2.req, Parser.getTypes(Parser.options.extendsTypes))) { | ||
parser = new Parser(body, _this2.req.headers, Parser.options.limit, Parser.options.path); | ||
} | ||
getEnableParser(body) { | ||
let parser = null; | ||
Object.entries(this.parsers).forEach(([name, Parser]) => { | ||
if (this.enableTypes.includes(name)) { | ||
if ((0, _TypeIs2.default)(this.req, Parser.getTypes(Parser.options.extendsTypes))) { | ||
parser = new Parser(body, this.req.headers, Parser.options.limit, Parser.options.path); | ||
} | ||
}); | ||
return parser; | ||
} | ||
}]); | ||
} | ||
}); | ||
return parser; | ||
} | ||
} | ||
return ParserFactory; | ||
}(); | ||
exports.default = ParserFactory; |
@@ -6,20 +6,10 @@ 'use strict'; | ||
}); | ||
const uuid = require('uuid'); | ||
const Path = require('path'); | ||
const crypto = require('crypto'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var uuid = require('uuid'); | ||
var Path = require('path'); | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var StreamParser = function () { | ||
function StreamParser(body, headers) { | ||
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1024 * 1024; | ||
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : os.tmpdir(); | ||
_classCallCheck(this, StreamParser); | ||
class StreamParser { | ||
constructor(body, headers, limit = 1024 * 1024, path = os.tmpdir()) { | ||
this.body = body; | ||
@@ -31,33 +21,25 @@ this.headers = headers; | ||
_createClass(StreamParser, [{ | ||
key: 'parse', | ||
value: function parse() { | ||
var filename = uuid(); | ||
var file = { | ||
name: filename, | ||
path: Path.join(this.path, filename), | ||
mimetype: null, | ||
size: this.body.length, | ||
sha256: crypto.createHash('sha256').update(this.body).digest('hex') | ||
}; | ||
var dirname = Path.dirname(file.path); | ||
if (!fs.existsSync(dirname)) { | ||
fs.mkdirSync(dirname); | ||
} | ||
fs.writeFileSync(file.path, this.body); | ||
return file; | ||
parse() { | ||
const filename = uuid(); | ||
const file = { | ||
name: filename, | ||
path: Path.join(this.path, filename), | ||
mimetype: null, | ||
size: this.body.length, | ||
sha256: crypto.createHash('sha256').update(this.body).digest('hex') | ||
}; | ||
const dirname = Path.dirname(file.path); | ||
if (!fs.existsSync(dirname)) { | ||
fs.mkdirSync(dirname); | ||
} | ||
}], [{ | ||
key: 'getTypes', | ||
value: function getTypes() { | ||
var extendsTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
fs.writeFileSync(file.path, this.body); | ||
return file; | ||
} | ||
var originTypes = new Array('application/octet-stream'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
}]); | ||
static getTypes(extendsTypes = []) { | ||
const originTypes = new Array('application/octet-stream'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
} | ||
return StreamParser; | ||
}(); | ||
exports.default = StreamParser; |
@@ -6,13 +6,4 @@ 'use strict'; | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var TextParser = function () { | ||
function TextParser(body, headers) { | ||
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1024 * 1024; | ||
_classCallCheck(this, TextParser); | ||
class TextParser { | ||
constructor(body, headers, limit = 1024 * 1024) { | ||
this.body = body; | ||
@@ -23,20 +14,12 @@ this.headers = headers; | ||
_createClass(TextParser, [{ | ||
key: 'parse', | ||
value: function parse() { | ||
return this.body.toString(this.headers['content-encoding'] || 'utf8'); | ||
} | ||
}], [{ | ||
key: 'getTypes', | ||
value: function getTypes() { | ||
var extendsTypes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
parse() { | ||
return this.body.toString(this.headers['content-encoding'] || 'utf8'); | ||
} | ||
var originTypes = new Array('text/plain'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
}]); | ||
static getTypes(extendsTypes = []) { | ||
const originTypes = new Array('text/plain'); | ||
return Array.from(new Set(originTypes.concat(extendsTypes))); | ||
} | ||
} | ||
return TextParser; | ||
}(); | ||
exports.default = TextParser; |
@@ -15,4 +15,4 @@ 'use strict'; | ||
var typer = require('media-typer'); | ||
var mime = require('mime-types'); | ||
const typer = require('media-typer'); | ||
const mime = require('mime-types'); | ||
@@ -45,7 +45,7 @@ /** | ||
function typeis(value, types_) { | ||
var i = void 0; | ||
var types = types_; | ||
let i; | ||
let types = types_; | ||
// remove parameters and normalize | ||
var val = tryNormalizeType(value); | ||
const val = tryNormalizeType(value); | ||
@@ -70,3 +70,3 @@ // no type or invalid | ||
var type = void 0; | ||
let type; | ||
for (i = 0; i < types.length; i++) { | ||
@@ -124,3 +124,3 @@ if (mimeMatch(normalize(type = types[i]), val)) { | ||
function typeofrequest(req, types_) { | ||
var types = types_; | ||
let types = types_; | ||
@@ -135,3 +135,3 @@ // no body | ||
types = new Array(arguments.length - 1); | ||
for (var i = 0; i < types.length; i++) { | ||
for (let i = 0; i < types.length; i++) { | ||
types[i] = arguments[i + 1]; | ||
@@ -142,3 +142,3 @@ } | ||
// request content type | ||
var value = req.headers['content-type']; | ||
const value = req.headers['content-type']; | ||
@@ -179,3 +179,3 @@ return typeis(value, types); | ||
// "+json" -> "*/*+json" expando | ||
return '*/*' + type; | ||
return `*/*${type}`; | ||
} | ||
@@ -204,4 +204,4 @@ | ||
// split types | ||
var actualParts = actual.split('/'); | ||
var expectedParts = expected.split('/'); | ||
const actualParts = actual.split('/'); | ||
const expectedParts = expected.split('/'); | ||
@@ -241,3 +241,3 @@ // invalid format | ||
// parse the type | ||
var type = typer.parse(value); | ||
const type = typer.parse(value); | ||
@@ -244,0 +244,0 @@ // remove the parameters |
{ | ||
"name": "http-body-parser", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "🎨 A body parser for node, koa, koa2, express. support json, form, text, multipart and stream type body.", | ||
@@ -8,3 +8,4 @@ "main": "lib/index.js", | ||
"build": "babel src -d lib", | ||
"test": "ava", | ||
"test": "ava ava test/src.*.spec.js", | ||
"test:build": "ava test/lib.*.spec.js", | ||
"prepublish": "npm run build" | ||
@@ -43,3 +44,3 @@ }, | ||
"engines": { | ||
"node": ">=7.6" | ||
"node": ">=6.0" | ||
}, | ||
@@ -59,14 +60,3 @@ "devDependencies": { | ||
"uuid": "^3.0.1" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"test/*.spec.js" | ||
], | ||
"tap": false, | ||
"babel": "inherit", | ||
"require": [ | ||
"babel-register", | ||
"babel-polyfill" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
583654
38
703
6