systemlynx 1.15.10 → 1.16.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
@@ -124,7 +124,6 @@ describe("SystemLynx Objects", () => {
124
124
  "addBeforware",
125
125
  "addAfterware",
126
126
  "server",
127
- "WebSocket",
128
127
  ])
129
128
  .that.respondsTo("startService")
130
129
  .that.respondsTo("addModule")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "systemlynx",
3
- "version": "1.15.10",
3
+ "version": "1.16.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "browser": {
@@ -2,22 +2,21 @@
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 randomPort = () => Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
9
- const createSSLServer = (app, options) => {
10
- const https = require("https");
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, customWebSocketServer) {
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
- socketPort: null,
19
+ port: null,
21
20
  useREST: false,
22
21
  useService: true,
23
22
  staticRouting: false,
@@ -25,60 +24,58 @@ module.exports = function createServerManager(customServer, customWebSocketServe
25
24
  beforeware: { $all: [] },
26
25
  afterware: { $all: [] },
27
26
  };
27
+
28
28
  const server = createServer(customServer);
29
29
  const router = createRouter(server, () => serverConfigurations);
30
- const { SocketServer, WebSocket } = createWebSocket(customWebSocketServer);
31
30
  const moduleQueue = [];
32
31
  const modules = [];
33
32
 
34
- const ServerManager = { server, WebSocket };
33
+ const ServerManager = { server };
35
34
 
36
35
  ServerManager.startService = (options) => {
37
- let {
38
- route,
39
- host = "localhost",
40
- port,
41
- socketPort = randomPort(),
42
- staticRouting,
43
- ssl,
44
- } = options;
36
+ let { route, host = "localhost", port, staticRouting, ssl } = options;
37
+
38
+ route = route.charAt(0) === "/" ? route.substr(1) : route;
39
+ route = route.charAt(route.length - 1) === "/" ? route.slice(0, -1) : route;
45
40
 
46
41
  const namespace = staticRouting ? route : shortId();
47
- SocketServer.listen(socketPort);
48
- SocketEmitter.apply(ServerManager, [namespace, WebSocket]);
49
42
 
50
- route = route.charAt(0) === "/" ? route.substr(1) : route;
51
- route =
52
- route.charAt(route.length - 1) === "/" ? route.substr(route.length - 1) : route;
53
43
  const protocol = ssl ? "https" : "http";
54
44
  const serviceUrl = `${protocol}://${host}:${port}/${route}`;
55
45
 
56
- serverConfigurations = {
57
- ...serverConfigurations,
46
+ const httpServer = ssl ? https.createServer(ssl, server) : http.createServer(server);
47
+
48
+ const WebSocket = socketIO(httpServer);
49
+
50
+ SocketEmitter.apply(ServerManager, [namespace, WebSocket]);
51
+
52
+ Object.assign(serverConfigurations, {
58
53
  ...options,
54
+ server: httpServer,
55
+ WebSocket,
59
56
  serviceUrl,
60
57
  route,
61
- socketPort,
62
- };
58
+ port,
59
+ ssl,
60
+ });
61
+ const wsProtocol = ssl ? "wss" : "ws";
62
+
63
63
  const connectionData = {
64
64
  modules,
65
65
  host,
66
66
  route: `/${route}`,
67
67
  port,
68
68
  serviceUrl,
69
- namespace: `http://${host}:${socketPort}/${namespace}`,
69
+ namespace: `${wsProtocol}://${host}:${port}/${namespace}`,
70
70
  SystemLynxService: true,
71
71
  };
72
72
 
73
73
  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
74
  res.json({ ...connectionData, modules });
77
75
  });
78
76
 
