UNPKG

@xcrap/impit-client

Version:

Xcrap Impit Client is a package within the Xcrap framework that implements an HTTP client using the Impit library.

88 lines (87 loc) 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImpitClient = void 0; const core_1 = require("@xcrap/core"); const impit_1 = require("impit"); class ImpitClient extends core_1.BaseClient { constructor(options = {}) { super(options); const impitOptions = { browser: options.browser, ignoreTlsErrors: options.ignoreTlsErrors, vanillaFallback: options.vanillaFallback, timeout: options.timeout, http3: options.http3, followRedirects: options.followRedirects, maxRedirects: options.maxRedirects, cookieJar: options.cookieJar, headers: options.headers, }; this.impit = new impit_1.Impit(impitOptions); } async fetch({ url, maxRetries = 0, retries = 0, retryDelay, method = "GET", headers, ...options }) { const failedAttempts = []; const attemptRequest = async (currentRetry) => { var _a, _b, _c, _d, _e; try { const fullUrl = this.currentProxyUrl ? `${this.currentProxyUrl}${url}` : url; const requestHeaders = new Headers(headers); requestHeaders.set("User-Agent", (_a = this.currentUserAgent) !== null && _a !== void 0 ? _a : core_1.defaultUserAgent); const response = await this.impit.fetch(fullUrl, { ...options, method: method, headers: requestHeaders, }); if (!this.isSuccess(response.status)) { throw new core_1.InvalidStatusCodeError(response.status); } return new core_1.HttpResponse({ status: response.status, statusText: response.statusText, headers: response.headers, body: await response.text(), attempts: currentRetry + 1, failedAttempts: failedAttempts, }); } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; failedAttempts.push({ error: errorMessage, timestamp: new Date() }); if (currentRetry < maxRetries) { if (retryDelay !== undefined && retryDelay > 0) { await (0, core_1.delay)(retryDelay); } return await attemptRequest(currentRetry + 1); } return new core_1.HttpResponse({ status: ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) || 500, statusText: ((_c = error.response) === null || _c === void 0 ? void 0 : _c.statusText) || "Request Failed", body: ((_d = error.response) === null || _d === void 0 ? void 0 : _d.data) || errorMessage, headers: ((_e = error.response) === null || _e === void 0 ? void 0 : _e.headers) || {}, attempts: currentRetry + 1, failedAttempts: failedAttempts, }); } }; return attemptRequest(retries); } async fetchMany({ requests, concurrency, requestDelay }) { const results = []; const executing = []; for (let i = 0; i < requests.length; i++) { const promise = this.executeRequest({ request: requests[i], index: i, requestDelay: requestDelay, results: results }).then(() => undefined); executing.push(promise); if (this.shouldThrottle(executing, concurrency)) { await this.handleConcurrency(executing); } } await Promise.all(executing); return results; } } exports.ImpitClient = ImpitClient;