Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 589ac50

Browse filesBrowse files
H4adnodejs-github-bot
authored andcommitted
lib: faster internal createBlob
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>
1 parent 86fe5a8 commit 589ac50
Copy full SHA for 589ac50

File tree

Expand file treeCollapse file tree

3 files changed

+26
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-6
lines changed
Open diff view settings
Collapse file

‎lib/internal/blob.js‎

Copy file name to clipboardExpand all lines: lib/internal/blob.js
+17-6Lines changed: 17 additions & 6 deletions
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,
@@ -403,13 +404,23 @@ function ClonedBlob() {
403404
}
404405
ClonedBlob.prototype[kDeserialize] = () => {};
405406

407+
function TransferrableBlob(handle, length, type = '') {
408+
markTransferMode(this, true, false);
409+
this[kHandle] = handle;
410+
this[kType] = type;
411+
this[kLength] = length;
412+
}
413+
414+
ObjectSetPrototypeOf(TransferrableBlob.prototype, Blob.prototype);
415+
ObjectSetPrototypeOf(TransferrableBlob, Blob);
416+
406417
function createBlob(handle, length, type = '') {
407-
return ReflectConstruct(function() {
408-
markTransferMode(this, true, false);
409-
this[kHandle] = handle;
410-
this[kType] = type;
411-
this[kLength] = length;
412-
}, [], Blob);
418+
const transferredBlob = new TransferrableBlob(handle, length, type);
419+
420+
// Fix issues like: https://github.com/nodejs/node/pull/49730#discussion_r1331720053
421+
transferredBlob.constructor = Blob;
422+
423+
return transferredBlob;
413424
}
414425

415426
ObjectDefineProperty(Blob.prototype, SymbolToStringTag, {
Collapse file

‎test/parallel/test-blob-createobjecturl.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-blob-createobjecturl.js
+2Lines changed: 2 additions & 0 deletions
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(),
Collapse file

‎test/parallel/test-blob.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-blob.js
+7Lines changed: 7 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.