@midwayjs/ws 3.19.0 → 4.0.0-alpha.1

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.
@@ -10,7 +10,8 @@ exports.WebSocketConfiguration = void 0;
10
10
  const core_1 = require("@midwayjs/core");
11
11
  let WebSocketConfiguration = class WebSocketConfiguration {
12
12
  };
13
- WebSocketConfiguration = __decorate([
13
+ exports.WebSocketConfiguration = WebSocketConfiguration;
14
+ exports.WebSocketConfiguration = WebSocketConfiguration = __decorate([
14
15
  (0, core_1.Configuration)({
15
16
  namespace: 'webSocket',
16
17
  importConfigs: [
@@ -25,5 +26,4 @@ WebSocketConfiguration = __decorate([
25
26
  ],
26
27
  })
27
28
  ], WebSocketConfiguration);
28
- exports.WebSocketConfiguration = WebSocketConfiguration;
29
29
  //# sourceMappingURL=configuration.js.map
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
3
+ import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, IMidwayBootstrapOptions } from '@midwayjs/core';
4
4
  import * as http from 'http';
5
5
  import { Application, Context, IMidwayWSApplication, IMidwayWSConfigurationOptions, NextFunction } from './interface';
6
6
  export declare class MidwayWSFramework extends BaseFramework<Application, Context, IMidwayWSConfigurationOptions> {
@@ -8,12 +8,10 @@ export declare class MidwayWSFramework extends BaseFramework<Application, Contex
8
8
  protected heartBeatInterval: NodeJS.Timeout;
9
9
  protected connectionMiddlewareManager: ContextMiddlewareManager<Context, unknown, unknown>;
10
10
  configure(): IMidwayWSConfigurationOptions;
11
- applicationInitialize(options: IMidwayBootstrapOptions): void;
11
+ applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
12
12
  app: IMidwayWSApplication;
13
- protected afterContainerReady(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
14
13
  run(): Promise<void>;
15
14
  protected beforeStop(): Promise<void>;
16
- getFrameworkType(): MidwayFrameworkType;
17
15
  private loadMidwayController;
18
16
  private addNamespace;
19
17
  private bindSocketResponse;
package/dist/framework.js CHANGED
@@ -20,7 +20,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
20
20
  configure() {
21
21
  return this.configService.getConfiguration('webSocket');
22
22
  }
23
- applicationInitialize(options) {
23
+ async applicationInitialize(options) {
24
24
  this.configurationOptions.noServer = true;
25
25
  const opts = Object.assign({}, this.configurationOptions, { port: null });
26
26
  this.app = new WebSocket.Server(opts);
@@ -32,19 +32,16 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
32
32
  return this.getConnectionMiddleware();
33
33
  },
34
34
  });
35
- }
36
- async afterContainerReady(options) {
37
35
  await this.loadMidwayController();
38
36
  }
39
37
  async run() {
40
- var _a;
41
38
  let server;
42
39
  if (!this.configurationOptions.port) {
43
40
  server = this.applicationContext.get(core_1.HTTP_SERVER_KEY);
44
41
  this.logger.info('[midway:ws] WebSocket server find shared http server and will be attach.');
45
42
  }
46
43
  else {
47
- server = (_a = this.configurationOptions.server) !== null && _a !== void 0 ? _a : http.createServer();
44
+ server = this.configurationOptions.server ?? http.createServer();
48
45
  }
49
46
  server.on('upgrade', (request, socket, head) => {
50
47
  this.app.handleUpgrade(request, socket, head, ws => {
@@ -74,24 +71,19 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
74
71
  this.server.close();
75
72
  });
76
73
  }
77
- getFrameworkType() {
78
- return core_1.MidwayFrameworkType.WS;
79
- }
80
74
  async loadMidwayController() {
81
75
  // create room
82
- const controllerModules = (0, core_1.listModule)(core_1.WS_CONTROLLER_KEY);
76
+ const controllerModules = core_1.DecoratorManager.listModule(core_1.WS_CONTROLLER_KEY);
83
77
  if (controllerModules.length > 0) {
84
78
  // ws just one namespace
85
79
  await this.addNamespace(controllerModules[0]);
86
80
  }
87
81
  }
88
82
  async addNamespace(target) {
89
- var _a, _b;
90
- const controllerOption = (0, core_1.getClassMetadata)(core_1.WS_CONTROLLER_KEY, target);
91
- const controllerMiddleware = (_a = controllerOption.routerOptions.middleware) !== null && _a !== void 0 ? _a : [];
92
- const controllerConnectionMiddleware = (_b = controllerOption.routerOptions.connectionMiddleware) !== null && _b !== void 0 ? _b : [];
83
+ const controllerOption = core_1.MetadataManager.getOwnMetadata(core_1.WS_CONTROLLER_KEY, target);
84
+ const controllerMiddleware = controllerOption.routerOptions.middleware ?? [];
85
+ const controllerConnectionMiddleware = controllerOption.routerOptions.connectionMiddleware ?? [];
93
86
  this.app.on('connection', async (socket, request) => {
94
- var _a;
95
87
  socket.isAlive = true;
96
88
  socket.on('error', error => {
97
89
  this.logger.error(`socket got error: ${error}`);
@@ -109,7 +101,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
109
101
  ...controllerConnectionMiddleware,
110
102
  ], this.app);
111
103
  await connectFn(socket);
112
- const wsEventInfos = (0, core_1.getClassMetadata)(core_1.WS_EVENT_KEY, target);
104
+ const wsEventInfos = core_1.MetadataManager.getMetadata(core_1.WS_EVENT_KEY, target);
113
105
  // 存储方法对应的响应处理
114
106
  const methodMap = {};
115
107
  if (wsEventInfos.length) {
@@ -120,7 +112,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
120
112
  if (wsEventInfo.eventType === core_1.WSEventTypeEnum.ON_CONNECTION) {
121
113
  try {
122
114
  const fn = await this.middlewareService.compose([
123
- ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
115
+ ...(wsEventInfo?.eventOptions?.middleware || []),
124
116
  async (ctx, next) => {
125
117
  const isPassed = await this.app
126
118
  .getFramework()
@@ -145,11 +137,10 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
145
137
  debug('[ws]: got message', wsEventInfo.messageEventName, args);
146
138
  try {
147
139
  const result = await (await this.applyMiddleware(async (ctx, next) => {
148
- var _a;
149
140
  // add controller middleware
150
141
  const fn = await this.middlewareService.compose([
151
142
  ...controllerMiddleware,
152
- ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
143
+ ...(wsEventInfo?.eventOptions?.middleware || []),
153
144
  async (ctx, next) => {
154
145
  // eslint-disable-next-line prefer-spread
155
146
  return controller[wsEventInfo.propertyName].apply(controller, args);
@@ -227,7 +218,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
227
218
  }
228
219
  }
229
220
  getFrameworkName() {
230
- return 'midway:ws';
221
+ return 'webSocket';
231
222
  }
232
223
  useConnectionMiddleware(middleware) {
233
224
  this.connectionMiddlewareManager.insertLast(middleware);
@@ -248,10 +239,10 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
248
239
  }, this.configurationOptions.serverHeartbeatInterval);
249
240
  }
250
241
  };
251
- MidwayWSFramework = __decorate([
242
+ exports.MidwayWSFramework = MidwayWSFramework;
243
+ exports.MidwayWSFramework = MidwayWSFramework = __decorate([
252
244
  (0, core_1.Framework)()
253
245
  ], MidwayWSFramework);
254
- exports.MidwayWSFramework = MidwayWSFramework;
255
246
  function formatResult(result) {
256
247
  return core_1.Types.isObject(result) ? JSON.stringify(result) : result;
257
248
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@midwayjs/ws",
3
- "version": "3.19.0",
3
+ "version": "4.0.0-alpha.1",
4
4
  "description": "Midway Web Framework for ws",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
10
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
9
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
10
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
11
11
  "ci": "npm run test"
12
12
  },
13
13
  "keywords": [
@@ -23,8 +23,8 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/core": "^3.19.0",
27
- "@midwayjs/mock": "^3.19.0",
26
+ "@midwayjs/core": "^4.0.0-alpha.1",
27
+ "@midwayjs/mock": "^4.0.0-alpha.1",
28
28
  "fs-extra": "11.2.0"
29
29
  },
30
30
  "dependencies": {
@@ -39,5 +39,5 @@
39
39
  "engines": {
40
40
  "node": ">=12"
41
41
  },
42
- "gitHead": "437e92d9375b0e375f83aa363f3e6a60187c7109"
42
+ "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
43
43
  }