import { type AxiosAdapter } from 'axios'; import type { WithLogger } from './types.ts'; /** * Tuning for the rate-limited fetch wrapper. * - `maxRetries`: attempts on transient (429/5xx) responses. * - `seed`: optional proactive starting cap for hosts known to always throttle * (e.g. TON public). When set, the default scope starts ACTIVE at this rate * instead of full speed. It still adapts (relaxes up / tightens down). */ export type RateLimitOpts = { maxRetries: number; /** Max concurrent in-flight requests per endpoint (default 5). */ maxInFlight?: number; seed?: { limit: number; windowMs: number; }; }; /** Derive a stable key from a fetch input (string | URL | Request). */ export declare function endpointKey(input: Parameters[0]): string; /** * Parses a Retry-After header value into an epoch-ms wait-until time. * Handles both delta-seconds (integer) and HTTP-date formats. * @param value - The raw header value. * @returns Epoch-ms when retry is allowed, or null if unparseable. */ export declare function parseRetryAfter(value: string | null): number | null; /** Parsed rate-limit header information. */ export interface ParsedRateLimitHeaders { /** Remaining allowed requests in the current window. */ remaining?: number; /** Total limit for the window. */ limit?: number; /** Epoch-ms when the window resets. */ resetAt?: number; /** Epoch-ms when retry is allowed (from Retry-After). */ retryAfterAt?: number; } /** * Parses standard and de-facto rate-limit response headers. * * Supports: * - `Retry-After`: delta-seconds or HTTP-date * - IETF draft: `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset` (delta-seconds) * - Combined `RateLimit:` header (e.g. `limit=100, remaining=50, reset=10`) * - De-facto: `X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Reset` * (reset heuristic: if value \> (now/1000 - 1e9) treat as epoch-seconds, else delta-seconds) * @param headers - Response headers. * @returns Parsed rate-limit info. */ export declare function parseRateLimitHeaders(headers: Headers): ParsedRateLimitHeaders; /** * Returns starting rate-limit opts for a host. Most hosts get NO proactive seed * (they start at full speed and only adapt if they actually return 429s). Known * always-throttled public hosts get an informed `seed` so they start paced — but * the seed is just a starting point; the adaptive limiter still relaxes up or * tightens down from there based on observed responses. * Chain files call `createRateLimitedFetch(fetchProfileForUrl(url), ctx)`. * @param url - The endpoint URL string. * @returns Partial RateLimitOpts (optionally with a `seed`) for the host. */ export declare function fetchProfileForUrl(url: string): Partial; /** * Returns the learned getLogs max range for an endpoint, if set. * @param input - Fetch input (string, URL, or Request). * @returns Max block range, or undefined if not learned. */ export declare function getEndpointLogRange(input: Parameters[0]): number | undefined; /** * Sets the learned getLogs max range for an endpoint. * @param input - Fetch input (string, URL, or Request). * @param maxRange - The learned max block range. * @param source - Whether learned from an error or a success. */ export declare function setEndpointLogRange(input: Parameters[0], maxRange: number, source: 'error' | 'success'): void; /** * Creates a fetch wrapper that runs at full speed by default and adaptively * paces only when an endpoint actually rate-limits it. Per (endpoint, method) * limiters learn the real limit/window from response headers or observed timing, * pace to it, tighten on repeat 429s, and relax back to full speed when limits * stop. Shares learned state per endpoint across all instances. * @returns The wrapped fetch function. */ export declare function createRateLimitedFetch(opts?: Partial, { logger, abort }?: { abort?: AbortSignal; } & WithLogger): typeof fetch; /** * Creates an axios adapter that routes requests through a custom `fetch` function, * with optional `AbortSignal` propagation. * * Wraps axios's built-in `'fetch'` adapter so that all HTTP traffic goes through * the provided `fetchFn` (e.g. a rate-limited fetch). When `abort` is supplied, * it is merged (via `AbortSignal.any`) with any per-request signal already set on * the axios config, so callers don't need to thread the abort signal manually. * * @param fetchFn - The `fetch` implementation to bind (e.g. from `createRateLimitedFetch`). * @param abort - Optional `AbortSignal` to merge into every request's signal. * @returns An axios adapter ready to pass as `httpAdapter` in an axios/TonClient config. * * @example * ```typescript * const fetchFn = createRateLimitedFetch(fetchProfileForUrl(url), ctx) * const httpAdapter = createAxiosFetchAdapter(fetchFn, ctx?.abort) * const client = new TonClient({ endpoint: url, httpAdapter }) * ``` */ export declare function createAxiosFetchAdapter(fetchFn: typeof fetch, abort?: AbortSignal): AxiosAdapter; /** * Performs a fetch request with timeout and abort signal support. * * @param url - URL to fetch * @param operation - Operation name for error context * @param opts - Optional configuration: * - `timeoutMs` — request timeout in milliseconds (default: 30000). * - `signal` — an external `AbortSignal` to cancel the request. * - `fetch` — custom fetch function (defaults to `globalThis.fetch`). * - `init` — additional `RequestInit` fields merged into the fetch call. * @returns Promise resolving to Response * @throws CCIPTimeoutError if request times out * @throws CCIPAbortError if request is aborted via signal */ export declare function fetchWithTimeout(url: string, operation: string, opts?: { timeoutMs?: number; signal?: AbortSignal; fetch?: typeof globalThis.fetch; init?: RequestInit; }): Promise; /** Range error info from a getLogs "range too large" error. */ export interface LogRangeErrorInfo { /** Maximum allowed block range, if extractable from the error message. */ maxRange?: number; /** Suggested [from, to] block range in decimal, if provided by the RPC. */ suggestedRange?: [number, number]; } /** * Parses RPC errors for "getLogs block range too large" messages. * * Covers Alchemy, Infura, QuickNode, and generic EVM provider patterns. * Also checks JSON-RPC error code -32005. * * @param err - The caught error (any shape). * @returns Non-null LogRangeErrorInfo if the error is a range error, null otherwise. */ export declare function parseLogRangeError(err: unknown): LogRangeErrorInfo | null; //# sourceMappingURL=fetch.d.ts.map