-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.ts
More file actions
48 lines (41 loc) · 960 Bytes
/
rpc.ts
File metadata and controls
48 lines (41 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import z from "zod";
type RPCEvent = z.infer<typeof rpcEventSchema>;
export function parseRPCEvent(data: Buffer<ArrayBufferLike>): RPCEvent | null {
try {
const json = JSON.parse(data.toString());
return rpcEventSchema.parse(json);
} catch {
return null;
}
}
const roomChangeSchema = z.object({
method: z.literal("room_change"),
params: z.object({
uuid: z.string(),
viewer_count: z.number(),
}),
});
const newRoomSchema = z.object({
method: z.literal("new_room"),
params: z.object({
uuid: z.string(),
}),
});
const newThumbnailSchema = z.object({
method: z.literal("new_thumbnail"),
params: z.object({
uuid: z.string(),
}),
});
const terminateRoomSchema = z.object({
method: z.literal("terminate_room"),
params: z.object({
uuid: z.string(),
}),
});
const rpcEventSchema = z.discriminatedUnion("method", [
roomChangeSchema,
newRoomSchema,
newThumbnailSchema,
terminateRoomSchema,
]);