@unikvs/brotli.wasm
Middleware that compresses and decompresses with a WebAssembly Brotli implementation.
Overview
@unikvs/brotli.wasm is a plugin that compresses bytes with Brotli for storage and decompresses them on read. Middleware
It lazily resolves brotli-wasm via dynamic import, so it works in both browsers and Node.js. Use it when you want Brotli regardless of runtime.
nameisBrotliWasm.isOpenbecomestrueafteropen(). It isfalseinitially.- Streaming
encodeStreamanddecodeStreamare not provided. Only batched conversion is supported.
Install with:
npm install @unikvs/brotli.wasm @logtape/logtapepnpm add @unikvs/brotli.wasm @logtape/logtapeyarn add @unikvs/brotli.wasm @logtape/logtapebun add @unikvs/brotli.wasm @logtape/logtapenub add @unikvs/brotli.wasm @logtape/logtapeaube add @unikvs/brotli.wasm @logtape/logtapeUsage
BrotliWasm implements IMiddleware. Pass it to UniKvs with use().
import { BrotliWasm } from "@unikvs/brotli.wasm";
Constructor arguments:
level?number | undefined
Compression level. Specify an integer from 0 to 11. Higher values compress better but take longer. Defaults to 11 when omitted.
number | undefinedThe following example compresses written values with Brotli for storage and decompresses them on read.
import { BrotliWasm } from "@unikvs/brotli.wasm";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ foo: unikvs.Value<Uint8Array> }>()
.use(new BrotliWasm({ level: 11 }))
.persist(new Memory())
.build();
await kvs.open();
await kvs.set("foo", new Uint8Array([0, 1, 2]));
const bytes = await kvs.get("foo");
console.log(bytes); // Decompressed bytes
await kvs.close();
Data types
| Method | Input | Output |
|---|---|---|
encode |
Uint8Array<ArrayBuffer> |
Uint8Array<ArrayBuffer> |
decode |
Uint8Array<ArrayBuffer> |
Uint8Array<ArrayBuffer> |
encodecompresses input bytes with Brotli.decodedecompresses input bytes with Brotli.- Streaming
encodeStreamanddecodeStreamare not provided.
Errors
Errors thrown by this package:
BrotliWasmInitError
Meaning
Thrown when open() fails to initialize the brotli-wasm module.
How to fix
Check that brotli-wasm is installed correctly and that the browser can fetch the WASM file.
Example
The following example catches BrotliWasmInitError.
import { BrotliWasm, BrotliWasmInitError } from "@unikvs/brotli.wasm";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ foo: unikvs.Value<Uint8Array> }>()
.use(new BrotliWasm())
.persist(new Memory())
.build();
try {
await kvs.open();
} catch (ex) {
if (ex instanceof BrotliWasmInitError) {
console.log("Failed to initialize");
} else {
throw ex;
}
}
BrotliWasmInvalidLevelError
actualunknown
The specified compression level.
unknownMeaning
The constructor throws this error when level is not an integer from 0 to 11.
How to fix
Specify an integer from 0 to 11. It defaults to 11 when omitted.
Example
The following example catches BrotliWasmInvalidLevelError.
import { BrotliWasm, BrotliWasmInvalidLevelError } from "@unikvs/brotli.wasm";
try {
new BrotliWasm({ level: 12 });
} catch (ex) {
if (ex instanceof BrotliWasmInvalidLevelError) {
console.log(ex.meta); // { actual: 12 }
} else {
throw ex;
}
}
Calling encode or similar before open() throws a TypeError. Compression and decompression failures normalize the upstream thrown value to an Error before propagating it.