79
77
  return new Promise((resolve) => {
80
- const _server = ssl ? createSSLServer(server, ssl) : server;
81
- _server.listen(port, () => {
78
+ httpServer.listen(port, () => {
82
79
  console.log(`[SystemLynx][Service]: Listening on ${serviceUrl}\n`);
83
80
  moduleQueue.forEach(({ name, Module, reserved_methods }) =>
84
81
  ServerManager.addModule(name, Module, reserved_methods)
@@ -97,12 +94,15 @@ module.exports = function createServerManager(customServer, customWebSocketServe
97
94
  staticRouting,
98
95
  useService,
99
96
  useREST,
100
- socketPort,
97
+ port,
101
98
  beforeware,
102
99
  afterware,
100
+ WebSocket,
101
+ ssl,
103
102
  } = serverConfigurations;
104
103
 
105
104
  if (!serviceUrl) return moduleQueue.push({ name, Module, reserved_methods });
105
+
106
106
  const exclude_methods = [
107
107
  "on",
108
108
  "emit",
@@ -115,30 +115,36 @@ module.exports = function createServerManager(customServer, customWebSocketServe
115
115
  const namespace = staticRouting ? name : shortId();
116
116
 
117
117
  SocketEmitter.apply(Module, [namespace, WebSocket]);
118
+
118
119
  const before_validators = [...beforeware.$all, ...(beforeware[name] || [])];
119
120
  const after_validators = [...afterware.$all, ...(afterware[name] || [])];
120
121
 
121
122
  if (useService) {
122
123
  const path = staticRouting ? `${route}/${name}` : `${shortId()}/${shortId()}`;
124
+ const wsProtocol = ssl ? "wss" : "ws";
123
125
 
124
126
  modules.push({
125
- namespace: `http://${host}:${socketPort}/${namespace}`,
127
+ namespace: `${wsProtocol}://${host}:${port}/${namespace}`,
126
128
  route: `/${path}`,
127
129
  name,
128
130
  methods,
129
131
  });
132
+
130
133
  methods.forEach((method) => {
131
134
  const nsp = `${name}.${method.fn}`;
132
135
  const beforeValidators = [...before_validators, ...(beforeware[nsp] || [])];
133
136
  const afterValidators = [...after_validators, ...(afterware[nsp] || [])];
137
+
134
138
  router.addService(Module, path, method, name, beforeValidators, afterValidators);
135
139
  });
136
140
  }
141
+
137
142
  if (useREST)
138
143
  methods.forEach((method) => {
139
144
  const nsp = `${name}.${method.fn}`;
140
145
  const beforeValidators = [...before_validators, ...(beforeware[nsp] || [])];
141
146
  const afterValidators = [...after_validators, ...(afterware[nsp] || [])];
147
+
142
148
  switch (method.fn) {
143
149
  case "get":
144
150
  case "put":
@@ -156,7 +162,6 @@ module.exports = function createServerManager(customServer, customWebSocketServe
156
162
  });
157
163
  };
158
164
 
159
- // Shared function for adding middleware (both beforeware and afterware)
160
165
  const addMiddleware = (type, ...args) => {
161
166
  const name = typeof args[0] === "string" ? args.shift() : "$all";
162
167
  args.forEach(async (middleware) => {
@@ -168,7 +173,6 @@ module.exports = function createServerManager(customServer, customWebSocketServe
168
173
  });
169
174
  };
170
175
 
171
- // Helper function to add a single middleware item
172
176
  const addMiddlewareItem = (type, name, middleware) => {
173
177
  if (Array.isArray(middleware))
174
178
  return middleware.map((m) => addMiddlewareItem(type, name, m));
@@ -186,18 +190,8 @@ module.exports = function createServerManager(customServer, customWebSocketServe
186
190
  });
187
191
  };
188
192
 
189
- // Use the shared function for both middleware types
190
193
  ServerManager.addBeforware = (...args) => addMiddleware("beforeware", ...args);
191
194
  ServerManager.addAfterware = (...args) => addMiddleware("afterware", ...args);
195
+
192
196
  return ServerManager;
193
197
  };
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 = 4500;
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 = 5500;
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
  });