From 6077aa10ed56e6eb58ff2cb9cdd75add67b87afe Mon Sep 17 00:00:00 2001 From: inoway46 Date: Wed, 25 Nov 2026 20:49:55 +0900 Subject: [PATCH] http2: add signal to compat request Add `request.signal` to `Http2ServerRequest`, matching `message.signal` in HTTP/1. The signal follows the stream, not the socket: it is aborted when the stream closes before the response has been sent completely. Refs: https://github.com/nodejs/node/pull/62541 Refs: https://github.com/nodejs/node/pull/64392 Signed-off-by: inoway46 Assisted-by: Claude Code --- doc/api/http2.md | 47 +++ lib/internal/http2/compat.js | 36 ++ lib/internal/http2/core.js | 20 ++ lib/internal/http2/util.js | 10 + .../test-http2-compat-serverrequest-signal.js | 337 ++++++++++++++++++ 5 files changed, 450 insertions(+) create mode 100644 test/parallel/test-http2-compat-serverrequest-signal.js diff --git a/doc/api/http2.md b/doc/api/http2.md index 03f971a07542..adc4446d686e 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -4490,6 +4490,53 @@ the server, then [`Http2Stream`][]s are destroyed when they time out. If a handler is assigned to the request, the response, or the server's `'timeout'` events, timed out sockets must be handled explicitly. +#### `request.signal` + + + +* Type: {AbortSignal} + +An {AbortSignal} that is aborted when the underlying [`Http2Stream`][] is +closed before the response has been sent completely. Other streams on the +same session do not affect the signal. +The signal is created lazily on first access — no {AbortController} is +allocated for requests that never use this property. + +This is useful for cancelling downstream asynchronous work such as database +queries or `fetch` calls when a client cancels the request. + +```mjs +import http2 from 'node:http2'; + +http2.createServer(async (req, res) => { + try { + const data = await fetch('https://example.com/api', { signal: req.signal }); + res.end(JSON.stringify(await data.json())); + } catch (err) { + if (err.name === 'AbortError') return; + res.statusCode = 500; + res.end('Internal Server Error'); + } +}).listen(3000); +``` + +```cjs +const http2 = require('node:http2'); + +http2.createServer(async (req, res) => { + try { + const data = await fetch('https://example.com/api', { signal: req.signal }); + res.end(JSON.stringify(await data.json())); + } catch (err) { + if (err.name === 'AbortError') return; + res.statusCode = 500; + res.end('Internal Server Error'); + } +}).listen(3000); +``` + #### `request.socket`