From cd2eaf465accf2bd6889552865cde0c5bbdc5c41 Mon Sep 17 00:00:00 2001 From: Stefano Date: Fri, 22 Mar 2024 18:42:25 +0100 Subject: [PATCH 1/5] fix(fetch): pause stream if data is received before headers in HTTP/2 --- lib/web/fetch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/index.js b/lib/web/fetch/index.js index d8c20c59bf7..a3654271183 100644 --- a/lib/web/fetch/index.js +++ b/lib/web/fetch/index.js @@ -2230,7 +2230,7 @@ async function httpNetworkFetch ( // 4. See pullAlgorithm... - return this.body.push(bytes) + return this.body ? this.body.push(bytes) : false }, onComplete () { From cf6a5ea81b335cc142cde3dc40fa6a263eba3a54 Mon Sep 17 00:00:00 2001 From: Stefano Gava Date: Sat, 23 Mar 2024 18:22:24 +0100 Subject: [PATCH 2/5] Revert "fix(fetch): pause stream if data is received before headers in HTTP/2" This reverts commit cd2eaf465accf2bd6889552865cde0c5bbdc5c41. --- lib/web/fetch/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/index.js b/lib/web/fetch/index.js index a3654271183..d8c20c59bf7 100644 --- a/lib/web/fetch/index.js +++ b/lib/web/fetch/index.js @@ -2230,7 +2230,7 @@ async function httpNetworkFetch ( // 4. See pullAlgorithm... - return this.body ? this.body.push(bytes) : false + return this.body.push(bytes) }, onComplete () { From 9219ee3cf3dd1d23c25936e9a1258a5890388c6f Mon Sep 17 00:00:00 2001 From: Stefano Gava Date: Sat, 23 Mar 2024 21:05:29 +0100 Subject: [PATCH 3/5] fix(H2 Client): pause stream if response data is sent before response event --- lib/dispatcher/client-h2.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/dispatcher/client-h2.js b/lib/dispatcher/client-h2.js index 8155d6e226a..c824ea9becb 100644 --- a/lib/dispatcher/client-h2.js +++ b/lib/dispatcher/client-h2.js @@ -253,6 +253,7 @@ function writeH2 (client, request) { /** @type {import('node:http2').ClientHttp2Stream} */ let stream + let isStreamResponseStarted = false const { hostname, port } = client[kUrl] @@ -388,6 +389,7 @@ function writeH2 (client, request) { ++session[kOpenStreams] stream.once('response', headers => { + isStreamResponseStarted = true const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers request.onResponseStarted() @@ -419,7 +421,7 @@ function writeH2 (client, request) { }) stream.on('data', (chunk) => { - if (request.onData(chunk) === false) { + if (!isStreamResponseStarted || request.onData(chunk) === false) { stream.pause() } }) From fb8d568883d71f31082d3897481b58f48f0c806d Mon Sep 17 00:00:00 2001 From: Stefano Gava Date: Sun, 24 Mar 2024 18:18:13 +0100 Subject: [PATCH 4/5] Revert "fix(H2 Client): pause stream if response data is sent before response event" This reverts commit 9219ee3cf3dd1d23c25936e9a1258a5890388c6f. --- lib/dispatcher/client-h2.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/dispatcher/client-h2.js b/lib/dispatcher/client-h2.js index c824ea9becb..8155d6e226a 100644 --- a/lib/dispatcher/client-h2.js +++ b/lib/dispatcher/client-h2.js @@ -253,7 +253,6 @@ function writeH2 (client, request) { /** @type {import('node:http2').ClientHttp2Stream} */ let stream - let isStreamResponseStarted = false const { hostname, port } = client[kUrl] @@ -389,7 +388,6 @@ function writeH2 (client, request) { ++session[kOpenStreams] stream.once('response', headers => { - isStreamResponseStarted = true const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers request.onResponseStarted() @@ -421,7 +419,7 @@ function writeH2 (client, request) { }) stream.on('data', (chunk) => { - if (!isStreamResponseStarted || request.onData(chunk) === false) { + if (request.onData(chunk) === false) { stream.pause() } }) From 6c661b3c62ce59fc35c8ebac125de67f382c1ef0 Mon Sep 17 00:00:00 2001 From: Stefano Gava Date: Sun, 24 Mar 2024 18:24:47 +0100 Subject: [PATCH 5/5] fix(H2 Client): bind stream 'data' listener once 'response' is triggered ref: https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options --- lib/dispatcher/client-h2.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/dispatcher/client-h2.js b/lib/dispatcher/client-h2.js index 8155d6e226a..d593eae4fca 100644 --- a/lib/dispatcher/client-h2.js +++ b/lib/dispatcher/client-h2.js @@ -394,6 +394,12 @@ function writeH2 (client, request) { if (request.onHeaders(Number(statusCode), realHeaders, stream.resume.bind(stream), '') === false) { stream.pause() } + + stream.on('data', (chunk) => { + if (request.onData(chunk) === false) { + stream.pause() + } + }) }) stream.once('end', () => { @@ -418,12 +424,6 @@ function writeH2 (client, request) { util.destroy(stream, err) }) - stream.on('data', (chunk) => { - if (request.onData(chunk) === false) { - stream.pause() - } - }) - stream.once('close', () => { session[kOpenStreams] -= 1 // TODO(HTTP/2): unref only if current streams count is 0