@zap-socket/server 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/events.d.ts +1 -1
- package/dist/events.js +1 -1
- package/dist/server.js +17 -10
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +21 -6
- package/package.json +1 -1
package/dist/events.d.ts
CHANGED
@@ -8,7 +8,7 @@ export type ZapEvent<T extends EventInput, R = any> = T extends z.ZodTypeAny ? {
|
|
8
8
|
process: () => R;
|
9
9
|
};
|
10
10
|
export type EventMap = Record<string, ZapEvent<any, any>>;
|
11
|
-
export declare const
|
11
|
+
export declare const zapEvent: <T extends EventInput, R>(eventObj: T extends z.ZodTypeAny ? {
|
12
12
|
input: T;
|
13
13
|
process: (input: z.infer<T>) => R;
|
14
14
|
} : {
|
package/dist/events.js
CHANGED
package/dist/server.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { WebSocketServer } from "ws";
|
2
|
-
import { generateId } from "./utils";
|
2
|
+
import { serialize, deserialize, generateId } from "./utils";
|
3
3
|
export class ZapServer {
|
4
4
|
wss;
|
5
5
|
wsToId;
|
@@ -10,22 +10,29 @@ export class ZapServer {
|
|
10
10
|
this.wsToId = new Map();
|
11
11
|
this.idToWs = new Map();
|
12
12
|
this.wss.on("connection", (ws) => {
|
13
|
-
const clientId = generateId();
|
14
|
-
console.log(`client ${clientId} connected`);
|
15
|
-
this.wsToId.set(ws, clientId);
|
16
|
-
this.idToWs.set(clientId, ws);
|
17
13
|
ws.on("message", (message) => {
|
14
|
+
const parsedMessage = deserialize(message.toString());
|
15
|
+
console.log("got message: ", parsedMessage);
|
18
16
|
if (!this.wsToId.get(ws)) {
|
19
17
|
const id = generateId();
|
20
18
|
this.wsToId.set(ws, id);
|
21
19
|
this.idToWs.set(id, ws);
|
22
20
|
ws.send("ID " + id);
|
21
|
+
console.log(`client ${id} connected.`);
|
22
|
+
return;
|
23
23
|
}
|
24
24
|
for (const [event, { process }] of Object.entries(this.events)) {
|
25
|
-
const parsedMessage =
|
26
|
-
if (parsedMessage
|
27
|
-
|
28
|
-
|
25
|
+
const parsedMessage = deserialize(message.toString());
|
26
|
+
if (parsedMessage &&
|
27
|
+
parsedMessage["event"] === event) {
|
28
|
+
const { data, requestId } = parsedMessage;
|
29
|
+
const result = process(data);
|
30
|
+
const serialized = serialize({ requestId, event, data: result });
|
31
|
+
// TODO: throw some nice error: only return stuff that is serializable
|
32
|
+
// i.e. primary data types and objects
|
33
|
+
if (!serialized)
|
34
|
+
return;
|
35
|
+
ws.send(serialized);
|
29
36
|
}
|
30
37
|
}
|
31
38
|
});
|
@@ -33,7 +40,7 @@ export class ZapServer {
|
|
33
40
|
this.removeClient(ws);
|
34
41
|
});
|
35
42
|
ws.on("error", (err) => {
|
36
|
-
console.error(`WebSocket error for ${
|
43
|
+
console.error(`WebSocket error for ${this.wsToId.get(ws)}:`, err);
|
37
44
|
});
|
38
45
|
});
|
39
46
|
}
|
package/dist/utils.d.ts
CHANGED
@@ -1 +1,4 @@
|
|
1
1
|
export declare const generateId: (length?: number) => string;
|
2
|
+
export declare const safeJsonParse: (jsonString: string) => Record<string, any> | null;
|
3
|
+
export declare const serialize: <T>(data: T) => string | null;
|
4
|
+
export declare const deserialize: <T>(data: string) => T | null;
|
package/dist/utils.js
CHANGED
@@ -1,10 +1,25 @@
|
|
1
|
-
export const generateId = (length =
|
1
|
+
export const generateId = (length = 8) => {
|
2
2
|
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
3
|
-
let id = "";
|
4
3
|
const randomValues = new Uint8Array(length);
|
5
4
|
crypto.getRandomValues(randomValues);
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
return Array.from(randomValues, (num) => chars[num % chars.length]).join('');
|
6
|
+
};
|
7
|
+
export const safeJsonParse = (jsonString) => {
|
8
|
+
try {
|
9
|
+
return JSON.parse(jsonString);
|
10
|
+
}
|
11
|
+
catch {
|
12
|
+
return null;
|
13
|
+
}
|
14
|
+
};
|
15
|
+
export const serialize = (data) => {
|
16
|
+
try {
|
17
|
+
return JSON.stringify(data);
|
18
|
+
}
|
19
|
+
catch {
|
20
|
+
return null;
|
21
|
+
}
|
22
|
+
};
|
23
|
+
export const deserialize = (data) => {
|
24
|
+
return safeJsonParse(data);
|
10
25
|
};
|
package/package.json
CHANGED