WebKLV

API Reference

Complete API reference for WebKLV

API Reference

Core Classes

KLVParser

Low-level iterator for raw KLV triplets.

class KLVParser implements Iterator<KLVPair>, Iterable<KLVPair> {
  constructor(source: Uint8Array, options: { keyLength: number });
  next(): IteratorResult<KLVPair>;
  [Symbol.iterator](): Iterator<KLVPair>;
}

Options:

  • keyLength (default: 16) — number of bytes forming the key

StreamParser

High-level parser that dispatches 16-byte Universal Labels to registered set parsers.

class StreamParser implements Iterable<Element> {
  static readonly parsers: Map<string, SetParserCtor>;
  static register(ctor: SetParserCtor & { key: Bytes }): void;
  constructor(source: Uint8Array);
  [Symbol.iterator](): Iterator<Element>;
}

SetParser

Base class for nested KLV local sets.

abstract class SetParser extends Element {
  readonly items: Map<string, Element>;
  get(key: Bytes | string): Element | undefined;
  toBytes(): Uint8Array;
  toString(): string;
  static registerParser(setClass, parserCtor): void;
}

Element

Abstract base for all KLV elements.

abstract class Element {
  readonly key: Uint8Array;
  value: unknown;
  get name(): string;
  get length(): Uint8Array;  // BER-encoded
  abstract toValueBytes(): Uint8Array;
  toBytes(): Uint8Array;
  toString(): string;
}

UnknownElement

Fallback element for unrecognized keys.

class UnknownElement extends Element {
  constructor(key: Uint8Array, value: Uint8Array);
  toValueBytes(): Uint8Array;
}

Element Parser Classes

BytesElementParser

Raw bytes. value is a BytesValue with:

  • value.value: Uint8Array — raw bytes
  • value.toString()"0xAA43" style hex string

StringElementParser

UTF-8 strings. value is a StringValue with:

  • value.value: string — decoded string
  • Accepts Uint8Array (decoded) or string (stored directly)

DateTimeElementParser

Microsecond timestamps. value is a DateTimeValue with:

  • value.value: Date — JavaScript Date (ms precision)
  • value.toString()"2009-01-12 22:08:22+00:00"
  • Lossless re-encoding via BigInt

MappedElementParser

Linear-mapped fixed-point floats. value is a MappedValue with:

  • value.value: number | null — decoded float, null if error sentinel
  • value.toString()"159.97436484321355" or "147.0" for whole numbers
  • Subclasses must declare static _domain, static _range, static _errorVal

Utility Functions

BER

berEncode(value: number): Uint8Array
berDecode(value: Uint8Array): number

Checksum

packetChecksum(data: Uint8Array): Uint8Array  // 2 bytes

Float Conversion

linearMap(srcValue, srcDomain, dstRange): number
bytesToFloat(value, domain, range, errorVal?): number | null
floatToBytes(value, domain, range, errorVal?): Uint8Array

Integer

bytesToInt(value: Uint8Array, signed?: boolean): number
intToBytes(value: number, length?: number, signed?: boolean): Uint8Array

DateTime

bytesToDatetime(value: Uint8Array): Date
datetimeToBytes(value: Date): Uint8Array

String

bytesToStr(value: Uint8Array): string
strToBytes(value: string): Uint8Array

Hex String

bytesToHexstr(value: Uint8Array, start?, sep?): string
hexstrToBytes(value: string): Uint8Array

Error Classes

ClassExtendsWhen thrown
KLVErrorErrorBase for all library errors
BERDecodeErrorKLVErrorMalformed BER length field
RangeErrorKLVErrorValue outside linear map domain/range
TruncatedDataErrorKLVErrorBuffer ends unexpectedly mid-field
LengthErrorKLVErrorWrong byte count for data type
ValidationErrorKLVErrorSchema validation failure