Skip to content

Commit

Permalink
Editorial: clean up examples
Browse files Browse the repository at this point in the history
* Wrap the <pre>s in <div>s to work better with the WHATWG stylesheet.
* Add IDs to suppress Bikeshed warnings
* Clean up the code a bit for the deflate-compress example, notably by using async iteration.
  • Loading branch information
domenic authored and annevk committed May 30, 2024
1 parent e29f858 commit f9bbac4
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -147,48 +147,54 @@ However, web developers have to pay attention to the situation when attackers ca

## Gzip-compress a stream ## {#example-gzip-compress-stream}

<pre class="example" highlight="js">
<div class="example" id="example-gzip-compress-stream-code">
<pre highlight="js">
const compressedReadableStream
= inputReadableStream.pipeThrough(new CompressionStream('gzip'));
</pre>
</div>

## Deflate-compress an ArrayBuffer to a Uint8Array ## {#example-deflate-compress}

<pre class="example" highlight="js">
<div class="example" id="example-deflate-compress-code">
<pre highlight="js">
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;
for (const chunk of cs.readable) {
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;
}
</pre>
</div>

## Gzip-decompress a Blob to Blob ## {#example-gzip-decompress}

<pre class="example" highlight="js">
<div class="example" id="example-gzip-decompress-code">
<pre highlight="js">
function decompressBlob(blob) {
const ds = new DecompressionStream('gzip');
const decompressionStream = blob.stream().pipeThrough(ds);
return new Response(decompressionStream).blob();
}
</pre>
</div>

<h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
Thanks to Canon Mukai, Domenic Denicola, and Yutaka Hirano, for their support.
Expand Down

0 comments on commit f9bbac4

Please sign in to comment.