-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ts
44 lines (38 loc) · 1003 Bytes
/
Utils.ts
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
import { v5 } from "https://deno.land/[email protected]/uuid/mod.ts";
import { flags } from "./server.ts";
import { isConnected } from "./WebSocketInterface.ts";
export async function generateId(
ws: WebSocket,
isHost: boolean,
): Promise<string> {
if (isHost) {
return generateHostId();
} else {
return await v5.generate(
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
new TextEncoder().encode(
JSON.stringify(ws) + Math.random() * Math.random(),
),
);
}
}
function generateHostId(): string {
if (flags.debug) return "0000";
let id: string;
do {
id = Math.floor(Math.random() * 998 + 1)
.toString().padStart(4, "0");
} while (isConnected(id));
return id;
}
export function isHostId(id: string): boolean {
return id.match(/^0[0-9]{3}$/) !== null;
}
export function safeParseJson(data: any): any | undefined {
try {
return JSON.parse(data);
} catch (e) {
return undefined;
console.error("Client sent invalid JSON", e);
}
}