Parser for the engine.io protocol encoding, handling packet and payload encoding/decoding with binary support
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Engine.IO Parser is the JavaScript parser for the engine.io protocol encoding, providing comprehensive functionality for encoding and decoding packets and payloads with binary data support. It serves as a shared component between engine.io-client and engine.io, offering seamless operation across browser and Node.js environments.
npm install engine.io-parserimport {
encodePacket,
decodePacket,
encodePayload,
decodePayload,
createPacketEncoderStream,
createPacketDecoderStream,
Packet,
PacketType,
BinaryType,
RawData,
protocol
} from "engine.io-parser";For CommonJS:
const {
encodePacket,
decodePacket,
encodePayload,
decodePayload,
Packet,
protocol
} = require("engine.io-parser");import { encodePacket, decodePacket, encodePayload, decodePayload, Packet } from "engine.io-parser";
// Single packet encoding/decoding
const packet: Packet = { type: "message", data: "hello world" };
encodePacket(packet, true, (encodedPacket) => {
console.log(encodedPacket); // "4hello world"
const decodedPacket = decodePacket(encodedPacket);
console.log(decodedPacket); // { type: "message", data: "hello world" }
});
// Multiple packet payload handling
const packets: Packet[] = [
{ type: "ping", data: "probe" },
{ type: "message", data: "test" },
{ type: "close" }
];
encodePayload(packets, (encodedPayload) => {
console.log(encodedPayload); // "2probe\x1e4test\x1e1"
const decodedPackets = decodePayload(encodedPayload);
console.log(decodedPackets); // Original packets array
});Engine.IO Parser is built around several key components:
Core functionality for encoding and decoding individual engine.io protocol packets with support for all packet types and binary data.
function encodePacket(
packet: Packet,
supportsBinary: boolean,
callback: (encodedPacket: RawData) => void
): void;
function decodePacket(
encodedPacket: RawData,
binaryType?: BinaryType
): Packet;Functionality for handling multiple packets as a single payload, using ASCII delimiter separation for efficient batch processing.
function encodePayload(
packets: Packet[],
callback: (encodedPayload: string) => void
): void;
function decodePayload(
encodedPayload: string,
binaryType?: BinaryType
): Packet[];Transform stream interfaces for processing packet data in streaming scenarios, with WebSocket-inspired framing for binary data.
function createPacketEncoderStream(): TransformStream;
function createPacketDecoderStream(
maxPayload: number,
binaryType: BinaryType
): TransformStream;type PacketType =
| "open"
| "close"
| "ping"
| "pong"
| "message"
| "upgrade"
| "noop"
| "error";
type BinaryType = "nodebuffer" | "arraybuffer" | "blob";
type RawData = any; // string | Buffer | ArrayBuffer | ArrayBufferView | Blob
interface Packet {
type: PacketType;
options?: { compress: boolean };
data?: RawData;
}
const protocol: 4;The parser exports the current protocol version:
const protocol: 4;Install with Tessl CLI
npx tessl i tessl/npm-engine-io-parser