/** * Download history, persisted to Raycast's LocalStorage. * * The storage backend is injectable so the logic is testable without a Raycast * host — and so a consumer with its own persistence can supply it. When omitted, * `@raycast/api`'s LocalStorage is loaded lazily (a bare `import` at module load * would make this module unusable outside Raycast, including from tests and the * detached runner). * * `meta` is the per-extension payload: Fetch stores nothing, iOS Apps stores its * AppDetails. Generic over it rather than union-typed, so no consumer has to * widen its type to accommodate another's. */ import type { DownloadErrorCode } from "./errors"; export interface DownloadRecord { id: string; filename: string; /** * Where the file is. Absent for a `startCommand` download whose output was * never resolved: the command chose the name, and nothing told the package. */ outputPath?: string; status: "completed" | "failed" | "cancelled"; /** * Source URL, when it is safe to persist. * * **The default is to persist whatever you pass, verbatim.** This module does * not inspect it and never has — an earlier version of this comment implied a * guarantee that no code backed up, which is worse than no comment at all. * * Signed URLs are bearer credentials: anyone who can read the extension's * LocalStorage can re-download with them until they expire. Keeping them out * is the CALLER's responsibility. Consumers of expiring-URL APIs should omit * this field and store a re-resolvable identifier in `meta` instead. * * If you want the library to enforce it, set `urlPolicy` on * `createDownloadHistory` — opt-in, because turning it on by default would * silently drop URLs an existing consumer relies on. */ url?: string; bytesDownloaded?: number; error?: { code: DownloadErrorCode; message: string; }; timestamp: number; meta?: M; } /** * True when a URL carries what looks like an embedded credential. * * Exported so a consumer can make the same judgement at its own call site — * e.g. to decide whether to store an identifier in `meta` instead. */ export declare function looksLikeSignedUrl(url: string): boolean; /** * What to do with `record.url` before persisting it. * * `"allow"` (the default) persists it verbatim, which is the historical * behaviour and the only one that cannot break an existing consumer. */ export type HistoryUrlPolicy = "allow" | "omit-signed" | "omit-all" | "throw-signed"; /** Minimal shape of Raycast's LocalStorage — the part this module uses. */ export interface HistoryStorage { getItem(key: string): Promise; setItem(key: string, value: string): Promise; removeItem(key: string): Promise; } export interface HistoryOptions { /** LocalStorage key. Default `"download-history"`. */ key?: string; /** Maximum rows retained, newest first. Default 100. */ limit?: number; /** * Collapse rows sharing a derived key, keeping the newest. iOS Apps passes * `r => r.meta?.bundleId` to keep one row per app rather than per download. * Return `undefined` to exempt a row from deduplication. */ dedupeBy?: (record: DownloadRecord) => string | undefined; /** Injected for tests or non-Raycast callers. Defaults to Raycast LocalStorage. */ storage?: HistoryStorage; /** * Whether to enforce that signed URLs stay out of persisted history. * Default `"allow"` — see `DownloadRecord.url`. `"omit-signed"` is the * setting a consumer of an expiring-URL API wants. */ urlPolicy?: HistoryUrlPolicy; /** * Directory for the cross-process lock file. Defaults to the package's own * status directory, which is per-extension. Override only to isolate tests. */ lockDir?: string; } export interface DownloadHistory { list(): Promise[]>; add(record: Omit, "timestamp"> & { timestamp?: number; }): Promise; addMany(records: Array, "timestamp"> & { timestamp?: number; }>): Promise; remove(id: string): Promise; clear(): Promise; clearOlderThan(ms: number): Promise; } /** * Fold finished downloads into history, then clear their status files. * * **Call this on command launch.** It closes a gap that is structural rather * than accidental: * * - The transfer completes in the DETACHED RUNNER, which cannot import * `@raycast/api` (it runs outside the Raycast host) and therefore cannot * write to LocalStorage. * - The command that *can* write to LocalStorage has usually been unloaded by * then — surviving dismissal is the whole point of the package. * * So nothing records the row at the moment the download finishes. Instead the * runner leaves a terminal status file behind, and the next command launch * reconciles it here. * * Without this, a user who starts a download and presses Escape gets the file * but no history entry — for every download they don't sit and watch. * * Idempotent: history rows are keyed by download id, and a reconciled status is * cleared, so repeat calls neither duplicate nor resurrect rows. * * @returns how many finished downloads were folded in. */ export declare function reconcileHistory(history: DownloadHistory, options?: { /** Where status files live. Defaults to the package's status directory. */ statusDir?: string; /** Map a status onto the per-consumer `meta` payload. */ toMeta?: (status: ReconcilableStatus) => M | undefined; /** Also delete status files for downloads that never finished. Default false. */ clearAbandoned?: boolean; }): Promise; /** The subset of `DownloadStatus` this module needs, without importing the type. */ export interface ReconcilableStatus { id: string; /** Identifies the ATTEMPT, so reconciliation cannot delete a retry's status. */ pid?: number; state: string; filename: string; /** Absent on a `startCommand` status whose output was not resolved. */ outputPath?: string; bytesDownloaded: number; startedAt: number; heartbeatAt: number; finishedAt?: number; error?: { code: string; message: string; }; meta?: Record; } export declare function createDownloadHistory(options?: HistoryOptions): DownloadHistory; //# sourceMappingURL=history.d.ts.map