Skip to content

Commit 500256d

Browse files
targosfoxxyz
authored andcommitted
buffer: make Blob's constructor more spec-compliant
PR-URL: nodejs#37361 Backport-PR-URL: nodejs#39704 Fixes: nodejs#37352 Fixes: nodejs#37356 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ea81e60 commit 500256d

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
@@ -50,7 +50,6 @@ const {
5050

5151
const {
5252
validateObject,
53-
validateString,
5453
isUint32,
5554
} = require('internal/validators');
5655

@@ -76,22 +75,10 @@ function getSource(source, encoding) {
7675
if (isBlob(source))
7776
return [source.size, source[kHandle]];
7877

79-
if (typeof source === 'string') {
80-
source = lazyBuffer().from(source, encoding);
81-
} else if (isAnyArrayBuffer(source)) {
78+
if (isAnyArrayBuffer(source)) {
8279
source = new Uint8Array(source);
8380
} else if (!isArrayBufferView(source)) {
84-
throw new ERR_INVALID_ARG_TYPE(
85-
'source',
86-
[
87-
'string',
88-
'ArrayBuffer',
89-
'SharedArrayBuffer',
90-
'Buffer',
91-
'TypedArray',
92-
'DataView'
93-
],
94-
source);
81+
source = lazyBuffer().from(`${source}`, encoding);
9582
}
9683

9784
// We copy into a new Uint8Array because the underlying
@@ -112,19 +99,16 @@ class InternalBlob extends JSTransferable {
11299
}
113100

114101
class Blob extends JSTransferable {
115-
constructor(sources = [], options) {
102+
constructor(sources = [], options = {}) {
116103
emitExperimentalWarning('buffer.Blob');
117104
if (sources === null ||
118105
typeof sources[SymbolIterator] !== 'function' ||
119106
typeof sources === 'string') {
120107
throw new ERR_INVALID_ARG_TYPE('sources', 'Iterable', sources);
121108
}
122-
if (options !== undefined)
123-
validateObject(options, 'options');
124-
const {
125-
encoding = 'utf8',
126-
type = '',
127-
} = { ...options };
109+
validateObject(options, 'options');
110+
const { encoding = 'utf8' } = options;
111+
let { type = '' } = options;
128112

129113
let length = 0;
130114
const sources_ = ArrayFrom(sources, (source) => {
@@ -133,16 +117,14 @@ class Blob extends JSTransferable {
133117
return src;
134118
});
135119

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

143123
super();
144124
this[kHandle] = createBlob(sources_, length);
145125
this[kLength] = length;
126+
127+
type = `${type}`;
146128
this[kType] = RegExpPrototypeTest(disallowedTypeCharacters, type) ?
147129
'' : StringPrototypeToLowerCase(type);
148130
}

test/parallel/test-blob.js

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

26-
assert.throws(() => new Blob(['test', 1]), {
27-
code: 'ERR_INVALID_ARG_TYPE'
28-
});
29-
3026
{
3127
const b = new Blob([]);
3228
assert(b);
@@ -44,15 +40,9 @@ assert.throws(() => new Blob(['test', 1]), {
4440
}
4541

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

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

0 commit comments

Comments
 (0)