Skip to content

Commit 6a04edc

Browse files
authoredDec 26, 2023
fix(fetch): do not abort fetch on redirect (#2545)
1 parent 4ed060f commit 6a04edc

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed
 

‎lib/fetch/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ async function httpFetch (fetchParams) {
12061206
// encouraged to, transmit an RST_STREAM frame.
12071207
// See, https://github.com/whatwg/fetch/issues/1288
12081208
if (request.redirect !== 'manual') {
1209-
fetchParams.controller.connection.destroy()
1209+
fetchParams.controller.connection.destroy(undefined, false)
12101210
}
12111211

12121212
// 2. Switch on request’s redirect mode:
@@ -1718,10 +1718,12 @@ async function httpNetworkFetch (
17181718
fetchParams.controller.connection = {
17191719
abort: null,
17201720
destroyed: false,
1721-
destroy (err) {
1721+
destroy (err, abort = true) {
17221722
if (!this.destroyed) {
17231723
this.destroyed = true
1724-
this.abort?.(err ?? new DOMException('The operation was aborted.', 'AbortError'))
1724+
if (abort) {
1725+
this.abort?.(err ?? new DOMException('The operation was aborted.', 'AbortError'))
1726+
}
17251727
}
17261728
}
17271729
}

‎test/fetch/redirect.js

+26
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,29 @@ test('Redirecting with an empty body does not throw an error - #2027', async (t)
4848
t.equal(await resp.text(), '/redirect/')
4949
t.ok(resp.redirected)
5050
})
51+
52+
test('Redirecting with a body does not fail to write body - #2543', async (t) => {
53+
const server = createServer((req, res) => {
54+
if (req.url === '/redirect') {
55+
res.writeHead(307, { location: '/target' })
56+
res.write('<a href="/redirect/">Moved Permanently</a>')
57+
setTimeout(() => res.end(), 500)
58+
} else {
59+
let body = ''
60+
req.on('data', (chunk) => { body += chunk })
61+
req.on('end', () => t.equals(body, 'body'))
62+
res.write('ok')
63+
res.end()
64+
}
65+
}).listen(0)
66+
67+
t.teardown(server.close.bind(server))
68+
await once(server, 'listening')
69+
70+
const resp = await fetch(`http://localhost:${server.address().port}/redirect`, {
71+
method: 'POST',
72+
body: 'body'
73+
})
74+
t.equal(await resp.text(), 'ok')
75+
t.ok(resp.redirected)
76+
})

0 commit comments

Comments
 (0)
Please sign in to comment.