From c9acca97ce2300ea78cc84e2f45092e9abf446c4 Mon Sep 17 00:00:00 2001 From: Stefano Gava Date: Mon, 25 Mar 2024 11:16:51 +0100 Subject: [PATCH] fix(H2 Client): bind stream 'data' listener only after received 'response' event (#2985) * fix(fetch): pause stream if data is received before headers in HTTP/2 * Revert "fix(fetch): pause stream if data is received before headers in HTTP/2" This reverts commit cd2eaf465accf2bd6889552865cde0c5bbdc5c41. * fix(H2 Client): pause stream if response data is sent before response event * Revert "fix(H2 Client): pause stream if response data is sent before response event" This reverts commit 9219ee3cf3dd1d23c25936e9a1258a5890388c6f. * fix(H2 Client): bind stream 'data' listener once 'response' is triggered ref: https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options --------- Co-authored-by: Stefano --- 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