UNPKG

infobip-rtc

Version:

Infobip RTC JavaScript SDK - Infobip WebRTC API Implementation

58 lines 2.01 kB
export class PortunusHostEvaluator { constructor(logger) { this.logger = logger; } getPortunusHosts(token) { if (token.locations && token.locations.length > 0) { return token.locations.map((loc) => this.generatePortunusHost(loc)); } const hosts = []; hosts.push(this.generatePortunusHost(token.location)); const altLocation = token.altLocation; if (altLocation) { hosts.push(this.generatePortunusHost(altLocation)); } return hosts; } generatePortunusHost(location) { if (!location) { return "portunus.infobip.com"; } if (location === "fr4") { return "portunus.infobip.net"; } if (["iop1", "iop2"].includes(location)) { return `portunus-${location}.ioinfobip.com`; } return `portunus-${location}.infobip.com`; } async evaluate(token) { const hosts = this.getPortunusHosts(token); let firstHost = hosts[0]; if (hosts.length === 1) { return firstHost; } return Promise.any(hosts.map(host => this.checkIfAvailable(host))) .catch(() => { this.logger.warn(`All Portunus hosts are unavailable, defaulting to the first one: ${firstHost}`); return firstHost; }); } async checkIfAvailable(portunusHost) { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 3000); try { const response = await fetch(`https://${portunusHost}/status`, { signal: controller.signal }); if (response.ok) { return portunusHost; } let message = `Host ${portunusHost} not available`; this.logger.warn(message); throw new Error(message); } finally { clearTimeout(timeoutId); } } } //# sourceMappingURL=PortunusHostEvaluator.js.map