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: #48668
Fixes: #48916
Fixes: #48232
Refs: 8cc1438
PR-URL: #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 RafaelGSS committed Aug 17, 2023
1 parent 8380800 commit dca8678
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
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
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 dca8678

Please sign in to comment.