Skip to content

Commit

Permalink
lib: fix WebIDL object and dictionary type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Jan 24, 2021
1 parent 835b85d commit a502d05
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ async function deriveKey(
}
if (baseKey.algorithm.name !== algorithm.name)
throw lazyDOMException('Key algorithm mismatch', 'InvalidAccessError');
validateObject(derivedKeyAlgorithm, 'derivedKeyAlgorithm');
validateObject(derivedKeyAlgorithm, 'derivedKeyAlgorithm', {
allowArray: true, allowFunction: true,
});
validateBoolean(extractable, 'extractable');
validateArray(keyUsages, 'keyUsages');

Expand Down
8 changes: 6 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class Event {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
if (options !== null)
validateObject(options, 'options');
validateObject(options, 'options', {
allowArray: true, allowFunction: true,
});
const { cancelable, bubbles, composed } = { ...options };
this[kCancelable] = !!cancelable;
this[kBubbles] = !!bubbles;
Expand Down Expand Up @@ -563,7 +565,9 @@ function shouldAddListener(listener) {
function validateEventListenerOptions(options) {
if (typeof options === 'boolean')
return { capture: options };
validateObject(options, 'options');
validateObject(options, 'options', {
allowArray: true, allowFunction: true,
});
return {
once: Boolean(options.once),
capture: Boolean(options.capture),
Expand Down
12 changes: 9 additions & 3 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,16 @@ function validateBoolean(value, name) {
}

const validateObject = hideStackFrames(
(value, name, { nullable = false } = {}) => {
(value, name, {
nullable = false,
allowArray = false,
allowFunction = false,
} = {}) => {
if ((!nullable && value === null) ||
ArrayIsArray(value) ||
typeof value !== 'object') {
(!allowArray && ArrayIsArray(value)) ||
(typeof value !== 'object' && (
!allowFunction || typeof value !== 'function'
))) {
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
}
});
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ const invalidArgValueError = {
validateObject({}, 'foo');
validateObject({ a: 42, b: 'foo' }, 'foo');

[undefined, null, true, false, 0, 0.0, 42, '', 'string', []]
[undefined, null, true, false, 0, 0.0, 42, '', 'string', [], () => {}]
.forEach((val) => {
assert.throws(() => {
validateObject(val, 'foo');
}, invalidArgTypeError);
});

// validateObject options tests:
validateObject(null, 'foo', { nullable: true });
validateObject([], 'foo', { allowArray: true });
validateObject(() => {}, 'foo', { allowFunction: true });
}

{
Expand Down

0 comments on commit a502d05

Please sign in to comment.