/** * Fetches remote media through DNS-pinned SSRF protection, enforces byte * limits, and derives safe filenames and MIME metadata. */ import { type PinnedLookupFetchLike } from "../network/fetch-guard.js"; import type { LookupFn, SsrfPolicy } from "../network/ssrf.js"; export type FetchMediaResult = { buffer: Buffer; contentType?: string; fileName?: string; }; export type MediaFetchErrorCode = "max_bytes" | "http_error" | "fetch_failed" | "invalid_response"; export declare class MediaFetchError extends Error { readonly code: MediaFetchErrorCode; constructor(code: MediaFetchErrorCode, message: string, cause?: unknown); } export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise; export declare const DEFAULT_MEDIA_FETCH_TIMEOUT_MS = 10000; export type FetchMediaOptions = { url: string; fetchImpl?: FetchLike; filePathHint?: string; /** Transparent application identification for hosts that require a named client. */ userAgent?: string; maxBytes?: number; maxRedirects?: number; timeoutMs?: number; /** Caller abort signal — composed with the timeout deadline via AbortSignal.any. */ signal?: AbortSignal; /** Require the declared response MIME type to start with this prefix. */ requiredContentTypePrefix?: string; /** Reject Content-Encoding other than identity when callers need raw media bytes. */ rejectContentEncoding?: boolean; ssrfPolicy?: SsrfPolicy; lookupFn?: LookupFn; pinnedFetchImpl?: PinnedLookupFetchLike; }; export declare function readErrorBodySnippet(res: Response, maxChars?: number): Promise; /** * Fetch remote media with SSRF protection. * * @param options - Fetch options * @returns Promise resolving to buffer, content type, and filename * @throws MediaFetchError on fetch failures */ export declare function fetchRemoteMedia(options: FetchMediaOptions): Promise; /** * Reads a response body under a hard byte cap, cancelling the stream as soon * as the running total exceeds maxBytes instead of materializing the payload * first — so a missing or lying Content-Length can never force an unbounded * allocation. Shared by the remote media fetcher above and the trusted local * attachment byte-fetches (ingest enrichment and on-demand transcription). * Falls back to a post-read check only when the response exposes no stream. * Throws MediaFetchError("max_bytes") on overflow. */ export declare function readResponseWithLimit(res: Response, maxBytes: number): Promise;