@midwayjs/socketio 4.0.0-beta.10 → 4.0.0-beta.12

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.
Files changed (2) hide show
  1. package/dist/framework.js +204 -33
  2. package/package.json +3 -3
package/dist/framework.js CHANGED
@@ -73,16 +73,53 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
73
73
  }
74
74
  }
75
75
  async addNamespace(target) {
76
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
77
+ const traceMetaResolver = this.configurationOptions?.tracing?.meta;
78
+ const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
79
+ const traceExtractor = this.configurationOptions?.tracing
80
+ ?.extractor;
76
81
  const controllerOption = core_1.MetadataManager.getOwnMetadata(core_2.WS_CONTROLLER_KEY, target);
77
82
  const nsp = this.app.of(controllerOption.namespace);
78
83
  this.namespaceList.push(controllerOption.namespace);
79
84
  const controllerMiddleware = controllerOption.routerOptions.middleware ?? [];
80
85
  const controllerConnectionMiddleware = controllerOption.routerOptions.connectionMiddleware ?? [];
81
86
  nsp.use((socket, next) => {
82
- this.app.createAnonymousContext(socket);
83
- socket.requestContext.registerObject('socket', socket);
84
- socket.app = this.app;
85
- next();
87
+ const entryCarrierDefault = socket?.handshake?.headers ?? {};
88
+ const entryCarrier = typeof traceExtractor === 'function'
89
+ ? traceExtractor({
90
+ ctx: socket,
91
+ carrier: entryCarrierDefault,
92
+ request: socket?.handshake,
93
+ custom: {
94
+ namespace: controllerOption.namespace || '/',
95
+ eventName: 'connect',
96
+ },
97
+ })
98
+ : entryCarrierDefault;
99
+ traceService
100
+ .runWithEntrySpan(`socketio.connect ${controllerOption.namespace || '/'}`, {
101
+ enable: traceEnabled,
102
+ carrier: entryCarrier ?? entryCarrierDefault,
103
+ attributes: {
104
+ 'midway.protocol': 'socketio',
105
+ 'midway.socketio.namespace': controllerOption.namespace || '/',
106
+ },
107
+ meta: traceMetaResolver,
108
+ metaArgs: {
109
+ ctx: socket,
110
+ carrier: entryCarrier ?? entryCarrierDefault,
111
+ custom: {
112
+ namespace: controllerOption.namespace || '/',
113
+ eventName: 'connect',
114
+ },
115
+ },
116
+ }, async () => {
117
+ this.app.createAnonymousContext(socket);
118
+ socket.requestContext.registerObject('socket', socket);
119
+ socket.app = this.app;
120
+ next();
121
+ })
122
+ .catch(err => next(err));
86
123
  });
87
124
  // `connect`事件应该使用**同步**的方式调用以保证后续事件监听能够正常得到处理
88
125
  nsp.on('connect', (socket) => {
@@ -106,14 +143,40 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
106
143
  methodMap[wsEventInfo.propertyName] = methodMap[wsEventInfo.propertyName] || { responseEvents: [] };
107
144
  const controller = await socket.requestContext.getAsync(target);
108
145
  try {
109
- const fn = await this.middlewareService.compose([
110
- ...(wsEventInfo?.eventOptions?.middleware || []),
111
- async (ctx, next) => {
112
- // eslint-disable-next-line prefer-spread
113
- return controller[wsEventInfo.propertyName].apply(controller, [socket]);
146
+ const onConnectionCarrierDefault = socket?.handshake?.headers ?? {};
147
+ const onConnectionCarrier = typeof traceExtractor === 'function'
148
+ ? traceExtractor({
149
+ ctx: socket,
150
+ carrier: onConnectionCarrierDefault,
151
+ custom: {
152
+ eventName: wsEventInfo.propertyName,
153
+ },
154
+ })
155
+ : onConnectionCarrierDefault;
156
+ const result = await traceService.runWithEntrySpan(`socketio.event ${wsEventInfo.propertyName}`, {
157
+ enable: traceEnabled,
158
+ carrier: onConnectionCarrier ?? onConnectionCarrierDefault,
159
+ attributes: {
160
+ 'midway.protocol': 'socketio',
161
+ 'midway.socketio.event': wsEventInfo.propertyName,
114
162
  },
115
- ], this.app);
116
- const result = await fn(socket);
163
+ meta: traceMetaResolver,
164
+ metaArgs: {
165
+ ctx: socket,
166
+ carrier: onConnectionCarrier ?? onConnectionCarrierDefault,
167
+ custom: {
168
+ eventName: wsEventInfo.propertyName,
169
+ },
170
+ },
171
+ }, async () => {
172
+ const fn = await this.middlewareService.compose([
173
+ ...(wsEventInfo?.eventOptions?.middleware || []),
174
+ async (ctx, next) => {
175
+ return controller[wsEventInfo.propertyName].apply(controller, [socket]);
176
+ },
177
+ ], this.app);
178
+ return await fn(socket);
179
+ });
117
180
  await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
118
181
  }
119
182
  catch (err) {
@@ -134,24 +197,51 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
134
197
  const controller = await socket.requestContext.getAsync(target);
135
198
  debug('got message', wsEventInfo.messageEventName, args);
136
199
  try {
137
- const result = await (await this.applyMiddleware(async (ctx, next) => {
138
- // add controller middleware
139
- const fn = await this.middlewareService.compose([
140
- ...controllerMiddleware,
141
- ...(wsEventInfo?.eventOptions?.middleware || []),
142
- async (ctx, next) => {
143
- const isPassed = await this.app
144
- .getFramework()
145
- .runGuard(ctx, target, wsEventInfo.propertyName);
146
- if (!isPassed) {
147
- throw new core_1.MidwayInvokeForbiddenError(wsEventInfo.propertyName, target);
148
- }
149
- // eslint-disable-next-line prefer-spread
150
- return controller[wsEventInfo.propertyName].apply(controller, args);
200
+ const onMessageCarrierDefault = socket?.handshake?.headers ?? {};
201
+ const onMessageCarrier = typeof traceExtractor === 'function'
202
+ ? traceExtractor({
203
+ ctx: socket,
204
+ carrier: onMessageCarrierDefault,
205
+ custom: {
206
+ eventName: wsEventInfo.messageEventName,
207
+ },
208
+ })
209
+ : onMessageCarrierDefault;
210
+ const result = await traceService.runWithEntrySpan(`socketio.message ${wsEventInfo.messageEventName}`, {
211
+ enable: traceEnabled,
212
+ carrier: onMessageCarrier ?? onMessageCarrierDefault,
213
+ attributes: {
214
+ 'midway.protocol': 'socketio',
215
+ 'midway.socketio.event': wsEventInfo.messageEventName,
216
+ },
217
+ meta: traceMetaResolver,
218
+ metaArgs: {
219
+ ctx: socket,
220
+ carrier: onMessageCarrier ?? onMessageCarrierDefault,
221
+ custom: {
222
+ eventName: wsEventInfo.messageEventName,
151
223
  },
152
- ], this.app);
153
- return await fn(ctx, next);
154
- }))(socket);
224
+ },
225
+ }, async () => {
226
+ return await (await this.applyMiddleware(async (ctx, next) => {
227
+ // add controller middleware
228
+ const fn = await this.middlewareService.compose([
229
+ ...controllerMiddleware,
230
+ ...(wsEventInfo?.eventOptions?.middleware || []),
231
+ async (ctx, next) => {
232
+ const isPassed = await this.app
233
+ .getFramework()
234
+ .runGuard(ctx, target, wsEventInfo.propertyName);
235
+ if (!isPassed) {
236
+ throw new core_1.MidwayInvokeForbiddenError(wsEventInfo.propertyName, target);
237
+ }
238
+ // eslint-disable-next-line prefer-spread
239
+ return controller[wsEventInfo.propertyName].apply(controller, args);
240
+ },
241
+ ], this.app);
242
+ return await fn(ctx, next);
243
+ }))(socket);
244
+ });
155
245
  if (typeof args[args.length - 1] === 'function') {
156
246
  // ack
157
247
  args[args.length - 1](result);
@@ -171,7 +261,32 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
171
261
  socket.on('disconnect', async (reason) => {
172
262
  const controller = await socket.requestContext.getAsync(target);
173
263
  try {
174
- const result = await controller[wsEventInfo.propertyName].apply(controller, [reason]);
264
+ const onDisconnectCarrierDefault = socket?.handshake?.headers ?? {};
265
+ const onDisconnectCarrier = typeof traceExtractor === 'function'
266
+ ? traceExtractor({
267
+ ctx: socket,
268
+ carrier: onDisconnectCarrierDefault,
269
+ custom: {
270
+ eventName: 'disconnect',
271
+ },
272
+ })
273
+ : onDisconnectCarrierDefault;
274
+ const result = await traceService.runWithEntrySpan(`socketio.disconnect ${wsEventInfo.propertyName}`, {
275
+ enable: traceEnabled,
276
+ carrier: onDisconnectCarrier ?? onDisconnectCarrierDefault,
277
+ attributes: {
278
+ 'midway.protocol': 'socketio',
279
+ 'midway.socketio.event': 'disconnect',
280
+ },
281
+ meta: traceMetaResolver,
282
+ metaArgs: {
283
+ ctx: socket,
284
+ carrier: onDisconnectCarrier ?? onDisconnectCarrierDefault,
285
+ custom: {
286
+ eventName: 'disconnect',
287
+ },
288
+ },
289
+ }, async () => await controller[wsEventInfo.propertyName].apply(controller, [reason]));
175
290
  await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
176
291
  }
177
292
  catch (err) {
@@ -196,6 +311,10 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
196
311
  });
197
312
  }
198
313
  async bindSocketResponse(result, socket, propertyName, methodMap) {
314
+ const traceService = this.applicationContext.get(core_1.MidwayTraceService);
315
+ const traceMetaResolver = this.configurationOptions?.tracing?.meta;
316
+ const traceEnabled = this.configurationOptions?.tracing?.enable !== false;
317
+ const traceInjector = this.configurationOptions?.tracing?.injector;
199
318
  if (result && methodMap[propertyName]) {
200
319
  for (const wsEventInfo of methodMap[propertyName].responseEvents) {
201
320
  if (wsEventInfo.eventType === core_2.WSEventTypeEnum.EMIT) {
@@ -204,12 +323,64 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
204
323
  return socket.to(name);
205
324
  }, socket);
206
325
  }
207
- // eslint-disable-next-line prefer-spread
208
- socket.emit.apply(socket, [wsEventInfo.messageEventName].concat(result));
326
+ const carrier = typeof traceInjector === 'function'
327
+ ? traceInjector({
328
+ ctx: socket,
329
+ request: result,
330
+ custom: {
331
+ eventName: wsEventInfo.messageEventName,
332
+ },
333
+ }) || {}
334
+ : {};
335
+ await traceService.runWithExitSpan(`socketio.emit ${wsEventInfo.messageEventName}`, {
336
+ enable: traceEnabled,
337
+ carrier,
338
+ attributes: {
339
+ 'midway.protocol': 'socketio',
340
+ 'midway.socketio.event': wsEventInfo.messageEventName,
341
+ },
342
+ meta: traceMetaResolver,
343
+ metaArgs: {
344
+ ctx: socket,
345
+ carrier,
346
+ custom: {
347
+ eventName: wsEventInfo.messageEventName,
348
+ },
349
+ },
350
+ }, async () => {
351
+ // eslint-disable-next-line prefer-spread
352
+ socket.emit.apply(socket, [wsEventInfo.messageEventName].concat(result));
353
+ });
209
354
  }
210
355
  else if (wsEventInfo.eventType === core_2.WSEventTypeEnum.BROADCAST) {
211
- // eslint-disable-next-line prefer-spread
212
- socket.nsp.emit.apply(socket.nsp, [].concat(result));
356
+ const carrier = typeof traceInjector === 'function'
357
+ ? traceInjector({
358
+ ctx: socket,
359
+ request: result,
360
+ custom: {
361
+ eventName: propertyName,
362
+ },
363
+ }) || {}
364
+ : {};
365
+ await traceService.runWithExitSpan(`socketio.broadcast ${propertyName}`, {
366
+ enable: traceEnabled,
367
+ carrier,
368
+ attributes: {
369
+ 'midway.protocol': 'socketio',
370
+ 'midway.socketio.event': propertyName,
371
+ },
372
+ meta: traceMetaResolver,
373
+ metaArgs: {
374
+ ctx: socket,
375
+ carrier,
376
+ custom: {
377
+ eventName: propertyName,
378
+ },
379
+ },
380
+ }, async () => {
381
+ // eslint-disable-next-line prefer-spread
382
+ socket.nsp.emit.apply(socket.nsp, [].concat(result));
383
+ });
213
384
  }
214
385
  }
215
386
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/socketio",
3
- "version": "4.0.0-beta.10",
3
+ "version": "4.0.0-beta.12",
4
4
  "description": "Midway Web Framework for socket.io",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
- "@midwayjs/core": "^4.0.0-beta.10",
27
+ "@midwayjs/core": "^4.0.0-beta.12",
28
28
  "fs-extra": "11.3.3",
29
29
  "socket.io-client": "4.8.1"
30
30
  },
@@ -39,5 +39,5 @@
39
39
  "engines": {
40
40
  "node": ">=20"
41
41
  },
42
- "gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
42
+ "gitHead": "1c48179b7c827ba8ec9351e9b6c36ec1726d3e11"
43
43
  }