Skip to content

Commit 86ee7d1

Browse files
committed
docs: add in stream example
1 parent 47542a2 commit 86ee7d1

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

README.md

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ 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` form with the default export everywhere.
33+
34+
#### In node.js:
3335

3436
```javascript
3537
const * as brotli = require('brotli-wasm');
@@ -40,22 +42,87 @@ const decompressedData = brotli.decompress(compressedData);
4042
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
4143
```
4244

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

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

48-
const brotli = await brotliPromise; // Import is async in browsers due to wasm requirements!
50+
const brotli = await brotliPromise // Import is async in browsers due to wasm requirements!
4951

50-
const compressedData = brotli.compress(Buffer.from('some input'));
51-
const decompressedData = brotli.decompress(compressedData);
52+
const textEncoder = new TextEncoder()
53+
const textDecoder = new TextDecoder()
5254

53-
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
55+
const input = 'some input'
56+
57+
const uncompressedData = textEncoder.encode('some input')
58+
// const uncompressedData = Buffer.from('some input') // With Buffer polyfill
59+
const compressedData = brotli.compress(uncompressedData)
60+
const decompressedData = brotli.decompress(compressedData)
61+
62+
console.log(textDecoder.decode(decompressedData)) // Prints 'some input'
63+
// console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input' with Buffer polyfill
5464
```
5565

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.
66+
The package itself has no runtime dependencies, you will need [browser Buffer polyfill](https://www.npmjs.com/package/browserify-zlib) if you prefer using `Buffer` over using `TextEncoder/TextDecoder`.
5767

58-
If you want to support node & browsers with the same code, you can use the `await` form with the default export everywhere.
68+
#### In browser with streams:
69+
```
70+
import brotliPromise from 'brotli-wasm' // Import the default export
71+
72+
const brotli = await brotliPromise // Import is async in browsers due to wasm requirements!
73+
74+
const input = 'some input'
75+
76+
const inputStream = new ReadableStream({
77+
start (controller) {
78+
controller.enqueue(input)
79+
controller.close()
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+
const textDecoderStream = new TextDecoderStream()
111+
const outputStream = new WritableStream({
112+
write (chunk) {
113+
output += chunk
114+
}
115+
})
116+
117+
let output = ''
118+
await inputStream
119+
.pipeThrough(textEncoderStream)
120+
.pipeThrough(compressionStream)
121+
.pipeThrough(decompressionStream)
122+
.pipeThrough(textDecoderStream)
123+
.pipeTo(outputStream)
124+
console.log(output) // Prints 'some input'
125+
```
59126

60127
## Alternatives
61128

0 commit comments

Comments
 (0)