Skip to content

Commit 26cd607

Browse files
committed
lib: make validateAbortSignal stricter
We should not force-check if the passed signal is **NOT** undefined and then proceed to check whether the value is an abort signal or not as the validator won't throw an error if the value is undefined as we straight up validate the values instead of checking if they're not undefined first. This unnecessary check can be done explicitly outside of the validator to only pass the value to the validator if its not undefined.
1 parent f8035ec commit 26cd607

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

lib/child_process.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,9 @@ function abortChildProcess(child, killSignal) {
690690
function spawn(file, args, options) {
691691
options = normalizeSpawnArguments(file, args, options);
692692
validateTimeout(options.timeout);
693-
validateAbortSignal(options.signal, 'options.signal');
693+
if (options.signal !== undefined)
694+
validateAbortSignal(options.signal, 'options.signal');
695+
694696
const killSignal = sanitizeKillSignal(options.killSignal);
695697
const child = new ChildProcess();
696698

lib/events.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,8 @@ function getEventListeners(emitterOrTarget, type) {
808808
*/
809809
async function once(emitter, name, options = {}) {
810810
const signal = options?.signal;
811-
validateAbortSignal(signal, 'options.signal');
811+
if (signal !== undefined)
812+
validateAbortSignal(signal, 'options.signal');
812813
if (signal?.aborted)
813814
throw new AbortError();
814815
return new Promise((resolve, reject) => {
@@ -886,7 +887,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
886887
*/
887888
function on(emitter, event, options) {
888889
const signal = options?.signal;
889-
validateAbortSignal(signal, 'options.signal');
890+
if (signal !== undefined)
891+
validateAbortSignal(signal, 'options.signal');
890892
if (signal?.aborted)
891893
throw new AbortError();
892894

lib/internal/fs/promises.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ async function writeFile(path, data, options) {
813813
data = Buffer.from(data, options.encoding || 'utf8');
814814
}
815815

816-
validateAbortSignal(options.signal);
816+
if (options.signal !== undefined)
817+
validateAbortSignal(options.signal);
817818
if (path instanceof FileHandle)
818819
return writeFileHandle(path, data, options.signal, options.encoding);
819820

lib/internal/streams/end-of-stream.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ function eos(stream, options, callback) {
4545
validateObject(options, 'options');
4646
}
4747
validateFunction(callback, 'callback');
48-
validateAbortSignal(options.signal, 'options.signal');
48+
if (options.signal !== undefined)
49+
validateAbortSignal(options.signal, 'options.signal');
4950

5051
callback = once(callback);
5152

lib/internal/validators.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,8 @@ const validateCallback = hideStackFrames((callback) => {
224224
});
225225

226226
const validateAbortSignal = hideStackFrames((signal, name) => {
227-
if (signal !== undefined &&
228-
(signal === null ||
229-
typeof signal !== 'object' ||
230-
!('aborted' in signal))) {
227+
if (signal === null || typeof signal !== 'object' ||
228+
!('aborted' in signal)) {
231229
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal);
232230
}
233231
});

lib/timers/promises.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ function setTimeout(after, value, options = {}) {
4141
options));
4242
}
4343
const { signal, ref = true } = options;
44-
try {
45-
validateAbortSignal(signal, 'options.signal');
46-
} catch (err) {
47-
return PromiseReject(err);
44+
if (signal !== undefined) {
45+
try {
46+
validateAbortSignal(signal, 'options.signal');
47+
} catch (err) {
48+
return PromiseReject(err);
49+
}
4850
}
4951
if (typeof ref !== 'boolean') {
5052
return PromiseReject(
@@ -85,10 +87,12 @@ function setImmediate(value, options = {}) {
8587
options));
8688
}
8789
const { signal, ref = true } = options;
88-
try {
89-
validateAbortSignal(signal, 'options.signal');
90-
} catch (err) {
91-
return PromiseReject(err);
90+
if (signal !== undefined) {
91+
try {
92+
validateAbortSignal(signal, 'options.signal');
93+
} catch (err) {
94+
return PromiseReject(err);
95+
}
9296
}
9397
if (typeof ref !== 'boolean') {
9498
return PromiseReject(
@@ -123,7 +127,8 @@ function setImmediate(value, options = {}) {
123127
async function* setInterval(after, value, options = {}) {
124128
validateObject(options, 'options');
125129
const { signal, ref = true } = options;
126-
validateAbortSignal(signal, 'options.signal');
130+
if (signal !== undefined)
131+
validateAbortSignal(signal, 'options.signal');
127132
validateBoolean(ref, 'options.ref');
128133

129134
if (signal?.aborted)

0 commit comments

Comments
 (0)