From 348cde07fd46af3aff58f03597e42f9d5af52844 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 10 Aug 2018 13:33:46 -0700 Subject: [PATCH] http2: emit timeout on compat request and response v8.x Backport Note: The timeout has been increased to 10ms. Fixes: https://github.com/nodejs/node/issues/20079 Backport-PR-URL: https://github.com/nodejs/node/pull/22850 PR-URL: https://github.com/nodejs/node/pull/22252 Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/http2/compat.js | 9 +++++++++ .../test-http2-compat-serverrequest-settimeout.js | 4 +++- .../test-http2-compat-serverresponse-settimeout.js | 4 +++- 3 files changed, 15 insertions(+), 2 deletions(-) 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..668ff891ef5555 100644 --- a/test/parallel/test-http2-compat-serverrequest-settimeout.js +++ b/test/parallel/test-http2-compat-serverrequest-settimeout.js @@ -6,13 +6,15 @@ if (!common.hasCrypto) const assert = require('assert'); const http2 = require('http2'); -const msecs = common.platformTimeout(1); +// Set the timeout to 10ms since ending the response stream resets the timer. +const msecs = common.platformTimeout(10); const server = http2.createServer(); server.on('request', (req, res) => { req.setTimeout(msecs, common.mustCall(() => { res.end(); })); + res.on('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..03cef357056069 100644 --- a/test/parallel/test-http2-compat-serverresponse-settimeout.js +++ b/test/parallel/test-http2-compat-serverresponse-settimeout.js @@ -6,13 +6,15 @@ if (!common.hasCrypto) const assert = require('assert'); const http2 = require('http2'); -const msecs = common.platformTimeout(1); +// Set the timeout to 10ms since ending the response stream resets the timer. +const msecs = common.platformTimeout(10); const server = http2.createServer(); server.on('request', (req, res) => { res.setTimeout(msecs, common.mustCall(() => { res.end(); })); + res.on('timeout', common.mustCall()); res.on('finish', common.mustCall(() => { res.setTimeout(msecs, common.mustNotCall()); process.nextTick(() => {