Skip to content

Commit

Permalink
lib: faster internal createBlob
Browse files Browse the repository at this point in the history
PR-URL: #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]>
  • Loading branch information
H4ad authored and nodejs-github-bot committed Oct 4, 2023
1 parent 86fe5a8 commit 589ac50
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
MathMin,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectSetPrototypeOf,
PromiseReject,
ReflectConstruct,
RegExpPrototypeExec,
Expand Down Expand Up @@ -403,13 +404,23 @@ function ClonedBlob() {
}
ClonedBlob.prototype[kDeserialize] = () => {};

function TransferrableBlob(handle, length, type = '') {
markTransferMode(this, true, false);
this[kHandle] = handle;
this[kType] = type;
this[kLength] = length;
}

ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
ObjectSetPrototypeOf(TransferrableBlob, Blob);

function createBlob(handle, length, type = '') {
return ReflectConstruct(function() {
markTransferMode(this, true, false);
this[kHandle] = handle;
this[kType] = type;
this[kLength] = length;
}, [], Blob);
const transferredBlob = new TransferrableBlob(handle, length, type);

// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
transferredBlob.constructor = Blob;

return transferredBlob;
}

ObjectDefineProperty(Blob.prototype, SymbolToStringTag, {
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-blob-createobjecturl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const assert = require('assert');
const id = URL.createObjectURL(blob);
assert.strictEqual(typeof id, 'string');
const otherBlob = resolveObjectURL(id);
assert.ok(otherBlob instanceof Blob);
assert.strictEqual(otherBlob.constructor, Blob);
assert.strictEqual(otherBlob.size, 5);
assert.strictEqual(
Buffer.from(await otherBlob.arrayBuffer()).toString(),
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,10 @@ assert.throws(() => new Blob({}), {

await new Blob(chunks).arrayBuffer();
})().then(common.mustCall());

{
const blob = new Blob(['hello']);

assert.ok(blob.slice(0, 1).constructor === Blob);
assert.ok(blob.slice(0, 1) instanceof Blob);
}

0 comments on commit 589ac50

Please sign in to comment.