Skip to content

Commit f7ca69c

Browse files
H4addebadree25
authored andcommitted
lib: faster internal createBlob
PR-URL: nodejs#49730 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Stephen Belanger <[email protected]>
1 parent bc25ec1 commit f7ca69c

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

lib/internal/blob.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
MathMin,
77
ObjectDefineProperties,
88
ObjectDefineProperty,
9+
ObjectSetPrototypeOf,
910
PromiseReject,
1011
ReflectConstruct,
1112
RegExpPrototypeExec,
@@ -391,13 +392,23 @@ function ClonedBlob() {
391392
}
392393
ClonedBlob.prototype[kDeserialize] = () => {};
393394

395+
function TransferrableBlob(handle, length, type = '') {
396+
markTransferMode(this, true, false);
397+
this[kHandle] = handle;
398+
this[kType] = type;
399+
this[kLength] = length;
400+
}
401+
402+
ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
403+
ObjectSetPrototypeOf(TransferrableBlob, Blob);
404+
394405
function createBlob(handle, length, type = '') {
395-
return ReflectConstruct(function() {
396-
markTransferMode(this, true, false);
397-
this[kHandle] = handle;
398-
this[kType] = type;
399-
this[kLength] = length;
400-
}, [], Blob);
406+
const transferredBlob = new TransferrableBlob(handle, length, type);
407+
408+
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
409+
transferredBlob.constructor = Blob;
410+
411+
return transferredBlob;
401412
}
402413

403414
ObjectDefineProperty(Blob.prototype, SymbolToStringTag, {

test/parallel/test-blob-createobjecturl.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const assert = require('assert');
2424
const id = URL.createObjectURL(blob);
2525
assert.strictEqual(typeof id, 'string');
2626
const otherBlob = resolveObjectURL(id);
27+
assert.ok(otherBlob instanceof Blob);
28+
assert.strictEqual(otherBlob.constructor, Blob);
2729
assert.strictEqual(otherBlob.size, 5);
2830
assert.strictEqual(
2931
Buffer.from(await otherBlob.arrayBuffer()).toString(),

test/parallel/test-blob.js

+7
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,10 @@ assert.throws(() => new Blob({}), {
473473

474474
await new Blob(chunks).arrayBuffer();
475475
})().then(common.mustCall());
476+
477+
{
478+
const blob = new Blob(['hello']);
479+
480+
assert.ok(blob.slice(0, 1).constructor === Blob);
481+
assert.ok(blob.slice(0, 1) instanceof Blob);
482+
}

0 commit comments

Comments
 (0)