systemlynx 1.5.0 → 1.7.0

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.
@@ -1,7 +1,7 @@
1
1
  const isObject = (value) =>
2
2
  typeof value === "object" ? (!value ? false : !Array.isArray(value)) : false;
3
3
  const isEmpty = (obj) => Object.getOwnPropertyNames(obj).length === 0;
4
- const isPromise = (p) => typeof p === "object" && typeof p.then === "function";
4
+ const isPromise = (p) => typeof p === "object" && typeof (p || {}).then === "function";
5
5
 
6
6
  module.exports = function SystemLynxRouter(server, config) {
7
7
  const addService = (Module, route, { fn, method }, module_name) => {
@@ -34,28 +34,28 @@ module.exports = function SystemLynxRouter(server, config) {
34
34
  const { query, file, files, body, fn, Module, module_name, method } = req;
35
35
  const { serviceUrl } = config();
36
36
  const presets = { serviceUrl, module_name, fn };
37
+ const unhandledMessage = `[SystemLynx]: handled error While calling ${module_name}.${fn}(...)`;
37
38
 
38
39
  const sendError = (error) => {
39
- const status = error.status || 500;
40
- const message = error.message || "Unexpected error";
41
- const unhandledMessage = status === 500 ? "Unhandled error" : "Error";
40
+ const status = (error || {}).status || 500;
41
+ const message = (error || {}).message || unhandledMessage;
42
42
  res.status(status).json({
43
43
  ...presets,
44
- error,
44
+ ...error,
45
45
  status,
46
- message: `[SystemLynx][error]: ${unhandledMessage} While calling ${module_name}.${fn}(...): ${message}`,
46
+ message,
47
47
  SystemLynxServiceError: true,
48
48
  });
49
49
  };
50
50
 
51
51
  const sendResponse = (returnValue) => {
52
- const status = returnValue.status || 200;
52
+ const status = (returnValue || {}).status || 200;
53
53
  if (status < 400) {
54
54
  res.status(status).json({
55
55
  ...presets,
56
56
  status,
57
57
  message:
58
- returnValue.message ||
58
+ (returnValue || {}).message ||
59
59
  `[SystemLynx][response]: ${module_name}.${fn}(...) returned successfully`,
60
60
  returnValue,
61
61
  });
@@ -6,14 +6,15 @@ module.exports = function SocketEmitter(namespace, WebSocket) {
6
6
  (this || {}).on && (this || {}).emit ? this : SystemLynxDispatcher.apply(this);
7
7
 
8
8
  const socket = WebSocket.of(`/${namespace}`);
9
- const emit = Emitter.emit;
9
+ //use $emit to emit events locally only
10
+ Emitter.$emit = Emitter.emit;
10
11
 
11
12
  Emitter.emit = (name, data) => {
12
13
  const id = shortid();
13
14
  const type = "WebSocket";
14
15
  socket.emit("dispatch", { id, name, data, type });
15
16
  //emit the same event locally
16
- emit(name, data);
17
+ Emitter.$emit(name, data);
17
18
  };
18
19
  return Emitter;
19
20
  };
@@ -1,27 +1,34 @@
1
1
  "use strict";
2
- const ServerManagerFactory = require("../ServerManager/ServerManager");
3
- const Dispatcher = require("../Dispatcher/Dispatcher");
2
+ const SystemLynxServerManager = require("../ServerManager/ServerManager");
3
+ const SystemLynxDispatcher = require("../Dispatcher/Dispatcher");
4
4
 
5
- module.exports = function ServiceFactory({ defaultModule = {} } = {}) {
6
- const ServerManager = ServerManagerFactory();
5
+ module.exports = function SystemLynxService(systemContext = {}) {
6
+ const ServerManager = SystemLynxServerManager();
7
7
  const { startService, Server, WebSocket } = ServerManager;
8
- const Service = { startService, Server, WebSocket, defaultModule };
8
+ const Service = { startService, Server, WebSocket };
9
9
 
10
10
  Service.module = function (name, constructor, reserved_methods = []) {
11
+ const exclude_methods = reserved_methods.concat(
12
+ Object.getOwnPropertyNames(systemContext)
13
+ );
14
+
11
15
  if (typeof constructor === "object" && constructor instanceof Object) {
12
- ServerManager.addModule(name, constructor, reserved_methods);
13
- return constructor;
16
+ const Module = SystemLynxDispatcher.apply({ ...constructor, ...systemContext }, [
17
+ undefined,
18
+ systemContext,
19
+ ]);
20
+ ServerManager.addModule(name, Module, exclude_methods);
21
+ return Module;
14
22
  }
15
23
 
16
24
  if (typeof constructor === "function") {
17
25
  if (constructor.constructor.name === "AsyncFunction")
18
26
  throw `[SystemLynx][Module][Error]: Module(name, constructor) function cannot receive an async function as the constructor`;
19
27
 
20
- const Module = Dispatcher.apply({ ...Service.defaultModule });
21
- const exclude_methods = [
22
- ...reserved_methods,
23
- ...Object.getOwnPropertyNames(Module),
24
- ];
28
+ const Module = SystemLynxDispatcher.apply(systemContext, [
29
+ undefined,
30
+ systemContext,
31
+ ]);
25
32
  constructor.apply(Module, [ServerManager.Server(), ServerManager.WebSocket()]);
26
33
  ServerManager.addModule(name, Module, exclude_methods);
27
34
  return Module;
@@ -1,13 +1,13 @@
1
1
  const { expect } = require("chai");
2
2
  const request = require("request");
3
- const ServiceFactory = require("./Service");
3
+ const SystemLynxService = require("./Service");
4
4
 
5
5
  describe("SystemLynxService", () => {
6
6
  it("should return a new instance of a Service", () => {
7
- const Service = ServiceFactory();
7
+ const Service = SystemLynxService();
8
8
  expect(Service)
9
9
  .to.be.an("object")
10
- .that.has.all.keys("startService", "module", "Server", "WebSocket", "defaultModule")
10
+ .that.has.all.keys("startService", "module", "Server", "WebSocket")
11
11
  .that.respondsTo("startService")
12
12
  .that.respondsTo("module")
13
13
  .that.respondsTo("Server")
@@ -17,7 +17,7 @@ describe("SystemLynxService", () => {
17
17
 
18
18
  describe("Service factory", () => {
19
19
  it("should be able to use Service.startService to initiate a ServerManager instance that hosts the Service Connection Data", async () => {
20
- const Service = ServiceFactory();
20
+ const Service = SystemLynxService();
21
21
  const route = "/testService";
22
22
  const port = 5500;
23
23
  const url = `http://localhost:${port}${route}`;
@@ -49,7 +49,7 @@ describe("Service factory", () => {
49
49
  });
50
50
 
51
51
  describe("Service.module(constructor)", () => {
52
- const Service = ServiceFactory();
52
+ const Service = SystemLynxService();
53
53
  const port = 6542;
54
54
  const route = "test/service";
55
55
  const url = `http://localhost:${port}/${route}`;
@@ -111,7 +111,7 @@ describe("Service.module(constructor)", () => {
111
111
  });
112
112
 
113
113
  describe("Service.module(object)", () => {
114
- const Service = ServiceFactory();
114
+ const Service = SystemLynxService();
115
115
  const port = 6543;
116
116
  const route = "test/service2";
117
117
  const url = `http://localhost:${port}/${route}`;
@@ -123,9 +123,11 @@ describe("Service.module(object)", () => {
123
123
 
124
124
  expect(mod)
125
125
  .to.be.an("Object")
126
- .that.has.all.keys("action1", "action2")
126
+ .that.has.all.keys("action1", "action2", "on", "emit")
127
127
  .that.respondsTo("action1")
128
- .that.respondsTo("action2");
128
+ .that.respondsTo("action2")
129
+ .that.respondsTo("on")
130
+ .that.respondsTo("emit");
129
131
  });
130
132
  it("should 'Serve' Service connection data created using an object as the constructor", async () => {
131
133
  await Service.startService({ route, port });
@@ -0,0 +1,7 @@
1
+ module.exports = function () {
2
+ const System = this || {};
3
+ System.services = [];
4
+ System.modules = [];
5
+ System.configurations = {};
6
+ System.routing = null;
7
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ module.exports = function SystemObject(system) {
3
+ const context = this || {};
4
+ context.useModule = (modName) =>
5
+ (system.modules.find((mod) => mod.name === modName) || {}).module || {};
6
+ context.useService = (serviceName) =>
7
+ (system.services.find((mod) => mod.name === serviceName) || {}).client || {};
8
+ context.useConfig = () => system.configurations.module || {};
9
+ return context;
10
+ };
@@ -1,9 +0,0 @@
1
- "use strict";
2
- module.exports = function SystemObject(system) {
3
- const App = this || {};
4
- App.useModule = modName => (system.Modules.find(mod => mod.name === modName) || {}).module || {};
5
- App.useService = serviceName =>
6
- (system.Services.find(mod => mod.name === serviceName) || {}).client || {};
7
- App.useConfig = () => system.configurations.module || {};
8
- return App;
9
- };