Zstd Library Guide

The @hiero-did-sdk/zstd package provides the Zstd class — a simple and efficient interface for compressing and decompressing data using the Zstandard (zstd) compression algorithm, with runtime adaptability for Node.js and React Native environments.

Overview

Use the Zstd library to:

  • Compress binary data into smaller size using the fast and powerful Zstandard algorithm.

  • Decompress previously compressed zstd data back to original form.

  • Automatically select a compatible compression module depending on runtime environment (Node.js native or React Native polyfill).

  • Support Uint8Array binary inputs and outputs for seamless integration with modern JavaScript/TypeScript applications.

Zstandard is a lossless compression algorithm known for offering excellent compression ratios at high speed. It supports a wide range of compression levels and is suitable for real-time compression scenarios.

Initialization

No explicit initialization is required. At runtime, Zstd attempts to load:

  • The Node.js zstd-napi native add-on module for high-performance compression/decompression.

  • If unavailable, a React Native compatible package react-native-zstd with internal data conversions to reconcile differences.

If no compatible module is found, an error is thrown prompting installation of one of these dependencies.

Basic Usage

import { Zstd } from '@hiero-did-sdk/zstd';

const inputData = new Uint8Array([/* binary data */]);

// Compress data
const compressedData = Zstd.compress(inputData);

// Decompress data
const originalData = Zstd.decompress(compressedData);

console.log('Original and decompressed data are equal:',
  inputData.length === originalData.length && inputData.every((val, i) => val === originalData[i])
);

Parameters:

  • inputData (Uint8Array, required): The raw binary data to compress or the compressed data to decompress.

Returns:

  • Uint8Array containing compressed bytes for compress.

  • Uint8Array containing decompressed original data for decompress.

Throws:

  • Error if no compatible zstd module is found.

Details

  • Compression is performed as a single zstd frame in memory.

  • The underlying native or polyfill module handles the actual compression logic.

  • For React Native, data format conversion (e.g. base64, buffers) is applied internally to match differing APIs.