@zap-socket/server 0.0.6 → 0.0.7

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/server.d.ts CHANGED
@@ -6,6 +6,11 @@ interface ZapServerConstructorT {
6
6
  }
7
7
  export declare class ZapServer<T extends EventMap> {
8
8
  wss: WebSocketServer;
9
+ onconnect: (handler: (ctx: {
10
+ id: string;
11
+ ws: WebSocket;
12
+ }) => void) => void;
13
+ private onconnectHandler;
9
14
  private wsToId;
10
15
  private idToWs;
11
16
  private _events;
package/dist/server.js CHANGED
@@ -5,6 +5,8 @@ const isClientEvent = (event) => {
5
5
  };
6
6
  export class ZapServer {
7
7
  wss;
8
+ onconnect;
9
+ onconnectHandler;
8
10
  wsToId;
9
11
  idToWs;
10
12
  _events = {};
@@ -13,78 +15,78 @@ export class ZapServer {
13
15
  this.wsToId = new Map();
14
16
  this.idToWs = new Map();
15
17
  this._events = events;
18
+ this.onconnectHandler = () => { };
19
+ this.onconnect = (handler) => {
20
+ this.onconnectHandler = handler;
21
+ };
16
22
  this.wss.on("listening", () => {
17
23
  if (callback)
18
24
  callback();
19
25
  });
20
26
  this.wss.on("connection", (ws, req) => {
21
27
  ws.on("message", (message) => {
22
- if (!this.wsToId.get(ws)) {
28
+ if (!this.wsToId.get(ws) && message.toString() === "OPEN") {
23
29
  const id = generateId();
24
30
  this.wsToId.set(ws, id);
25
31
  this.idToWs.set(id, ws);
26
32
  ws.send("ID " + id);
27
- console.log(`client ${id} connected.`);
33
+ this.onconnectHandler({
34
+ id,
35
+ ws
36
+ });
28
37
  return;
29
38
  }
30
39
  const clientId = this.wsToId.get(ws);
31
- for (const [event, eventObj] of Object.entries(this._events)) {
32
- if (!isClientEvent(eventObj))
33
- continue;
34
- const { process, middleware } = eventObj;
35
- const parsedMessage = deserialize(message.toString());
36
- if (parsedMessage &&
37
- (parsedMessage["event"] === event || parsedMessage["stream"] === event)) {
38
- const { data, requestId, streamId } = parsedMessage;
39
- // Do middleware checks
40
- const ctx = {};
41
- if (middleware) {
42
- middleware.forEach((m) => {
43
- const metadata = {
44
- id: clientId,
45
- ip: req.socket.remoteAddress,
46
- timestamp: Date.now(),
47
- size: message.toString().length
48
- };
49
- const msg = {
50
- event,
51
- data: parsedMessage,
52
- metadata
53
- };
54
- if (!m(ctx, msg)) {
55
- return;
56
- }
57
- });
58
- }
59
- // By this point all the middlewares allow to pass through
60
- if (requestId) {
61
- const result = process(data, { server: this, id: this.wsToId.get(ws), buffer: ctx });
62
- const serialized = serialize(requestId ? { requestId, event, data: result } : { event, data: result });
63
- // TODO: throw some nice error: only return stuff that is serializable
64
- // i.e. primary data types and objects
65
- if (!serialized)
66
- return;
67
- ws.send(serialized);
68
- }
69
- else if (streamId) {
70
- const consumeStream = async () => {
71
- const result = process(data, { server: this, id: this.wsToId.get(ws), buffer: ctx });
72
- for await (const x of result) {
73
- const packet = {
74
- streamId,
75
- fragment: x
76
- };
77
- this.sendMessageRaw(clientId, packet);
78
- }
79
- this.sendMessageRaw(clientId, {
80
- streamId,
81
- done: true
82
- });
83
- };
84
- consumeStream();
85
- }
86
- return; // finally return to avoid looping through rest of events unneccessarily
40
+ const parsedMessage = deserialize(message.toString());
41
+ if (!parsedMessage)
42
+ return;
43
+ const { event, stream, data, requestId, streamId } = parsedMessage;
44
+ const key = event || stream;
45
+ const eventObj = this._events[key];
46
+ if (!eventObj || !isClientEvent(eventObj))
47
+ return;
48
+ const { process, middleware } = eventObj;
49
+ // Setup middleware context
50
+ const ctx = {};
51
+ if (middleware) {
52
+ for (const m of middleware) {
53
+ const metadata = {
54
+ id: clientId,
55
+ ip: req.socket.remoteAddress,
56
+ timestamp: Date.now(),
57
+ size: message.toString().length,
58
+ };
59
+ const msg = {
60
+ event: key,
61
+ data: parsedMessage,
62
+ metadata,
63
+ };
64
+ if (!m(ctx, msg))
65
+ return;
66
+ }
67
+ }
68
+ // All middleware passed
69
+ const context = { server: this, id: this.wsToId.get(ws), buffer: ctx };
70
+ if (requestId) { // req-res premitive
71
+ const result = process(data, context);
72
+ if (result === undefined) { // just ACK the request process returns nothing
73
+ ws.send("ACK " + requestId);
74
+ return;
87
75
  }
76
+ const serialized = serialize({ requestId, event: key, data: result });
77
+ if (!serialized)
78
+ return;
79
+ ws.send(serialized);
80
+ }
81
+ else if (streamId) { // stream premitive
82
+ const consumeStream = async () => {
83
+ const result = process(data, context);
84
+ for await (const fragment of result) {
85
+ this.sendMessageRaw(clientId, { streamId, fragment });
86
+ }
87
+ this.sendMessageRaw(clientId, { streamId, done: true });
88
+ };
89
+ consumeStream();
88
90
  }
89
91
  });
90
92
  ws.on("close", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zap-socket/server",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "A fully typesafe tRPC-inspired WebSocket library with Zod validation, req-res model, and native subscriptions.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",