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()
}