Appearance
The Zstandard WASM library, provides a simplified wrapper around the Zstandard c++ library.
See Zstandard for more details.
ts
import { Zstd } from "@hpcc-js/wasm-zstd";
const zstd = await Zstd.load();
// Generate some "data"
const data = new Uint8Array(Array.from({ length: 100000 }, (_, i) => i % 256));
const compressed_data = zstd.compress(data);
const decompressed_data = zstd.decompress(compressed_data);Methods
load()
staticload():Promise<Zstd>
Compiles and instantiates the raw wasm.
INFO
In general WebAssembly compilation is disallowed on the main thread if the buffer size is larger than 4KB, hence forcing load to be asynchronous;
Returns
Promise<Zstd>
A promise to an instance of the Zstd class.
unload()
staticunload():Promise<void>
Unloades the compiled wasm instance.
Returns
Promise<void>
version()
version():
string
Returns
string
The Zstd c++ version
reset()
reset():
void
Resets the internal compression/decompression state.
Returns
void
setCompressionLevel()
setCompressionLevel(
level):void
Sets the compression level for streaming compression.
Parameters
level
number
Compression level (use minCLevel() to maxCLevel())
Returns
void
compress()
compress(
data,compressionLevel?):Uint8Array
Parameters
data
Uint8Array
Data to be compressed
compressionLevel?
number = ...
Compression v Speed tradeoff, when omitted it will default to zstd.defaultCLevel() which is currently 3.
Returns
Uint8Array
Compressed data.
TIP
A note on compressionLevel: The library supports regular compression levels from 1 up o 22. Levels >= 20, should be used with caution, as they require more memory. The library also offers negative compression levels, which extend the range of speed vs. ratio preferences. The lower the level, the faster the speed (at the cost of compression).
compressChunk()
compressChunk(
data):Uint8Array
Compresses a chunk of data in streaming mode. Call reset() before the first chunk, then compressChunk() for each chunk, and finally compressEnd().
Parameters
data
Uint8Array
Chunk of data to be compressed
Returns
Uint8Array
Compressed chunk data
compressEnd()
compressEnd():
Uint8Array
Finishes the streaming compression and returns any remaining compressed data.
Returns
Uint8Array
Final compressed data
decompress()
decompress(
compressedData):Uint8Array
Parameters
compressedData
Uint8Array
Data to be compressed
Returns
Uint8Array
Uncompressed data.
decompressChunk()
decompressChunk(
compressedData,outputSize):Uint8Array
Decompresses a chunk of data in streaming mode. Call reset() before the first chunk, then decompressChunk() for each chunk.
Parameters
compressedData
Uint8Array
Chunk of compressed data
outputSize
number
Expected output size for this chunk
Returns
Uint8Array
Decompressed chunk data
defaultCLevel()
defaultCLevel():
number
Returns
number
Default compression level (see notes above above).
minCLevel()
minCLevel():
number
Returns
number
maxCLevel()
maxCLevel():
number
Returns
number