Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

@unikvs/zstd.node

Middleware that compresses and decompresses Zstandard with Node.js node:zlib.

Overview

@unikvs/zstd.node is a plugin that compresses bytes with Zstandard for storage and decompresses them on read. Middleware Node.js only

It delegates to the Zstandard support in node:zlib, so it is Node.js-only. Use it when you want small stored values with fast processing.

  • name is ZstdNode.
  • isOpen becomes true after open(). It is false initially.
  • Node.js runtime only. This middleware lazily resolves node:util, node:zlib, and node:stream in open().

Install with:

npm install @unikvs/zstd.node @logtape/logtape
pnpm add @unikvs/zstd.node @logtape/logtape
yarn add @unikvs/zstd.node @logtape/logtape
bun add @unikvs/zstd.node @logtape/logtape
nub add @unikvs/zstd.node @logtape/logtape
aube add @unikvs/zstd.node @logtape/logtape

Usage

ZstdNode implements IMiddleware. Pass it to UniKvs with use().

import { ZstdNode } from "@unikvs/zstd.node";

Constructor arguments:

PropType
level?number | undefined

Compression level. Specify an integer from 1 to 22. Higher values compress better but take longer. Defaults to 3 when omitted.

Typenumber | undefined

The following example compresses written values with Zstandard for storage and decompresses them on read.

import { Memory } from "@unikvs/memory";
import { ZstdNode } from "@unikvs/zstd.node";
import * as unikvs from "unikvs";

const kvs = new unikvs.Builder<{ foo: unikvs.Value<Uint8Array> }>()
  .use(new ZstdNode({ level: 3 }))
  .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>
  • encode compresses input bytes with Zstandard.
  • decode decompresses input bytes with Zstandard.
  • encodeStream connects the input stream to a Zstandard compression stream.
  • decodeStream connects the input stream to a Zstandard decompression stream.
  • Chunks produced by the node:zlib stream are emitted as-is, without combining them into one chunk.

Errors

Errors thrown by this package:

ZstdNodeInvalidLevelError

PropType
actualunknown

The specified compression level.

Typeunknown

Meaning

The constructor throws this error when level is not an integer from 1 to 22.

How to fix

Specify an integer from 1 to 22. It defaults to 3 when omitted.

Example

The following example catches ZstdNodeInvalidLevelError.

import { ZstdNode, ZstdNodeInvalidLevelError } from "@unikvs/zstd.node";

try {
  new ZstdNode({ level: 0 });
} catch (ex) {
  if (ex instanceof ZstdNodeInvalidLevelError) {
    console.log(ex.meta); // { actual: 0 }
  } 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.

Was this page helpful?