@midwayjs/ws 4.0.0-beta.11 → 4.0.0-beta.13
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/framework.js +145 -32
- package/package.json +5 -5
package/dist/framework.js
CHANGED
|
@@ -134,6 +134,20 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
134
134
|
const controllerMiddleware = controllerOption.routerOptions.middleware ?? [];
|
|
135
135
|
const controllerConnectionMiddleware = controllerOption.routerOptions.connectionMiddleware ?? [];
|
|
136
136
|
this.app.on('connection', async (socket, request) => {
|
|
137
|
+
const traceService = this.applicationContext.get(core_1.MidwayTraceService);
|
|
138
|
+
const traceMetaResolver = this.configurationOptions?.tracing
|
|
139
|
+
?.meta;
|
|
140
|
+
const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
|
|
141
|
+
const traceExtractor = this.configurationOptions?.tracing
|
|
142
|
+
?.extractor;
|
|
143
|
+
const entryCarrierDefault = request.headers;
|
|
144
|
+
const entryCarrier = typeof traceExtractor === 'function'
|
|
145
|
+
? traceExtractor({
|
|
146
|
+
ctx: socket,
|
|
147
|
+
carrier: entryCarrierDefault,
|
|
148
|
+
request,
|
|
149
|
+
})
|
|
150
|
+
: entryCarrierDefault;
|
|
137
151
|
socket.isAlive = true;
|
|
138
152
|
socket.on('error', error => {
|
|
139
153
|
this.logger.error(`socket got error: ${error}`);
|
|
@@ -151,7 +165,22 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
151
165
|
...this.connectionMiddlewareManager,
|
|
152
166
|
...controllerConnectionMiddleware,
|
|
153
167
|
], this.app);
|
|
154
|
-
await
|
|
168
|
+
await traceService.runWithEntrySpan(`ws.connect ${request.url || '/'}`, {
|
|
169
|
+
enable: traceEnabled,
|
|
170
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
171
|
+
attributes: {
|
|
172
|
+
'midway.protocol': 'ws',
|
|
173
|
+
'midway.ws.event': 'connection',
|
|
174
|
+
},
|
|
175
|
+
meta: traceMetaResolver,
|
|
176
|
+
metaArgs: {
|
|
177
|
+
ctx: socket,
|
|
178
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
179
|
+
request,
|
|
180
|
+
},
|
|
181
|
+
}, async () => {
|
|
182
|
+
await connectFn(socket);
|
|
183
|
+
});
|
|
155
184
|
const wsEventInfos = core_1.MetadataManager.getMetadata(core_1.WS_EVENT_KEY, target);
|
|
156
185
|
// 存储方法对应的响应处理
|
|
157
186
|
const methodMap = {};
|
|
@@ -162,20 +191,37 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
162
191
|
// on connection
|
|
163
192
|
if (wsEventInfo.eventType === core_1.WSEventTypeEnum.ON_CONNECTION) {
|
|
164
193
|
try {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
194
|
+
const result = await traceService.runWithEntrySpan(`ws.event ${wsEventInfo.propertyName}`, {
|
|
195
|
+
enable: traceEnabled,
|
|
196
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
197
|
+
attributes: {
|
|
198
|
+
'midway.protocol': 'ws',
|
|
199
|
+
'midway.ws.event': wsEventInfo.propertyName,
|
|
200
|
+
},
|
|
201
|
+
meta: traceMetaResolver,
|
|
202
|
+
metaArgs: {
|
|
203
|
+
ctx: socket,
|
|
204
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
205
|
+
request,
|
|
206
|
+
custom: {
|
|
207
|
+
eventName: wsEventInfo.propertyName,
|
|
208
|
+
},
|
|
176
209
|
},
|
|
177
|
-
|
|
178
|
-
|
|
210
|
+
}, async () => {
|
|
211
|
+
const fn = await this.middlewareService.compose([
|
|
212
|
+
...(wsEventInfo?.eventOptions?.middleware || []),
|
|
213
|
+
async (ctx, next) => {
|
|
214
|
+
const isPassed = await this.app
|
|
215
|
+
.getFramework()
|
|
216
|
+
.runGuard(ctx, target, wsEventInfo.propertyName);
|
|
217
|
+
if (!isPassed) {
|
|
218
|
+
throw new core_1.MidwayInvokeForbiddenError(wsEventInfo.propertyName, target);
|
|
219
|
+
}
|
|
220
|
+
return controller[wsEventInfo.propertyName].apply(controller, [socket, request]);
|
|
221
|
+
},
|
|
222
|
+
], this.app);
|
|
223
|
+
return await fn(socket);
|
|
224
|
+
});
|
|
179
225
|
await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
|
|
180
226
|
}
|
|
181
227
|
catch (err) {
|
|
@@ -187,18 +233,36 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
187
233
|
socket.on(wsEventInfo.messageEventName, async (...args) => {
|
|
188
234
|
debug('[ws]: got message', wsEventInfo.messageEventName, args);
|
|
189
235
|
try {
|
|
190
|
-
const result = await (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
236
|
+
const result = await traceService.runWithEntrySpan(`ws.message ${wsEventInfo.messageEventName}`, {
|
|
237
|
+
enable: traceEnabled,
|
|
238
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
239
|
+
attributes: {
|
|
240
|
+
'midway.protocol': 'ws',
|
|
241
|
+
'midway.ws.event': wsEventInfo.messageEventName,
|
|
242
|
+
},
|
|
243
|
+
meta: traceMetaResolver,
|
|
244
|
+
metaArgs: {
|
|
245
|
+
ctx: socket,
|
|
246
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
247
|
+
request,
|
|
248
|
+
custom: {
|
|
249
|
+
eventName: wsEventInfo.messageEventName,
|
|
198
250
|
},
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
251
|
+
},
|
|
252
|
+
}, async () => {
|
|
253
|
+
return await (await this.applyMiddleware(async (ctx, next) => {
|
|
254
|
+
// add controller middleware
|
|
255
|
+
const fn = await this.middlewareService.compose([
|
|
256
|
+
...controllerMiddleware,
|
|
257
|
+
...(wsEventInfo?.eventOptions?.middleware || []),
|
|
258
|
+
async (ctx, next) => {
|
|
259
|
+
// eslint-disable-next-line prefer-spread
|
|
260
|
+
return controller[wsEventInfo.propertyName].apply(controller, args);
|
|
261
|
+
},
|
|
262
|
+
], this.app);
|
|
263
|
+
return await fn(ctx, next);
|
|
264
|
+
}))(socket);
|
|
265
|
+
});
|
|
202
266
|
if (typeof args[args.length - 1] === 'function') {
|
|
203
267
|
// ack
|
|
204
268
|
args[args.length - 1](result);
|
|
@@ -217,7 +281,25 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
217
281
|
// on socket disconnect
|
|
218
282
|
socket.on('close', async (reason) => {
|
|
219
283
|
try {
|
|
220
|
-
const result = await
|
|
284
|
+
const result = await traceService.runWithEntrySpan(`ws.disconnect ${wsEventInfo.propertyName}`, {
|
|
285
|
+
enable: traceEnabled,
|
|
286
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
287
|
+
attributes: {
|
|
288
|
+
'midway.protocol': 'ws',
|
|
289
|
+
'midway.ws.event': 'disconnect',
|
|
290
|
+
},
|
|
291
|
+
meta: traceMetaResolver,
|
|
292
|
+
metaArgs: {
|
|
293
|
+
ctx: socket,
|
|
294
|
+
carrier: entryCarrier ?? entryCarrierDefault,
|
|
295
|
+
request,
|
|
296
|
+
custom: {
|
|
297
|
+
eventName: 'disconnect',
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
}, async () => {
|
|
301
|
+
return await controller[wsEventInfo.propertyName].apply(controller, [reason]);
|
|
302
|
+
});
|
|
221
303
|
await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
|
|
222
304
|
}
|
|
223
305
|
catch (err) {
|
|
@@ -234,29 +316,60 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
234
316
|
});
|
|
235
317
|
}
|
|
236
318
|
async bindSocketResponse(result, socket, propertyName, methodMap) {
|
|
319
|
+
const traceService = this.applicationContext.get(core_1.MidwayTraceService);
|
|
320
|
+
const traceMetaResolver = this.configurationOptions?.tracing?.meta;
|
|
321
|
+
const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
|
|
322
|
+
const traceInjector = this.configurationOptions?.tracing?.injector;
|
|
323
|
+
const sendWithTrace = async (currentSocket, payload, eventName) => {
|
|
324
|
+
const carrier = typeof traceInjector === 'function'
|
|
325
|
+
? traceInjector({
|
|
326
|
+
ctx: socket,
|
|
327
|
+
request: payload,
|
|
328
|
+
custom: { eventName },
|
|
329
|
+
}) || {}
|
|
330
|
+
: {};
|
|
331
|
+
await traceService.runWithExitSpan(`ws.emit ${eventName}`, {
|
|
332
|
+
enable: traceEnabled,
|
|
333
|
+
carrier,
|
|
334
|
+
attributes: {
|
|
335
|
+
'midway.protocol': 'ws',
|
|
336
|
+
'midway.ws.event': eventName,
|
|
337
|
+
},
|
|
338
|
+
meta: traceMetaResolver,
|
|
339
|
+
metaArgs: {
|
|
340
|
+
ctx: socket,
|
|
341
|
+
carrier,
|
|
342
|
+
custom: {
|
|
343
|
+
eventName,
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
}, async () => {
|
|
347
|
+
currentSocket.send(formatResult(payload));
|
|
348
|
+
});
|
|
349
|
+
};
|
|
237
350
|
if (!result)
|
|
238
351
|
return;
|
|
239
352
|
if (methodMap[propertyName]) {
|
|
240
353
|
for (const wsEventInfo of methodMap[propertyName].responseEvents) {
|
|
241
354
|
if (wsEventInfo.eventType === core_1.WSEventTypeEnum.EMIT) {
|
|
242
|
-
socket.
|
|
355
|
+
await sendWithTrace(socket, result, wsEventInfo.messageEventName);
|
|
243
356
|
}
|
|
244
357
|
else if (wsEventInfo.eventType === core_1.WSEventTypeEnum.BROADCAST) {
|
|
245
|
-
this.app.clients
|
|
358
|
+
for (const client of this.app.clients) {
|
|
246
359
|
if (client.readyState === WebSocket.OPEN) {
|
|
247
|
-
client
|
|
360
|
+
await sendWithTrace(client, result, propertyName);
|
|
248
361
|
}
|
|
249
|
-
}
|
|
362
|
+
}
|
|
250
363
|
}
|
|
251
364
|
}
|
|
252
365
|
if (methodMap[propertyName].responseEvents.length === 0) {
|
|
253
366
|
// no emit decorator
|
|
254
|
-
socket
|
|
367
|
+
await sendWithTrace(socket, result, propertyName);
|
|
255
368
|
}
|
|
256
369
|
}
|
|
257
370
|
else {
|
|
258
371
|
// just send
|
|
259
|
-
socket
|
|
372
|
+
await sendWithTrace(socket, result, propertyName);
|
|
260
373
|
}
|
|
261
374
|
}
|
|
262
375
|
getFrameworkName() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/ws",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.13",
|
|
4
4
|
"description": "Midway Web Framework for ws",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
27
|
-
"@midwayjs/koa": "^4.0.0-beta.
|
|
28
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
26
|
+
"@midwayjs/core": "^4.0.0-beta.13",
|
|
27
|
+
"@midwayjs/koa": "^4.0.0-beta.13",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.13",
|
|
29
29
|
"fs-extra": "11.3.3"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"engines": {
|
|
41
41
|
"node": ">=20"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "9a38b66a84a6880370cac90d737f94f9c5f2f256"
|
|
44
44
|
}
|