You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-62Lines changed: 8 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,15 +16,17 @@ This is battle-tested, in production use in both node & browsers as part of [HTT
16
16
npm install brotli-wasm
17
17
```
18
18
19
-
You should be able to import this directly into Node, as normal, or into a browser using any bundler that supports ES modules & webassembly (e.g. Webpack v4 or v5).
19
+
You should be able to import this directly into Node, as normal, or in a browser using any bundler that supports ES modules & webassembly (e.g. Webpack v4 or v5, Vite, Rollup, and most others).
20
20
21
-
The browser build supports both sync (v4 or v5 syncWebAssembly mode) and async (v5 asyncWebAssembly) builds. When imported in a browser build the module always exports a _promise_, not a fixed value, as this is a requirement for synchronous builds, and you will need to`await`this after import.
21
+
For each target (node.js, commonjs bundlers & ESM bundlers) this module exports a different WASM file & setup, with a slightly different entrypoint. These entrypoints all expose a consistent default-export API, in addition to some other exports that may vary (e.g. Node exposes the brotli methods synchronously, while browsers always require an`await`due to WASM limitations).
22
22
23
-
In both builds, the module exposes two methods:
23
+
In all builds (after waiting for the exported promise in browsers) the module exposes two core methods:
24
24
25
25
*`compress(Buffer, [options])` - compresses a buffer using Brotli, returning the compressed buffer. An optional options object can be provided. The only currently supported option is `quality`: a number between 1 and 11.
26
26
*`decompress(Buffer)` - decompresses a buffer using Brotli, returning the original raw data.
27
27
28
+
### Usage
29
+
28
30
In node.js:
29
31
30
32
```javascript
@@ -39,7 +41,7 @@ console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some inp
39
41
In browsers:
40
42
41
43
```javascript
42
-
import*asbrotliPromisefrom'brotli-wasm';
44
+
importbrotliPromisefrom'brotli-wasm';// Import the default export
43
45
44
46
constbrotli=await brotliPromise; // Import is async in browsers due to wasm requirements!
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
50
52
```
51
53
52
-
You'll need a [browser Buffer polyfill](https://www.npmjs.com/package/browserify-zlib) for the above, or you can do the same using TextEncoder/Decoder instead if you prefer.
53
-
54
-
If you want to support node & browsers with the same code, you can use the latter `await` form here everywhere (since awaiting the fixed value in node just returns the value as-is).
55
-
56
-
57
-
## Vite and Rollup
58
-
59
-
For usage in Vite, you have to use the `vite-plugin-wasm` and `static-files` plugins. The config looks like this:
54
+
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.
0 commit comments