@the-minimal/protocol
Version:
Minimal and modular binary schema-full protocol for TypeScript
87 lines (73 loc) • 4.52 kB
text/typescript
type State = {
o: number;
a: Uint8Array;
v: DataView;
};
type Encoder<$Type> = (state: State, value: $Type) => void;
type Decoder<$Type> = (state: State) => $Type;
type InferEncoder<$Type> = $Type extends Encoder<infer $Value> ? $Value : never;
type InferDecoder<$Type> = $Type extends Decoder<infer $Value> ? $Value : never;
type EncodeObjectSchema = {
key: string;
type: Encoder<any>;
}[];
type DecodeObjectSchema = {
key: string;
type: Decoder<any>;
}[];
type InferEncodeObject<$Type extends EncodeObjectSchema> = {
[$Item in $Type[number] as $Item["key"]]: InferEncoder<$Item["type"]>;
};
type InferDecodeObject<$Type extends DecodeObjectSchema> = {
[$Item in $Type[number] as $Item["key"]]: InferDecoder<$Item["type"]>;
};
type InferEncodeTuple<$Type extends Encoder<any>[]> = {
[$Key in keyof $Type]: $Type[$Key] extends Encoder<infer $Value> ? $Value : never;
};
type InferDecodeTuple<$Type extends Decoder<any>[]> = {
[$Key in keyof $Type]: $Type[$Key] extends Decoder<infer $Value> ? $Value : never;
};
declare const ASCII_DECODER: TextDecoder;
declare const UTF8_DECODER: TextDecoder;
declare const UTF8_ENCODER: TextEncoder;
declare const encodeArray8: (type: any) => Encoder<unknown[]>;
declare const encodeArray16: (type: any) => Encoder<unknown[]>;
declare const decodeArray8: (type: any) => Decoder<unknown[]>;
declare const decodeArray16: (type: any) => Decoder<unknown[]>;
declare const encodeAscii8: Encoder<string>;
declare const encodeAscii16: Encoder<string>;
declare const decodeAscii8: Decoder<string>;
declare const decodeAscii16: Decoder<string>;
declare const encodeBool: Encoder<boolean>;
declare const decodeBool: Decoder<boolean>;
declare const encodeEnum: <const $Type extends any[]>(options: $Type) => Encoder<$Type[number]>;
declare const decodeEnum: <const $Type extends any[]>(options: $Type) => Decoder<$Type[number]>;
declare const encodeFloat32: Encoder<number>;
declare const encodeFloat64: Encoder<number>;
declare const decodeFloat32: Decoder<number>;
declare const decodeFloat64: Decoder<number>;
declare const encodeUint8: Encoder<number>;
declare const encodeUint16: Encoder<number>;
declare const encodeUint32: Encoder<number>;
declare const encodeInt8: Encoder<number>;
declare const encodeInt16: Encoder<number>;
declare const encodeInt32: Encoder<number>;
declare const decodeUint8: Decoder<number>;
declare const decodeUint16: Decoder<number>;
declare const decodeUint32: Decoder<number>;
declare const decodeInt8: Decoder<number>;
declare const decodeInt16: Decoder<number>;
declare const decodeInt32: Decoder<number>;
declare const encodeNullable: <const $Type>(type: Encoder<$Type>) => Encoder<$Type | null>;
declare const decodeNullable: <const $Type>(type: Decoder<$Type>) => Decoder<$Type | null>;
declare const encodeObject: <const $Type extends EncodeObjectSchema>(schema: $Type) => Encoder<InferEncodeObject<$Type>>;
declare const decodeObject: <const $Type extends DecodeObjectSchema>(schema: $Type) => Decoder<InferDecodeObject<$Type>>;
declare const encodeTap: <const $Type>(type: Encoder<$Type>, fn: (value: $Type) => any) => Encoder<$Type>;
declare const decodeTap: <const $Type>(type: Decoder<$Type>, fn: (value: $Type) => any) => Decoder<$Type>;
declare const encodeTuple: <const $Type extends Encoder<any>[]>(types: $Type) => Encoder<InferEncodeTuple<$Type>>;
declare const decodeTuple: <const $Type extends Decoder<any>[]>(types: $Type) => Decoder<InferDecodeTuple<$Type>>;
declare const encodeUnicode8: Encoder<string>;
declare const encodeUnicode16: Encoder<string>;
declare const decodeUnicode8: Decoder<string>;
declare const decodeUnicode16: Decoder<string>;
export { ASCII_DECODER, type DecodeObjectSchema, type Decoder, type EncodeObjectSchema, type Encoder, type InferDecodeObject, type InferDecodeTuple, type InferDecoder, type InferEncodeObject, type InferEncodeTuple, type InferEncoder, type State, UTF8_DECODER, UTF8_ENCODER, decodeArray16, decodeArray8, decodeAscii16, decodeAscii8, decodeBool, decodeEnum, decodeFloat32, decodeFloat64, decodeInt16, decodeInt32, decodeInt8, decodeNullable, decodeObject, decodeTap, decodeTuple, decodeUint16, decodeUint32, decodeUint8, decodeUnicode16, decodeUnicode8, encodeArray16, encodeArray8, encodeAscii16, encodeAscii8, encodeBool, encodeEnum, encodeFloat32, encodeFloat64, encodeInt16, encodeInt32, encodeInt8, encodeNullable, encodeObject, encodeTap, encodeTuple, encodeUint16, encodeUint32, encodeUint8, encodeUnicode16, encodeUnicode8 };