Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: implement interface converter and use it for AbortSignal validation #54965

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
jazelly marked this conversation as resolved.
Show resolved Hide resolved
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
18 changes: 17 additions & 1 deletion lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
ObjectAssign,
ObjectPrototypeIsPrototypeOf,
SafeSet,
String,
SymbolIterator,
Expand All @@ -20,6 +21,7 @@ const {

const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
Expand Down Expand Up @@ -275,20 +277,34 @@ function createSequenceConverter(converter) {
const val = converter(res.value, {
__proto__: null,
...opts,
context: `${opts.context}, index ${array.length}`,
jazelly marked this conversation as resolved.
Show resolved Hide resolved
context: `${opts.context}[${array.length}]`,
});
ArrayPrototypePush(array, val);
};
return array;
};
}

// https://webidl.spec.whatwg.org/#js-interface
function createInterfaceConverter(name, I) {
return (V, opts = kEmptyObject) => {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
// 1. If V implements I, then return the IDL interface type value that
jazelly marked this conversation as resolved.
Show resolved Hide resolved
// represents a reference to that platform object.
if (ObjectPrototypeIsPrototypeOf(I, V)) return V;
// 2. Throw a TypeError.
throw new ERR_INVALID_ARG_TYPE(
typeof opts.context === 'string' ? opts.context : 'value', name, V,
);
};
}


module.exports = {
type,
converters,
convertToInt,
createEnumConverter,
createInterfaceConverter,
createSequenceConverter,
evenRound,
makeException,
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);
});
});
Loading