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.
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.
-
data
(required, Uint8Array): The raw input data to compress.
-
A
Uint8Array
containing the compressed data.
-
Throws an error if no compatible zstd module is available in the environment.
-
Internally selects either the native Node.js
zstd-napi
module or the React Nativereact-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.
-
data
(required, Uint8Array): The compressed input data to decompress.
-
A
Uint8Array
containing the decompressed original data.
-
Throws an error if no compatible zstd module is available in the environment.
-
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.
-
A
ZstdModule
instance capable of compressing and decompressing data.
-
Throws an error if neither the Node.js
zstd-napi
package nor the React Nativereact-native-zstd
package is found, with instructions for installation.
-
Tries Node.js native binding
zstd-napi
first. -
If unavailable, tries React Native binding
react-native-zstd
, adapting API usage with data conversion.