Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify example. Fixes #29. #30

Merged
merged 3 commits into from
Feb 18, 2020
Merged
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
28 changes: 5 additions & 23 deletions explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,13 @@ 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 Response(input).body
.pipeThrough(new CompressionStream('deflate'));
return new Response(stream).arrayBuffer();
}
```

Expand Down