[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/prismarine](https://jsprismarine.dev/api/@jsprismarine/prismarine/index.md) / network/encodePacketBatch

# network/encodePacketBatch

## Classes

- [FullFlushFragmentCache](https://jsprismarine.dev/api/@jsprismarine/prismarine/network/encodePacketBatch/classes/FullFlushFragmentCache.md)

## Interfaces

### EncodedPacketBatch

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:132](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L132)

Encoded packet objects and the batch that carries them.

#### Properties

##### content

> **content**: `Buffer`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:134](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L134)

The framed bytes ready for the transport.

##### packetBuffers

> **packetBuffers**: readonly `Buffer`\<`ArrayBufferLike`\>[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:138](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L138)

Transient packet views, valid for the synchronous batch assembly and packet diagnostics.

##### packets

> **packets**: readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:136](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L136)

The version-specific packets that produced those bytes.

***

### EncodedPacketBuffers

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:144](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L144)

A protocol-stamped packet and its bytes, ready to join into one or more batches.

#### Properties

##### buffers

> **buffers**: `Buffer`\<`ArrayBufferLike`\>[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:148](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L148)

Bytes in the same order as `packets`.

##### packets

> **packets**: [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:146](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L146)

Packets prepared for the selected protocol version.

***

### PacketBatchOptions

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:9](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L9)

Options the server supplies when framing already-encoded Minecraft packets.

#### Extended by

- [`PacketBatchSendOptions`](https://jsprismarine.dev/api/@jsprismarine/prismarine/network/MinecraftSession/index.md#packetbatchsendoptions)

#### Properties

##### compressed?

> `optional` **compressed?**: `boolean`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:11](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L11)

Whether the negotiated batch format includes a compression prefix.

##### compressionAlgorithm?

> `optional` **compressionAlgorithm?**: [`PacketCompressionAlgorithm`](https://jsprismarine.dev/api/@jsprismarine/minecraft/index.md#packetcompressionalgorithm)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:13](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L13)

Algorithm advertised by this connection in `NetworkSettings`.

##### compressionLevel?

> `optional` **compressionLevel?**: `number`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:17](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L17)

zlib level for this batch; omitted to use Protocol's default.

##### compressionThreshold?

> `optional` **compressionThreshold?**: `number`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:15](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L15)

Smallest batch payload size to compress; zero leaves every batch uncompressed.

##### fragmentCache?

> `optional` **fragmentCache?**: [`FullFlushFragmentCache`](https://jsprismarine.dev/api/@jsprismarine/prismarine/network/encodePacketBatch/classes/FullFlushFragmentCache.md)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:23](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L23)

Cache shared fragments for the lifetime appropriate to the caller.

##### fragmentCodec?

> `optional` **fragmentCodec?**: [`ProtocolCodec`](https://jsprismarine.dev/api/@jsprismarine/protocol/index.md#protocolcodec)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:21](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L21)

The protocol layout owner used as part of each fragment cache key.

##### fragmentKeyDiscriminators?

> `optional` **fragmentKeyDiscriminators?**: readonly `string`[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:27](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L27)

Extra packet identity when a stable key alone does not identify the full packet.

##### fragmentKeys?

> `optional` **fragmentKeys?**: readonly `Buffer`\<`ArrayBufferLike`\>[]

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:25](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L25)

Optional stable per-packet cache keys, used by serialized chunk payloads.

##### fullFlushFragments?

> `optional` **fullFlushFragments?**: `boolean`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:19](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L19)

Use independently compressed, reusable packet fragments for this batch.

## Type Aliases

### PacketEncodingLifetime

> **PacketEncodingLifetime** = `"transient"` \| `"cached"`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:142](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L142)

Whether encoded packet views are for immediate framing or caller retention.

## Variables

### ASYNC\_PACKET\_BATCH\_THRESHOLD

> `const` **ASYNC\_PACKET\_BATCH\_THRESHOLD**: `number`

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:152](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L152)

Compressed packet bodies above this size use zlib's asynchronous API.

## Functions

### encodePacketBatch()

