@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/build/app.d.ts +6 -0
- package/build/app.js +35 -5
- package/build/app.js.map +1 -1
- package/build/common.d.ts +1 -1
- package/build/common.js +7 -7
- package/build/common.js.map +1 -1
- package/build/lib.d.ts +2 -2
- package/build/lib.js +2 -2
- package/build/lib.js.map +1 -1
- package/build/middleware/router.d.ts +3 -2
- package/build/middleware/router.js +98 -60
- package/build/middleware/router.js.map +1 -1
- package/build/middleware/session.d.ts +4 -0
- package/build/middleware/session.js +17 -40
- package/build/middleware/session.js.map +1 -1
- package/build/types.d.ts +36 -14
- package/build/types.js +91 -28
- package/build/types.js.map +1 -1
- package/build/websocket.d.ts +2 -1
- package/build/websocket.js +88 -36
- package/build/websocket.js.map +1 -1
- package/package.json +2 -2
- package/src/app.ts +36 -5
- package/src/common.ts +8 -8
- package/src/lib.ts +11 -1
- package/src/middleware/router.ts +103 -61
- package/src/middleware/session.ts +17 -40
- package/src/types.ts +119 -42
- package/src/websocket.ts +88 -37
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(
|
|
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.
|
|
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),
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
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
|
|