Skip to content

Commit dd2412c

Browse files
jazellytpoisseau
authored andcommitted
lib: validate signals with interface converter
PR-URL: nodejs#54965 Fixes: nodejs#54962 Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7d5b01b commit dd2412c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/internal/abort_controller.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ const {
4343
} = require('internal/errors');
4444
const {
4545
converters,
46+
createInterfaceConverter,
4647
createSequenceConverter,
4748
} = require('internal/webidl');
4849

4950
const {
50-
validateAbortSignal,
51-
validateAbortSignalArray,
5251
validateObject,
5352
validateUint32,
5453
kValidateObjectAllowObjects,
@@ -225,11 +224,11 @@ class AbortSignal extends EventTarget {
225224
* @returns {AbortSignal}
226225
*/
227226
static any(signals) {
228-
const signalsArray = createSequenceConverter(
229-
converters.any,
230-
)(signals);
227+
const signalsArray = converters['sequence<AbortSignal>'](
228+
signals,
229+
{ __proto__: null, context: 'signals' },
230+
);
231231

232-
validateAbortSignalArray(signalsArray, 'signals');
233232
const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true });
234233
if (!signalsArray.length) {
235234
return resultSignal;
@@ -349,6 +348,9 @@ class AbortSignal extends EventTarget {
349348
}
350349
}
351350

351+
converters.AbortSignal = createInterfaceConverter('AbortSignal', AbortSignal.prototype);
352+
converters['sequence<AbortSignal>'] = createSequenceConverter(converters.AbortSignal);
353+
352354
function ClonedAbortSignal() {
353355
return new AbortSignal(kDontThrowSymbol, { transferable: true });
354356
}
@@ -466,10 +468,7 @@ function transferableAbortController() {
466468
* @returns {Promise<void>}
467469
*/
468470
async function aborted(signal, resource) {
469-
if (signal === undefined) {
470-
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal);
471-
}
472-
validateAbortSignal(signal, 'signal');
471+
converters.AbortSignal(signal, { __proto__: null, context: 'signal' });
473472
validateObject(resource, 'resource', kValidateObjectAllowObjects);
474473
if (signal.aborted)
475474
return PromiseResolve();

test/parallel/test-abortsignal-any.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,17 @@ describe('AbortSignal.any()', { concurrency: !process.env.TEST_PARALLEL }, () =>
118118
controller.abort();
119119
assert.strictEqual(result, 1);
120120
});
121+
122+
it('throws TypeError if any value does not implement AbortSignal', () => {
123+
const expectedError = { code: 'ERR_INVALID_ARG_TYPE' };
124+
assert.throws(() => AbortSignal.any([ null ]), expectedError);
125+
assert.throws(() => AbortSignal.any([ undefined ]), expectedError);
126+
assert.throws(() => AbortSignal.any([ '123' ]), expectedError);
127+
assert.throws(() => AbortSignal.any([ 123 ]), expectedError);
128+
assert.throws(() => AbortSignal.any([{}]), expectedError);
129+
assert.throws(() => AbortSignal.any([{ aborted: true }]), expectedError);
130+
assert.throws(() => AbortSignal.any([{
131+
aborted: true, reason: '', throwIfAborted: null,
132+
}]), expectedError);
133+
});
121134
});

0 commit comments

Comments
 (0)