Skip to content

API reference / @jsprismarine/protocol / BatchCodec

Class: BatchCodec ​

Defined in: protocol/src/BatchCodec.ts:33

A batch: the outermost Minecraft layer, sitting directly inside a RakNet frame.

0xFE │ [algorithm byte] │ ⟨varint len, packet⟩ ⟨varint len, packet⟩ …

The algorithm byte is present if and only if compression has been negotiated - which is not the same as this batch being compressed, and conflating the two is the trap here. A connection sends RequestNetworkSettings and receives NetworkSettings with no prefix at all; from the packet after that one, every batch carries a prefix, and a peer is free to make it UNCOMPRESSED_BATCH whenever compressing would not pay. So the two states are two codecs - BatchCodec.uncompressed and BatchCodec.compressed - and a connection replaces one with the other exactly once.

Nothing here knows what a packet is: a batch is a list of opaque buffers. That is what lets a proxy route on ids it has no definition for, and what keeps this usable from both sides of a connection.

Methods ​

decode() ​

decode(batch): Buffer<ArrayBufferLike>[]

Defined in: protocol/src/BatchCodec.ts:121

Parameters ​

batch ​

Buffer

Returns ​

Buffer<ArrayBufferLike>[]


decodeAsync() ​

decodeAsync(batch): Promise<Buffer<ArrayBufferLike>[]>

Defined in: protocol/src/BatchCodec.ts:128

The async entry point; small compressed bodies inflate inline, and larger ones use zlib's async API.

Parameters ​

batch ​

Buffer

Returns ​

Promise<Buffer<ArrayBufferLike>[]>


encode() ​

encode(packets): Buffer

Defined in: protocol/src/BatchCodec.ts:54

Parameters ​

packets ​

readonly Buffer<ArrayBufferLike>[]

Returns ​

Buffer


encodeAsync() ​

encodeAsync(packets): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/BatchCodec.ts:65

The same, with the deflate handed to zlib's asynchronous API.

Parameters ​

packets ​

readonly Buffer<ArrayBufferLike>[]

Returns ​

Promise<Buffer<ArrayBufferLike>>


encodeFullFlushFragment() ​

encodeFullFlushFragment(packet): Buffer

Defined in: protocol/src/BatchCodec.ts:80

Encodes one packet's length and bytes as an independently reusable raw-deflate fragment.

Parameters ​

packet ​

Buffer

One already encoded packet.

Returns ​

Buffer

The non-final raw-deflate fragment for that packet.


encodeFullFlushFragmentAsync() ​

encodeFullFlushFragmentAsync(packet): Promise<Buffer<ArrayBufferLike>>

Defined in: protocol/src/BatchCodec.ts:91

Encodes one packet fragment using zlib's asynchronous API.

Parameters ​

packet ​

Buffer

One already encoded packet.

Returns ​

Promise<Buffer<ArrayBufferLike>>

The non-final raw-deflate fragment for that packet.


encodeFullFlushFragments() ​

encodeFullFlushFragments(packets, fragments): Buffer

Defined in: protocol/src/BatchCodec.ts:106

Assembles a normal Bedrock batch from cached full-flush fragments.

The final empty block is the only final marker in the stream. Each fragment ended with Z_FULL_FLUSH, so it has no back-reference into another packet and can be shared safely.

Parameters ​

packets ​

readonly Buffer<ArrayBufferLike>[]

The decoded packet bytes in the order the client must receive them.

fragments ​

readonly Buffer<ArrayBufferLike>[]

One cached encodeFullFlushFragment result per packet.

Returns ​

Buffer

A compressed Bedrock batch carrying the concatenated packet stream.


isCompressionEnabled() ​

isCompressionEnabled(): boolean

Defined in: protocol/src/BatchCodec.ts:50

Whether compression has been negotiated, i.e. whether batches carry a prefix byte.

Returns ​

boolean


compressed() ​

static compressed(settings): BatchCodec

Defined in: protocol/src/BatchCodec.ts:45

The post-negotiation form: every batch carries an algorithm byte.

Parameters ​

settings ​

CompressionSettings | CompressionCodec

Returns ​

BatchCodec


uncompressed() ​

static uncompressed(): BatchCodec

Defined in: protocol/src/BatchCodec.ts:40

The pre-negotiation form, carrying no algorithm byte. Only RequestNetworkSettings and NetworkSettings itself travel this way.

Returns ​

BatchCodec