Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,8 @@ class Http2ServerResponse extends Stream {
}

get finished() {
const stream = this[kStream];
return stream.destroyed ||
stream._writableState.ended ||
this[kState].closed;
const state = this[kState];
return state.ending;
}

get socket() {
Expand Down Expand Up @@ -700,12 +698,11 @@ class Http2ServerResponse extends Stream {
if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);

const isFinished = this.finished;
state.headRequest = stream.headRequest;
state.ending = true;

if (typeof cb === 'function') {
if (isFinished)
if (stream.writableEnded)
this.once('finish', cb);
else
stream.once('finish', cb);
Expand All @@ -714,7 +711,7 @@ class Http2ServerResponse extends Stream {
if (!stream.headersSent)
this.writeHead(this[kState].statusCode);

if (isFinished)
if (this[kState].closed || stream.destroyed)
onStreamCloseResponse.call(stream);
else
stream.end();
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ const {
// Http2ServerResponse.end is necessary on HEAD requests in compat
// for http1 compatibility
const server = createServer(mustCall((request, response) => {
strictEqual(response.finished, true);
strictEqual(response.writableEnded, false);
strictEqual(response.finished, false);
response.writeHead(HTTP_STATUS_OK, { foo: 'bar' });
strictEqual(response.finished, false);
response.end('data', mustCall());
strictEqual(response.writableEnded, true);
strictEqual(response.finished, true);
}));
server.listen(0, mustCall(() => {
const { port } = server.address();
Expand Down