Skip to content

Commit 458a410

Browse files
committedFeb 28, 2021
buffer: make Blob's constructor more spec-compliant
PR-URL: #37361 Fixes: #37352 Fixes: #37356 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0d564ce commit 458a410

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed
 

‎lib/internal/blob.js

+8-26
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ const {
4848

4949
const {
5050
validateObject,
51-
validateString,
5251
isUint32,
5352
} = require('internal/validators');
5453

@@ -74,22 +73,10 @@ function getSource(source, encoding) {
7473
if (isBlob(source))
7574
return [source.size, source[kHandle]];
7675

77-
if (typeof source === 'string') {
78-
source = lazyBuffer().from(source, encoding);
79-
} else if (isAnyArrayBuffer(source)) {
76+
if (isAnyArrayBuffer(source)) {
8077
source = new Uint8Array(source);
8178
} else if (!isArrayBufferView(source)) {
82-
throw new ERR_INVALID_ARG_TYPE(
83-
'source',
84-
[
85-
'string',
86-
'ArrayBuffer',
87-
'SharedArrayBuffer',
88-
'Buffer',
89-
'TypedArray',
90-
'DataView'
91-
],
92-
source);
79+
source = lazyBuffer().from(`${source}`, encoding);
9380
}
9481

9582
// We copy into a new Uint8Array because the underlying
@@ -110,19 +97,16 @@ class InternalBlob extends JSTransferable {
11097
}
11198

11299
class Blob extends JSTransferable {
113-
constructor(sources = [], options) {
100+
constructor(sources = [], options = {}) {
114101
emitExperimentalWarning('buffer.Blob');
115102
if (sources === null ||
116103
typeof sources[SymbolIterator] !== 'function' ||
117104
typeof sources === 'string') {
118105
throw new ERR_INVALID_ARG_TYPE('sources', 'Iterable', sources);
119106
}
120-
if (options !== undefined)
121-
validateObject(options, 'options');
122-
const {
123-
encoding = 'utf8',
124-
type = '',
125-
} = { ...options };
107+
validateObject(options, 'options');
108+
const { encoding = 'utf8' } = options;
109+
let { type = '' } = options;
126110

127111
let length = 0;
128112
const sources_ = ArrayFrom(sources, (source) => {
@@ -131,16 +115,14 @@ class Blob extends JSTransferable {
131115
return src;
132116
});
133117

134-
// This is a MIME media type but we're not actively checking the syntax.
135-
// But, to be fair, neither does Chrome.
136-
validateString(type, 'options.type');
137-
138118
if (!isUint32(length))
139119
throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF);
140120

141121
super();
142122
this[kHandle] = createBlob(sources_, length);
143123
this[kLength] = length;
124+
125+
type = `${type}`;
144126
this[kType] = RegExpPrototypeTest(disallowedTypeCharacters, type) ?
145127
'' : StringPrototypeToLowerCase(type);
146128
}

‎test/parallel/test-blob.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ assert.throws(() => new Blob({}), {
2222
code: 'ERR_INVALID_ARG_TYPE'
2323
});
2424

25-
assert.throws(() => new Blob(['test', 1]), {
26-
code: 'ERR_INVALID_ARG_TYPE'
27-
});
28-
2925
{
3026
const b = new Blob([]);
3127
assert(b);
@@ -43,15 +39,9 @@ assert.throws(() => new Blob(['test', 1]), {
4339
}
4440

4541
{
46-
assert.throws(() => new Blob([], { type: 1 }), {
47-
code: 'ERR_INVALID_ARG_TYPE'
48-
});
49-
assert.throws(() => new Blob([], { type: false }), {
50-
code: 'ERR_INVALID_ARG_TYPE'
51-
});
52-
assert.throws(() => new Blob([], { type: {} }), {
53-
code: 'ERR_INVALID_ARG_TYPE'
54-
});
42+
assert.strictEqual(new Blob([], { type: 1 }).type, '1');
43+
assert.strictEqual(new Blob([], { type: false }).type, 'false');
44+
assert.strictEqual(new Blob([], { type: {} }).type, '[object object]');
5545
}
5646

5747
{
@@ -193,3 +183,10 @@ assert.throws(() => new Blob(['test', 1]), {
193183
writable: false
194184
});
195185
}
186+
187+
{
188+
const b = new Blob(['test', 42]);
189+
b.text().then(common.mustCall((text) => {
190+
assert.strictEqual(text, 'test42');
191+
}));
192+
}

0 commit comments

Comments
 (0)
Please sign in to comment.