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
fix: pipe stream inside error handler #3375
Conversation
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.
LGTM.
Alternative solution (It maybe a Breaking Change): We change the ...
Reply.prototype.removeHeader = function (key) {
// Node.js does not like headers with keys set to undefined,
// so we have to delete the key.
this[kReplyHeaders][key.toLowerCase()] = undefined
return this
}
...
function onSendEnd (reply, payload) {
const res = reply.raw
const req = reply.request
const statusCode = res.statusCode
// we clean up the header at the very last stage
// it can minimize the number of times using `delete`
const keys = Object.keys(reply[kReplyHeaders])
/* eslint-disable no-var */
for (var i = 0; i !== keys.length; ++i) {
const key = keys[i]
if (reply[kReplyHeaders][key] === undefined) delete reply[kReplyHeaders][key]
}
if (payload === undefined || payload === null) {
reply[kReplySent] = true
// according to https://tools.ietf.org/html/rfc7230#section-3.3.2
// we cannot send a content-length for 304 and 204, and all status code
// < 200.
// For HEAD we don't overwrite the `content-length`
if (statusCode >= 200 && statusCode !== 204 && statusCode !== 304 && req.method !== 'HEAD') {
reply[kReplyHeaders]['content-length'] = '0'
}
res.writeHead(statusCode, reply[kReplyHeaders])
// avoid ArgumentsAdaptorTrampoline from V8
res.end(null, null, null)
return
}
if (typeof payload.pipe === 'function') {
reply[kReplySent] = true
sendStream(payload, res, reply)
return
}
if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {
throw new FST_ERR_REP_INVALID_PAYLOAD_TYPE(typeof payload)
}
if (!reply[kReplyHeaders]['content-length']) {
reply[kReplyHeaders]['content-length'] = '' + Buffer.byteLength(payload)
} else if (req.raw.method !== 'HEAD' && reply[kReplyHeaders]['content-length'] !== Buffer.byteLength(payload)) {
reply[kReplyHeaders]['content-length'] = '' + Buffer.byteLength(payload)
}
reply[kReplySent] = true
res.writeHead(statusCode, reply[kReplyHeaders])
// avoid ArgumentsAdaptorTrampoline from V8
res.end(payload, null, 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.
lgtm
I find this solution correct |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Fix when pipe stream inside custom handler, it will cause an error because of
undefined
header being set.This issue is found by
poloniumcz
in Discord Server.Checklist
npm run test
andnpm run benchmark
and the Code of conduct