2020-05-23 01:11:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Modeled very closely on the AbortController implementation
|
|
|
|
// in https://github.com/mysticatea/abort-controller (MIT license)
|
|
|
|
|
|
|
|
const {
|
2024-09-08 10:53:14 +00:00
|
|
|
ArrayPrototypePush,
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectAssign,
|
|
|
|
ObjectDefineProperties,
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty,
|
2024-09-08 10:53:14 +00:00
|
|
|
ObjectSetPrototypeOf,
|
2023-02-07 17:43:57 +00:00
|
|
|
PromiseResolve,
|
2024-10-19 08:13:58 +00:00
|
|
|
PromiseWithResolvers,
|
2021-11-20 20:55:05 +00:00
|
|
|
SafeFinalizationRegistry,
|
|
|
|
SafeSet,
|
2024-09-05 14:47:52 +00:00
|
|
|
SafeWeakRef,
|
2020-05-23 01:11:14 +00:00
|
|
|
Symbol,
|
2020-11-14 11:46:29 +00:00
|
|
|
SymbolToStringTag,
|
2020-05-23 01:11:14 +00:00
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
2020-10-30 22:38:26 +00:00
|
|
|
defineEventHandler,
|
2020-05-23 01:11:14 +00:00
|
|
|
EventTarget,
|
2020-10-26 11:56:48 +00:00
|
|
|
Event,
|
2021-11-20 20:55:05 +00:00
|
|
|
kTrustEvent,
|
|
|
|
kNewListener,
|
|
|
|
kRemoveListener,
|
2023-07-05 23:12:37 +00:00
|
|
|
kResistStopPropagation,
|
2023-02-07 17:43:57 +00:00
|
|
|
kWeakHandler,
|
2020-05-23 01:11:14 +00:00
|
|
|
} = require('internal/event_target');
|
|
|
|
const {
|
|
|
|
customInspectSymbol,
|
2022-07-29 18:31:24 +00:00
|
|
|
kEmptyObject,
|
2023-02-07 17:43:57 +00:00
|
|
|
kEnumerableProperty,
|
2020-05-23 01:11:14 +00:00
|
|
|
} = require('internal/util');
|
|
|
|
const { inspect } = require('internal/util/inspect');
|
2021-03-11 22:25:45 +00:00
|
|
|
const {
|
|
|
|
codes: {
|
2021-07-28 01:41:18 +00:00
|
|
|
ERR_ILLEGAL_CONSTRUCTOR,
|
2022-07-29 18:31:24 +00:00
|
|
|
ERR_INVALID_ARG_TYPE,
|
2021-03-11 22:25:45 +00:00
|
|
|
ERR_INVALID_THIS,
|
2023-02-26 10:34:02 +00:00
|
|
|
},
|
2021-03-11 22:25:45 +00:00
|
|
|
} = require('internal/errors');
|
2024-09-06 17:40:21 +00:00
|
|
|
const {
|
|
|
|
converters,
|
2024-09-17 11:48:37 +00:00
|
|
|
createInterfaceConverter,
|
2024-09-06 17:40:21 +00:00
|
|
|
createSequenceConverter,
|
|
|
|
} = require('internal/webidl');
|
2020-05-23 01:11:14 +00:00
|
|
|
|
2021-11-20 20:55:05 +00:00
|
|
|
const {
|
2023-02-07 17:43:57 +00:00
|
|
|
validateObject,
|
2021-11-20 20:55:05 +00:00
|
|
|
validateUint32,
|
2024-04-20 16:24:25 +00:00
|
|
|
kValidateObjectAllowObjects,
|
2021-11-20 20:55:05 +00:00
|
|
|
} = require('internal/validators');
|
|
|
|
|
|
|
|
const {
|
|
|
|
DOMException,
|
|
|
|
} = internalBinding('messaging');
|
|
|
|
|
|
|
|
const {
|
|
|
|
clearTimeout,
|
|
|
|
setTimeout,
|
|
|
|
} = require('timers');
|
2023-05-18 08:11:07 +00:00
|
|
|
const assert = require('internal/assert');
|
2021-11-20 20:55:05 +00:00
|
|
|
|
2021-12-01 16:18:37 +00:00
|
|
|
const {
|
2023-07-07 17:00:00 +00:00
|
|
|
kDeserialize,
|
|
|
|
kTransfer,
|
|
|
|
kTransferList,
|
2024-09-25 08:51:28 +00:00
|
|
|
markTransferMode,
|
2023-07-07 17:00:00 +00:00
|
|
|
} = require('internal/worker/js_transferable');
|
2021-11-20 20:55:05 +00:00
|
|
|
|
2021-12-01 16:18:37 +00:00
|
|
|
let _MessageChannel;
|
|
|
|
|
2024-04-07 19:32:02 +00:00
|
|
|
const kDontThrowSymbol = Symbol('kDontThrowSymbol');
|
|
|
|
|
2023-07-07 17:00:00 +00:00
|
|
|
// Loading the MessageChannel and markTransferable have to be done lazily
|
2021-12-01 16:18:37 +00:00
|
|
|
// because otherwise we'll end up with a require cycle that ends up with
|
|
|
|
// an incomplete initialization of abort_controller.
|
|
|
|
|
|
|
|
function lazyMessageChannel() {
|
|
|
|
_MessageChannel ??= require('internal/worker/io').MessageChannel;
|
|
|
|
return new _MessageChannel();
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:55:05 +00:00
|
|
|
const clearTimeoutRegistry = new SafeFinalizationRegistry(clearTimeout);
|
2024-10-14 20:33:41 +00:00
|
|
|
const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeakRef) => {
|
|
|
|
const signal = signalWeakRef.deref();
|
|
|
|
if (signal === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
signal[kDependantSignals].forEach((ref) => {
|
|
|
|
if (ref.deref() === undefined) {
|
|
|
|
signal[kDependantSignals].delete(ref);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2023-05-18 08:11:07 +00:00
|
|
|
const gcPersistentSignals = new SafeSet();
|
2021-12-01 16:18:37 +00:00
|
|
|
|
|
|
|
const kAborted = Symbol('kAborted');
|
|
|
|
const kReason = Symbol('kReason');
|
|
|
|
const kCloneData = Symbol('kCloneData');
|
|
|
|
const kTimeout = Symbol('kTimeout');
|
2022-07-29 18:31:24 +00:00
|
|
|
const kMakeTransferable = Symbol('kMakeTransferable');
|
2023-05-18 08:11:07 +00:00
|
|
|
const kComposite = Symbol('kComposite');
|
|
|
|
const kSourceSignals = Symbol('kSourceSignals');
|
|
|
|
const kDependantSignals = Symbol('kDependantSignals');
|
2020-05-23 01:11:14 +00:00
|
|
|
|
|
|
|
function customInspect(self, obj, depth, options) {
|
|
|
|
if (depth < 0)
|
|
|
|
return self;
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
const opts = ObjectAssign({}, options, {
|
2023-02-26 10:34:02 +00:00
|
|
|
depth: options.depth === null ? null : options.depth - 1,
|
2020-05-23 01:11:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return `${self.constructor.name} ${inspect(obj, opts)}`;
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:43:57 +00:00
|
|
|
function validateThisAbortSignal(obj) {
|
2021-03-11 22:25:45 +00:00
|
|
|
if (obj?.[kAborted] === undefined)
|
|
|
|
throw new ERR_INVALID_THIS('AbortSignal');
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:55:05 +00:00
|
|
|
// Because the AbortSignal timeout cannot be canceled, we don't want the
|
|
|
|
// presence of the timer alone to keep the AbortSignal from being garbage
|
|
|
|
// collected if it otherwise no longer accessible. We also don't want the
|
|
|
|
// timer to keep the Node.js process open on it's own. Therefore, we wrap
|
|
|
|
// the AbortSignal in a WeakRef and have the setTimeout callback close
|
|
|
|
// over the WeakRef rather than directly over the AbortSignal, and we unref
|
|
|
|
// the created timer object. Separately, we add the signal to a
|
|
|
|
// FinalizerRegistry that will clear the timeout when the signal is gc'd.
|
|
|
|
function setWeakAbortSignalTimeout(weakRef, delay) {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
const signal = weakRef.deref();
|
|
|
|
if (signal !== undefined) {
|
2023-05-18 08:11:07 +00:00
|
|
|
gcPersistentSignals.delete(signal);
|
2021-11-20 20:55:05 +00:00
|
|
|
abortSignal(
|
|
|
|
signal,
|
|
|
|
new DOMException(
|
|
|
|
'The operation was aborted due to timeout',
|
|
|
|
'TimeoutError'));
|
|
|
|
}
|
|
|
|
}, delay);
|
|
|
|
timeout.unref();
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
class AbortSignal extends EventTarget {
|
2024-04-07 19:32:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {symbol | undefined} dontThrowSymbol
|
|
|
|
* @param {{
|
|
|
|
* aborted? : boolean,
|
|
|
|
* reason? : any,
|
|
|
|
* transferable? : boolean,
|
|
|
|
* composite? : boolean,
|
|
|
|
* }} [init]
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
constructor(dontThrowSymbol = undefined, init = kEmptyObject) {
|
|
|
|
if (dontThrowSymbol !== kDontThrowSymbol) {
|
|
|
|
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
|
|
|
}
|
|
|
|
super();
|
|
|
|
|
|
|
|
const {
|
|
|
|
aborted = false,
|
|
|
|
reason = undefined,
|
|
|
|
transferable = false,
|
|
|
|
composite = false,
|
|
|
|
} = init;
|
|
|
|
this[kAborted] = aborted;
|
|
|
|
this[kReason] = reason;
|
|
|
|
this[kComposite] = composite;
|
|
|
|
if (transferable) {
|
2024-09-25 08:51:28 +00:00
|
|
|
markTransferMode(this, false, true);
|
2024-04-07 19:32:02 +00:00
|
|
|
}
|
2020-11-12 14:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:27:32 +00:00
|
|
|
/**
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2021-03-11 22:25:45 +00:00
|
|
|
get aborted() {
|
2023-02-07 17:43:57 +00:00
|
|
|
validateThisAbortSignal(this);
|
2021-03-11 22:25:45 +00:00
|
|
|
return !!this[kAborted];
|
|
|
|
}
|
2020-05-23 01:11:14 +00:00
|
|
|
|
2021-11-13 16:27:32 +00:00
|
|
|
/**
|
|
|
|
* @type {any}
|
|
|
|
*/
|
|
|
|
get reason() {
|
2023-02-07 17:43:57 +00:00
|
|
|
validateThisAbortSignal(this);
|
2021-11-13 16:27:32 +00:00
|
|
|
return this[kReason];
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:28:30 +00:00
|
|
|
throwIfAborted() {
|
2023-02-08 13:29:09 +00:00
|
|
|
validateThisAbortSignal(this);
|
|
|
|
if (this[kAborted]) {
|
|
|
|
throw this[kReason];
|
2021-11-24 15:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
[customInspectSymbol](depth, options) {
|
|
|
|
return customInspect(this, {
|
2023-02-26 10:34:02 +00:00
|
|
|
aborted: this.aborted,
|
2020-05-23 01:11:14 +00:00
|
|
|
}, depth, options);
|
|
|
|
}
|
2021-03-10 16:59:52 +00:00
|
|
|
|
2021-11-13 16:27:32 +00:00
|
|
|
/**
|
2023-03-22 15:50:41 +00:00
|
|
|
* @param {any} [reason]
|
2021-11-13 16:27:32 +00:00
|
|
|
* @returns {AbortSignal}
|
|
|
|
*/
|
2021-11-24 15:28:30 +00:00
|
|
|
static abort(
|
|
|
|
reason = new DOMException('This operation was aborted', 'AbortError')) {
|
2024-04-07 19:32:02 +00:00
|
|
|
return new AbortSignal(kDontThrowSymbol, { aborted: true, reason });
|
2021-03-10 16:59:52 +00:00
|
|
|
}
|
2021-11-20 20:55:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} delay
|
|
|
|
* @returns {AbortSignal}
|
|
|
|
*/
|
|
|
|
static timeout(delay) {
|
2022-10-31 12:47:38 +00:00
|
|
|
validateUint32(delay, 'delay', false);
|
2024-04-07 19:32:02 +00:00
|
|
|
const signal = new AbortSignal(kDontThrowSymbol);
|
2021-11-20 20:55:05 +00:00
|
|
|
signal[kTimeout] = true;
|
|
|
|
clearTimeoutRegistry.register(
|
|
|
|
signal,
|
2024-09-05 14:47:52 +00:00
|
|
|
setWeakAbortSignalTimeout(new SafeWeakRef(signal), delay));
|
2021-11-20 20:55:05 +00:00
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:11:07 +00:00
|
|
|
/**
|
|
|
|
* @param {AbortSignal[]} signals
|
|
|
|
* @returns {AbortSignal}
|
|
|
|
*/
|
|
|
|
static any(signals) {
|
2024-09-17 11:48:37 +00:00
|
|
|
const signalsArray = converters['sequence<AbortSignal>'](
|
|
|
|
signals,
|
|
|
|
{ __proto__: null, context: 'signals' },
|
|
|
|
);
|
2024-09-06 17:40:21 +00:00
|
|
|
|
2024-04-07 19:32:02 +00:00
|
|
|
const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true });
|
2024-09-06 17:40:21 +00:00
|
|
|
if (!signalsArray.length) {
|
2023-06-15 12:55:12 +00:00
|
|
|
return resultSignal;
|
|
|
|
}
|
2024-09-05 14:47:52 +00:00
|
|
|
const resultSignalWeakRef = new SafeWeakRef(resultSignal);
|
2023-05-18 08:11:07 +00:00
|
|
|
resultSignal[kSourceSignals] = new SafeSet();
|
2024-09-06 17:40:21 +00:00
|
|
|
for (let i = 0; i < signalsArray.length; i++) {
|
|
|
|
const signal = signalsArray[i];
|
2023-05-18 08:11:07 +00:00
|
|
|
if (signal.aborted) {
|
|
|
|
abortSignal(resultSignal, signal.reason);
|
|
|
|
return resultSignal;
|
|
|
|
}
|
|
|
|
signal[kDependantSignals] ??= new SafeSet();
|
|
|
|
if (!signal[kComposite]) {
|
2024-10-14 20:33:41 +00:00
|
|
|
const signalWeakRef = new SafeWeakRef(signal);
|
|
|
|
resultSignal[kSourceSignals].add(signalWeakRef);
|
2023-05-18 08:11:07 +00:00
|
|
|
signal[kDependantSignals].add(resultSignalWeakRef);
|
2024-10-14 20:33:41 +00:00
|
|
|
dependantSignalsCleanupRegistry.register(resultSignal, signalWeakRef);
|
2023-05-18 08:11:07 +00:00
|
|
|
} else if (!signal[kSourceSignals]) {
|
|
|
|
continue;
|
|
|
|
} else {
|
2024-10-14 20:33:41 +00:00
|
|
|
for (const sourceSignalWeakRef of signal[kSourceSignals]) {
|
|
|
|
const sourceSignal = sourceSignalWeakRef.deref();
|
|
|
|
if (!sourceSignal) {
|
2023-05-18 08:11:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-10-14 20:33:41 +00:00
|
|
|
assert(!sourceSignal.aborted);
|
|
|
|
assert(!sourceSignal[kComposite]);
|
2023-05-18 08:11:07 +00:00
|
|
|
|
2024-10-14 20:33:41 +00:00
|
|
|
if (resultSignal[kSourceSignals].has(sourceSignalWeakRef)) {
|
2023-05-18 08:11:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-10-14 20:33:41 +00:00
|
|
|
resultSignal[kSourceSignals].add(sourceSignalWeakRef);
|
|
|
|
sourceSignal[kDependantSignals].add(resultSignalWeakRef);
|
|
|
|
dependantSignalsCleanupRegistry.register(resultSignal, sourceSignalWeakRef);
|
2023-05-18 08:11:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resultSignal;
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:55:05 +00:00
|
|
|
[kNewListener](size, type, listener, once, capture, passive, weak) {
|
|
|
|
super[kNewListener](size, type, listener, once, capture, passive, weak);
|
2023-05-18 08:11:07 +00:00
|
|
|
const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
|
|
|
|
if (isTimeoutOrNonEmptyCompositeSignal &&
|
2021-11-20 20:55:05 +00:00
|
|
|
type === 'abort' &&
|
|
|
|
!this.aborted &&
|
|
|
|
!weak &&
|
|
|
|
size === 1) {
|
2023-05-18 08:11:07 +00:00
|
|
|
// If this is a timeout signal, or a non-empty composite signal, and we're adding a non-weak abort
|
2021-11-20 20:55:05 +00:00
|
|
|
// listener, then we don't want it to be gc'd while the listener
|
|
|
|
// is attached and the timer still hasn't fired. So, we retain a
|
|
|
|
// strong ref that is held for as long as the listener is registered.
|
2023-05-18 08:11:07 +00:00
|
|
|
gcPersistentSignals.add(this);
|
2021-11-20 20:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[kRemoveListener](size, type, listener, capture) {
|
|
|
|
super[kRemoveListener](size, type, listener, capture);
|
2023-05-18 08:11:07 +00:00
|
|
|
const isTimeoutOrNonEmptyCompositeSignal = this[kTimeout] || (this[kComposite] && this[kSourceSignals]?.size);
|
|
|
|
if (isTimeoutOrNonEmptyCompositeSignal && type === 'abort' && size === 0) {
|
|
|
|
gcPersistentSignals.delete(this);
|
2021-11-20 20:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 16:18:37 +00:00
|
|
|
|
|
|
|
[kTransfer]() {
|
2023-02-07 17:43:57 +00:00
|
|
|
validateThisAbortSignal(this);
|
2021-12-01 16:18:37 +00:00
|
|
|
const aborted = this.aborted;
|
|
|
|
if (aborted) {
|
|
|
|
const reason = this.reason;
|
|
|
|
return {
|
|
|
|
data: { aborted, reason },
|
|
|
|
deserializeInfo: 'internal/abort_controller:ClonedAbortSignal',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { port1, port2 } = this[kCloneData];
|
|
|
|
this[kCloneData] = undefined;
|
|
|
|
|
|
|
|
this.addEventListener('abort', () => {
|
|
|
|
port1.postMessage(this.reason);
|
|
|
|
port1.close();
|
|
|
|
}, { once: true });
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: { port: port2 },
|
|
|
|
deserializeInfo: 'internal/abort_controller:ClonedAbortSignal',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[kTransferList]() {
|
|
|
|
if (!this.aborted) {
|
|
|
|
const { port1, port2 } = lazyMessageChannel();
|
|
|
|
port1.unref();
|
|
|
|
port2.unref();
|
|
|
|
this[kCloneData] = {
|
|
|
|
port1,
|
|
|
|
port2,
|
|
|
|
};
|
|
|
|
return [port2];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
[kDeserialize]({ aborted, reason, port }) {
|
|
|
|
if (aborted) {
|
|
|
|
this[kAborted] = aborted;
|
|
|
|
this[kReason] = reason;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
port.onmessage = ({ data }) => {
|
|
|
|
abortSignal(this, data);
|
|
|
|
port.close();
|
|
|
|
port.onmessage = undefined;
|
|
|
|
};
|
|
|
|
// The receiving port, by itself, should never keep the event loop open.
|
|
|
|
// The unref() has to be called *after* setting the onmessage handler.
|
|
|
|
port.unref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-17 11:48:37 +00:00
|
|
|
converters.AbortSignal = createInterfaceConverter('AbortSignal', AbortSignal.prototype);
|
|
|
|
converters['sequence<AbortSignal>'] = createSequenceConverter(converters.AbortSignal);
|
|
|
|
|
2021-12-01 16:18:37 +00:00
|
|
|
function ClonedAbortSignal() {
|
2024-04-07 19:32:02 +00:00
|
|
|
return new AbortSignal(kDontThrowSymbol, { transferable: true });
|
2020-05-23 01:11:14 +00:00
|
|
|
}
|
2021-12-01 16:18:37 +00:00
|
|
|
ClonedAbortSignal.prototype[kDeserialize] = () => {};
|
2020-05-23 01:11:14 +00:00
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperties(AbortSignal.prototype, {
|
2022-02-11 17:30:47 +00:00
|
|
|
aborted: kEnumerableProperty,
|
2020-05-23 01:11:14 +00:00
|
|
|
});
|
|
|
|
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2020-11-14 11:46:29 +00:00
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: 'AbortSignal',
|
|
|
|
});
|
|
|
|
|
2020-11-02 20:59:54 +00:00
|
|
|
defineEventHandler(AbortSignal.prototype, 'abort');
|
|
|
|
|
2024-09-08 10:53:14 +00:00
|
|
|
// https://dom.spec.whatwg.org/#dom-abortsignal-abort
|
2021-11-13 16:27:32 +00:00
|
|
|
function abortSignal(signal, reason) {
|
2024-09-08 10:53:14 +00:00
|
|
|
// 1. If signal is aborted, then return.
|
2020-05-23 01:11:14 +00:00
|
|
|
if (signal[kAborted]) return;
|
2024-09-08 10:53:14 +00:00
|
|
|
|
|
|
|
// 2. Set signal's abort reason to reason if it is given;
|
|
|
|
// otherwise to a new "AbortError" DOMException.
|
2020-05-23 01:11:14 +00:00
|
|
|
signal[kAborted] = true;
|
2021-11-13 16:27:32 +00:00
|
|
|
signal[kReason] = reason;
|
2024-09-08 10:53:14 +00:00
|
|
|
// 3. Let dependentSignalsToAbort be a new list.
|
|
|
|
const dependentSignalsToAbort = ObjectSetPrototypeOf([], null);
|
|
|
|
// 4. For each dependentSignal of signal's dependent signals:
|
|
|
|
signal[kDependantSignals]?.forEach((s) => {
|
|
|
|
const dependentSignal = s.deref();
|
|
|
|
// 1. If dependentSignal is not aborted, then:
|
|
|
|
if (dependentSignal && !dependentSignal[kAborted]) {
|
|
|
|
// 1. Set dependentSignal's abort reason to signal's abort reason.
|
|
|
|
dependentSignal[kReason] = reason;
|
|
|
|
dependentSignal[kAborted] = true;
|
|
|
|
// 2. Append dependentSignal to dependentSignalsToAbort.
|
|
|
|
ArrayPrototypePush(dependentSignalsToAbort, dependentSignal);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 5. Run the abort steps for signal
|
|
|
|
runAbort(signal);
|
|
|
|
// 6. For each dependentSignal of dependentSignalsToAbort,
|
|
|
|
// run the abort steps for dependentSignal.
|
|
|
|
for (let i = 0; i < dependentSignalsToAbort.length; i++) {
|
|
|
|
const dependentSignal = dependentSignalsToAbort[i];
|
|
|
|
runAbort(dependentSignal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// To run the abort steps for an AbortSignal signal
|
|
|
|
function runAbort(signal) {
|
2020-10-26 11:56:48 +00:00
|
|
|
const event = new Event('abort', {
|
2023-02-26 10:34:02 +00:00
|
|
|
[kTrustEvent]: true,
|
2020-10-26 11:56:48 +00:00
|
|
|
});
|
2020-05-23 01:11:14 +00:00
|
|
|
signal.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
class AbortController {
|
2022-11-19 20:13:08 +00:00
|
|
|
#signal;
|
2020-05-23 01:11:14 +00:00
|
|
|
|
2021-11-13 16:27:32 +00:00
|
|
|
/**
|
|
|
|
* @type {AbortSignal}
|
|
|
|
*/
|
2021-03-11 22:25:45 +00:00
|
|
|
get signal() {
|
2024-04-07 19:32:02 +00:00
|
|
|
this.#signal ??= new AbortSignal(kDontThrowSymbol);
|
2022-07-16 06:07:53 +00:00
|
|
|
return this.#signal;
|
2021-03-11 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:27:32 +00:00
|
|
|
/**
|
2023-03-22 15:50:41 +00:00
|
|
|
* @param {any} [reason]
|
2021-11-13 16:27:32 +00:00
|
|
|
*/
|
2021-11-24 15:28:30 +00:00
|
|
|
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
|
2024-04-07 19:32:02 +00:00
|
|
|
abortSignal(this.#signal ??= new AbortSignal(kDontThrowSymbol), reason);
|
2021-03-11 22:25:45 +00:00
|
|
|
}
|
2020-05-23 01:11:14 +00:00
|
|
|
|
|
|
|
[customInspectSymbol](depth, options) {
|
|
|
|
return customInspect(this, {
|
2023-02-26 10:34:02 +00:00
|
|
|
signal: this.signal,
|
2020-05-23 01:11:14 +00:00
|
|
|
}, depth, options);
|
|
|
|
}
|
2022-07-29 18:31:24 +00:00
|
|
|
|
|
|
|
static [kMakeTransferable]() {
|
|
|
|
const controller = new AbortController();
|
2024-04-07 19:32:02 +00:00
|
|
|
controller.#signal = new AbortSignal(kDontThrowSymbol, { transferable: true });
|
2022-07-29 18:31:24 +00:00
|
|
|
return controller;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables the AbortSignal to be transferable using structuredClone/postMessage.
|
|
|
|
* @param {AbortSignal} signal
|
|
|
|
* @returns {AbortSignal}
|
|
|
|
*/
|
|
|
|
function transferableAbortSignal(signal) {
|
|
|
|
if (signal?.[kAborted] === undefined)
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal);
|
2024-09-25 08:51:28 +00:00
|
|
|
markTransferMode(signal, false, true);
|
2023-07-07 17:00:00 +00:00
|
|
|
return signal;
|
2022-07-29 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an AbortController with a transferable AbortSignal
|
|
|
|
*/
|
|
|
|
function transferableAbortController() {
|
|
|
|
return AbortController[kMakeTransferable]();
|
2020-05-23 01:11:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:43:57 +00:00
|
|
|
/**
|
|
|
|
* @param {AbortSignal} signal
|
|
|
|
* @param {any} resource
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function aborted(signal, resource) {
|
2024-09-17 11:48:37 +00:00
|
|
|
converters.AbortSignal(signal, { __proto__: null, context: 'signal' });
|
2024-04-20 16:24:25 +00:00
|
|
|
validateObject(resource, 'resource', kValidateObjectAllowObjects);
|
2023-02-07 17:43:57 +00:00
|
|
|
if (signal.aborted)
|
|
|
|
return PromiseResolve();
|
2024-10-19 08:13:58 +00:00
|
|
|
const abortPromise = PromiseWithResolvers();
|
2023-07-05 23:12:37 +00:00
|
|
|
const opts = { __proto__: null, [kWeakHandler]: resource, once: true, [kResistStopPropagation]: true };
|
|
|
|
signal.addEventListener('abort', abortPromise.resolve, opts);
|
2023-02-07 17:43:57 +00:00
|
|
|
return abortPromise.promise;
|
|
|
|
}
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperties(AbortController.prototype, {
|
2022-02-11 17:30:47 +00:00
|
|
|
signal: kEnumerableProperty,
|
|
|
|
abort: kEnumerableProperty,
|
2020-05-23 01:11:14 +00:00
|
|
|
});
|
|
|
|
|
2020-11-14 11:46:29 +00:00
|
|
|
ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2020-11-14 11:46:29 +00:00
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: 'AbortController',
|
|
|
|
});
|
|
|
|
|
2020-05-23 01:11:14 +00:00
|
|
|
module.exports = {
|
|
|
|
AbortController,
|
|
|
|
AbortSignal,
|
2021-12-01 16:18:37 +00:00
|
|
|
ClonedAbortSignal,
|
2023-02-07 17:43:57 +00:00
|
|
|
aborted,
|
2022-07-29 18:31:24 +00:00
|
|
|
transferableAbortSignal,
|
|
|
|
transferableAbortController,
|
2020-05-23 01:11:14 +00:00
|
|
|
};
|