Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/core/decode_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
Comment thread
nicolo-ribaudo marked this conversation as resolved.
Resolved

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) {
Expand Down
50 changes: 50 additions & 0 deletions test/unit/stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading