thor-io.vnext 2.0.5-beta.3 → 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.
Files changed (185) hide show
  1. package/.gitattributes +17 -17
  2. package/build/index.d.ts +24 -0
  3. package/build/index.js +47 -0
  4. package/build/src/Connection/ClientInfo.d.ts +27 -0
  5. package/{src/Client → build/src/Connection}/ClientInfo.js +7 -0
  6. package/build/src/Connection/Connection.d.ts +117 -0
  7. package/build/src/Connection/Connection.js +202 -0
  8. package/build/src/Connection/IClientInfo.d.ts +20 -0
  9. package/build/src/Controller/ControllerBase.d.ts +134 -0
  10. package/build/src/Controller/ControllerBase.js +543 -0
  11. package/build/src/Controller/Subscription.d.ts +27 -0
  12. package/build/src/Controller/Subscription.js +22 -0
  13. package/build/src/Controllers/BrokerController/Broker.js +145 -0
  14. package/build/src/Controllers/BrokerController/BrokerController.d.ts +61 -0
  15. package/build/src/Controllers/BrokerController/BrokerController.js +115 -0
  16. package/{src/Controllers/BrokerController/Broker.js → build/src/Controllers/BrokerController/BrokerrController.js} +74 -85
  17. package/build/src/Controllers/BrokerController/Models/PeerConnection.d.ts +7 -0
  18. package/build/src/Controllers/BrokerController/Models/PeerConnection.js +14 -0
  19. package/build/src/Controllers/BrokerController/Models/Signal.js +11 -0
  20. package/build/src/Decorators/CanInvoke.d.ts +10 -0
  21. package/build/src/Decorators/CanInvoke.js +22 -0
  22. package/build/src/Decorators/CanSet.d.ts +10 -0
  23. package/build/src/Decorators/CanSet.js +20 -0
  24. package/build/src/Decorators/ControllerProperties.d.ts +10 -0
  25. package/build/src/Decorators/ControllerProperties.js +20 -0
  26. package/{src/Controllers/BrokerController/Models/InstantMessage.js → build/src/Interfaces/IInterceptor.js} +2 -5
  27. package/build/src/Interfaces/ITransport.d.ts +94 -0
  28. package/build/src/Interfaces/ITransportMessage.d.ts +41 -0
  29. package/build/src/Messages/BufferMessage.d.ts +38 -0
  30. package/{src → build/src}/Messages/BufferMessage.js +32 -2
  31. package/build/src/Messages/ErrorMessage.d.ts +29 -0
  32. package/build/src/Messages/ErrorMessage.js +22 -0
  33. package/build/src/Messages/PipeMessage.d.ts +46 -0
  34. package/build/src/Messages/PipeMessage.js +41 -0
  35. package/build/src/Messages/TextMessage.d.ts +88 -0
  36. package/build/src/Messages/TextMessage.js +94 -0
  37. package/build/src/Messages/WebSocketMessage.d.ts +34 -0
  38. package/build/src/Messages/WebSocketMessage.js +41 -0
  39. package/build/src/Plugin.js +32 -0
  40. package/build/src/Server/Plugin.d.ts +37 -0
  41. package/build/src/Server/Plugin.js +32 -0
  42. package/build/src/Server/ThorIOServer.d.ts +68 -0
  43. package/build/src/Server/ThorIOServer.js +131 -0
  44. package/build/src/Subscription.js +22 -0
  45. package/build/src/ThorIO.js +142 -0
  46. package/build/src/ThorIOServer.d.ts +64 -0
  47. package/build/src/ThorIOServer.js +131 -0
  48. package/build/src/Transports/BufferMessageTransport.d.ts +78 -0
  49. package/build/src/Transports/BufferMessageTransport.js +85 -0
  50. package/build/src/Transports/PipeMessageTransport.d.ts +79 -0
  51. package/build/src/Transports/PipeMessageTransport.js +90 -0
  52. package/build/src/Transports/WebSocketMessageTransport.d.ts +87 -0
  53. package/build/src/Transports/WebSocketMessageTransport.js +93 -0
  54. package/build/src/Utils/BufferUtils.d.ts +32 -0
  55. package/build/src/Utils/BufferUtils.js +57 -0
  56. package/build/src/Utils/StringUtils.d.ts +15 -0
  57. package/build/src/Utils/StringUtils.js +31 -0
  58. package/build/src/test.js +5 -0
  59. package/docs/.nojekyll +1 -0
  60. package/docs/assets/hierarchy.js +1 -0
  61. package/docs/assets/highlight.css +43 -0
  62. package/docs/assets/icons.js +18 -0
  63. package/docs/assets/icons.svg +1 -0
  64. package/docs/assets/main.js +60 -0
  65. package/docs/assets/navigation.js +1 -0
  66. package/docs/assets/search.js +1 -0
  67. package/docs/assets/style.css +1610 -0
  68. package/docs/classes/BrokerController.html +164 -0
  69. package/docs/classes/BufferMessage.html +11 -0
  70. package/docs/classes/BufferMessageTransport.html +31 -0
  71. package/docs/classes/BufferUtils.html +16 -0
  72. package/docs/classes/ClientInfo.html +11 -0
  73. package/docs/classes/Connection.html +42 -0
  74. package/docs/classes/ControllerBase.html +144 -0
  75. package/docs/classes/ErrorMessage.html +13 -0
  76. package/docs/classes/PeerConnection.html +4 -0
  77. package/docs/classes/PipeMessage.html +17 -0
  78. package/docs/classes/PipeMessageTransport.html +33 -0
  79. package/docs/classes/Plugin.html +13 -0
  80. package/docs/classes/Signal.html +5 -0
  81. package/docs/classes/StringUtils.html +9 -0
  82. package/docs/classes/Subscription.html +11 -0
  83. package/docs/classes/TextMessage.html +37 -0
  84. package/docs/classes/ThorIOServer.html +25 -0
  85. package/docs/classes/WebSocketMessage.html +18 -0
  86. package/docs/classes/WebSocketMessageTransport.html +35 -0
  87. package/docs/functions/CanInvoke.html +5 -0
  88. package/docs/functions/CanSet.html +5 -0
  89. package/docs/functions/ControllerProperties.html +5 -0
  90. package/docs/hierarchy.html +1 -0
  91. package/docs/index.html +24 -0
  92. package/docs/interfaces/ITransport.html +46 -0
  93. package/docs/interfaces/ITransportMessage.html +21 -0
  94. package/docs/modules.html +1 -0
  95. package/index.ts +32 -37
  96. package/licence +21 -21
  97. package/package.json +48 -50
  98. package/readme.md +32 -1
  99. package/src/{Client → Connection}/ClientInfo.ts +34 -43
  100. package/src/Connection/Connection.ts +229 -0
  101. package/src/Connection/IClientInfo.ts +22 -0
  102. package/src/Controller/ControllerBase.ts +561 -457
  103. package/src/{Subscription.ts → Controller/Subscription.ts} +32 -34
  104. package/src/Controllers/BrokerController/BrokerController.ts +121 -0
  105. package/src/Controllers/BrokerController/Models/PeerConnection.ts +12 -34
  106. package/src/Controllers/BrokerController/Models/Signal.ts +11 -43
  107. package/src/Decorators/CanInvoke.ts +21 -13
  108. package/src/Decorators/CanSet.ts +19 -12
  109. package/src/Decorators/ControllerProperties.ts +19 -16
  110. package/src/Interfaces/ITransport.ts +107 -23
  111. package/src/Interfaces/ITransportMessage.ts +45 -41
  112. package/src/Messages/BufferMessage.ts +70 -69
  113. package/src/Messages/ErrorMessage.ts +34 -6
  114. package/src/Messages/PipeMessage.ts +56 -62
  115. package/src/Messages/TextMessage.ts +139 -93
  116. package/src/Messages/WebSocketMessage.ts +39 -41
  117. package/src/Server/Plugin.ts +56 -0
  118. package/src/Server/ThorIOServer.ts +121 -0
  119. package/src/ThorIOServer.ts +117 -0
  120. package/src/Transports/BufferMessageTransport.ts +109 -100
  121. package/src/Transports/PipeMessageTransport.ts +112 -104
  122. package/src/Transports/WebSocketMessageTransport.ts +122 -110
  123. package/src/Utils/BufferUtils.ts +55 -61
  124. package/src/Utils/StringUtils.ts +28 -17
  125. package/tsconfig.json +109 -19
  126. package/typedoc.json +6 -0
  127. package/index.d.ts +0 -24
  128. package/index.js +0 -46
  129. package/src/Client/ClientInfo.d.ts +0 -6
  130. package/src/Connection.d.ts +0 -24
  131. package/src/Connection.js +0 -121
  132. package/src/Connection.ts +0 -263
  133. package/src/Controller/ControllerBase.d.ts +0 -43
  134. package/src/Controller/ControllerBase.js +0 -331
  135. package/src/Controllers/BrokerController/Broker.d.ts +0 -21
  136. package/src/Controllers/BrokerController/Broker.ts +0 -153
  137. package/src/Controllers/BrokerController/Models/InstantMessage.d.ts +0 -3
  138. package/src/Controllers/BrokerController/Models/InstantMessage.ts +0 -15
  139. package/src/Controllers/BrokerController/Models/PeerConnection.d.ts +0 -5
  140. package/src/Controllers/BrokerController/Models/PeerConnection.js +0 -9
  141. package/src/Controllers/BrokerController/Models/Signal.js +0 -10
  142. package/src/Decorators/CanInvoke.d.ts +0 -2
  143. package/src/Decorators/CanInvoke.js +0 -9
  144. package/src/Decorators/CanSet.d.ts +0 -1
  145. package/src/Decorators/CanSet.js +0 -8
  146. package/src/Decorators/ControllerProperties.d.ts +0 -1
  147. package/src/Decorators/ControllerProperties.js +0 -10
  148. package/src/Interfaces/IInterceptor.d.ts +0 -2
  149. package/src/Interfaces/IInterceptor.ts +0 -2
  150. package/src/Interfaces/ITransport.d.ts +0 -18
  151. package/src/Interfaces/ITransportMessage.d.ts +0 -8
  152. package/src/Listener.d.ts +0 -5
  153. package/src/Listener.js +0 -9
  154. package/src/Listener.ts +0 -34
  155. package/src/Messages/BufferMessage.d.ts +0 -10
  156. package/src/Messages/ErrorMessage.d.ts +0 -4
  157. package/src/Messages/ErrorMessage.js +0 -8
  158. package/src/Messages/PipeMessage.d.ts +0 -12
  159. package/src/Messages/PipeMessage.js +0 -20
  160. package/src/Messages/TextMessage.d.ts +0 -15
  161. package/src/Messages/TextMessage.js +0 -50
  162. package/src/Messages/WebSocketMessage.d.ts +0 -10
  163. package/src/Messages/WebSocketMessage.js +0 -15
  164. package/src/Plugin.d.ts +0 -6
  165. package/src/Plugin.js +0 -11
  166. package/src/Plugin.ts +0 -37
  167. package/src/Subscription.d.ts +0 -5
  168. package/src/Subscription.js +0 -9
  169. package/src/ThorIO.d.ts +0 -18
  170. package/src/ThorIO.js +0 -65
  171. package/src/ThorIO.ts +0 -148
  172. package/src/Transports/BufferMessageTransport.d.ts +0 -21
  173. package/src/Transports/BufferMessageTransport.js +0 -31
  174. package/src/Transports/PipeMessageTransport.d.ts +0 -20
  175. package/src/Transports/PipeMessageTransport.js +0 -33
  176. package/src/Transports/WebSocketMessageTransport.d.ts +0 -20
  177. package/src/Transports/WebSocketMessageTransport.js +0 -31
  178. package/src/Utils/BufferUtils.d.ts +0 -5
  179. package/src/Utils/BufferUtils.js +0 -29
  180. package/src/Utils/StringUtils.d.ts +0 -4
  181. package/src/Utils/StringUtils.js +0 -13
  182. /package/{src/Interfaces/IInterceptor.js → build/src/Connection/IClientInfo.js} +0 -0
  183. /package/{src → build/src}/Controllers/BrokerController/Models/Signal.d.ts +0 -0
  184. /package/{src → build/src}/Interfaces/ITransport.js +0 -0
  185. /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 * as net from 'net';
