Skip to content

Commit

Permalink
lib: fix WebIDL object and dictionary type conversion
Browse files Browse the repository at this point in the history
PR-URL: #37047
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ExE-Boss authored and danielleadams committed Feb 16, 2021
1 parent 8483de4 commit f6f9af6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 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
9 changes: 6 additions & 3 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class Event {
constructor(type, options = null) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
if (options !== null)
validateObject(options, 'options');
validateObject(options, 'options', {
allowArray: true, allowFunction: true, nullable: true,
});
const { cancelable, bubbles, composed } = { ...options };
this[kCancelable] = !!cancelable;
this[kBubbles] = !!bubbles;
Expand Down Expand Up @@ -601,7 +602,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
1 change: 0 additions & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ let asyncTest = Promise.resolve();
'foo',
1,
false,
function() {},
].forEach((i) => (
throws(() => new Event('foo', i), {
code: 'ERR_INVALID_ARG_TYPE',
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 f6f9af6

Please sign in to comment.