@aokiapp/tlv
Version:
Tag-Length-Value (TLV) parser and builder library with schema support. Provides both parsing and building APIs as submodules.
126 lines • 4.4 kB
TypeScript
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 EncodedType - The type before encoding.
*/
export interface PrimitiveTLVSchema<EncodedType = DefaultEncodeType> extends TLVSchemaBase {
/**
* Optional encode function which can return either a value or a Promise of a value.
*/
readonly encode?: (data: EncodedType) => ArrayBuffer | Promise<ArrayBuffer>;
}
/**
* 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<any> | ConstructedTLVSchema<readonly TLVSchema[]>;
export type BuildData<S extends TLVSchema> = S extends ConstructedTLVSchema<infer F> ? {
[Field in F[number] as Field["name"]]: BuildData<Field>;
} : S extends PrimitiveTLVSchema<infer EncodedType> ? EncodedType : never;
/**
* A builder that builds TLV data based on a given schema (synchronous or asynchronous).
* @template S - The schema type.
*/
export declare class SchemaBuilder<S extends TLVSchema> {
schema: S;
strict: boolean;
/**
* Constructs a SchemaBuilder for the specified schema.
* @param schema - The TLV schema to use.
*/
constructor(schema: S, options?: {
strict?: boolean;
});
/**
* Overloaded method: synchronous version.
* @param data - The input data matching the schema structure.
* @returns Built TLV result.
*/
build(data: BuildData<S>): ArrayBuffer;
/**
* Overloaded method: asynchronous version.
* @param data - The input data matching the schema structure.
* @param options - Enable async building.
* @returns A Promise of built ArrayBuffer.
*/
build(data: BuildData<S>, options: {
async: true;
}): Promise<ArrayBuffer>;
/**
* Builds data in synchronous mode.
* @param data - The input data.
* @returns Built TLV result.
*/
buildSync(data: BuildData<S>): ArrayBuffer;
/**
* Builds data in asynchronous mode.
* @param data - The input data.
* @returns A Promise of built TLV result.
*/
buildAsync(data: BuildData<S>): Promise<ArrayBuffer>;
/**
* Recursively builds data in synchronous mode.
* @param schema - The schema to build with.
* @param data - The data to build.
* @returns Built result.
*/
private buildWithSchemaSync;
/**
* Recursively builds data in asynchronous mode.
* @param schema - The schema to build with.
* @param data - The data to build.
* @returns A Promise of the built result.
*/
private buildWithSchemaAsync;
}
/**
* Utility class for creating new TLV schemas (identical to parser schemas).
*/
export declare class Schema {
/**
* Creates a primitive TLV schema definition.
* @param name - The name of the field.
* @param encode - Optional encode function.
* @param options - Optional tag class and tag number.
* @returns A primitive TLV schema object.
*/
static primitive<N extends string, E>(name: N, encode: (data: E) => ArrayBuffer | Promise<ArrayBuffer>, options?: {
tagClass?: TagClass;
tagNumber?: number;
}): PrimitiveTLVSchema<E> & {
name: N;
};
static primitive<N extends string>(name: N, encode?: (data: ArrayBuffer) => ArrayBuffer | Promise<ArrayBuffer>, options?: {
tagClass?: TagClass;
tagNumber?: number;
}): PrimitiveTLVSchema<ArrayBuffer> & {
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-builder.d.ts.map