Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
buffer: fix validation of options in Blob constructor
  • Loading branch information
aduh95 committed Oct 25, 2022
commit 138f0d762948c55707c8a48b1f6c2f5510b7320a
7 changes: 4 additions & 3 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const {
const {
validateObject,
isUint32,
validateDictionary,
} = require('internal/validators');

const kHandle = Symbol('kHandle');
Expand Down Expand Up @@ -138,17 +139,17 @@ class Blob {
* }} [options]
* @constructs {Blob}
*/
constructor(sources = [], options = kEmptyObject) {
constructor(sources = [], options) {
if (sources === null ||
typeof sources[SymbolIterator] !== 'function' ||
typeof sources === 'string') {
throw new ERR_INVALID_ARG_TYPE('sources', 'a sequence', sources);
}
validateObject(options, 'options');
validateDictionary(options, 'options');
let {
type = '',
endings = 'transparent',
} = options;
} = options ?? kEmptyObject;

endings = `${endings}`;
if (endings !== 'transparent' && endings !== 'native')
Expand Down
15 changes: 15 additions & 0 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ const validateObject = hideStackFrames(
}
});

/**
* @callback validateDictionary
* @param {*} value
* @param {string} name
Comment thread
aduh95 marked this conversation as resolved.
Outdated
*/

/** @type {validateDictionary} */
const validateDictionary = hideStackFrames(
Comment thread
joyeecheung marked this conversation as resolved.
(value, name) => {
if (value != null && typeof value !== 'object' && typeof value !== 'function') {
throw new ERR_INVALID_ARG_TYPE(name, 'Object or Function', value);
Comment thread
aduh95 marked this conversation as resolved.
Outdated
}
});

/**
* @callback validateArray
* @param {*} value
Expand Down Expand Up @@ -511,6 +525,7 @@ module.exports = {
validateBooleanArray,
validateBoolean,
validateBuffer,
validateDictionary,
validateEncoding,
validateFunction,
validateInt32,
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,32 @@ assert.throws(() => new Blob({}), {
assert.strictEqual(blob.size, 28);
assert.strictEqual(blob.type, '');
})().then(common.mustCall());

{
// Testing the defaults
[undefined, null, Object.create(null), { type: undefined }, {
get type() {}, // eslint-disable-line getter-return
}].forEach((options) => {
assert.strictEqual(
new Blob([], options).type,
new Blob([]).type,
);
});

Reflect.defineProperty(Object.prototype, 'type', {
__proto__: null,
configurable: true,
get: common.mustCall(() => 3, 7),
});

[{}, [], () => {}, Number, new Number(), new String(), new Boolean()].forEach(
(options) => {
assert.strictEqual(new Blob([], options).type, '3');
},
);
[0, '', true, Symbol(), 0n].forEach((options) => {
assert.throws(() => new Blob([], options), { code: 'ERR_INVALID_ARG_TYPE' });
});

delete Object.prototype.type;
}