af-consul
Advanced tools
Comparing version 3.0.25 to 3.0.26
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -85,2 +85,3 @@ "use strict"; | ||
addAdditionalAPProps(accessPoint); | ||
// @ts-ignore | ||
this[apKey] = accessPoint; | ||
@@ -99,2 +100,3 @@ Object.entries(apData).forEach(([propName, v]) => { | ||
} | ||
// @ts-ignore | ||
const accessPoint = this[apKey]; | ||
@@ -139,2 +141,3 @@ if (!accessPoint) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -154,2 +157,3 @@ if (!(accessPoint === null || accessPoint === void 0 ? void 0 : accessPoint.isAP)) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -156,0 +160,0 @@ if (!(accessPoint === null || accessPoint === void 0 ? void 0 : accessPoint.isAP)) { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -47,2 +47,3 @@ "use strict"; | ||
Object.entries(api).forEach(([k, v]) => { | ||
// @ts-ignore | ||
value[k] = typeof v === 'function' ? v.bind(api) : v; | ||
@@ -49,0 +50,0 @@ }); |
@@ -72,2 +72,3 @@ "use strict"; | ||
tags, | ||
// @ts-ignore | ||
meta: { ...metaObj, ...meta }, | ||
@@ -78,3 +79,5 @@ }; | ||
.forEach(([n, v]) => { | ||
// @ts-ignore | ||
if (!check[n]) { | ||
// @ts-ignore | ||
check[n] = v; | ||
@@ -81,0 +84,0 @@ } |
@@ -0,0 +0,0 @@ "use strict"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=interfaces.js.map |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
"use strict"; | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-console,prefer-spread */ | ||
// noinspection UnnecessaryLocalVariableJS,JSUnusedGlobalSymbols | ||
@@ -17,3 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const mutex = new async_mutex_1.Mutex(); | ||
const dbg = { on: constants_1.CONSUL_DEBUG_ON, curl: /af-consul:curl/i.test(constants_1.DEBUG) }; | ||
const dbg = { | ||
on: constants_1.CONSUL_DEBUG_ON, | ||
curl: /af-consul:curl/i.test(constants_1.DEBUG), | ||
}; | ||
const debug = (msg) => { | ||
@@ -25,4 +28,5 @@ if (dbg.on) { | ||
const agentTypeS = Symbol.for('agentType'); | ||
const consulConfigTypes = ['reg', 'dev', 'prd']; | ||
const getConsulAgentOptions = async (clOptions) => { | ||
const { agent, service } = clOptions.config.consul; | ||
const { agent, service, } = clOptions.config.consul; | ||
// const regAgent = { ..._.pick(reg, ['host', 'port', 'secure', 'token']) }; | ||
@@ -40,3 +44,3 @@ const secure_ = (0, utils_1.parseBoolean)(agent.reg.secure); | ||
if (agent[id]) { | ||
const { host, port, secure, token, dc } = agent[id]; | ||
const { host, port, secure, token, dc, } = agent[id]; | ||
result[id] = { | ||
@@ -54,6 +58,7 @@ host: String(host || reg.host), | ||
}); | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
if (!Number(result[id].port)) { | ||
throw new Error(`The port for consul agent[${id}] is invalid: [${result[id].port}]`); | ||
} | ||
// @ts-ignore | ||
result[id][agentTypeS] = id; | ||
@@ -74,5 +79,6 @@ }); | ||
const consulInstances = {}; | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
const consulAgentOptions = fullConsulAgentOptions[id]; | ||
const consulInstance = Consul(consulAgentOptions); // { host, port, secure, defaults: { token } } | ||
// @ts-ignore | ||
consulInstance[agentTypeS] = id; | ||
@@ -93,3 +99,3 @@ consulInstances[id] = consulInstance; | ||
const { res } = request || {}; | ||
const { statusCode = 0, body = null } = res || {}; | ||
const { statusCode = 0, body = null, } = res || {}; | ||
debug(`${rqId}HTTP Status: ${statusCode}`); | ||
@@ -114,3 +120,3 @@ if (statusCode > 299 && !((_c = (_b = (_a = request.opts) === null || _a === void 0 ? void 0 : _a.skipCodes) === null || _b === void 0 ? void 0 : _b.includes) === null || _c === void 0 ? void 0 : _c.call(_b, statusCode))) { | ||
const agentType = serviceId.substring(0, 3); | ||
return /(dev|prd)/.test(agentType) ? agentType : 'reg'; | ||
return (/(dev|prd)/.test(agentType) ? agentType : 'reg'); | ||
}; | ||
@@ -142,2 +148,3 @@ const getAgentOptionsByServiceID = (serviceId) => fullConsulAgentOptions[getAgentTypeByServiceID(serviceId)]; | ||
namesArr.forEach((v) => { | ||
// @ts-ignore | ||
fn = fn[v]; | ||
@@ -147,3 +154,3 @@ }); | ||
try { | ||
// eslint-disable-next-line prefer-spread | ||
// @ts-ignore | ||
fn[method].apply(fn, args); | ||
@@ -169,3 +176,5 @@ } | ||
if (!apiArgs.consulInstance && !apiArgs.agentOptions) { | ||
const agentType = (Object.entries(fullConsulAgentOptions).find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
const agentType = (Object.entries(fullConsulAgentOptions) | ||
.find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
@@ -179,3 +188,3 @@ } | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const { service: serviceId, dc } = apiArgs.options; | ||
const { service: serviceId, dc, } = apiArgs.options; | ||
if (!dc) { | ||
@@ -193,3 +202,8 @@ const agentOptions = getAgentOptionsByServiceID(serviceId); | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const result = await this.consulHealthService({ options: { service: serviceName, passing: true } }); | ||
const result = await this.consulHealthService({ | ||
options: { | ||
service: serviceName, | ||
passing: true, | ||
}, | ||
}); | ||
logger.debug(`No info about service ID ${color_1.cyan}${serviceName}`); | ||
@@ -214,3 +228,3 @@ return (_a = result === null || result === void 0 ? void 0 : result[0]) === null || _a === void 0 ? void 0 : _a.Service; | ||
} | ||
const { Address = (_a = result[0].Node) === null || _a === void 0 ? void 0 : _a.Node, Port } = (result[0].Service || {}); | ||
const { Address = (_a = result[0].Node) === null || _a === void 0 ? void 0 : _a.Node, Port, } = (result[0].Service || {}); | ||
const host = await (0, fqdn_1.getFQDNCached)(Address); | ||
@@ -246,6 +260,11 @@ return { | ||
if (healthServiceInfo) { | ||
const nodeHost = (((_a = healthServiceInfo.Node) === null || _a === void 0 ? void 0 : _a.Node) || '').toLowerCase().split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions).find(([, aOpt]) => aOpt.host.toLowerCase().startsWith(nodeHost)) || []; | ||
const nodeHost = (((_a = healthServiceInfo.Node) === null || _a === void 0 ? void 0 : _a.Node) || '').toLowerCase() | ||
.split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions) | ||
.find(([, aOpt]) => aOpt.host.toLowerCase() | ||
.startsWith(nodeHost)) || []; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
const isDeregister = await this.agentServiceDeregister(serviceId, apiArgs); | ||
// @ts-ignore | ||
const m = (wasnt = '') => `Previous registration of service '${color_1.cyan}${serviceId}${color_1.reset}'${wasnt} removed from consul agent ${color_1.blue}${fullConsulAgentOptions[agentType].host}${color_1.reset}`; | ||
@@ -334,3 +353,6 @@ if (isDeregister) { | ||
const value = await (0, exports.prepareConsulAPI)(clOptions); | ||
consulApiCache[hash] = { created: Date.now(), value }; | ||
consulApiCache[hash] = { | ||
created: Date.now(), | ||
value, | ||
}; | ||
} | ||
@@ -337,0 +359,0 @@ return consulApiCache[hash].value; |
@@ -0,0 +0,0 @@ /* eslint-disable no-console */ |
@@ -0,0 +0,0 @@ import * as http from 'http'; |
@@ -82,2 +82,3 @@ /* eslint-disable no-await-in-loop */ | ||
addAdditionalAPProps(accessPoint); | ||
// @ts-ignore | ||
this[apKey] = accessPoint; | ||
@@ -96,2 +97,3 @@ Object.entries(apData).forEach(([propName, v]) => { | ||
} | ||
// @ts-ignore | ||
const accessPoint = this[apKey]; | ||
@@ -136,2 +138,3 @@ if (!accessPoint) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -151,2 +154,3 @@ if (!(accessPoint === null || accessPoint === void 0 ? void 0 : accessPoint.isAP)) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -153,0 +157,0 @@ if (!(accessPoint === null || accessPoint === void 0 ? void 0 : accessPoint.isAP)) { |
@@ -0,0 +0,0 @@ export const PREFIX = 'AF-CONSUL'; |
@@ -0,0 +0,0 @@ /* eslint-disable no-console */ |
@@ -43,2 +43,3 @@ /* eslint-disable no-console */ | ||
Object.entries(api).forEach(([k, v]) => { | ||
// @ts-ignore | ||
value[k] = typeof v === 'function' ? v.bind(api) : v; | ||
@@ -45,0 +46,0 @@ }); |
@@ -68,2 +68,3 @@ import { getPackageJson, parseMeta, parseTags, removeAroundQuotas } from './lib/utils'; | ||
tags, | ||
// @ts-ignore | ||
meta: { ...metaObj, ...meta }, | ||
@@ -74,3 +75,5 @@ }; | ||
.forEach(([n, v]) => { | ||
// @ts-ignore | ||
if (!check[n]) { | ||
// @ts-ignore | ||
check[n] = v; | ||
@@ -77,0 +80,0 @@ } |
@@ -0,0 +0,0 @@ export { AccessPoints } from './access-points/access-points'; |
export {}; | ||
//# sourceMappingURL=interfaces.js.map |
@@ -0,0 +0,0 @@ // noinspection JSUnusedGlobalSymbols |
@@ -0,0 +0,0 @@ class ToCurl { |
@@ -0,0 +0,0 @@ import * as os from 'os'; |
@@ -0,0 +0,0 @@ import * as _ from 'lodash'; |
@@ -0,0 +0,0 @@ const getHttpRequestText = (request) => { |
@@ -0,0 +0,0 @@ /* eslint-disable no-console */ |
@@ -0,0 +0,0 @@ import * as fs from 'fs'; |
@@ -1,2 +0,2 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-console,prefer-spread */ | ||
// noinspection UnnecessaryLocalVariableJS,JSUnusedGlobalSymbols | ||
@@ -14,3 +14,6 @@ import * as Consul from 'consul'; | ||
const mutex = new Mutex(); | ||
const dbg = { on: CONSUL_DEBUG_ON, curl: /af-consul:curl/i.test(DEBUG) }; | ||
const dbg = { | ||
on: CONSUL_DEBUG_ON, | ||
curl: /af-consul:curl/i.test(DEBUG), | ||
}; | ||
const debug = (msg) => { | ||
@@ -22,4 +25,5 @@ if (dbg.on) { | ||
const agentTypeS = Symbol.for('agentType'); | ||
const consulConfigTypes = ['reg', 'dev', 'prd']; | ||
const getConsulAgentOptions = async (clOptions) => { | ||
const { agent, service } = clOptions.config.consul; | ||
const { agent, service, } = clOptions.config.consul; | ||
// const regAgent = { ..._.pick(reg, ['host', 'port', 'secure', 'token']) }; | ||
@@ -37,3 +41,3 @@ const secure_ = parseBoolean(agent.reg.secure); | ||
if (agent[id]) { | ||
const { host, port, secure, token, dc } = agent[id]; | ||
const { host, port, secure, token, dc, } = agent[id]; | ||
result[id] = { | ||
@@ -51,6 +55,7 @@ host: String(host || reg.host), | ||
}); | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
if (!Number(result[id].port)) { | ||
throw new Error(`The port for consul agent[${id}] is invalid: [${result[id].port}]`); | ||
} | ||
// @ts-ignore | ||
result[id][agentTypeS] = id; | ||
@@ -71,5 +76,6 @@ }); | ||
const consulInstances = {}; | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
const consulAgentOptions = fullConsulAgentOptions[id]; | ||
const consulInstance = Consul(consulAgentOptions); // { host, port, secure, defaults: { token } } | ||
// @ts-ignore | ||
consulInstance[agentTypeS] = id; | ||
@@ -90,3 +96,3 @@ consulInstances[id] = consulInstance; | ||
const { res } = request || {}; | ||
const { statusCode = 0, body = null } = res || {}; | ||
const { statusCode = 0, body = null, } = res || {}; | ||
debug(`${rqId}HTTP Status: ${statusCode}`); | ||
@@ -111,3 +117,3 @@ if (statusCode > 299 && !((_c = (_b = (_a = request.opts) === null || _a === void 0 ? void 0 : _a.skipCodes) === null || _b === void 0 ? void 0 : _b.includes) === null || _c === void 0 ? void 0 : _c.call(_b, statusCode))) { | ||
const agentType = serviceId.substring(0, 3); | ||
return /(dev|prd)/.test(agentType) ? agentType : 'reg'; | ||
return (/(dev|prd)/.test(agentType) ? agentType : 'reg'); | ||
}; | ||
@@ -139,2 +145,3 @@ const getAgentOptionsByServiceID = (serviceId) => fullConsulAgentOptions[getAgentTypeByServiceID(serviceId)]; | ||
namesArr.forEach((v) => { | ||
// @ts-ignore | ||
fn = fn[v]; | ||
@@ -144,3 +151,3 @@ }); | ||
try { | ||
// eslint-disable-next-line prefer-spread | ||
// @ts-ignore | ||
fn[method].apply(fn, args); | ||
@@ -166,3 +173,5 @@ } | ||
if (!apiArgs.consulInstance && !apiArgs.agentOptions) { | ||
const agentType = (Object.entries(fullConsulAgentOptions).find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
const agentType = (Object.entries(fullConsulAgentOptions) | ||
.find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
@@ -176,3 +185,3 @@ } | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const { service: serviceId, dc } = apiArgs.options; | ||
const { service: serviceId, dc, } = apiArgs.options; | ||
if (!dc) { | ||
@@ -190,3 +199,8 @@ const agentOptions = getAgentOptionsByServiceID(serviceId); | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const result = await this.consulHealthService({ options: { service: serviceName, passing: true } }); | ||
const result = await this.consulHealthService({ | ||
options: { | ||
service: serviceName, | ||
passing: true, | ||
}, | ||
}); | ||
logger.debug(`No info about service ID ${cyan}${serviceName}`); | ||
@@ -211,3 +225,3 @@ return (_a = result === null || result === void 0 ? void 0 : result[0]) === null || _a === void 0 ? void 0 : _a.Service; | ||
} | ||
const { Address = (_a = result[0].Node) === null || _a === void 0 ? void 0 : _a.Node, Port } = (result[0].Service || {}); | ||
const { Address = (_a = result[0].Node) === null || _a === void 0 ? void 0 : _a.Node, Port, } = (result[0].Service || {}); | ||
const host = await getFQDNCached(Address); | ||
@@ -243,6 +257,11 @@ return { | ||
if (healthServiceInfo) { | ||
const nodeHost = (((_a = healthServiceInfo.Node) === null || _a === void 0 ? void 0 : _a.Node) || '').toLowerCase().split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions).find(([, aOpt]) => aOpt.host.toLowerCase().startsWith(nodeHost)) || []; | ||
const nodeHost = (((_a = healthServiceInfo.Node) === null || _a === void 0 ? void 0 : _a.Node) || '').toLowerCase() | ||
.split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions) | ||
.find(([, aOpt]) => aOpt.host.toLowerCase() | ||
.startsWith(nodeHost)) || []; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
const isDeregister = await this.agentServiceDeregister(serviceId, apiArgs); | ||
// @ts-ignore | ||
const m = (wasnt = '') => `Previous registration of service '${cyan}${serviceId}${reset}'${wasnt} removed from consul agent ${blue}${fullConsulAgentOptions[agentType].host}${reset}`; | ||
@@ -330,3 +349,6 @@ if (isDeregister) { | ||
const value = await prepareConsulAPI(clOptions); | ||
consulApiCache[hash] = { created: Date.now(), value }; | ||
consulApiCache[hash] = { | ||
created: Date.now(), | ||
value, | ||
}; | ||
} | ||
@@ -333,0 +355,0 @@ return consulApiCache[hash].value; |
@@ -0,0 +0,0 @@ /// <reference types="node" /> |
@@ -0,0 +0,0 @@ import { AccessPoints } from './access-points'; |
@@ -0,0 +0,0 @@ import { IAccessPoint, IAccessPoints, ILogger, Maybe } from '../interfaces'; |
@@ -0,0 +0,0 @@ export declare const PREFIX = "AF-CONSUL"; |
import { ICLOptions, IConsulAPI, IRegisterConfig, IRegisterCyclic } from './interfaces'; | ||
export declare const getRegisterCyclic: (opts: ICLOptions, consulApi: IConsulAPI, registerConfig: IRegisterConfig) => IRegisterCyclic; | ||
//# sourceMappingURL=cyclic-register.d.ts.map |
@@ -0,0 +0,0 @@ import { IAFConsulAPI, ICache, ICLOptions } from './interfaces'; |
@@ -0,0 +0,0 @@ import { ICLOptions, IRegisterConfig } from './interfaces'; |
@@ -0,0 +0,0 @@ export { AccessPoints } from './access-points/access-points'; |
@@ -0,0 +0,0 @@ /// <reference types="node" /> |
@@ -0,0 +0,0 @@ export declare const reset = "\u001B[0m"; |
declare const getCurlText: (req: any, isPapi?: boolean) => string; | ||
export default getCurlText; | ||
//# sourceMappingURL=curl-text.d.ts.map |
export declare const getFQDN: (h?: string, withError?: boolean, onlyDomain?: boolean) => Promise<string | null>; | ||
export declare const getFQDNCached: (...args: any[]) => Promise<string | null>; | ||
//# sourceMappingURL=fqdn.d.ts.map |
import { ICLOptions } from '../interfaces'; | ||
export declare const getConfigHash: (options: ICLOptions) => string; | ||
//# sourceMappingURL=hash.d.ts.map |
declare const getHttpRequestText: (request: any) => string; | ||
export default getHttpRequestText; | ||
//# sourceMappingURL=http-request-text.d.ts.map |
@@ -0,0 +0,0 @@ declare const loggerStub: { |
import { ICache, IConsulServiceInfo, IRegisterConfig, Maybe } from '../interfaces'; | ||
export declare const removeAroundQuotas: (str: string) => string; | ||
export declare const parseBoolean: (bv: any) => boolean; | ||
export declare const substitute: (meta: object, data: object) => void; | ||
export declare const parseMeta: (m: string | object | undefined, data: object) => {}; | ||
export declare const substitute: (meta: any, data: any) => void; | ||
export declare const parseMeta: (m: string | object | undefined, data: object) => any; | ||
export declare const parseTags: (t: any) => string[]; | ||
@@ -7,0 +7,0 @@ export declare const serviceConfigDiff: (registerConfig: IRegisterConfig, serviceInfo: Maybe<IConsulServiceInfo>) => any[]; |
@@ -0,0 +0,0 @@ import { ICLOptions, IConsulAPI } from './interfaces'; |
{ | ||
"name": "af-consul", | ||
"version": "3.0.25", | ||
"version": "3.0.26", | ||
"description": "A highly specialized function library", | ||
@@ -19,3 +19,2 @@ "main": "./dist/cjs/src/index.js", | ||
"lint:fix": "eslint . --ext .ts --fix", | ||
"release": "np --yolo", | ||
"clean": "shx rm -rf dist/*", | ||
@@ -44,7 +43,6 @@ "build": "tsc -b tsconfig.json tsconfig.esm.json tsconfig.types.json", | ||
"jest": "^29.6.4", | ||
"np": "^7.6.3", | ||
"shx": "^0.3.4", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.5" | ||
"typescript": "^5.2.2" | ||
}, | ||
@@ -51,0 +49,0 @@ "np": { |
@@ -95,2 +95,3 @@ /* eslint-disable no-await-in-loop */ | ||
// @ts-ignore | ||
this[apKey] = accessPoint; | ||
@@ -111,2 +112,3 @@ Object.entries(apData).forEach(([propName, v]) => { | ||
} | ||
// @ts-ignore | ||
const accessPoint = this[apKey]; | ||
@@ -156,2 +158,3 @@ if (!accessPoint) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -172,2 +175,3 @@ if (!accessPoint?.isAP) { | ||
if (accessPointKey) { | ||
// @ts-ignore | ||
const accessPoint = this[accessPointKey]; | ||
@@ -174,0 +178,0 @@ if (!accessPoint?.isAP) { |
@@ -53,2 +53,3 @@ /* eslint-disable no-console */ | ||
Object.entries(api).forEach(([k, v]) => { | ||
// @ts-ignore | ||
value[k] = typeof v === 'function' ? v.bind(api) : v; | ||
@@ -55,0 +56,0 @@ }); |
@@ -74,2 +74,3 @@ import { getPackageJson, parseMeta, parseTags, removeAroundQuotas } from './lib/utils'; | ||
tags, | ||
// @ts-ignore | ||
meta: <Record<string, string>>{ ...metaObj, ...meta }, | ||
@@ -80,3 +81,5 @@ }; | ||
.forEach(([n, v]) => { | ||
// @ts-ignore | ||
if (!check[n]) { | ||
// @ts-ignore | ||
check[n] = v; | ||
@@ -83,0 +86,0 @@ } |
@@ -25,3 +25,3 @@ import * as fs from 'fs'; | ||
export const substitute = (meta: object, data: object): void => { | ||
export const substitute = (meta: any, data: any): void => { | ||
const re = /%?{([^}]+)}/g; | ||
@@ -40,3 +40,3 @@ Object.entries(meta).forEach(([k, v]) => { | ||
export const parseMeta = (m: string | object | undefined, data: object) => { | ||
const metaData = {}; | ||
const metaData = {} as any; | ||
if (!m) { | ||
@@ -104,4 +104,4 @@ return metaData; | ||
mastBeEquals.some(([p1, p2]) => { | ||
if (registerConfig[p1] !== serviceInfo[p2]) { | ||
diff = [p1, registerConfig[p1], p2, serviceInfo[p2]]; | ||
if (registerConfig[p1 as keyof IRegisterConfig] !== serviceInfo[p2]) { | ||
diff = [p1, registerConfig[p1 as keyof IRegisterConfig], p2, serviceInfo[p2]]; | ||
return true; | ||
@@ -108,0 +108,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable no-console,prefer-spread */ | ||
// noinspection UnnecessaryLocalVariableJS,JSUnusedGlobalSymbols | ||
@@ -14,7 +14,7 @@ | ||
ICLOptions, | ||
IConsul, | ||
IConsul, IConsulAgentConfig, | ||
IConsulAgentOptions, | ||
IConsulAPI, | ||
IConsulHealthServiceInfo, | ||
IConsulServiceInfo, | ||
IConsulServiceInfo, IFullConsulAgentConfig, | ||
IFullConsulAgentOptions, | ||
@@ -36,3 +36,6 @@ ILogger, | ||
const dbg = { on: CONSUL_DEBUG_ON, curl: /af-consul:curl/i.test(DEBUG) }; | ||
const dbg = { | ||
on: CONSUL_DEBUG_ON, | ||
curl: /af-consul:curl/i.test(DEBUG), | ||
}; | ||
const debug = (msg: string) => { | ||
@@ -46,4 +49,9 @@ if (dbg.on) { | ||
const consulConfigTypes = ['reg', 'dev', 'prd'] as (keyof IFullConsulAgentConfig)[]; | ||
const getConsulAgentOptions = async (clOptions: ICLOptions): Promise<IFullConsulAgentOptions> => { | ||
const { agent, service } = clOptions.config.consul; | ||
const { | ||
agent, | ||
service, | ||
} = clOptions.config.consul; | ||
@@ -61,5 +69,11 @@ // const regAgent = { ..._.pick(reg, ['host', 'port', 'secure', 'token']) }; | ||
['dev', 'prd'].forEach((id) => { | ||
(['dev', 'prd'] as (keyof IFullConsulAgentConfig)[]).forEach((id) => { | ||
if (agent[id]) { | ||
const { host, port, secure, token, dc } = agent[id]; | ||
const { | ||
host, | ||
port, | ||
secure, | ||
token, | ||
dc, | ||
} = agent[id] as IConsulAgentConfig; | ||
result[id] = { | ||
@@ -76,6 +90,7 @@ host: String(host || reg.host), | ||
}); | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
if (!Number(result[id].port)) { | ||
throw new Error(`The port for consul agent[${id}] is invalid: [${result[id].port}]`); | ||
} | ||
// @ts-ignore | ||
result[id][agentTypeS] = id; | ||
@@ -99,5 +114,6 @@ }); | ||
const consulInstances = {} as { reg: IConsul, dev: IConsul, prd: IConsul }; | ||
['reg', 'dev', 'prd'].forEach((id) => { | ||
consulConfigTypes.forEach((id) => { | ||
const consulAgentOptions = fullConsulAgentOptions[id]; | ||
const consulInstance: IConsul = Consul(consulAgentOptions) as IConsul; // { host, port, secure, defaults: { token } } | ||
// @ts-ignore | ||
consulInstance[agentTypeS] = id; | ||
@@ -120,3 +136,6 @@ | ||
const { res } = request || {}; | ||
const { statusCode = 0, body = null } = res || {}; | ||
const { | ||
statusCode = 0, | ||
body = null, | ||
} = res || {}; | ||
debug(`${rqId}HTTP Status: ${statusCode}`); | ||
@@ -138,5 +157,5 @@ if (statusCode > 299 && !request.opts?.skipCodes?.includes?.(statusCode)) { | ||
const getAgentTypeByServiceID = (serviceId: string): string => { | ||
const getAgentTypeByServiceID = (serviceId: string): keyof IFullConsulAgentConfig => { | ||
const agentType = serviceId.substring(0, 3); | ||
return /(dev|prd)/.test(agentType) ? agentType : 'reg'; | ||
return (/(dev|prd)/.test(agentType) ? agentType : 'reg') as keyof IFullConsulAgentConfig; | ||
}; | ||
@@ -176,2 +195,3 @@ const getAgentOptionsByServiceID = (serviceId: string): IConsulAgentOptions => fullConsulAgentOptions[getAgentTypeByServiceID(serviceId)]; | ||
namesArr.forEach((v) => { | ||
// @ts-ignore | ||
fn = fn[v]; | ||
@@ -181,3 +201,3 @@ }); | ||
try { | ||
// eslint-disable-next-line prefer-spread | ||
// @ts-ignore | ||
fn[method].apply(fn, args); | ||
@@ -204,3 +224,5 @@ } catch (err: Error | any) { | ||
if (!apiArgs.consulInstance && !apiArgs.agentOptions) { | ||
const agentType = (Object.entries(fullConsulAgentOptions).find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
const agentType = (Object.entries(fullConsulAgentOptions) | ||
.find(([, v]) => v.dc === dc) || ['dev'])[0]; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
@@ -215,3 +237,6 @@ } | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const { service: serviceId, dc } = apiArgs.options; | ||
const { | ||
service: serviceId, | ||
dc, | ||
} = apiArgs.options; | ||
if (!dc) { | ||
@@ -229,3 +254,8 @@ const agentOptions = getAgentOptionsByServiceID(serviceId); | ||
// ### GET https://<context.host>:<context.port>/v1/health/service/<apiArgs.options.serviceId>?passing=true&dc=<apiArgs.options.dc || context.dc> | ||
const result = await this.consulHealthService({ options: { service: serviceName, passing: true } }); | ||
const result = await this.consulHealthService({ | ||
options: { | ||
service: serviceName, | ||
passing: true, | ||
}, | ||
}); | ||
logger.debug(`No info about service ID ${cyan}${serviceName}`); | ||
@@ -251,3 +281,6 @@ return result?.[0]?.Service; | ||
const { Address = result[0].Node?.Node, Port } = (result[0].Service || {}) as IConsulServiceInfo; | ||
const { | ||
Address = result[0].Node?.Node, | ||
Port, | ||
} = (result[0].Service || {}) as IConsulServiceInfo; | ||
@@ -286,9 +319,13 @@ const host = await getFQDNCached(Address); | ||
if (healthServiceInfo) { | ||
const nodeHost = (healthServiceInfo.Node?.Node || '').toLowerCase().split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions).find(([, aOpt]) => aOpt.host.toLowerCase().startsWith(nodeHost)) || []; | ||
const nodeHost = (healthServiceInfo.Node?.Node || '').toLowerCase() | ||
.split('.')[0] || ''; | ||
const [agentType = 'reg'] = Object.entries(fullConsulAgentOptions) | ||
.find(([, aOpt]) => aOpt.host.toLowerCase() | ||
.startsWith(nodeHost)) || []; | ||
// @ts-ignore | ||
apiArgs.consulInstance = consulInstances[agentType]; | ||
const isDeregister = await this.agentServiceDeregister(serviceId, apiArgs); | ||
const m = (wasnt: string = '') => `Previous registration of service '${cyan}${serviceId}${reset}'${ | ||
wasnt} removed from consul agent ${blue}${fullConsulAgentOptions[agentType].host}${reset}`; | ||
// @ts-ignore | ||
const m = (wasnt: string = '') => `Previous registration of service '${cyan}${serviceId}${reset}'${wasnt} removed from consul agent ${blue}${fullConsulAgentOptions[agentType].host}${reset}`; | ||
if (isDeregister) { | ||
@@ -381,5 +418,8 @@ logger.info(m()); | ||
const value = await prepareConsulAPI(clOptions); | ||
consulApiCache[hash] = { created: Date.now(), value }; | ||
consulApiCache[hash] = { | ||
created: Date.now(), | ||
value, | ||
}; | ||
} | ||
return consulApiCache[hash].value; | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15
4681
314517