Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ overrides:
processor: 'markdown/markdown'
rules:
no-param-reassign: error
no-var: error
34 changes: 17 additions & 17 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -126,8 +126,8 @@ function createJsonParser (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)
Expand All @@ -153,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]
Expand All @@ -169,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]
}
Expand Down
6 changes: 3 additions & 3 deletions lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -60,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')
Expand All @@ -83,7 +83,7 @@ function createQueryParser (options) {
return function parse (body, encoding) {
if (!body.length) return {}

var paramCount = parameterCount(body, parameterLimit)
const paramCount = parameterCount(body, parameterLimit)

if (paramCount === undefined) {
debug('too many parameters')
Expand All @@ -92,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 {
Expand Down
20 changes: 10 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,7 +25,7 @@ module.exports = {
* @private
*/
function getCharset (req) {
var header = req.headers['content-type']
const header = req.headers['content-type']
if (!header) return undefined
return contentType.parse(header).parameters.charset?.toLowerCase()
}
Expand Down Expand Up @@ -57,13 +57,13 @@ function normalizeOptions (options, defaultType) {
throw new TypeError('defaultType must be provided')
}

var inflate = options?.inflate !== false
var limit = typeof options?.limit === 'undefined' || options?.limit === null
const inflate = options?.inflate !== false
const limit = typeof options?.limit === 'undefined' || options?.limit === null
? 102400 // 100kb default
: bytes.parse(options.limit)
var type = options?.type || defaultType
var verify = options?.verify || false
var defaultCharset = options?.defaultCharset || 'utf-8'
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`)
Expand All @@ -74,7 +74,7 @@ function normalizeOptions (options, defaultType) {
}

// create the appropriate type checking function
var shouldParse = typeof type !== 'function'
const shouldParse = typeof type !== 'function'
? typeChecker(type)
: type

Expand Down
4 changes: 2 additions & 2 deletions test/body-parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

var assert = require('node:assert')
const assert = require('node:assert')

var bodyParser = require('..')
const bodyParser = require('..')

describe('bodyParser()', function () {
it('should throw an error', function () {
Expand Down
Loading