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 <yagiz@nizipli.com>
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • 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.