index.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*!
  2. * TSBuffer v2.2.10
  3. * -----------------------------------------
  4. * MIT LICENSE
  5. * KingWorks (C) Copyright 2025
  6. * https://github.com/k8w/tsbuffer
  7. */
  8. import { CustomTypeSchema } from 'tsbuffer-schema';
  9. import { TSBufferProto } from 'tsbuffer-schema';
  10. import { TSBufferSchema } from 'tsbuffer-schema';
  11. import { TSBufferValidator } from 'tsbuffer-validator';
  12. export declare class Base64Util {
  13. static bufferToBase64(buf: Uint8Array): string;
  14. static base64ToBuffer(base64: string): Uint8Array;
  15. }
  16. /** @public */
  17. export declare interface DecodeOptions {
  18. /** Skip validate value *after* decode */
  19. skipValidate?: boolean;
  20. }
  21. /** @public */
  22. export declare type DecodeOutput<T> = {
  23. isSucc: true;
  24. /** Decoded value */
  25. value: T;
  26. errMsg?: undefined;
  27. } | {
  28. isSucc: false;
  29. /** Error message */
  30. errMsg: string;
  31. errPhase: "decode" | "validate" | undefined;
  32. value?: undefined;
  33. };
  34. /** @public */
  35. declare type EncodeJsonOutput = {
  36. isSucc: true;
  37. /** Encoded JSON Object */
  38. json: any;
  39. errMsg?: undefined;
  40. } | {
  41. isSucc: false;
  42. /** Error message */
  43. errMsg: string;
  44. json?: undefined;
  45. };
  46. /** @public */
  47. export declare interface EncodeOptions {
  48. /** Skip validate value *before* encode */
  49. skipValidate?: boolean;
  50. }
  51. /** @public */
  52. export declare type EncodeOutput = {
  53. isSucc: true;
  54. /** Encoded binary buffer */
  55. buf: Uint8Array;
  56. errMsg?: undefined;
  57. } | {
  58. isSucc: false;
  59. /** Error message */
  60. errMsg: string;
  61. buf?: undefined;
  62. };
  63. /**
  64. * @public
  65. */
  66. export declare class TSBuffer<Proto extends TSBufferProto = TSBufferProto> {
  67. constructor(proto: Proto, options?: Partial<TSBufferOptions>);
  68. /**
  69. * 编码
  70. * @param value - 要编码的值
  71. * @param schemaOrId - Schema 或 SchemaID,例如`a/b.ts`下的`Test`类型,其ID为`a/b/Test`
  72. */
  73. encode(value: any, schemaOrId: string | TSBufferSchema, options?: EncodeOptions): EncodeOutput;
  74. /**
  75. * 解码
  76. * @param buf - 待解码的二进制数据
  77. * @param schemaOrId - Schema 或 SchemaID,例如`a/b.ts`下的`Test`类型,其ID为`a/b/Test`
  78. */
  79. decode<T = unknown>(buf: Uint8Array, schemaOrId: string | TSBufferSchema, options?: DecodeOptions): DecodeOutput<T>;
  80. /**
  81. * 编码为 JSON Object,根据协议将 JSON 不支持的格式(如 ArrayBuffer、Date、ObjectId)转换成 JSON 可传输的格式
  82. * @param value
  83. * @param schemaOrId
  84. * @param options
  85. */
  86. encodeJSON(value: any, schemaOrId: string | TSBufferSchema, options?: EncodeOptions): EncodeJsonOutput;
  87. /**
  88. * 从 JSON Object 解码,根据协议将 ArrayBuffer、Date、ObjectId 等类型从 JSON 中还原
  89. * @param json - JSON Object (是 JSON 对象,而非 JSON 字符串)
  90. * @param schemaOrId
  91. * @param options
  92. */
  93. decodeJSON<T = unknown>(json: any, schemaOrId: string | TSBufferSchema, options?: DecodeOptions): DecodeOutput<T>;
  94. validate: TSBufferValidator<Proto>["validate"];
  95. prune: TSBufferValidator<Proto>["prune"];
  96. }
  97. /** @public */
  98. export declare interface TSBufferOptions {
  99. /**
  100. * 检查值中是否包含Schema定义之外多余的字段
  101. * 仅对 `validate` 方法生效
  102. * 是因为实现机制原因, `prune` `encode` `decode` 方法都会天然保证不会混入多余字段
  103. *
  104. * 默认:`true`
  105. */
  106. excessPropertyChecks: boolean;
  107. /**
  108. * 同 `tsconfig.json` 中的 `strictNullChecks`
  109. * 是否使用严格等于去判定 `undefined` 和 `null`
  110. * 如果该值为 `false`,则在编码过程中,`null` 在类型不兼容时可编码为`undefined`,
  111. * 解码过程中,`undefined` 在类型不兼容时可解码为 `null`。
  112. * @defaultValue false
  113. */
  114. strictNullChecks: boolean;
  115. /**
  116. * 正常编码流程是:先校验value类型合法,再进行编码
  117. * 此值为 `true` 时,将跳过校验步骤以提升性能
  118. * 但需要自行确保值类型合法,否则可能引发不确定的问题
  119. * 默认为 `false`
  120. */
  121. skipEncodeValidate: boolean;
  122. /**
  123. * 正常解码流程是:先进行二进制解码,再校验解码后的类型符合Schema定义
  124. * 此值为 `true` 时,将跳过校验步骤以提升性能
  125. * 但需要自行确保值类型合法,否则可能引发不确定的问题
  126. * 默认为 `false`
  127. */
  128. skipDecodeValidate: boolean;
  129. /**
  130. * Clone the proto, don't change this if you don't know what it is.
  131. * @defaultValue true
  132. */
  133. cloneProto?: boolean;
  134. /**
  135. * Append `CustomTypeSchema` to given schema,
  136. * to customize validate & encode methods for specific types.
  137. * For example 'mongodb/ObjectId'.
  138. */
  139. customTypes?: {
  140. [schemaId: string]: CustomTypeSchema;
  141. };
  142. }
  143. export { }