/*! * TSBuffer v2.2.10 * ----------------------------------------- * MIT LICENSE * KingWorks (C) Copyright 2025 * https://github.com/k8w/tsbuffer */ import { CustomTypeSchema } from 'tsbuffer-schema'; import { TSBufferProto } from 'tsbuffer-schema'; import { TSBufferSchema } from 'tsbuffer-schema'; import { TSBufferValidator } from 'tsbuffer-validator'; export declare class Base64Util { static bufferToBase64(buf: Uint8Array): string; static base64ToBuffer(base64: string): Uint8Array; } /** @public */ export declare interface DecodeOptions { /** Skip validate value *after* decode */ skipValidate?: boolean; } /** @public */ export declare type DecodeOutput = { isSucc: true; /** Decoded value */ value: T; errMsg?: undefined; } | { isSucc: false; /** Error message */ errMsg: string; errPhase: "decode" | "validate" | undefined; value?: undefined; }; /** @public */ declare type EncodeJsonOutput = { isSucc: true; /** Encoded JSON Object */ json: any; errMsg?: undefined; } | { isSucc: false; /** Error message */ errMsg: string; json?: undefined; }; /** @public */ export declare interface EncodeOptions { /** Skip validate value *before* encode */ skipValidate?: boolean; } /** @public */ export declare type EncodeOutput = { isSucc: true; /** Encoded binary buffer */ buf: Uint8Array; errMsg?: undefined; } | { isSucc: false; /** Error message */ errMsg: string; buf?: undefined; }; /** * @public */ export declare class TSBuffer { constructor(proto: Proto, options?: Partial); /** * 编码 * @param value - 要编码的值 * @param schemaOrId - Schema 或 SchemaID,例如`a/b.ts`下的`Test`类型,其ID为`a/b/Test` */ encode(value: any, schemaOrId: string | TSBufferSchema, options?: EncodeOptions): EncodeOutput; /** * 解码 * @param buf - 待解码的二进制数据 * @param schemaOrId - Schema 或 SchemaID,例如`a/b.ts`下的`Test`类型,其ID为`a/b/Test` */ decode(buf: Uint8Array, schemaOrId: string | TSBufferSchema, options?: DecodeOptions): DecodeOutput; /** * 编码为 JSON Object,根据协议将 JSON 不支持的格式(如 ArrayBuffer、Date、ObjectId)转换成 JSON 可传输的格式 * @param value * @param schemaOrId * @param options */ encodeJSON(value: any, schemaOrId: string | TSBufferSchema, options?: EncodeOptions): EncodeJsonOutput; /** * 从 JSON Object 解码,根据协议将 ArrayBuffer、Date、ObjectId 等类型从 JSON 中还原 * @param json - JSON Object (是 JSON 对象,而非 JSON 字符串) * @param schemaOrId * @param options */ decodeJSON(json: any, schemaOrId: string | TSBufferSchema, options?: DecodeOptions): DecodeOutput; validate: TSBufferValidator["validate"]; prune: TSBufferValidator["prune"]; } /** @public */ export declare interface TSBufferOptions { /** * 检查值中是否包含Schema定义之外多余的字段 * 仅对 `validate` 方法生效 * 是因为实现机制原因, `prune` `encode` `decode` 方法都会天然保证不会混入多余字段 * * 默认:`true` */ excessPropertyChecks: boolean; /** * 同 `tsconfig.json` 中的 `strictNullChecks` * 是否使用严格等于去判定 `undefined` 和 `null` * 如果该值为 `false`,则在编码过程中,`null` 在类型不兼容时可编码为`undefined`, * 解码过程中,`undefined` 在类型不兼容时可解码为 `null`。 * @defaultValue false */ strictNullChecks: boolean; /** * 正常编码流程是:先校验value类型合法,再进行编码 * 此值为 `true` 时,将跳过校验步骤以提升性能 * 但需要自行确保值类型合法,否则可能引发不确定的问题 * 默认为 `false` */ skipEncodeValidate: boolean; /** * 正常解码流程是:先进行二进制解码,再校验解码后的类型符合Schema定义 * 此值为 `true` 时,将跳过校验步骤以提升性能 * 但需要自行确保值类型合法,否则可能引发不确定的问题 * 默认为 `false` */ skipDecodeValidate: boolean; /** * Clone the proto, don't change this if you don't know what it is. * @defaultValue true */ cloneProto?: boolean; /** * Append `CustomTypeSchema` to given schema, * to customize validate & encode methods for specific types. * For example 'mongodb/ObjectId'. */ customTypes?: { [schemaId: string]: CustomTypeSchema; }; } export { }