The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Files: a file is opened when its body is first read
Claude620ba6703490
diff --git a/src/server/file-response.ts b/src/server/file-response.tsindex bde932be..04154faa 100644--- a/src/server/file-response.ts+++ b/src/server/file-response.ts@@ -31,6 +31,26 @@ export function fileResponse(request: Request, relPath: string, opts: { cache?:pipeline(stream, patched, () => {}); // a read error ends the response (pipeline destroys both)return patched;};+ // Opened when the body is first read: a body never read (Next answers HEAD with GET and drops the body) holds no+ // file open, and a file deleted in between (its run deleted) fails the read. A stream opened at once has no error+ // listener until it is read, so that ENOENT was an uncaught exception.+ const body = (range?: { start: number; end: number }): ReadableStream<Uint8Array> => {+ let chunks: AsyncIterator<Uint8Array> | undefined;+ return new ReadableStream<Uint8Array>(+ {+ async pull(controller) {+ chunks ??= read(range)[Symbol.asyncIterator]();+ const { value, done } = await chunks.next();+ if (done) controller.close();+ else controller.enqueue(value);+ },+ async cancel() {+ await chunks?.return?.();+ },+ },+ { highWaterMark: 0 },+ );+ };const headers: Record<string, string> = {// Transcripts, subtitles and scripts are UTF-8 text."Content-Type": /^text\/|^application\/(json|x-subrip)$/.test(type) ? `${type}; charset=utf-8` : type,@@ -48,12 +68,12 @@ export function fileResponse(request: Request, relPath: string, opts: { cache?:const start = m[1] ? Number(m[1]) : Math.max(0, size - Number(m[2]));const end = m[1] && m[2] ? Math.min(Number(m[2]), size - 1) : size - 1;if (start >= size || start > end) return new Response(null, { status: 416, headers: { "Content-Range": `bytes */${size}` } });- return new Response(read({ start, end }) as unknown as ReadableStream, {+ return new Response(body({ start, end }), {status: 206,headers: { ...headers, "Content-Range": `bytes ${start}-${end}/${size}`, "Content-Length": String(end - start + 1) },});}- return new Response(read() as unknown as ReadableStream, {+ return new Response(body(), {headers: { ...headers, "Content-Length": String(size) },});}diff --git a/tests/files-route.test.ts b/tests/files-route.test.tsindex 944f7f4d..b087fafa 100644--- a/tests/files-route.test.ts+++ b/tests/files-route.test.ts@@ -1,7 +1,7 @@import fs from "node:fs";import os from "node:os";import path from "node:path";-import { afterAll, describe, expect, it } from "vitest";+import { afterAll, describe, expect, it, vi } from "vitest";const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-files-route-"));process.env.FUOBE_DATA_DIR = dir;@@ -30,3 +30,33 @@ describe("/api/files decodes a path once", () => {expect(await ok.text()).toBe("hello");});});++describe("/api/files opens a file when its body is read", () => {+ it("so a body never read (HEAD) holds nothing open, and a file deleted first fails the read, not the process", async () => {+ fs.mkdirSync(path.join(dir, "files", "outputs"), { recursive: true });+ const file = path.join(dir, "files", "outputs", "b.txt");+ fs.writeFileSync(file, "0123456789");+ const ask = (init?: RequestInit) => GET(new Request("http://localhost:3000/api/files/outputs/b.txt", init), { params: Promise.resolve({ path: ["outputs", "b.txt"] }) } as never);+ const open = vi.spyOn(fs, "open");+ const opened = () => open.mock.calls.filter(([p]) => p === file).length;+ try {+ // Next answers HEAD with GET and never reads (or cancels) the body.+ const head = await ask({ method: "HEAD" });+ const part = await ask({ headers: { range: "bytes=2-4" } });+ await new Promise((r) => setTimeout(r, 20));+ expect(head.status).toBe(200);+ expect(part.status).toBe(206);+ expect(opened()).toBe(0);+ expect(await part.text()).toBe("234");+ expect(opened()).toBe(1);+ // Its run is deleted (the owner deletes an account's run) while the response is on its way: the read fails,+ // and no ENOENT is thrown from a stream nobody listens to.+ const late = await ask();+ fs.rmSync(file);+ await new Promise((r) => setTimeout(r, 20));+ await expect(late.text()).rejects.toThrow();+ } finally {+ open.mockRestore();+ }+ });+});