@@ -147,48 +147,54 @@ However, web developers have to pay attention to the situation when attackers ca
147
147
148
148
## Gzip-compress a stream ## {#example-gzip-compress-stream}
149
149
150
- <pre class="example" highlight="js">
150
+ <div class="example" id="example-gzip-compress-stream-code">
151
+ <pre highlight="js">
151
152
const compressedReadableStream
152
153
= inputReadableStream.pipeThrough(new CompressionStream('gzip' ));
153
154
</pre>
155
+ </div>
154
156
155
157
## Deflate-compress an ArrayBuffer to a Uint8Array ## {#example-deflate-compress}
156
158
157
- <pre class="example" highlight="js">
159
+ <div class="example" id="example-deflate-compress-code">
160
+ <pre highlight="js">
158
161
async function compressArrayBuffer(input) {
159
162
const cs = new CompressionStream('deflate' );
163
+
160
164
const writer = cs.writable.getWriter();
161
165
writer.write(input);
162
166
writer.close();
167
+
163
168
const output = [];
164
- const reader = cs.readable.getReader();
165
169
let totalSize = 0;
166
- while (true) {
167
- const { value, done } = await reader.read();
168
- if (done)
169
- break;
170
+ for (const chunk of cs.readable) {
170
171
output.push(value);
171
172
totalSize += value.byteLength;
172
173
}
174
+
173
175
const concatenated = new Uint8Array(totalSize);
174
176
let offset = 0;
175
177
for (const array of output) {
176
178
concatenated.set(array, offset);
177
179
offset += array.byteLength;
178
180
}
181
+
179
182
return concatenated;
180
183
}
181
184
</pre>
185
+ </div>
182
186
183
187
## Gzip-decompress a Blob to Blob ## {#example-gzip-decompress}
184
188
185
- <pre class="example" highlight="js">
189
+ <div class="example" id="example-gzip-decompress-code">
190
+ <pre highlight="js">
186
191
function decompressBlob(blob) {
187
192
const ds = new DecompressionStream('gzip' );
188
193
const decompressionStream = blob.stream().pipeThrough(ds);
189
194
return new Response(decompressionStream).blob();
190
195
}
191
196
</pre>
197
+ </div>
192
198
193
199
<h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
194
200
Thanks to Canon Mukai, Domenic Denicola, and Yutaka Hirano, for their support.
0 commit comments