diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 173fa3429bec52..06d283a4b8bbea 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -229,6 +229,13 @@ function onStreamCloseRequest() { req.emit('close'); } +function onStreamTimeout(kind) { + return function onStreamTimeout() { + const obj = this[kind]; + obj.emit('timeout'); + }; +} + class Http2ServerRequest extends Readable { constructor(stream, headers, options, rawHeaders) { super(options); @@ -251,6 +258,7 @@ class Http2ServerRequest extends Readable { stream.on('error', onStreamError); stream.on('aborted', onStreamAbortedRequest); stream.on('close', onStreamCloseRequest); + stream.on('timeout', onStreamTimeout(kRequest)); this.on('pause', onRequestPause); this.on('resume', onRequestResume); } @@ -403,6 +411,7 @@ class Http2ServerResponse extends Stream { stream.on('aborted', onStreamAbortedResponse); stream.on('close', onStreamCloseResponse); stream.on('wantTrailers', onStreamTrailersReady); + stream.on('timeout', onStreamTimeout(kResponse)); } // User land modules such as finalhandler just check truthiness of this diff --git a/test/parallel/test-http2-compat-serverrequest-settimeout.js b/test/parallel/test-http2-compat-serverrequest-settimeout.js index f7189161802301..e7cad0975621dd 100644 --- a/test/parallel/test-http2-compat-serverrequest-settimeout.js +++ b/test/parallel/test-http2-compat-serverrequest-settimeout.js @@ -13,6 +13,11 @@ server.on('request', (req, res) => { req.setTimeout(msecs, common.mustCall(() => { res.end(); })); + // Note: The timeout is reset in the call to res.end() above. The 'timeout' + // event listener might be called more than once if it takes more than 1ms + // to close, so use the `once` event listener here. (This is more likely to + // happen in Node 8 than 10.) + res.once('timeout', common.mustCall()); res.on('finish', common.mustCall(() => { req.setTimeout(msecs, common.mustNotCall()); process.nextTick(() => { diff --git a/test/parallel/test-http2-compat-serverresponse-settimeout.js b/test/parallel/test-http2-compat-serverresponse-settimeout.js index bb09633727ccf7..22a9659633c0b1 100644 --- a/test/parallel/test-http2-compat-serverresponse-settimeout.js +++ b/test/parallel/test-http2-compat-serverresponse-settimeout.js @@ -13,6 +13,11 @@ server.on('request', (req, res) => { res.setTimeout(msecs, common.mustCall(() => { res.end(); })); + // Note: The timeout is reset in the call to res.end() above. The 'timeout' + // event listener might be called more than once if it takes more than 1ms + // to close, so use the `once` event listener here. (This is more likely to + // happen in Node 8 than 10.) + res.once('timeout', common.mustCall()); res.on('finish', common.mustCall(() => { res.setTimeout(msecs, common.mustNotCall()); process.nextTick(() => {