Skip to content

Commit 675cbc4

Browse files
committed
Update & simplify the docs to describe the new API
1 parent 41291f3 commit 675cbc4

File tree

1 file changed

+8
-62
lines changed

1 file changed

+8
-62
lines changed

README.md

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ This is battle-tested, in production use in both node & browsers as part of [HTT
1616
npm install brotli-wasm
1717
```
1818

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).
2020

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).
2222

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:
2424

2525
* `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.
2626
* `decompress(Buffer)` - decompresses a buffer using Brotli, returning the original raw data.
2727

28+
### Usage
29+
2830
In node.js:
2931

3032
```javascript
@@ -39,7 +41,7 @@ console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some inp
3941
In browsers:
4042

4143
```javascript
42-
import * as brotliPromise from 'brotli-wasm';
44+
import brotliPromise from 'brotli-wasm'; // Import the default export
4345

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

@@ -49,65 +51,9 @@ const decompressedData = brotli.decompress(compressedData);
4951
console.log(Buffer.from(decompressedData).toString('utf8')); // Prints 'some input'
5052
```
5153

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.
6055

61-
```javascript
62-
import { defineConfig } from "vite";
63-
import wasm from "vite-plugin-wasm";
64-
import { wasm as rollupWasm } from "@rollup/plugin-wasm";
65-
import { viteStaticCopy } from "vite-plugin-static-copy";
66-
67-
// https://vitejs.dev/config/
68-
export default defineConfig({
69-
plugins: [
70-
viteStaticCopy({
71-
targets: [
72-
{
73-
src: require.resolve("brotli-wasm/pkg.web/brotli_wasm_bg.wasm"),
74-
dest: "."
75-
}
76-
]
77-
}),
78-
wasm(),
79-
topLevelAwait()
80-
],
81-
build: {
82-
minify: false,
83-
target: ["esnext"],
84-
sourcemap: true,
85-
rollupOptions: {
86-
treeshake: false,
87-
plugins: [rollupWasm()]
88-
},
89-
ssr: false
90-
}
91-
});
92-
```
93-
94-
and you can call it in your code like this:
95-
96-
```typescript
97-
import init, { decompress } from "brotli-wasm/pkg.web/brotli_wasm";
98-
99-
const initPromise = init("brotli_wasm_bg.wasm");
100-
export const brotliDecompress = async (buffer: Uint8Array): Promise<Uint8Array | undefined> => {
101-
try {
102-
await initPromise;
103-
const output = decompress(buffer);
104-
return output;
105-
} catch (e) {
106-
console.error(e);
107-
return;
108-
}
109-
};
110-
```
56+
If you want to support node & browsers with the same code, you can use the `await` form with the default export everywhere.
11157

11258
## Alternatives
11359

0 commit comments

Comments
 (0)