Skip to content

API reference / @jsprismarine/protocol / CompressionCodec

Class: CompressionCodec ​

Defined in: protocol/src/CompressionCodec.ts:64

The compression half of a batch, and nothing else: no framing, no packet boundaries, no session state beyond what NetworkSettings negotiated once.

An instance is what a connection agreed to. The decompression side is static because a batch names its own algorithm in its prefix byte, and a peer is entitled to send an uncompressed one at any point - so what we decompress with is read off the wire, not assumed from what we negotiated.

Constructors ​

Constructor ​

new CompressionCodec(settings): CompressionCodec

Defined in: protocol/src/CompressionCodec.ts:69

Parameters ​

settings ​

CompressionSettings

Returns ​

CompressionCodec

Methods ​

algorithmFor() ​

algorithmFor(byteLength): PacketCompressionAlgorithm

Defined in: protocol/src/CompressionCodec.ts:104

The algorithm this payload will actually go out under, threshold included.

Parameters ​

byteLength ​

number

Returns ​

PacketCompressionAlgorithm


compress() ​

compress(payload): Buffer

Defined in: protocol/src/CompressionCodec.ts:108

Parameters ​

payload ​

Buffer

Returns ​

Buffer


compressAsync() ​

compressAsync(payload): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/CompressionCodec.ts:129

The same, on zlib's asynchronous API.

Node runs that on the libuv thread pool, so the work genuinely leaves the main thread rather than merely being deferred on it. Worth the extra await for chunk batches, which are the large and frequent ones; small packets cost more in a trip through the pool than they do to compress.

Parameters ​

payload ​

Buffer

Returns ​

Promise<Buffer<ArrayBufferLike>>


compressFullFlush() ​

compressFullFlush(payload): Buffer

Defined in: protocol/src/CompressionCodec.ts:147

Compresses one packet payload into a reusable, non-final raw-deflate fragment.

Parameters ​

payload ​

Buffer

One packet's varint length and encoded bytes.

Returns ​

Buffer

A byte-aligned fragment that ended with Z_FULL_FLUSH.


compressFullFlushAsync() ​

compressFullFlushAsync(payload): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/CompressionCodec.ts:160

Compresses one packet payload into a reusable full-flush fragment on libuv's pool.

Parameters ​

payload ​

Buffer

One packet's varint length and encoded bytes.

Returns ​

Promise<Buffer<ArrayBufferLike>>

The byte-aligned raw-deflate fragment.


decompress() ​

decompress(algorithm, payload): Buffer

Defined in: protocol/src/CompressionCodec.ts:203

Reads a batch's prefix byte with maxDecompressedLength already applied.

Parameters ​

algorithm ​

PacketCompressionAlgorithm

payload ​

Buffer

Returns ​

Buffer


decompressAsync() ​

decompressAsync(algorithm, payload): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/CompressionCodec.ts:207

Parameters ​

algorithm ​

PacketCompressionAlgorithm

payload ​

Buffer

Returns ​

Promise<Buffer<ArrayBufferLike>>


getAlgorithm() ​

getAlgorithm(): PacketCompressionAlgorithm

Defined in: protocol/src/CompressionCodec.ts:88

Returns ​

PacketCompressionAlgorithm


shouldCompress() ​

shouldCompress(byteLength): boolean

Defined in: protocol/src/CompressionCodec.ts:96

Whether a payload of this size is worth compressing. Below the threshold deflate reliably makes the payload larger, and the peer is told so by the prefix.

Parameters ​

byteLength ​

number

Returns ​

boolean


decompress() ​

static decompress(algorithm, payload, maxOutputLength?): Buffer

Defined in: protocol/src/CompressionCodec.ts:168

Parameters ​

algorithm ​

PacketCompressionAlgorithm

payload ​

Buffer

maxOutputLength? ​

number = DEFAULT_MAX_DECOMPRESSED_LENGTH

Returns ​

Buffer


decompressAsync() ​

static decompressAsync(algorithm, payload, maxOutputLength?): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/CompressionCodec.ts:185

Parameters ​

algorithm ​

PacketCompressionAlgorithm

payload ​

Buffer

maxOutputLength? ​

number = DEFAULT_MAX_DECOMPRESSED_LENGTH

Returns ​

Promise<Buffer<ArrayBufferLike>>


fromPrefix() ​

static fromPrefix(prefix): PacketCompressionAlgorithm

Defined in: protocol/src/CompressionCodec.ts:79

The algorithm a batch's one byte prefix names, widened to the negotiated form.

Parameters ​

prefix ​

number

the byte following the batch id.

Returns ​

PacketCompressionAlgorithm


toPrefix() ​

static toPrefix(algorithm): number

Defined in: protocol/src/CompressionCodec.ts:84

The inverse of fromPrefix: what to write for an algorithm.

Parameters ​

algorithm ​

PacketCompressionAlgorithm

Returns ​

number