{"size": 2081, "ext": "js", "max_stars_repo_path": "App/node_modules/firebase-tools/lib/operation-poller.js", "max_stars_repo_name": "RetoeGo/Stuby", "max_stars_repo_stars_event_min_datetime": "2021-11-14T10:16:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-14T10:16:42.000Z", "max_issues_repo_path": "App/node_modules/firebase-tools/lib/operation-poller.js", "max_issues_repo_name": "RetoeGo/Stuby", "max_issues_repo_issues_event_min_datetime": "2021-07-16T14:11:20.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-21T01:55:22.000Z", "max_forks_repo_path": "node_modules/firebase-tools/lib/operation-poller.js", "max_forks_repo_name": "MohammedAbidNafi/Wi-Fi-QR-CODE-GENERATOR", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 41, "max_forks_count": null, "avg_line_length": 35.2711864407, "max_line_length": 97, "alphanum_fraction": 0.5790485344}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pollOperation = exports.OperationPoller = void 0;
const apiv2_1 = require("./apiv2");
const error_1 = require("./error");
const queue_1 = require("./throttler/queue");
const DEFAULT_INITIAL_BACKOFF_DELAY_MILLIS = 250;
const DEFAULT_MASTER_TIMEOUT_MILLIS = 30000;
class OperationPoller {
    async poll(options) {
        const queue = new queue_1.Queue({
            name: options.pollerName || "LRO Poller",
            concurrency: 1,
            retries: Number.MAX_SAFE_INTEGER,
            backoff: options.backoff || DEFAULT_INITIAL_BACKOFF_DELAY_MILLIS,
        });
        const masterTimeout = options.masterTimeout || DEFAULT_MASTER_TIMEOUT_MILLIS;
        const { response, error } = await queue.run(this.getPollingTask(options), masterTimeout);
        queue.close();
        if (error) {
            throw error instanceof error_1.FirebaseError
                ? error
                : new error_1.FirebaseError(error.message, { status: error.code });
        }
        return response;
    }
    getPollingTask(options) {
        const apiClient = new apiv2_1.Client({
            urlPrefix: options.apiOrigin,
            apiVersion: options.apiVersion,
            auth: true,
        });
        return async () => {
            let res;
            try {
                res = await apiClient.get(options.operationResourceName);
            }
            catch (err) {
                if (err.status === 500 || err.status === 503) {
                    throw err;
                }
                return { error: err };
            }
            if (options.onPoll) {
                options.onPoll(res.body);
            }
            if (!res.body.done) {
                throw new Error("Polling incomplete, should trigger retry with backoff");
            }
            return res.body;
        };
    }
}
exports.OperationPoller = OperationPoller;
function pollOperation(options) {
    return new OperationPoller().poll(options);
}
exports.pollOperation = pollOperation;
