thor-io.vnext 2.0.6 → 3.1.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/.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,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Plugin = void 0;
|
|
4
|
+
const StringUtils_1 = require("../Utils/StringUtils");
|
|
5
|
+
/**
|
|
6
|
+
* Represents a plugin with a specific type.
|
|
7
|
+
* @template T The type of the plugin instance.
|
|
8
|
+
*/
|
|
9
|
+
class Plugin {
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of Plugin.
|
|
12
|
+
* @param {T} object The plugin instance.
|
|
13
|
+
*/
|
|
14
|
+
constructor(object) {
|
|
15
|
+
this.id = StringUtils_1.StringUtils.newGuid();
|
|
16
|
+
this.alias = Reflect.getMetadata("alias", object);
|
|
17
|
+
this.instance = object;
|
|
18
|
+
const metaData = Reflect.getMetadataKeys(object);
|
|
19
|
+
metaData.forEach(metaDataKey => {
|
|
20
|
+
console.info(`Controller settings ${metaDataKey} = `, Reflect.getOwnMetadata(metaDataKey, object));
|
|
21
|
+
});
|
|
22
|
+
console.info(`Created the plugin with an id of ${this.id}`);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets the instance of the plugin.
|
|
26
|
+
* @returns {T} The plugin instance.
|
|
27
|
+
*/
|
|
28
|
+
getInstance() {
|
|
29
|
+
return this.instance;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Plugin = Plugin;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as net from 'net';
|
|
2
|
+
import { ControllerBase } from '../Controller/ControllerBase';
|
|
3
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
4
|
+
/**
|
|
5
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ThorIOServer {
|
|
8
|
+
/**
|
|
9
|
+
* An array of plugins containing registered controllers.
|
|
10
|
+
*/
|
|
11
|
+
private controllers;
|
|
12
|
+
/**
|
|
13
|
+
* A Map of connections indexed by their ID.
|
|
14
|
+
*/
|
|
15
|
+
private connections;
|
|
16
|
+
/**
|
|
17
|
+
* An array of active net.Server instances representing endpoints.
|
|
18
|
+
*/
|
|
19
|
+
private endpoints;
|
|
20
|
+
/**
|
|
21
|
+
* (Optional) Interceptors for custom logic during communication.
|
|
22
|
+
*/
|
|
23
|
+
interceptors: any;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new instance of ThorIOServer.
|
|
26
|
+
*
|
|
27
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
28
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
29
|
+
*/
|
|
30
|
+
static createInstance(controllers: Array<any>): ThorIOServer;
|
|
31
|
+
/**
|
|
32
|
+
* Constructor for ThorIOServer.
|
|
33
|
+
*
|
|
34
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
35
|
+
*/
|
|
36
|
+
constructor(controllers: Array<ControllerBase>);
|
|
37
|
+
/**
|
|
38
|
+
* Removes a connection from the server.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} id The ID of the connection to remove.
|
|
41
|
+
* @param {number} reason The reason for removing the connection.
|
|
42
|
+
*/
|
|
43
|
+
removeConnection(id: string, reason: number): void;
|
|
44
|
+
/**
|
|
45
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
46
|
+
*
|
|
47
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
48
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
49
|
+
* @param {number} port The port number to listen on.
|
|
50
|
+
* @returns {net.Server} The created net.Server instance.
|
|
51
|
+
*/
|
|
52
|
+
addEndpoint(typeOfTransport: {
|
|
53
|
+
new (...args: any[]): ITransport;
|
|
54
|
+
}, host: string, port: number): net.Server;
|
|
55
|
+
/**
|
|
56
|
+
* Adds a WebSocket connection to the server.
|
|
57
|
+
*
|
|
58
|
+
* @param {any} ws The WebSocket object.
|
|
59
|
+
* @param {any} req The request object (if applicable).
|
|
60
|
+
*/
|
|
61
|
+
addWebSocket(ws: any, req: any): void;
|
|
62
|
+
/**
|
|
63
|
+
* Adds a new connection to the server's internal state.
|
|
64
|
+
*
|
|
65
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
66
|
+
*/
|
|
67
|
+
private addConnection;
|
|
68
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.ThorIOServer = void 0;
|
|
37
|
+
const net = __importStar(require("net"));
|
|
38
|
+
const Connection_1 = require("../Connection/Connection");
|
|
39
|
+
const Plugin_1 = require("../Server/Plugin");
|
|
40
|
+
const WebSocketMessageTransport_1 = require("../Transports/WebSocketMessageTransport");
|
|
41
|
+
/**
|
|
42
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
43
|
+
*/
|
|
44
|
+
class ThorIOServer {
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new instance of ThorIOServer.
|
|
47
|
+
*
|
|
48
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
49
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
50
|
+
*/
|
|
51
|
+
static createInstance(controllers) {
|
|
52
|
+
return new ThorIOServer(controllers);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Constructor for ThorIOServer.
|
|
56
|
+
*
|
|
57
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
58
|
+
*/
|
|
59
|
+
constructor(controllers) {
|
|
60
|
+
this.endpoints = new Array();
|
|
61
|
+
this.connections = new Map();
|
|
62
|
+
this.controllers = new Map();
|
|
63
|
+
controllers.forEach((ctrl) => {
|
|
64
|
+
if (!Reflect.hasOwnMetadata("alias", ctrl)) {
|
|
65
|
+
throw "Failed to register one of the specified Controllers, missing ControllerProperties alias attribute";
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
console.log(`Starting registering the '${Reflect.getMetadata("alias", ctrl)}' controller.`);
|
|
69
|
+
}
|
|
70
|
+
const plugin = new Plugin_1.Plugin(ctrl);
|
|
71
|
+
this.controllers.set(Reflect.getMetadata("alias", ctrl), plugin);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Removes a connection from the server.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} id The ID of the connection to remove.
|
|
78
|
+
* @param {number} reason The reason for removing the connection.
|
|
79
|
+
*/
|
|
80
|
+
removeConnection(id, reason) {
|
|
81
|
+
try {
|
|
82
|
+
this.connections.delete(id);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.warn(`Failed to delete a connection with id ${id}, reason: ${reason}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
90
|
+
*
|
|
91
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
92
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
93
|
+
* @param {number} port The port number to listen on.
|
|
94
|
+
* @returns {net.Server} The created net.Server instance.
|
|
95
|
+
*/
|
|
96
|
+
addEndpoint(typeOfTransport, host, port) {
|
|
97
|
+
const endpoint = net.createServer((socket) => {
|
|
98
|
+
const transport = new typeOfTransport(socket);
|
|
99
|
+
this.addConnection(transport);
|
|
100
|
+
});
|
|
101
|
+
endpoint.listen(port, host, ((listener) => {
|
|
102
|
+
// do op
|
|
103
|
+
}));
|
|
104
|
+
this.endpoints.push(endpoint);
|
|
105
|
+
return endpoint;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Adds a WebSocket connection to the server.
|
|
109
|
+
*
|
|
110
|
+
* @param {any} ws The WebSocket object.
|
|
111
|
+
* @param {any} req The request object (if applicable).
|
|
112
|
+
*/
|
|
113
|
+
addWebSocket(ws, req) {
|
|
114
|
+
const transport = new WebSocketMessageTransport_1.WebSocketMessageTransport(ws, req);
|
|
115
|
+
this.addConnection(transport);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds a new connection to the server's internal state.
|
|
119
|
+
*
|
|
120
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
121
|
+
*/
|
|
122
|
+
addConnection(transport) {
|
|
123
|
+
transport.addEventListener("close", (reason) => {
|
|
124
|
+
if (transport.onClose)
|
|
125
|
+
transport.onClose(reason);
|
|
126
|
+
this.removeConnection(transport.id, reason);
|
|
127
|
+
});
|
|
128
|
+
this.connections.set(transport.id, new Connection_1.Connection(transport, this.connections, this.controllers));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.ThorIOServer = ThorIOServer;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Subscription = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Represents a subscription to a specific topic with an associated controller.
|
|
6
|
+
*
|
|
7
|
+
* @export
|
|
8
|
+
* @class Subscription
|
|
9
|
+
*/
|
|
10
|
+
class Subscription {
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of Subscription.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} topic - The topic of the subscription.
|
|
15
|
+
* @param {string} controller - The controller associated with the subscription.
|
|
16
|
+
*/
|
|
17
|
+
constructor(topic, controller) {
|
|
18
|
+
this.topic = topic;
|
|
19
|
+
this.controller = controller;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Subscription = Subscription;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.ThorIO = void 0;
|
|
23
|
+
const net = __importStar(require("net"));
|
|
24
|
+
const Connection_1 = require("./Connection/Connection");
|
|
25
|
+
const Plugin_1 = require("./Plugin");
|
|
26
|
+
const WebSocketMessageTransport_1 = require("./Transports/WebSocketMessageTransport");
|
|
27
|
+
class MockTransport {
|
|
28
|
+
constructor() {
|
|
29
|
+
this.readyState = 0;
|
|
30
|
+
this.id = 'mock-transport-id';
|
|
31
|
+
this.onMessage = () => { };
|
|
32
|
+
this.onClose = () => { };
|
|
33
|
+
this.onOpen = () => { };
|
|
34
|
+
this.interceptors = new Map();
|
|
35
|
+
}
|
|
36
|
+
addEventListener(topic, fn) {
|
|
37
|
+
throw new Error('Method not implemented.');
|
|
38
|
+
}
|
|
39
|
+
ping() {
|
|
40
|
+
throw new Error('Method not implemented.');
|
|
41
|
+
}
|
|
42
|
+
// Implement other methods of ITransport interface as needed
|
|
43
|
+
send(message) { }
|
|
44
|
+
close(code, reason) { }
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* ThorIO server class for managing connections and controllers.
|
|
48
|
+
*/
|
|
49
|
+
class ThorIO {
|
|
50
|
+
/**
|
|
51
|
+
* Constructor for ThorIO.
|
|
52
|
+
*
|
|
53
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
54
|
+
*/
|
|
55
|
+
constructor(controllers) {
|
|
56
|
+
this.endpoints = new Array();
|
|
57
|
+
this.connections = new Map();
|
|
58
|
+
this.controllers = new Array();
|
|
59
|
+
controllers.forEach((ctrl) => {
|
|
60
|
+
if (!Reflect.hasOwnMetadata("alias", ctrl)) {
|
|
61
|
+
throw "Faild to register on of the specified Controller's";
|
|
62
|
+
}
|
|
63
|
+
let plugin = new Plugin_1.Plugin(ctrl);
|
|
64
|
+
this.controllers.push(plugin);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new instance of ThorIO.
|
|
69
|
+
*
|
|
70
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
71
|
+
* @returns {ThorIO} A new ThorIO instance.
|
|
72
|
+
*/
|
|
73
|
+
static createInstance(controllers) {
|
|
74
|
+
return new ThorIO(controllers);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates sealed (standalone) instances of controllers marked with the "seald" metadata.
|
|
78
|
+
*/
|
|
79
|
+
createSealdControllers() {
|
|
80
|
+
this.controllers.forEach((controller) => {
|
|
81
|
+
if (Reflect.getMetadata("seald", controller.instance)) {
|
|
82
|
+
new controller.instance(new Connection_1.Connection(new MockTransport(), this.connections, this.controllers));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Removes a connection from the server.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} id The ID of the connection to remove.
|
|
90
|
+
* @param {number} reason The reason for removing the connection.
|
|
91
|
+
*/
|
|
92
|
+
removeConnection(id, reason) {
|
|
93
|
+
try {
|
|
94
|
+
this.connections.delete(id);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
101
|
+
*
|
|
102
|
+
* @param {typeof ITransport} typeOfTransport A constructor function for the transport type.
|
|
103
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
104
|
+
* @param {number} port The port number to listen on.
|
|
105
|
+
* @returns {net.Server} The created net.Server instance.
|
|
106
|
+
*/
|
|
107
|
+
addEndpoint(typeOfTransport, host, port) {
|
|
108
|
+
let endpoint = net.createServer((socket) => {
|
|
109
|
+
let transport = new typeOfTransport(socket);
|
|
110
|
+
this.addConnection(transport);
|
|
111
|
+
});
|
|
112
|
+
endpoint.listen(port, host, ((listener) => {
|
|
113
|
+
// do op
|
|
114
|
+
}));
|
|
115
|
+
this.endpoints.push(endpoint);
|
|
116
|
+
return endpoint;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Adds a WebSocket connection to the server.
|
|
120
|
+
*
|
|
121
|
+
* @param {any} ws The WebSocket object.
|
|
122
|
+
* @param {any} req The request object (if applicable).
|
|
123
|
+
*/
|
|
124
|
+
addWebSocket(ws, req) {
|
|
125
|
+
let transport = new WebSocketMessageTransport_1.WebSocketMessageTransport(ws, req);
|
|
126
|
+
this.addConnection(transport);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Adds a new connection to the server's internal state.
|
|
130
|
+
*
|
|
131
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
132
|
+
*/
|
|
133
|
+
addConnection(transport) {
|
|
134
|
+
transport.addEventListener("close", (reason) => {
|
|
135
|
+
if (transport.onClose)
|
|
136
|
+
transport.onClose(reason);
|
|
137
|
+
this.removeConnection(transport.id, reason);
|
|
138
|
+
});
|
|
139
|
+
this.connections.set(transport.id, new Connection_1.Connection(transport, this.connections, this.controllers));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.ThorIO = ThorIO;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as net from 'net';
|
|
2
|
+
import { ControllerBase } from './Controller/ControllerBase';
|
|
3
|
+
import { ITransport } from './Interfaces/ITransport';
|
|
4
|
+
/**
|
|
5
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ThorIOServer {
|
|
8
|
+
/**
|
|
9
|
+
* An array of plugins containing registered controllers.
|
|
10
|
+
*/
|
|
11
|
+
private controllers;
|
|
12
|
+
/**
|
|
13
|
+
* A Map of connections indexed by their ID.
|
|
14
|
+
*/
|
|
15
|
+
private connections;
|
|
16
|
+
/**
|
|
17
|
+
* An array of active net.Server instances representing endpoints.
|
|
18
|
+
*/
|
|
19
|
+
private endpoints;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new instance of ThorIOServer.
|
|
22
|
+
*
|
|
23
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
24
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
25
|
+
*/
|
|
26
|
+
static createInstance(controllers: Array<any>): ThorIOServer;
|
|
27
|
+
/**
|
|
28
|
+
* Constructor for ThorIOServer.
|
|
29
|
+
*
|
|
30
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
31
|
+
*/
|
|
32
|
+
constructor(controllers: Array<ControllerBase>);
|
|
33
|
+
/**
|
|
34
|
+
* Removes a connection from the server.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} id The ID of the connection to remove.
|
|
37
|
+
* @param {number} reason The reason for removing the connection.
|
|
38
|
+
*/
|
|
39
|
+
removeConnection(id: string, reason: number): void;
|
|
40
|
+
/**
|
|
41
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
42
|
+
*
|
|
43
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
44
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
45
|
+
* @param {number} port The port number to listen on.
|
|
46
|
+
* @returns {net.Server} The created net.Server instance.
|
|
47
|
+
*/
|
|
48
|
+
addEndpoint(typeOfTransport: {
|
|
49
|
+
new (...args: any[]): ITransport;
|
|
50
|
+
}, host: string, port: number): net.Server;
|
|
51
|
+
/**
|
|
52
|
+
* Adds a WebSocket connection to the server.
|
|
53
|
+
*
|
|
54
|
+
* @param {any} ws The WebSocket object.
|
|
55
|
+
* @param {any} req The request object (if applicable).
|
|
56
|
+
*/
|
|
57
|
+
addWebSocket(ws: any, req: any): void;
|
|
58
|
+
/**
|
|
59
|
+
* Adds a new connection to the server's internal state.
|
|
60
|
+
*
|
|
61
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
62
|
+
*/
|
|
63
|
+
private addConnection;
|
|
64
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.ThorIOServer = void 0;
|
|
37
|
+
const net = __importStar(require("net"));
|
|
38
|
+
const Connection_1 = require("./Connection/Connection");
|
|
39
|
+
const Plugin_1 = require("./Server/Plugin");
|
|
40
|
+
const WebSocketMessageTransport_1 = require("./Transports/WebSocketMessageTransport");
|
|
41
|
+
/**
|
|
42
|
+
* ThorIOServer server class for managing connections and controllers.
|
|
43
|
+
*/
|
|
44
|
+
class ThorIOServer {
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new instance of ThorIOServer.
|
|
47
|
+
*
|
|
48
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
49
|
+
* @returns {ThorIOServer} A new ThorIOServer instance.
|
|
50
|
+
*/
|
|
51
|
+
static createInstance(controllers) {
|
|
52
|
+
return new ThorIOServer(controllers);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Constructor for ThorIOServer.
|
|
56
|
+
*
|
|
57
|
+
* @param {Array<ControllerBase>} controllers An array of controllers to register.
|
|
58
|
+
*/
|
|
59
|
+
constructor(controllers) {
|
|
60
|
+
this.endpoints = new Array();
|
|
61
|
+
this.connections = new Map();
|
|
62
|
+
this.controllers = new Map();
|
|
63
|
+
controllers.forEach((ctrl) => {
|
|
64
|
+
if (!Reflect.hasOwnMetadata("alias", ctrl)) {
|
|
65
|
+
throw "Failed to register one of the specified Controllers, missing ControllerProperties alias attribute";
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
console.log(`Starting registering the '${Reflect.getMetadata("alias", ctrl)}' controller.`);
|
|
69
|
+
}
|
|
70
|
+
const plugin = new Plugin_1.Plugin(ctrl);
|
|
71
|
+
this.controllers.set(Reflect.getMetadata("alias", ctrl), plugin);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Removes a connection from the server.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} id The ID of the connection to remove.
|
|
78
|
+
* @param {number} reason The reason for removing the connection.
|
|
79
|
+
*/
|
|
80
|
+
removeConnection(id, reason) {
|
|
81
|
+
try {
|
|
82
|
+
this.connections.delete(id);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
console.warn(`Failed to delete a connection with id ${id}, reason: ${reason}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Adds a new endpoint to the server using the specified transport type, host, and port.
|
|
90
|
+
*
|
|
91
|
+
* @param {new (...args: any[]) => ITransport} typeOfTransport A constructor function for the transport type.
|
|
92
|
+
* @param {string} host The hostname or IP address to listen on.
|
|
93
|
+
* @param {number} port The port number to listen on.
|
|
94
|
+
* @returns {net.Server} The created net.Server instance.
|
|
95
|
+
*/
|
|
96
|
+
addEndpoint(typeOfTransport, host, port) {
|
|
97
|
+
const endpoint = net.createServer((socket) => {
|
|
98
|
+
const transport = new typeOfTransport(socket);
|
|
99
|
+
this.addConnection(transport);
|
|
100
|
+
});
|
|
101
|
+
endpoint.listen(port, host, ((listener) => {
|
|
102
|
+
// do op
|
|
103
|
+
}));
|
|
104
|
+
this.endpoints.push(endpoint);
|
|
105
|
+
return endpoint;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Adds a WebSocket connection to the server.
|
|
109
|
+
*
|
|
110
|
+
* @param {any} ws The WebSocket object.
|
|
111
|
+
* @param {any} req The request object (if applicable).
|
|
112
|
+
*/
|
|
113
|
+
addWebSocket(ws, req) {
|
|
114
|
+
const transport = new WebSocketMessageTransport_1.WebSocketMessageTransport(ws, req);
|
|
115
|
+
this.addConnection(transport);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Adds a new connection to the server's internal state.
|
|
119
|
+
*
|
|
120
|
+
* @param {ITransport} transport The transport object representing the connection.
|
|
121
|
+
*/
|
|
122
|
+
addConnection(transport) {
|
|
123
|
+
transport.addEventListener("close", (reason) => {
|
|
124
|
+
if (transport.onClose)
|
|
125
|
+
transport.onClose(reason);
|
|
126
|
+
this.removeConnection(transport.id, reason);
|
|
127
|
+
});
|
|
128
|
+
this.connections.set(transport.id, new Connection_1.Connection(transport, this.connections, this.controllers));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.ThorIOServer = ThorIOServer;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { IncomingMessage } from 'http';
|
|
2
|
+
import * as net from 'net';
|
|
3
|
+
import { ITransport } from '../Interfaces/ITransport';
|
|
4
|
+
import { ITransportMessage } from '../Interfaces/ITransportMessage';
|
|
5
|
+
/**
|
|
6
|
+
* A transport implementation for handling buffer-based messages over a socket.
|
|
7
|
+
*
|
|
8
|
+
* @export
|
|
9
|
+
* @class BufferMessageTransport
|
|
10
|
+
* @implements {ITransport}
|
|
11
|
+
*/
|
|
12
|
+
export declare class BufferMessageTransport implements ITransport {
|
|
13
|
+
socket: net.Socket;
|
|
14
|
+
/**
|
|
15
|
+
* Unique identifier for this transport instance.
|
|
16
|
+
*
|
|
17
|
+
* @type {string}
|
|
18
|
+
*/
|
|
19
|
+
id: string;
|
|
20
|
+
/**
|
|
21
|
+
* Event handler for receiving transport messages.
|
|
22
|
+
*
|
|
23
|
+
* @type {(message: ITransportMessage) => void}
|
|
24
|
+
*/
|
|
25
|
+
onMessage: (message: ITransportMessage) => void;
|
|
26
|
+
/**
|
|
27
|
+
* The underlying socket used for communication.
|
|
28
|
+
*
|
|
29
|
+
* @type {net.Socket}
|
|
30
|
+
*/
|
|
31
|
+
constructor(socket: net.Socket);
|
|
32
|
+
/**
|
|
33
|
+
* The incoming HTTP request associated with the transport (if any).
|
|
34
|
+
*
|
|
35
|
+
* @type {IncomingMessage | any}
|
|
36
|
+
*/
|
|
37
|
+
request: IncomingMessage | any;
|
|
38
|
+
/**
|
|
39
|
+
* Event handler for when the transport connection is closed.
|
|
40
|
+
*
|
|
41
|
+
* @type {() => void}
|
|
42
|
+
*/
|
|
43
|
+
onClose: () => void;
|
|
44
|
+
/**
|
|
45
|
+
* Event handler for when the transport connection is opened.
|
|
46
|
+
*
|
|
47
|
+
* @type {() => void}
|
|
48
|
+
*/
|
|
49
|
+
onOpen: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* The ready state of the transport connection.
|
|
52
|
+
*
|
|
53
|
+
* @readonly
|
|
54
|
+
* @type {number}
|
|
55
|
+
*/
|
|
56
|
+
get readyState(): number;
|
|
57
|
+
/**
|
|
58
|
+
* Sends a string message over the transport.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} data - The message to send.
|
|
61
|
+
*/
|
|
62
|
+
send(data: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Adds an event listener to the underlying socket.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} name - The name of the event.
|
|
67
|
+
* @param {Function} fn - The callback function to invoke when the event occurs.
|
|
68
|
+
*/
|
|
69
|
+
addEventListener(name: string, fn: any): void;
|
|
70
|
+
/**
|
|
71
|
+
* Sends a ping message (no-op for this transport).
|
|
72
|
+
*/
|
|
73
|
+
ping(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Closes the transport connection and destroys the socket.
|
|
76
|
+
*/
|
|
77
|
+
close(): void;
|
|
78
|
+
}
|