af-consul
Version:
A highly specialized function library
55 lines • 2.81 kB
JavaScript
/* eslint-disable no-console */
// noinspection JSUnusedGlobalSymbols
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAPI = exports.apiCache = exports.accessPointsUpdater = void 0;
const prepare_consul_api_1 = require("./prepare-consul-api");
const constants_1 = require("./constants");
const hash_1 = require("./lib/hash");
const get_register_config_1 = require("./get-register-config");
const utils_1 = require("./lib/utils");
const cyclic_register_1 = require("./cyclic-register");
const color_1 = require("./lib/color");
var access_points_updater_1 = require("./access-points/access-points-updater");
Object.defineProperty(exports, "accessPointsUpdater", { enumerable: true, get: function () { return access_points_updater_1.accessPointsUpdater; } });
const defaultGetConsulUIAddress = (serviceId) => {
const { NODE_ENV, CONSUL_UI_HOST, CONSUL_DC_PROD, CONSUL_DC_DEV } = process.env;
const p = NODE_ENV === 'production';
return `https://${CONSUL_UI_HOST || ''}/ui/dc-${p ? (CONSUL_DC_PROD || 'prd') : (CONSUL_DC_DEV || 'dev')}/services/${serviceId}/instances`;
};
const debug = (msg) => {
if (constants_1.CONSUL_DEBUG_ON) {
console.log(`${color_1.magenta}${constants_1.PREFIX}${color_1.reset}: ${msg}`);
}
};
// cached
exports.apiCache = {};
const getAPI = async (options) => {
var _a;
const hash = (0, hash_1.getConfigHash)(options);
if (!exports.apiCache[hash]) {
const api = await (0, prepare_consul_api_1.getConsulApiCached)(options);
const registerConfig = await (0, get_register_config_1.getRegisterConfig)(options);
const serviceId = registerConfig.id;
(0, utils_1.minimizeCache)(exports.apiCache, constants_1.MAX_API_CACHED);
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.info(`Consul UI: ${(options.getConsulUIAddress || defaultGetConsulUIAddress)(serviceId)}`);
debug(`${color_1.yellow} REGISTER CONFIG:\n${JSON.stringify(registerConfig, undefined, 2)}\n${color_1.reset}`);
const value = {
registerConfig,
serviceId,
register: {
once: async (registerType = 'if-not-registered') => api.registerService(registerConfig, { registerType }),
cyclic: (0, cyclic_register_1.getRegisterCyclic)(options, api, registerConfig),
},
deregister: (svcId, agentOptions) => api.deregisterIfNeed(svcId || serviceId, agentOptions),
};
Object.entries(api).forEach(([k, v]) => {
// @ts-ignore
value[k] = typeof v === 'function' ? v.bind(api) : v;
});
exports.apiCache[hash] = { created: Date.now(), value };
}
return exports.apiCache[hash].value;
};
exports.getAPI = getAPI;
//# sourceMappingURL=get-api.js.map
;