@miqro/core 5.0.9 → 5.0.11

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/src/websocket.ts CHANGED
@@ -26,10 +26,17 @@ export class WebSocketServer implements ConnectionUpgrader {
26
26
  const clients = this.clients.values();
27
27
  for (const client of clients) {
28
28
  if (fromUUID === undefined || client.uuid !== fromUUID) {
29
- tR.push(this.writeTo(client.uuid, data));
29
+ tR.push(new Promise<void>(async (resolve, reject) => {
30
+ try {
31
+ await this.writeTo(client.uuid, data);
32
+ resolve();
33
+ } catch (e) {
34
+ reject(e);
35
+ }
36
+ }));
30
37
  }
31
38
  }
32
- await Promise.all(tR);
39
+ await Promise.allSettled(tR);
33
40
  }
34
41
 
35
42
  public async writeTo(clientUUID: string, data: string) {
@@ -39,14 +46,26 @@ export class WebSocketServer implements ConnectionUpgrader {
39
46
  }
40
47
  return new Promise<void>((resolve, reject) => {
41
48
  try {
42
- client.req.socket.write(createFrame(data), function SocketWriteCallback(err?: Error) {
49
+ client.req.socket.write(createFrame(data), (err?: Error) => {
43
50
  if (err) {
51
+ try {
52
+ client.req.socket.destroy(err);
53
+ } catch (e) {
54
+ console.error(e);
55
+ }
56
+ this.clients.delete(clientUUID);
44
57
  reject(err);
45
58
  } else {
46
59
  resolve();
47
60
  }
48
61
  })
49
62
  } catch (e) {
63
+ try {
64
+ client.req.socket.destroy();
65
+ } catch (e) {
66
+ console.error(e);
67
+ }
68
+ this.clients.delete(clientUUID);
50
69
  reject(e);
51
70
  }
52
71
  });
@@ -118,6 +137,18 @@ export class WebSocketServer implements ConnectionUpgrader {
118
137
  }
119
138
  });
120
139
 
140
+ socket.on("error", (error) => {
141
+ req.logger.debug("upgraded connection error!");
142
+ req.logger.error(error);
143
+ if (this.options.onError) {
144
+ try {
145
+ this.options.onError(client, error);
146
+ } catch (e) {
147
+ req.logger.error(e);
148
+ }
149
+ }
150
+ });
151
+
121
152
  socket.on("end", () => {
122
153
  req.logger.debug("upgraded connection disconnected!");
123
154
  this.clients.delete(uuid);
@@ -130,6 +161,8 @@ export class WebSocketServer implements ConnectionUpgrader {
130
161
  }
131
162
  });
132
163
 
164
+
165
+
133
166
  req.logger.debug("connection upgraded");
134
167
  this.clients.set(uuid, client);
135
168
  if (this.options.onConnection) {
@@ -149,8 +182,9 @@ const ClusterWebSocketServerMessageType = "ClusterWebSocketServerMessage";
149
182
 
150
183
  export interface ClusterWebSocketServerMessage {
151
184
  type: typeof ClusterWebSocketServerMessageType;
152
- action: "connection" | "disconnection" | "sendMessage";
185
+ action: "connection" | "disconnection" | "sendMessage" | "error";
153
186
  clientUUID?: string;
187
+ errorMessage?: string;
154
188
  fromUUID?: string;
155
189
  payload?: Serializable;
156
190
  }
@@ -171,6 +205,19 @@ export class ClusterWebSocketServer extends WebSocketServer {
171
205
  return options.validate ? options.validate(req) : true;
172
206
  }
173
207
  },
208
+ onError: (req, error) => {
209
+ if (process.send) {
210
+ process.send({
211
+ type: ClusterWebSocketServerMessageType,
212
+ action: "error",
213
+ clientUUID: req.uuid,
214
+ errorMessage: error.message
215
+ } as ClusterWebSocketServerMessage);
216
+ }
217
+ if (options.onError) {
218
+ options.onError(req, error);
219
+ }
220
+ },
174
221
  onConnection: (req) => {
175
222
  if (process.send) {
176
223
  process.send({
@@ -201,40 +248,44 @@ export class ClusterWebSocketServer extends WebSocketServer {
201
248
  }
202
249
  ClusterWebSocketServer.instance = this;
203
250
  if (process.send) {
204
- process.on("message", (data) => {
205
- const msg = (data as ClusterWebSocketServerMessage);
206
- if (
207
- msg &&
208
- msg.type === ClusterWebSocketServerMessageType &&
209
- msg.action) {
210
- // receive message from cluster workers
211
- switch (msg.action) {
212
- case "connection":
213
- if (!msg.clientUUID) {
214
- throw new Error(`action [${msg.action}] without clientUUID`);
215
- }
216
- this.remoteClients.add(msg.clientUUID);
217
- break;
218
- case "disconnection":
219
- if (!msg.clientUUID) {
220
- throw new Error(`action [${msg.action}] without clientUUID`);
221
- }
222
- this.remoteClients.delete(msg.clientUUID);
223
- break;
224
- case "sendMessage": {
225
- const payload = String(msg.payload);
226
- if (!msg.clientUUID) {
227
- // broadcast to local clients
228
- super.broadcast(payload, msg.fromUUID);
229
- } else if (this.isConnected(msg.clientUUID)) {
230
- // write if local client
231
- super.writeTo(msg.clientUUID, payload);
251
+ process.on("message", async (data) => {
252
+ try {
253
+ const msg = (data as ClusterWebSocketServerMessage);
254
+ if (
255
+ msg &&
256
+ msg.type === ClusterWebSocketServerMessageType &&
257
+ msg.action) {
258
+ // receive message from cluster workers
259
+ switch (msg.action) {
260
+ case "connection":
261
+ if (!msg.clientUUID) {
262
+ throw new Error(`action [${msg.action}] without clientUUID`);
263
+ }
264
+ this.remoteClients.add(msg.clientUUID);
265
+ break;
266
+ case "disconnection":
267
+ if (!msg.clientUUID) {
268
+ throw new Error(`action [${msg.action}] without clientUUID`);
269
+ }
270
+ this.remoteClients.delete(msg.clientUUID);
271
+ break;
272
+ case "sendMessage": {
273
+ const payload = String(msg.payload);
274
+ if (!msg.clientUUID) {
275
+ // broadcast to local clients
276
+ await super.broadcast(payload, msg.fromUUID);
277
+ } else if (this.isConnected(msg.clientUUID)) {
278
+ // write if local client
279
+ await super.writeTo(msg.clientUUID, payload);
280
+ }
281
+ break;
232
282
  }
233
- break;
283
+ default:
284
+ throw new Error(`action [${msg.action}] not supported`);
234
285
  }
235
- default:
236
- throw new Error(`action [${msg.action}] not supported`);
237
286
  }
287
+ } catch (e) {
288
+ console.error(e);
238
289
  }
239
290
  });
240
291
  }
@@ -249,7 +300,7 @@ export class ClusterWebSocketServer extends WebSocketServer {
249
300
  fromUUID
250
301
  } as ClusterWebSocketServerMessage);
251
302
  }
252
- super.broadcast(payload, fromUUID);
303
+ return super.broadcast(payload, fromUUID);
253
304
  }
254
305
 
255
306
  public async writeTo(clientUUID: string, payload: string): Promise<void> {
@@ -268,7 +319,7 @@ export class ClusterWebSocketServer extends WebSocketServer {
268
319
  // due to the implementation of isUUIDValid depends on this.remoteClients to be sync there can be two clients registered with the same UUID in the cluster
269
320
  if (this.clients.has(clientUUID)) {
270
321
  // write to local client
271
- super.writeTo(clientUUID, payload);
322
+ return super.writeTo(clientUUID, payload);
272
323
  }
273
324
  }
274
325