@midwayjs/socketio 3.4.0-beta.6 → 3.4.0-beta.7

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.
@@ -1,11 +1,11 @@
1
- import { BaseFramework, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
2
- import { IMidwaySocketIOApplication, IMidwaySocketIOConfigurationOptions, IMidwaySocketIOContext } from './interface';
3
- export declare class MidwaySocketIOFramework extends BaseFramework<IMidwaySocketIOApplication, IMidwaySocketIOContext, IMidwaySocketIOConfigurationOptions> {
1
+ import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, MidwayFrameworkType } from '@midwayjs/core';
2
+ import { Application, IMidwaySocketIOOptions, Context, NextFunction } from './interface';
3
+ export declare class MidwaySocketIOFramework extends BaseFramework<Application, Context, IMidwaySocketIOOptions> {
4
4
  private namespaceList;
5
- configure(): IMidwaySocketIOConfigurationOptions;
6
- applicationInitialize(options: IMidwayBootstrapOptions): void;
7
- app: IMidwaySocketIOApplication;
8
- protected afterContainerReady(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
5
+ app: Application;
6
+ protected connectionMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
7
+ configure(): IMidwaySocketIOOptions;
8
+ applicationInitialize(): void;
9
9
  run(): Promise<void>;
10
10
  protected beforeStop(): Promise<void>;
11
11
  getFrameworkType(): MidwayFrameworkType;
@@ -13,5 +13,7 @@ export declare class MidwaySocketIOFramework extends BaseFramework<IMidwaySocket
13
13
  private addNamespace;
14
14
  private bindSocketResponse;
15
15
  getFrameworkName(): string;
16
+ useConnectionMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
17
+ getConnectionMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
16
18
  }
17
19
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -16,17 +16,24 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
16
16
  constructor() {
17
17
  super(...arguments);
18
18
  this.namespaceList = [];
19
+ this.connectionMiddlewareManager = this.createMiddlewareManager();
19
20
  }
20
21
  configure() {
21
22
  return this.configService.getConfiguration('socketIO');
22
23
  }
23
- applicationInitialize(options) {
24
+ applicationInitialize() {
24
25
  this.app = new socket_io_1.Server(this.configurationOptions);
25
- }
26
- async afterContainerReady(options) {
27
- await this.loadMidwayController();
26
+ this.defineApplicationProperties({
27
+ useConnectionMiddleware: (middleware) => {
28
+ return this.useConnectionMiddleware(middleware);
29
+ },
30
+ getConnectionMiddleware: () => {
31
+ return this.getConnectionMiddleware();
32
+ },
33
+ });
28
34
  }
29
35
  async run() {
36
+ await this.loadMidwayController();
30
37
  if (this.configurationOptions.adapter) {
31
38
  this.app.adapter(this.configurationOptions.adapter);
32
39
  this.logger.debug('[midway:socketio] init socket.io-redis ready!');
@@ -61,9 +68,12 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
61
68
  }
62
69
  }
63
70
  async addNamespace(target) {
71
+ var _a, _b;
64
72
  const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.WS_CONTROLLER_KEY, target);
65
73
  const nsp = this.app.of(controllerOption.namespace);
66
74
  this.namespaceList.push(controllerOption.namespace);
75
+ const controllerMiddleware = (_a = controllerOption.routerOptions.middleware) !== null && _a !== void 0 ? _a : [];
76
+ const controllerConnectionMiddleware = (_b = controllerOption.routerOptions.connectionMiddleware) !== null && _b !== void 0 ? _b : [];
67
77
  nsp.use((socket, next) => {
68
78
  this.app.createAnonymousContext(socket);
69
79
  socket.requestContext.registerObject('socket', socket);
@@ -71,6 +81,13 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
71
81
  next();
72
82
  });
73
83
  nsp.on('connect', async (socket) => {
84
+ var _a;
85
+ // run connection middleware
86
+ const connectFn = await this.middlewareService.compose([
87
+ ...this.connectionMiddlewareManager,
88
+ ...controllerConnectionMiddleware,
89
+ ], this.app);
90
+ await connectFn(socket);
74
91
  const wsEventInfos = (0, decorator_1.getClassMetadata)(decorator_1.WS_EVENT_KEY, target);
75
92
  // 存储方法对应的响应处理
76
93
  const methodMap = {};
@@ -81,7 +98,14 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
81
98
  // on connection
82
99
  if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.ON_CONNECTION) {
83
100
  try {
84
- const result = await controller[wsEventInfo.propertyName].apply(controller, [socket]);
101
+ const fn = await this.middlewareService.compose([
102
+ ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
103
+ async (ctx, next) => {
104
+ // eslint-disable-next-line prefer-spread
105
+ return controller[wsEventInfo.propertyName].apply(controller, [socket]);
106
+ },
107
+ ], this.app);
108
+ const result = await fn(socket);
85
109
  await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
86
110
  }
87
111
  catch (err) {
@@ -94,8 +118,17 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
94
118
  debug('got message', wsEventInfo.messageEventName, args);
95
119
  try {
96
120
  const result = await (await this.applyMiddleware(async (ctx, next) => {
97
- // eslint-disable-next-line prefer-spread
98
- return controller[wsEventInfo.propertyName].apply(controller, args);
121
+ var _a;
122
+ // add controller middleware
123
+ const fn = await this.middlewareService.compose([
124
+ ...controllerMiddleware,
125
+ ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
126
+ async (ctx, next) => {
127
+ // eslint-disable-next-line prefer-spread
128
+ return controller[wsEventInfo.propertyName].apply(controller, args);
129
+ },
130
+ ], this.app);
131
+ return await fn(ctx, next);
99
132
  }))(socket);
100
133
  if (typeof args[args.length - 1] === 'function') {
101
134
  // ack
@@ -158,6 +191,12 @@ let MidwaySocketIOFramework = class MidwaySocketIOFramework extends core_1.BaseF
158
191
  getFrameworkName() {
159
192
  return 'midway:socketIO';
160
193
  }
194
+ useConnectionMiddleware(middleware) {
195
+ this.connectionMiddlewareManager.insertLast(middleware);
196
+ }
197
+ getConnectionMiddleware() {
198
+ return this.connectionMiddlewareManager;
199
+ }
161
200
  };
162
201
  MidwaySocketIOFramework = __decorate([
163
202
  (0, decorator_1.Framework)()
@@ -1,18 +1,22 @@
1
1
  import * as SocketIO from 'socket.io';
2
- import { IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
3
- export declare type IMidwaySocketIOApplication = IMidwayApplication<IMidwaySocketIOContext, {
4
- use(fn: (socket: IMidwaySocketIOContext, fn: (err?: any) => void) => void): SocketIO.Namespace;
2
+ import { CommonMiddlewareUnion, ContextMiddlewareManager, IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
3
+ export declare type Application = IMidwayApplication<Context, {
4
+ use(fn: (socket: Context, fn: (err?: any) => void) => void): SocketIO.Namespace;
5
+ useConnectionMiddleware: (middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>) => void;
6
+ getConnectionMiddleware: ContextMiddlewareManager<Context, NextFunction, undefined>;
5
7
  } & SocketIO.Server>;
6
- export declare type IMidwaySocketIOConfigurationOptions = {
8
+ export declare type IMidwaySocketIOOptions = {
7
9
  port?: number;
8
10
  pubClient?: any;
9
11
  subClient?: any;
10
12
  } & Partial<SocketIO.ServerOptions> & IConfigurationOptions;
11
- export declare type IMidwaySocketIOContext = IMidwayContext<SocketIO.Socket & {
12
- app: IMidwaySocketIOApplication;
13
+ export declare type Context = IMidwayContext<SocketIO.Socket & {
14
+ app: Application;
13
15
  }>;
14
- export declare type Application = IMidwaySocketIOApplication;
15
- export interface Context extends IMidwaySocketIOContext {
16
- }
17
16
  export declare type NextFunction = BaseNextFunction;
17
+ declare module '@midwayjs/core/dist/interface' {
18
+ interface MidwayConfig {
19
+ socketIO?: IMidwaySocketIOOptions;
20
+ }
21
+ }
18
22
  //# sourceMappingURL=interface.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/socketio",
3
- "version": "3.4.0-beta.6",
3
+ "version": "3.4.0-beta.7",
4
4
  "description": "Midway Web Framework for socket.io",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
@@ -24,14 +24,14 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
- "@midwayjs/decorator": "^3.4.0-beta.6",
28
- "@midwayjs/mock": "^3.4.0-beta.6",
27
+ "@midwayjs/decorator": "^3.4.0-beta.7",
28
+ "@midwayjs/mock": "^3.4.0-beta.7",
29
29
  "@types/socket.io-client": "1.4.36",
30
30
  "fs-extra": "10.0.1",
31
31
  "socket.io-client": "4.4.1"
32
32
  },
33
33
  "dependencies": {
34
- "@midwayjs/core": "^3.4.0-beta.6",
34
+ "@midwayjs/core": "^3.4.0-beta.7",
35
35
  "@midwayjs/logger": "^2.15.0",
36
36
  "socket.io": "4.4.1",
37
37
  "socket.io-redis": "^6.1.0"
@@ -44,5 +44,5 @@
44
44
  "engines": {
45
45
  "node": ">=12"
46
46
  },
47
- "gitHead": "8e187c4593d4832de32b6745af3e195f6b90816c"
47
+ "gitHead": "4d5cc59a7a33e49beeaf20fcfaf766438649959c"
48
48
  }