systemlynx 1.15.11 → 1.17.11
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/index.test.js
CHANGED
package/package.json
CHANGED
|
@@ -2,83 +2,81 @@
|
|
|
2
2
|
const createServer = require("./components/Server");
|
|
3
3
|
const createRouter = require("./components/Router");
|
|
4
4
|
const SocketEmitter = require("./components/SocketEmitter");
|
|
5
|
-
const createWebSocket = require("./components/WebSocketServer");
|
|
6
5
|
const parseMethods = require("./components/parseMethods");
|
|
7
6
|
const shortId = require("shortid");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
return https.createServer(options, app);
|
|
12
|
-
};
|
|
7
|
+
const http = require("http");
|
|
8
|
+
const https = require("https");
|
|
9
|
+
const socketIO = require("socket.io");
|
|
13
10
|
|
|
14
|
-
module.exports = function createServerManager(customServer
|
|
11
|
+
module.exports = function createServerManager(customServer) {
|
|
15
12
|
let serverConfigurations = {
|
|
13
|
+
server: null,
|
|
14
|
+
WebSocket: null,
|
|
16
15
|
route: null,
|
|
17
16
|
port: null,
|
|
18
17
|
host: "localhost",
|
|
19
18
|
serviceUrl: null,
|
|
20
|
-
|
|
19
|
+
port: null,
|
|
21
20
|
useREST: false,
|
|
22
21
|
useService: true,
|
|
23
22
|
staticRouting: false,
|
|
24
23
|
ssl: { key: "", cert: "" },
|
|
25
24
|
beforeware: { $all: [] },
|
|
26
25
|
afterware: { $all: [] },
|
|
26
|
+
protocol: "http",
|
|
27
27
|
};
|
|
28
|
+
|
|
28
29
|
const server = createServer(customServer);
|
|
29
30
|
const router = createRouter(server, () => serverConfigurations);
|
|
30
|
-
const { SocketServer, WebSocket } = createWebSocket(customWebSocketServer);
|
|
31
31
|
const moduleQueue = [];
|
|
32
32
|
const modules = [];
|
|
33
33
|
|
|
34
|
-
const ServerManager = { server
|
|
34
|
+
const ServerManager = { server };
|
|
35
35
|
|
|
36
36
|
ServerManager.startService = (options) => {
|
|
37
|
-
let {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
socketPort = randomPort(),
|
|
42
|
-
staticRouting,
|
|
43
|
-
ssl,
|
|
44
|
-
} = options;
|
|
37
|
+
let { route, host = "localhost", port, staticRouting, ssl, protocol } = options;
|
|
38
|
+
|
|
39
|
+
route = route.charAt(0) === "/" ? route.substr(1) : route;
|
|
40
|
+
route = route.charAt(route.length - 1) === "/" ? route.slice(0, -1) : route;
|
|
45
41
|
|
|
46
42
|
const namespace = staticRouting ? route : shortId();
|
|
47
|
-
|
|
43
|
+
if (!["http", "https"].includes(protocol)) protocol = ssl ? "https" : "http";
|
|
44
|
+
const serviceUrl = `${protocol}://${host}:${port}/${route}`;
|
|
45
|
+
|
|
46
|
+
const httpServer = ssl ? https.createServer(ssl, server) : http.createServer(server);
|
|
47
|
+
|
|
48
|
+
const WebSocket = socketIO(httpServer);
|
|
49
|
+
|
|
48
50
|
SocketEmitter.apply(ServerManager, [namespace, WebSocket]);
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
route =
|
|
52
|
-
route.charAt(route.length - 1) === "/" ? route.substr(route.length - 1) : route;
|
|
53
|
-
const protocol = ssl ? "https" : "http";
|
|
54
|
-
const serviceUrl = `${protocol}://${host}:${port}/${route}`;
|
|
52
|
+
const wsProtocol = protocol === "https" ? "wss" : "ws";
|
|
55
53
|
|
|
56
|
-
serverConfigurations = {
|
|
57
|
-
...serverConfigurations,
|
|
58
|
-
...options,
|
|
59
|
-
serviceUrl,
|
|
60
|
-
route,
|
|
61
|
-
socketPort,
|
|
62
|
-
};
|
|
63
54
|
const connectionData = {
|
|
64
55
|
modules,
|
|
65
56
|
host,
|
|
66
57
|
route: `/${route}`,
|
|
67
58
|
port,
|
|
68
59
|
serviceUrl,
|
|
69
|
-
namespace:
|
|
60
|
+
namespace: `${wsProtocol}://${host}:${port}/${namespace}`,
|
|
70
61
|
SystemLynxService: true,
|
|
71
62
|
};
|
|
72
63
|
|
|
64
|
+
Object.assign(serverConfigurations, {
|
|
65
|
+
...options,
|
|
66
|
+
server: httpServer,
|
|
67
|
+
WebSocket,
|
|
68
|
+
serviceUrl,
|
|
69
|
+
route,
|
|
70
|
+
port,
|
|
71
|
+
protocol,
|
|
72
|
+
});
|
|
73
|
+
|
|
73
74
|
server.get(`/${route}`, (req, res) => {
|
|
74
|
-
//The route will return connection data for the service including an array of
|
|
75
|
-
//modules (objects) which contain instructions on how to make request to each object
|
|
76
75
|
res.json({ ...connectionData, modules });
|
|
77
76
|
});
|
|
78
77
|
|
|
79
78
|
return new Promise((resolve) => {
|
|
80
|
-
|
|
81
|
-
_server.listen(port, () => {
|
|
79
|
+
httpServer.listen(port, () => {
|
|
82
80
|
console.log(`[SystemLynx][Service]: Listening on ${serviceUrl}\n`);
|
|
83
81
|
moduleQueue.forEach(({ name, Module, reserved_methods }) =>
|
|
84
82
|
ServerManager.addModule(name, Module, reserved_methods)
|
|
@@ -97,12 +95,15 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
97
95
|
staticRouting,
|
|
98
96
|
useService,
|
|
99
97
|
useREST,
|
|
100
|
-
|
|
98
|
+
port,
|
|
101
99
|
beforeware,
|
|
102
100
|
afterware,
|
|
101
|
+
WebSocket,
|
|
102
|
+
protocol,
|
|
103
103
|
} = serverConfigurations;
|
|
104
104
|
|
|
105
105
|
if (!serviceUrl) return moduleQueue.push({ name, Module, reserved_methods });
|
|
106
|
+
|
|
106
107
|
const exclude_methods = [
|
|
107
108
|
"on",
|
|
108
109
|
"emit",
|
|
@@ -115,30 +116,36 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
115
116
|
const namespace = staticRouting ? name : shortId();
|
|
116
117
|
|
|
117
118
|
SocketEmitter.apply(Module, [namespace, WebSocket]);
|
|
119
|
+
|
|
118
120
|
const before_validators = [...beforeware.$all, ...(beforeware[name] || [])];
|
|
119
121
|
const after_validators = [...afterware.$all, ...(afterware[name] || [])];
|
|
120
122
|
|
|
121
123
|
if (useService) {
|
|
122
124
|
const path = staticRouting ? `${route}/${name}` : `${shortId()}/${shortId()}`;
|
|
125
|
+
const wsProtocol = protocol === "https" ? "wss" : "ws";
|
|
123
126
|
|
|
124
127
|
modules.push({
|
|
125
|
-
namespace:
|
|
128
|
+
namespace: `${wsProtocol}://${host}:${port}/${namespace}`,
|
|
126
129
|
route: `/${path}`,
|
|
127
130
|
name,
|
|
128
131
|
methods,
|
|
129
132
|
});
|
|
133
|
+
|
|
130
134
|
methods.forEach((method) => {
|
|
131
135
|
const nsp = `${name}.${method.fn}`;
|
|
132
136
|
const beforeValidators = [...before_validators, ...(beforeware[nsp] || [])];
|
|
133
137
|
const afterValidators = [...after_validators, ...(afterware[nsp] || [])];
|
|
138
|
+
|
|
134
139
|
router.addService(Module, path, method, name, beforeValidators, afterValidators);
|
|
135
140
|
});
|
|
136
141
|
}
|
|
142
|
+
|
|
137
143
|
if (useREST)
|
|
138
144
|
methods.forEach((method) => {
|
|
139
145
|
const nsp = `${name}.${method.fn}`;
|
|
140
146
|
const beforeValidators = [...before_validators, ...(beforeware[nsp] || [])];
|
|
141
147
|
const afterValidators = [...after_validators, ...(afterware[nsp] || [])];
|
|
148
|
+
|
|
142
149
|
switch (method.fn) {
|
|
143
150
|
case "get":
|
|
144
151
|
case "put":
|
|
@@ -156,7 +163,6 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
156
163
|
});
|
|
157
164
|
};
|
|
158
165
|
|
|
159
|
-
// Shared function for adding middleware (both beforeware and afterware)
|
|
160
166
|
const addMiddleware = (type, ...args) => {
|
|
161
167
|
const name = typeof args[0] === "string" ? args.shift() : "$all";
|
|
162
168
|
args.forEach(async (middleware) => {
|
|
@@ -168,7 +174,6 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
168
174
|
});
|
|
169
175
|
};
|
|
170
176
|
|
|
171
|
-
// Helper function to add a single middleware item
|
|
172
177
|
const addMiddlewareItem = (type, name, middleware) => {
|
|
173
178
|
if (Array.isArray(middleware))
|
|
174
179
|
return middleware.map((m) => addMiddlewareItem(type, name, m));
|
|
@@ -186,18 +191,8 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
186
191
|
});
|
|
187
192
|
};
|
|
188
193
|
|
|
189
|
-
// Use the shared function for both middleware types
|
|
190
194
|
ServerManager.addBeforware = (...args) => addMiddleware("beforeware", ...args);
|
|
191
195
|
ServerManager.addAfterware = (...args) => addMiddleware("afterware", ...args);
|
|
196
|
+
|
|
192
197
|
return ServerManager;
|
|
193
198
|
};
|
|
194
|
-
|
|
195
|
-
//creating an ssl setup with openssl cli
|
|
196
|
-
//1. `openssl genrsa -out key.pem` to generate a private key
|
|
197
|
-
//2. create a certificate signing request using the key we just generated
|
|
198
|
-
// `openssl req -new -key key.pem -out csr.pem`
|
|
199
|
-
//3. Answer the prompts in the terminal
|
|
200
|
-
//4. use the newly generate csr to generate a ssl certificate
|
|
201
|
-
// openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out cert.pem
|
|
202
|
-
//(x509 is the standard to use for the certificate, days are the number of day the cert is valid)
|
|
203
|
-
//5. csr.pem is no longer needed
|
|
@@ -14,7 +14,6 @@ describe("createServerManager function", () => {
|
|
|
14
14
|
"addBeforware",
|
|
15
15
|
"addAfterware",
|
|
16
16
|
"server",
|
|
17
|
-
"WebSocket",
|
|
18
17
|
])
|
|
19
18
|
.that.respondsTo("startService")
|
|
20
19
|
.that.respondsTo("addModule")
|
|
@@ -89,7 +88,7 @@ describe("ServerManager", () => {
|
|
|
89
88
|
it("should be able call ServerManager.addModule method before or after calling ServerManager.startService", async () => {
|
|
90
89
|
const ServerManager = createServerManager();
|
|
91
90
|
const route = "/testService";
|
|
92
|
-
const port =
|
|
91
|
+
const port = 4600;
|
|
93
92
|
const url = `http://localhost:${port}${route}`;
|
|
94
93
|
const name = "TestModule";
|
|
95
94
|
|
|
@@ -26,7 +26,7 @@ describe("Service factory", () => {
|
|
|
26
26
|
it("should be able to use Service.startService to initiate a ServerManager instance that hosts the Service Connection Data", async () => {
|
|
27
27
|
const Service = createService();
|
|
28
28
|
const route = "/testService";
|
|
29
|
-
const port =
|
|
29
|
+
const port = 5600;
|
|
30
30
|
const url = `http://localhost:${port}${route}`;
|
|
31
31
|
|
|
32
32
|
await Service.startService({ route, port });
|
|
@@ -109,9 +109,7 @@ describe("Service.module(constructor)", () => {
|
|
|
109
109
|
]);
|
|
110
110
|
expect(results.modules[0].name, "mod");
|
|
111
111
|
expect(results.modules[0].route).to.be.a("String");
|
|
112
|
-
expect(results.modules[0].namespace).to.match(
|
|
113
|
-
new RegExp("https?://localhost:\\d+/.+")
|
|
114
|
-
);
|
|
112
|
+
expect(results.modules[0].namespace).to.match(new RegExp("wss?://localhost:\\d+/.+"));
|
|
115
113
|
expect(results.SystemLynxServerService, {
|
|
116
114
|
serviceUrl: "localhost:6542/test/service",
|
|
117
115
|
});
|
|
@@ -249,9 +247,7 @@ describe("Service.module(object)", () => {
|
|
|
249
247
|
]);
|
|
250
248
|
expect(results.modules[0].name, "mod");
|
|
251
249
|
expect(results.modules[0].route).to.be.a("String");
|
|
252
|
-
expect(results.modules[0].namespace).to.match(
|
|
253
|
-
new RegExp("https?://localhost:\\d+/.+")
|
|
254
|
-
);
|
|
250
|
+
expect(results.modules[0].namespace).to.match(new RegExp("wss?://localhost:\\d+/.+"));
|
|
255
251
|
expect(results.SystemLynxServerService, {
|
|
256
252
|
serviceUrl: "localhost:6542/test/service",
|
|
257
253
|
});
|