Skip to content

compress ​

zlib and gzip deflate and inflate, with streaming helpers.

Import

ts
import { Deflater, Inflater, Compress } from "compress";

Deflater class ​

ts
class Deflater

Streaming deflate. Construct via Compress.createDeflater(level?). Feed input chunks with feed, drain compressed bytes with takeOutput, and call finish once when the input is exhausted. After finish, takeOutput may still be called one or more times to drain the tail of the compressed stream.

Deflater.feed ​

ts
feed(data: Buffer | string): void

Append data to the input stream. Throws if the stream was already finished. Strings are treated as raw bytes.

Deflater.finish ​

ts
finish(): void

Mark the input stream complete and flush any pending compressed bytes into the output buffer. Throws if already finished.

Deflater.isFinished ​

ts
isFinished(): boolean

True after finish has been called and every byte has been drained via takeOutput. Until then, the deflater is still in mid-stream.

Deflater.takeOutput ​

ts
takeOutput(): Buffer

Drain and return the compressed bytes accumulated so far. The internal buffer is cleared. Returns an empty Buffer when no output is currently available.

Inflater class ​

ts
class Inflater

Streaming inflate. Construct via Compress.createInflater(). Feed compressed chunks with feed; drain decompressed bytes with takeOutput. The stream finishes implicitly when the compressed payload's terminator is reached — there is no explicit finish call, mirroring how zlib's inflate state machine works.

Inflater.feed ​

ts
feed(data: Buffer | string): void

Append data to the compressed input stream. Throws an Error if the stream has already reached its end. Throws a RangeError if the data is corrupt or the output limit is exceeded. Strings are treated as raw bytes.

Inflater.isFinished ​

ts
isFinished(): boolean

True once the inflater has consumed the compressed stream's terminator. Further feed calls will throw.

Inflater.takeOutput ​

ts
takeOutput(): Buffer

Drain and return the decompressed bytes accumulated so far. The internal buffer is cleared. Returns an empty Buffer when no output is currently available.

Compress static class ​

ts
static class Compress

One-shot zlib / gzip helpers plus factories for the streaming variants. The class is static-only — there are no instances. The level argument on the compressing helpers is clamped to the zlib range 0..9 and defaults to 6. The maxOutput argument on the decompressing helpers caps the output size in bytes; pass 0 or a negative value (or omit it) for the default cap of 256 MiB.

Compress.compress ​

ts
static compress(data: Buffer | string, level?: int): Buffer

Deflate data using zlib framing. Returns the compressed payload as a Buffer. level is clamped to 0..9 and defaults to 6.

Example

ts
import { Compress } from "compress";
const packed: Buffer = Compress.compress("aaaaaaaaaaaaaaaaaaaaaaaa");
console.log(packed.length < 24); // true

Compress.createDeflater ​

ts
static createDeflater(level?: int): Deflater

Construct a streaming Deflater. level is clamped to 0..9 and defaults to 6.

Compress.createInflater ​

ts
static createInflater(maxOutput?: int): Inflater

Construct a streaming Inflater. maxOutput caps the total decompressed output in bytes; omit, pass 0, or pass a negative value for the default cap of 256 MiB. feed throws a RangeError when the cap is exceeded or when the compressed data is corrupt.

Compress.decompress ​

ts
static decompress(data: Buffer | string, maxOutput?: int): Buffer

Inflate zlib-framed data. Returns the decompressed payload as a Buffer. maxOutput caps the output in bytes; omit, pass 0, or pass a negative value for the default cap of 256 MiB.

Example

ts
import { Compress } from "compress";
const packed: Buffer = Compress.compress("hello hello hello");
console.log(Compress.decompress(packed).toString()); // hello hello hello

Compress.gzipCompress ​

ts
static gzipCompress(data: Buffer | string, level?: int): Buffer

Deflate data using gzip framing. Returns the compressed payload as a Buffer. level is clamped to 0..9 and defaults to 6.

Compress.gzipDecompress ​

ts
static gzipDecompress(data: Buffer | string, maxOutput?: int): Buffer

Inflate gzip-framed data. Returns the decompressed payload as a Buffer. maxOutput caps the output in bytes; omit, pass 0, or pass a negative value for the default cap of 256 MiB.


Generated from lang/types/compress.d.tks in the ThicketScript repository.