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

feat: make use of addAbortListener where applicable #2195

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/api/abort-signal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { addAbortListener } = require('../core/util')
const { RequestAbortedError } = require('../core/errors')

const kListener = Symbol('kListener')
Expand Down Expand Up @@ -29,11 +30,7 @@ function addSignal (self, signal) {
abort(self)
}

if ('addEventListener' in self[kSignal]) {
self[kSignal].addEventListener('abort', self[kListener])
} else {
self[kSignal].addListener('abort', self[kListener])
}
addAbortListener(self[kSignal], self[kListener])
}

function removeSignal (self) {
Expand Down
9 changes: 6 additions & 3 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ module.exports = class BodyReadable extends Readable {
const abortFn = () => {
this.destroy()
}
let signalListenerCleanup
if (signal) {
if (typeof signal !== 'object' || !('aborted' in signal)) {
throw new InvalidArgumentError('signal must be an AbortSignal')
}
util.throwIfAborted(signal)
signal.addEventListener('abort', abortFn, { once: true })
signalListenerCleanup = util.addAbortListener(signal, abortFn)
}
try {
for await (const chunk of this) {
Expand All @@ -173,8 +174,10 @@ module.exports = class BodyReadable extends Readable {
} catch {
util.throwIfAborted(signal)
} finally {
if (signal) {
signal.removeEventListener('abort', abortFn)
if (typeof signalListenerCleanup === 'function') {
signalListenerCleanup()
} else if (signalListenerCleanup) {
signalListenerCleanup[Symbol.dispose]()
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,24 @@ function throwIfAborted (signal) {
}
}

let events
function addAbortListener (signal, listener) {
if (typeof Symbol.dispose === 'symbol') {
if (!events) {
events = require('events')
}
if (typeof events.addAbortListener === 'function' && 'aborted' in signal) {
return events.addAbortListener(signal, listener)
}
}
if ('addEventListener' in signal) {
signal.addEventListener('abort', listener, { once: true })
return () => signal.removeEventListener('abort', listener)
}
signal.addListener('abort', listener)
return () => signal.removeListener('abort', listener)
}

const hasToWellFormed = !!String.prototype.toWellFormed

/**
Expand Down Expand Up @@ -469,6 +487,7 @@ module.exports = {
isFormDataLike,
buildURL,
throwIfAborted,
addAbortListener,
nodeMajor,
nodeMinor,
nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13)
Expand Down
9 changes: 4 additions & 5 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const {
const { kHeadersList } = require('../core/symbols')
const EE = require('events')
const { Readable, pipeline } = require('stream')
const { isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util')
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util')
const { dataURLProcessor, serializeAMimeType } = require('./dataURL')
const { TransformStream } = require('stream/web')
const { getGlobalDispatcher } = require('../global')
Expand Down Expand Up @@ -174,8 +174,8 @@ async function fetch (input, init = {}) {
let controller = null

// 11. Add the following abort steps to requestObject’s signal:
requestObject.signal.addEventListener(
'abort',
addAbortListener(
requestObject.signal,
() => {
// 1. Set locallyAborted to true.
locallyAborted = true
Expand All @@ -189,8 +189,7 @@ async function fetch (input, init = {}) {
// 4. Abort the fetch() call with p, request, responseObject,
// and requestObject’s signal’s abort reason.
abortFetch(p, request, responseObject, requestObject.signal.reason)
},
{ once: true }
ronag marked this conversation as resolved.
Show resolved Hide resolved
}
)

// 12. Let handleFetchDone given response response be to finalize and
Expand Down
11 changes: 6 additions & 5 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ class Request {

// 28. Set this’s signal to a new AbortSignal object with this’s relevant
// Realm.
// TODO: could this be simplified with AbortSignal.any
// (https://dom.spec.whatwg.org/#dom-abortsignal-any)
const ac = new AbortController()
this[kSignal] = ac.signal
this[kSignal][kRealm] = this[kRealm]
Expand Down Expand Up @@ -385,7 +387,7 @@ class Request {
}
} catch {}

signal.addEventListener('abort', abort, { once: true })
util.addAbortListener(signal, abort)
requestFinalizer.register(ac, { signal, abort })
}
}
Expand Down Expand Up @@ -733,12 +735,11 @@ class Request {
if (this.signal.aborted) {
ac.abort(this.signal.reason)
} else {
this.signal.addEventListener(
'abort',
util.addAbortListener(
this.signal,
() => {
ac.abort(this.signal.reason)
},
{ once: true }
}
)
}
clonedRequestObject[kSignal] = ac.signal
Expand Down
Loading