wire.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // V4 physical wire schema:logical topic frame 保持原子 seq 语义,超大帧只在
  2. // UTF-8 byte 层分片。codec/reassembly 纯函数见 wire-codec.ts。
  3. import { z } from "zod";
  4. import { PROTOCOL_V4_LIMITS, V4_WIRE_PROTOCOL_VERSION } from "./core.js";
  5. import { topicWireBase64Schema } from "./wire-binary.js";
  6. export const topicWireChecksumSchema = z
  7. .object({
  8. algorithm: z.literal("crc32"),
  9. value: z.string().regex(/^[0-9a-f]{8}$/u),
  10. })
  11. .strict();
  12. export type TopicWireChecksum = z.infer<typeof topicWireChecksumSchema>;
  13. /** publisher reservation 对物理帧用途的权威标记;consumer 禁止按 RPC 时序猜测。 */
  14. export const topicFrameDeliveryKindSchema = z.enum(["initial", "online", "recovery"]);
  15. export type TopicFrameDeliveryKind = z.infer<typeof topicFrameDeliveryKindSchema>;
  16. export type TopicWireFrame<F> =
  17. | {
  18. wireVersion: typeof V4_WIRE_PROTOCOL_VERSION;
  19. kind: "complete";
  20. deliveryKind: TopicFrameDeliveryKind;
  21. logicalFrameId: string;
  22. logicalFrameOrdinal: number;
  23. topic: string;
  24. subscriptionId: string;
  25. frame: F;
  26. }
  27. | {
  28. wireVersion: typeof V4_WIRE_PROTOCOL_VERSION;
  29. kind: "fragment";
  30. deliveryKind: TopicFrameDeliveryKind;
  31. logicalFrameId: string;
  32. logicalFrameOrdinal: number;
  33. topic: string;
  34. subscriptionId: string;
  35. fragmentIndex: number;
  36. fragmentCount: number;
  37. logicalBytes: number;
  38. checksum: TopicWireChecksum;
  39. dataBase64: string;
  40. };
  41. /**
  42. * service 边界只验证可安全路由/计量的 outer 形状;range/base64/checksum/logical
  43. * payload 的完整校验必须在 ownership 过滤后的 assembler 中产生 typed fault。
  44. */
  45. export const topicWireFrameCandidateSchema = z.discriminatedUnion("kind", [
  46. z
  47. .object({
  48. wireVersion: z.literal(V4_WIRE_PROTOCOL_VERSION),
  49. kind: z.literal("complete"),
  50. // ownership 路由只读 topic/subId;坏 deliveryKind 必须进入 owned assembler
  51. // 产生 typed fault,不能在 service 边界 warn/drop 后让 store 永久等待。
  52. deliveryKind: z.unknown().optional(),
  53. logicalFrameId: z.string().min(1),
  54. logicalFrameOrdinal: z.number(),
  55. topic: z.string().min(1),
  56. subscriptionId: z.string().min(1),
  57. // inner payload 故意不在 service route boundary 校验;缺失/类型/extra 由
  58. // ownership 后 assembler 统一转 typed fault,避免早期 warn/drop 永久 loading。
  59. frame: z.unknown().optional(),
  60. })
  61. .passthrough(),
  62. z
  63. .object({
  64. wireVersion: z.literal(V4_WIRE_PROTOCOL_VERSION),
  65. kind: z.literal("fragment"),
  66. deliveryKind: z.unknown().optional(),
  67. logicalFrameId: z.string().min(1),
  68. logicalFrameOrdinal: z.number(),
  69. topic: z.string().min(1),
  70. subscriptionId: z.string().min(1),
  71. fragmentIndex: z.unknown().optional(),
  72. fragmentCount: z.unknown().optional(),
  73. logicalBytes: z.unknown().optional(),
  74. checksum: z.unknown().optional(),
  75. dataBase64: z.unknown().optional(),
  76. })
  77. .passthrough(),
  78. ]);
  79. export type TopicWireFrameCandidate = z.infer<typeof topicWireFrameCandidateSchema>;
  80. export function createTopicWireFrameSchema<F extends z.ZodTypeAny>(frameSchema: F) {
  81. return z
  82. .discriminatedUnion("kind", [
  83. z
  84. .object({
  85. wireVersion: z.literal(V4_WIRE_PROTOCOL_VERSION),
  86. kind: z.literal("complete"),
  87. deliveryKind: topicFrameDeliveryKindSchema,
  88. logicalFrameId: z.string().min(1),
  89. logicalFrameOrdinal: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
  90. topic: z.string().min(1),
  91. subscriptionId: z.string().min(1),
  92. frame: frameSchema,
  93. })
  94. .strict(),
  95. z
  96. .object({
  97. wireVersion: z.literal(V4_WIRE_PROTOCOL_VERSION),
  98. kind: z.literal("fragment"),
  99. deliveryKind: topicFrameDeliveryKindSchema,
  100. logicalFrameId: z.string().min(1),
  101. logicalFrameOrdinal: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
  102. topic: z.string().min(1),
  103. subscriptionId: z.string().min(1),
  104. fragmentIndex: z.number().int().nonnegative(),
  105. fragmentCount: z
  106. .number()
  107. .int()
  108. .positive()
  109. .max(PROTOCOL_V4_LIMITS.logicalFrameAssemblyMaxFragments),
  110. logicalBytes: z.number().int().positive(),
  111. checksum: topicWireChecksumSchema,
  112. dataBase64: topicWireBase64Schema,
  113. })
  114. .strict(),
  115. ])
  116. .superRefine((wire, context) => {
  117. const value = wire as unknown as TopicWireFrame<z.output<F>>;
  118. if (value.kind === "fragment") {
  119. if (value.fragmentIndex >= value.fragmentCount) {
  120. context.addIssue({
  121. code: "custom",
  122. message: "fragmentIndex must be smaller than fragmentCount",
  123. path: ["fragmentIndex"],
  124. });
  125. }
  126. if (value.fragmentCount > value.logicalBytes) {
  127. context.addIssue({
  128. code: "custom",
  129. message: "fragmentCount cannot exceed logicalBytes",
  130. path: ["fragmentCount"],
  131. });
  132. }
  133. return;
  134. }
  135. const frame = value.frame as {
  136. topic?: unknown;
  137. subscriptionId?: unknown;
  138. };
  139. if (frame.topic !== value.topic) {
  140. context.addIssue({
  141. code: "custom",
  142. message: "complete wire topic must match logical frame topic",
  143. path: ["topic"],
  144. });
  145. }
  146. if (frame.subscriptionId !== value.subscriptionId) {
  147. context.addIssue({
  148. code: "custom",
  149. message: "complete wire subscriptionId must match logical frame subscriptionId",
  150. path: ["subscriptionId"],
  151. });
  152. }
  153. });
  154. }