@aokiapp/tlv
Version:
Tag-Length-Value (TLV) parser and builder library with schema support. Provides both parsing and building APIs as submodules.
107 lines • 4.88 kB
TypeScript
import { TagClass } from "../common/types.js";
type DefaultDecodeType = 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.
*/
export 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 DecodedType - The type after decoding.
*/
export interface PrimitiveTLVSchema<N extends string = string, DecodedType = DefaultDecodeType> extends TLVSchemaBase<N> {
/**
* Optional decode function for synchronous decoding.
* Use a method signature to improve assignability across unions.
*/
decode(buffer: ArrayBuffer): DecodedType;
}
/**
* Interface for defining a constructed TLV schema.
* @template F - The array of child field schemas.
*/
export interface ConstructedTLVSchema<N extends string = string, F extends readonly TLVSchema[] = readonly TLVSchema[]> extends TLVSchemaBase<N> {
readonly fields: F;
readonly isSet: boolean;
}
export interface RepeatedTLVSchema<N extends string = string, Item extends TLVSchema = TLVSchema> extends TLVSchemaBase<N> {
readonly item: Item;
}
export type TLVSchema<N extends string = string, D = unknown> = PrimitiveTLVSchema<N, D> | ConstructedTLVSchema<N, readonly TLVSchema[]> | RepeatedTLVSchema<N, TLVSchema>;
type ParsedResult<S extends TLVSchema> = S extends ConstructedTLVSchema ? ParsedResultFromConstructed<S> : S extends RepeatedTLVSchema ? ParsedResultFromRepeated<S> : S extends PrimitiveTLVSchema<string, unknown> ? ParsedResultFromPrimitive<S> : never;
type ParsedResultFromConstructed<S> = S extends ConstructedTLVSchema<string, infer Fields> ? Fields extends readonly TLVSchema[] ? {
[K in Fields[number] as K["optional"] extends true ? never : K["name"]]: ParsedResult<K>;
} & {
[K in Fields[number] as K["optional"] extends true ? K["name"] : never]?: ParsedResult<K>;
} : never : never;
type ParsedResultFromRepeated<S> = S extends RepeatedTLVSchema<string, infer Item> ? ParsedResult<Item>[] : never;
type ParsedResultFromPrimitive<S> = S extends PrimitiveTLVSchema<string, infer D> ? D : never;
/**
* A parser that parses TLV data based on a given schema.
* Provides synchronous parse operations.
*/
export declare class SchemaParser<S extends TLVSchema> {
readonly schema: S;
readonly strict: boolean;
private depthCounter;
private readonly maxDepth;
constructor(schema: S, options?: {
strict?: boolean;
maxDepth?: number;
});
parse(buffer: ArrayBuffer): ParsedResult<S>;
private parseTopLevel;
private parsePrimitive;
private parseConstructed;
/**
* Strict, linear SEQUENCE matching (unchanged behavior).
* - Consumes children in schema order
* - Optional fields may be skipped
* - Repeated fields consume zero or more consecutive matching children
* - Any mismatch immediately fails (independent of 'strict')
* - No extra children are allowed
*/
private parseConstructedSequence;
/**
* SET parsing with order-independent matching and DER canonical validation when strict is true.
* - Collects all child TLVs (raw bytes + TLV)
* - Immediately fails on unknown children (independent of 'strict')
* - Non-repeated fields: matches at most one child based on tagClass, tagNumber, constructed
* - Repeated fields: collects all matching children (SET OF) in any order
* - Fails when a required non-repeated field is missing or leftover children remain
*/
private parseConstructedSet;
private matchesFieldTag;
private compareUnsignedLex;
private ensureDepth;
private isConstructed;
private isRepeated;
}
/**
* Utility class for creating new TLV schemas (identical to builder schemas).
*/
export declare class Schema {
static primitive<N extends string, O extends SchemaOptions, DecodedType>(name: N, options: O, decode: (buffer: ArrayBuffer) => DecodedType): PrimitiveTLVSchema<N, DecodedType> & 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-parser.d.ts.map