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 bytesvalue.toString()→"0xAA43"style hex string
StringElementParser
UTF-8 strings. value is a StringValue with:
value.value: string— decoded string- Accepts
Uint8Array(decoded) orstring(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,nullif error sentinelvalue.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): numberChecksum
packetChecksum(data: Uint8Array): Uint8Array // 2 bytesFloat Conversion
linearMap(srcValue, srcDomain, dstRange): number
bytesToFloat(value, domain, range, errorVal?): number | null
floatToBytes(value, domain, range, errorVal?): Uint8ArrayInteger
bytesToInt(value: Uint8Array, signed?: boolean): number
intToBytes(value: number, length?: number, signed?: boolean): Uint8ArrayDateTime
bytesToDatetime(value: Uint8Array): Date
datetimeToBytes(value: Date): Uint8ArrayString
bytesToStr(value: Uint8Array): string
strToBytes(value: string): Uint8ArrayHex String
bytesToHexstr(value: Uint8Array, start?, sep?): string
hexstrToBytes(value: string): Uint8ArrayError Classes
| Class | Extends | When thrown |
|---|---|---|
KLVError | Error | Base for all library errors |
BERDecodeError | KLVError | Malformed BER length field |
RangeError | KLVError | Value outside linear map domain/range |
TruncatedDataError | KLVError | Buffer ends unexpectedly mid-field |
LengthError | KLVError | Wrong byte count for data type |
ValidationError | KLVError | Schema validation failure |