@autofleet/node-common 1.1.51 → 1.1.53
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/network/index.js +7 -11
- package/package.json +1 -1
- package/settings/index.js +16 -1
- package/settings/index.test.js +17 -0
package/network/index.js
CHANGED
|
@@ -28,17 +28,13 @@ const defaultSettings = {
|
|
|
28
28
|
paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
const getHttpRequestObject =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
body: jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : response.config.data,
|
|
39
|
-
requestMethod: response.config.method.toUpperCase(),
|
|
40
|
-
};
|
|
41
|
-
};
|
|
31
|
+
const getHttpRequestObject = response => ({
|
|
32
|
+
status: response.status,
|
|
33
|
+
requestUrl: response.config.url,
|
|
34
|
+
query: response.config.params,
|
|
35
|
+
body: response.config.data,
|
|
36
|
+
requestMethod: response.config.method.toUpperCase(),
|
|
37
|
+
});
|
|
42
38
|
|
|
43
39
|
module.exports = class Network {
|
|
44
40
|
constructor(settings = {}) {
|
package/package.json
CHANGED
package/settings/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const EventEmitter = require('events');
|
|
1
2
|
const NodeCache = require('node-cache');
|
|
2
3
|
const Network = require('../network');
|
|
3
4
|
const Logger = require('../logger');
|
|
@@ -6,17 +7,22 @@ const logger = Logger();
|
|
|
6
7
|
require('dotenv').config();
|
|
7
8
|
|
|
8
9
|
const fiveMinutes = 60 * 5;
|
|
10
|
+
const watingToNetwork = 'watingToNetwork';
|
|
9
11
|
|
|
10
12
|
const findUrl = serviceUrl => serviceUrl ||
|
|
11
13
|
(process.env.SETTING_MS_SERVICE_HOST && process.env.SETTING_MS_SERVICE_HOST.length > 0 ? `http://${process.env.SETTING_MS_SERVICE_HOST}/` : undefined) ||
|
|
12
14
|
(process.env.NODE_ENV !== 'test' ? 'http://setting-ms.autofleet.io/' : 'http://localhost:9999/');
|
|
13
15
|
|
|
14
16
|
module.exports = class SettingsManager {
|
|
17
|
+
static readNetworkValue(networkValue, defaultValue) {
|
|
18
|
+
return typeof networkValue !== 'undefined' ? networkValue : defaultValue;
|
|
19
|
+
}
|
|
15
20
|
constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
|
|
16
21
|
const localServiceUrl = findUrl(serviceUrl);
|
|
17
22
|
|
|
18
23
|
this.ttl = ttl;
|
|
19
24
|
this.settingsCache = new NodeCache();
|
|
25
|
+
this.networkEvents = new EventEmitter();
|
|
20
26
|
this.network = new Network({
|
|
21
27
|
serviceUrl: localServiceUrl,
|
|
22
28
|
});
|
|
@@ -30,6 +36,13 @@ module.exports = class SettingsManager {
|
|
|
30
36
|
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
31
37
|
const cacheValue = this.settingsCache.get(cacheKey);
|
|
32
38
|
if (cacheValue !== undefined) {
|
|
39
|
+
if (watingToNetwork === cacheValue) {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
42
|
+
resolve(SettingsManager.readNetworkValue(networkValue, defaultValue));
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
33
46
|
return cacheValue;
|
|
34
47
|
}
|
|
35
48
|
|
|
@@ -39,6 +52,7 @@ module.exports = class SettingsManager {
|
|
|
39
52
|
|
|
40
53
|
let networkValue;
|
|
41
54
|
try {
|
|
55
|
+
this.settingsCache.set(cacheKey, watingToNetwork, this.ttl);
|
|
42
56
|
const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
|
|
43
57
|
params: {
|
|
44
58
|
labels,
|
|
@@ -50,7 +64,8 @@ module.exports = class SettingsManager {
|
|
|
50
64
|
logger.error('Cant get setting from network');
|
|
51
65
|
}
|
|
52
66
|
|
|
53
|
-
|
|
67
|
+
this.networkEvents.emit(cacheKey, networkValue);
|
|
68
|
+
const returnValue = SettingsManager.readNetworkValue(networkValue, defaultValue);
|
|
54
69
|
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
55
70
|
return returnValue;
|
|
56
71
|
}
|
package/settings/index.test.js
CHANGED
|
@@ -72,6 +72,23 @@ describe('Settings', () => {
|
|
|
72
72
|
expect(value2).toEqual('value1');
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
+
|
|
76
|
+
it('Use only one request for similar keys', async () => {
|
|
77
|
+
const m = mockSetting('key1', 'value1');
|
|
78
|
+
const settings = new Settings({
|
|
79
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const [value, value2] = await Promise.all([
|
|
83
|
+
settings.get('key1', 'dv'),
|
|
84
|
+
settings.get('key1', 'dv'),
|
|
85
|
+
]);
|
|
86
|
+
expect(value).toEqual('value1');
|
|
87
|
+
expect(m.isDone()).toBeTruthy();
|
|
88
|
+
|
|
89
|
+
expect(value2).toEqual('value1');
|
|
90
|
+
});
|
|
91
|
+
|
|
75
92
|
it('Finds with labels', async () => {
|
|
76
93
|
const m = mockSetting('key1', 'value1', [{ fleetId: 'uuid' }]);
|
|
77
94
|
const settings = new Settings({
|