@magnet.me/consultant 1.2.6 → 1.3.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.
- package/README.md +1 -1
- package/out/agent.js +8 -7
- package/out/agent.js.map +1 -1
- package/out/config.js +12 -20
- package/out/config.js.map +1 -1
- package/out/service.js +8 -8
- package/out/service.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[](https://magnet.me
|
|
1
|
+
[](https://magnet.me "Discover the best companies, jobs and internships at Magnet.me")
|
|
2
2
|
|
|
3
3
|
# Consultant
|
|
4
4
|
###### Fetches your service's configuration from Consul, and subscribes to any changes.
|
package/out/agent.js
CHANGED
|
@@ -5,26 +5,27 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = fetchAgent;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
|
9
9
|
|
|
10
10
|
var _properties = _interopRequireDefault(require("./properties"));
|
|
11
11
|
|
|
12
|
-
var _promisify = _interopRequireDefault(require("./promisify"));
|
|
13
|
-
|
|
14
12
|
var _consultantError = _interopRequireDefault(require("./consultant-error"));
|
|
15
13
|
|
|
16
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
15
|
|
|
18
16
|
async function fetchAgent(consulHost) {
|
|
17
|
+
const uri = `${consulHost}/v1/agent/self`;
|
|
18
|
+
|
|
19
19
|
try {
|
|
20
|
-
const res = await (0,
|
|
21
|
-
uri: `${consulHost}/v1/agent/self`,
|
|
20
|
+
const res = await (0, _nodeFetch.default)(uri, {
|
|
22
21
|
timeout: 5000,
|
|
23
22
|
headers: {
|
|
23
|
+
'accept': 'application/json',
|
|
24
|
+
'content-type': 'application/json',
|
|
24
25
|
'user-agent': _properties.default.userAgent
|
|
25
26
|
}
|
|
26
|
-
}
|
|
27
|
-
return
|
|
27
|
+
});
|
|
28
|
+
return await res.json();
|
|
28
29
|
} catch (e) {
|
|
29
30
|
throw new _consultantError.default(`Could not retrieve agent from consul: ${e}`);
|
|
30
31
|
}
|
package/out/agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent.js"],"names":["fetchAgent","consulHost","
|
|
1
|
+
{"version":3,"sources":["../src/agent.js"],"names":["fetchAgent","consulHost","uri","res","timeout","headers","properties","userAgent","json","e","ConsultantError"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;AAEe,eAAeA,UAAf,CAA0BC,UAA1B,EAAsC;AACpD,QAAMC,GAAG,GAAI,GAAED,UAAW,gBAA1B;;AACA,MAAI;AACH,UAAME,GAAG,GAAG,MAAM,wBAAMD,GAAN,EAAW;AAC5BE,MAAAA,OAAO,EAAG,IADkB;AAE5BC,MAAAA,OAAO,EAAG;AACT,kBAAW,kBADF;AAET,wBAAiB,kBAFR;AAGT,sBAAeC,oBAAWC;AAHjB;AAFkB,KAAX,CAAlB;AAQA,WAAO,MAAMJ,GAAG,CAACK,IAAJ,EAAb;AACA,GAVD,CAWA,OAAOC,CAAP,EAAU;AACT,UAAM,IAAIC,wBAAJ,CAAqB,yCAAwCD,CAAE,EAA/D,CAAN;AACA;AACD","sourcesContent":["import fetch from 'node-fetch';\nimport properties from './properties';\nimport ConsultantError from './consultant-error';\n\nexport default async function fetchAgent(consulHost) {\n\tconst uri = `${consulHost}/v1/agent/self`;\n\ttry {\n\t\tconst res = await fetch(uri, {\n\t\t\ttimeout : 5000,\n\t\t\theaders : {\n\t\t\t\t'accept' : 'application/json',\n\t\t\t\t'content-type' : 'application/json',\n\t\t\t\t'user-agent' : properties.userAgent\n\t\t\t}\n\t\t});\n\t\treturn await res.json();\n\t}\n\tcatch (e) {\n\t\tthrow new ConsultantError(`Could not retrieve agent from consul: ${e}`);\n\t}\n}\n"],"file":"agent.js"}
|
package/out/config.js
CHANGED
|
@@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = initializeConfiguration;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var _promisify = _interopRequireDefault(require("./promisify"));
|
|
8
|
+
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
|
11
9
|
|
|
12
10
|
var _properties = _interopRequireDefault(require("./properties"));
|
|
13
11
|
|
|
@@ -23,7 +21,6 @@ const configUpdater = (consulHost, prefix, service, callback) => {
|
|
|
23
21
|
let consulIndex;
|
|
24
22
|
let timeoutId;
|
|
25
23
|
let shutdown = false;
|
|
26
|
-
let runningRequest;
|
|
27
24
|
const self = {
|
|
28
25
|
async poll() {
|
|
29
26
|
let timeout = 500;
|
|
@@ -36,18 +33,16 @@ const configUpdater = (consulHost, prefix, service, callback) => {
|
|
|
36
33
|
uri += `&index=${consulIndex}`;
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
const response = await (0,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
json: true
|
|
46
|
-
}, cb);
|
|
36
|
+
const response = await (0, _nodeFetch.default)(uri, {
|
|
37
|
+
headers: {
|
|
38
|
+
'accept': 'application/json',
|
|
39
|
+
'content-type': 'application/json',
|
|
40
|
+
'user-agent': _properties.default.userAgent
|
|
41
|
+
}
|
|
47
42
|
});
|
|
48
43
|
consulIndex = response.headers['x-consul-index'];
|
|
49
44
|
|
|
50
|
-
if (response.
|
|
45
|
+
if (response.status === 404) {
|
|
51
46
|
timeout = 5000;
|
|
52
47
|
const prefixText = prefix ? ` with prefix '${prefix}'` : ''; // eslint-disable-next-line no-console
|
|
53
48
|
|
|
@@ -55,14 +50,15 @@ const configUpdater = (consulHost, prefix, service, callback) => {
|
|
|
55
50
|
return;
|
|
56
51
|
}
|
|
57
52
|
|
|
58
|
-
if (response.
|
|
53
|
+
if (response.status !== 200) {
|
|
59
54
|
timeout = 60000; // eslint-disable-next-line no-console
|
|
60
55
|
|
|
61
|
-
console.warn(`Error retrieving data from Consul: ${response.
|
|
56
|
+
console.warn(`Error retrieving data from Consul: ${response.status}: ${await response.text()}`);
|
|
62
57
|
return;
|
|
63
58
|
}
|
|
64
59
|
|
|
65
|
-
const
|
|
60
|
+
const json = await response.json();
|
|
61
|
+
const properties = parseBody(json, prefix, service);
|
|
66
62
|
|
|
67
63
|
if (callback) {
|
|
68
64
|
callback(properties);
|
|
@@ -77,10 +73,6 @@ const configUpdater = (consulHost, prefix, service, callback) => {
|
|
|
77
73
|
stop() {
|
|
78
74
|
shutdown = true;
|
|
79
75
|
|
|
80
|
-
if (runningRequest) {
|
|
81
|
-
runningRequest.abort();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
76
|
if (timeoutId) {
|
|
85
77
|
clearTimeout(timeoutId);
|
|
86
78
|
}
|
package/out/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/config.js"],"names":["configUpdater","consulHost","prefix","service","callback","consulIndex","timeoutId","shutdown","
|
|
1
|
+
{"version":3,"sources":["../src/config.js"],"names":["configUpdater","consulHost","prefix","service","callback","consulIndex","timeoutId","shutdown","self","poll","timeout","prefixUri","uri","name","response","headers","props","userAgent","status","prefixText","console","warn","text","json","properties","parseBody","setTimeout","stop","clearTimeout","body","map","kvPair","key","Key","value","Value","filter","reduce","Object","assign","updateConfig","newConfig","callbacks","updates","keys","deletes","forEach","length","initializeConfiguration","Set","process","env","CONSUL_HOST","defaultHost","identifier","liveProperties","updater","newProperties","getProperties","register","add","deregister","delete"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;;;AAEA,MAAMA,aAAa,GAAG,CAACC,UAAD,EAAaC,MAAb,EAAqBC,OAArB,EAA8BC,QAA9B,KAA2C;AAChE,MAAIC,WAAJ;AACA,MAAIC,SAAJ;AACA,MAAIC,QAAQ,GAAG,KAAf;AACA,QAAMC,IAAI,GAA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AM,CAACC,MAAP,CAAc,EAAd,EAAkBgB,cAAlB,CAAP;AACA,KAHK;;AAINI,IAAAA,QAAQ,CAACvD,QAAD,EAAW;AAClBsC,MAAAA,SAAS,CAACkB,GAAV,CAAcxD,QAAd;AACA,KANK;;AAONyD,IAAAA,UAAU,CAACzD,QAAD,EAAW;AACpBsC,MAAAA,SAAS,CAACoB,MAAV,CAAiB1D,QAAjB;AACA,KATK;;AAUNuB,IAAAA,IAAI,GAAG;AACN6B,MAAAA,OAAO,CAAC7B,IAAR;AACA;;AAZK,GAAP;AAcA","sourcesContent":["import fetch from 'node-fetch';\nimport props from './properties';\nimport fetchIdentifier from './identifier';\nimport {parseKey, keyApplies} from './key-parser';\nimport atob from 'atob';\n\nconst configUpdater = (consulHost, prefix, service, callback) => {\n\tlet consulIndex;\n\tlet timeoutId;\n\tlet shutdown = false;\n\tconst self = {\n\t\tasync poll() {\n\t\t\tlet timeout = 500;\n\t\t\ttry {\n\t\t\t\tconst prefixUri = prefix ? `${prefix}/` : '';\n\t\t\t\tlet uri = `${consulHost}/v1/kv/${prefixUri}${service.name}/?recurse=true`;\n\t\t\t\tif (consulIndex) {\n\t\t\t\t\turi += `&index=${consulIndex}`;\n\t\t\t\t}\n\t\t\t\tconst response = await fetch(uri, {\n\t\t\t\t\theaders : {\n\t\t\t\t\t\t'accept' : 'application/json',\n\t\t\t\t\t\t'content-type' : 'application/json',\n\t\t\t\t\t\t'user-agent' : props.userAgent\n\t\t\t\t\t},\n\t\t\t\t});\n\n\t\t\t\tconsulIndex = response.headers['x-consul-index'];\n\t\t\t\tif (response.status === 404) {\n\t\t\t\t\ttimeout = 5000;\n\t\t\t\t\tconst prefixText = prefix ? ` with prefix '${prefix}'` : '';\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.warn(`'${service.name}'${prefixText} cannot be found in Consul`);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (response.status !== 200) {\n\t\t\t\t\ttimeout = 60000;\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.warn(`Error retrieving data from Consul: ${response.status}: ${await response.text()}`);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst json = await response.json();\n\t\t\t\tconst properties = parseBody(json, prefix, service);\n\t\t\t\tif (callback) {\n\t\t\t\t\tcallback(properties);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\tif (!shutdown) {\n\t\t\t\t\ttimeoutId = setTimeout(self.poll, timeout);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\tstop() {\n\t\t\tshutdown = true;\n\t\t\tif (timeoutId) {\n\t\t\t\tclearTimeout(timeoutId);\n\t\t\t}\n\t\t}\n\t};\n\n\treturn self;\n};\n\n/**\n * Parses the fetched config from Consul and converts it to a proper object.\n * @param body {Array} - The response body\n * @param prefix {string} - The prefix to extract from the key path\n * @param service {Object} - The service identifier to match key specifiers against\n * @return {Object} A properties object containing all valid properties as specified in the body\n */\nfunction parseBody(body, prefix, service) {\n\treturn body.map(kvPair => {\n\t\treturn {key : parseKey(kvPair.Key, prefix), value : kvPair.Value};\n\t})\n\t\t.filter(kvPair => kvPair.key.key && keyApplies(kvPair.key, service))\n\t\t.map(kvPair => {\n\t\t\treturn {[kvPair.key.key] : atob(kvPair.value)};\n\t\t})\n\t\t.reduce((properties, kvPair) => Object.assign(properties, kvPair), {});\n}\n\nconst updateConfig = (properties, newConfig, callbacks) => {\n\tconst updates = Object.keys(newConfig).filter(key => properties[key] !== newConfig[key]);\n\n\tObject.assign(properties, newConfig);\n\n\tconst deletes = Object.keys(properties).filter(key => !newConfig[key]);\n\tdeletes.forEach(key => delete properties[key]);\n\n\tif (updates.length > 0 || deletes.length > 0) {\n\t\tcallbacks.forEach(callback => callback(Object.assign({}, properties)));\n\t}\n};\n\nexport default async function initializeConfiguration({consulHost, service, prefix}) {\n\tconst callbacks = new Set();\n\n\tconsulHost = consulHost || process.env.CONSUL_HOST || props.defaultHost;\n\n\tconst identifier = await fetchIdentifier(service, consulHost);\n\n\tlet liveProperties = {};\n\n\tconst updater = configUpdater(consulHost, prefix, identifier, (newProperties) => {\n\t\tupdateConfig(liveProperties, newProperties, callbacks);\n\t});\n\n\tawait updater.poll();\n\n\treturn {\n\t\tgetProperties() {\n\t\t\treturn Object.assign({}, liveProperties);\n\t\t},\n\t\tregister(callback) {\n\t\t\tcallbacks.add(callback);\n\t\t},\n\t\tderegister(callback) {\n\t\t\tcallbacks.delete(callback);\n\t\t},\n\t\tstop() {\n\t\t\tupdater.stop();\n\t\t}\n\t};\n}\n"],"file":"config.js"}
|
package/out/service.js
CHANGED
|
@@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = register;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var _promisify = _interopRequireDefault(require("./promisify"));
|
|
8
|
+
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
|
|
11
9
|
|
|
12
10
|
var _consultantError = _interopRequireDefault(require("./consultant-error"));
|
|
13
11
|
|
|
@@ -27,7 +25,7 @@ const deregister = async (host, instance) => {
|
|
|
27
25
|
};
|
|
28
26
|
|
|
29
27
|
try {
|
|
30
|
-
await (0,
|
|
28
|
+
await (0, _nodeFetch.default)(req);
|
|
31
29
|
} catch (e) {
|
|
32
30
|
throw new _consultantError.default(`Could not deregister from Consul: ${e}`);
|
|
33
31
|
}
|
|
@@ -67,17 +65,19 @@ async function register({
|
|
|
67
65
|
});
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
const
|
|
71
|
-
|
|
68
|
+
const url = `${consulHost}/v1/agent/service/register`;
|
|
69
|
+
const opts = {
|
|
72
70
|
method: 'PUT',
|
|
73
71
|
headers: {
|
|
72
|
+
'accept': 'application/json',
|
|
73
|
+
'content-type': 'application/json',
|
|
74
74
|
'user-agent': _properties.default.userAgent
|
|
75
75
|
},
|
|
76
|
-
|
|
76
|
+
body: JSON.stringify(body)
|
|
77
77
|
};
|
|
78
78
|
|
|
79
79
|
try {
|
|
80
|
-
await (0,
|
|
80
|
+
await (0, _nodeFetch.default)(url, opts);
|
|
81
81
|
} catch (e) {
|
|
82
82
|
throw new _consultantError.default('Could not register service with Consul', e);
|
|
83
83
|
}
|
package/out/service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/service.js"],"names":["deregister","host","instance","req","url","method","headers","properties","userAgent","
|
|
1
|
+
{"version":3,"sources":["../src/service.js"],"names":["deregister","host","instance","req","url","method","headers","properties","userAgent","e","ConsultantError","register","service","healthCheckPath","healthCheckInterval","consulHost","process","env","CONSUL_HOST","defaultHost","portNumber","Number","port","name","identifier","body","Id","Name","Address","Port","Object","assign","Check","HTTP","Interval","opts","JSON","stringify"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAEA,MAAMA,UAAU,GAAG,OAAOC,IAAP,EAAaC,QAAb,KAA0B;AAC5C,QAAMC,GAAG,GAAG;AACXC,IAAAA,GAAG,EAAI,GAAEH,IAAK,gCAA+BC,QAAQ,CAACA,QAAS,EADpD;AAEXG,IAAAA,MAAM,EAAG,KAFE;AAGXC,IAAAA,OAAO,EAAG;AACT,oBAAeC,oBAAWC;AADjB;AAHC,GAAZ;;AAQA,MAAI;AACH,UAAM,wBAAML,GAAN,CAAN;AACA,GAFD,CAGA,OAAOM,CAAP,EAAU;AACT,UAAM,IAAIC,wBAAJ,CAAqB,qCAAoCD,CAAE,EAA3D,CAAN;AACA;AACD,CAfD;;AAiBe,eAAeE,QAAf,CAAwB;AAACC,EAAAA,OAAD;AAAUC,EAAAA,eAAV;AAA2BC,EAAAA,mBAAmB,GAAG,EAAjD;AAAqDC,EAAAA;AAArD,CAAxB,EAA0F;AACxGA,EAAAA,UAAU,GAAGA,UAAU,IAAIC,OAAO,CAACC,GAAR,CAAYC,WAA1B,IAAyCX,oBAAWY,WAAjE;AAEA,QAAMC,UAAU,GA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,CAGA,OAAO1B,CAAP,EAAU;AACT,UAAM,IAAIC,wBAAJ,CAAoB,wCAApB,EAA8DD,CAA9D,CAAN;AACA;;AAED,SAAO;AACNe,IAAAA,UADM;AAENxB,IAAAA,UAAU,EAAG,MAAMA,UAAU,CAACe,UAAD,EAAaS,UAAb;AAFvB,GAAP;AAIA","sourcesContent":["import fetch from 'node-fetch';\nimport ConsultantError from './consultant-error';\nimport properties from './properties';\nimport fetchIdentifier from './identifier';\n\nconst deregister = async (host, instance) => {\n\tconst req = {\n\t\turl : `${host}/v1/agent/service/deregister/${instance.instance}`,\n\t\tmethod : 'PUT',\n\t\theaders : {\n\t\t\t'user-agent' : properties.userAgent\n\t\t}\n\t};\n\n\ttry {\n\t\tawait fetch(req);\n\t}\n\tcatch (e) {\n\t\tthrow new ConsultantError(`Could not deregister from Consul: ${e}`);\n\t}\n};\n\nexport default async function register({service, healthCheckPath, healthCheckInterval = 10, consulHost}) {\n\tconsulHost = consulHost || process.env.CONSUL_HOST || properties.defaultHost;\n\n\tconst portNumber = Number(service.port);\n\tif (!(portNumber > 0 && portNumber < 65536)) {\n\t\tthrow new ConsultantError(`service.port=${service.port} is not a proper port`);\n\t}\n\n\tif (!service.name) {\n\t\tthrow new ConsultantError('service.name was not defined');\n\t}\n\n\tconst identifier = await fetchIdentifier(service, consulHost);\n\n\tconst body = {\n\t\tId : identifier.instance,\n\t\tName : identifier.name,\n\t\tAddress : identifier.host,\n\t\tPort : service.port,\n\t};\n\n\tif (healthCheckPath && healthCheckInterval > 0) {\n\t\tObject.assign(body, {\n\t\t\tCheck : {\n\t\t\t\tHTTP : `http://${identifier.host}:${service.port}${healthCheckPath}`,\n\t\t\t\tInterval : healthCheckInterval\n\t\t\t}\n\t\t});\n\t}\n\n\tconst url = `${consulHost}/v1/agent/service/register`;\n\tconst opts = {\n\t\tmethod : 'PUT',\n\t\theaders : {\n\t\t\t'accept' : 'application/json',\n\t\t\t'content-type' : 'application/json',\n\t\t\t'user-agent' : properties.userAgent\n\t\t},\n\t\tbody : JSON.stringify(body)\n\t};\n\n\ttry {\n\t\tawait fetch(url, opts);\n\t}\n\tcatch (e) {\n\t\tthrow new ConsultantError('Could not register service with Consul', e);\n\t}\n\n\treturn {\n\t\tidentifier,\n\t\tderegister : () => deregister(consulHost, identifier)\n\t};\n}\n"],"file":"service.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magnet.me/consultant",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A library to register services to Consul and listen for property changes.",
|
|
5
5
|
"main": "out/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"license": "Apache-2.0",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"atob": "^2.0.3",
|
|
16
|
-
"
|
|
16
|
+
"node-fetch": "^2.6.7",
|
|
17
17
|
"uuid": "^7.0.3"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|