From c00779891f3dca57aa108c6d840a58d4484c73bb Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 17 Feb 2020 11:54:39 +0000 Subject: [PATCH 1/3] Simplify example. Fixes #29. --- explainer.md | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/explainer.md b/explainer.md index 4a48c1f..81c6f86 100644 --- a/explainer.md +++ b/explainer.md @@ -93,31 +93,14 @@ there are a number of benefits to a built-in capability: const compressedReadableStream = inputReadableStream.pipeThrough(new CompressionStream('gzip')); ``` -### Deflate-compress an ArrayBuffer to a Uint8Array +### Deflate-compress an ArrayBuffer ```javascript -async function compressArrayBuffer(input) { - const cs = new CompressionStream('deflate'); - const writer = cs.writable.getWriter(); - writer.write(input); - writer.close(); - const output = []; - const reader = cs.readable.getReader(); - let totalSize = 0; - while (true) { - const { value, done } = await reader.read(); - if (done) - break; - output.push(value); - totalSize += value.byteLength; - } - const concatenated = new Uint8Array(totalSize); - let offset = 0; - for (const array of output) { - concatenated.set(array, offset); - offset += array.byteLength; - } - return concatenated; +function compressArrayBuffer(input) { + const stream = new Blob(input) + .stream() + .pipeThrough(new CompressionStream('deflate')); + return new Response(stream).arrayBuffer(); } ``` From 3bdea6ad7df0db9212d134ee64bbf3ac5eb0e916 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Mon, 17 Feb 2020 15:31:55 +0000 Subject: [PATCH 2/3] Update explainer.md --- explainer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/explainer.md b/explainer.md index 81c6f86..581d331 100644 --- a/explainer.md +++ b/explainer.md @@ -97,7 +97,7 @@ const compressedReadableStream = inputReadableStream.pipeThrough(new Compression ```javascript function compressArrayBuffer(input) { - const stream = new Blob(input) + const stream = new Blob([input]) .stream() .pipeThrough(new CompressionStream('deflate')); return new Response(stream).arrayBuffer(); From b005ebf1ca3dc3db7c528d682d525cf907f00aa5 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 18 Feb 2020 08:35:38 +0000 Subject: [PATCH 3/3] Use response rather than blob --- explainer.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/explainer.md b/explainer.md index 581d331..39173b7 100644 --- a/explainer.md +++ b/explainer.md @@ -97,8 +97,7 @@ const compressedReadableStream = inputReadableStream.pipeThrough(new Compression ```javascript function compressArrayBuffer(input) { - const stream = new Blob([input]) - .stream() + const stream = new Response(input).body .pipeThrough(new CompressionStream('deflate')); return new Response(stream).arrayBuffer(); }