From 1cfb3281acc34138d734edec14049b02530c0260 Mon Sep 17 00:00:00 2001 From: feugy Date: Tue, 30 Aug 2022 16:33:11 +0200 Subject: [PATCH] fix(fetch): hangs on a stream response with manual redirect --- lib/fetch/index.js | 4 +++- test/fetch/client-fetch.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index c7c88ec40b3..fd82d77ff38 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -1048,7 +1048,9 @@ async function httpFetch (fetchParams) { // and the connection uses HTTP/2, then user agents may, and are even // encouraged to, transmit an RST_STREAM frame. // See, https://github.com/whatwg/fetch/issues/1288 - fetchParams.controller.connection.destroy() + if (request.redirect !== 'manual') { + fetchParams.controller.connection.destroy() + } // 2. Switch on request’s redirect mode: if (request.redirect === 'error') { diff --git a/test/fetch/client-fetch.js b/test/fetch/client-fetch.js index 4ec4545d544..240956df036 100644 --- a/test/fetch/client-fetch.js +++ b/test/fetch/client-fetch.js @@ -304,6 +304,34 @@ test('redirect with body', (t) => { }) }) +test('redirect with stream', (t) => { + t.plan(3) + + const location = '/asd' + const body = 'hello!' + const server = createServer(async (req, res) => { + res.writeHead(302, { location }) + let count = 0 + const l = setInterval(() => { + res.write(body[count++]) + if (count === body.length) { + res.end() + clearInterval(l) + } + }, 50) + }) + t.teardown(server.close.bind(server)) + + server.listen(0, async () => { + const res = await fetch(`http://localhost:${server.address().port}`, { + redirect: 'manual' + }) + t.equal(res.status, 302) + t.equal(res.headers.get('location'), location) + t.equal(await res.text(), body) + }) +}) + test('fail to extract locked body', (t) => { t.plan(1)