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
9 changes: 8 additions & 1 deletion benchmark/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ const fs = require('fs');
const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');

const bench = common.createBenchmark(main, {
response: ['end', 'pipe'],
size: [64],
requests: [100, 1000, 5000],
streams: [1, 10, 20, 40, 100, 200],
clients: [2],
benchmarker: ['test-double-http2'],
duration: 5,
}, { flags: ['--no-warnings'] });

function main({ requests, streams, clients, duration }) {
function main({ response, size, requests, streams, clients, duration }) {
const http2 = require('http2');
const body = 'a'.repeat(size);
const server = http2.createServer();
server.on('request', (req, res) => {
if (response === 'end') {
res.end(body);
return;
}
const out = fs.createReadStream(file);
res.setHeader('content-type', 'text/html');
out.pipe(res);
Expand Down
36 changes: 29 additions & 7 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,26 +864,45 @@ class Http2ServerResponse extends Stream {
// skipped entirely when none have been registered.
state.finishing = true;

if (chunk !== null && chunk !== undefined)
const hasChunk = chunk !== null && chunk !== undefined;
const endWithChunk = hasChunk &&
this.write === Http2ServerResponsePrototypeWrite;
if (endWithChunk) {
if (!stream.headersSent)
this.writeHead(state.statusCode);
} else if (hasChunk) {
this.write(chunk, encoding);
}

const previousHeadRequest = state.headRequest;
const previousEnding = state.ending;
state.headRequest = stream.headRequest;
state.ending = true;

let finishTarget;
if (typeof cb === 'function') {
if (stream.writableEnded)
this.once('finish', cb);
else
stream.once('finish', cb);
finishTarget = stream.writableEnded ? this : stream;
finishTarget.once('finish', cb);
}

if (!stream.headersSent)
this.writeHead(this[kState].statusCode);

if (this[kState].closed || stream.destroyed)
onStreamCloseResponse.call(stream);
else
stream.end();
else {
try {
if (endWithChunk)
stream.end(chunk, encoding);
else
stream.end();
} catch (err) {
state.ending = previousEnding;
state.headRequest = previousHeadRequest;
finishTarget?.removeListener('finish', cb);
throw err;
}
}

return this;
}
Expand Down Expand Up @@ -998,6 +1017,9 @@ class Http2ServerResponse extends Stream {
}
}

// Preserve user-defined write behavior when end() receives a final chunk.
const Http2ServerResponsePrototypeWrite = Http2ServerResponse.prototype.write;

function onServerStream(ServerRequest, ServerResponse,
stream, headers, flags, rawHeaders) {
const server = this;
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ const {
// It may be invoked repeatedly without throwing errors
// but callback will only be called once
const server = createServer(mustCall((request, response) => {
const stream = response.stream;
const streamEnd = stream.end;
stream.write = mustNotCall();
stream.end = mustCall(function(chunk, encoding) {
assert.strictEqual(chunk, 'end');
assert.strictEqual(encoding, 'utf8');
return streamEnd.call(this, chunk, encoding);
});
response.end('end', 'utf8', mustCall(() => {
response.end(mustCall());
process.nextTick(() => {
Expand Down
13 changes: 10 additions & 3 deletions test/parallel/test-http2-options-server-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');

class MyServerResponse extends h2.Http2ServerResponse {
status(code) {
return this.writeHead(code, { 'Content-Type': 'text/plain' });
}

write(...args) {
this.writeCalled = true;
return super.write(...args);
}
}

const server = h2.createServer({
Http2ServerResponse: MyServerResponse
}, (req, res) => {
}, common.mustCall((req, res) => {
res.status(200);
res.end();
});
res.end('body');
assert.strictEqual(res.writeCalled, true);
}));
server.listen(0);

server.on('listening', common.mustCall(() => {
Expand Down
Loading