Skip to content

Commit

Permalink
buffer: make Blob's constructor more spec-compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Feb 13, 2021
1 parent b5f728b commit 5e91855
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
24 changes: 5 additions & 19 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,10 @@ function getSource(source, encoding) {
if (isBlob(source))
return [source.size, source[kHandle]];

if (typeof source === 'string') {
source = lazyBuffer().from(source, encoding);
} else if (isAnyArrayBuffer(source)) {
if (isAnyArrayBuffer(source)) {
source = new Uint8Array(source);
} else if (!isArrayBufferView(source)) {
throw new ERR_INVALID_ARG_TYPE(
'source',
[
'string',
'ArrayBuffer',
'SharedArrayBuffer',
'Buffer',
'TypedArray',
'DataView'
],
source);
source = lazyBuffer().from(`${source}`, encoding);
}

// We copy into a new Uint8Array because the underlying
Expand Down Expand Up @@ -117,7 +105,7 @@ class Blob extends JSTransferable {
}
if (options !== undefined)
validateObject(options, 'options');
const {
let {
encoding = 'utf8',
type = '',
} = { ...options };
Expand All @@ -129,16 +117,14 @@ class Blob extends JSTransferable {
return src;
});

// This is a MIME media type but we're not actively checking the syntax.
// But, to be fair, neither does Chrome.
validateString(type, 'options.type');

if (!isUint32(length))
throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF);

super();
this[kHandle] = createBlob(sources_, length);
this[kLength] = length;

type = `${type}`;
this[kType] = RegExpPrototypeTest(disallowedTypeCharacters, type) ?
'' : StringPrototypeToLowerCase(type);
}
Expand Down
23 changes: 10 additions & 13 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ assert.throws(() => new Blob({}), {
code: 'ERR_INVALID_ARG_TYPE'
});

assert.throws(() => new Blob(['test', 1]), {
code: 'ERR_INVALID_ARG_TYPE'
});

{
const b = new Blob([]);
assert(b);
Expand All @@ -43,15 +39,9 @@ assert.throws(() => new Blob(['test', 1]), {
}

{
assert.throws(() => new Blob([], { type: 1 }), {
code: 'ERR_INVALID_ARG_TYPE'
});
assert.throws(() => new Blob([], { type: false }), {
code: 'ERR_INVALID_ARG_TYPE'
});
assert.throws(() => new Blob([], { type: {} }), {
code: 'ERR_INVALID_ARG_TYPE'
});
assert.strictEqual(new Blob([], { type: 1 }).type, '1');
assert.strictEqual(new Blob([], { type: false }).type, 'false');
assert.strictEqual(new Blob([], { type: {} }).type, '[object object]');
}

{
Expand Down Expand Up @@ -182,3 +172,10 @@ assert.throws(() => new Blob(['test', 1]), {
const b = new Blob(['hello'], { type: '\x01' });
assert.strictEqual(b.type, '');
}

{
const b = new Blob(['test', 42]);
b.text().then(common.mustCall((text) => {
assert.strictEqual(text, 'test42');
}));
}

0 comments on commit 5e91855

Please sign in to comment.