Skip to content

Commit

Permalink
fix(fetch): do not abort fetch on redirect (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelyan committed Dec 26, 2023
1 parent 4ed060f commit 6a04edc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
})

0 comments on commit 6a04edc

Please sign in to comment.