Skip to content

Commit 89a340f

Browse files
authored
fix(fetch): hangs on a stream response with manual redirect (nodejs#1627)
1 parent 03cfc4b commit 89a340f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/fetch/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,9 @@ async function httpFetch (fetchParams) {
10481048
// and the connection uses HTTP/2, then user agents may, and are even
10491049
// encouraged to, transmit an RST_STREAM frame.
10501050
// See, https://github.com/whatwg/fetch/issues/1288
1051-
fetchParams.controller.connection.destroy()
1051+
if (request.redirect !== 'manual') {
1052+
fetchParams.controller.connection.destroy()
1053+
}
10521054

10531055
// 2. Switch on request’s redirect mode:
10541056
if (request.redirect === 'error') {

test/fetch/client-fetch.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,34 @@ test('redirect with body', (t) => {
304304
})
305305
})
306306

307+
test('redirect with stream', (t) => {
308+
t.plan(3)
309+
310+
const location = '/asd'
311+
const body = 'hello!'
312+
const server = createServer(async (req, res) => {
313+
res.writeHead(302, { location })
314+
let count = 0
315+
const l = setInterval(() => {
316+
res.write(body[count++])
317+
if (count === body.length) {
318+
res.end()
319+
clearInterval(l)
320+
}
321+
}, 50)
322+
})
323+
t.teardown(server.close.bind(server))
324+
325+
server.listen(0, async () => {
326+
const res = await fetch(`http://localhost:${server.address().port}`, {
327+
redirect: 'manual'
328+
})
329+
t.equal(res.status, 302)
330+
t.equal(res.headers.get('location'), location)
331+
t.equal(await res.text(), body)
332+
})
333+
})
334+
307335
test('fail to extract locked body', (t) => {
308336
t.plan(1)
309337

0 commit comments

Comments
 (0)