Skip to content

Commit 815c569

Browse files
authored
Merge pull request #15 from willfarrell/feature/stream-example
Docs: add in stream example
2 parents 47542a2 + db8383a commit 815c569

File tree

1 file changed

+77
-7
lines changed

1 file changed

+77
-7
lines changed

README.md

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,103 @@ For advanced use data-streaming use cases, `CompressStream` and `DecompressStrea
2929

3030
### Usage
3131

32-
In node.js:
32+
If you want to support node & browsers with the same code, you can use the `await` browser-compatible form with the default export everywhere.
33+
34+
#### In node.js:
3335

3436
```javascript
35-
const * as brotli = require('brotli-wasm');
37+
const brotli = require('brotli-wasm');
3638

3739
const compressedData = brotli.compress(Buffer.from('some input'));
3840
const decompressedData = brotli.decompress(compressedData);
3941

4042
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
4143
```
4244

43-
In browsers:
45+
#### In browsers:
4446

4547
```javascript
4648
import brotliPromise from 'brotli-wasm'; // Import the default export
4749

4850
const brotli = await brotliPromise; // Import is async in browsers due to wasm requirements!
4951

50-
const compressedData = brotli.compress(Buffer.from('some input'));
52+
const textEncoder = new TextEncoder();
53+
const textDecoder = new TextDecoder();
54+
55+
const input = 'some input';
56+
57+
const uncompressedData = textEncoder.encode('some input');
58+
const compressedData = brotli.compress(uncompressedData);
5159
const decompressedData = brotli.decompress(compressedData);
5260

53-
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
61+
console.log(textDecoder.decode(decompressedData)); // Prints 'some input'
5462
```
5563

56-
The package itself has no runtime dependencies, but you will need a [browser Buffer polyfill](https://www.npmjs.com/package/browserify-zlib) for the above example code, or you can do the same using TextEncoder/Decoder instead if you prefer.
64+
The package itself has no runtime dependencies, although if you prefer using `Buffer` over using `TextEncoder/TextDecoder` you may want a [browser Buffer polyfill](https://www.npmjs.com/package/browserify-zlib).
65+
66+
#### In browser with streams:
67+
68+
```javascript
69+
import brotliPromise from 'brotli-wasm'; // Import the default export
70+
71+
const brotli = await brotliPromise; // Import is async in browsers due to wasm requirements!
72+
73+
const input = 'some input';
74+
75+
const inputStream = new ReadableStream({
76+
start (controller) {
77+
controller.enqueue(input);
78+
controller.close();
79+
}
80+
});
81+
82+
const textEncoderStream = new TextEncoderStream();
83+
const compressStream = new brotli.CompressStream();
84+
const compressionStream = new TransformStream({
85+
start () {},
86+
transform (chunk, controller) {
87+
controller.enqueue(compressStream.compress(chunk, 100));
88+
},
89+
flush (controller) {
90+
if (compressStream.result() === brotli.BrotliStreamResult.NeedsMoreInput) {
91+
controller.enqueue(compressStream.compress(undefined, 100));
92+
}
93+
controller.terminate();
94+
}
95+
});
96+
97+
const decompressStream = new brotli.DecompressStream();
98+
const decompressionStream = new TransformStream({
99+
start () {},
100+
transform (chunk, controller) {
101+
controller.enqueue(decompressStream.decompress(chunk, 100));
102+
},
103+
flush (controller) {
104+
if (decompressStream.result() === brotli.BrotliStreamResult.NeedsMoreInput) {
105+
controller.enqueue(decompressStream.decompress(undefined, 100));
106+
}
107+
controller.terminate();
108+
}
109+
});
110+
111+
const textDecoderStream = new TextDecoderStream()
112+
const outputStream = new WritableStream({
113+
write (chunk) {
114+
output += chunk;
115+
}
116+
});
117+
118+
let output = '';
119+
await inputStream
120+
.pipeThrough(textEncoderStream)
121+
.pipeThrough(compressionStream)
122+
.pipeThrough(decompressionStream)
123+
.pipeThrough(textDecoderStream)
124+
.pipeTo(outputStream);
125+
console.log(output); // Prints 'some input'
126+
```
57127

58-
If you want to support node & browsers with the same code, you can use the `await` form with the default export everywhere.
128+
Note that `TransformStream` has become available in all browsers as of mid-2022. https://caniuse.com/mdn-api_transformstream
59129

60130
## Alternatives
61131

0 commit comments

Comments
 (0)