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.
59 lines (58 loc) • 1.96 kB
TypeScript
import { SerialParser } from '../types/index.js';
export interface SlipOptions {
/** Custom START byte. When set, each packet must begin with this byte. */
START?: number;
/** Escape byte for START. Defaults to `0xDB` (same as ESC). */
ESC_START?: number;
/** Escape byte. Defaults to `0xDB`. */
ESC?: number;
/** Packet end byte. Defaults to `0xC0`. */
END?: number;
/** ESC sequence for END byte. Defaults to `0xDC`. */
ESC_END?: number;
/** ESC sequence for ESC byte. Defaults to `0xDD`. */
ESC_ESC?: number;
/**
* Adds an END byte at the beginning of each packet (Bluetooth quirk).
* Only applies to `slipEncode`. Defaults to false.
*/
bluetoothQuirk?: boolean;
}
/**
* Creates a SLIP decoder parser that strips framing and emits each decoded
* packet as a `Uint8Array`.
*
* @param options - Optional custom framing byte values.
* @returns A {@link SerialParser} that emits `Uint8Array` values.
*
* @example
* ```ts
* import { AbstractSerialDevice, slipDecoder } from 'webserial-core';
*
* class MyDevice extends AbstractSerialDevice<Uint8Array> {
* constructor() {
* super({ baudRate: 115200, parser: slipDecoder() });
* }
* }
* ```
*/
export declare function slipDecoder(options?: SlipOptions): SerialParser<Uint8Array>;
/**
* SLIP-encodes a single outgoing packet.
*
* Escapes all END and ESC bytes in `data`, then appends an END byte.
* If `options.bluetoothQuirk` is true, an additional END byte is prepended.
*
* @param data - Raw packet bytes to encode.
* @param options - Optional custom framing byte values.
* @returns A new `Uint8Array` containing the SLIP-encoded packet.
*
* @example
* ```ts
* import { slipEncode } from 'webserial-core';
*
* const encoded = slipEncode(new Uint8Array([0x01, 0xC0, 0x02]));
* // device.send(encoded);
* ```
*/
export declare function slipEncode(data: Uint8Array, options?: SlipOptions): Uint8Array;