GeezerVison is showing github.com — http2: emit timeout on compat request and response · nodejs/node@348cde0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/compat.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -229,6 +229,13 @@ function onStreamCloseRequest() { | 229 | 229 | req.emit('close'); | 230 | 230 | } | 231 | 231 |
| | 232 | +function onStreamTimeout(kind) { | | 233 | + return function onStreamTimeout() { | | 234 | + const obj = this[kind]; | | 235 | + obj.emit('timeout'); | | 236 | + }; | | 237 | +} | | 238 | + | 232 | 239 | class Http2ServerRequest extends Readable { | 233 | 240 | constructor(stream, headers, options, rawHeaders) { | 234 | 241 | super(options); | @@ -251,6 +258,7 @@ class Http2ServerRequest extends Readable { | 251 | 258 | stream.on('error', onStreamError); | 252 | 259 | stream.on('aborted', onStreamAbortedRequest); | 253 | 260 | stream.on('close', onStreamCloseRequest); | | 261 | + stream.on('timeout', onStreamTimeout(kRequest)); | 254 | 262 | this.on('pause', onRequestPause); | 255 | 263 | this.on('resume', onRequestResume); | 256 | 264 | } | @@ -403,6 +411,7 @@ class Http2ServerResponse extends Stream { | 403 | 411 | stream.on('aborted', onStreamAbortedResponse); | 404 | 412 | stream.on('close', onStreamCloseResponse); | 405 | 413 | stream.on('wantTrailers', onStreamTrailersReady); | | 414 | + stream.on('timeout', onStreamTimeout(kResponse)); | 406 | 415 | } | 407 | 416 |
| 408 | 417 | // User land modules such as finalhandler just check truthiness of this | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-compat-serverrequest-settimeout.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,13 +6,15 @@ if (!common.hasCrypto) | 6 | 6 | const assert = require('assert'); | 7 | 7 | const http2 = require('http2'); | 8 | 8 |
| 9 | | -const msecs = common.platformTimeout(1); | | 9 | +// Set the timeout to 10ms since ending the response stream resets the timer. | | 10 | +const msecs = common.platformTimeout(10); | 10 | 11 | const server = http2.createServer(); | 11 | 12 |
| 12 | 13 | server.on('request', (req, res) => { | 13 | 14 | req.setTimeout(msecs, common.mustCall(() => { | 14 | 15 | res.end(); | 15 | 16 | })); | | 17 | + res.on('timeout', common.mustCall()); | 16 | 18 | res.on('finish', common.mustCall(() => { | 17 | 19 | req.setTimeout(msecs, common.mustNotCall()); | 18 | 20 | process.nextTick(() => { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-compat-serverresponse-settimeout.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,13 +6,15 @@ if (!common.hasCrypto) | 6 | 6 | const assert = require('assert'); | 7 | 7 | const http2 = require('http2'); | 8 | 8 |
| 9 | | -const msecs = common.platformTimeout(1); | | 9 | +// Set the timeout to 10ms since ending the response stream resets the timer. | | 10 | +const msecs = common.platformTimeout(10); | 10 | 11 | const server = http2.createServer(); | 11 | 12 |
| 12 | 13 | server.on('request', (req, res) => { | 13 | 14 | res.setTimeout(msecs, common.mustCall(() => { | 14 | 15 | res.end(); | 15 | 16 | })); | | 17 | + res.on('timeout', common.mustCall()); | 16 | 18 | res.on('finish', common.mustCall(() => { | 17 | 19 | res.setTimeout(msecs, common.mustNotCall()); | 18 | 20 | process.nextTick(() => { | |
|