Skip to content

Commit

Permalink
lib: fix to add resolve() before return at Blob.stream()'s source.pull()
Browse files Browse the repository at this point in the history
Add lacked calling resolve() for finish ReadableStream source.pull().

Fixes: nodejs#48668
Fixes: nodejs#48916
Fixes: nodejs#48232
Refs: nodejs@8cc1438
PR-URL: nodejs#48935
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
  • Loading branch information
bellbind authored and rluvaton committed Aug 15, 2023
1 parent 4f4f659 commit 92c3615
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ class Blob {
queueMicrotask(() => {
if (c.desiredSize <= 0) {
// A manual backpressure check.
if (this.pendingPulls.length !== 0) {
// A case of waiting pull finished (= not yet canceled)
const pending = this.pendingPulls.shift();
pending.resolve();
}
return;
}
readNext();
Expand Down
58 changes: 58 additions & 0 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,64 @@ assert.throws(() => new Blob({}), {
reader.closed.then(common.mustCall());
})().then(common.mustCall());

(async () => {
const b = new Blob(['A', 'B', 'C']);
const stream = b.stream();
const chunks = [];
const decoder = new TextDecoder();
await stream.pipeTo(new WritableStream({
write(chunk) {
chunks.push(decoder.decode(chunk, { stream: true }));
}
}));
assert.strictEqual(chunks.join(''), 'ABC');
})().then(common.mustCall());

(async () => {
const b = new Blob(['A', 'B', 'C']);
const stream = b.stream();
const chunks = [];
const decoder = new TextDecoder();
await stream.pipeTo(
new WritableStream({
write(chunk) {
chunks.push(decoder.decode(chunk, { stream: true }));
},
})
);
assert.strictEqual(chunks.join(''), 'ABC');
})().then(common.mustCall());

(async () => {
// Ref: https://github.com/nodejs/node/issues/48668
const chunks = [];
const stream = new Blob(['Hello world']).stream();
const decoder = new TextDecoder();
await Promise.resolve();
await stream.pipeTo(
new WritableStream({
write(chunk) {
chunks.push(decoder.decode(chunk, { stream: true }));
},
})
);
assert.strictEqual(chunks.join(''), 'Hello world');
})().then(common.mustCall());

(async () => {
// Ref: https://github.com/nodejs/node/issues/48668
if (common.hasCrypto) {
// Can only do this test if we have node built with crypto
const file = new Blob(['<svg></svg>'], { type: 'image/svg+xml' });
const url = URL.createObjectURL(file);
const res = await fetch(url);
const blob = await res.blob();
assert.strictEqual(blob.size, 11);
assert.strictEqual(blob.type, 'image/svg+xml');
assert.strictEqual(await blob.text(), '<svg></svg>');
}
})().then(common.mustCall());

(async () => {
const b = new Blob(Array(10).fill('hello'));
const stream = b.stream();
Expand Down

0 comments on commit 92c3615

Please sign in to comment.