/** * @file Download helpers for dlx binaries — fetch tarballs from URLs, verify * integrity, cache to disk with concurrency locking. * * - `downloadBinary` — high-level URL→cached-binary flow * - `downloadBinaryFile` — low-level fetch+verify with processLock Split out of * `dlx/binary.ts` for size hygiene. */ import type { DlxBinaryOptions } from './binary-types.js'; import type { HttpDownloadWriteStreamFactory } from '../http-request/download-types.js'; /** * Download a binary from a URL with caching, without executing it. Similar to * downloadNpmPackage from dlx/package. * * Returns `{binaryPath, downloaded, integrity}`. The `integrity` field is the * SRI-formatted `sha512-` hash of the cached file — computed by the * downloader on first fetch, persisted to cache metadata, and re-read on * subsequent cache hits. This supports the trust-on-first-use pattern: * * // 1. First call — caller has no pinned integrity yet. const { integrity } = * await downloadBinary({ url, name: 'tool' }) // Caller writes `integrity` back * to external-tools.json or similar. * * // 2. Subsequent calls — caller pins the integrity for verification. await * downloadBinary({ url, name: 'tool', hash: integrity }) * * @example * ```typescript * const { binaryPath, integrity } = await downloadBinary({ * url: 'https://example.com/tool-linux-x64', * name: 'tool', * }) * console.log(`Binary at: ${binaryPath}, pin: ${integrity}`) * ``` * * @returns `{binaryPath, downloaded, integrity}` — binary location, whether * this call fetched (vs. cache-hit), and the computed SRI integrity for * future pinning. */ export declare function downloadBinary(options: Omit): Promise<{ binaryPath: string; downloaded: boolean; integrity: string; }>; export interface DownloadBinaryFileOptions { /** * Factory for the destination write stream. Injected by tests and by callers * that need to tee or throttle the download. */ createWriteStream?: HttpDownloadWriteStreamFactory | undefined; /** * Extra request headers, forwarded to `httpDownload`. Needed for an asset * behind auth: a private GitHub release answers an unauthenticated request * with a bare 404, which reads as "never published" rather than "not * allowed". */ headers?: Record | undefined; /** * SRI string (`sha512-`) the download must match, verified from the * response stream before the temp file is atomically published. */ integrity?: string | undefined; /** * Hex SHA-256 the download must match, verified from the same stream. */ sha256?: string | undefined; } /** * Download a file from a URL with integrity checking and concurrent download * protection. Uses processLock to prevent multiple processes from downloading * the same binary simultaneously. * * Supports two integrity verification methods: * * - Sha256: Hex SHA-256 checksum (verified from the download stream via * httpDownload) * - Integrity: SRI format sha512-, verified from the same stream * * Both checks happen before the download temp is atomically published. * * @example * ```typescript * const integrity = await downloadBinaryFile( * 'https://example.com/tool-linux-x64', * '/tmp/dlx-cache/tool', * { integrity: 'sha512-…' }, * ) * console.log(`Integrity: ${integrity}`) * ``` */ export declare function downloadBinaryFile(url: string, destPath: string, options?: DownloadBinaryFileOptions | undefined): Promise; export interface BinaryFileDigests { integrity: string; sha256: string; } /** * Compute both DLX digests in one bounded-memory file pass. */ export declare function hashBinaryFile(filePath: string): Promise; /** * Check a cached binary against the caller's pin. * * A cache HIT skips the download, and with it every check the download * performs, so a caller that pinned a hash needs the on-disk bytes measured * here or the pin governs only the first fetch on each machine. Anything able * to write the dlx cache would otherwise reach execution. Fails closed: the * mismatching file is deleted so the next call fetches again, and the caller * sees the throw. * * Returns the digests it computed so a caller doing trust-on-first-use can * reuse them instead of hashing the file twice. With no pin supplied there is * nothing to compare, and the digests are still returned. */ export declare function verifyCachedBinary(binaryPath: string, options?: { integrity?: string | undefined; sha256?: string | undefined; } | undefined): Promise;