Skip to content

Commit

Permalink
lib: validate signals with interface converter
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Sep 16, 2024
1 parent ea1249c commit 8f1cafc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ const {
} = require('internal/errors');
const {
converters,
createInterfaceConverter,
createSequenceConverter,
} = require('internal/webidl');

const {
validateAbortSignal,
validateAbortSignalArray,
validateObject,
validateUint32,
kValidateObjectAllowObjects,
Expand Down Expand Up @@ -229,11 +228,11 @@ class AbortSignal extends EventTarget {
* @returns {AbortSignal}
*/
static any(signals) {
const signalsArray = createSequenceConverter(
converters.any,
)(signals);
const signalsArray = converters['sequence<AbortSignal>'](
signals,
{ __proto__: null, context: 'signals' },
);

validateAbortSignalArray(signalsArray, 'signals');
const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true });
if (!signalsArray.length) {
return resultSignal;
Expand Down Expand Up @@ -353,6 +352,9 @@ class AbortSignal extends EventTarget {
}
}

converters.AbortSignal = createInterfaceConverter('AbortSignal', AbortSignal.prototype);
converters['sequence<AbortSignal>'] = createSequenceConverter(converters.AbortSignal);

function ClonedAbortSignal() {
return new AbortSignal(kDontThrowSymbol, { transferable: true });
}
Expand Down Expand Up @@ -442,10 +444,7 @@ function transferableAbortController() {
* @returns {Promise<void>}
*/
async function aborted(signal, resource) {
if (signal === undefined) {
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal);
}
validateAbortSignal(signal, 'signal');
converters.AbortSignal(signal, { __proto__: null, context: 'signal' });
validateObject(resource, 'resource', kValidateObjectAllowObjects);
if (signal.aborted)
return PromiseResolve();
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-abortsignal-any.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,17 @@ describe('AbortSignal.any()', { concurrency: !process.env.TEST_PARALLEL }, () =>
controller.abort();
assert.strictEqual(result, 1);
});

it('throws TypeError if any value does not implement AbortSignal', () => {
const expectedError = { code: 'ERR_INVALID_ARG_TYPE' };
assert.throws(() => AbortSignal.any([ null ]), expectedError);
assert.throws(() => AbortSignal.any([ undefined ]), expectedError);
assert.throws(() => AbortSignal.any([ '123' ]), expectedError);
assert.throws(() => AbortSignal.any([ 123 ]), expectedError);
assert.throws(() => AbortSignal.any([{}]), expectedError);
assert.throws(() => AbortSignal.any([{ aborted: true }]), expectedError);
assert.throws(() => AbortSignal.any([{
aborted: true, reason: '', throwIfAborted: null,
}]), expectedError);
});
});

0 comments on commit 8f1cafc

Please sign in to comment.