update
This commit is contained in:
+17
-17
@@ -11,13 +11,13 @@
|
||||
* @private
|
||||
*/
|
||||
|
||||
var createError = require('http-errors')
|
||||
var getBody = require('raw-body')
|
||||
var iconv = require('iconv-lite')
|
||||
var onFinished = require('on-finished')
|
||||
var zlib = require('node:zlib')
|
||||
var hasBody = require('type-is').hasBody
|
||||
var { getCharset } = require('./utils')
|
||||
const createError = require('http-errors')
|
||||
const getBody = require('raw-body')
|
||||
const iconv = require('iconv-lite')
|
||||
const onFinished = require('on-finished')
|
||||
const zlib = require('node:zlib')
|
||||
const hasBody = require('type-is').hasBody
|
||||
const { getCharset } = require('./utils')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
@@ -63,7 +63,7 @@ function read (req, res, next, parse, debug, options) {
|
||||
return
|
||||
}
|
||||
|
||||
var encoding = null
|
||||
let encoding = null
|
||||
if (options?.skipCharset !== true) {
|
||||
encoding = getCharset(req) || options.defaultCharset
|
||||
|
||||
@@ -78,12 +78,12 @@ function read (req, res, next, parse, debug, options) {
|
||||
}
|
||||
}
|
||||
|
||||
var length
|
||||
var opts = options
|
||||
var stream
|
||||
let length
|
||||
const opts = options
|
||||
let stream
|
||||
|
||||
// read options
|
||||
var verify = opts.verify
|
||||
const verify = opts.verify
|
||||
|
||||
try {
|
||||
// get the content stream
|
||||
@@ -112,7 +112,7 @@ function read (req, res, next, parse, debug, options) {
|
||||
debug('read body')
|
||||
getBody(stream, opts, function (error, body) {
|
||||
if (error) {
|
||||
var _error
|
||||
let _error
|
||||
|
||||
if (error.type === 'encoding.unsupported') {
|
||||
// echo back charset
|
||||
@@ -153,7 +153,7 @@ function read (req, res, next, parse, debug, options) {
|
||||
}
|
||||
|
||||
// parse
|
||||
var str = body
|
||||
let str = body
|
||||
try {
|
||||
debug('parse body')
|
||||
str = typeof body !== 'string' && encoding !== null
|
||||
@@ -182,8 +182,8 @@ function read (req, res, next, parse, debug, options) {
|
||||
* @private
|
||||
*/
|
||||
function contentstream (req, debug, inflate) {
|
||||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
|
||||
var length = req.headers['content-length']
|
||||
const encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
|
||||
const length = req.headers['content-length']
|
||||
|
||||
debug('content-encoding "%s"', encoding)
|
||||
|
||||
@@ -199,7 +199,7 @@ function contentstream (req, debug, inflate) {
|
||||
return req
|
||||
}
|
||||
|
||||
var stream = createDecompressionStream(encoding, debug)
|
||||
const stream = createDecompressionStream(encoding, debug)
|
||||
req.pipe(stream)
|
||||
return stream
|
||||
}
|
||||
|
||||
+69
-41
@@ -12,9 +12,9 @@
|
||||
* @private
|
||||
*/
|
||||
|
||||
var debug = require('debug')('body-parser:json')
|
||||
var read = require('../read')
|
||||
var { normalizeOptions } = require('../utils')
|
||||
const debug = require('debug')('body-parser:json')
|
||||
const read = require('../read')
|
||||
const { normalizeOptions } = require('../utils')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
@@ -33,10 +33,10 @@ module.exports = json
|
||||
* %x0A / ; Line feed or New line
|
||||
* %x0D ) ; Carriage return
|
||||
*/
|
||||
var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
|
||||
const FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
|
||||
|
||||
var JSON_SYNTAX_CHAR = '#'
|
||||
var JSON_SYNTAX_REGEXP = /#+/g
|
||||
const JSON_SYNTAX_CHAR = '#'
|
||||
const JSON_SYNTAX_REGEXP = /#+/g
|
||||
|
||||
/**
|
||||
* Create a middleware to parse JSON bodies.
|
||||
@@ -48,35 +48,7 @@ var JSON_SYNTAX_REGEXP = /#+/g
|
||||
function json (options) {
|
||||
const normalizedOptions = normalizeOptions(options, 'application/json')
|
||||
|
||||
var reviver = options?.reviver
|
||||
var strict = options?.strict !== false
|
||||
|
||||
function parse (body) {
|
||||
if (body.length === 0) {
|
||||
// special-case empty json body, as it's a common client-side mistake
|
||||
// TODO: maybe make this configurable or part of "strict" option
|
||||
return {}
|
||||
}
|
||||
|
||||
if (strict) {
|
||||
var first = firstchar(body)
|
||||
|
||||
if (first !== '{' && first !== '[') {
|
||||
debug('strict violation')
|
||||
throw createStrictSyntaxError(body, first)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
debug('parse json')
|
||||
return JSON.parse(body, reviver)
|
||||
} catch (e) {
|
||||
throw normalizeJsonSyntaxError(e, {
|
||||
message: e.message,
|
||||
stack: e.stack
|
||||
})
|
||||
}
|
||||
}
|
||||
const parse = createJsonParser(options)
|
||||
|
||||
const readOptions = {
|
||||
...normalizedOptions,
|
||||
@@ -89,6 +61,62 @@ function json (options) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a JSON parse function
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @return {function}
|
||||
* @private
|
||||
*/
|
||||
function createJsonParser (options) {
|
||||
const reviver = options?.reviver
|
||||
const strict = options?.strict !== false
|
||||
|
||||
if (strict) {
|
||||
return function parse (body) {
|
||||
if (body.length === 0) {
|
||||
// special-case empty json body, as it's a common client-side mistake
|
||||
// TODO: maybe make this configurable or part of "strict" option
|
||||
return {}
|
||||
}
|
||||
|
||||
const first = firstchar(body)
|
||||
if (first !== '{' && first !== '[') {
|
||||
debug('strict violation')
|
||||
throw createStrictSyntaxError(body, first)
|
||||
}
|
||||
|
||||
try {
|
||||
debug('parse json')
|
||||
return JSON.parse(body, reviver)
|
||||
} catch (e) {
|
||||
throw normalizeJsonSyntaxError(e, {
|
||||
message: e.message,
|
||||
stack: e.stack
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return function parse (body) {
|
||||
if (body.length === 0) {
|
||||
// special-case empty json body, as it's a common client-side mistake
|
||||
// TODO: maybe make this configurable or part of "strict" option
|
||||
return {}
|
||||
}
|
||||
|
||||
try {
|
||||
debug('parse json')
|
||||
return JSON.parse(body, reviver)
|
||||
} catch (e) {
|
||||
throw normalizeJsonSyntaxError(e, {
|
||||
message: e.message,
|
||||
stack: e.stack
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create strict violation syntax error matching native error.
|
||||
*
|
||||
@@ -98,8 +126,8 @@ function json (options) {
|
||||
* @private
|
||||
*/
|
||||
function createStrictSyntaxError (str, char) {
|
||||
var index = str.indexOf(char)
|
||||
var partial = ''
|
||||
const index = str.indexOf(char)
|
||||
let partial = ''
|
||||
|
||||
if (index !== -1) {
|
||||
partial = str.substring(0, index) + JSON_SYNTAX_CHAR.repeat(str.length - index)
|
||||
@@ -125,7 +153,7 @@ function createStrictSyntaxError (str, char) {
|
||||
* @private
|
||||
*/
|
||||
function firstchar (str) {
|
||||
var match = FIRST_CHAR_REGEXP.exec(str)
|
||||
const match = FIRST_CHAR_REGEXP.exec(str)
|
||||
|
||||
return match
|
||||
? match[1]
|
||||
@@ -141,10 +169,10 @@ function firstchar (str) {
|
||||
* @private
|
||||
*/
|
||||
function normalizeJsonSyntaxError (error, obj) {
|
||||
var keys = Object.getOwnPropertyNames(error)
|
||||
const keys = Object.getOwnPropertyNames(error)
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i]
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i]
|
||||
if (key !== 'stack' && key !== 'message') {
|
||||
delete error[key]
|
||||
}
|
||||
|
||||
+3
-3
@@ -10,9 +10,9 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var debug = require('debug')('body-parser:raw')
|
||||
var read = require('../read')
|
||||
var { normalizeOptions, passthrough } = require('../utils')
|
||||
const debug = require('debug')('body-parser:raw')
|
||||
const read = require('../read')
|
||||
const { normalizeOptions, passthrough } = require('../utils')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
|
||||
+3
-3
@@ -10,9 +10,9 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var debug = require('debug')('body-parser:text')
|
||||
var read = require('../read')
|
||||
var { normalizeOptions, passthrough } = require('../utils')
|
||||
const debug = require('debug')('body-parser:text')
|
||||
const read = require('../read')
|
||||
const { normalizeOptions, passthrough } = require('../utils')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
|
||||
+16
-20
@@ -12,11 +12,11 @@
|
||||
* @private
|
||||
*/
|
||||
|
||||
var createError = require('http-errors')
|
||||
var debug = require('debug')('body-parser:urlencoded')
|
||||
var read = require('../read')
|
||||
var qs = require('qs')
|
||||
var { normalizeOptions } = require('../utils')
|
||||
const createError = require('http-errors')
|
||||
const debug = require('debug')('body-parser:urlencoded')
|
||||
const read = require('../read')
|
||||
const qs = require('qs')
|
||||
const { normalizeOptions } = require('../utils')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
@@ -39,13 +39,7 @@ function urlencoded (options) {
|
||||
}
|
||||
|
||||
// create the appropriate query parser
|
||||
var queryparse = createQueryParser(options)
|
||||
|
||||
function parse (body, encoding) {
|
||||
return body.length
|
||||
? queryparse(body, encoding)
|
||||
: {}
|
||||
}
|
||||
const parse = createQueryParser(options)
|
||||
|
||||
const readOptions = {
|
||||
...normalizedOptions,
|
||||
@@ -66,13 +60,13 @@ function urlencoded (options) {
|
||||
* @private
|
||||
*/
|
||||
function createQueryParser (options) {
|
||||
var extended = Boolean(options?.extended)
|
||||
var parameterLimit = options?.parameterLimit !== undefined
|
||||
const extended = Boolean(options?.extended)
|
||||
let parameterLimit = options?.parameterLimit !== undefined
|
||||
? options?.parameterLimit
|
||||
: 1000
|
||||
var charsetSentinel = options?.charsetSentinel
|
||||
var interpretNumericEntities = options?.interpretNumericEntities
|
||||
var depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0
|
||||
const charsetSentinel = options?.charsetSentinel
|
||||
const interpretNumericEntities = options?.interpretNumericEntities
|
||||
const depth = extended ? (options?.depth !== undefined ? options?.depth : 32) : 0
|
||||
|
||||
if (isNaN(parameterLimit) || parameterLimit < 1) {
|
||||
throw new TypeError('option parameterLimit must be a positive number')
|
||||
@@ -86,8 +80,10 @@ function createQueryParser (options) {
|
||||
parameterLimit = parameterLimit | 0
|
||||
}
|
||||
|
||||
return function queryparse (body, encoding) {
|
||||
var paramCount = parameterCount(body, parameterLimit)
|
||||
return function parse (body, encoding) {
|
||||
if (!body.length) return {}
|
||||
|
||||
const paramCount = parameterCount(body, parameterLimit)
|
||||
|
||||
if (paramCount === undefined) {
|
||||
debug('too many parameters')
|
||||
@@ -96,7 +92,7 @@ function createQueryParser (options) {
|
||||
})
|
||||
}
|
||||
|
||||
var arrayLimit = extended ? Math.max(100, paramCount) : paramCount
|
||||
const arrayLimit = extended ? Math.max(100, paramCount) : paramCount
|
||||
|
||||
debug('parse ' + (extended ? 'extended ' : '') + 'urlencoding')
|
||||
try {
|
||||
|
||||
+18
-16
@@ -4,9 +4,9 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var bytes = require('bytes')
|
||||
var contentType = require('content-type')
|
||||
var typeis = require('type-is')
|
||||
const bytes = require('bytes')
|
||||
const contentType = require('content-type')
|
||||
const typeis = require('type-is')
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
@@ -25,11 +25,9 @@ module.exports = {
|
||||
* @private
|
||||
*/
|
||||
function getCharset (req) {
|
||||
try {
|
||||
return (contentType.parse(req).parameters.charset || '').toLowerCase()
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
const header = req.headers['content-type']
|
||||
if (!header) return undefined
|
||||
return contentType.parse(header).parameters.charset?.toLowerCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,20 +57,24 @@ function normalizeOptions (options, defaultType) {
|
||||
throw new TypeError('defaultType must be provided')
|
||||
}
|
||||
|
||||
var inflate = options?.inflate !== false
|
||||
var limit = typeof options?.limit !== 'number'
|
||||
? bytes.parse(options?.limit || '100kb')
|
||||
: options?.limit
|
||||
var type = options?.type || defaultType
|
||||
var verify = options?.verify || false
|
||||
var defaultCharset = options?.defaultCharset || 'utf-8'
|
||||
const inflate = options?.inflate !== false
|
||||
const limit = typeof options?.limit === 'undefined' || options?.limit === null
|
||||
? 102400 // 100kb default
|
||||
: bytes.parse(options.limit)
|
||||
const type = options?.type || defaultType
|
||||
const verify = options?.verify || false
|
||||
const defaultCharset = options?.defaultCharset || 'utf-8'
|
||||
|
||||
if (limit === null) {
|
||||
throw new TypeError(`option limit "${String(options.limit)}" is invalid`)
|
||||
}
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
// create the appropriate type checking function
|
||||
var shouldParse = typeof type !== 'function'
|
||||
const shouldParse = typeof type !== 'function'
|
||||
? typeChecker(type)
|
||||
: type
|
||||
|
||||
|
||||
Reference in New Issue
Block a user