@midwayjs/ws 4.0.0-alpha.1 → 4.0.0-beta.2
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/README.md +1 -1
- package/dist/configuration.js +7 -0
- package/dist/framework.d.ts +8 -1
- package/dist/framework.js +56 -13
- package/dist/interface.d.ts +10 -0
- package/package.json +8 -7
package/README.md
CHANGED
package/dist/configuration.js
CHANGED
|
@@ -21,6 +21,13 @@ exports.WebSocketConfiguration = WebSocketConfiguration = __decorate([
|
|
|
21
21
|
enableServerHeartbeatCheck: false,
|
|
22
22
|
serverHeartbeatInterval: 30000,
|
|
23
23
|
},
|
|
24
|
+
midwayLogger: {
|
|
25
|
+
clients: {
|
|
26
|
+
wsLogger: {
|
|
27
|
+
fileLogName: 'midway-ws.log',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
24
31
|
},
|
|
25
32
|
},
|
|
26
33
|
],
|
package/dist/framework.d.ts
CHANGED
|
@@ -2,16 +2,23 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { BaseFramework, CommonMiddlewareUnion, ContextMiddlewareManager, IMidwayBootstrapOptions } from '@midwayjs/core';
|
|
4
4
|
import * as http from 'http';
|
|
5
|
-
import { Application, Context, IMidwayWSApplication, IMidwayWSConfigurationOptions, NextFunction } from './interface';
|
|
5
|
+
import { Application, Context, IMidwayWSApplication, IMidwayWSConfigurationOptions, NextFunction, UpgradeAuthHandler } 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 upgradeAuthHandler: UpgradeAuthHandler | null;
|
|
11
|
+
protected frameworkLoggerName: string;
|
|
10
12
|
configure(): IMidwayWSConfigurationOptions;
|
|
11
13
|
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
12
14
|
app: IMidwayWSApplication;
|
|
13
15
|
run(): Promise<void>;
|
|
14
16
|
protected beforeStop(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* 设置升级前鉴权处理函数
|
|
19
|
+
* @param handler 鉴权处理函数,传入 null 可以禁用鉴权
|
|
20
|
+
*/
|
|
21
|
+
onWebSocketUpgrade(handler: UpgradeAuthHandler | null): void;
|
|
15
22
|
private loadMidwayController;
|
|
16
23
|
private addNamespace;
|
|
17
24
|
private bindSocketResponse;
|
package/dist/framework.js
CHANGED
|
@@ -16,6 +16,8 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
16
16
|
constructor() {
|
|
17
17
|
super(...arguments);
|
|
18
18
|
this.connectionMiddlewareManager = this.createMiddlewareManager();
|
|
19
|
+
this.upgradeAuthHandler = null;
|
|
20
|
+
this.frameworkLoggerName = 'wsLogger';
|
|
19
21
|
}
|
|
20
22
|
configure() {
|
|
21
23
|
return this.configService.getConfiguration('webSocket');
|
|
@@ -31,6 +33,9 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
31
33
|
getConnectionMiddleware: () => {
|
|
32
34
|
return this.getConnectionMiddleware();
|
|
33
35
|
},
|
|
36
|
+
onWebSocketUpgrade: (handler) => {
|
|
37
|
+
return this.onWebSocketUpgrade(handler);
|
|
38
|
+
},
|
|
34
39
|
});
|
|
35
40
|
await this.loadMidwayController();
|
|
36
41
|
}
|
|
@@ -43,7 +48,34 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
43
48
|
else {
|
|
44
49
|
server = this.configurationOptions.server ?? http.createServer();
|
|
45
50
|
}
|
|
46
|
-
|
|
51
|
+
if (this.configurationOptions.enableServerHeartbeatCheck) {
|
|
52
|
+
if (server.listening) {
|
|
53
|
+
this.startHeartBeat();
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
server.on('listening', () => {
|
|
57
|
+
this.startHeartBeat();
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
server.on('upgrade', async (request, socket, head) => {
|
|
62
|
+
// check if the upgrade auth handler is set
|
|
63
|
+
if (this.upgradeAuthHandler) {
|
|
64
|
+
try {
|
|
65
|
+
const authResult = await this.upgradeAuthHandler(request, socket, head);
|
|
66
|
+
if (!authResult) {
|
|
67
|
+
this.logger.warn('[midway:ws] WebSocket upgrade authentication failed');
|
|
68
|
+
socket.destroy();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.logger.debug('[midway:ws] WebSocket upgrade authentication passed');
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
this.logger.error('[midway:ws] WebSocket upgrade authentication error:', error);
|
|
75
|
+
socket.destroy();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
47
79
|
this.app.handleUpgrade(request, socket, head, ws => {
|
|
48
80
|
this.app.emit('connection', ws, request);
|
|
49
81
|
});
|
|
@@ -53,13 +85,19 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
53
85
|
await new Promise(resolve => {
|
|
54
86
|
server.listen(this.configurationOptions.port, () => {
|
|
55
87
|
this.logger.info(`[midway:ws] WebSocket server port = ${this.configurationOptions.port} start success.`);
|
|
56
|
-
if (this.configurationOptions.enableServerHeartbeatCheck) {
|
|
57
|
-
this.startHeartBeat();
|
|
58
|
-
}
|
|
59
88
|
resolve();
|
|
60
89
|
});
|
|
61
90
|
});
|
|
62
91
|
}
|
|
92
|
+
this.app.on('error', err => {
|
|
93
|
+
this.logger.error('socket server got error', err);
|
|
94
|
+
});
|
|
95
|
+
this.app.on('close', () => {
|
|
96
|
+
if (this.heartBeatInterval) {
|
|
97
|
+
clearInterval(this.heartBeatInterval);
|
|
98
|
+
}
|
|
99
|
+
this.logger.info('socket server close');
|
|
100
|
+
});
|
|
63
101
|
}
|
|
64
102
|
async beforeStop() {
|
|
65
103
|
return new Promise(resolve => {
|
|
@@ -71,6 +109,19 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
71
109
|
this.server.close();
|
|
72
110
|
});
|
|
73
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* 设置升级前鉴权处理函数
|
|
114
|
+
* @param handler 鉴权处理函数,传入 null 可以禁用鉴权
|
|
115
|
+
*/
|
|
116
|
+
onWebSocketUpgrade(handler) {
|
|
117
|
+
this.upgradeAuthHandler = handler;
|
|
118
|
+
if (handler) {
|
|
119
|
+
this.logger.info('[midway:ws] WebSocket upgrade authentication handler set');
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
this.logger.info('[midway:ws] WebSocket upgrade authentication handler removed');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
74
125
|
async loadMidwayController() {
|
|
75
126
|
// create room
|
|
76
127
|
const controllerModules = core_1.DecoratorManager.listModule(core_1.WS_CONTROLLER_KEY);
|
|
@@ -94,6 +145,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
94
145
|
// create request context
|
|
95
146
|
this.app.createAnonymousContext(socket);
|
|
96
147
|
socket.requestContext.registerObject('socket', socket);
|
|
148
|
+
socket.request = request;
|
|
97
149
|
socket.app = this.app;
|
|
98
150
|
// run connection middleware
|
|
99
151
|
const connectFn = await this.middlewareService.compose([
|
|
@@ -181,15 +233,6 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
181
233
|
}
|
|
182
234
|
}
|
|
183
235
|
});
|
|
184
|
-
this.app.on('error', err => {
|
|
185
|
-
this.logger.error('socket server got error', err);
|
|
186
|
-
});
|
|
187
|
-
this.app.on('close', () => {
|
|
188
|
-
if (this.heartBeatInterval) {
|
|
189
|
-
clearInterval(this.heartBeatInterval);
|
|
190
|
-
}
|
|
191
|
-
this.logger.info('socket server close');
|
|
192
|
-
});
|
|
193
236
|
}
|
|
194
237
|
async bindSocketResponse(result, socket, propertyName, methodMap) {
|
|
195
238
|
if (!result)
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
1
4
|
import * as WebSocket from 'ws';
|
|
2
5
|
import { CommonMiddlewareUnion, ContextMiddlewareManager, IConfigurationOptions, IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
6
|
+
import type { IncomingMessage } from 'http';
|
|
3
7
|
export type IMidwayWSApplication = IMidwayApplication<IMidwayWSContext, {
|
|
4
8
|
useConnectionMiddleware: (middleware: CommonMiddlewareUnion<Context, NextFunction, undefined>) => void;
|
|
5
9
|
getConnectionMiddleware: ContextMiddlewareManager<Context, NextFunction, undefined>;
|
|
10
|
+
onWebSocketUpgrade: (handler: UpgradeAuthHandler | null) => void;
|
|
6
11
|
}> & WebSocket.Server;
|
|
7
12
|
export type IMidwayWSConfigurationOptions = {
|
|
8
13
|
pubClient?: any;
|
|
@@ -19,9 +24,14 @@ export type IMidwayWSConfigurationOptions = {
|
|
|
19
24
|
export type IMidwayWSContext = IMidwayContext<WebSocket & {
|
|
20
25
|
app: IMidwayWSApplication;
|
|
21
26
|
isAlive: boolean;
|
|
27
|
+
request: IncomingMessage;
|
|
22
28
|
}>;
|
|
23
29
|
export type Application = IMidwayWSApplication;
|
|
24
30
|
export type NextFunction = BaseNextFunction;
|
|
25
31
|
export interface Context extends IMidwayWSContext {
|
|
26
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* WebSocket 升级前鉴权处理函数类型
|
|
35
|
+
*/
|
|
36
|
+
export type UpgradeAuthHandler = (request: IncomingMessage, socket: any, head: Buffer) => Promise<boolean>;
|
|
27
37
|
//# sourceMappingURL=interface.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/ws",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-beta.2",
|
|
4
4
|
"description": "Midway Web Framework for ws",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -23,12 +23,13 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/core": "^4.0.0-
|
|
27
|
-
"@midwayjs/
|
|
28
|
-
"
|
|
26
|
+
"@midwayjs/core": "^4.0.0-beta.2",
|
|
27
|
+
"@midwayjs/koa": "^4.0.0-beta.2",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.2",
|
|
29
|
+
"fs-extra": "11.3.0"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@types/ws": "8.5.
|
|
32
|
+
"@types/ws": "8.5.14",
|
|
32
33
|
"ws": "8.18.0"
|
|
33
34
|
},
|
|
34
35
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"url": "https://github.com/midwayjs/midway.git"
|
|
38
39
|
},
|
|
39
40
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
41
|
+
"node": ">=20"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
|
|
43
44
|
}
|