@midwayjs/ws 4.0.0-beta.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/dist/framework.d.ts +7 -1
- package/dist/framework.js +55 -13
- package/dist/interface.d.ts +10 -0
- package/package.json +6 -5
package/dist/framework.d.ts
CHANGED
|
@@ -2,17 +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;
|
|
10
11
|
protected frameworkLoggerName: string;
|
|
11
12
|
configure(): IMidwayWSConfigurationOptions;
|
|
12
13
|
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
13
14
|
app: IMidwayWSApplication;
|
|
14
15
|
run(): Promise<void>;
|
|
15
16
|
protected beforeStop(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* 设置升级前鉴权处理函数
|
|
19
|
+
* @param handler 鉴权处理函数,传入 null 可以禁用鉴权
|
|
20
|
+
*/
|
|
21
|
+
onWebSocketUpgrade(handler: UpgradeAuthHandler | null): void;
|
|
16
22
|
private loadMidwayController;
|
|
17
23
|
private addNamespace;
|
|
18
24
|
private bindSocketResponse;
|
package/dist/framework.js
CHANGED
|
@@ -16,6 +16,7 @@ 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;
|
|
19
20
|
this.frameworkLoggerName = 'wsLogger';
|
|
20
21
|
}
|
|
21
22
|
configure() {
|
|
@@ -32,6 +33,9 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
32
33
|
getConnectionMiddleware: () => {
|
|
33
34
|
return this.getConnectionMiddleware();
|
|
34
35
|
},
|
|
36
|
+
onWebSocketUpgrade: (handler) => {
|
|
37
|
+
return this.onWebSocketUpgrade(handler);
|
|
38
|
+
},
|
|
35
39
|
});
|
|
36
40
|
await this.loadMidwayController();
|
|
37
41
|
}
|
|
@@ -44,7 +48,34 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
44
48
|
else {
|
|
45
49
|
server = this.configurationOptions.server ?? http.createServer();
|
|
46
50
|
}
|
|
47
|
-
|
|
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
|
+
}
|
|
48
79
|
this.app.handleUpgrade(request, socket, head, ws => {
|
|
49
80
|
this.app.emit('connection', ws, request);
|
|
50
81
|
});
|
|
@@ -54,13 +85,19 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
54
85
|
await new Promise(resolve => {
|
|
55
86
|
server.listen(this.configurationOptions.port, () => {
|
|
56
87
|
this.logger.info(`[midway:ws] WebSocket server port = ${this.configurationOptions.port} start success.`);
|
|
57
|
-
if (this.configurationOptions.enableServerHeartbeatCheck) {
|
|
58
|
-
this.startHeartBeat();
|
|
59
|
-
}
|
|
60
88
|
resolve();
|
|
61
89
|
});
|
|
62
90
|
});
|
|
63
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
|
+
});
|
|
64
101
|
}
|
|
65
102
|
async beforeStop() {
|
|
66
103
|
return new Promise(resolve => {
|
|
@@ -72,6 +109,19 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
72
109
|
this.server.close();
|
|
73
110
|
});
|
|
74
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
|
+
}
|
|
75
125
|
async loadMidwayController() {
|
|
76
126
|
// create room
|
|
77
127
|
const controllerModules = core_1.DecoratorManager.listModule(core_1.WS_CONTROLLER_KEY);
|
|
@@ -95,6 +145,7 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
95
145
|
// create request context
|
|
96
146
|
this.app.createAnonymousContext(socket);
|
|
97
147
|
socket.requestContext.registerObject('socket', socket);
|
|
148
|
+
socket.request = request;
|
|
98
149
|
socket.app = this.app;
|
|
99
150
|
// run connection middleware
|
|
100
151
|
const connectFn = await this.middlewareService.compose([
|
|
@@ -182,15 +233,6 @@ let MidwayWSFramework = class MidwayWSFramework extends core_1.BaseFramework {
|
|
|
182
233
|
}
|
|
183
234
|
}
|
|
184
235
|
});
|
|
185
|
-
this.app.on('error', err => {
|
|
186
|
-
this.logger.error('socket server got error', err);
|
|
187
|
-
});
|
|
188
|
-
this.app.on('close', () => {
|
|
189
|
-
if (this.heartBeatInterval) {
|
|
190
|
-
clearInterval(this.heartBeatInterval);
|
|
191
|
-
}
|
|
192
|
-
this.logger.info('socket server close');
|
|
193
|
-
});
|
|
194
236
|
}
|
|
195
237
|
async bindSocketResponse(result, socket, propertyName, methodMap) {
|
|
196
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-beta.
|
|
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,8 +23,9 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
27
|
-
"@midwayjs/
|
|
26
|
+
"@midwayjs/core": "^4.0.0-beta.2",
|
|
27
|
+
"@midwayjs/koa": "^4.0.0-beta.2",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.2",
|
|
28
29
|
"fs-extra": "11.3.0"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
@@ -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
|
}
|