@unikvs/compression
How to use @unikvs/compression, middleware that compresses and decompresses bytes in a given format.
Overview
Middleware that compresses and decompresses bytes in a given format. Middleware
It converts both single reads/writes and streams transparently, without changing caller types.
nameis"Compression".isOpenis alwaystrue.- Available formats depend on the runtime’s
CompressionStreamandDecompressionStream.
Install with:
npm install @unikvs/compressionpnpm add @unikvs/compressionyarn add @unikvs/compressionbun add @unikvs/compressionnub add @unikvs/compressionaube add @unikvs/compressionUsage
Compression implements IMiddleware. Pass it to UniKvs with use().
import { Compression } from "@unikvs/compression";
Constructor arguments:
formatCompressionFormat
Compression format to use. Specify a format supported by both `CompressionStream` and `DecompressionStream`.
CompressionFormatThe following example stores bytes compressed with gzip and decompresses them on read.
import { Compression } from "@unikvs/compression";
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ file: unikvs.Value<Uint8Array> }>()
.use(new Compression("gzip"))
.persist(new Memory())
.build();
await kvs.open();
await kvs.set("file", new Uint8Array([0x01, 0x02, 0x03]));
const file = await kvs.get("file");
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 bytes in the given format.decodedecompresses compressed bytes in the given format.encodeStreampasses the stream throughCompressionStreamas-is.decodeStreampasses the stream throughDecompressionStreamas-is.- Chunk boundaries are left to the compressor and decompressor.
Errors
This package has no package-specific errors.
If an unsupported format is specified or corrupted data is decompressed, the runtime’s native error propagates unchanged.