UNPKG

@aokiapp/tlv

Version:

Tag-Length-Value (TLV) parser and builder library with schema support. Provides both parsing and building APIs as submodules.

128 lines 4.48 kB
import { TagClass } from "../common/types.js"; type DefaultEncodeType = ArrayBuffer; /** * Base interface for a TLV schema object. */ interface TLVSchemaBase { readonly name: string; readonly tagClass?: TagClass; readonly tagNumber?: number; } /** * Interface for defining a primitive TLV schema. * @template DecodedType - The type after decoding. */ export interface PrimitiveTLVSchema<DecodedType = DefaultEncodeType> extends TLVSchemaBase { /** * Optional decode function which can return either a value or a Promise of a value. */ readonly decode?: (buffer: ArrayBuffer) => DecodedType | Promise<DecodedType>; } /** * Interface for defining a constructed TLV schema. * @template F - The array of child field schemas. */ export interface ConstructedTLVSchema<F extends readonly TLVSchema[]> extends TLVSchemaBase { readonly fields: F; } type TLVSchema = PrimitiveTLVSchema<unknown> | ConstructedTLVSchema<readonly TLVSchema[]>; type ParsedResult<S extends TLVSchema> = S extends ConstructedTLVSchema<infer F> ? { [Field in F[number] as Field["name"]]: ParsedResult<Field>; } : S extends PrimitiveTLVSchema<infer DecodedType> ? DecodedType : never; /** * A parser that parses TLV data based on a given schema (synchronous or asynchronous). * @template S - The schema type. */ export declare class SchemaParser<S extends TLVSchema> { schema: S; buffer: ArrayBuffer; view: DataView<ArrayBuffer>; offset: number; strict: boolean; /** * Constructs a SchemaParser for the specified schema. * @param schema - The TLV schema to use. */ constructor(schema: S, options?: { strict?: boolean; }); /** * Overloaded method: synchronous version. * @param buffer - The input data as an ArrayBuffer. * @returns Parsed result matching the schema. */ parse(buffer: ArrayBuffer): ParsedResult<S>; /** * Overloaded method: asynchronous version. * @param buffer - The input data as an ArrayBuffer. * @param options - Enable async parsing. * @returns A Promise of parsed result matching the schema. */ parse(buffer: ArrayBuffer, options: { async: true; }): Promise<ParsedResult<S>>; /** * Parses data in synchronous mode. * @param buffer - The input data. * @returns Parsed result matching the schema. */ parseSync(buffer: ArrayBuffer): ParsedResult<S>; /** * Parses data in asynchronous mode. * @param buffer - The input data. * @returns A Promise of parsed result matching the schema. */ parseAsync(buffer: ArrayBuffer): Promise<ParsedResult<S>>; /** * Recursively parses data in synchronous mode. * @param schema - The schema to parse with. * @returns Parsed result. */ private parseWithSchemaSync; /** * Recursively parses data in asynchronous mode. * @param schema - The schema to parse with. * @returns A Promise of the parsed result. */ private parseWithSchemaAsync; /** * Validates tag information against the expected schema. * @param tagInfo - The parsed tag info. * @param schema - The schema to validate. * @throws Error if tag class, tag number, or constructed status does not match. */ private validateTagInfo; } /** * Utility class for creating new TLV schemas. */ export declare class Schema { /** * Creates a primitive TLV schema definition. * @param name - The name of the field. * @param decode - Optional decode function. * @param options - Optional tag class and tag number. * @returns A primitive TLV schema object. */ static primitive<N extends string, D = ArrayBuffer>(name: N, decode?: (buffer: ArrayBuffer) => D | Promise<D>, options?: { tagClass?: TagClass; tagNumber?: number; }): PrimitiveTLVSchema<D> & { name: N; }; /** * Creates a constructed TLV schema definition. * @param name - The name of the field. * @param fields - An array of TLV schema definitions. * @param options - Optional tag class and tag number. * @returns A constructed TLV schema object. */ static constructed<N extends string, F extends readonly TLVSchema[]>(name: N, fields: F, options?: { tagClass?: TagClass; tagNumber?: number; }): ConstructedTLVSchema<F> & { name: N; }; } export {}; //# sourceMappingURL=schema-parser.d.ts.map