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: follow signal.reason in Request #1580

Merged
merged 2 commits into from Jul 27, 2022
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
10 changes: 5 additions & 5 deletions lib/fetch/request.js
Expand Up @@ -367,9 +367,9 @@ class Request {
}

if (signal.aborted) {
ac.abort()
ac.abort(signal.reason)
} else {
const abort = () => ac.abort()
const abort = () => ac.abort(signal.reason)
signal.addEventListener('abort', abort, { once: true })
requestFinalizer.register(this, { signal, abort })
}
Expand Down Expand Up @@ -726,12 +726,12 @@ class Request {
// 4. Make clonedRequestObject’s signal follow this’s signal.
const ac = new AbortController()
if (this.signal.aborted) {
ac.abort()
ac.abort(this.signal.reason)
} else {
this.signal.addEventListener(
'abort',
function () {
ac.abort()
() => {
ac.abort(this.signal.reason)
},
{ once: true }
)
Expand Down
27 changes: 21 additions & 6 deletions test/fetch/request.js
Expand Up @@ -8,6 +8,7 @@ const {
Headers
} = require('../../')
const { kState } = require('../../lib/fetch/symbols.js')
const hasSignalReason = !!~process.version.localeCompare('v16.14.0', undefined, { numeric: true })

test('arg validation', async (t) => {
// constructor
Expand Down Expand Up @@ -270,9 +271,12 @@ test('undefined signal', t => {

test('pre aborted signal', t => {
const ac = new AbortController()
ac.abort()
ac.abort('gwak')
const req = new Request('http://asd', { signal: ac.signal })
t.equal(req.signal.aborted, true)
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
}
t.end()
})

Expand All @@ -283,16 +287,23 @@ test('post aborted signal', t => {
const req = new Request('http://asd', { signal: ac.signal })
t.equal(req.signal.aborted, false)
ac.signal.addEventListener('abort', () => {
t.pass()
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
} else {
t.pass()
}
})
ac.abort()
ac.abort('gwak')
})

test('pre aborted signal cloned', t => {
const ac = new AbortController()
ac.abort()
ac.abort('gwak')
const req = new Request('http://asd', { signal: ac.signal }).clone()
t.equal(req.signal.aborted, true)
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
}
t.end()
})

Expand Down Expand Up @@ -324,9 +335,13 @@ test('post aborted signal cloned', t => {
const req = new Request('http://asd', { signal: ac.signal }).clone()
t.equal(req.signal.aborted, false)
ac.signal.addEventListener('abort', () => {
t.pass()
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
} else {
t.pass()
}
})
ac.abort()
ac.abort('gwak')
})

test('Passing headers in init', (t) => {
Expand Down