systemlynx 1.9.5 → 1.10.5
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/package.json +1 -1
- package/systemlynx/App/tests/App.test.js +25 -5
- package/systemlynx/Client/Client.js +12 -9
- package/systemlynx/Client/components/ClientModule.js +4 -3
- package/systemlynx/Client/components/HeaderSetter.js +9 -0
- package/systemlynx/Client/components/ServiceRequestHandler.js +7 -4
- package/systemlynx/Client/tests/Client.test.js +40 -1
- package/systemlynx/HttpClient/HttpClient.js +7 -6
package/package.json
CHANGED
|
@@ -63,13 +63,17 @@ describe("App: Loading Services", () => {
|
|
|
63
63
|
"$clearEvent",
|
|
64
64
|
"resetConnection",
|
|
65
65
|
"disconnect",
|
|
66
|
+
"headers",
|
|
67
|
+
"setHeaders",
|
|
66
68
|
"mod"
|
|
67
69
|
)
|
|
68
70
|
.that.respondsTo("emit")
|
|
69
71
|
.that.respondsTo("on")
|
|
70
72
|
.that.respondsTo("$clearEvent")
|
|
71
73
|
.that.respondsTo("resetConnection")
|
|
72
|
-
.that.respondsTo("disconnect")
|
|
74
|
+
.that.respondsTo("disconnect")
|
|
75
|
+
.that.respondsTo("headers")
|
|
76
|
+
.that.respondsTo("setHeaders");
|
|
73
77
|
resolve();
|
|
74
78
|
}
|
|
75
79
|
);
|
|
@@ -100,13 +104,17 @@ describe("App: Loading Services", () => {
|
|
|
100
104
|
"$clearEvent",
|
|
101
105
|
"resetConnection",
|
|
102
106
|
"disconnect",
|
|
107
|
+
"headers",
|
|
108
|
+
"setHeaders",
|
|
103
109
|
"mod"
|
|
104
110
|
)
|
|
105
111
|
.that.respondsTo("emit")
|
|
106
112
|
.that.respondsTo("$clearEvent")
|
|
107
113
|
.that.respondsTo("on")
|
|
108
114
|
.that.respondsTo("resetConnection")
|
|
109
|
-
.that.respondsTo("disconnect")
|
|
115
|
+
.that.respondsTo("disconnect")
|
|
116
|
+
.that.respondsTo("headers")
|
|
117
|
+
.that.respondsTo("setHeaders");
|
|
110
118
|
resolve();
|
|
111
119
|
});
|
|
112
120
|
});
|
|
@@ -136,12 +144,16 @@ describe("App: Loading Services", () => {
|
|
|
136
144
|
"$clearEvent",
|
|
137
145
|
"resetConnection",
|
|
138
146
|
"disconnect",
|
|
147
|
+
"headers",
|
|
148
|
+
"setHeaders",
|
|
139
149
|
"mod"
|
|
140
150
|
)
|
|
141
151
|
.that.respondsTo("emit")
|
|
142
152
|
.that.respondsTo("$clearEvent")
|
|
143
153
|
.that.respondsTo("on")
|
|
144
|
-
.that.respondsTo("resetConnection")
|
|
154
|
+
.that.respondsTo("resetConnection")
|
|
155
|
+
.that.respondsTo("headers")
|
|
156
|
+
.that.respondsTo("setHeaders");
|
|
145
157
|
})
|
|
146
158
|
.on("service_loaded:test", (test) => {
|
|
147
159
|
expect(test)
|
|
@@ -152,12 +164,16 @@ describe("App: Loading Services", () => {
|
|
|
152
164
|
"$clearEvent",
|
|
153
165
|
"resetConnection",
|
|
154
166
|
"disconnect",
|
|
167
|
+
"headers",
|
|
168
|
+
"setHeaders",
|
|
155
169
|
"mod"
|
|
156
170
|
)
|
|
157
171
|
.that.respondsTo("emit")
|
|
158
172
|
.that.respondsTo("$clearEvent")
|
|
159
173
|
.that.respondsTo("on")
|
|
160
|
-
.that.respondsTo("resetConnection")
|
|
174
|
+
.that.respondsTo("resetConnection")
|
|
175
|
+
.that.respondsTo("headers")
|
|
176
|
+
.that.respondsTo("setHeaders");
|
|
161
177
|
resolve();
|
|
162
178
|
});
|
|
163
179
|
});
|
|
@@ -189,12 +205,16 @@ describe("App: Loading Services", () => {
|
|
|
189
205
|
"$clearEvent",
|
|
190
206
|
"resetConnection",
|
|
191
207
|
"disconnect",
|
|
208
|
+
"headers",
|
|
209
|
+
"setHeaders",
|
|
192
210
|
"mod"
|
|
193
211
|
)
|
|
194
212
|
.that.respondsTo("emit")
|
|
195
213
|
.that.respondsTo("$clearEvent")
|
|
196
214
|
.that.respondsTo("on")
|
|
197
|
-
.that.respondsTo("resetConnection")
|
|
215
|
+
.that.respondsTo("resetConnection")
|
|
216
|
+
.that.respondsTo("headers")
|
|
217
|
+
.that.respondsTo("setHeaders");
|
|
198
218
|
resolve();
|
|
199
219
|
})
|
|
200
220
|
.on("ready", resolve);
|
|
@@ -1,32 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const loadConnectionData = require("./components/loadConnectionData");
|
|
3
3
|
const SocketDispatcher = require("./components/SocketDispatcher");
|
|
4
|
+
const HeaderSetter = require("./components/HeaderSetter");
|
|
4
5
|
const ClientModule = require("./components/ClientModule");
|
|
5
6
|
const HttpClient = require("../HttpClient/HttpClient");
|
|
6
7
|
|
|
7
8
|
module.exports = function createClient(httpClient = HttpClient(), systemContext) {
|
|
8
9
|
const Client = {};
|
|
9
|
-
Client.
|
|
10
|
+
Client.cachedServices = {};
|
|
10
11
|
|
|
11
12
|
Client.loadService = async (url, options = {}) => {
|
|
12
|
-
if (Client.
|
|
13
|
-
return Client.
|
|
13
|
+
if (Client.cachedServices[url] && !options.forceReload)
|
|
14
|
+
return Client.cachedServices[url];
|
|
14
15
|
|
|
15
16
|
const connData = await loadConnectionData(httpClient, url, options);
|
|
16
17
|
const Service = Client.createService(connData);
|
|
17
|
-
Client.
|
|
18
|
+
Client.cachedServices[url] = Service;
|
|
18
19
|
await new Promise((resolve) => Service.on("connect", resolve));
|
|
19
20
|
return Service;
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
Client.createService = (connData) => {
|
|
23
24
|
const events = {};
|
|
24
|
-
if (Client.loadedServices[connData.serviceUrl])
|
|
25
|
-
return Client.loadedServices[connData.serviceUrl];
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
if (Client.cachedServices[connData.serviceUrl])
|
|
27
|
+
return Client.cachedServices[connData.serviceUrl];
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
const Service = {};
|
|
30
|
+
SocketDispatcher.apply(Service, [connData.namespace, events, systemContext]);
|
|
31
|
+
HeaderSetter.apply(Service);
|
|
32
|
+
Client.cachedServices[connData.serviceUrl] = Service;
|
|
30
33
|
|
|
31
34
|
Service.resetConnection = async (cb) => {
|
|
32
35
|
const { modules, host, port, namespace } = await loadConnectionData(
|
|
@@ -52,7 +55,7 @@ module.exports = function createClient(httpClient = HttpClient(), systemContext)
|
|
|
52
55
|
httpClient,
|
|
53
56
|
mod,
|
|
54
57
|
connData,
|
|
55
|
-
Service
|
|
58
|
+
Service,
|
|
56
59
|
systemContext
|
|
57
60
|
))
|
|
58
61
|
);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const headerSetter = require("./HeaderSetter");
|
|
2
3
|
const ServiceRequestHandler = require("./ServiceRequestHandler");
|
|
3
4
|
const SocketDispatcher = require("./SocketDispatcher");
|
|
4
5
|
const getProtocol = (url) => url.match(/^(\w+):\/\//)[0];
|
|
@@ -7,11 +8,11 @@ module.exports = function SystemLynxClientModule(
|
|
|
7
8
|
httpClient,
|
|
8
9
|
{ methods, namespace, route, connectionData, name },
|
|
9
10
|
{ port, host, serviceUrl },
|
|
10
|
-
|
|
11
|
+
Service,
|
|
11
12
|
systemContext
|
|
12
13
|
) {
|
|
13
14
|
const events = {};
|
|
14
|
-
const ClientModule = {};
|
|
15
|
+
const ClientModule = headerSetter.apply({});
|
|
15
16
|
|
|
16
17
|
ClientModule.__setConnection = (host, port, route, namespace) => {
|
|
17
18
|
ClientModule.__connectionData = () => ({ route, host, port });
|
|
@@ -35,7 +36,7 @@ module.exports = function SystemLynxClientModule(
|
|
|
35
36
|
protocol,
|
|
36
37
|
method,
|
|
37
38
|
fn,
|
|
38
|
-
|
|
39
|
+
Service,
|
|
39
40
|
connectionData && reconnectModule,
|
|
40
41
|
]);
|
|
41
42
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const isObject = (value) =>
|
|
3
3
|
typeof value === "object" ? (!value ? false : !Array.isArray(value)) : false;
|
|
4
|
-
|
|
4
|
+
const isEmpty = (obj) => Object.getOwnPropertyNames(obj).length === 0;
|
|
5
5
|
const makeQuery = (data) =>
|
|
6
6
|
Object.getOwnPropertyNames(data).reduce(
|
|
7
7
|
(query, name) => (query += `${name}=${data[name]}&`),
|
|
@@ -12,7 +12,7 @@ module.exports = function ServiceRequestHandler(
|
|
|
12
12
|
protocol,
|
|
13
13
|
method,
|
|
14
14
|
fn,
|
|
15
|
-
|
|
15
|
+
Service,
|
|
16
16
|
reconnectModule
|
|
17
17
|
) {
|
|
18
18
|
const ClientModule = this;
|
|
@@ -26,7 +26,8 @@ module.exports = function ServiceRequestHandler(
|
|
|
26
26
|
const defaultURL = `${protocol}${host}:${port}${route}/${fn}`;
|
|
27
27
|
const { file, files } = __arguments[0] || {};
|
|
28
28
|
const url = file ? singleFileURL : files ? multiFileURL : defaultURL;
|
|
29
|
-
|
|
29
|
+
const defaultHeaders = ClientModule.headers();
|
|
30
|
+
const headers = !isEmpty(defaultHeaders) ? defaultHeaders : Service.headers();
|
|
30
31
|
if (url === defaultURL)
|
|
31
32
|
httpClient
|
|
32
33
|
.request({
|
|
@@ -37,6 +38,7 @@ module.exports = function ServiceRequestHandler(
|
|
|
37
38
|
}`,
|
|
38
39
|
method,
|
|
39
40
|
body: { __arguments },
|
|
41
|
+
headers,
|
|
40
42
|
})
|
|
41
43
|
.then((results) => cb(null, results))
|
|
42
44
|
.catch((err) => ErrorHandler(err, errCount, cb));
|
|
@@ -46,6 +48,7 @@ module.exports = function ServiceRequestHandler(
|
|
|
46
48
|
url,
|
|
47
49
|
method,
|
|
48
50
|
formData: { ...__arguments[0], __arguments },
|
|
51
|
+
headers,
|
|
49
52
|
})
|
|
50
53
|
.then((results) => cb(null, results))
|
|
51
54
|
.catch((err) => ErrorHandler(err, errCount, cb));
|
|
@@ -58,7 +61,7 @@ module.exports = function ServiceRequestHandler(
|
|
|
58
61
|
console.log(err);
|
|
59
62
|
errCount++;
|
|
60
63
|
if (reconnectModule) reconnectModule(() => tryRequest(cb, errCount));
|
|
61
|
-
else reconnectService(() => tryRequest(cb, errCount));
|
|
64
|
+
else Service.reconnectService(() => tryRequest(cb, errCount));
|
|
62
65
|
} else console.error(Error(`[SystemLynx][Service][Error]: Invalid route:${err}`));
|
|
63
66
|
};
|
|
64
67
|
|
|
@@ -47,13 +47,17 @@ describe("Client", () => {
|
|
|
47
47
|
"$clearEvent",
|
|
48
48
|
"resetConnection",
|
|
49
49
|
"disconnect",
|
|
50
|
+
"headers",
|
|
51
|
+
"setHeaders",
|
|
50
52
|
"orders"
|
|
51
53
|
)
|
|
52
54
|
.that.respondsTo("emit")
|
|
53
55
|
.that.respondsTo("$clearEvent")
|
|
54
56
|
.that.respondsTo("on")
|
|
55
57
|
.that.respondsTo("resetConnection")
|
|
56
|
-
.that.respondsTo("disconnect")
|
|
58
|
+
.that.respondsTo("disconnect")
|
|
59
|
+
.that.respondsTo("headers")
|
|
60
|
+
.that.respondsTo("setHeaders");
|
|
57
61
|
|
|
58
62
|
expect(buAPI.orders)
|
|
59
63
|
.to.be.an("object")
|
|
@@ -62,6 +66,8 @@ describe("Client", () => {
|
|
|
62
66
|
"on",
|
|
63
67
|
"$clearEvent",
|
|
64
68
|
"disconnect",
|
|
69
|
+
"headers",
|
|
70
|
+
"setHeaders",
|
|
65
71
|
"__setConnection",
|
|
66
72
|
"__connectionData",
|
|
67
73
|
"action1",
|
|
@@ -73,6 +79,8 @@ describe("Client", () => {
|
|
|
73
79
|
.that.respondsTo("$clearEvent")
|
|
74
80
|
.that.respondsTo("on")
|
|
75
81
|
.that.respondsTo("emit")
|
|
82
|
+
.that.respondsTo("headers")
|
|
83
|
+
.that.respondsTo("setHeaders")
|
|
76
84
|
.that.respondsTo("$clearEvent")
|
|
77
85
|
.that.respondsTo("__setConnection")
|
|
78
86
|
.that.respondsTo("__connectionData")
|
|
@@ -219,4 +227,35 @@ describe("Service", () => {
|
|
|
219
227
|
const results3 = await AsyncMath.round(10.2);
|
|
220
228
|
expect(results3).to.equal(10);
|
|
221
229
|
});
|
|
230
|
+
it("should maintain service and module level headers on a Client instance", async () => {
|
|
231
|
+
const service = createService();
|
|
232
|
+
const route = "setHeaders/test";
|
|
233
|
+
const port = 4999;
|
|
234
|
+
const host = "localhost";
|
|
235
|
+
const url = `http://localhost:${port}/${route}`;
|
|
236
|
+
service.module("Test", function () {
|
|
237
|
+
this.getHeaders = function () {
|
|
238
|
+
return this.req.headers.origin;
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
service.module("Test2", function () {
|
|
242
|
+
this.getHeaders = function () {
|
|
243
|
+
return this.req.headers.origin;
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
await service.startService({ route, port, host });
|
|
248
|
+
|
|
249
|
+
const Client = createClient();
|
|
250
|
+
const myService = await Client.loadService(url);
|
|
251
|
+
myService.setHeaders({ Origin: `http://localhost:${port}` });
|
|
252
|
+
myService.Test.setHeaders({ Origin: `http://localhost:${port + 1}` });
|
|
253
|
+
|
|
254
|
+
//because a module level headers were set for Test then I expect what was set
|
|
255
|
+
// for The Test2 module I expect the Service level header to be applied
|
|
256
|
+
const results = await myService.Test.getHeaders();
|
|
257
|
+
expect(results).to.equal(`http://localhost:${port + 1}`);
|
|
258
|
+
const results2 = await myService.Test2.getHeaders();
|
|
259
|
+
expect(results2).to.equal(`http://localhost:${port}`);
|
|
260
|
+
});
|
|
222
261
|
});
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const
|
|
2
|
+
const request = require("request");
|
|
3
3
|
const json = true;
|
|
4
4
|
|
|
5
|
-
module.exports = function
|
|
5
|
+
module.exports = function createHttpClient() {
|
|
6
6
|
const Client = this || {};
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
Client.request = ({ method, url, body, headers }, cb) => {
|
|
8
9
|
const tryRequest = (callback) => {
|
|
9
|
-
|
|
10
|
+
request({ method, url, body, json, headers }, (err, res, body) => {
|
|
10
11
|
if (err) callback(err);
|
|
11
12
|
else if (res.statusCode >= 400) callback(body);
|
|
12
13
|
else callback(null, body, res);
|
|
@@ -22,9 +23,9 @@ module.exports = function createClient() {
|
|
|
22
23
|
);
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
Client.upload = ({ url, formData }, cb) => {
|
|
26
|
+
Client.upload = ({ url, formData, headers }, cb) => {
|
|
26
27
|
const tryRequest = (callback) => {
|
|
27
|
-
|
|
28
|
+
request.post({ url, formData, json, headers }, (err, res, body) => {
|
|
28
29
|
if (err) callback(err);
|
|
29
30
|
else if (res.statusCode >= 400) callback(body);
|
|
30
31
|
else callback(null, body, res);
|