@midwayjs/ws 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,9 +1,10 @@
1
1
  /// <reference types="node" />
2
- import { BaseFramework, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
2
+ import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
3
3
  import * as http from 'http';
4
- import { IMidwayWSApplication, IMidwayWSConfigurationOptions, IMidwayWSContext } from './interface';
5
- export declare class MidwayWSFramework extends BaseFramework<IMidwayWSApplication, IMidwayWSContext, IMidwayWSConfigurationOptions> {
4
+ import { Application, Context, IMidwayWSApplication, IMidwayWSConfigurationOptions, NextFunction } from './interface';
5
+ export declare class MidwayWSFramework extends BaseFramework<Application, Context, IMidwayWSConfigurationOptions> {
6
6
  server: http.Server;
7
+ protected connectionMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
7
8
  configure(): IMidwayWSConfigurationOptions;
8
9
  applicationInitialize(options: IMidwayBootstrapOptions): void;
9
10
  app: IMidwayWSApplication;
@@ -15,5 +16,7 @@ export declare class MidwayWSFramework extends BaseFramework<IMidwayWSApplicatio
15
16
  private addNamespace;
16
17
  private bindSocketResponse;
17
18
  getFrameworkName(): string;
19
+ useConnectionMiddleware(middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>): void;
20
+ getConnectionMiddleware(): ContextMiddlewareManager<Context, NextFunction, undefined>;
18
21
  }
19
22
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -14,6 +14,10 @@ const debug = (0, util_1.debuglog)('midway:debug');
14
14
  const WebSocket = require("ws");
15
15
  const decorator_1 = require("@midwayjs/decorator");
16
16
  let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
17
+ constructor() {
18
+ super(...arguments);
19
+ this.connectionMiddlewareManager = this.createMiddlewareManager();
20
+ }
17
21
  configure() {
18
22
  return this.configService.getConfiguration('webSocket');
19
23
  }
@@ -21,6 +25,14 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
21
25
  this.configurationOptions.noServer = true;
22
26
  const opts = Object.assign({}, this.configurationOptions, { port: null });
23
27
  this.app = new WebSocket.Server(opts);
28
+ this.defineApplicationProperties({
29
+ useConnectionMiddleware: (middleware) => {
30
+ return this.useConnectionMiddleware(middleware);
31
+ },
32
+ getConnectionMiddleware: () => {
33
+ return this.getConnectionMiddleware();
34
+ },
35
+ });
24
36
  }
25
37
  async afterContainerReady(options) {
26
38
  await this.loadMidwayController();
@@ -72,11 +84,22 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
72
84
  }
73
85
  }