> **encodePacketBatch**(`packets`, `codec`, `options?`, `lifetime?`): [`EncodedPacketBatch`](#encodedpacketbatch)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:163](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L163)

Prepares each packet for one negotiated Protocol codec and frames their bytes with Protocol's
batch codec. The server owns codec selection; Protocol owns packet encoding and batch layout.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

Packets as the caller built them.

##### codec

[`ProtocolCodec`](https://jsprismarine.dev/api/@jsprismarine/protocol/index.md#protocolcodec)

The negotiated packet ids, layouts, and Protocol context.

##### options?

[`PacketBatchOptions`](#packetbatchoptions) = `{}`

Compression state and optional zlib level.

##### lifetime?

[`PacketEncodingLifetime`](#packetencodinglifetime) = `'transient'`

Keep packet bytes on each packet when callers need its cached `getBuffer()` contract.

#### Returns

[`EncodedPacketBatch`](#encodedpacketbatch)

Version-specific packets and their framed batch.

***

### encodePacketBatchAsync()

> **encodePacketBatchAsync**(`packets`, `codec`, `options?`): `Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:312](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L312)

The async counterpart for large batches whose deflate should leave the main thread.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

##### codec

[`ProtocolCodec`](https://jsprismarine.dev/api/@jsprismarine/protocol/index.md#protocolcodec)

##### options?

[`PacketBatchOptions`](#packetbatchoptions) = `{}`

#### Returns

`Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

***

### encodePacketBatchBySize()

> **encodePacketBatchBySize**(`packets`, `codec`, `options?`, `lifetime?`): [`EncodedPacketBatch`](#encodedpacketbatch) \| `Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:298](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L298)

Encodes small batches synchronously and moves larger deflates to the libuv pool.
Packet encoding still runs once on the calling thread; only zlib moves off it.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

Packets as the caller built them.

##### codec

[`ProtocolCodec`](https://jsprismarine.dev/api/@jsprismarine/protocol/index.md#protocolcodec)

The negotiated packet ids, layouts, and Protocol context.

##### options?

[`PacketBatchOptions`](#packetbatchoptions) = `{}`

Compression state and optional zlib level.

##### lifetime?

[`PacketEncodingLifetime`](#packetencodinglifetime) = `'transient'`

#### Returns

[`EncodedPacketBatch`](#encodedpacketbatch) \| `Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

The batch directly below the threshold, or a promise above it.

***

### encodePacketBatchFromBuffers()

> **encodePacketBatchFromBuffers**(`packets`, `packetBuffers`, `options?`): [`EncodedPacketBatch`](#encodedpacketbatch)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:195](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L195)

Frames protocol-encoded packet buffers without encoding their packet objects again.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

The version-specific packets that produced `packetBuffers`.

##### packetBuffers

readonly `Buffer`\<`ArrayBufferLike`\>[]

Encoded packet bodies in the same order as `packets`.

##### options?

[`PacketBatchOptions`](#packetbatchoptions) = `{}`

Compression state, zlib level, and size threshold.

#### Returns

[`EncodedPacketBatch`](#encodedpacketbatch)

The batch ready for the transport.

***

### encodePacketBatchFromBuffersBySize()

> **encodePacketBatchFromBuffersBySize**(`packets`, `packetBuffers`, `options?`): [`EncodedPacketBatch`](#encodedpacketbatch) \| `Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:226](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L226)

Frames already encoded packets, compressing only when the configured body-size threshold is met.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

The version-specific packets that produced `packetBuffers`.

##### packetBuffers

readonly `Buffer`\<`ArrayBufferLike`\>[]

Encoded packet bodies in the same order as `packets`.

##### options?

[`PacketBatchOptions`](#packetbatchoptions) = `{}`

Compression state, zlib level, and size threshold.

#### Returns

[`EncodedPacketBatch`](#encodedpacketbatch) \| `Promise`\<[`EncodedPacketBatch`](#encodedpacketbatch)\>

The batch directly below the async deflate threshold, or a promise above it.

***

### encodePacketsForCodec()

> **encodePacketsForCodec**(`packets`, `codec`, `lifetime?`): [`EncodedPacketBuffers`](#encodedpacketbuffers)

Defined in: [packages/prismarine/src/network/encodePacketBatch.ts:180](https://github.com/JSPrismarine/JSPrismarine/blob/30551f3817767ce3108756cea53083ae9be610da/packages/prismarine/src/network/encodePacketBatch.ts#L180)

Encodes packets once for one protocol codec so their buffers can be shared across audiences.

#### Parameters

##### packets

readonly [`NetworkPacket`](https://jsprismarine.dev/api/@jsprismarine/protocol/classes/NetworkPacket.md)\<`unknown`\>[]

Packets as the caller built them.

##### codec

[`ProtocolCodec`](https://jsprismarine.dev/api/@jsprismarine/protocol/index.md#protocolcodec)

The negotiated packet ids, layouts, and Protocol context.

##### lifetime?

[`PacketEncodingLifetime`](#packetencodinglifetime) = `'transient'`

#### Returns

[`EncodedPacketBuffers`](#encodedpacketbuffers)

Protocol-stamped packets and their bytes in matching order.
