diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 0d0e9906dbdb31..167c0521b4573d 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -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(); diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 27dee5690d7e06..a517bad1ccb42d 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -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([''], { 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(), ''); + } +})().then(common.mustCall()); + (async () => { const b = new Blob(Array(10).fill('hello')); const stream = b.stream();