@miqro/core 5.0.18 → 5.1.0
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.js +6 -2
- package/build/app.js.map +1 -1
- package/build/middleware/body-parser.d.ts +9 -2
- package/build/middleware/body-parser.js +30 -25
- package/build/middleware/body-parser.js.map +1 -1
- package/build/middleware/router.js +9 -2
- package/build/middleware/router.js.map +1 -1
- package/build/types.d.ts +24 -7
- package/build/types.js +28 -4
- package/build/types.js.map +1 -1
- package/build/websocket.js +62 -34
- package/build/websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/app.ts +6 -2
- package/src/middleware/body-parser.ts +37 -24
- package/src/middleware/router.ts +9 -2
- package/src/types.ts +37 -10
- package/src/websocket.ts +62 -35
package/src/websocket.ts
CHANGED
|
@@ -37,12 +37,21 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
37
37
|
if (!client) {
|
|
38
38
|
throw new Error("client not found");
|
|
39
39
|
}
|
|
40
|
+
if (!client.socket.writable) {
|
|
41
|
+
this.clients.delete(clientUUID);
|
|
42
|
+
throw new Error("socket not writable");
|
|
43
|
+
}
|
|
44
|
+
if (client.socket.writableLength > (this.options.maxFrameSize ?? DEFAULT_MAX_FRAME_SIZE)) {
|
|
45
|
+
client.socket.destroy();
|
|
46
|
+
this.clients.delete(clientUUID);
|
|
47
|
+
throw new Error("client send buffer full");
|
|
48
|
+
}
|
|
40
49
|
return new Promise<void>((resolve, reject) => {
|
|
41
50
|
try {
|
|
42
|
-
client.
|
|
51
|
+
client.socket.write(createFrame(data), (err?: Error | null) => {
|
|
43
52
|
if (err) {
|
|
44
53
|
try {
|
|
45
|
-
client.
|
|
54
|
+
client.socket.destroy(err);
|
|
46
55
|
} catch (e) {
|
|
47
56
|
console.error(e);
|
|
48
57
|
}
|
|
@@ -51,10 +60,10 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
51
60
|
} else {
|
|
52
61
|
resolve();
|
|
53
62
|
}
|
|
54
|
-
})
|
|
63
|
+
});
|
|
55
64
|
} catch (e) {
|
|
56
65
|
try {
|
|
57
|
-
client.
|
|
66
|
+
client.socket.destroy();
|
|
58
67
|
} catch (e) {
|
|
59
68
|
console.error(e);
|
|
60
69
|
}
|
|
@@ -114,8 +123,8 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
114
123
|
|
|
115
124
|
socket.write(createUpgradeHeaders(acceptKey, extraHeaders));
|
|
116
125
|
|
|
117
|
-
socket.on("data", (data: Buffer) => {
|
|
118
|
-
const frame = parseFrame(data);
|
|
126
|
+
socket.on("data", async (data: Buffer) => {
|
|
127
|
+
const frame = parseFrame(data, this.options.maxFrameSize);
|
|
119
128
|
if (frame === PING) {
|
|
120
129
|
try {
|
|
121
130
|
socket.write(Buffer.from([0x8A, 0x00])); // pong frame
|
|
@@ -128,7 +137,7 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
128
137
|
socket.destroy();
|
|
129
138
|
} else {
|
|
130
139
|
try {
|
|
131
|
-
this.options.onMessage(client, frame);
|
|
140
|
+
await this.options.onMessage(client, frame);
|
|
132
141
|
} catch (e) {
|
|
133
142
|
req.logger.error(e);
|
|
134
143
|
}
|
|
@@ -145,24 +154,24 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
145
154
|
}
|
|
146
155
|
});
|
|
147
156
|
|
|
148
|
-
socket.on("error", (error) => {
|
|
157
|
+
socket.on("error", async (error) => {
|
|
149
158
|
req.logger.debug("upgraded connection error!");
|
|
150
159
|
req.logger.error(error);
|
|
151
160
|
if (this.options.onError) {
|
|
152
161
|
try {
|
|
153
|
-
this.options.onError(client, error);
|
|
162
|
+
await this.options.onError(client, error);
|
|
154
163
|
} catch (e) {
|
|
155
164
|
req.logger.error(e);
|
|
156
165
|
}
|
|
157
166
|
}
|
|
158
167
|
});
|
|
159
168
|
|
|
160
|
-
socket.on("end", () => {
|
|
169
|
+
socket.on("end", async () => {
|
|
161
170
|
req.logger.debug("upgraded connection disconnected!");
|
|
162
171
|
this.clients.delete(uuid);
|
|
163
172
|
if (this.options.onDisconnect) {
|
|
164
173
|
try {
|
|
165
|
-
this.options.onDisconnect(client);
|
|
174
|
+
await this.options.onDisconnect(client);
|
|
166
175
|
} catch (e) {
|
|
167
176
|
req.logger.error(e);
|
|
168
177
|
}
|
|
@@ -175,7 +184,7 @@ export class WebSocketServer implements ConnectionUpgrader {
|
|
|
175
184
|
this.clients.set(uuid, client);
|
|
176
185
|
if (this.options.onConnection) {
|
|
177
186
|
try {
|
|
178
|
-
this.options.onConnection(client);
|
|
187
|
+
await this.options.onConnection(client);
|
|
179
188
|
} catch (e) {
|
|
180
189
|
req.logger.error(e);
|
|
181
190
|
}
|
|
@@ -213,41 +222,53 @@ export class ClusterWebSocketServer extends WebSocketServer {
|
|
|
213
222
|
return options.validate ? options.validate(req) : true;
|
|
214
223
|
}
|
|
215
224
|
},
|
|
216
|
-
onError: (req, error) => {
|
|
225
|
+
onError: async (req, error) => {
|
|
217
226
|
if (process.send) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
227
|
+
try {
|
|
228
|
+
process.send({
|
|
229
|
+
type: ClusterWebSocketServerMessageType,
|
|
230
|
+
action: "error",
|
|
231
|
+
clientUUID: req.uuid,
|
|
232
|
+
errorMessage: error.message
|
|
233
|
+
} as ClusterWebSocketServerMessage);
|
|
234
|
+
} catch (e) {
|
|
235
|
+
req.req.logger.error(e);
|
|
236
|
+
}
|
|
224
237
|
}
|
|
225
238
|
if (options.onError) {
|
|
226
|
-
options.onError(req, error);
|
|
239
|
+
await options.onError(req, error);
|
|
227
240
|
}
|
|
228
241
|
},
|
|
229
|
-
onConnection: (req) => {
|
|
242
|
+
onConnection: async (req) => {
|
|
230
243
|
if (process.send) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
244
|
+
try {
|
|
245
|
+
process.send({
|
|
246
|
+
type: ClusterWebSocketServerMessageType,
|
|
247
|
+
action: "connection",
|
|
248
|
+
clientUUID: req.uuid
|
|
249
|
+
} as ClusterWebSocketServerMessage);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
req.req.logger.error(e);
|
|
252
|
+
}
|
|
236
253
|
}
|
|
237
254
|
if (options.onConnection) {
|
|
238
|
-
options.onConnection(req);
|
|
255
|
+
await options.onConnection(req);
|
|
239
256
|
}
|
|
240
257
|
},
|
|
241
|
-
onDisconnect: (req) => {
|
|
258
|
+
onDisconnect: async (req) => {
|
|
242
259
|
if (process.send) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
260
|
+
try {
|
|
261
|
+
process.send({
|
|
262
|
+
type: ClusterWebSocketServerMessageType,
|
|
263
|
+
action: "disconnection",
|
|
264
|
+
clientUUID: req.uuid
|
|
265
|
+
} as ClusterWebSocketServerMessage);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
req.req.logger.error(e);
|
|
268
|
+
}
|
|
248
269
|
}
|
|
249
270
|
if (options.onDisconnect) {
|
|
250
|
-
options.onDisconnect(req);
|
|
271
|
+
await options.onDisconnect(req);
|
|
251
272
|
}
|
|
252
273
|
}
|
|
253
274
|
});
|
|
@@ -355,7 +376,9 @@ function createUpgradeHeaders(acceptKey: string, extraHeaders?: {
|
|
|
355
376
|
return `HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${acceptValue}${extra}\r\n\r\n`;
|
|
356
377
|
}
|
|
357
378
|
|
|
358
|
-
|
|
379
|
+
const DEFAULT_MAX_FRAME_SIZE = 1024 * 1024; // 1 MiB
|
|
380
|
+
|
|
381
|
+
function parseFrame(buffer: Buffer, maxFrameSize = DEFAULT_MAX_FRAME_SIZE) {
|
|
359
382
|
const firstByte = buffer.readUInt8(0);
|
|
360
383
|
const opCode = firstByte & 0b00001111; // get last 4 bits of a byte
|
|
361
384
|
|
|
@@ -381,6 +404,10 @@ function parseFrame(buffer: Buffer) {
|
|
|
381
404
|
offset += 8;
|
|
382
405
|
}
|
|
383
406
|
|
|
407
|
+
if (payloadLength > maxFrameSize) {
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
|
|
384
411
|
const isMasked = Boolean((secondByte >>> 7) & 0x1); // get first bit of a second byte
|
|
385
412
|
|
|
386
413
|
if (isMasked) {
|