From 864f964460a2396836d9daa7552badae33aa898a Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Tue, 17 Sep 2024 21:18:37 +0930 Subject: [PATCH] lib: validate signals with interface converter PR-URL: https://github.com/nodejs/node/pull/54965 Fixes: https://github.com/nodejs/node/issues/54962 Reviewed-By: Matthew Aitken Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/abort_controller.js | 19 +++++++++---------- test/parallel/test-abortsignal-any.mjs | 13 +++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 3b34ed34afe346..48ac99a14f33aa 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -43,12 +43,11 @@ const { } = require('internal/errors'); const { converters, + createInterfaceConverter, createSequenceConverter, } = require('internal/webidl'); const { - validateAbortSignal, - validateAbortSignalArray, validateObject, validateUint32, kValidateObjectAllowObjects, @@ -225,11 +224,11 @@ class AbortSignal extends EventTarget { * @returns {AbortSignal} */ static any(signals) { - const signalsArray = createSequenceConverter( - converters.any, - )(signals); + const signalsArray = converters['sequence']( + signals, + { __proto__: null, context: 'signals' }, + ); - validateAbortSignalArray(signalsArray, 'signals'); const resultSignal = new AbortSignal(kDontThrowSymbol, { composite: true }); if (!signalsArray.length) { return resultSignal; @@ -349,6 +348,9 @@ class AbortSignal extends EventTarget { } } +converters.AbortSignal = createInterfaceConverter('AbortSignal', AbortSignal.prototype); +converters['sequence'] = createSequenceConverter(converters.AbortSignal); + function ClonedAbortSignal() { return new AbortSignal(kDontThrowSymbol, { transferable: true }); } @@ -466,10 +468,7 @@ function transferableAbortController() { * @returns {Promise} */ 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(); diff --git a/test/parallel/test-abortsignal-any.mjs b/test/parallel/test-abortsignal-any.mjs index 4378c44d987f50..19b5569c4779d1 100644 --- a/test/parallel/test-abortsignal-any.mjs +++ b/test/parallel/test-abortsignal-any.mjs @@ -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); + }); });