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
http2: compat ERR_STREAM_ALREADY_FINISHED
Make http/2 compat end() match Writable and http/1.
  • Loading branch information
ronag committed Aug 22, 2019
commit 0605719934612becfcf759f4d5fd599f36832f49
24 changes: 18 additions & 6 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const {
ERR_HTTP2_STATUS_INVALID,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
ERR_INVALID_HTTP_TOKEN
ERR_INVALID_HTTP_TOKEN,
ERR_STREAM_ALREADY_FINISHED
},
hideStackFrames
} = require('internal/errors');
Expand Down Expand Up @@ -652,11 +653,6 @@ class Http2ServerResponse extends Stream {
const stream = this[kStream];
const state = this[kState];

if ((state.closed || state.ending) &&
state.headRequest === stream.headRequest) {
return this;
}

if (typeof chunk === 'function') {
cb = chunk;
chunk = null;
Expand All @@ -665,6 +661,22 @@ class Http2ServerResponse extends Stream {
encoding = 'utf8';
}

if (this.writableEnded) {
if (typeof cb === 'function') {
Comment thread
ronag marked this conversation as resolved.
Outdated
if (!this.finished) {
this.on('finish', cb);
} else {
cb(new ERR_STREAM_ALREADY_FINISHED('end'));
}
}
return this;
}

if ((state.closed || state.ending) &&
state.headRequest === stream.headRequest) {
return this;
}

if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);

Expand Down
24 changes: 18 additions & 6 deletions test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
mustCall,
mustNotCall,
expectsError,
hasCrypto,
platformTimeout,
skip
Expand All @@ -25,16 +26,22 @@ const {
// but callback will only be called once
const server = createServer(mustCall((request, response) => {
response.end('end', 'utf8', mustCall(() => {
response.end(mustNotCall());
response.end(mustCall());
process.nextTick(() => {
response.end(mustNotCall());
response.end(expectsError({
code: 'ERR_STREAM_ALREADY_FINISHED'
}));
server.close();
});
}));
response.on('finish', mustCall(() => {
response.end(mustNotCall());
response.end(expectsError({
code: 'ERR_STREAM_ALREADY_FINISHED'
}));
}));
response.end(expectsError({
code: 'ERR_STREAM_ALREADY_FINISHED'
}));
response.end(mustNotCall());
}));
server.listen(0, mustCall(() => {
let data = '';
Expand Down Expand Up @@ -292,7 +299,10 @@ const {
}));
response.end('data', mustCall(() => {
strictEqual(finished, false);
response.end('data', mustNotCall());
strictEqual(response.end('data', expectsError({
code: 'ERR_STREAM_ALREADY_FINISHED'
})), response);
strictEqual(response.end(), response);
}));
}));
server.listen(0, mustCall(() => {
Expand Down Expand Up @@ -326,7 +336,9 @@ const {
// Should be able to respond to HEAD with just .end
const server = createServer(mustCall((request, response) => {
response.end('data', mustCall());
response.end(mustNotCall());
strictEqual(response.end('data', expectsError({
code: 'ERR_STREAM_ALREADY_FINISHED'
})), response);
}));
server.listen(0, mustCall(() => {
const { port } = server.address();
Expand Down