abort-signal.js
This file
Path
lib/utils/abort-signal.js
15 lines · 708 bytes · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
12345678910111213141516import {once} from 'node:events';
// Combines `util.aborted()` and `events.addAbortListener()`: promise-based and cleaned up with a stop signalexport const onAbortedSignal = async (mainSignal, stopSignal) => { if (!mainSignal.aborted) { await once(mainSignal, 'abort', {signal: stopSignal}); }};
// Validate the `cancelSignal` and `unpipeSignal` options, so a wrong type fails right away instead of failing later with an unrelated errorexport const validateAbortSignal = (signal, optionName) => { if (signal !== undefined && Object.prototype.toString.call(signal) !== '[object AbortSignal]') { throw new TypeError(`The \`${optionName}\` option must be an AbortSignal: ${String(signal)}.`); }};