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

+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.
*