diff --git a/src/core/decode_stream.js b/src/core/decode_stream.js index 094c2795cf6ba..90b0cbe4df69d 100644 --- a/src/core/decode_stream.js +++ b/src/core/decode_stream.js @@ -128,25 +128,30 @@ class DecodeStream extends BaseStream { try { const { readable, writable } = new DecompressionStream(name); const writer = writable.getWriter(); - await writer.ready; - - // We can't await writer.write() because it'll block until the reader - // starts which happens few lines below. - writer - .write(bytes) - .then(async () => { - await writer.ready; - await writer.close(); - }) - .catch(() => {}); + // Don't await the writer: it only makes progress once the reader pulls. + // close() is queued behind write(), so the stream is only closed once + // all the bytes have been written. + // Handle decompression errors through reader.read() below. + writer.write(bytes).catch(() => {}); + writer.close().catch(() => {}); const chunks = []; let totalLength = 0; - for await (const chunk of readable) { + // Faster than `for await`. + const reader = readable.getReader(); + while (true) { + const { value: chunk, done } = await reader.read(); + if (done) { + break; + } chunks.push(chunk); totalLength += chunk.byteLength; } + // Avoid copying single-chunk output. + if (chunks.length === 1) { + return { decompressed: chunks[0], compressed: bytes }; + } const data = new Uint8Array(totalLength); let offset = 0; for (const chunk of chunks) { diff --git a/test/unit/stream_spec.js b/test/unit/stream_spec.js index b022018cd661c..97326e3d9c826 100644 --- a/test/unit/stream_spec.js +++ b/test/unit/stream_spec.js @@ -86,6 +86,56 @@ describe("stream", function () { expect(await flate.asyncGetBytes()).toBeNull(); expect(flate.getString()).toEqual(expected.substring(0, 107)); }); + + describe("with DecompressionStream", function () { + async function deflate(data) { + const { readable, writable } = new CompressionStream("deflate"); + const writer = writable.getWriter(); + writer.write(data); + writer.close(); + return new Response(readable).bytes(); + } + + const data = new Uint8Array(100000); + for (let i = 0; i < data.length; i++) { + data[i] = (i * 7) ^ (i >> 9); + } + let compressed; + + beforeAll(async function () { + compressed = await deflate(data); + }); + + it("should decode a large stream", async function () { + const flate = new FlateStream(new Stream(compressed)); + expect(await flate.asyncGetBytes()).toEqual(data); + }); + + it("should decode an image stream, whatever its expected length", async function () { + for (const length of [data.length, 1000, 2 * data.length]) { + const flate = new FlateStream(new Stream(compressed)); + expect(await flate.getImageData(length)).toEqual( + data.subarray(0, length) + ); + } + }); + + it("should decode an empty stream", async function () { + // Empty stored blocks, to get above the async length threshold. + const emptyBlock = final => [final ? 1 : 0, 0, 0, 0xff, 0xff]; + const adler32 = [0, 0, 0, 1]; + const empty = new Uint8Array([ + 120, + 1, + ...Array(256).fill(emptyBlock(false)).flat(), + ...emptyBlock(true), + ...adler32, + ]); + const flate = new FlateStream(new Stream(empty)); + expect(await flate.asyncGetBytes()).toEqual(new Uint8Array(0)); + expect(flate.isAsync).toEqual(true); + }); + }); }); describe("StreamsSequenceStream", function () {