Skip to content

Commit

Permalink
http2: make compat finished match http/1
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jan 11, 2020
1 parent 2a94c9f commit cd39f47
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
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

0 comments on commit cd39f47

Please sign in to comment.