thor-io.vnext 2.0.6 → 3.1.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/.gitattributes +17 -17
- package/{index.d.ts → build/index.d.ts} +11 -8
- package/build/index.js +47 -0
- package/build/src/Connection/ClientInfo.d.ts +27 -0
- package/{src → build/src}/Connection/ClientInfo.js +7 -0
- package/build/src/Connection/Connection.d.ts +117 -0
- package/build/src/Connection/Connection.js +202 -0
- package/build/src/Connection/IClientInfo.d.ts +20 -0
- package/build/src/Controller/ControllerBase.d.ts +134 -0
- package/build/src/Controller/ControllerBase.js +543 -0
- package/build/src/Controller/Subscription.d.ts +27 -0
- package/build/src/Controller/Subscription.js +22 -0
- package/build/src/Controllers/BrokerController/Broker.js +145 -0
- package/build/src/Controllers/BrokerController/BrokerController.d.ts +61 -0
- package/build/src/Controllers/BrokerController/BrokerController.js +115 -0
- package/{src/Controllers/BrokerController/Broker.js → build/src/Controllers/BrokerController/BrokerrController.js} +74 -85
- package/build/src/Controllers/BrokerController/Models/PeerConnection.d.ts +7 -0
- package/build/src/Controllers/BrokerController/Models/PeerConnection.js +14 -0
- package/build/src/Controllers/BrokerController/Models/Signal.js +11 -0
- package/build/src/Decorators/CanInvoke.d.ts +10 -0
- package/build/src/Decorators/CanInvoke.js +22 -0
- package/build/src/Decorators/CanSet.d.ts +10 -0
- package/build/src/Decorators/CanSet.js +20 -0
- package/build/src/Decorators/ControllerProperties.d.ts +10 -0
- package/build/src/Decorators/ControllerProperties.js +20 -0
- package/build/src/Interfaces/IInterceptor.js +2 -0
- package/build/src/Interfaces/ITransport.d.ts +94 -0
- package/build/src/Interfaces/ITransportMessage.d.ts +41 -0
- package/build/src/Messages/BufferMessage.d.ts +38 -0
- package/{src → build/src}/Messages/BufferMessage.js +32 -2
- package/build/src/Messages/ErrorMessage.d.ts +29 -0
- package/build/src/Messages/ErrorMessage.js +22 -0
- package/build/src/Messages/PipeMessage.d.ts +46 -0
- package/build/src/Messages/PipeMessage.js +41 -0
- package/build/src/Messages/TextMessage.d.ts +88 -0
- package/build/src/Messages/TextMessage.js +94 -0
- package/build/src/Messages/WebSocketMessage.d.ts +34 -0
- package/build/src/Messages/WebSocketMessage.js +41 -0
- package/build/src/Plugin.js +32 -0
- package/build/src/Server/Plugin.d.ts +37 -0
- package/build/src/Server/Plugin.js +32 -0
- package/build/src/Server/ThorIOServer.d.ts +68 -0
- package/build/src/Server/ThorIOServer.js +131 -0
- package/build/src/Subscription.js +22 -0
- package/build/src/ThorIO.js +142 -0
- package/build/src/ThorIOServer.d.ts +64 -0
- package/build/src/ThorIOServer.js +131 -0
- package/build/src/Transports/BufferMessageTransport.d.ts +78 -0
- package/build/src/Transports/BufferMessageTransport.js +85 -0
- package/build/src/Transports/PipeMessageTransport.d.ts +79 -0
- package/build/src/Transports/PipeMessageTransport.js +90 -0
- package/build/src/Transports/WebSocketMessageTransport.d.ts +87 -0
- package/build/src/Transports/WebSocketMessageTransport.js +93 -0
- package/build/src/Utils/BufferUtils.d.ts +32 -0
- package/build/src/Utils/BufferUtils.js +57 -0
- package/build/src/Utils/StringUtils.d.ts +15 -0
- package/build/src/Utils/StringUtils.js +31 -0
- package/build/src/test.js +5 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +43 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1610 -0
- package/docs/classes/BrokerController.html +164 -0
- package/docs/classes/BufferMessage.html +11 -0
- package/docs/classes/BufferMessageTransport.html +31 -0
- package/docs/classes/BufferUtils.html +16 -0
- package/docs/classes/ClientInfo.html +11 -0
- package/docs/classes/Connection.html +42 -0
- package/docs/classes/ControllerBase.html +144 -0
- package/docs/classes/ErrorMessage.html +13 -0
- package/docs/classes/PeerConnection.html +4 -0
- package/docs/classes/PipeMessage.html +17 -0
- package/docs/classes/PipeMessageTransport.html +33 -0
- package/docs/classes/Plugin.html +13 -0
- package/docs/classes/Signal.html +5 -0
- package/docs/classes/StringUtils.html +9 -0
- package/docs/classes/Subscription.html +11 -0
- package/docs/classes/TextMessage.html +37 -0
- package/docs/classes/ThorIOServer.html +25 -0
- package/docs/classes/WebSocketMessage.html +18 -0
- package/docs/classes/WebSocketMessageTransport.html +35 -0
- package/docs/functions/CanInvoke.html +5 -0
- package/docs/functions/CanSet.html +5 -0
- package/docs/functions/ControllerProperties.html +5 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +24 -0
- package/docs/interfaces/ITransport.html +46 -0
- package/docs/interfaces/ITransportMessage.html +21 -0
- package/docs/modules.html +1 -0
- package/index.ts +32 -35
- package/licence +21 -21
- package/package.json +48 -49
- package/readme.md +32 -71
- package/src/Connection/ClientInfo.ts +34 -37
- package/src/Connection/Connection.ts +229 -278
- package/src/Connection/IClientInfo.ts +22 -0
- package/src/Controller/ControllerBase.ts +561 -458
- package/src/{Subscription.ts → Controller/Subscription.ts} +32 -34
- package/src/Controllers/BrokerController/BrokerController.ts +121 -0
- package/src/Controllers/BrokerController/Models/PeerConnection.ts +12 -34
- package/src/Controllers/BrokerController/Models/Signal.ts +11 -43
- package/src/Decorators/CanInvoke.ts +21 -13
- package/src/Decorators/CanSet.ts +19 -12
- package/src/Decorators/ControllerProperties.ts +19 -16
- package/src/Interfaces/ITransport.ts +107 -23
- package/src/Interfaces/ITransportMessage.ts +45 -41
- package/src/Messages/BufferMessage.ts +70 -69
- package/src/Messages/ErrorMessage.ts +34 -6
- package/src/Messages/PipeMessage.ts +56 -62
- package/src/Messages/TextMessage.ts +139 -93
- package/src/Messages/WebSocketMessage.ts +39 -41
- package/src/Server/Plugin.ts +56 -0
- package/src/Server/ThorIOServer.ts +121 -0
- package/src/ThorIOServer.ts +117 -0
- package/src/Transports/BufferMessageTransport.ts +109 -100
- package/src/Transports/PipeMessageTransport.ts +112 -104
- package/src/Transports/WebSocketMessageTransport.ts +122 -110
- package/src/Utils/BufferUtils.ts +55 -61
- package/src/Utils/StringUtils.ts +28 -17
- package/tsconfig.json +109 -18
- package/typedoc.json +6 -0
- package/index.js +0 -40
- package/src/Connection/ClientInfo.d.ts +0 -6
- package/src/Connection/Connection.d.ts +0 -24
- package/src/Connection/Connection.js +0 -129
- package/src/Controller/ControllerBase.d.ts +0 -43
- package/src/Controller/ControllerBase.js +0 -331
- package/src/Controllers/BrokerController/Broker.d.ts +0 -21
- package/src/Controllers/BrokerController/Broker.ts +0 -153
- package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
- package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
- package/src/Controllers/BrokerController/Models/Signal.js +0 -10
- package/src/Decorators/CanInvoke.d.ts +0 -2
- package/src/Decorators/CanInvoke.js +0 -9
- package/src/Decorators/CanSet.d.ts +0 -1
- package/src/Decorators/CanSet.js +0 -8
- package/src/Decorators/ControllerProperties.d.ts +0 -1
- package/src/Decorators/ControllerProperties.js +0 -10
- package/src/Interfaces/IInterceptor.d.ts +0 -2
- package/src/Interfaces/IInterceptor.ts +0 -2
- package/src/Interfaces/ITransport.d.ts +0 -18
- package/src/Interfaces/ITransportMessage.d.ts +0 -8
- package/src/Messages/BufferMessage.d.ts +0 -10
- package/src/Messages/ErrorMessage.d.ts +0 -4
- package/src/Messages/ErrorMessage.js +0 -8
- package/src/Messages/PipeMessage.d.ts +0 -12
- package/src/Messages/PipeMessage.js +0 -20
- package/src/Messages/TextMessage.d.ts +0 -15
- package/src/Messages/TextMessage.js +0 -50
- package/src/Messages/WebSocketMessage.d.ts +0 -10
- package/src/Messages/WebSocketMessage.js +0 -15
- package/src/Plugin.d.ts +0 -6
- package/src/Plugin.js +0 -11
- package/src/Plugin.ts +0 -37
- package/src/Subscription.d.ts +0 -5
- package/src/Subscription.js +0 -9
- package/src/ThorIO.d.ts +0 -19
- package/src/ThorIO.js +0 -67
- package/src/ThorIO.ts +0 -153
- package/src/Transports/BufferMessageTransport.d.ts +0 -21
- package/src/Transports/BufferMessageTransport.js +0 -31
- package/src/Transports/PipeMessageTransport.d.ts +0 -20
- package/src/Transports/PipeMessageTransport.js +0 -33
- package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
- package/src/Transports/WebSocketMessageTransport.js +0 -31
- package/src/Utils/BufferUtils.d.ts +0 -5
- package/src/Utils/BufferUtils.js +0 -29
- package/src/Utils/StringUtils.d.ts +0 -4
- package/src/Utils/StringUtils.js +0 -13
- /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
- /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
- /package/{src → build/src}/Interfaces/ITransport.js +0 -0
- /package/{src → build/src}/Interfaces/ITransportMessage.js +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import * as net from 'net';
|
|
2
|
+
|
|
3
|
+
import { Connection } from '../Connection/Connection';
|
|
4
|
+
import { ControllerBase } from '../Controller/ControllerBase';
|
|
5
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
6
|
+
import { Plugin } from '../Server/Plugin';
|
|
7
|
+
import {
|
|
8
|
+
WebSocketMessageTransport,
|
|
9
|
+
} from '../Transports/WebSocketMessageTransport';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
13
|
+
*/
|
|
14
|
+
export class ThorIOServer {
|
|
15
|
+
/**
|
|
16
|
+
* An array of plugins containing registered controllers.
|
|
17
|
+
*/
|
|
18
|
+
private controllers: Map<string, Plugin<ControllerBase>>;
|
|
19
|
+
/**
|
|
20
|
+
* A Map of connections indexed by their ID.
|
|
21
|
+
*/
|
|
22
|
+
private connections: Map<string, Connection>;
|
|
23
|
+
/**
|
|
24
|
+
* An array of active net.Server instances representing endpoints.
|
|
25
|
+
*/
|
|
26
|
+
private endpoints: Array<net.Server>;
|
|
27
|
+
/**
|
|
28
|
+
* (Optional) Interceptors for custom logic during communication.
|
|
29
|
+
*/
|
|
30
|
+
interceptors: any;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new instance of ThorIOServer.
|
|
34
|
+
*
|
|
35
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
36
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
37
|
+
*/
|
|
38
|
+
static createInstance(controllers: Array<any>): ThorIOServer {
|
|
39
|
+
return new ThorIOServer(controllers);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Constructor for ThorIOServer.
|
|
44
|
+
*
|
|
45
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
46
|
+
*/
|
|
47
|
+
constructor(controllers: Array<ControllerBase>) {
|
|
48
|
+
this.endpoints = new Array<net.Server>();
|
|
49
|
+
this.connections = new Map<string, Connection>();
|
|
50
|
+
this.controllers = new Map<string, Plugin<ControllerBase>>();
|
|
51
|
+
controllers.forEach((ctrl: ControllerBase) => {
|
|
52
|
+
if (!Reflect.hasOwnMetadata("alias", ctrl)) {
|
|
53
|
+
throw "Failed to register one of the specified Controllers, missing ControllerProperties alias attribute";
|
|
54
|
+
} else {
|
|
55
|
+
console.log(`Starting registering the '${Reflect.getMetadata("alias", ctrl)}' controller.`);
|
|
56
|
+
}
|
|
57
|
+
const plugin = new Plugin<ControllerBase>(ctrl as ControllerBase);
|
|
58
|
+
this.controllers.set(Reflect.getMetadata("alias", ctrl), plugin);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Removes a connection from the server.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} id The ID of the connection to remove.
|
|
66
|
+
* @param {number} reason The reason for removing the connection.
|
|
67
|
+
*/
|
|
68
|
+
removeConnection(id: string, reason: number): void {
|
|
69
|
+
try {
|
|
70
|
+
this.connections.delete(id);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.warn(`Failed to delete a connection with id ${id}, reason: ${reason}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
79
|
+
*
|
|
80
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
81
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
82
|
+
* @param {number} port The port number to listen on.
|
|
83
|
+
* @returns {net.Server} The created net.Server instance.
|
|
84
|
+
*/
|
|
85
|
+
addEndpoint(typeOfTransport: { new(...args: any[]): ITransport; }, host: string, port: number): net.Server {
|
|
86
|
+
const endpoint = net.createServer((socket: net.Socket) => {
|
|
87
|
+
const transport = new typeOfTransport(socket);
|
|
88
|
+
this.addConnection(transport);
|
|
89
|
+
});
|
|
90
|
+
endpoint.listen(port, host, ((listener: any) => {
|
|
91
|
+
// do op
|
|
92
|
+
}));
|
|
93
|
+
this.endpoints.push(endpoint);
|
|
94
|
+
return endpoint;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Adds a WebSocket connection to the server.
|
|
99
|
+
*
|
|
100
|
+
* @param {any} ws The WebSocket object.
|
|
101
|
+
* @param {any} req The request object (if applicable).
|
|
102
|
+
*/
|
|
103
|
+
addWebSocket(ws: any, req: any): void {
|
|
104
|
+
const transport = new WebSocketMessageTransport(ws, req);
|
|
105
|
+
this.addConnection(transport);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Adds a new connection to the server's internal state.
|
|
110
|
+
*
|
|
111
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
112
|
+
*/
|
|
113
|
+
private addConnection(transport: ITransport): void {
|
|
114
|
+
transport.addEventListener("close", (reason: number) => {
|
|
115
|
+
if (transport.onClose)
|
|
116
|
+
transport.onClose(reason);
|
|
117
|
+
this.removeConnection(transport.id, reason);
|
|
118
|
+
});
|
|
119
|
+
this.connections.set(transport.id, new Connection(transport, this.connections, this.controllers));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as net from 'net';
|
|
2
|
+
|
|
3
|
+
import { Connection } from './Connection/Connection';
|
|
4
|
+
import { ControllerBase } from './Controller/ControllerBase';
|
|
5
|
+
import { ITransport } from './Interfaces/ITransport';
|
|
6
|
+
import { Plugin } from './Server/Plugin';
|
|
7
|
+
import {
|
|
8
|
+
WebSocketMessageTransport,
|
|
9
|
+
} from './Transports/WebSocketMessageTransport';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
13
|
+
*/
|
|
14
|
+
export class ThorIOServer {
|
|
15
|
+
/**
|
|
16
|
+
* An array of plugins containing registered controllers.
|
|
17
|
+
*/
|
|
18
|
+
private controllers: Map<string, Plugin<ControllerBase>>;
|
|
19
|
+
/**
|
|
20
|
+
* A Map of connections indexed by their ID.
|
|
21
|
+
*/
|
|
22
|
+
private connections: Map<string, Connection>;
|
|
23
|
+
/**
|
|
24
|
+
* An array of active net.Server instances representing endpoints.
|
|
25
|
+
*/
|
|
26
|
+
private endpoints: Array<net.Server>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new instance of ThorIOServer.
|
|
30
|
+
*
|
|
31
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
32
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
33
|
+
*/
|
|
34
|
+
static createInstance(controllers: Array<any>): ThorIOServer {
|
|
35
|
+
return new ThorIOServer(controllers);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Constructor for ThorIOServer.
|
|
40
|
+
*
|
|
41
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
42
|
+
*/
|
|
43
|
+
constructor(controllers: Array<ControllerBase>) {
|
|
44
|
+
this.endpoints = new Array<net.Server>();
|
|
45
|
+
this.connections = new Map<string, Connection>();
|
|
46
|
+
this.controllers = new Map<string, Plugin<ControllerBase>>();
|
|
47
|
+
controllers.forEach((ctrl: ControllerBase) => {
|
|
48
|
+
if (!Reflect.hasOwnMetadata("alias", ctrl)) {
|
|
49
|
+
throw "Failed to register one of the specified Controllers, missing ControllerProperties alias attribute";
|
|
50
|
+
} else {
|
|
51
|
+
console.log(`Starting registering the '${Reflect.getMetadata("alias", ctrl)}' controller.`);
|
|
52
|
+
}
|
|
53
|
+
const plugin = new Plugin<ControllerBase>(ctrl as ControllerBase);
|
|
54
|
+
this.controllers.set(Reflect.getMetadata("alias", ctrl), plugin);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Removes a connection from the server.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} id The ID of the connection to remove.
|
|
62
|
+
* @param {number} reason The reason for removing the connection.
|
|
63
|
+
*/
|
|
64
|
+
removeConnection(id: string, reason: number): void {
|
|
65
|
+
try {
|
|
66
|
+
this.connections.delete(id);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
console.warn(`Failed to delete a connection with id ${id}, reason: ${reason}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
75
|
+
*
|
|
76
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
77
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
78
|
+
* @param {number} port The port number to listen on.
|
|
79
|
+
* @returns {net.Server} The created net.Server instance.
|
|
80
|
+
*/
|
|
81
|
+
addEndpoint(typeOfTransport: { new(...args: any[]): ITransport; }, host: string, port: number): net.Server {
|
|
82
|
+
const endpoint = net.createServer((socket: net.Socket) => {
|
|
83
|
+
const transport = new typeOfTransport(socket);
|
|
84
|
+
this.addConnection(transport);
|
|
85
|
+
});
|
|
86
|
+
endpoint.listen(port, host, ((listener: any) => {
|
|
87
|
+
// do op
|
|
88
|
+
}));
|
|
89
|
+
this.endpoints.push(endpoint);
|
|
90
|
+
return endpoint;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Adds a WebSocket connection to the server.
|
|
95
|
+
*
|
|
96
|
+
* @param {any} ws The WebSocket object.
|
|
97
|
+
* @param {any} req The request object (if applicable).
|
|
98
|
+
*/
|
|
99
|
+
addWebSocket(ws: any, req: any): void {
|
|
100
|
+
const transport = new WebSocketMessageTransport(ws, req);
|
|
101
|
+
this.addConnection(transport);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Adds a new connection to the server's internal state.
|
|
106
|
+
*
|
|
107
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
108
|
+
*/
|
|
109
|
+
private addConnection(transport: ITransport): void {
|
|
110
|
+
transport.addEventListener("close", (reason: number) => {
|
|
111
|
+
if (transport.onClose)
|
|
112
|
+
transport.onClose(reason);
|
|
113
|
+
this.removeConnection(transport.id, reason);
|
|
114
|
+
});
|
|
115
|
+
this.connections.set(transport.id, new Connection(transport, this.connections, this.controllers));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -1,100 +1,109 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* @
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @type {string}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
1
|
+
import { IncomingMessage } from 'http';
|
|
2
|
+
import * as net from 'net';
|
|
3
|
+
|
|
4
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
5
|
+
import { ITransportMessage } from '../Interfaces/ITransportMessage';
|
|
6
|
+
import { BufferMessage } from '../Messages/BufferMessage';
|
|
7
|
+
import { StringUtils } from '../Utils/StringUtils';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A transport implementation for handling buffer-based messages over a socket.
|
|
11
|
+
*
|
|
12
|
+
* @export
|
|
13
|
+
* @class BufferMessageTransport
|
|
14
|
+
* @implements {ITransport}
|
|
15
|
+
*/
|
|
16
|
+
export class BufferMessageTransport implements ITransport {
|
|
17
|
+
/**
|
|
18
|
+
* Unique identifier for this transport instance.
|
|
19
|
+
*
|
|
20
|
+
* @type {string}
|
|
21
|
+
*/
|
|
22
|
+
id: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Event handler for receiving transport messages.
|
|
26
|
+
*
|
|
27
|
+
* @type {(message: ITransportMessage) => void}
|
|
28
|
+
*/
|
|
29
|
+
onMessage: (message: ITransportMessage) => void = (message: BufferMessage) => { };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The underlying socket used for communication.
|
|
33
|
+
*
|
|
34
|
+
* @type {net.Socket}
|
|
35
|
+
*/
|
|
36
|
+
constructor(public socket: net.Socket) {
|
|
37
|
+
this.id = StringUtils.newGuid();
|
|
38
|
+
this.socket.addListener("data", (buffer: Buffer) => {
|
|
39
|
+
const bm = new BufferMessage(buffer, false);
|
|
40
|
+
this.onMessage(bm);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The incoming HTTP request associated with the transport (if any).
|
|
46
|
+
*
|
|
47
|
+
* @type {IncomingMessage | any}
|
|
48
|
+
*/
|
|
49
|
+
request: IncomingMessage | any;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Event handler for when the transport connection is closed.
|
|
54
|
+
*
|
|
55
|
+
* @type {() => void}
|
|
56
|
+
*/
|
|
57
|
+
onClose: () => void = () => { };
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Event handler for when the transport connection is opened.
|
|
61
|
+
*
|
|
62
|
+
* @type {() => void}
|
|
63
|
+
*/
|
|
64
|
+
onOpen: () => void = () => { };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The ready state of the transport connection.
|
|
68
|
+
*
|
|
69
|
+
* @readonly
|
|
70
|
+
* @type {number}
|
|
71
|
+
*/
|
|
72
|
+
get readyState() {
|
|
73
|
+
return 1; // Open
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Sends a string message over the transport.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} data - The message to send.
|
|
80
|
+
*/
|
|
81
|
+
send(data: string) {
|
|
82
|
+
const bm = new BufferMessage(Buffer.from(data), false);
|
|
83
|
+
this.socket.write(bm.toBuffer());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Adds an event listener to the underlying socket.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} name - The name of the event.
|
|
90
|
+
* @param {Function} fn - The callback function to invoke when the event occurs.
|
|
91
|
+
*/
|
|
92
|
+
addEventListener(name: string, fn: any) {
|
|
93
|
+
this.socket.addListener(name, fn);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Sends a ping message (no-op for this transport).
|
|
98
|
+
*/
|
|
99
|
+
ping() {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Closes the transport connection and destroys the socket.
|
|
105
|
+
*/
|
|
106
|
+
close() {
|
|
107
|
+
this.socket.destroy();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -1,104 +1,112 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { TextMessage } from '../Messages/TextMessage';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @export
|
|
12
|
-
* @class PipeMessageTransport
|
|
13
|
-
* @implements {ITransport}
|
|
14
|
-
*/
|
|
15
|
-
export class PipeMessageTransport implements ITransport {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
1
|
+
import * as net from 'net';
|
|
2
|
+
|
|
3
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
4
|
+
import { PipeMessage } from '../Messages/PipeMessage';
|
|
5
|
+
import { TextMessage } from '../Messages/TextMessage';
|
|
6
|
+
import { StringUtils } from '../Utils/StringUtils';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A transport implementation for handling pipe-based messages over a socket.
|
|
10
|
+
*
|
|
11
|
+
* @export
|
|
12
|
+
* @class PipeMessageTransport
|
|
13
|
+
* @implements {ITransport}
|
|
14
|
+
*/
|
|
15
|
+
export class PipeMessageTransport implements ITransport {
|
|
16
|
+
/**
|
|
17
|
+
* Unique identifier for this transport instance.
|
|
18
|
+
*
|
|
19
|
+
* @type {string}
|
|
20
|
+
*/
|
|
21
|
+
id: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Event handler for receiving transport messages.
|
|
25
|
+
*
|
|
26
|
+
* @type {(message: any) => void}
|
|
27
|
+
*/
|
|
28
|
+
onMessage: (message: any) => void = () => { };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sends data over the transport.
|
|
32
|
+
*
|
|
33
|
+
* @param {any} data - The data to send.
|
|
34
|
+
*/
|
|
35
|
+
send(data: any) {
|
|
36
|
+
const message = new PipeMessage(data, false);
|
|
37
|
+
this.socket.write(message.toBuffer());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Closes the transport connection and destroys the socket.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} reason - The reason code for closing the connection.
|
|
44
|
+
* @param {any} message - An optional message describing the closure.
|
|
45
|
+
*/
|
|
46
|
+
close(reason: number, message: any) {
|
|
47
|
+
this.socket.destroy();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Adds an event listener to the underlying socket.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} name - The name of the event.
|
|
54
|
+
* @param {Function} fn - The callback function to invoke when the event occurs.
|
|
55
|
+
*/
|
|
56
|
+
addEventListener(name: string, fn: any) {
|
|
57
|
+
this.socket.addListener(name, fn);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The ready state of the transport connection.
|
|
62
|
+
*
|
|
63
|
+
* @readonly
|
|
64
|
+
* @type {number}
|
|
65
|
+
*/
|
|
66
|
+
get readyState(): number {
|
|
67
|
+
return 1; // Open
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Sends a ping message (no-op for this transport).
|
|
72
|
+
*/
|
|
73
|
+
ping() {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Creates an instance of PipeMessageTransport.
|
|
79
|
+
*
|
|
80
|
+
* @param {net.Socket} socket - The underlying socket used for communication.
|
|
81
|
+
*/
|
|
82
|
+
constructor(public socket: net.Socket) {
|
|
83
|
+
this.id = StringUtils.newGuid();
|
|
84
|
+
socket.addListener("data", (buffer: Buffer) => {
|
|
85
|
+
const args = buffer.toString().split("|");
|
|
86
|
+
const message = new TextMessage(args[1], args[2], args[0]);
|
|
87
|
+
this.onMessage(new PipeMessage(message.toString(), false));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The incoming request associated with the transport (if any).
|
|
93
|
+
*
|
|
94
|
+
* @type {any}
|
|
95
|
+
*/
|
|
96
|
+
request: any;
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Event handler for when the transport connection is closed.
|
|
101
|
+
*
|
|
102
|
+
* @type {() => void}
|
|
103
|
+
*/
|
|
104
|
+
onClose: () => void = () => { };
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Event handler for when the transport connection is opened.
|
|
108
|
+
*
|
|
109
|
+
* @type {() => void}
|
|
110
|
+
*/
|
|
111
|
+
onOpen: () => void = () => { };
|
|
112
|
+
}
|