@tsed/socketio 8.0.0 → 8.0.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/package.json +16 -15
- package/src/SocketIOModule.spec.ts +87 -0
- package/src/SocketIOModule.ts +113 -0
- package/src/class/SocketHandlersBuilder.spec.ts +988 -0
- package/src/class/SocketHandlersBuilder.ts +266 -0
- package/src/class/SocketProviderMetadata.ts +57 -0
- package/src/class/SocketSessionData.spec.ts +190 -0
- package/src/class/SocketSessionData.ts +81 -0
- package/src/constants/constants.ts +1 -0
- package/src/decorators/args.spec.ts +57 -0
- package/src/decorators/args.ts +60 -0
- package/src/decorators/broadcast.spec.ts +23 -0
- package/src/decorators/broadcast.ts +28 -0
- package/src/decorators/broadcastOthers.spec.ts +23 -0
- package/src/decorators/broadcastOthers.ts +28 -0
- package/src/decorators/emit.spec.ts +23 -0
- package/src/decorators/emit.ts +28 -0
- package/src/decorators/input.spec.ts +22 -0
- package/src/decorators/input.ts +33 -0
- package/src/decorators/inputAndBroadcast.spec.ts +25 -0
- package/src/decorators/inputAndBroadcast.ts +28 -0
- package/src/decorators/inputAndBroadcastOthers.spec.ts +24 -0
- package/src/decorators/inputAndBroadcastOthers.ts +28 -0
- package/src/decorators/inputAndEmit.spec.ts +24 -0
- package/src/decorators/inputAndEmit.ts +28 -0
- package/src/decorators/io.ts +34 -0
- package/src/decorators/nsp.spec.ts +70 -0
- package/src/decorators/nsp.ts +101 -0
- package/src/decorators/reason.spec.ts +26 -0
- package/src/decorators/reason.ts +30 -0
- package/src/decorators/socket.spec.ts +26 -0
- package/src/decorators/socket.ts +32 -0
- package/src/decorators/socketErr.spec.ts +25 -0
- package/src/decorators/socketErr.ts +26 -0
- package/src/decorators/socketEventName.spec.ts +25 -0
- package/src/decorators/socketEventName.ts +26 -0
- package/src/decorators/socketFilter.ts +27 -0
- package/src/decorators/socketMiddleware.spec.ts +21 -0
- package/src/decorators/socketMiddleware.ts +72 -0
- package/src/decorators/socketMiddlewareError.spec.ts +22 -0
- package/src/decorators/socketMiddlewareError.ts +70 -0
- package/src/decorators/socketNsp.spec.ts +26 -0
- package/src/decorators/socketNsp.ts +32 -0
- package/src/decorators/socketReturns.ts +25 -0
- package/src/decorators/socketService.spec.ts +44 -0
- package/src/decorators/socketService.ts +24 -0
- package/src/decorators/socketSession.spec.ts +47 -0
- package/src/decorators/socketSession.ts +53 -0
- package/src/decorators/socketUseAfter.spec.ts +38 -0
- package/src/decorators/socketUseAfter.ts +69 -0
- package/src/decorators/socketUseBefore.spec.ts +34 -0
- package/src/decorators/socketUseBefore.ts +73 -0
- package/src/index.ts +41 -0
- package/src/interfaces/OnNamespaceInit.ts +8 -0
- package/src/interfaces/SocketFilters.ts +14 -0
- package/src/interfaces/SocketHandlerMetadata.ts +17 -0
- package/src/interfaces/SocketInjectableNsp.ts +4 -0
- package/src/interfaces/SocketParamMetadata.ts +9 -0
- package/src/interfaces/SocketProviderTypes.ts +7 -0
- package/src/interfaces/SocketReturnsTypes.ts +8 -0
- package/src/interfaces/interfaces.ts +9 -0
- package/src/services/SocketIOServer.spec.ts +33 -0
- package/src/services/SocketIOServer.ts +15 -0
- package/src/services/SocketIOService.spec.ts +54 -0
- package/src/services/SocketIOService.ts +69 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/socketio",
|
|
3
3
|
"description": "Socket.io package for Ts.ED framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.0.
|
|
5
|
+
"version": "8.0.2",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tsed",
|
|
8
8
|
"socket.io",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"typings": "./lib/types/index.d.ts",
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
|
+
"@tsed/source": "./src/index.ts",
|
|
19
20
|
"types": "./lib/types/index.d.ts",
|
|
20
21
|
"import": "./lib/esm/index.js",
|
|
21
22
|
"default": "./lib/esm/index.js"
|
|
@@ -33,16 +34,16 @@
|
|
|
33
34
|
"tslib": "2.7.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@tsed/barrels": "8.0.
|
|
37
|
-
"@tsed/core": "8.0.
|
|
38
|
-
"@tsed/di": "8.0.
|
|
39
|
-
"@tsed/json-mapper": "8.0.
|
|
40
|
-
"@tsed/openspec": "8.0.
|
|
41
|
-
"@tsed/platform-http": "8.0.
|
|
42
|
-
"@tsed/platform-middlewares": "8.0.
|
|
43
|
-
"@tsed/schema": "8.0.
|
|
44
|
-
"@tsed/socketio-testing": "8.0.
|
|
45
|
-
"@tsed/typescript": "8.0.
|
|
37
|
+
"@tsed/barrels": "8.0.2",
|
|
38
|
+
"@tsed/core": "8.0.2",
|
|
39
|
+
"@tsed/di": "8.0.2",
|
|
40
|
+
"@tsed/json-mapper": "8.0.2",
|
|
41
|
+
"@tsed/openspec": "8.0.2",
|
|
42
|
+
"@tsed/platform-http": "8.0.2",
|
|
43
|
+
"@tsed/platform-middlewares": "8.0.2",
|
|
44
|
+
"@tsed/schema": "8.0.2",
|
|
45
|
+
"@tsed/socketio-testing": "8.0.2",
|
|
46
|
+
"@tsed/typescript": "8.0.2",
|
|
46
47
|
"eslint": "9.12.0",
|
|
47
48
|
"express": "^4.21.0",
|
|
48
49
|
"socket.io": "4.8.0",
|
|
@@ -51,11 +52,11 @@
|
|
|
51
52
|
"vitest": "2.1.2"
|
|
52
53
|
},
|
|
53
54
|
"peerDependencies": {
|
|
54
|
-
"@tsed/di": "8.0.
|
|
55
|
-
"@tsed/json-mapper": "8.0.
|
|
55
|
+
"@tsed/di": "8.0.2",
|
|
56
|
+
"@tsed/json-mapper": "8.0.2",
|
|
56
57
|
"@tsed/logger": ">=6.7.5",
|
|
57
|
-
"@tsed/platform-middlewares": "8.0.
|
|
58
|
-
"@tsed/schema": "8.0.
|
|
58
|
+
"@tsed/platform-middlewares": "8.0.2",
|
|
59
|
+
"@tsed/schema": "8.0.2",
|
|
59
60
|
"socket.io": ">=4.0.0"
|
|
60
61
|
},
|
|
61
62
|
"peerDependenciesMeta": {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {PlatformConfiguration} from "@tsed/platform-http";
|
|
2
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
3
|
+
import Http from "http";
|
|
4
|
+
import Https from "https";
|
|
5
|
+
|
|
6
|
+
import {SocketIOModule, SocketIOServer, SocketIOService} from "./index.js";
|
|
7
|
+
|
|
8
|
+
async function createModuleFixture() {
|
|
9
|
+
const serverSettingsService = PlatformTest.get<PlatformConfiguration>(PlatformConfiguration);
|
|
10
|
+
// GIVEN
|
|
11
|
+
const socketIOServer = {attach: vi.fn(), adapter: vi.fn()};
|
|
12
|
+
const httpServer = {type: "http", get: vi.fn().mockReturnValue("httpServer")};
|
|
13
|
+
const httpsServer = {type: "https", get: vi.fn().mockReturnValue("httpsServer")};
|
|
14
|
+
const socketIOService = {addSocketProvider: vi.fn()};
|
|
15
|
+
const socketIOModule = await PlatformTest.invoke<SocketIOModule>(SocketIOModule, [
|
|
16
|
+
{token: Http.Server, use: httpServer},
|
|
17
|
+
{token: Https.Server, use: httpsServer},
|
|
18
|
+
{token: SocketIOServer, use: socketIOServer},
|
|
19
|
+
{token: SocketIOService, use: socketIOService}
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
vi.spyOn(socketIOModule as any, "getWebsocketServices").mockReturnValue([{provider: "provider"}]);
|
|
23
|
+
vi.spyOn(socketIOModule as any, "printSocketEvents").mockReturnValue(undefined);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
socketIOModule,
|
|
27
|
+
serverSettingsService,
|
|
28
|
+
httpServer,
|
|
29
|
+
httpsServer,
|
|
30
|
+
socketIOService,
|
|
31
|
+
socketIOServer
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe("SocketIOModule", () => {
|
|
36
|
+
describe("$afterListen()", () => {
|
|
37
|
+
describe("with http server", () => {
|
|
38
|
+
beforeAll(() =>
|
|
39
|
+
PlatformTest.create({
|
|
40
|
+
httpsPort: 8081,
|
|
41
|
+
socketIO: {config: "config", adapter: vi.fn()} as never
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
afterAll(() => {
|
|
45
|
+
PlatformTest.reset();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should call attach method", async () => {
|
|
49
|
+
const {socketIOModule, httpServer, httpsServer, socketIOService, socketIOServer} = await createModuleFixture();
|
|
50
|
+
|
|
51
|
+
// WHEN
|
|
52
|
+
await socketIOModule.$afterListen();
|
|
53
|
+
|
|
54
|
+
expect(socketIOServer.attach).toHaveBeenCalledTimes(2);
|
|
55
|
+
|
|
56
|
+
expect((socketIOModule as any).getWebsocketServices).toHaveBeenCalledWith();
|
|
57
|
+
expect(socketIOServer.adapter).toHaveBeenCalled();
|
|
58
|
+
expect(socketIOService.addSocketProvider).toHaveBeenCalledWith({provider: "provider"});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe("with https server", () => {
|
|
62
|
+
beforeAll(() =>
|
|
63
|
+
PlatformTest.create({
|
|
64
|
+
httpsPort: 8081,
|
|
65
|
+
http: false,
|
|
66
|
+
socketIO: {config: "config"} as never
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
afterAll(() => {
|
|
71
|
+
PlatformTest.reset();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should call attach method", async () => {
|
|
75
|
+
const {socketIOModule, httpsServer, serverSettingsService, socketIOService, socketIOServer} = await createModuleFixture();
|
|
76
|
+
|
|
77
|
+
// WHEN
|
|
78
|
+
await socketIOModule.$afterListen();
|
|
79
|
+
|
|
80
|
+
expect(socketIOServer.attach).toHaveBeenCalled();
|
|
81
|
+
|
|
82
|
+
expect((socketIOModule as any).getWebsocketServices).toHaveBeenCalledWith();
|
|
83
|
+
expect(socketIOService.addSocketProvider).toHaveBeenCalledWith({provider: "provider"});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {nameOf} from "@tsed/core";
|
|
2
|
+
import {Constant, Inject, InjectorService, Module, OnDestroy, Provider} from "@tsed/di";
|
|
3
|
+
import {$log} from "@tsed/logger";
|
|
4
|
+
import type {AfterListen} from "@tsed/platform-http";
|
|
5
|
+
import Http from "http";
|
|
6
|
+
import Https from "https";
|
|
7
|
+
import {Server, ServerOptions} from "socket.io";
|
|
8
|
+
|
|
9
|
+
import {SocketProviderMetadata} from "./class/SocketProviderMetadata.js"; // tslint:disable-line: no-unused-variable
|
|
10
|
+
import {PROVIDER_TYPE_SOCKET_SERVICE} from "./constants/constants.js";
|
|
11
|
+
import {IO} from "./decorators/io.js";
|
|
12
|
+
import {SocketIOService} from "./services/SocketIOService.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @ignore
|
|
16
|
+
*/
|
|
17
|
+
@Module()
|
|
18
|
+
export class SocketIOModule implements AfterListen, OnDestroy {
|
|
19
|
+
@Constant("logger.disableRoutesSummary", false)
|
|
20
|
+
protected disableRoutesSummary: boolean;
|
|
21
|
+
|
|
22
|
+
@Constant("socketIO", {})
|
|
23
|
+
protected settings: Partial<ServerOptions>;
|
|
24
|
+
|
|
25
|
+
@Inject()
|
|
26
|
+
protected injector: InjectorService;
|
|
27
|
+
|
|
28
|
+
@Inject(Http.Server)
|
|
29
|
+
protected httpServer: Http.Server | null;
|
|
30
|
+
|
|
31
|
+
@Inject(Https.Server)
|
|
32
|
+
protected httpsServer: Https.Server | null;
|
|
33
|
+
|
|
34
|
+
@IO()
|
|
35
|
+
private io: Server;
|
|
36
|
+
|
|
37
|
+
@Inject()
|
|
38
|
+
private socketIOService: SocketIOService;
|
|
39
|
+
|
|
40
|
+
$afterListen() {
|
|
41
|
+
if (this.httpServer) {
|
|
42
|
+
this.io.attach(this.httpServer, {...this.settings});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (this.httpsServer) {
|
|
46
|
+
this.io.attach(this.httpsServer, {...this.settings});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (this.settings.adapter) {
|
|
50
|
+
this.io.adapter(this.settings.adapter);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.getWebsocketServices().forEach((provider) => this.socketIOService.addSocketProvider(provider));
|
|
54
|
+
|
|
55
|
+
if (!this.disableRoutesSummary) {
|
|
56
|
+
this.printSocketEvents();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return this.injector.emit("$afterSocketListen");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
$onDestroy() {
|
|
63
|
+
return this.io?.close && this.io?.engine && new Promise((resolve) => this.io.close(() => resolve(undefined)));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @returns {Provider<any>[]}
|
|
69
|
+
*/
|
|
70
|
+
protected getWebsocketServices(): Provider<any>[] {
|
|
71
|
+
return Array.from(this.injector.getProviders(PROVIDER_TYPE_SOCKET_SERVICE));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
protected printSocketEvents() {
|
|
78
|
+
const list = this.getWebsocketServices().reduce((acc: any[], provider) => {
|
|
79
|
+
const socketProvider = new SocketProviderMetadata(provider.store.get("socketIO"));
|
|
80
|
+
|
|
81
|
+
if (socketProvider.namespace) {
|
|
82
|
+
socketProvider.getHandlers().forEach((handler) => {
|
|
83
|
+
acc.push({
|
|
84
|
+
namespace: socketProvider.namespace,
|
|
85
|
+
inputEvent: handler.eventName,
|
|
86
|
+
outputEvent: (handler.returns && handler.returns.eventName) || handler.eventName || "",
|
|
87
|
+
outputType: (handler.returns && handler.returns.type) || "",
|
|
88
|
+
name: `${nameOf(provider.useClass)}.${handler.methodClassName}`
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return acc;
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
this.injector.logger.info("Socket events mounted:");
|
|
97
|
+
|
|
98
|
+
const str = $log.drawTable(list, {
|
|
99
|
+
padding: 1,
|
|
100
|
+
header: {
|
|
101
|
+
namespace: "Namespace",
|
|
102
|
+
inputEvent: "Input event",
|
|
103
|
+
outputEvent: "Output event",
|
|
104
|
+
outputType: "Output type",
|
|
105
|
+
name: "Class method"
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
this.injector.logger.info("\n" + str.trim());
|
|
110
|
+
|
|
111
|
+
this.injector.logger.info("Socket server started...");
|
|
112
|
+
}
|
|
113
|
+
}
|