Skip to content

Commit db8383a

Browse files
committed
Tweak a few small README style points
1 parent 92723d8 commit db8383a

File tree

1 file changed

+61
-60
lines changed

1 file changed

+61
-60
lines changed

README.md

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ For advanced use data-streaming use cases, `CompressStream` and `DecompressStrea
2929

3030
### Usage
3131

32-
If you want to support node & browsers with the same code, you can use the `await` form with the default export everywhere.
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.
3333

3434
#### In node.js:
3535

3636
```javascript
37-
const * as brotli = require('brotli-wasm');
37+
const brotli = require('brotli-wasm');
3838

3939
const compressedData = brotli.compress(Buffer.from('some input'));
4040
const decompressedData = brotli.decompress(compressedData);
@@ -45,83 +45,84 @@ console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some inp
4545
#### In browsers:
4646

4747
```javascript
48-
import brotliPromise from 'brotli-wasm' // Import the default export
48+
import brotliPromise from 'brotli-wasm'; // Import the default export
4949

50-
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!
5151

52-
const textEncoder = new TextEncoder()
53-
const textDecoder = new TextDecoder()
52+
const textEncoder = new TextEncoder();
53+
const textDecoder = new TextDecoder();
5454

55-
const input = 'some input'
55+
const input = 'some input';
5656

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)
57+
const uncompressedData = textEncoder.encode('some input');
58+
const compressedData = brotli.compress(uncompressedData);
59+
const decompressedData = brotli.decompress(compressedData);
6160

62-
console.log(textDecoder.decode(decompressedData)) // Prints 'some input'
63-
// console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input' with Buffer polyfill
61+
console.log(textDecoder.decode(decompressedData)); // Prints 'some input'
6462
```
6563

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`.
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).
6765

6866
#### In browser with streams:
69-
```
70-
import brotliPromise from 'brotli-wasm' // Import the default export
7167

72-
const brotli = await brotliPromise // Import is async in browsers due to wasm requirements!
68+
```javascript
69+
import brotliPromise from 'brotli-wasm'; // Import the default export
7370

74-
const input = 'some input'
71+
const brotli = await brotliPromise; // Import is async in browsers due to wasm requirements!
72+
73+
const input = 'some input';
7574

7675
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()
76+
start (controller) {
77+
controller.enqueue(input);
78+
controller.close();
79+
}
80+
});
81+
82+
const textEncoderStream = new TextEncoderStream();
83+
const compressStream = new brotli.CompressStream();
8484
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()
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();
9898
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-
})
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+
110111
const textDecoderStream = new TextDecoderStream()
111112
const outputStream = new WritableStream({
112-
write (chunk) {
113-
output += chunk
114-
}
115-
})
113+
write (chunk) {
114+
output += chunk;
115+
}
116+
});
116117

117-
let output = ''
118+
let output = '';
118119
await inputStream
119-
.pipeThrough(textEncoderStream)
120-
.pipeThrough(compressionStream)
121-
.pipeThrough(decompressionStream)
122-
.pipeThrough(textDecoderStream)
123-
.pipeTo(outputStream)
124-
console.log(output) // Prints 'some input'
120+
.pipeThrough(textEncoderStream)
121+
.pipeThrough(compressionStream)
122+
.pipeThrough(decompressionStream)
123+
.pipeThrough(textDecoderStream)
124+
.pipeTo(outputStream);
125+
console.log(output); // Prints 'some input'
125126
```
126127

127128
Note that `TransformStream` has become available in all browsers as of mid-2022. https://caniuse.com/mdn-api_transformstream

0 commit comments

Comments
 (0)