Skip to content

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);

Extends

  • MainModuleEx<MainModule>

Methods

load()

static load(): 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()

static unload(): void

Unloades the compiled wasm instance.

Returns

void


malloc()

malloc(size): HeapU8

Parameters

size

number

Returns

HeapU8

Inherited from

MainModuleEx.malloc


free()

free(data): void

Parameters

data

HeapU8

Returns

void

Inherited from

MainModuleEx.free


dataToHeap()

dataToHeap(data): HeapU8

Parameters

data

Uint8Array

Returns

HeapU8

Inherited from

MainModuleEx.dataToHeap


heapView()

heapView(data): Uint8Array

Parameters

data

HeapU8

Returns

Uint8Array

Inherited from

MainModuleEx.heapView


heapToUint8Array()

heapToUint8Array(data): Uint8Array

Parameters

data

HeapU8

Returns

Uint8Array

Inherited from

MainModuleEx.heapToUint8Array


lengthBytes()

lengthBytes(str): number

Parameters

str

string

Returns

number

Inherited from

MainModuleEx.lengthBytes


stringToHeap()

stringToHeap(str): HeapU8

Parameters

str

string

Returns

HeapU8

Inherited from

MainModuleEx.stringToHeap


heapToString()

heapToString(data): string

Parameters

data

HeapU8

Returns

string

Inherited from

MainModuleEx.heapToString


hasFilesystem()

hasFilesystem(): boolean

Returns

boolean

Inherited from

MainModuleEx.hasFilesystem


createPath()

createPath(path, canRead?, canWrite?): void

Parameters

path

string

canRead?

boolean

canWrite?

boolean

Returns

void

Inherited from

MainModuleEx.createPath


createDataFile()

createDataFile(path, data, canRead?, canWrite?, canOwn?): void

Parameters

path

string

data

Uint8Array

canRead?

boolean

canWrite?

boolean

canOwn?

boolean

Returns

void

Inherited from

MainModuleEx.createDataFile


preloadFile()

preloadFile(path, data, canRead?, canWrite?, dontCreateFile?, canOwn?, preFinish?): Promise<void>

Parameters

path

string

data

Uint8Array

canRead?

boolean

canWrite?

boolean

dontCreateFile?

boolean

canOwn?

boolean

preFinish?

boolean

Returns

Promise<void>

Inherited from

MainModuleEx.preloadFile


unlink(path): any

Parameters

path

string

Returns

any

Inherited from

MainModuleEx.unlink


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

Released under the Apache-2.0 License.