|
| 1 | +import * as z from 'zod/mini'; |
| 2 | + |
| 3 | +/** |
| 4 | + * DDP Message Specification |
| 5 | + * https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md |
| 6 | + */ |
| 7 | + |
| 8 | +// Establishing connection |
| 9 | +const ConnectSchema = z.strictObject({ |
| 10 | + msg: z.literal('connect'), |
| 11 | + session: z.optional(z.string()), |
| 12 | + version: z.string(), |
| 13 | + support: z.array(z.string()), |
| 14 | +}); |
| 15 | + |
| 16 | +const ConnectedSchema = z.strictObject({ |
| 17 | + msg: z.literal('connected'), |
| 18 | + session: z.string(), |
| 19 | +}); |
| 20 | + |
| 21 | +const FailedSchema = z.strictObject({ |
| 22 | + msg: z.literal('failed'), |
| 23 | + version: z.string(), |
| 24 | +}); |
| 25 | + |
| 26 | +// Heartbeats |
| 27 | +const PingSchema = z.strictObject({ |
| 28 | + msg: z.literal('ping'), |
| 29 | + id: z.optional(z.string()), |
| 30 | +}); |
| 31 | + |
| 32 | +const PongSchema = z.strictObject({ |
| 33 | + msg: z.literal('pong'), |
| 34 | + id: z.optional(z.string()), |
| 35 | +}); |
| 36 | + |
| 37 | +// Managing data |
| 38 | +const AddedSchema = z.strictObject({ |
| 39 | + msg: z.literal('added'), |
| 40 | + collection: z.string(), |
| 41 | + id: z.json(), |
| 42 | + fields: z.optional(z.record(z.string(), z.json())), |
| 43 | + // FIXME: Apparently Meteor can send `cleared` in `added`... |
| 44 | + cleared: z.optional(z.array(z.string())), |
| 45 | +}); |
| 46 | + |
| 47 | +const AddedBeforeSchema = z.strictObject({ |
| 48 | + msg: z.literal('addedBefore'), |
| 49 | + collection: z.string(), |
| 50 | + id: z.json(), |
| 51 | + fields: z.optional(z.record(z.string(), z.json())), |
| 52 | + before: z.optional(z.string()), |
| 53 | +}); |
| 54 | + |
| 55 | +const ChangedSchema = z.strictObject({ |
| 56 | + msg: z.literal('changed'), |
| 57 | + collection: z.string(), |
| 58 | + id: z.json(), |
| 59 | + fields: z.optional(z.record(z.string(), z.json())), |
| 60 | + cleared: z.optional(z.array(z.string())), |
| 61 | +}); |
| 62 | + |
| 63 | +const MovedBeforeSchema = z.strictObject({ |
| 64 | + msg: z.literal('movedBefore'), |
| 65 | + collection: z.string(), |
| 66 | + id: z.json(), |
| 67 | + before: z.optional(z.string()), |
| 68 | +}); |
| 69 | + |
| 70 | +const RemovedSchema = z.strictObject({ |
| 71 | + msg: z.literal('removed'), |
| 72 | + collection: z.string(), |
| 73 | + id: z.json(), |
| 74 | +}); |
| 75 | +// Managing subscriptions |
| 76 | +const NosubSchema = z.strictObject({ |
| 77 | + msg: z.literal('nosub'), |
| 78 | + id: z.string(), |
| 79 | + error: z.optional(z.json()), |
| 80 | +}); |
| 81 | +const ReadySchema = z.strictObject({ |
| 82 | + msg: z.literal('ready'), |
| 83 | + subs: z.array(z.string()), |
| 84 | +}); |
| 85 | +const SubSchema = z.strictObject({ |
| 86 | + msg: z.literal('sub'), |
| 87 | + id: z.string(), |
| 88 | + name: z.string(), |
| 89 | + params: z.optional(z.array(z.json())), |
| 90 | +}); |
| 91 | + |
| 92 | +const UnsubSchema = z.strictObject({ |
| 93 | + msg: z.literal('unsub'), |
| 94 | + id: z.string(), |
| 95 | +}); |
| 96 | + |
| 97 | +// Remote procedure calls |
| 98 | +const MethodSchema = z.strictObject({ |
| 99 | + msg: z.literal('method'), |
| 100 | + id: z.string(), |
| 101 | + method: z.string(), |
| 102 | + params: z.optional(z.array(z.json())), |
| 103 | + randomSeed: z.optional(z.string()), |
| 104 | +}); |
| 105 | + |
| 106 | +const ResultSchema = z.strictObject({ |
| 107 | + msg: z.literal('result'), |
| 108 | + id: z.string(), |
| 109 | + error: z.optional(z.json()), |
| 110 | + result: z.optional(z.json()), |
| 111 | +}); |
| 112 | + |
| 113 | +const UpdatedSchema = z.strictObject({ |
| 114 | + msg: z.literal('updated'), |
| 115 | + methods: z.array(z.string()), |
| 116 | +}); |
| 117 | + |
| 118 | +const ServerIdSchema = z.strictObject({ |
| 119 | + msg: z.literal('server_id'), |
| 120 | + server_id: z.string(), |
| 121 | +}); |
| 122 | + |
| 123 | +/** |
| 124 | + * Main DDP Message Schema |
| 125 | + */ |
| 126 | +const DDPMessageSchema = z.discriminatedUnion('msg', [ |
| 127 | + // Connection |
| 128 | + ConnectSchema, |
| 129 | + ConnectedSchema, |
| 130 | + FailedSchema, |
| 131 | + |
| 132 | + // Heartbeats |
| 133 | + PingSchema, |
| 134 | + PongSchema, |
| 135 | + |
| 136 | + // Data |
| 137 | + AddedSchema, |
| 138 | + AddedBeforeSchema, |
| 139 | + ChangedSchema, |
| 140 | + MovedBeforeSchema, |
| 141 | + RemovedSchema, |
| 142 | + |
| 143 | + // Subscriptions |
| 144 | + NosubSchema, |
| 145 | + ReadySchema, |
| 146 | + SubSchema, |
| 147 | + UnsubSchema, |
| 148 | + |
| 149 | + // RPC |
| 150 | + MethodSchema, |
| 151 | + ResultSchema, |
| 152 | + UpdatedSchema, |
| 153 | + |
| 154 | + // Server ID |
| 155 | + ServerIdSchema, |
| 156 | +]); |
| 157 | + |
| 158 | +/** |
| 159 | + * Inferred TypeScript Type |
| 160 | + * This is "erasable" as it compiles away completely. |
| 161 | + */ |
| 162 | +export type DDPMessage = z.infer<typeof DDPMessageSchema>; |
| 163 | + |
| 164 | +/** |
| 165 | + * Parses a DDP message from JSON string |
| 166 | + * @param json The JSON string to parse |
| 167 | + * @returns The parsed DDP message |
| 168 | + * @throws If the JSON is invalid or does not conform to the DDP message schema |
| 169 | + */ |
| 170 | +export const parseDDPMessage = (json: string): DDPMessage => { |
| 171 | + return DDPMessageSchema.parse(JSON.parse(json)); |
| 172 | +}; |
0 commit comments