or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdpacket-operations.mdpayload-operations.mdstreaming-operations.md
tile.json

tessl/npm-engine-io-parser

Parser for the engine.io protocol encoding, handling packet and payload encoding/decoding with binary support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/engine.io-parser@5.2.x

To install, run

npx @tessl/cli install tessl/npm-engine-io-parser@5.2.0

index.mddocs/

Engine.IO Parser

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.

Package Information

  • Package Name: engine.io-parser
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install engine.io-parser

Core Imports

import { 
  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");

Basic Usage

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
});

Architecture

Engine.IO Parser is built around several key components:

  • Protocol Support: Implements engine.io protocol version 4 with standardized packet types
  • Dual Environment: Unified API with platform-specific optimizations for Node.js and browsers
  • Binary Handling: Automatic format conversion between Buffer, ArrayBuffer, Blob, and base64 encoding
  • Streaming Interface: Transform streams for efficient processing of packet streams
  • Error Resilience: Graceful handling of malformed packets with standardized error responses

Capabilities

Packet Operations

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;

Packet Operations

Payload Operations

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[];

Payload Operations

Streaming Operations

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;

Streaming Operations

Core Types

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;

Constants

The parser exports the current protocol version:

const protocol: 4;