74
86
  async addNamespace(target) {
87
+ var _a, _b;
88
+ const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.WS_CONTROLLER_KEY, target);
89
+ const controllerMiddleware = (_a = controllerOption.routerOptions.middleware) !== null && _a !== void 0 ? _a : [];
90
+ const controllerConnectionMiddleware = (_b = controllerOption.routerOptions.connectionMiddleware) !== null && _b !== void 0 ? _b : [];
75
91
  this.app.on('connection', async (socket, request) => {
92
+ var _a;
76
93
  // create request context
77
94
  this.app.createAnonymousContext(socket);
78
95
  socket.requestContext.registerObject('socket', socket);
79
96
  socket.app = this.app;
97
+ // run connection middleware
98
+ const connectFn = await this.middlewareService.compose([
99
+ ...this.connectionMiddlewareManager,
100
+ ...controllerConnectionMiddleware,
101
+ ], this.app);
102
+ await connectFn(socket);
80
103
  const wsEventInfos = (0, decorator_1.getClassMetadata)(decorator_1.WS_EVENT_KEY, target);
81
104
  // 存储方法对应的响应处理
82
105
  const methodMap = {};
@@ -87,7 +110,14 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
87
110
  // on connection
88
111
  if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.ON_CONNECTION) {
89
112
  try {
90
- const result = await controller[wsEventInfo.propertyName].apply(controller, [socket, request]);
113
+ const fn = await this.middlewareService.compose([
114
+ ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
115
+ async (ctx, next) => {
116
+ // eslint-disable-next-line prefer-spread
117
+ return controller[wsEventInfo.propertyName].apply(controller, [socket, request]);
118
+ },
119
+ ], this.app);
120
+ const result = await fn(socket);
91
121
  await this.bindSocketResponse(result, socket, wsEventInfo.propertyName, methodMap);
92
122
  }
93
123
  catch (err) {
@@ -100,8 +130,17 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
100
130
  debug('[ws]: got message', wsEventInfo.messageEventName, args);
101
131
  try {
102
132
  const result = await (await this.applyMiddleware(async (ctx, next) => {
103
- // eslint-disable-next-line prefer-spread
104
- return controller[wsEventInfo.propertyName].apply(controller, args);
133
+ var _a;
134
+ // add controller middleware
135
+ const fn = await this.middlewareService.compose([
136
+ ...controllerMiddleware,
137
+ ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
138
+ async (ctx, next) => {
139
+ // eslint-disable-next-line prefer-spread
140
+ return controller[wsEventInfo.propertyName].apply(controller, args);
141
+ },
142
+ ], this.app);
143
+ return await fn(ctx, next);
105
144
  }))(socket);
106
145
  if (typeof args[args.length - 1] === 'function') {
107
146
  // ack
@@ -172,6 +211,12 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
172
211
  getFrameworkName() {
173
212
  return 'midway:ws';
174
213
  }
214
+ useConnectionMiddleware(middleware) {
215
+ this.connectionMiddlewareManager.insertLast(middleware);
216
+ }
217
+ getConnectionMiddleware() {
218
+ return this.connectionMiddlewareManager;
219
+ }
175
220
  };
176
221
  MidwayWSFramework = __decorate([
177
222
  (0, decorator_1.Framework)()
@@ -1,6 +1,9 @@
1
1
  import * as WebSocket from 'ws';
2
- import { IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
3
- export declare type IMidwayWSApplication = IMidwayApplication<IMidwayWSContext> & WebSocket.Server;
2
+ import { CommonMiddlewareUnion, ContextMiddlewareManager, IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
3
+ export declare type IMidwayWSApplication = IMidwayApplication<IMidwayWSContext, {
4
+ useConnectionMiddleware: (middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>) => void;
5
+ getConnectionMiddleware: ContextMiddlewareManager<Context, NextFunction, undefined>;
6
+ }> & WebSocket.Server;
4
7
  export declare type IMidwayWSConfigurationOptions = {
5
8
  pubClient?: any;
6
9
  subClient?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/ws",
3
- "version": "3.4.0-beta.6",
3
+ "version": "3.4.0-beta.7",
4
4
  "description": "Midway Web Framework for ws",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
@@ -23,13 +23,13 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/decorator": "^3.4.0-beta.6",
27
- "@midwayjs/mock": "^3.4.0-beta.6",
26
+ "@midwayjs/decorator": "^3.4.0-beta.7",
27
+ "@midwayjs/mock": "^3.4.0-beta.7",
28
28
  "@types/ws": "8.5.3",
29
29
  "fs-extra": "10.0.1"
30
30
  },
31
31
  "dependencies": {
32
- "@midwayjs/core": "^3.4.0-beta.6",
32
+ "@midwayjs/core": "^3.4.0-beta.7",
33
33
  "@midwayjs/logger": "^2.15.0",
34
34
  "ws": "8.8.0"
35
35
  },
@@ -41,5 +41,5 @@
41
41
  "engines": {
42
42
  "node": ">=12"
43
43
  },
44
- "gitHead": "8e187c4593d4832de32b6745af3e195f6b90816c"
44
+ "gitHead": "4d5cc59a7a33e49beeaf20fcfaf766438649959c"
45
45
  }