Skip to content

Commit

Permalink
fix: follow signal.reason in Request (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros authored Jul 27, 2022
1 parent 40af2c0 commit dd613ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit dd613ef

Please sign in to comment.