UNPKG

@tidecloak/js

Version:

TideCloak client side JS SDK

87 lines 3.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ClientBase_js_1 = __importDefault(require("./ClientBase.js")); const Serialization_js_1 = require("../Cryptide/Serialization.js"); const Utils_js_1 = require("../Tools/Utils.js"); class PollingClient extends ClientBase_js_1.default { constructor(url) { super(url); this.stopPolling = false; } /** * @param {string[]} ids */ async EstablishHttpTunnel(uid, ids) { const data = this._createFormData({ 'orkIds': ids.map(n => n.toString()), }); // Establish the connection var channelId = ""; const response = await this._post(`/Authentication/AccountRecovery/EstablishConnection?aruid=${uid}`, data); const responseData = await this._handleError(response, "EstablishConnection"); if (responseData.split(":")[0] === "--CONNECTED--") { channelId = responseData.split(":")[1]; } else { throw Error("orks.couldNotEstablishAConnection"); } return channelId; } /** * @param {bigint} channelId */ async pollServer(channelId, uiUpdateCallback, signal) { const cId = channelId.toString(); // Parameters for retry if server is down. const maxRetries = 10; // Maximum number of retries let retryCount = 0; const retryInterval = 5000; // 5 seconds between retries const maxRetryDuration = 5 * 60 * 1000; // 5 minutes total retry duration.. const startTime = Date.now(); while (!this.stopPolling) { try { const response = await this._get(`/Authentication/AccountRecovery/WaitForUpdates?channelId=${cId}`, 60000, signal); // update timeout to something more appropriate if (!response.ok) throw new Error("ORK did not return status OK"); const responseData = await response.json(); if (responseData.status === "PING") { continue; } if (responseData.status === "CANCELLED") { return { status: "cancelled" }; } if (responseData.status === "REJECTED") { throw new Error(responseData.message); } if (responseData.status === "IN PROGRESS") { uiUpdateCallback(responseData.key); continue; } if (responseData.status === "RECOVERY") { const encRequests = responseData.encRequest; const bitwise = (0, Serialization_js_1.deserializeBitArray)((0, Serialization_js_1.base64ToBytes)(responseData.bitwise)); if (encRequests.length < Utils_js_1.Threshold) break; return { encRequests, bitwise, status: "recovered" }; } } catch (error) { retryCount++; if (retryCount >= maxRetries || (Date.now() - startTime) >= maxRetryDuration) { throw new Error("orks.pollingTimedOut"); } await new Promise(resolve => setTimeout(resolve, retryInterval)); continue; } } return { status: "cancelled" }; } stop() { this.stopPolling = true; } } exports.default = PollingClient; //# sourceMappingURL=PollingClient.js.map