Skip to content

Commit

Permalink
lib: validate signals with interface converter
Browse files Browse the repository at this point in the history
PR-URL: #54965
Fixes: #54962
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
jazelly authored and nodejs-github-bot committed Sep 29, 2024
1 parent a5a946d commit e973c3e
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 @@ -43,12 +43,11 @@ const {
} = require('internal/errors');
const {
converters,
createInterfaceConverter,
createSequenceConverter,
} = require('internal/webidl');

const {
validateAbortSignal,
validateAbortSignalArray,
validateObject,
validateUint32,
kValidateObjectAllowObjects,
Expand Down Expand Up @@ -225,11 +224,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 @@ -349,6 +348,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 @@ -466,10 +468,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 e973c3e

Please sign in to comment.