@autofleet/node-common 1.1.74 → 1.1.76
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/.npmignore +4 -0
- package/package.json +1 -1
- package/settings/index.js +29 -9
- package/settings/index.test.js +30 -1
package/.npmignore
ADDED
package/package.json
CHANGED
package/settings/index.js
CHANGED
|
@@ -8,19 +8,31 @@ const logger = Logger();
|
|
|
8
8
|
require('dotenv').config();
|
|
9
9
|
|
|
10
10
|
const fiveMinutes = 60 * 5;
|
|
11
|
-
const
|
|
11
|
+
const waitingToNetwork = 'waitingToNetwork';
|
|
12
12
|
|
|
13
13
|
const findUrl = serviceUrl => serviceUrl ||
|
|
14
14
|
(process.env.SETTING_MS_SERVICE_HOST && process.env.SETTING_MS_SERVICE_HOST.length > 0 ? `http://${process.env.SETTING_MS_SERVICE_HOST}/` : undefined) ||
|
|
15
15
|
(process.env.NODE_ENV !== 'test' ? 'http://setting-ms.autofleet.io/' : 'http://localhost:9999/');
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
class SettingsManager {
|
|
18
|
+
static get NEVER_DEFAULT_VALUE() {
|
|
19
|
+
return 'NEVER_DEFAULT_VALUE';
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
static readNetworkValue(networkValue, defaultValue) {
|
|
19
|
-
|
|
23
|
+
const returnValue = typeof networkValue !== 'undefined' ? networkValue : defaultValue;
|
|
24
|
+
|
|
25
|
+
if (returnValue === SettingsManager.NEVER_DEFAULT_VALUE) {
|
|
26
|
+
throw new Error('Cannot find value from network or cache, default value is set to never');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return returnValue;
|
|
20
30
|
}
|
|
31
|
+
|
|
21
32
|
constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
|
|
22
33
|
const localServiceUrl = findUrl(serviceUrl);
|
|
23
34
|
|
|
35
|
+
this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
|
|
24
36
|
this.ttl = ttl;
|
|
25
37
|
this.settingsCache = new NodeCache();
|
|
26
38
|
this.networkEvents = new EventEmitter();
|
|
@@ -29,7 +41,7 @@ module.exports = class SettingsManager {
|
|
|
29
41
|
});
|
|
30
42
|
}
|
|
31
43
|
|
|
32
|
-
async get(key, defaultValue, labels = []) {
|
|
44
|
+
async get(key, defaultValue, labels = [], { timeout = 1000 } = {}) {
|
|
33
45
|
if (typeof defaultValue === 'undefined') {
|
|
34
46
|
throw new Error('Can\'t get a key without defaultValue');
|
|
35
47
|
}
|
|
@@ -41,7 +53,7 @@ module.exports = class SettingsManager {
|
|
|
41
53
|
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
42
54
|
const cacheValue = this.settingsCache.get(cacheKey);
|
|
43
55
|
if (cacheValue !== undefined) {
|
|
44
|
-
if (
|
|
56
|
+
if (waitingToNetwork === cacheValue) {
|
|
45
57
|
return new Promise((resolve) => {
|
|
46
58
|
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
47
59
|
resolve(SettingsManager.readNetworkValue(networkValue, defaultValue));
|
|
@@ -57,12 +69,12 @@ module.exports = class SettingsManager {
|
|
|
57
69
|
|
|
58
70
|
let networkValue;
|
|
59
71
|
try {
|
|
60
|
-
this.settingsCache.set(cacheKey,
|
|
72
|
+
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
61
73
|
const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
|
|
62
74
|
params: {
|
|
63
75
|
labels,
|
|
64
76
|
},
|
|
65
|
-
timeout
|
|
77
|
+
timeout,
|
|
66
78
|
});
|
|
67
79
|
networkValue = networkReponse.data.value;
|
|
68
80
|
} catch (error) {
|
|
@@ -70,7 +82,13 @@ module.exports = class SettingsManager {
|
|
|
70
82
|
}
|
|
71
83
|
|
|
72
84
|
this.networkEvents.emit(cacheKey, networkValue);
|
|
73
|
-
|
|
85
|
+
let returnValue;
|
|
86
|
+
try {
|
|
87
|
+
returnValue = SettingsManager.readNetworkValue(networkValue, defaultValue);
|
|
88
|
+
} catch (e) {
|
|
89
|
+
this.settingsCache.del(cacheKey);
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
74
92
|
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
75
93
|
return returnValue;
|
|
76
94
|
}
|
|
@@ -83,4 +101,6 @@ module.exports = class SettingsManager {
|
|
|
83
101
|
flush() {
|
|
84
102
|
return this.settingsCache.flushAll();
|
|
85
103
|
}
|
|
86
|
-
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = SettingsManager;
|
package/settings/index.test.js
CHANGED
|
@@ -36,7 +36,7 @@ describe('Settings', () => {
|
|
|
36
36
|
expect(await settings.get('key1', false)).toBe(false);
|
|
37
37
|
});
|
|
38
38
|
|
|
39
|
-
it('Uses env.SETTING_MS_SERVICE_HOST as
|
|
39
|
+
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', async () => {
|
|
40
40
|
const m = mockSetting('key1', 'value1');
|
|
41
41
|
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
42
42
|
const settings = new Settings();
|
|
@@ -109,11 +109,40 @@ describe('Settings', () => {
|
|
|
109
109
|
expect(value).toEqual('dv');
|
|
110
110
|
});
|
|
111
111
|
|
|
112
|
+
it('Throws an error if network error and no default value', async () => {
|
|
113
|
+
const settings = new Settings({
|
|
114
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
118
|
+
expect(f())
|
|
119
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('Throws an error if network error and no default value and success the next time', async (done) => {
|
|
123
|
+
const settings = new Settings({
|
|
124
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
128
|
+
expect(f())
|
|
129
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
setTimeout(async () => {
|
|
133
|
+
const m = mockSetting('key1', 'value1');
|
|
134
|
+
expect(await settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|
|
135
|
+
expect(m.isDone());
|
|
136
|
+
done();
|
|
137
|
+
}, 100);
|
|
138
|
+
});
|
|
139
|
+
|
|
112
140
|
it('Values can be flushed', async () => {
|
|
113
141
|
const m1 = mockSetting('key1', 'value1');
|
|
114
142
|
const settings = new Settings({
|
|
115
143
|
serviceUrl: `http://${serviceUrl}/`,
|
|
116
144
|
});
|
|
145
|
+
settings.flush();
|
|
117
146
|
|
|
118
147
|
const value = await settings.get('key1', 'dv');
|
|
119
148
|
expect(value).toEqual('value1');
|