Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch): do not abort fetch on redirect #2545

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/fetch/index.js
Expand Up @@ -1206,7 +1206,7 @@ async function httpFetch (fetchParams) {
// encouraged to, transmit an RST_STREAM frame.
// See, https://github.com/whatwg/fetch/issues/1288
if (request.redirect !== 'manual') {
fetchParams.controller.connection.destroy()
fetchParams.controller.connection.destroy(undefined, false)
}

// 2. Switch on request’s redirect mode:
Expand Down Expand Up @@ -1718,10 +1718,12 @@ async function httpNetworkFetch (
fetchParams.controller.connection = {
abort: null,
destroyed: false,
destroy (err) {
destroy (err, abort = true) {
if (!this.destroyed) {
this.destroyed = true
this.abort?.(err ?? new DOMException('The operation was aborted.', 'AbortError'))
if (abort) {
this.abort?.(err ?? new DOMException('The operation was aborted.', 'AbortError'))
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/fetch/redirect.js
Expand Up @@ -48,3 +48,29 @@ test('Redirecting with an empty body does not throw an error - #2027', async (t)
t.equal(await resp.text(), '/redirect/')
t.ok(resp.redirected)
})

test('Redirecting with a body does not fail to write body - #2543', async (t) => {
const server = createServer((req, res) => {
if (req.url === '/redirect') {
res.writeHead(307, { location: '/target' })
res.write('<a href="/redirect/">Moved Permanently</a>')
setTimeout(() => res.end(), 500)
} else {
let body = ''
req.on('data', (chunk) => { body += chunk })
req.on('end', () => t.equals(body, 'body'))
res.write('ok')
res.end()
}
}).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

const resp = await fetch(`http://localhost:${server.address().port}/redirect`, {
method: 'POST',
body: 'body'
})
t.equal(await resp.text(), 'ok')
t.ok(resp.redirected)
})