-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Are you sure this is an issue with the hapi core module or are you just looking for some help?
Base on my understanding of the doc on h.abandon and the behavior I am able to repro, I believe so.
Is this a security related issue?
No
What are you trying to achieve or the steps to reproduce?
I encountered this issue while trying to use webpack-hot-middleware.
webpack-hot-middleware uses EventStream and established a keep alive connection on the request.raw.res object. In that case, it has taken over the request lifecycle. Instead of leaving Hapi hanging with an unresolved promise, I'd like to return h.abandon.
According to https://hapijs.com/api#h.abandon:
It is the developer's responsibility to write and end the response directly via request.raw.res.
I interpret that as Hapi would not try to do anything with response.
However, I get the following error after returning h.abandon:
(node:10352) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at Object.internals.writeHead (/Users/xchen11/dev/pub/hapi-abandon/node_modules/hapi/lib/transmit.js:323:21)
I was able to repro the same behavior with this simple sample:
"use strict";
const Hapi = require("hapi");
// Create a server with a host and port
const server = Hapi.server({
host: "localhost",
port: 8000
});
server.ext({
type: "onRequest",
method: async (request, h) => {
const res = request.raw.res;
res.setHeader("content-type", "text/plain");
res.end("hello");
return h.abandon;
}
});
// Start the server
async function start() {
try {
await server.start();
} catch (err) {
console.log(err);
process.exit(1);
}
console.log("Server running at:", server.info.uri);
}
start();What was the result you received?
(node:10352) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at Object.internals.writeHead (/Users/xchen11/dev/pub/hapi-abandon/node_modules/hapi/lib/transmit.js:323:21)
What did you expect?
Hapi abandoning the request lifecycle and response and not attempt to set header on the response object.
Context
-
node version:
10.13.0 -
hapi version:
17.7.0 -
os: MacOS High Sierra 10.13.6
-
any other relevant information: