/** * What is known about a `.part` file, bound to the PATH rather than to a * download id. * * Everything else in this package is addressed by id: statuses, locks, leases. * That is correct for reporting an attempt and wrong for protecting a file, * because the file is shared. Several ids can name one `outputPath`, a retry * routinely uses a new id, and a consumer can point an unrelated download at a * path a previous one left bytes in. An id-keyed record cannot answer the only * question that matters here — "may I append to THIS file?" — without scanning * every status on disk and hoping none was pruned. * * So the answer lives beside the file, in two sidecars with different lifetimes: * * `.state` durable. Survives attempts, and is what makes a * contaminated partial refuse to be resumed onto by an * attempt that knows nothing about the one that spoiled * it. Removed only when the partial is verifiably gone or * has been replaced. * `.claim` ephemeral. Says an attempt is live against this path * right now, and is how a second one is refused instead * of racing it. Created with `wx`, so the claim is the * filesystem's answer rather than ours. * * Measured, and the reason this module exists (curl 8.7.1, local server): * seeding an 8-byte partial with garbage and resuming against a Range-capable * server exits 0, reports 206, and produces `XXXXXXXX89ABCDEFGHIJ` where the * canonical body is `0123456789ABCDEFGHIJ`. curl cannot detect it — a range * request is a promise that the prefix is already correct. Only the side that * wrote the prefix can keep that promise. */ /** Durable facts about the bytes currently in a `.part` file. */ export interface PartialState { v: 1; /** * The bytes cannot be vouched for: an attempt wrote something into them that * does not belong to the download and could not take it back out. Resuming * appends the real file to that, and the result is published under the user's * filename. */ unsafe?: true; /** Fingerprint of the exact URL these bytes came from. */ urlHash?: string; /** * Fingerprint of origin + path only, with the query dropped. * * Exists because a signed URL is not stable. The documented recovery from an * expired link is to re-request a fresh one and resume onto the partial * already on disk — and a fresh signature is a different URL, so matching on * the whole URL would restart every signed download from zero and quietly * undo the feature it was protecting. */ resourceHash?: string; /** `ETag` from the response that produced these bytes, when it was a strong one. */ etag?: string; /** `Last-Modified`, used as the `If-Range` validator when there is no strong ETag. */ lastModified?: string; } export interface PartialClaim { pid: number; startedAtMs: number; id: string; /** * Who holds this claim, as a value only the holder knows. * * Without it every release is "unlink whatever is at this path", and a * late-firing handler from a failed attempt deletes the claim a RETRY has * since taken — handing the path to a third attempt while the second is * mid-transfer. The token makes release mean "release MINE". */ token: string; } export declare function statePath(partPath: string): string; export declare function claimPath(partPath: string): string; /** Header dump for the in-flight attempt. Transient; removed when it is parsed. */ export declare function headerPath(partPath: string): string; /** * A URL reduced to something safe to leave on disk. * * Hashed rather than stored: a signed URL is a bearer credential, and this file * sits in the user's Downloads folder next to the partial. The hash answers the * only question asked of it — "is this the same URL as last time?" — and * answers nothing else. */ export declare function urlFingerprint(url: string): string; /** * Identity of the RESOURCE a URL names, ignoring the query string. * * Two signed URLs for one file differ only in their signature parameters, so * dropping the query is what lets a re-signed link resume. It is deliberately * the weaker test: an API that selects the resource with a query parameter * (`?file=123`) produces one fingerprint for two different files, which is why * a resume on a resource match alone is not permitted — see `mayResume`. */ export declare function resourceFingerprint(url: string): string; /** * May the bytes described by `state` be appended to, for a request to `url`? * * Three cases, and only the first two are yes: * * - the exact URL matches: the same request produced these bytes; * - the RESOURCE matches and a validator was recorded: probably a re-signed * link, and if it is not, `If-Range` makes the server answer 200 and curl * refuse (exit 33) rather than splice. The validator is what makes the * weaker identity test safe, so it is required rather than nice to have; * - anything else, including a partial with nothing recorded about it. */ export declare function mayResume(state: PartialState | undefined, url: string): boolean; export declare function readPartialState(partPath: string): PartialState | undefined; /** * Replace the durable state for a partial. * * Written through a temporary file and renamed, because a reader that catches * this file half-written gets `undefined` from `readPartialState` — which reads * as "nothing recorded" and permits the resume this file exists to forbid. */ export declare function writePartialState(partPath: string, state: PartialState): boolean; /** * Record that the bytes in this partial must never be resumed onto. * * Merges rather than replaces: the validators already recorded stay useful for * the attempt that eventually replaces the file. Returns false when the marker * could not be persisted, which is a worse failure than it looks — the caller * then has a contaminated file that nothing on disk warns about, and must say * so in the status instead. * * On that failure the existing state is removed: it vouches for the bytes as * they WERE, and with no state at all `mayResume` refuses. A full disk refuses * the write but still allows the unlink. */ export declare function markPartialUnsafe(partPath: string): boolean; export declare function clearPartialState(partPath: string): void; /** * Make a partial safe to start over from, and forget everything known about it. * * Truncation rather than deletion, deliberately: the empty `.part` is also the * RESERVATION on the final filename (`uniquePath({reserve: true})`), and * deleting it hands that name to the next caller while this download is still * going to use it. Falls back to deletion when truncation is refused, since a * lost reservation beats contaminated bytes. * * Returns false when the bytes are still there afterwards. */ export declare function resetPartial(partPath: string): boolean; /** * Claim this path for one live attempt, or report who holds it. * * `openSync(path, "wx")` is the whole mechanism: the filesystem decides, once, * which of two simultaneous callers wins. Anything built on "read, then decide, * then write" loses to the other process between the read and the write, which * is the case this is here to prevent. * * A claim whose owner is dead is STOLEN rather than honoured. A runner killed * mid-transfer leaves its claim behind, and a claim that outlives its owner * would wedge that filename until someone deleted a file they have no reason to * know about. */ export declare function claimPartialPath(partPath: string, owner: Omit): string | undefined; /** * Re-point a claim at the runner once it has a pid of its own. * * Refuses to write over a claim that is no longer ours: between taking it and * updating it, a stale-steal by another process can have replaced it, and * overwriting that would take a path a live attempt now owns. */ export declare function updatePartialClaim(partPath: string, owner: PartialClaim): void; /** * Release a claim this caller holds. * * `token` is checked against what is on disk, because the release can arrive * late: a spawn `error` event fires after `startDownload` has thrown, by which * time the caller may have retried and taken a NEW claim. Deleting that one * hands the path to a third attempt while the second is still writing. * * `force` exists for `killDownload`, which has just terminated the holder and * is cleaning up on its behalf. */ export declare function releasePartialClaim(partPath: string, token?: string, force?: boolean): void; /** Who holds this path right now, if anyone alive does. */ export declare function partialClaimHolder(partPath: string): PartialClaim | undefined; /** * Pull the `If-Range` validators out of a curl header dump. * * Returns the LAST response block's validators and nothing else. A dump holds * one block per hop, and only the final one describes the bytes that reached * disk — see the boundary reset below for what carrying one forward costs. * * Only a STRONG ETag is usable: RFC 9110 forbids a weak validator in * `If-Range`, because two weak-equivalent representations may differ byte for * byte — which is exactly the difference a resumed transfer cannot survive. * `Last-Modified` is the documented fallback. */ export declare function parseValidators(dump: string): { etag?: string; lastModified?: string; }; /** * The status of the LAST response block in a curl header dump, and whether that * block's headers are complete (terminated by the blank line), or undefined when * no response has been dumped yet. * * The runner's close handler has curl's own `http_code`; a cancellation, and the * meter watching a transfer still in flight, have only this. */ export declare function parseFinalStatus(dump: string): { status: number; complete: boolean; } | undefined; /** * The complete length a 416 reported, from the LAST response block's * `Content-Range: bytes * /N` (RFC 9110 §14.4), or undefined if it gave none. * * This is how a resumed request learns the partial is already the whole file: * asking for the range that starts at byte N of an N-byte resource is * unsatisfiable by definition. Same boundary rule as `parseValidators` — an * earlier hop's header never describes the final response. */ export declare function parseUnsatisfiedRangeTotal(dump: string): number | undefined; //# sourceMappingURL=partial.d.ts.map