@@ -179,48 +179,54 @@ However, web developers have to pay attention to the situation when attackers ca
179
179
180
180
## Gzip-compress a stream ## {#example-gzip-compress-stream}
181
181
182
- <pre class="example" highlight="js">
182
+ <div class="example" id="example-gzip-compress-stream-code">
183
+ <pre highlight="js">
183
184
const compressedReadableStream
184
185
= inputReadableStream.pipeThrough(new CompressionStream('gzip' ));
185
186
</pre>
187
+ </div>
186
188
187
189
## Deflate-compress an ArrayBuffer to a Uint8Array ## {#example-deflate-compress}
188
190
189
- <pre class="example" highlight="js">
191
+ <div class="example" id="example-deflate-compress-code">
192
+ <pre highlight="js">
190
193
async function compressArrayBuffer(input) {
191
194
const cs = new CompressionStream('deflate' );
195
+
192
196
const writer = cs.writable.getWriter();
193
197
writer.write(input);
194
198
writer.close();
199
+
195
200
const output = [];
196
- const reader = cs.readable.getReader();
197
201
let totalSize = 0;
198
- while (true) {
199
- const { value, done } = await reader.read();
200
- if (done)
201
- break;
202
+ for (const chunk of cs.readable) {
202
203
output.push(value);
203
204
totalSize += value.byteLength;
204
205
}
206
+
205
207
const concatenated = new Uint8Array(totalSize);
206
208
let offset = 0;
207
209
for (const array of output) {
208
210
concatenated.set(array, offset);
209
211
offset += array.byteLength;
210
212
}
213
+
211
214
return concatenated;
212
215
}
213
216
</pre>
217
+ </div>
214
218
215
219
## Gzip-decompress a Blob to Blob ## {#example-gzip-decompress}
216
220
217
- <pre class="example" highlight="js">
221
+ <div class="example" id="example-gzip-decompress-code">
222
+ <pre highlight="js">
218
223
function decompressBlob(blob) {
219
224
const ds = new DecompressionStream('gzip' );
220
225
const decompressionStream = blob.stream().pipeThrough(ds);
221
226
return new Response(decompressionStream).blob();
222
227
}
223
228
</pre>
229
+ </div>
224
230
225
231
<h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
226
232
Thanks to Canon Mukai, Domenic Denicola, and Yutaka Hirano, for their support.
0 commit comments