2
- import { ITransport } from '../Interfaces/ITransport';
3
- import { ITransportMessage } from '../Interfaces/ITransportMessage';
4
- import { BufferMessage } from '../Messages/BufferMessage';
5
- import { StringUtils } from '../Utils/StringUtils';
6
- import { IInterceptor } from '../Interfaces/IInterceptor';
7
- import { IncomingMessage } from 'http';
8
- /**
9
- *
10
- *
11
- * @export
12
- * @class BufferMessageTransport
13
- * @implements {ITransport}
14
- */
15
- export class BufferMessageTransport implements ITransport {
16
-
17
- /**
18
- *
19
- *
20
- * @type {string}
21
- * @memberOf BufferMessageTransport
22
- */
23
- id: string;
24
- /**
25
- *
26
- *
27
- *
28
- * @memberOf BufferMessageTransport
29
- */
30
- onMessage: (messsage: ITransportMessage) => void;
31
- /**
32
- * Creates an instance of BufferMessageTransport.
33
- *
34
- * @param {net.Socket} socket
35
- *
36
- * @memberOf BufferMessageTransport
37
- */
38
- constructor(public socket: net.Socket) {
39
- this.id = StringUtils.newGuid();
40
- this.socket.addListener("data", (buffer: Buffer) => {
41
- let bm = new BufferMessage(buffer, false);
42
- this.onMessage(bm);
43
- });
44
- }
45
- request: IncomingMessage | any;
46
- interceptors: Map<string, IInterceptor>;
47
- onClose: () => void;
48
- onOpen: () => void;
49
- /**
50
- *
51
- *
52
- * @readonly
53
- *
54
- * @memberOf BufferMessageTransport
55
- */
56
- get readyState() {
57
- return 1;
58
- }
59
- /**
60
- *
61
- *
62
- * @param {string} data
63
- *
64
- * @memberOf BufferMessageTransport
65
- */
66
- send(data: string) {
67
- let bm = new BufferMessage(new Buffer(data), false);
68
- this.socket.write(bm.toBuffer());
69
- }
70
- /**
71
- *
72
- *
73
- * @param {string} name
74
- * @param {Function} fn
75
- *
76
- * @memberOf BufferMessageTransport
77
- */
78
- addEventListener(name: string, fn: any) {
79
- this.socket.addListener(name, fn);
80
- }
81
- /**
82
- *
83
- *
84
- * @returns
85
- *
86
- * @memberOf BufferMessageTransport
87
- */
88
- ping() {
89
- return;
90
- }
91
- /**
92
- *
93
- *
94
- *
95
- * @memberOf BufferMessageTransport
96
- */
97
- close() {
98
- this.socket.destroy();
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 { ITransport } from '../Interfaces/ITransport';
2
- import { PipeMessage } from '../Messages/PipeMessage';
3
- import * as net from 'net';
4
- import { StringUtils } from '../Utils/StringUtils';
5
- import { TextMessage } from '../Messages/TextMessage';
6
- import { IInterceptor } from '../Interfaces/IInterceptor';
7
- import { IncomingMessage } from 'http';
8
- /**
9
- *
10
- *
11
- * @export
12
- * @class PipeMessageTransport
13
- * @implements {ITransport}
14
- */
15
- export class PipeMessageTransport implements ITransport {
16
-
17
- /**
18
- *
19
- *no
20
- * @type {string}
21
- * @memberOf PipeMessageTransport
22
- */
23
- id: string;
24
- /**
25
- *
26
- *
27
- *
28
- * @memberOf PipeMessageTransport
29
- */
30
- onMessage: (message: PipeMessage) => void;
31
- /**
32
- *
33
- *
34
- * @param {*} data
35
- *
36
- * @memberOf PipeMessageTransport
37
- */
38
- send(data: any) {
39
- let message = new PipeMessage(data, false);
40
- this.socket.write(message.toBuffer());
41
- }
42
- /**
43
- *
44
- *
45
- * @param {number} reason
46
- * @param {*} message
47
- *
48
- * @memberOf PipeMessageTransport
49
- */
50
- close(reason: number, message: any) {
51
- this.socket.destroy();
52
- }
53
- /**
54
- *
55
- *
56
- * @param {string} name
57
- * @param {Function} fn
58
- *
59
- * @memberOf PipeMessageTransport
60
- */
61
- addEventListener(name: string, fn: any) {
62
- this.socket.addListener(name, fn);
63
- }
64
- /**
65
- *
66
- *
67
- * @readonly
68
- * @type {number}
69
- * @memberOf PipeMessageTransport
70
- */
71
- get readyState(): number {
72
- return 1;
73
- }
74
- /**
75
- *
76
- *
77
- * @returns
78
- *
79
- * @memberOf PipeMessageTransport
80
- */
81
- ping() {
82
- return;
83
- }
84
- /**
85
- * Creates an instance of PipeMessageTransport.
86
- *s
87
- * @param {net.Socket} socket
88
- *
89
- * @memberOf PipeMessageTransport
90
- */
91
- constructor(public socket: net.Socket) {
92
- this.id = StringUtils.newGuid();
93
- socket.addListener("data", (buffer: Buffer) => {
94
- let args = buffer.toString().split("|");
95
- let message = new TextMessage(args[1], args[2], args[0]);
96
- this.onMessage(new PipeMessage(message.toString(), false));
97
- });
98
-
99
- }
100
- request: any;
101
- interceptors: Map<string, IInterceptor>;
102
- onClose: () => void;
103
- onOpen: () => void;
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
+ }