Functionality for handling multiple packets as a single payload using ASCII delimiter separation, enabling efficient batch processing of engine.io protocol messages.
Encodes multiple packets into a single payload string using ASCII character 30 (0x1E) as the delimiter between encoded packets.
/**
* Encodes multiple packets into a single payload string
* @param packets - Array of packets to encode into payload
* @param callback - Callback receiving the encoded payload string
*/
function encodePayload(
packets: Packet[],
callback: (encodedPayload: string) => void
): void;Usage Examples:
import { encodePayload, Packet } from "engine.io-parser";
// Multiple text packets
const packets: Packet[] = [
{ type: "ping", data: "probe" },
{ type: "message", data: "hello" },
{ type: "close" }
];
encodePayload(packets, (payload) => {
console.log(payload); // "2probe\x1e4hello\x1e1"
// \x1e is the ASCII separator character (char code 30)
});
// Mixed packet types
const mixedPackets: Packet[] = [
{ type: "open", data: '{"sid":"abc123"}' },
{ type: "message", data: "user message" },
{ type: "ping" }
];
encodePayload(mixedPackets, (payload) => {
console.log(payload); // '0{"sid":"abc123"}\x1e4user message\x1e2'
});
// Binary packets (encoded as base64)
const binaryPackets: Packet[] = [
{ type: "message", data: Buffer.from([1, 2, 3]) },
{ type: "message", data: "text after binary" }
];
encodePayload(binaryPackets, (payload) => {
// Binary data is automatically base64 encoded in payloads
console.log(payload); // "bAQID\x1e4text after binary"
});Decodes a payload string back into an array of individual packets, with automatic error handling for malformed payloads.
/**
* Decodes a payload string into an array of packets
* @param encodedPayload - The payload string to decode
* @param binaryType - How to handle binary data ("nodebuffer", "arraybuffer", "blob")
* @returns Array of decoded packets, stops at first error packet
*/
function decodePayload(
encodedPayload: string,
binaryType?: BinaryType
): Packet[];Usage Examples:
import { decodePayload, BinaryType } from "engine.io-parser";
// Basic payload decoding
const payload = "2probe\x1e4hello\x1e1";
const packets = decodePayload(payload);
console.log(packets);
// [
// { type: "ping", data: "probe" },
// { type: "message", data: "hello" },
// { type: "close" }
// ]
// Binary data handling
const binaryPayload = "bAQID\x1e4text message";
const binaryPackets = decodePayload(binaryPayload, "nodebuffer");
console.log(binaryPackets);
// [
// { type: "message", data: Buffer.from([1, 2, 3]) },
// { type: "message", data: "text message" }
// ]
// ArrayBuffer binary handling (browser)
const arrayBufferPackets = decodePayload(binaryPayload, "arraybuffer");
console.log(arrayBufferPackets);
// [
// { type: "message", data: ArrayBuffer containing [1, 2, 3] },
// { type: "message", data: "text message" }
// ]
// Error handling for malformed payloads
const malformedPayload = "invalid\x1e4valid";
const errorPackets = decodePayload(malformedPayload);
console.log(errorPackets);
// [{ type: "error", data: "parser error" }]
// Stops at first error, doesn't process remaining packetsPayloads use a simple concatenation format with ASCII delimiter separation:
[encoded_packet_1][SEPARATOR][encoded_packet_2][SEPARATOR]...[encoded_packet_n]Where:
SEPARATOR is ASCII character 30 (0x1E)binaryType parameter:
"nodebuffer": Returns Buffer objects (Node.js default)"arraybuffer": Returns ArrayBuffer objects"blob": Returns Blob objects (browser only)[{ type: "error", data: "parser error" }][]interface Packet {
type: PacketType;
options?: { compress: boolean };
data?: RawData;
}
type PacketType =
| "open" | "close" | "ping" | "pong"
| "message" | "upgrade" | "noop" | "error";
type BinaryType = "nodebuffer" | "arraybuffer" | "blob";
type RawData = any; // string | Buffer | ArrayBuffer | ArrayBufferView | Blob