@unikvs/brotli.node
Middleware that compresses and decompresses Brotli with Node.js node:zlib.
Overview
@unikvs/brotli.node is a plugin that compresses bytes with Brotli for storage and decompresses them on read. Middleware Node.js only
Because Web CompressionStream has no Brotli format, compression is always delegated to node:zlib. Use it to keep stored values small.
nameisBrotli.isOpenbecomestrueafteropen(). It isfalseinitially.- Node.js runtime only. This middleware lazily resolves
node:util,node:zlib, andnode:streaminopen().
Install with:
npm install @unikvs/brotli.node @logtape/logtapepnpm add @unikvs/brotli.node @logtape/logtapeyarn add @unikvs/brotli.node @logtape/logtapebun add @unikvs/brotli.node @logtape/logtapenub add @unikvs/brotli.node @logtape/logtapeaube add @unikvs/brotli.node @logtape/logtapeUsage
Brotli implements IMiddleware. Pass it to UniKvs with use().
import { Brotli } from "@unikvs/brotli.node";
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 { Brotli } from "@unikvs/brotli.node";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ foo: unikvs.Value<Uint8Array> }>()
.use(new Brotli({ 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> |
encodeStream |
Uint8Array<ArrayBuffer> |
Uint8Array<ArrayBuffer> |
decodeStream |
Uint8Array<ArrayBuffer> |
Uint8Array<ArrayBuffer> |
encodecompresses input bytes with Brotli.decodedecompresses input bytes with Brotli.encodeStreamconnects the input stream to a Brotli compression stream.decodeStreamconnects the input stream to a Brotli decompression stream.- Chunks produced by the
node:zlibstream are emitted as-is, without combining them into one chunk.
Errors
Errors thrown by this package:
BrotliInvalidLevelError
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 BrotliInvalidLevelError.
import { Brotli, BrotliInvalidLevelError } from "@unikvs/brotli.node";
try {
new Brotli({ level: 12 });
} catch (ex) {
if (ex instanceof BrotliInvalidLevelError) {
console.log(ex.meta); // { actual: 12 }
} else {
throw ex;
}
}
If open() fails to resolve the runtime, this middleware throws UnsupportedRuntimeError. Calling encode or similar before open() throws a TypeError. Decompression failures propagate the upstream error unchanged.