Zstd API Reference

This document provides a concise API reference for the Zstd class and the related ZstdModule interface, which provide compression and decompression functionality using the Zstandard (zstd) algorithm, with support for both Node.js and React Native environments.

Interfaces

ZstdModule

Interface representing a Zstandard compression module.

Methods
  • compress(data: Uint8Array): Uint8Array Compresses the given data and returns the compressed bytes.

  • decompress(data: Uint8Array): Uint8Array Decompresses the given compressed data and returns the original bytes.

Class: Zstd

Provides static methods for compressing and decompressing binary data using zstd compression, automatically selecting an appropriate implementation based on the runtime environment.

compress

public static compress(data: Uint8Array): Uint8Array

Compresses input data using Zstandard compression.

Parameters
  • data (required, Uint8Array): The raw input data to compress.

Returns
  • A Uint8Array containing the compressed data.

Throws
  • Throws an error if no compatible zstd module is available in the environment.

Details
  • Internally selects either the native Node.js zstd-napi module or the React Native react-native-zstd module.

  • For React Native, performs necessary data conversions to align the APIs.

decompress

public static decompress(data: Uint8Array): Uint8Array

Decompresses input data previously compressed with Zstandard.

Parameters
  • data (required, Uint8Array): The compressed input data to decompress.

Returns
  • A Uint8Array containing the decompressed original data.

Throws
  • Throws an error if no compatible zstd module is available in the environment.

Details
  • Uses the first available zstd module in the environment as with compress.

  • Handles required data conversions for React Native implementations.

Internal Functions (not part of public API)

getAvailableZstdModule

function getAvailableZstdModule(): ZstdModule

Determines and returns a compatible Zstandard compression module available in the current runtime.

Returns
  • A ZstdModule instance capable of compressing and decompressing data.

Throws
  • Throws an error if neither the Node.js zstd-napi package nor the React Native react-native-zstd package is found, with instructions for installation.

Details
  • Tries Node.js native binding zstd-napi first.

  • If unavailable, tries React Native binding react-native-zstd, adapting API usage with data conversion.