UNPKG

@aokiapp/tlv

Version:

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

85 lines 3.82 kB
import { TagClass } from "../common/index"; type DefaultEncodeType = ArrayBuffer; type SchemaOptions = { readonly tagClass?: TagClass; readonly tagNumber?: number; readonly optional?: boolean; readonly isSet?: boolean; }; type OptionalFlag<O extends SchemaOptions | undefined> = { readonly optional: O extends { optional: true; } ? true : false; }; /** * Base interface for a TLV schema object. */ interface TLVSchemaBase<N extends string = string> { readonly name: N; readonly tagClass: TagClass; readonly tagNumber: number; /** * When present, this field is optional in a constructed container. */ readonly optional: boolean; } /** * Interface for defining a primitive TLV schema. * @template EncodedType - The type before encoding. */ interface PrimitiveTLVSchema<N extends string = string, EncodedType = DefaultEncodeType> extends TLVSchemaBase<N> { /** * Optional encode function for synchronous encoding. * Use a method signature to improve assignability across unions. */ encode(data: EncodedType): ArrayBuffer; } /** * Interface for defining a constructed TLV schema. * @template F - The array of child field schemas. */ interface ConstructedTLVSchema<N extends string = string, F extends readonly TLVSchema[] = readonly TLVSchema[]> extends TLVSchemaBase<N> { readonly fields: F; readonly isSet: boolean; } interface RepeatedTLVSchema<N extends string = string, Item extends TLVSchema = TLVSchema> extends TLVSchemaBase<N> { readonly item: Item; } type TLVSchema<N extends string = string, E = unknown> = PrimitiveTLVSchema<N, E> | ConstructedTLVSchema<N, readonly TLVSchema[]> | RepeatedTLVSchema<N, TLVSchema>; type BuildData<S extends TLVSchema> = S extends ConstructedTLVSchema ? BuildDataFromConstructed<S> : S extends RepeatedTLVSchema ? BuildDataFromRepeated<S> : S extends PrimitiveTLVSchema<string, unknown> ? BuildDataFromPrimitive<S> : never; type BuildDataFromConstructed<S> = S extends ConstructedTLVSchema<string, infer Fields> ? Fields extends readonly TLVSchema[] ? { [K in Fields[number] as K["optional"] extends true ? never : K["name"]]: BuildData<K>; } & { [K in Fields[number] as K["optional"] extends true ? K["name"] : never]?: BuildData<K>; } : never : never; type BuildDataFromRepeated<S> = S extends RepeatedTLVSchema<string, infer Item> ? BuildData<Item>[] : never; type BuildDataFromPrimitive<S> = S extends PrimitiveTLVSchema<string, infer E> ? E : never; /** * A TLV builder that encodes data according to a provided schema. * Strict mode enforces presence of all required fields and type expectations. */ export declare class SchemaBuilder<S extends TLVSchema> { readonly schema: S; readonly strict: boolean; constructor(schema: S, options?: { strict?: boolean; }); build(data: BuildData<S>): ArrayBuffer; private encodeTopLevel; private encodePrimitive; private encodeConstructed; private isConstructed; private isRepeated; private concatBuffers; private compareUnsignedLex; } /** * Utility class for creating new TLV schemas (identical to parser schemas). */ export declare class Schema { static primitive<N extends string, O extends SchemaOptions, EncodedType>(name: N, options: O, encode: (data: EncodedType) => ArrayBuffer): PrimitiveTLVSchema<N, EncodedType> & OptionalFlag<O>; static constructed<N extends string, O extends SchemaOptions, Fields extends readonly TLVSchema[]>(name: N, options: O, fields: Fields): ConstructedTLVSchema<N, Fields> & OptionalFlag<O>; static repeated<N extends string, O extends SchemaOptions, Item extends TLVSchema>(name: N, options: O, item: Item): RepeatedTLVSchema<N, Item> & OptionalFlag<O>; } export {}; //# sourceMappingURL=schema-builder.d.ts.map