@push-stream-std/push-stream-lzd (0.0.4)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-stream-lzd@0.0.4"@push-stream-std/push-stream-lzd": "0.0.4"About this package
push-stream-lzd
LZD+ dictionary compression and decompression as push-stream transforms.
Published as @push-stream-std/push-stream-lzd.
Synopsis
import { createDocumentCompressorTransform, createDocumentDecompressorTransform } from '@push-stream-std/push-stream-lzd';
const compressor = createDocumentCompressorTransform({ mode: 'stateful' });
const decompressor = createDocumentDecompressorTransform({ mode: 'stateful' });
source.pipe(compressor).pipe(sink);
compressedSource.pipe(decompressor).pipe(resultSink);
import { compressLzdPlus, decompressLzdPlus, createLzdPlusState } from '@push-stream-std/push-stream-lzd';
const state = createLzdPlusState();
const payload = compressLzdPlus('hello world', state);
const text = decompressLzdPlus(payload, state);
console.log(text); // 'hello world'
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-stream-lzd
Description
Provides LZD+ dictionary-based compression and decompression through a suffix trie with open-addressing hash maps. The codec layer offers both object-factor and binary encoding modes. The stream layer provides through transforms (createDocumentCompressorTransform / createDocumentDecompressorTransform) that operate on structured document frames, supporting both stateless (per-frame dictionary) and stateful (accumulated dictionary) modes with optional durability for checkpoint/restore.
API
Codec layer — @push-stream-std/push-stream-lzd
createLzdPlusState(dictionary)
Creates a fresh LZD+ compression state initialized with an optional dictionary of seed strings.
- dictionary
string[](default[]) – Initial dictionary entries for seeding the trie. - Returns A state object containing
dictionaryStart,dictionaryLength,textStore,trie, andscratch.
compressLzdPlus(text, state, options)
Compresses text using the LZD+ codec, returning a factor-based payload.
- text
string– Text to compress. - state
object– Compression state fromcreateLzdPlusState. - options
object(optional):- emitAdditions
boolean(defaulttrue) – Include dictionary addition positions in the result.
- emitAdditions
- Returns
{ encoding: 'lzd+', length, factors, additions }.
compressLzdPlusBinary(text, state, options)
Compresses text using the LZD+ codec, returning a binary Uint8Array payload.
- text
string– Text to compress. - state
object– Compression state. - options
object(optional):- emitAdditions
boolean(defaulttrue) – Include dictionary addition positions.
- emitAdditions
- Returns
{ encoding: 'lzd+', length, data: Uint8Array, additions }.
decompressLzdPlus(payload, state)
Decompresses a factor-based payload back to text.
- payload
object– Must haveencoding: 'lzd+'andfactorsarray. - state
object– Decompression state (updated in place with new dictionary entries). - Returns
string– The reconstructed text.
encodeLzdPlusPayload(payload)
Encodes a factor-based payload to a binary Uint8Array with the LZD+ wire format (magic header, version, length, factor count, factor body).
decodeLzdPlusPayload(bytes)
Decodes a binary Uint8Array in the LZD+ wire format back to a factor-based payload.
serializeLzdPlusState(state)
Serializes the current compression state's dictionary to a JSON-compatible snapshot.
- Returns
{ dictionary: string[] }.
restoreLzdPlusState(snapshot)
Restores a compression state from a previously serialized snapshot.
- snapshot
{ dictionary: string[] }. - Returns A fresh state object.
Stream layer
createDocumentCompressorTransform(options)
Creates a through stream that compresses textual content in structured document frames.
- options
object(optional):- mode
'stateless' | 'stateful'(default'stateless') – Dictionary lifetime. Stateful accumulates across frames; stateless creates a new dictionary per frame. - durability
object(optional) – If{ enabled: true }, emits checkpoint - keepOriginal
boolean(defaultfalse) – If true, preservesoriginalTextin the frame. - includeDictionarySnapshot
boolean(defaultfalse) – Includes the serialized dictionary in each compressed payload. - extractText
(frame) => { text, locationKey } | null– Custom text extractor. - attachCompressed
(frame, payload, info, options) => frame– Custom payload attacher.
- mode
- Returns A through object with
pipe(),write(),end(),resume(),abort(),paused,ended,source,sink.
createDocumentDecompressorTransform(options)
Creates a through stream that decompresses LZD+ content back to text in structured document frames.
- options
object(optional):- mode
'stateless' | 'stateful'(default'stateless') – Dictionary lifetime. - durability
object(optional) – If{ enabled: true }, supports state restore via META frames. - extractCompressed
(frame) => { payload, locationKey } | null– Custom compressed payload extractor. - attachText
(frame, text, info) => frame– Custom text attacher.
- mode
- Returns A through object with the same through stream interface.
Tests
npm test
Requirements
Node.js 18 or later. ESM only.
License
MIT
Dependencies
Dependencies
| ID | Version |
|---|---|
| lz4js | ^0.2.0 |