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