/** * Launching and cancelling detached downloads. * * `startDownload` spawns the runner so it outlives the calling command. * Verified: a parent that calls `process.exit(0)` immediately after spawning * still sees the child complete a multi-megabyte transfer. * * Cancellation has to reach the curl grandchild, not just the runner — killing * the runner alone orphans curl, which keeps writing to a file nobody tracks. * The two platforms need different mechanisms for that: * * POSIX `detached: true` makes the runner a process-group leader, so * `process.kill(-pid)` signals the whole group. The runner catches * SIGTERM and records `cancelled` itself. * Windows There are no process groups and a negative pid is invalid; also * `detached` there means "new console window", which would flash a * black box at the user. So the child is merely `unref()`d and * cancellation goes through `taskkill /T`, which walks the tree. * Nothing catchable is delivered, so `killDownload` records the * terminal status on the runner's behalf. */ import { type DownloadState, type DownloadStatus, type StatusBase } from "./status"; export interface StartDownloadOptions { /** Stable identifier. Defaults to a random UUID. Reuse it to resume. */ id?: string; url: string; /** Final destination. Bytes land in `.part` until verified. */ outputPath: string; filename?: string; headers?: Record; /** Known size, used for progress and to verify completeness. */ expectedBytes?: number; /** * How to treat a size mismatch against `expectedBytes`. * * `"strict"` (default) fails the download. Correct when the size comes from * the same authority that minted the URL — an API response, say. * * `"advisory"` uses the size for progress but publishes the file anyway. * Correct when the size came from a separate `HEAD` request, which can * legitimately disagree with the `GET`: gzip transfer-encoding, a stale * `content-length`, or a server that serves different bodies to each verb. * Strict checking there would reject perfectly good downloads. */ sizeCheck?: "strict" | "advisory"; /** Continue from an existing partial file via HTTP Range. Default true. */ resume?: boolean; /** * Follow HTTP redirects (curl `--location`). Default true. * * Turning it off is a real choice, not a no-op: an unfollowed 3xx is then a * FAILURE, because the body curl writes is the redirect stub, not the file. */ followRedirects?: boolean; speedLimitBytes?: number; stallSeconds?: number; /** Cap transfer rate in bytes/sec. Actually slows the download. */ limitRateBytes?: number; /** Opaque payload echoed into the status file. NEVER put a signed URL here. */ meta?: Record; statusDir?: string; /** Override the runner path (tests). Defaults to the sibling `runner.js`. */ runnerPath?: string; /** * Post a desktop notification when the download finishes or fails. * * This exists because of the gap detaching creates: once the window closes, * the toast is gone, and a completed transfer has nothing left to report to. * The runner outlives the command, so it is the only thing that can speak. * * See `RunnerPayload.notifyOnFinish` in `runner.ts` for the delivery routes * and why `raycastDeeplink` is opt-in. */ notifyOnFinish?: { title: string; enabled: boolean; raycastDeeplink?: boolean; }; } export interface DownloadTicket { id: string; pid: number; statusPath: string; } /** * Absolute path to the runner, searching the locations above. * * Returns the conventional path when nothing is found, so the caller's error * names a concrete location rather than an empty string. */ export declare function runnerPath(): string; /** The locations `runnerPath()` searched — for a diagnosable error message. */ export declare function runnerSearchPaths(): string[]; /** * Spawn a detached download. Resolves once the runner has written its first * status, so the caller can immediately watch a file that exists. */ export declare function startDownload(options: StartDownloadOptions): Promise; /** What a launcher-written status adds to the seed: a failure, when there was one. */ type StatusExtra = Pick; /** * The runner to spawn, or a `validation` error naming every place searched. * * @internal Shared by `startDownload` and `startCommand`. */ export declare function existingRunner(override?: string): string; /** @internal */ export interface RunnerLaunch { id: string; statusDir: string; runner: string; /** The runner's payload JSON. Delivered on stdin, never argv or a file. */ payload: string; /** The status to write on this attempt's behalf: a spawn failure, or the seed. */ statusFor: (pid: number, state: DownloadState, extra?: StatusExtra) => DownloadStatus; /** The runner could not be started. May fire after `launchRunner` has thrown. */ onSpawnFailed?: () => void; /** The runner has a pid. */ onSpawned?: (pid: number) => void; } /** * Spawn the detached runner, deliver its payload, and wait for its first * status. Resolves once there is a status file to watch. * * @internal Shared by `startDownload` and `startCommand`, which differ only in * the payload and the status shape. Everything here is about the process. */ export declare function launchRunner(launch: RunnerLaunch): Promise; export interface KillOptions { statusDir?: string; /** Escalate to SIGKILL if the group is still alive after this long. */ graceMs?: number; } /** * Cancel a download by signalling its process group. * * Refuses to signal when the recorded `(pid, startTime)` no longer matches a * live process: macOS `kern.maxproc` is 16000, so a stale pid can belong to an * unrelated process, and killing its whole group would be a serious bug. * * Returns true when a signal was delivered. */ export declare function killDownload(ticket: { id: string; pid?: number; }, options?: KillOptions): Promise; /** * Reconcile a status whose runner is no longer alive. * * The important case is `finalizing`: the runner renamed the file into place and * died before recording completion. The bytes are on disk and correct, so * reporting a failure — and offering to re-download hundreds of megabytes — * would be wrong. */ export declare function reconcile(status: DownloadStatus, statusDir?: string): DownloadStatus; export {}; //# sourceMappingURL=detach.d.ts.map