UNPKG

vlt

Version:
354 lines (346 loc) 11.2 kB
var global = globalThis; import {Buffer} from "node:buffer"; import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers"; import {createRequire as _vlt_createRequire} from "node:module"; var require = _vlt_createRequire(import.meta.filename); import { __commonJS, __toESM } from "./chunk-AECDW3EJ.js"; // ../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry_operation.js var require_retry_operation = __commonJS({ "../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry_operation.js"(exports, module) { function RetryOperation(timeouts, options) { if (typeof options === "boolean") { options = { forever: options }; } this._originalTimeouts = JSON.parse(JSON.stringify(timeouts)); this._timeouts = timeouts; this._options = options || {}; this._maxRetryTime = options && options.maxRetryTime || Infinity; this._fn = null; this._errors = []; this._attempts = 1; this._operationTimeout = null; this._operationTimeoutCb = null; this._timeout = null; this._operationStart = null; this._timer = null; if (this._options.forever) { this._cachedTimeouts = this._timeouts.slice(0); } } module.exports = RetryOperation; RetryOperation.prototype.reset = function() { this._attempts = 1; this._timeouts = this._originalTimeouts.slice(0); }; RetryOperation.prototype.stop = function() { if (this._timeout) { clearTimeout(this._timeout); } if (this._timer) { clearTimeout(this._timer); } this._timeouts = []; this._cachedTimeouts = null; }; RetryOperation.prototype.retry = function(err) { if (this._timeout) { clearTimeout(this._timeout); } if (!err) { return false; } var currentTime = (/* @__PURE__ */ new Date()).getTime(); if (err && currentTime - this._operationStart >= this._maxRetryTime) { this._errors.push(err); this._errors.unshift(new Error("RetryOperation timeout occurred")); return false; } this._errors.push(err); var timeout = this._timeouts.shift(); if (timeout === void 0) { if (this._cachedTimeouts) { this._errors.splice(0, this._errors.length - 1); timeout = this._cachedTimeouts.slice(-1); } else { return false; } } var self = this; this._timer = setTimeout(function() { self._attempts++; if (self._operationTimeoutCb) { self._timeout = setTimeout(function() { self._operationTimeoutCb(self._attempts); }, self._operationTimeout); if (self._options.unref) { self._timeout.unref(); } } self._fn(self._attempts); }, timeout); if (this._options.unref) { this._timer.unref(); } return true; }; RetryOperation.prototype.attempt = function(fn, timeoutOps) { this._fn = fn; if (timeoutOps) { if (timeoutOps.timeout) { this._operationTimeout = timeoutOps.timeout; } if (timeoutOps.cb) { this._operationTimeoutCb = timeoutOps.cb; } } var self = this; if (this._operationTimeoutCb) { this._timeout = setTimeout(function() { self._operationTimeoutCb(); }, self._operationTimeout); } this._operationStart = (/* @__PURE__ */ new Date()).getTime(); this._fn(this._attempts); }; RetryOperation.prototype.try = function(fn) { console.log("Using RetryOperation.try() is deprecated"); this.attempt(fn); }; RetryOperation.prototype.start = function(fn) { console.log("Using RetryOperation.start() is deprecated"); this.attempt(fn); }; RetryOperation.prototype.start = RetryOperation.prototype.try; RetryOperation.prototype.errors = function() { return this._errors; }; RetryOperation.prototype.attempts = function() { return this._attempts; }; RetryOperation.prototype.mainError = function() { if (this._errors.length === 0) { return null; } var counts = {}; var mainError = null; var mainErrorCount = 0; for (var i = 0; i < this._errors.length; i++) { var error = this._errors[i]; var message = error.message; var count = (counts[message] || 0) + 1; counts[message] = count; if (count >= mainErrorCount) { mainError = error; mainErrorCount = count; } } return mainError; }; } }); // ../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry.js var require_retry = __commonJS({ "../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/lib/retry.js"(exports) { var RetryOperation = require_retry_operation(); exports.operation = function(options) { var timeouts = exports.timeouts(options); return new RetryOperation(timeouts, { forever: options && (options.forever || options.retries === Infinity), unref: options && options.unref, maxRetryTime: options && options.maxRetryTime }); }; exports.timeouts = function(options) { if (options instanceof Array) { return [].concat(options); } var opts = { retries: 10, factor: 2, minTimeout: 1 * 1e3, maxTimeout: Infinity, randomize: false }; for (var key in options) { opts[key] = options[key]; } if (opts.minTimeout > opts.maxTimeout) { throw new Error("minTimeout is greater than maxTimeout"); } var timeouts = []; for (var i = 0; i < opts.retries; i++) { timeouts.push(this.createTimeout(i, opts)); } if (options && options.forever && !timeouts.length) { timeouts.push(this.createTimeout(i, opts)); } timeouts.sort(function(a, b) { return a - b; }); return timeouts; }; exports.createTimeout = function(attempt, opts) { var random = opts.randomize ? Math.random() + 1 : 1; var timeout = Math.round(random * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt)); timeout = Math.min(timeout, opts.maxTimeout); return timeout; }; exports.wrap = function(obj, options, methods) { if (options instanceof Array) { methods = options; options = null; } if (!methods) { methods = []; for (var key in obj) { if (typeof obj[key] === "function") { methods.push(key); } } } for (var i = 0; i < methods.length; i++) { var method = methods[i]; var original = obj[method]; obj[method] = function retryWrapper(original2) { var op = exports.operation(options); var args = Array.prototype.slice.call(arguments, 1); var callback = args.pop(); args.push(function(err) { if (op.retry(err)) { return; } if (err) { arguments[0] = op.mainError(); } callback.apply(this, arguments); }); op.attempt(function() { original2.apply(obj, args); }); }.bind(obj, original); obj[method].options = options; } }; } }); // ../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/index.js var require_retry2 = __commonJS({ "../../node_modules/.pnpm/retry@0.13.1/node_modules/retry/index.js"(exports, module) { module.exports = require_retry(); } }); // ../../node_modules/.pnpm/p-retry@6.2.1/node_modules/p-retry/index.js var import_retry = __toESM(require_retry2(), 1); // ../../node_modules/.pnpm/is-network-error@1.1.0/node_modules/is-network-error/index.js var objectToString = Object.prototype.toString; var isError = (value) => objectToString.call(value) === "[object Error]"; var errorMessages = /* @__PURE__ */ new Set([ "network error", // Chrome "Failed to fetch", // Chrome "NetworkError when attempting to fetch resource.", // Firefox "The Internet connection appears to be offline.", // Safari 16 "Load failed", // Safari 17+ "Network request failed", // `cross-fetch` "fetch failed", // Undici (Node.js) "terminated" // Undici (Node.js) ]); function isNetworkError(error) { const isValid = error && isError(error) && error.name === "TypeError" && typeof error.message === "string"; if (!isValid) { return false; } if (error.message === "Load failed") { return error.stack === void 0; } return errorMessages.has(error.message); } // ../../node_modules/.pnpm/p-retry@6.2.1/node_modules/p-retry/index.js var AbortError = class extends Error { constructor(message) { super(); if (message instanceof Error) { this.originalError = message; ({ message } = message); } else { this.originalError = new Error(message); this.originalError.stack = this.stack; } this.name = "AbortError"; this.message = message; } }; var decorateErrorWithCounts = (error, attemptNumber, options) => { const retriesLeft = options.retries - (attemptNumber - 1); error.attemptNumber = attemptNumber; error.retriesLeft = retriesLeft; return error; }; async function pRetry(input, options) { return new Promise((resolve, reject) => { options = { ...options }; options.onFailedAttempt ??= () => { }; options.shouldRetry ??= () => true; options.retries ??= 10; const operation = import_retry.default.operation(options); const abortHandler = () => { operation.stop(); reject(options.signal?.reason); }; if (options.signal && !options.signal.aborted) { options.signal.addEventListener("abort", abortHandler, { once: true }); } const cleanUp = () => { options.signal?.removeEventListener("abort", abortHandler); operation.stop(); }; operation.attempt(async (attemptNumber) => { try { const result = await input(attemptNumber); cleanUp(); resolve(result); } catch (error) { try { if (!(error instanceof Error)) { throw new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`); } if (error instanceof AbortError) { throw error.originalError; } if (error instanceof TypeError && !isNetworkError(error)) { throw error; } decorateErrorWithCounts(error, attemptNumber, options); if (!await options.shouldRetry(error)) { operation.stop(); reject(error); } await options.onFailedAttempt(error); if (!operation.retry(error)) { throw operation.mainError(); } } catch (finalError) { decorateErrorWithCounts(finalError, attemptNumber, options); cleanUp(); reject(finalError); } } }); }); } export { AbortError, pRetry }; //# sourceMappingURL=chunk-M34JYYXI.js.map