Cars project 3 do dokończenia

This commit is contained in:
aplikacje committed 2026-11-05 10:50:57 +01:00
1 parent 3b96ac1be8
commit b3be59a011
159 files changed
+9794 -3786

No files matched your search

+17 -13
View File
@@ -37,6 +37,10 @@ This module provides the following parsers:
* [Text body parser](#bodyparsertextoptions)
* [URL-encoded form body parser](#bodyparserurlencodedoptions)
All parsers support automatic inflation of `gzip`, `br` (brotli), `deflate`
and `zstd` encodings. `zstd` is available in Node.js `^22.15.0` and
`>=23.8.0`.
Other body parsers you might be interested in:
- [body](https://www.npmjs.com/package/body#readme)
@@ -65,6 +69,12 @@ The `bodyParser` object exposes various factories to create middlewares. All
middlewares will populate the `req.body` property with the parsed body when
the `Content-Type` request header matches the `type` option.
**Note** As of v2, `req.body` is left `undefined` when no body is parsed (for
example when the request has neither `Content-Length` nor `Transfer-Encoding`,
or when `Content-Type` does not match). In v1, `req.body` was always initialized
to `{}`. Code that assumes `req.body` is always an object should use optional
chaining (for example `req.body?.username`) or set a default.
The various errors returned by this module are described in the
[errors section](#errors).
@@ -72,8 +82,7 @@ The various errors returned by this module are described in the
Returns middleware that only parses `json` and only looks at requests where
the `Content-Type` header matches the `type` option. This parser accepts any
Unicode encoding of the body and supports automatic inflation of `gzip`,
`br` (brotli) and `deflate` encodings.
Unicode encoding of the body.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`).
@@ -110,7 +119,7 @@ argument. You can find more information on this argument
##### strict
When set to `true`, will only accept arrays and objects; when `false` will
When set to `true`, it will only accept arrays and objects; when `false`, it will
accept anything `JSON.parse` accepts. Defaults to `true`.
##### type
@@ -133,9 +142,7 @@ encoding of the request. The parsing can be aborted by throwing an error.
### bodyParser.raw([options])
Returns middleware that parses all bodies as a `Buffer` and only looks at
requests where the `Content-Type` header matches the `type` option. This
parser supports automatic inflation of `gzip`, `br` (brotli) and `deflate`
encodings.
requests where the `Content-Type` header matches the `type` option.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This will be a `Buffer` object
@@ -181,9 +188,7 @@ encoding of the request. The parsing can be aborted by throwing an error.
### bodyParser.text([options])
Returns middleware that parses all bodies as a string and only looks at
requests where the `Content-Type` header matches the `type` option. This
parser supports automatic inflation of `gzip`, `br` (brotli) and `deflate`
encodings.
requests where the `Content-Type` header matches the `type` option.
A new `body` string containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This will be a string of the
@@ -234,8 +239,7 @@ encoding of the request. The parsing can be aborted by throwing an error.
Returns middleware that only parses `urlencoded` bodies and only looks at
requests where the `Content-Type` header matches the `type` option. This
parser accepts only UTF-8 and ISO-8859-1 encodings of the body and supports
automatic inflation of `gzip`, `br` (brotli) and `deflate` encodings.
parser accepts only UTF-8 and ISO-8859-1 encodings of the body.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This object will contain
@@ -346,7 +350,7 @@ the entity value that failed parsing.
### entity verify failed
This error will occur when the request contained an entity that could not be
failed verification by the defined `verify` option. The `status` property is
verified by the defined `verify` option. The `status` property is
set to `403`, the `type` property is set to `'entity.verify.failed'`, and the
`body` property is set to the entity value that failed verification.
@@ -413,7 +417,7 @@ property is set to the encoding that is unsupported.
### The input exceeded the depth
This error occurs when using `bodyParser.urlencoded` with the `extended` property set to `true` and the input exceeds the configured `depth` option. The `status` property is set to `400`. It is recommended to review the `depth` option and evaluate if it requires a higher value. When the `depth` option is set to `32` (default value), the error will not be thrown.
This error occurs when using `bodyParser.urlencoded` with the `extended` property set to `true` and the input exceeds the configured `depth` option. The `status` property is set to `400`. It is recommended to review the `depth` option and evaluate if it requires a higher value. With the default `depth` of `32`, an input with 33 or more levels of nesting gets this error.
## Examples
+77 -60
View File
@@ -17,7 +17,7 @@ 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')
const contentType = require('content-type')
/**
* Module exports.
@@ -25,6 +25,12 @@ const { getCharset } = require('./utils')
module.exports = read
/**
* @const
* whether current node version has zstandard support
*/
const hasZstandardSupport = 'createZstdDecompress' in zlib
/**
* Read a request into a buffer and parse.
*
@@ -79,38 +85,53 @@ function read (req, res, next, parse, debug, options) {
}
let length
const opts = options
let stream
// read options
const verify = opts.verify
const verify = options.verify
try {
// get the content stream
stream = contentstream(req, debug, opts.inflate)
length = stream.length
stream.length = undefined
} catch (err) {
return next(err)
}
// set raw-body options
opts.length = length
opts.encoding = verify
? null
: encoding
// assert charset is supported
if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
// assert charset is supported before creating the decompression stream, so an
// unsupported charset cannot leave a decoder piped with no error listener
if (verify && encoding !== null && !iconv.encodingExists(encoding)) {
return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
charset: encoding.toLowerCase(),
type: 'charset.unsupported'
}))
}
try {
const contentEncoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
debug('content-encoding "%s"', contentEncoding)
if (options.inflate === false && contentEncoding !== 'identity') {
throw createError(415, 'content encoding unsupported', {
encoding: contentEncoding,
type: 'encoding.unsupported'
})
}
// get the content stream
if (contentEncoding === 'identity') {
length = req.headers['content-length']
stream = req
} else {
stream = createDecompressionStream(contentEncoding, debug)
req.pipe(stream)
}
} catch (err) {
return next(err)
}
// set raw-body options
const rawBodyOptions = {
length,
encoding: verify ? null : encoding,
limit: options.limit
}
// read body
debug('read body')
getBody(stream, opts, function (error, body) {
getBody(stream, rawBodyOptions, function (error, body) {
if (error) {
let _error
@@ -138,6 +159,17 @@ function read (req, res, next, parse, debug, options) {
return
}
// detach and destroy the decompression stream to
// avoid decompression errors on the finished stream
if (stream !== req) {
req.unpipe(stream)
stream.on('error', () => {})
stream.destroy()
// consume any remaining bytes so the HTTP request can finish
req.resume()
}
// verify
if (verify) {
try {
@@ -145,7 +177,7 @@ function read (req, res, next, parse, debug, options) {
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err, {
body: body,
body,
type: err.type || 'entity.verify.failed'
}))
return
@@ -172,38 +204,6 @@ function read (req, res, next, parse, debug, options) {
})
}
/**
* Get the content stream of the request.
*
* @param {Object} req
* @param {Function} debug
* @param {boolean} inflate
* @returns {Object}
* @private
*/
function contentstream (req, debug, inflate) {
const encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
const length = req.headers['content-length']
debug('content-encoding "%s"', encoding)
if (inflate === false && encoding !== 'identity') {
throw createError(415, 'content encoding unsupported', {
encoding: encoding,
type: 'encoding.unsupported'
})
}
if (encoding === 'identity') {
req.length = length
return req
}
const stream = createDecompressionStream(encoding, debug)
req.pipe(stream)
return stream
}
/**
* Create a decompression stream for the given encoding.
* @param {string} encoding
@@ -222,12 +222,16 @@ function createDecompressionStream (encoding, debug) {
case 'br':
debug('brotli decompress body')
return zlib.createBrotliDecompress()
default:
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
encoding: encoding,
type: 'encoding.unsupported'
})
case 'zstd':
if (hasZstandardSupport) {
debug('zstd decompress body')
return zlib.createZstdDecompress()
}
}
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
encoding,
type: 'encoding.unsupported'
})
}
/**
@@ -245,3 +249,16 @@ function dump (req, callback) {
req.resume()
}
}
/**
* Get the charset of a request.
*
* @param {Object} req
* @returns {string | undefined}
* @private
*/
function getCharset (req) {
const header = req.headers['content-type']
if (!header) return undefined
return contentType.parse(header).parameters.charset?.toLowerCase()
}
+11 -11
View File
@@ -23,7 +23,7 @@ const { normalizeOptions } = require('../utils')
module.exports = json
/**
* RegExp to match the first non-space in a string.
* RegExp to match the first non-whitespace character in a string.
*
* Allowed whitespace is defined in RFC 7159:
*
@@ -33,7 +33,7 @@ module.exports = json
* %x0A / ; Line feed or New line
* %x0D ) ; Carriage return
*/
const FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
const FIRST_NON_WHITESPACE_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d]|$)/ // eslint-disable-line no-control-regex
const JSON_SYNTAX_CHAR = '#'
const JSON_SYNTAX_REGEXP = /#+/g
@@ -80,10 +80,12 @@ function createJsonParser (options) {
return {}
}
const first = firstchar(body)
if (first !== '{' && first !== '[') {
debug('strict violation')
throw createStrictSyntaxError(body, first)
if (body[0] !== '{' && body[0] !== '[') {
const first = firstNonWhitespaceChar(body)
if (first !== '{' && first !== '[') {
debug('strict violation')
throw createStrictSyntaxError(body, first)
}
}
try {
@@ -152,12 +154,10 @@ function createStrictSyntaxError (str, char) {
* @returns {string|undefined}
* @private
*/
function firstchar (str) {
const match = FIRST_CHAR_REGEXP.exec(str)
function firstNonWhitespaceChar (str) {
const match = FIRST_NON_WHITESPACE_REGEXP.exec(str)
return match
? match[1]
: undefined
return match && match[1] ? match[1] : undefined
}
/**
+5 -5
View File
@@ -98,12 +98,12 @@ function createQueryParser (options) {
try {
return qs.parse(body, {
allowPrototypes: true,
arrayLimit: arrayLimit,
depth: depth,
charsetSentinel: charsetSentinel,
interpretNumericEntities: interpretNumericEntities,
arrayLimit,
depth,
charsetSentinel,
interpretNumericEntities,
charset: encoding,
parameterLimit: parameterLimit,
parameterLimit,
strictDepth: true
})
} catch (err) {
-15
View File
@@ -5,31 +5,16 @@
*/
const bytes = require('bytes')
const contentType = require('content-type')
const typeis = require('type-is')
/**
* Module exports.
*/
module.exports = {
getCharset,
normalizeOptions,
passthrough
}
/**
* Get the charset of a request.
*
* @param {Object} req
* @returns {string | undefined}
* @private
*/
function getCharset (req) {
const header = req.headers['content-type']
if (!header) return undefined
return contentType.parse(header).parameters.charset?.toLowerCase()
}
/**
* Get the simple type checker.
*
+7 -12
View File
@@ -1,7 +1,7 @@
{
"name": "body-parser",
"description": "Node.js body parsing middleware",
"version": "2.3.0",
"version": "2.4.0",
"contributors": [
"Douglas Christopher Wilson <[email protected]>",
"Jonathan Ong <[email protected]> (http://jongleberry.com)"
@@ -27,26 +27,21 @@
},
"dependencies": {
"bytes": "^3.1.2",
"content-type": "^2.0.0",
"content-type": "^2.1.0",
"debug": "^4.4.3",
"http-errors": "^2.0.1",
"iconv-lite": "^0.7.2",
"iconv-lite": "^0.7.3",
"on-finished": "^2.4.1",
"qs": "^6.15.2",
"qs": "^6.16.1",
"raw-body": "^3.0.2",
"type-is": "^2.1.0"
},
"devDependencies": {
"eslint": "^8.57.1",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-markdown": "^3.0.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.6.0",
"eslint-plugin-standard": "^4.1.0",
"eslint": "^9.39.5",
"mocha": "^11.7.6",
"neostandard": "^0.13.0",
"nyc": "^17.1.0",
"supertest": "^7.2.2"
"supertest": "7.3.0"
},
"files": [
"lib/",