@midwayjs/ws 3.5.1 → 3.6.0

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.
@@ -7,11 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  };
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.WebSocketConfiguration = void 0;
10
- const decorator_1 = require("@midwayjs/decorator");
10
+ const core_1 = require("@midwayjs/core");
11
11
  let WebSocketConfiguration = class WebSocketConfiguration {
12
12
  };
13
13
  WebSocketConfiguration = __decorate([
14
- (0, decorator_1.Configuration)({
14
+ (0, core_1.Configuration)({
15
15
  namespace: 'webSocket',
16
16
  importConfigs: [
17
17
  {
package/dist/framework.js CHANGED
@@ -12,7 +12,6 @@ const http = require("http");
12
12
  const util_1 = require("util");
13
13
  const debug = (0, util_1.debuglog)('midway:debug');
14
14
  const WebSocket = require("ws");
15
- const decorator_1 = require("@midwayjs/decorator");
16
15
  let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
17
16
  constructor() {
18
17
  super(...arguments);
@@ -77,7 +76,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
77
76
  }
78
77
  async loadMidwayController() {
79
78
  // create room
80
- const controllerModules = (0, decorator_1.listModule)(decorator_1.WS_CONTROLLER_KEY);
79
+ const controllerModules = (0, core_1.listModule)(core_1.WS_CONTROLLER_KEY);
81
80
  if (controllerModules.length > 0) {
82
81
  // ws just one namespace
83
82
  await this.addNamespace(controllerModules[0]);
@@ -85,7 +84,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
85
84
  }
86
85
  async addNamespace(target) {
87
86
  var _a, _b;
88
- const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.WS_CONTROLLER_KEY, target);
87
+ const controllerOption = (0, core_1.getClassMetadata)(core_1.WS_CONTROLLER_KEY, target);
89
88
  const controllerMiddleware = (_a = controllerOption.routerOptions.middleware) !== null && _a !== void 0 ? _a : [];
90
89
  const controllerConnectionMiddleware = (_b = controllerOption.routerOptions.connectionMiddleware) !== null && _b !== void 0 ? _b : [];
91
90
  this.app.on('connection', async (socket, request) => {
@@ -100,7 +99,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
100
99
  ...controllerConnectionMiddleware,
101
100
  ], this.app);
102
101
  await connectFn(socket);
103
- const wsEventInfos = (0, decorator_1.getClassMetadata)(decorator_1.WS_EVENT_KEY, target);
102
+ const wsEventInfos = (0, core_1.getClassMetadata)(core_1.WS_EVENT_KEY, target);
104
103
  // 存储方法对应的响应处理
105
104
  const methodMap = {};
106
105
  if (wsEventInfos.length) {
@@ -108,11 +107,17 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
108
107
  methodMap[wsEventInfo.propertyName] = methodMap[wsEventInfo.propertyName] || { responseEvents: [] };
109
108
  const controller = await socket.requestContext.getAsync(target);
110
109
  // on connection
111
- if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.ON_CONNECTION) {
110
+ if (wsEventInfo.eventType === core_1.WSEventTypeEnum.ON_CONNECTION) {
112
111
  try {
113
112
  const fn = await this.middlewareService.compose([
114
113
  ...(((_a = wsEventInfo === null || wsEventInfo === void 0 ? void 0 : wsEventInfo.eventOptions) === null || _a === void 0 ? void 0 : _a.middleware) || []),
115
114
  async (ctx, next) => {
115
+ const isPassed = await this.app
116
+ .getFramework()
117
+ .runGuard(ctx, target, wsEventInfo.propertyName);
118
+ if (!isPassed) {
119
+ throw new core_1.MidwayInvokeForbiddenError(wsEventInfo.propertyName, target);
120
+ }
116
121
  // eslint-disable-next-line prefer-spread
117
122
  return controller[wsEventInfo.propertyName].apply(controller, [socket, request]);
118
123
  },
@@ -124,7 +129,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
124
129
  this.logger.error(err);
125
130
  }
126
131
  }
127
- else if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.ON_MESSAGE) {
132
+ else if (wsEventInfo.eventType === core_1.WSEventTypeEnum.ON_MESSAGE) {
128
133
  // on user custom event
129
134
  socket.on(wsEventInfo.messageEventName, async (...args) => {
130
135
  debug('[ws]: got message', wsEventInfo.messageEventName, args);
@@ -156,7 +161,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
156
161
  }
157
162
  });
158
163
  }
159
- else if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.ON_DISCONNECTION) {
164
+ else if (wsEventInfo.eventType === core_1.WSEventTypeEnum.ON_DISCONNECTION) {
160
165
  // on socket disconnect
161
166
  socket.on('close', async (reason) => {
162
167
  try {
@@ -187,10 +192,10 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
187
192
  return;
188
193
  if (methodMap[propertyName]) {
189
194
  for (const wsEventInfo of methodMap[propertyName].responseEvents) {
190
- if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.EMIT) {
195
+ if (wsEventInfo.eventType === core_1.WSEventTypeEnum.EMIT) {
191
196
  socket.send(formatResult(result));
192
197
  }
193
- else if (wsEventInfo.eventType === decorator_1.WSEventTypeEnum.BROADCAST) {
198
+ else if (wsEventInfo.eventType === core_1.WSEventTypeEnum.BROADCAST) {
194
199
  this.app.clients.forEach(client => {
195
200
  if (client.readyState === WebSocket.OPEN) {
196
201
  client.send(formatResult(result));
@@ -219,10 +224,10 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
219
224
  }
220
225
  };
221
226
  MidwayWSFramework = __decorate([
222
- (0, decorator_1.Framework)()
227
+ (0, core_1.Framework)()
223
228
  ], MidwayWSFramework);
224
229
  exports.MidwayWSFramework = MidwayWSFramework;
225
230
  function formatResult(result) {
226
- return decorator_1.Types.isObject(result) ? JSON.stringify(result) : result;
231
+ return core_1.Types.isObject(result) ? JSON.stringify(result) : result;
227
232
  }
228
233
  //# sourceMappingURL=framework.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/ws",
3
- "version": "3.5.1",
3
+ "version": "3.6.0",
4
4
  "description": "Midway Web Framework for ws",
5
5
  "main": "dist/index",
6
6
  "typings": "index.d.ts",
@@ -23,15 +23,14 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/decorator": "^3.4.11",
27
- "@midwayjs/mock": "^3.5.1",
26
+ "@midwayjs/mock": "^3.6.0",
28
27
  "@types/ws": "8.5.3",
29
28
  "fs-extra": "10.0.1"
30
29
  },
31
30
  "dependencies": {
32
- "@midwayjs/core": "^3.5.1",
31
+ "@midwayjs/core": "^3.6.0",
33
32
  "@midwayjs/logger": "^2.15.0",
34
- "ws": "8.8.1"
33
+ "ws": "8.9.0"
35
34
  },
36
35
  "author": "Harry Chen <czy88840616@gmail.com>",
37
36
  "repository": {
@@ -41,5 +40,5 @@
41
40
  "engines": {
42
41
  "node": ">=12"
43
42
  },
44
- "gitHead": "3b63fc768617037644602dd5ad66d3734ce01b16"
43
+ "gitHead": "22643b0e8519766bb7c68b975930199fc136336e"
45
44
  }