Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(resources): give up on a download that stops receiving data
Elian Doranbd387c0e16d1
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.jsonindex 1be60dd31b..2d492735e7 100644--- a/apps/server/src/assets/translations/en/server.json+++ b/apps/server/src/assets/translations/en/server.json@@ -588,6 +588,7 @@"size-mismatch": "{{name}} does not have the expected size.","hash-mismatch": "{{name}} does not match its expected checksum, so it was discarded.","delete-while-downloading": "{{- title}} is being downloaded. Cancel the download before deleting it.",- "not-foreign": "{{- name}} is not an unused entry of the resources folder."+ "not-foreign": "{{- name}} is not an unused entry of the resources folder.",+ "stalled": "Downloading {{name}} stopped: nothing arrived for {{seconds}} seconds. Check your connection, then try again."}}diff --git a/apps/server/src/services/resources.spec.ts b/apps/server/src/services/resources.spec.tsindex e8b2d94b79..adec6a10f2 100644--- a/apps/server/src/services/resources.spec.ts+++ b/apps/server/src/services/resources.spec.ts@@ -11,6 +11,8 @@ import { type ResourceDefinition, type ResourceFile, ResourceManager } from "./rconst MODEL = Buffer.alloc(300_000, "m");const TOKENS = Buffer.from("a\nb\nc\n");+/** How many bytes of the model `/model-stall.bin` sends before it stops sending anything. */+const STALL_AFTER = 1000;let server: http.Server;let baseUrl = "";@@ -41,6 +43,9 @@ beforeAll(async () => {res.end(MODEL);} else if (req.url === "/tokens.txt") {res.end(TOKENS);+ } else if (req.url === "/model-stall.bin") {+ res.write(MODEL.subarray(0, STALL_AFTER));+ stalled.push(res);} else if (req.url === "/stall") {res.flushHeaders();stalled.push(res);@@ -157,6 +162,18 @@ describe("ResourceManager", () => {expect(manager.getPath("test.short")).toBeNull();});+ it("fails a download that receives nothing for too long, keeping its bytes for the next try", async () => {+ const impatient = new ResourceManager(baseDir, (progress) => broadcasts.push(progress),+ { stallTimeoutMs: 200 });+ impatient.register(defineModel({ modelUrl: "/model-stall.bin" }));++ impatient.startDownload([ "test.model" ]);+ const failed = await waitForEnd();+ expect(failed.status).toBe("failed");+ expect(failed.error).toMatch(/model\.bin/);+ expect((await impatient.getStatus([ "test.model" ])).resources[0].downloadedSize).toBe(STALL_AFTER);+ });+it("joins a running download, and cancels it without leaving an empty partial file", async () => {manager.register(defineModel({ tokensUrl: "/stall" }));diff --git a/apps/server/src/services/resources.ts b/apps/server/src/services/resources.tsindex 7dc4ad5844..49ac64b7de 100644--- a/apps/server/src/services/resources.ts+++ b/apps/server/src/services/resources.ts@@ -45,6 +45,8 @@ type VerifiedFiles = Record<string, { size: number; mtimeMs: number }>;const ID_PATTERN = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/;const CONTENT_HASH_PATTERN = /^[0-9a-f]{12}$/;const PROGRESS_INTERVAL_MS = 250;+/** How long a download waits for the next bytes before it gives up on the connection. */+const STALL_TIMEOUT_MS = 30_000;/** How many hex digits of the files' combined hash a resource's directory name carries. */const CONTENT_HASH_LENGTH = 12;/** Holds the {@link VerifiedFiles} of a resource, in its directory. */@@ -66,7 +68,8 @@ export class ResourceManager {constructor(private readonly baseDir: string,- private readonly broadcast: (progress: ResourceDownloadProgress) => void+ private readonly broadcast: (progress: ResourceDownloadProgress) => void,+ private readonly options: { stallTimeoutMs?: number } = {}) {}register(...definitions: ResourceDefinition[]) {@@ -192,7 +195,7 @@ export class ResourceManager {await downloadFile(file, destination, controller.signal, (bytes) => {receive(bytes);report(false);- });+ }, this.options.stallTimeoutMs);}const { size, mtimeMs } = await fs.promises.stat(destination);verified[file.path] = { size, mtimeMs };@@ -330,20 +333,29 @@ export class ResourceManager {* SHA-256 match. A `.partial` left by an interrupted download is continued: its bytes are hashed* and the rest is asked for with a `Range` header, or the file starts over when the server sends* all of it. A failed download keeps its `.partial` while it is shorter than the file, so that the- * next one continues it; a complete one that fails its hash is deleted.+ * next one continues it; a complete one that fails its hash is deleted. A connection that sends+ * nothing for `stallTimeoutMs`, before the response or within it, fails the download.*/export async function downloadFile(file: ResourceFile, destination: string, signal: AbortSignal,- onProgress: (bytes: number) => void) {+ onProgress: (bytes: number) => void, stallTimeoutMs = STALL_TIMEOUT_MS) {const partial = `${destination}.partial`;const name = path.basename(file.path);+ const stall = new AbortController();+ let stallTimer: ReturnType<typeof setTimeout> | undefined;+ const waitForBytes = () => {+ clearTimeout(stallTimer);+ stallTimer = setTimeout(() => stall.abort(), stallTimeoutMs);+ };+ const requestSignal = AbortSignal.any([ signal, stall.signal ]);try {let hash = createHash("sha256");let received = getPartialSize(destination, file);if (received) {await hashFileInto(partial, hash);}+ waitForBytes();const response = await fetch(file.url, {- signal,+ signal: requestSignal,headers: received ? { Range: `bytes=${received}-` } : undefined});if (!response.ok || !response.body) {@@ -363,6 +375,7 @@ export async function downloadFile(file: ResourceFile, destination: string, signconst meter = new Transform({transform(chunk: Buffer, _encoding, callback) {+ waitForBytes();received += chunk.length;if (received > file.size) {callback(new Error(t("resources.size-mismatch", { name })));@@ -375,7 +388,7 @@ export async function downloadFile(file: ResourceFile, destination: string, sign});const body = Readable.fromWeb(response.body as NodeReadableStream<Uint8Array>);const output = fs.createWriteStream(partial, { flags: received ? "a" : "w" });- await pipeline(body, meter, output, { signal });+ await pipeline(body, meter, output, { signal: requestSignal });if (received !== file.size) {throw new Error(t("resources.size-mismatch", { name }));@@ -388,7 +401,12 @@ export async function downloadFile(file: ResourceFile, destination: string, signif (getPartialSize(destination, file) === 0) {await fs.promises.rm(partial, { force: true });}+ if (stall.signal.aborted && !signal.aborted) {+ throw new Error(t("resources.stalled", { name, seconds: Math.round(stallTimeoutMs / 1000) }));+ }throw e;+ } finally {+ clearTimeout(stallTimer);}}