Compression API · shared contract
Move bytes through the codec that fits.
Compressor and Decompressor are the narrow interfaces shared by loaders, writers, and applications. Pick a stable root class for normal use, or select a backend when bundle size, throughput, or reproducibility calls for it.
The codec contract
Keep codec choice separate from the data format.
A Parquet reader, a ZIP writer, and an application-level payload can all use the same direction-specific interface. The implementation can change with the runtime without changing the surrounding pipeline.
Direction
Compressor for writing; Decompressor for reading
Execution
Async, synchronous, and batch operations
Selection
Native stream, compact fallback, or explicit backend
Portability
Browser and Node.js implementations
Detailed guide
Shared fields and methods
The reference below defines codec metadata, preload behavior, one-shot methods, batch methods, and error expectations.
Compressor and Decompressor are the independent base classes for encoding and decoding
transforms. Use the narrow type that matches the operation: applications then import only the
codec direction they need.
import type {Compressor, Decompressor} from '@loaders.gl/compression/codec';
const writerCodecs: Compressor[] = [];
const loaderCodecs: Decompressor[] = [];
Library-neutral implementations such as GZipCompressor and GZipDecompressor are the normal
choice. They select a balanced built-in or compact fallback implementation. Backend-named classes
are available for applications that deliberately pin a library.
Shared fields
name: string
The canonical format name, such as gzip or zstd.
extensions: string[]
File extensions commonly associated with the format.
contentEncodings: string[]
HTTP Content-Encoding values associated with the format.
isSupported: boolean
Whether the implementation can be selected in the current environment.
preload(modules?): Promise<Compressor | Decompressor | void>
Root-level lazy classes return the selected concrete built-in or fallback implementation. Concrete
backend classes may simply initialize their dependency. Async operations normally call this when
needed. Call preload() before a synchronous method on a root-level class.
Compressor methods
compress(data: ArrayBuffer): Promise<ArrayBuffer>
Compresses one buffer asynchronously. This is the preferred one-shot API because it can use built-in streams and initialize optional codecs.
compressSync(data: ArrayBuffer): ArrayBuffer
Compresses synchronously when the selected implementation supports it. Otherwise it throws a clear unsupported or preload-required error.
compressBatches(data): AsyncIterable<ArrayBuffer>
Compresses iterable or async iterable buffers. Streaming implementations emit chunks as they become available; non-streaming implementations concatenate the input and yield one result.
Decompressor methods
decompress(data: ArrayBuffer, size?): Promise<ArrayBuffer>
Decompresses one buffer asynchronously. size supplies an expected or maximum output size to
implementations that require one.
decompressSync(data: ArrayBuffer, size?): ArrayBuffer
Decompresses synchronously when supported by the selected implementation.
decompressBatches(data): AsyncIterable<ArrayBuffer>
Decompresses iterable or async iterable buffers, incrementally when the backend supports streams.
Choosing a format
The deprecated combined Compression classes are retained for v5 migration but are not the
recommended API. See the v5 upgrade guide for import replacements.