-
-
Notifications
You must be signed in to change notification settings - Fork 744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: optimize internal read function #587
base: master
Are you sure you want to change the base?
Conversation
stream.length = undefined | ||
} catch (err) { | ||
return next(err) | ||
const charset = options.charset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed the option to charset
because it holds the content-type
header charset (Content-Type: text/plain; charset=utf-8
) or the default charset
/** | ||
* Get the content stream of the request. | ||
* | ||
* @param {object} req | ||
* @param {function} debug | ||
* @param {boolean} [inflate=true] | ||
* @return {object} | ||
* @api private | ||
*/ | ||
|
||
function contentstream (req, debug, inflate) { | ||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase() | ||
var 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 | ||
} | ||
|
||
var stream = createDecompressionStream(encoding, debug) | ||
req.pipe(stream) | ||
return stream | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be seen as follow-up to #564. I included the contentstream
function in the main read
function for multiple reasons:
- to prevent the
req
object from being altered by settinglength
and resetting it inread
. - This also improves code by only setting the
raw-body
length
option and thus also only callingreq.headers['content-length']
when the requests needs no inflation.
if (contentEncoding === 'identity') { | ||
// set raw-body expected length | ||
stream = req | ||
options.length = req.headers['content-length'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improves the code by only accessing req.headers['content-length']
and setting the length
option for raw-body
when the stream needs no decompression
debug: debug, | ||
encoding: charset, | ||
inflate: inflate, | ||
limit: limit, | ||
verify: verify, | ||
charsetSentinel: charsetSentinel, | ||
interpretNumericEntities: interpretNumericEntities | ||
charset, | ||
inflate, | ||
limit, | ||
verify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need the 4 options here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve added comments to explain my changes. The diffs don’t fully capture the modifications clearly, so I’d recommend comparing the file from master with the version in this branch for better context.
Let me know if you have any questions or need further clarifications. Looking forward to your feedback! 😊
var encoding = opts.encoding !== null | ||
? opts.encoding | ||
: null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove that 😅
This PR aims to improve the maintainability and readability of the internal read function. It also removes some weird parts of that code.