UNPKG

webserial-core

Version:

A strongly-typed, event-driven, abstract TypeScript library for the Web Serial API with custom parsers, command queue, handshake validation, and auto-reconnect.

37 lines (36 loc) 1.48 kB
import { SerialParser } from '../types/index.js'; export interface PacketLengthOptions { /** Delimiter byte that marks the start of a packet. Defaults to `0xAA`. */ delimiter?: number; /** Total overhead bytes in the packet (delimiter + length field(s) + any footer). Defaults to `2`. */ packetOverhead?: number; /** Number of consecutive bytes that encode the payload length. Defaults to `1`. */ lengthBytes?: number; /** Byte offset from the delimiter to the first length byte. Defaults to `1`. */ lengthOffset?: number; /** Maximum allowed payload length. Packets exceeding this are discarded. Defaults to `0xFF`. */ maxLen?: number; } /** * Creates a packet-length parser that buffers bytes, locates the delimiter, * reads the embedded length field, and emits complete packets as `Uint8Array`. * * @param options - Configuration options. * @returns A {@link SerialParser} that emits `Uint8Array` values. * * @example * ```ts * import { AbstractSerialDevice, packetLength } from 'webserial-core'; * * // Packets: [0xBC][0x00][len0][len1][cargo...][footer0][footer1] * class MyDevice extends AbstractSerialDevice<Uint8Array> { * constructor() { * super({ * baudRate: 115200, * parser: packetLength({ delimiter: 0xbc, packetOverhead: 5, lengthBytes: 2, lengthOffset: 2 }), * }); * } * } * ``` */ export declare function packetLength(options?: PacketLengthOptions): SerialParser<Uint8Array>;