af-consul
Version:
A highly specialized function library
55 lines • 2.45 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAccessPointAvailability = exports.isHttpAvailable = void 0;
const http = __importStar(require("http"));
const https = __importStar(require("https"));
const constants_1 = require("../constants");
const isHttpAvailable = (url) => new Promise((resolve) => {
const client = /^https:/i.test(url) ? https : http;
client.request(url, (r) => resolve(r.statusCode > 0)).on('error', () => resolve(false)).end();
});
exports.isHttpAvailable = isHttpAvailable;
const checkAccessPointAvailability = async (accessPoints, accessPointId, onError) => {
const it = `Access point "${accessPointId}"`;
const ap = accessPoints.getAP(accessPointId);
if (!ap) {
return onError(`${it} is not found`);
}
if (!ap.waitForHostPortUpdated) {
return onError(`${it} has no method "waitForHostPortUpdated"`);
}
if (!(await ap.waitForHostPortUpdated(constants_1.CONSUL_AP_UPDATE_TIMEOUT_MILLIS))) {
return onError(`${it} update timed out`);
}
const { host, port, protocol = 'http', path = '' } = ap;
const url = `${protocol}://${host}:${port}${path}`;
if (!(await (0, exports.isHttpAvailable)(url))) {
return onError(`${it} is not available`);
}
return true;
};
exports.checkAccessPointAvailability = checkAccessPointAvailability;
//# sourceMappingURL=access-points-utils.js.map
;