Skip to content

One-click character swap for videos: an open-source, local Higgsfield Genjutsu alternative built on ComfyUI and MiniMax H3, with FLUX.3 Klein relighting.

Stream uploads to disk, check their bytes, gate outputs and clean up inputs (#16)

mixbaga206cf80f1c8
diff --git a/app/api/jobs/[id]/outputs/[outputId]/route.ts b/app/api/jobs/[id]/outputs/[outputId]/route.ts
index 99e5860..20f3488 100644
--- a/app/api/jobs/[id]/outputs/[outputId]/route.ts
+++ b/app/api/jobs/[id]/outputs/[outputId]/route.ts
@@ -3,6 +3,9 @@ import { getOutput } from "../../../../../../lib/comfy.ts";
export const runtime = "nodejs";
+// SVG is left out: it can carry script.
+const SERVED_TYPES = /^(video\/[\w.+-]+|image\/(png|jpeg|webp|gif)|application\/json)$/;
+
export async function GET(request: Request, { params }: { params: Promise<{ id: string; outputId: string }> }) {
const denied = apiAccess(request);
if (denied) return denied;
@@ -11,8 +14,14 @@ export async function GET(request: Request, { params }: { params: Promise<{ id:
const { name, response } = await getOutput(id, outputId, request.headers.get("Range"));
if (!response.ok || !response.body) return Response.json({ error: "The output is not ready." }, { status: 502 });
- const headers = new Headers({ "Cache-Control": "private, max-age=300" });
- for (const header of ["Content-Type", "Content-Length", "Content-Range", "Accept-Ranges"]) {
+ const type = response.headers.get("Content-Type")?.split(";")[0].trim().toLowerCase() ?? "";
+ if (!SERVED_TYPES.test(type)) {
+ await response.body.cancel();
+ return Response.json({ error: "Openjutsu only serves video, image and JSON outputs." }, { status: 502 });
+ }
+
+ const headers = new Headers({ "Cache-Control": "private, max-age=300", "Content-Type": type, "X-Content-Type-Options": "nosniff" });
+ for (const header of ["Content-Length", "Content-Range", "Accept-Ranges"]) {
const value = response.headers.get(header);
if (value) headers.set(header, value);
}
diff --git a/app/api/jobs/route.ts b/app/api/jobs/route.ts
index f0bf977..291df17 100644
--- a/app/api/jobs/route.ts
+++ b/app/api/jobs/route.ts
@@ -1,4 +1,4 @@
-import { apiAccess, HttpError, readFormData } from "../../../lib/http.ts";
+import { apiAccess, HttpError, IMAGE_SIGNATURE, readFormData, StoredFile, VIDEO_SIGNATURE } from "../../../lib/http.ts";
import { NextResponse } from "next/server";
import { listModels, submitAdvanced, submitColor, submitControl, submitFace, submitMask, submitMontage, submitObject, submitPeople, submitSimple } from "../../../lib/comfy.ts";
import type { ControlSettings } from "../../../lib/control.ts";
@@ -12,9 +12,14 @@ import { CLIP_SECONDS, DEFAULT_TARGET, MARGINS, MAX_SEED, MIN_SECTION_SECONDS, M
export const runtime = "nodejs";
+const FILE_CHECKS = {
+ video: { signature: VIDEO_SIGNATURE, error: "The source must be an MP4, MOV, MKV, WebM or AVI video." },
+ picture: { signature: IMAGE_SIGNATURE, error: "Photos must be PNG, JPEG or WebP images." },
+};
+
function parseVideo(form: FormData) {
const video = form.get("video");
- if (!(video instanceof File) || video.size === 0) throw new Error("Drop a source video.");
+ if (!(video instanceof StoredFile) || video.size === 0) throw new Error("Drop a source video.");
if (!video.type.startsWith("video/") && !/\.(mp4|mov|mkv|webm|avi)$/i.test(video.name)) throw new Error("The source must be a video file.");
return video;
}
@@ -36,7 +41,7 @@ function parseMask(form: FormData) {
}
function parsePicture(picture: FormDataEntryValue | null) {
- if (!(picture instanceof File) || picture.size === 0) throw new Error("Add a character photo.");
+ if (!(picture instanceof StoredFile) || picture.size === 0) throw new Error("Add a character photo.");
if (!picture.type.startsWith("image/")) throw new Error("The character photo must be an image.");
return picture;
}
@@ -74,7 +79,7 @@ function parseObject(form: FormData) {
if (!target) throw new Error(mode === "add" ? "Say where the object goes, for example on the table." : mode === "background" ? "Say what stays, for example person." : "Say what to change, for example hair.");
const changeTo = String(form.get("change_to") ?? "").replace(/\s+/g, " ").trim().slice(0, 300);
const entry = form.get("picture");
- const picture = entry instanceof File && entry.size > 0 && mode !== "remove" ? parsePicture(entry) : null;
+ const picture = entry instanceof StoredFile && entry.size > 0 && mode !== "remove" ? parsePicture(entry) : null;
if (mode !== "remove" && !changeTo && !picture) throw new Error(mode === "add" ? "Describe the object to add, or add a picture." : "Say what it becomes, or add a picture.");
const settings: ObjectSettings = {
mode: mode as EditMode, target, changeTo, biggestOnly: form.get("biggest_only") === "true", keepOutline: form.get("keep_outline") === "true",
@@ -84,16 +89,16 @@ function parseObject(form: FormData) {
return { picture, settings };
}
-function parseModel(form: FormData, available: { models: string[]; loras: string[] } | null): ModelSettings {
+function parseModel(form: FormData, available: { models: string[]; loras: string[] }): ModelSettings {
const steps = Number(form.get("steps"));
if (!Number.isInteger(steps) || steps < STEPS.min || steps > STEPS.max) throw new Error(`Steps must be a whole number from ${STEPS.min} to ${STEPS.max}.`);
const model = String(form.get("model") ?? "").trim();
- if (!model || (available && !available.models.includes(model))) throw new Error("Choose a base model ComfyUI has.");
+ if (!model || !available.models.includes(model)) throw new Error("Choose a base model ComfyUI has.");
const names = form.getAll("lora").map((name) => String(name).trim());
const strengths = form.getAll("lora_strength").map(Number);
if (names.length > MAX_LORAS || strengths.length !== names.length) throw new Error(`Use at most ${MAX_LORAS} LoRAs, each with a strength.`);
const loras = names.map((name, i) => {
- if (!name || (available && !available.loras.includes(name))) throw new Error("Choose LoRAs ComfyUI has.");
+ if (!name || !available.loras.includes(name)) throw new Error("Choose LoRAs ComfyUI has.");
if (!(strengths[i] >= STRENGTH.min && strengths[i] <= STRENGTH.max)) throw new Error(`LoRA strength must be ${STRENGTH.min} to ${STRENGTH.max}.`);
return { name, strength: strengths[i] };
});
@@ -186,24 +191,43 @@ function parseMontage(form: FormData) {
return { pictures, settings, review: { tracks: String(tracks), labels: parseLabels(form.get("labels"), pictures.length) } };
}
-let submitting = false;
+// Uploads stream to temp files, so the cap bounds disk use rather than memory.
+const MAX_UPLOADS = 3;
+let uploads = 0;
export async function POST(request: Request) {
const denied = apiAccess(request);
if (denied) return denied;
- if (submitting) return NextResponse.json({ error: "Another upload is in progress. Try again shortly." }, { status: 429, headers: { "Retry-After": "5" } });
- submitting = true;
+ if (uploads >= MAX_UPLOADS) return NextResponse.json({ error: "Another upload is in progress. Try again shortly." }, { status: 429, headers: { "Retry-After": "5" } });
+ uploads++;
try {
return await submitJob(request);
} finally {
- submitting = false;
+ uploads--;
}
}
+function invalid(error: unknown) {
+ return NextResponse.json({ error: error instanceof Error ? error.message : "Invalid request." }, { status: error instanceof HttpError ? error.status : 400 });
+}
+
async function submitJob(request: Request) {
+ let upload: Awaited<ReturnType<typeof readFormData>>;
+ try {
+ upload = await readFormData(request, FILE_CHECKS);
+ } catch (error) {
+ return invalid(error);
+ }
+ try {
+ return await submitForm(upload.form);
+ } finally {
+ await upload.dispose();
+ }
+}
+
+async function submitForm(form: FormData) {
let submit: () => ReturnType<typeof submitSimple>;
try {
- const form = await readFormData(request);
const mode = form.get("mode");
const video = parseVideo(form);
const section = parseSection(form);
@@ -244,7 +268,7 @@ async function submitJob(request: Request) {
throw new Error("Unknown mode.");
}
} catch (error) {
- return NextResponse.json({ error: error instanceof Error ? error.message : "Invalid request." }, { status: error instanceof HttpError ? error.status : 400 });
+ return invalid(error);
}
try {
diff --git a/app/api/models/route.ts b/app/api/models/route.ts
index 6a315c1..db94ae5 100644
--- a/app/api/models/route.ts
+++ b/app/api/models/route.ts
@@ -1,6 +1,6 @@
import { apiAccess } from "../../../lib/http.ts";
import { NextResponse } from "next/server";
-import { listModels } from "../../../lib/comfy.ts";
+import { listModels, MODELS_UNAVAILABLE } from "../../../lib/comfy.ts";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
@@ -8,5 +8,10 @@ export const dynamic = "force-dynamic";
export async function GET(request: Request) {
const denied = apiAccess(request);
if (denied) return denied;
- return NextResponse.json((await listModels()) ?? { models: [], loras: [] });
+ try {
+ return NextResponse.json(await listModels());
+ } catch {
+ // Empty lists turn the pickers into text fields.
+ return NextResponse.json({ models: [], loras: [], error: MODELS_UNAVAILABLE }, { status: 502 });
+ }
}
diff --git a/lib/comfy.ts b/lib/comfy.ts
index 73018e3..39a73e7 100644
--- a/lib/comfy.ts
+++ b/lib/comfy.ts
@@ -1,7 +1,8 @@
-import { validateJobId } from "./http.ts";
+import { HttpError, validateJobId, type StoredFile } from "./http.ts";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
-import { Comfy, COMFY_CLOUD_BASE_URL, resolveCredentials, type Job } from "@comfyorg/sdk";
+import { setTimeout as sleep } from "node:timers/promises";
+import { Asset, Comfy, COMFY_CLOUD_BASE_URL, NotFound, resolveCredentials, type Job, type Workflow } from "@comfyorg/sdk";
import { ComfyLow } from "@comfyorg/sdk/low";
import { MASK_NODES, NODES, cropSource, pruneMask, pruneSimple, setSection, type ApiGraph, type Margin, type Resolution, type Section, type SimpleSettings } from "./simple.ts";
import { VIDEO, nodeByTitle, pruneAdvanced, type AdvancedSettings } from "./advanced.ts";
@@ -23,9 +24,13 @@ export type PublicJob = {
export const TERMINAL = new Set(["succeeded", "failed", "canceled", "expired"]);
+// undici clones any request it may redirect, teeing the body into a copy nobody reads, so a whole upload would wait in
+// memory. Uploads are never redirected.
+const fetchUploads: typeof fetch = (input, init) => fetch(input, init?.body instanceof FormData ? { ...init, redirect: "error" } : init);
+
// COMFY_BASE_URL and the optional COMFY_API_KEY come from .env.local; the SDK reads both.
export function client() {
- return new Comfy({ clientInfo: "openjutsu" });
+ return new Comfy({ clientInfo: "openjutsu", fetch: fetchUploads });
}
// The same deployment as client(); the SDK's resolveBaseUrl() is the Comfy Router's, not COMFY_BASE_URL.
@@ -69,130 +74,189 @@ export function publicJob(job: Job): PublicJob {
};
}
-function upload(comfy: Comfy, file: File) {
- return file.arrayBuffer().then((bytes) =>
- comfy.assets.fromBytes(new Uint8Array(bytes), {
- filename: file.name.replace(/[^\w.\- ]+/g, "_").slice(-100) || "upload",
- contentType: file.type || "application/octet-stream",
- }),
- );
+// The SDK streams the temp file from disk and names the upload after it.
+function upload(comfy: Comfy, file: StoredFile) {
+ return comfy.assets.fromFile(file.path);
}
-export async function submitSimple(video: File, section: Section, picture: File, settings: SimpleSettings, model: ModelSettings) {
+// Inputs this server uploaded, with the jobs still using each. The proxy hands identical bytes the same asset id,
+// so an input is deleted only once no job here uses it, and only if this server uploaded it rather than reused it.
+const inputs = ((globalThis as { [key: symbol]: unknown })[Symbol.for("openjutsu.inputs")] ??= new Map()) as Map<string, { jobs: number; owned: boolean }>;
+
+function hold(asset: Asset) {
+ const entry = inputs.get(asset.id!) ?? { jobs: 0, owned: false };
+ inputs.set(asset.id!, { jobs: entry.jobs + 1, owned: entry.owned || asset.createdNew === true });
+}
+
+async function release(comfy: Comfy, ids: string[]) {
+ for (const id of ids) {
+ const entry = inputs.get(id);
+ if (!entry) continue;
+ if (entry.jobs > 1) {
+ inputs.set(id, { ...entry, jobs: entry.jobs - 1 });
+ continue;
+ }
+ inputs.delete(id);
+ if (entry.owned) await comfy.assets.delete(id).catch(() => {});
+ }
+}
+
+// Retry and every other run upload their inputs again from the page, so nothing reuses them after a terminal status.
+async function releaseWhenDone(comfy: Comfy, job: Job, ids: string[]) {
+ for (;;) {
+ try {
+ await job.wait();
+ break;
+ } catch (error) {
+ // The proxy forgets jobs when it restarts.
+ if (error instanceof NotFound) break;
+ await sleep(30_000);
+ }
+ }
+ await release(comfy, ids);
+}
+
+async function submit(comfy: Comfy, workflow: Workflow) {
+ const assets = Object.values(workflow.json).flatMap((node) => Object.values((node as { inputs?: object }).inputs ?? {})).filter((value) => value instanceof Asset);
+ const ids: string[] = [];
+ try {
+ for (const asset of assets) {
+ ids.push(await asset.commit());
+ hold(asset);
+ }
+ const job = await comfy.submit(workflow);
+ void releaseWhenDone(comfy, job, ids);
+ return publicJob(job);
+ } catch (error) {
+ await release(comfy, ids);
+ throw error;
+ }
+}
+
+export async function submitSimple(video: StoredFile, section: Section, picture: StoredFile, settings: SimpleSettings, model: ModelSettings) {
const comfy = client();
const graph = pruneSimple(await loadGraph("simple"), settings);
applyModel(graph, model);
setSection(graph, NODES.video, section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(NODES.video, "video", await upload(comfy, video));
- workflow.setInput(NODES.picture, "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(NODES.video, "video", upload(comfy, video));
+ workflow.setInput(NODES.picture, "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitAdvanced(video: File, section: Section, pictures: File[], settings: AdvancedSettings, maxQuality: boolean, model: ModelSettings) {
+export async function submitAdvanced(video: StoredFile, section: Section, pictures: StoredFile[], settings: AdvancedSettings, maxQuality: boolean, model: ModelSettings) {
const comfy = client();
const graph = pruneAdvanced(await loadGraph(maxQuality ? "advanced_maxq" : "advanced"), pictures.length, settings);
applyModel(graph, model);
setSection(graph, nodeByTitle(graph, VIDEO), section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, VIDEO), "video", await upload(comfy, video));
- for (const [i, picture] of pictures.entries()) workflow.setInput(nodeByTitle(graph, `Picture ${i + 1}`), "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, VIDEO), "video", upload(comfy, video));
+ for (const [i, picture] of pictures.entries()) workflow.setInput(nodeByTitle(graph, `Picture ${i + 1}`), "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitObject(video: File, section: Section, picture: File | null, settings: ObjectSettings, model: ModelSettings) {
+export async function submitObject(video: StoredFile, section: Section, picture: StoredFile | null, settings: ObjectSettings, model: ModelSettings) {
const comfy = client();
const graph = pruneObject(await loadGraph("object"), !!picture, settings);
applyModel(graph, model);
setSection(graph, nodeByTitle(graph, OBJECT_VIDEO), section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, OBJECT_VIDEO), "video", await upload(comfy, video));
- if (picture && settings.mode !== "remove") workflow.setInput(nodeByTitle(graph, OBJECT_PICTURE), "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, OBJECT_VIDEO), "video", upload(comfy, video));
+ if (picture && settings.mode !== "remove") workflow.setInput(nodeByTitle(graph, OBJECT_PICTURE), "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitControl(video: File, section: Section, picture: File, settings: ControlSettings, model: ModelSettings) {
+export async function submitControl(video: StoredFile, section: Section, picture: StoredFile, settings: ControlSettings, model: ModelSettings) {
const comfy = client();
const graph = pruneControl(await loadGraph("control"), settings);
applyModel(graph, model);
setControlSection(graph, section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, CONTROL_VIDEO), "video", await upload(comfy, video));
- workflow.setInput(nodeByTitle(graph, CONTROL_PICTURE), "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, CONTROL_VIDEO), "video", upload(comfy, video));
+ workflow.setInput(nodeByTitle(graph, CONTROL_PICTURE), "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitFace(video: File, section: Section, picture: File, settings: FaceSettings, model: ModelSettings) {
+export async function submitFace(video: StoredFile, section: Section, picture: StoredFile, settings: FaceSettings, model: ModelSettings) {
const comfy = client();
const graph = pruneFace(await loadGraph("face"), settings);
applyModel(graph, model);
setSection(graph, nodeByTitle(graph, FACE_VIDEO), section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, FACE_VIDEO), "video", await upload(comfy, video));
- workflow.setInput(nodeByTitle(graph, FACE_PICTURE), "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, FACE_VIDEO), "video", upload(comfy, video));
+ workflow.setInput(nodeByTitle(graph, FACE_PICTURE), "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitColor(video: File, section: Section, settings: ColorSettings) {
+export async function submitColor(video: StoredFile, section: Section, settings: ColorSettings) {
const comfy = client();
const graph = pruneColor(await loadGraph("color"), settings);
setSection(graph, nodeByTitle(graph, COLOR_VIDEO), section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, COLOR_VIDEO), "video", await upload(comfy, video));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, COLOR_VIDEO), "video", upload(comfy, video));
+ return submit(comfy, workflow);
}
-export async function submitMask(video: File, section: Section, hatFix: boolean, margin: Margin, target: string) {
+export async function submitMask(video: StoredFile, section: Section, hatFix: boolean, margin: Margin, target: string) {
const comfy = client();
const graph = pruneMask(await loadGraph("mask"), hatFix, margin, target);
setSection(graph, MASK_NODES.video, section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(MASK_NODES.video, "video", await upload(comfy, video));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(MASK_NODES.video, "video", upload(comfy, video));
+ return submit(comfy, workflow);
}
-export async function submitMontage(video: File, section: Section, pictures: File[], settings: AdvancedSettings, review: Review | null, model: ModelSettings) {
+export async function submitMontage(video: StoredFile, section: Section, pictures: StoredFile[], settings: AdvancedSettings, review: Review | null, model: ModelSettings) {
const comfy = client();
const graph = pruneMontage(await loadGraph("montage"), pictures.length, settings, review);
applyModel(graph, model);
setSection(graph, nodeByTitle(graph, VIDEO), section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(nodeByTitle(graph, VIDEO), "video", await upload(comfy, video));
- for (const [i, picture] of pictures.entries()) workflow.setInput(nodeByTitle(graph, `Picture ${i + 1}`), "image", await upload(comfy, picture));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(nodeByTitle(graph, VIDEO), "video", upload(comfy, video));
+ for (const [i, picture] of pictures.entries()) workflow.setInput(nodeByTitle(graph, `Picture ${i + 1}`), "image", upload(comfy, picture));
+ return submit(comfy, workflow);
}
-export async function submitPeople(video: File, section: Section, roles: string[], resolution: Resolution) {
+export async function submitPeople(video: StoredFile, section: Section, roles: string[], resolution: Resolution) {
const comfy = client();
const graph = await loadGraph("people");
graph[PEOPLE_NODES.crops].inputs.people = peopleRoles(roles);
if (typeof resolution === "object") cropSource(graph, PEOPLE_NODES.video, resolution);
setSection(graph, PEOPLE_NODES.video, section);
const workflow = comfy.workflows.fromJson(graph);
- workflow.setInput(PEOPLE_NODES.video, "video", await upload(comfy, video));
- return publicJob(await comfy.submit(workflow));
+ workflow.setInput(PEOPLE_NODES.video, "video", upload(comfy, video));
+ return submit(comfy, workflow);
+}
+
+// COMFYUI_URL points at ComfyUI itself, not the proxy: the proxy lists no nodes, models or presets.
+export async function comfyUi(path: string, timeout: number) {
+ const url = (process.env.COMFYUI_URL?.trim() || "http://127.0.0.1:8188").replace(/\/+$/, "");
+ const response = await fetch(`${url}${path}`, { cache: "no-store", signal: AbortSignal.timeout(timeout) });
+ if (!response.ok) throw new Error(`ComfyUI answered ${path} with HTTP ${response.status}.`);
+ return response.json();
}
-const COMFYUI_URL = process.env.COMFYUI_URL || "http://127.0.0.1:8188";
+export const MODELS_UNAVAILABLE = "Could not read the model lists from ComfyUI. Make sure it is running and COMFYUI_URL points to it, then try again.";
-// The proxy has no model listing, so the pickers read ComfyUI's own node info. COMFYUI_URL points at ComfyUI itself
-// (not the proxy); without it, or when it does not answer, the page falls back to typed file names.
-export async function listModels(): Promise<{ models: string[]; loras: string[] } | null> {
+// The pickers read ComfyUI's own node info; when that fails, the page falls back to typed file names.
+export async function listModels(): Promise<{ models: string[]; loras: string[] }> {
const options = async (node: string, input: string) => {
- const info = await (await fetch(`${COMFYUI_URL}/object_info/${node}`, { signal: AbortSignal.timeout(5000) })).json();
- return info[node].input.required[input][0] as string[];
+ const names = (await comfyUi(`/object_info/${node}`, 5000))?.[node]?.input?.required?.[input]?.[0];
+ if (!Array.isArray(names) || names.some((name) => typeof name !== "string")) throw new Error(`ComfyUI sent no ${input} list.`);
+ return names as string[];
};
try {
const [models, loras] = await Promise.all([options("UNETLoader", "unet_name"), options("LoraLoaderModelOnly", "lora_name")]);
return { models, loras };
- } catch {
- return null;
+ } catch (error) {
+ console.warn(`${MODELS_UNAVAILABLE} ${error instanceof Error ? error.message : ""}`.trim());
+ throw new HttpError(502, MODELS_UNAVAILABLE);
}
}
// Orion4D's saved DCTL Tone Mapper presets, read from ComfyUI like the model lists; empty when it does not answer.
export async function listTonePresets(): Promise<{ name: string; settings: Record<string, unknown> }[]> {
- const get = async (path: string) => (await fetch(`${COMFYUI_URL}/orion4d_fxmax/${path}`, { signal: AbortSignal.timeout(5000) })).json();
+ const get = (path: string) => comfyUi(`/orion4d_fxmax/${path}`, 5000);
try {
const { presets } = await get("presets/color_fx_dctl_tone_mapper");
return await Promise.all((presets as string[]).map(async (name) => ({ name, settings: (await get(`preset/color_fx_dctl_tone_mapper/${encodeURIComponent(name)}`)).settings })));
@@ -209,7 +273,10 @@ export function getJob(id: string) {
export async function getOutput(jobId: string, outputId: string, range: string | null) {
const job = await getJob(jobId);
const output = job.outputs.find((candidate) => candidate.id === outputId);
- // A running job lists no outputs yet; its mask previews arrive as live output events, fetched by asset id.
+ // A running job lists no outputs yet; its mask previews arrive as live output events, so any other id must name an
+ // asset this job produced. Uploaded inputs have no producing job.
+ const asset = output ? null : await low().getAsset(outputId).catch(() => null);
+ if (!output && (!asset || asset.job_id !== job.id)) throw new HttpError(404, "This job has no such output.");
const { url } = output ? await output.getDownloadUrl() : await low().getAssetContentUrl(outputId);
- return { name: output?.name ?? outputId, response: await fetch(url, { headers: range ? { Range: range } : {} }) };
+ return { name: output?.name ?? asset?.file_path ?? outputId, response: await fetch(url, { headers: range ? { Range: range } : {} }) };
}
diff --git a/lib/http.ts b/lib/http.ts
index 50a8053..abb8793 100644
--- a/lib/http.ts
+++ b/lib/http.ts
@@ -1,5 +1,9 @@
import { createHash, timingSafeEqual } from "node:crypto";
+import { openAsBlob } from "node:fs";
+import { mkdir, mkdtemp, open as openFile, rm, type FileHandle } from "node:fs/promises";
import type { AddressInfo } from "node:net";
+import { tmpdir } from "node:os";
+import { dirname, join } from "node:path";
export class HttpError extends Error {
status: number;
@@ -116,23 +120,141 @@ export function apiAccess(request: Request) {
// Total multipart body, including reference pictures and boundaries; the proxy separately caps each file at 500 MB.
export const MAX_UPLOAD_BYTES = 512 * 1024 * 1024;
+const MAX_FIELD_BYTES = 1024 * 1024;
+const MAX_HEADER_BYTES = 16 * 1024;
+const MAX_FILES = 16;
+const HEAD_BYTES = 12;
-export async function readFormData(request: Request, limit = MAX_UPLOAD_BYTES) {
+// Leading bytes of what ComfyUI's loaders read: MP4/MOV, MKV/WebM and AVI; PNG, JPEG and WebP.
+export const VIDEO_SIGNATURE = /^(?:[^]{4}ftyp|\x1a\x45\xdf\xa3|RIFF[^]{4}AVI )/;
+export const IMAGE_SIGNATURE = /^(?:\x89PNG\r\n\x1a\n|\xff\xd8\xff|RIFF[^]{4}WEBP)/;
+export type FileCheck = { signature: RegExp; error: string };
+
+// A file part already on disk, so it can be uploaded without reading it into memory.
+export class StoredFile extends File {
+ readonly path: string;
+
+ constructor(blob: Blob, name: string, type: string, path: string) {
+ super([blob], name, { type });
+ this.path = path;
+ }
+}
+
+// ComfyUI keeps the upload under its temp file's name, and Windows reserves device names even with an extension.
+export function uploadName(name: string) {
+ const safe = name.replace(/[^\w.\- ]+/g, "_").slice(-100).replace(/[. ]+$/, "") || "upload";
+ return /^(con|prn|aux|nul|com\d|lpt\d)(\.|$)/i.test(safe) ? `_${safe}` : safe;
+}
+
+type Part = { name: string; text?: Buffer[]; file?: { name: string; type: string; path: string; handle: FileHandle; head: Buffer; size: number; check?: FileCheck } };
+
+// Streams a multipart body to temp files, checking each file's first bytes as they arrive. Call dispose() when done.
+export async function readFormData(request: Request, checks: Record<string, FileCheck> = {}, limit = MAX_UPLOAD_BYTES) {
const tooLarge = () => new HttpError(413, `Uploads must total no more than ${Math.round(limit / 1024 / 1024)} MB.`);
if (Number(request.headers.get("Content-Length")) > limit) throw tooLarge();
+ const boundary = request.headers.get("Content-Type")?.match(/^multipart\/form-data\s*;.*?\bboundary=(?:"([^"]+)"|([^\s;]+))/i);
+ if (!boundary || !request.body) throw new HttpError(400, "Send the upload as multipart/form-data.");
+ const malformed = () => new HttpError(400, "The upload is not a valid multipart form.");
+ const delimiter = Buffer.from(`\r\n--${boundary[1] ?? boundary[2]}`);
+ const dir = await mkdtemp(join(tmpdir(), "openjutsu-"));
+ const dispose = () => rm(dir, { recursive: true, force: true });
+ const reader = request.body.getReader();
+ const form = new FormData();
+ let part: Part | null = null;
let size = 0;
- const body = request.body?.pipeThrough(new TransformStream<Uint8Array, Uint8Array>({
- transform(chunk, controller) {
- size += chunk.byteLength;
- if (size > limit) throw tooLarge();
- controller.enqueue(chunk);
- },
- }), { signal: request.signal });
+ let fieldBytes = 0;
+ let files = 0;
+
+ const open = async (headers: string): Promise<Part> => {
+ const disposition = headers.match(/^content-disposition:[ \t]*form-data(.*)$/im)?.[1] ?? "";
+ const param = (key: string) => disposition.match(new RegExp(`;\\s*${key}=(?:"([^"]*)"|([^;\\s]+))`, "i"))?.slice(1).find((value) => value !== undefined);
+ const name = param("name") ?? "";
+ const filename = param("filename");
+ if (filename === undefined) return { name, text: [] };
+ if (++files > MAX_FILES) throw new HttpError(400, `Send no more than ${MAX_FILES} files.`);
+ const path = join(dir, String(files), uploadName(filename));
+ await mkdir(dirname(path));
+ const type = headers.match(/^content-type:[ \t]*([^\r\n]*)$/im)?.[1].trim() ?? "";
+ return { name, file: { name: filename, type, path, handle: await openFile(path, "wx"), head: Buffer.alloc(0), size: 0, check: Object.hasOwn(checks, name) ? checks[name] : undefined } };
+ };
+ const sniff = (file: NonNullable<Part["file"]>) => {
+ if (file.check && !file.check.signature.test(file.head.toString("latin1"))) throw new HttpError(400, file.check.error);
+ file.check = undefined;
+ };
+ const write = async ({ text, file }: Part, chunk: Buffer) => {
+ if (text) {
+ fieldBytes += chunk.length;
+ if (fieldBytes > MAX_FIELD_BYTES) throw new HttpError(413, `Form fields must total no more than ${MAX_FIELD_BYTES / 1024 / 1024} MB.`);
+ text.push(Buffer.from(chunk));
+ } else if (file && chunk.length > 0) {
+ if (file.head.length < HEAD_BYTES) file.head = Buffer.concat([file.head, chunk.subarray(0, HEAD_BYTES - file.head.length)]);
+ if (file.head.length === HEAD_BYTES) sniff(file);
+ file.size += chunk.length;
+ await file.handle.write(chunk);
+ }
+ };
+ const finish = async ({ name, text, file }: Part) => {
+ if (text) form.append(name, Buffer.concat(text).toString("utf8"));
+ if (!file) return;
+ await file.handle.close();
+ if (file.size > 0) sniff(file);
+ form.append(name, new StoredFile(await openAsBlob(file.path), file.name, file.type, file.path));
+ };
+
try {
- return await new Response(body, { headers: request.headers }).formData();
+ // The leading line break lets the first boundary match the delimiter that ends every part.
+ let buffer = Buffer.from("\r\n");
+ let state: "preamble" | "next" | "headers" | "body" | "end" = "preamble";
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ size += value.byteLength;
+ if (size > limit) throw tooLarge();
+ buffer = Buffer.concat([buffer, value]);
+ for (let more = true; more;) {
+ if (state === "preamble" || state === "body") {
+ const at = buffer.indexOf(delimiter);
+ // Without a delimiter, keep only the tail that could still start one.
+ const end = at === -1 ? Math.max(0, buffer.length - delimiter.length + 1) : at;
+ if (part) await write(part, buffer.subarray(0, end));
+ buffer = buffer.subarray(at === -1 ? end : at + delimiter.length);
+ if (at === -1) more = false;
+ else {
+ if (part) await finish(part);
+ part = null;
+ state = "next";
+ }
+ } else if (state === "next") {
+ const mark = buffer.toString("latin1", 0, 2);
+ if (mark.length < 2) more = false;
+ else if (mark === "--") state = "end";
+ else if (mark !== "\r\n") throw malformed();
+ else {
+ buffer = buffer.subarray(2);
+ state = "headers";
+ }
+ } else if (state === "headers") {
+ const at = buffer.indexOf("\r\n\r\n");
+ if (at === -1) {
+ if (buffer.length > MAX_HEADER_BYTES) throw malformed();
+ more = false;
+ } else {
+ part = await open(buffer.toString("utf8", 0, at));
+ buffer = buffer.subarray(at + 4);
+ state = "body";
+ }
+ } else {
+ buffer = Buffer.alloc(0);
+ more = false;
+ }
+ }
+ }
+ if (state !== "end") throw malformed();
+ return { form, dispose };
} catch (error) {
- // Multipart parsers may wrap the stream error in a TypeError.
- if (size > limit) throw tooLarge();
+ await reader.cancel().catch(() => {});
+ await part?.file?.handle.close().catch(() => {});
+ await dispose();
throw error;
}
}
diff --git a/lib/setup.ts b/lib/setup.ts
index e3cdccb..506caf5 100644
--- a/lib/setup.ts
+++ b/lib/setup.ts
@@ -1,4 +1,5 @@
import { pruneAdvanced, type AdvancedSettings } from "./advanced.ts";
+import { comfyUi } from "./comfy.ts";
import { pruneColor, type ColorSettings } from "./color.ts";
import { pruneControl, type ControlSettings } from "./control.ts";
import { pruneFace, type FaceSettings } from "./face.ts";
@@ -98,10 +99,7 @@ export function inspectSetup(graph: ApiGraph, registry: NodeRegistry): SetupResu
export async function checkSetup(graph: ApiGraph): Promise<SetupResult> {
try {
- const url = process.env.COMFYUI_URL || "http://127.0.0.1:8188";
- const response = await fetch(`${url.replace(/\/+$/, "")}/object_info`, { cache: "no-store", signal: AbortSignal.timeout(8000) });
- if (!response.ok) throw new Error("Node list unavailable");
- const registry = await response.json();
+ const registry = await comfyUi("/object_info", 8000);
if (!registry || typeof registry !== "object" || Array.isArray(registry)) throw new Error("Invalid node list");
return inspectSetup(graph, registry);
} catch {
diff --git a/test/http.test.ts b/test/http.test.ts
index 7ba26b2..b1ca24e 100644
--- a/test/http.test.ts
+++ b/test/http.test.ts
@@ -1,7 +1,8 @@
import assert from "node:assert/strict";
import { createServer, type Server } from "node:net";
import test from "node:test";
-import { apiAccess, authorize, HttpError, loopbackOnly, PASSWORD_REQUIRED, PASSWORD_TOO_SHORT, readFormData, validateJobId } from "../lib/http.ts";
+import { existsSync } from "node:fs";
+import { apiAccess, authorize, HttpError, IMAGE_SIGNATURE, loopbackOnly, PASSWORD_REQUIRED, PASSWORD_TOO_SHORT, readFormData, StoredFile, uploadName, validateJobId, VIDEO_SIGNATURE } from "../lib/http.ts";
import { getJob } from "../lib/comfy.ts";
import { register } from "../instrumentation.ts";
@@ -139,17 +140,18 @@ test("bounded multipart parsing preserves video bytes, duplicate pictures and ro
form.append("picture", new File([name], `${name}.png`, { type: "image/png" }));
form.append("role", name);
}
- const parsed = await readFormData(new Request("http://localhost/api/jobs", { method: "POST", body: form }), 4096);
+ const { form: parsed, dispose } = await readFormData(new Request("http://localhost/api/jobs", { method: "POST", body: form }), {}, 4096);
assert.deepEqual(parsed.getAll("role"), ["one", "two"]);
assert.deepEqual(parsed.getAll("picture").map((file) => (file as File).name), ["one.png", "two.png"]);
assert.deepEqual(new Uint8Array(await (parsed.get("video") as File).arrayBuffer()), new Uint8Array([0, 1, 255]));
+ await dispose();
});
test("declared oversize bodies are rejected without reading them", async () => {
let read = false;
const body = new ReadableStream({ pull() { read = true; } }, { highWaterMark: 0 });
const init = { method: "POST", body, duplex: "half", headers: { "Content-Length": "2048" } };
- await assert.rejects(readFormData(new Request("http://localhost/api/jobs", init), 1024), (e: unknown) => e instanceof HttpError && e.status === 413);
+ await assert.rejects(readFormData(new Request("http://localhost/api/jobs", init), {}, 1024), (e: unknown) => e instanceof HttpError && e.status === 413);
assert.equal(read, false);
});
@@ -164,9 +166,107 @@ test("chunked uploads cannot bypass the limit with a missing or false Content-Le
const headers = new Headers({ "Content-Type": "multipart/form-data; boundary=test" });
if (length !== null) headers.set("Content-Length", length);
const init = { method: "POST", body, duplex: "half", headers };
- await assert.rejects(readFormData(new Request("http://localhost/api/jobs", init), 1024), (e: unknown) => e instanceof HttpError && e.status === 413);
+ await assert.rejects(readFormData(new Request("http://localhost/api/jobs", init), {}, 1024), (e: unknown) => e instanceof HttpError && e.status === 413);
await new Promise<void>((resolve) => setImmediate(resolve));
assert.equal(canceled, true);
assert.equal(pulls, 3);
}
});
+
+async function multipart(form: FormData) {
+ const response = new Response(form);
+ return { body: new Uint8Array(await response.arrayBuffer()), type: response.headers.get("Content-Type")! };
+}
+
+function chunked(body: Uint8Array, type: string, size: number) {
+ let offset = 0;
+ const stream = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (offset >= body.length) return controller.close();
+ controller.enqueue(body.slice(offset, offset += size));
+ },
+ });
+ return new Request("http://localhost/api/jobs", { method: "POST", body: stream, duplex: "half", headers: { "Content-Type": type } } as RequestInit);
+}
+
+test("multipart parts split at any byte come back whole, with files on disk until disposed", async () => {
+ const video = new Uint8Array(3000).map((_, i) => i % 251);
+ video.set([13, 10, 45, 45], 1000);
+ const form = new FormData();
+ form.set("mode", "simple");
+ form.set("video", new File([video], "my clip?.mp4", { type: "video/mp4" }));
+ form.set("role", "the dancer — white polo");
+ form.append("picture", new File(["a"], "same.png", { type: "image/png" }));
+ form.append("picture", new File(["b"], "same.png", { type: "image/png" }));
+ const { body, type } = await multipart(form);
+ for (const size of [1, 2, 7, 64, 1000, body.length]) {
+ const { form: parsed, dispose } = await readFormData(chunked(body, type, size));
+ assert.equal(parsed.get("mode"), "simple");
+ assert.equal(parsed.get("role"), "the dancer — white polo");
+ const file = parsed.get("video") as StoredFile;
+ assert.ok(file instanceof StoredFile);
+ assert.equal(file.name, "my clip?.mp4");
+ assert.equal(file.type, "video/mp4");
+ assert.match(file.path, /my clip_\.mp4$/);
+ assert.deepEqual(new Uint8Array(await file.arrayBuffer()), video);
+ const pictures = parsed.getAll("picture") as StoredFile[];
+ assert.deepEqual(await Promise.all(pictures.map((picture) => picture.text())), ["a", "b"]);
+ assert.notEqual(pictures[0].path, pictures[1].path);
+ await dispose();
+ assert.equal(existsSync(file.path), false);
+ }
+});
+
+test("truncated or malformed multipart bodies are refused and leave no temp files", async () => {
+ const form = new FormData();
+ form.set("video", new File(["0123456789abcdef"], "clip.mp4"));
+ const { body, type } = await multipart(form);
+ const refuse = (bytes: Uint8Array, contentType = type) => assert.rejects(readFormData(chunked(bytes, contentType, 5)), (e: unknown) => e instanceof HttpError && e.status === 400);
+ await refuse(body.slice(0, body.length - 10));
+ await refuse(body.slice(0, 60));
+ await refuse(body, "application/x-www-form-urlencoded");
+ await refuse(new TextEncoder().encode(`--${type.split("boundary=")[1]}\r\n${"x".repeat(20000)}`));
+});
+
+test("file checks read the first bytes as they arrive and stop the upload there", async () => {
+ const checks = { video: { signature: VIDEO_SIGNATURE, error: "Not a video." }, picture: { signature: IMAGE_SIGNATURE, error: "Not a picture." } };
+ const form = new FormData();
+ form.set("video", new File(["#!/bin/sh", "x".repeat(100_000)], "clip.mp4", { type: "video/mp4" }));
+ const { body, type } = await multipart(form);
+ let read = 0;
+ const stream = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ controller.enqueue(body.slice(read, read += 1000));
+ if (read >= body.length) controller.close();
+ },
+ }, { highWaterMark: 0 });
+ const request = new Request("http://localhost/api/jobs", { method: "POST", body: stream, duplex: "half", headers: { "Content-Type": type } } as RequestInit);
+ await assert.rejects(readFormData(request, checks), (e: unknown) => e instanceof HttpError && e.status === 400 && e.message === "Not a video.");
+ assert.ok(read <= 1000);
+
+ const short = new FormData();
+ short.set("picture", new File([new Uint8Array([0xff, 0xd8, 0xff])], "tiny.jpg", { type: "image/jpeg" }));
+ short.set("video", new File([], "none.mp4"));
+ const { form: parsed, dispose } = await readFormData(new Request("http://localhost/api/jobs", { method: "POST", body: short }), checks);
+ assert.equal((parsed.get("picture") as File).size, 3);
+ await dispose();
+});
+
+test("signatures accept the containers ComfyUI reads and nothing else", () => {
+ const bytes = (...parts: (string | number[])[]) => Buffer.concat(parts.map((part) => Buffer.from(part as string, typeof part === "string" ? "latin1" : undefined))).toString("latin1");
+ for (const head of [bytes([0, 0, 0, 0x20], "ftypisom"), bytes([0, 0, 0, 0x14], "ftypqt "), bytes([0x1a, 0x45, 0xdf, 0xa3, 0x9f]), bytes("RIFF", [1, 2, 3, 4], "AVI LIST")]) assert.ok(VIDEO_SIGNATURE.test(head), head);
+ for (const head of [bytes("RIFF", [1, 2, 3, 4], "WAVE"), bytes("ftypisom...."), bytes("<html>"), bytes([0x89], "PNG\r\n\x1a\n")]) assert.ok(!VIDEO_SIGNATURE.test(head), head);
+ for (const head of [bytes([0x89], "PNG\r\n\x1a\n"), bytes([0xff, 0xd8, 0xff, 0xe0]), bytes("RIFF", [1, 2, 3, 4], "WEBPVP8 ")]) assert.ok(IMAGE_SIGNATURE.test(head), head);
+ for (const head of [bytes("GIF89a"), bytes("<svg>"), bytes("RIFF", [1, 2, 3, 4], "AVI "), bytes([0xff, 0xd8])]) assert.ok(!IMAGE_SIGNATURE.test(head), head);
+});
+
+test("upload names are safe as file names everywhere", () => {
+ assert.equal(uploadName("../../etc/passwd"), ".._.._etc_passwd");
+ assert.equal(uploadName("C:\\Users\\me\\clip.mp4"), "C_Users_me_clip.mp4");
+ assert.equal(uploadName("CON.mp4"), "_CON.mp4");
+ assert.equal(uploadName("lpt1"), "_lpt1");
+ assert.equal(uploadName("console.mp4"), "console.mp4");
+ assert.equal(uploadName("..."), "upload");
+ assert.equal(uploadName("clip.mp4. "), "clip.mp4");
+ assert.equal(uploadName(""), "upload");
+});
diff --git a/test/jobs.test.ts b/test/jobs.test.ts
new file mode 100644
index 0000000..12f0259
--- /dev/null
+++ b/test/jobs.test.ts
@@ -0,0 +1,177 @@
+import assert from "node:assert/strict";
+import { mkdtemp, readdir } from "node:fs/promises";
+import { register } from "node:module";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import test from "node:test";
+import { setTimeout as sleep } from "node:timers/promises";
+import { fakeProxy } from "./proxy.ts";
+
+// next/server has no exports map, so Node's ESM resolver needs the file name.
+register("data:text/javascript,export const resolve = (specifier, context, next) => next(specifier === 'next/server' ? 'next/server.js' : specifier, context);");
+
+const proxy = await fakeProxy();
+const temp = await mkdtemp(join(tmpdir(), "openjutsu-test-"));
+process.env.COMFY_BASE_URL = proxy.url;
+process.env.COMFYUI_URL = `${proxy.url}/`;
+process.env.TMPDIR = temp;
+delete process.env.OPENJUTSU_PASSWORD;
+delete process.env.COMFY_API_KEY;
+const { POST } = await import("../app/api/jobs/route.ts");
+const { GET: getModels } = await import("../app/api/models/route.ts");
+const { GET: getOutput } = await import("../app/api/jobs/[id]/outputs/[outputId]/route.ts");
+const { client } = await import("../lib/comfy.ts");
+// Each submitted job is watched until it ends; end them all so the watchers return.
+test.after(async () => {
+ for (const job of proxy.jobs.values()) if (job.status === "queued") proxy.finish(job.id, "canceled");
+ const inputs = (globalThis as { [key: symbol]: Map<string, unknown> })[Symbol.for("openjutsu.inputs")];
+ await until(() => inputs.size === 0);
+ await proxy.close();
+});
+
+let unique = 0;
+const mp4 = () => new File([new Uint8Array([0, 0, 0, 0x18]), "ftypisom", `clip ${++unique}`], "clip.mp4", { type: "video/mp4" });
+const png = () => new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), `photo ${++unique}`], "photo.png", { type: "image/png" });
+const SIMPLE = { mode: "simple", role: "", resolution: "480p", motion: "512", seed: "1", clip_seconds: "6", steps: "8", model: "base.safetensors" };
+
+function post(fields: Record<string, string | File>) {
+ const form = new FormData();
+ for (const [name, value] of Object.entries(fields)) form.append(name, value);
+ return POST(new Request("http://127.0.0.1:3000/api/jobs", { method: "POST", body: form }));
+}
+
+async function until(condition: () => boolean) {
+ for (let waited = 0; !condition(); waited += 50) {
+ if (waited > 8000) throw new Error("Timed out");
+ await sleep(50);
+ }
+}
+
+const leftovers = async () => (await readdir(temp)).filter((name) => name.startsWith("openjutsu-"));
+
+test("uploads stream from temp files, which are gone once the job is submitted", async () => {
+ const video = mp4();
+ const response = await post({ ...SIMPLE, video, picture: png() });
+ assert.equal(response.status, 202, JSON.stringify(await response.clone().json()));
+ const job = await response.json();
+ const inputs = proxy.jobs.get(job.id)!.inputs.map((id) => proxy.assets.get(id)!);
+ assert.deepEqual(inputs.map((asset) => [asset.filePath, asset.size]).sort(), [["clip.mp4", video.size], ["photo.png", 8 + `photo ${unique}`.length]]);
+ assert.deepEqual(await leftovers(), []);
+});
+
+test("files are checked by their first bytes, not their declared type", async () => {
+ const uploads = proxy.uploads.length;
+ const fake = new File(["<html>not a video</html>"], "clip.mp4", { type: "video/mp4" });
+ let response = await post({ mode: "mask", video: fake });
+ assert.equal(response.status, 400);
+ assert.match((await response.json()).error, /MP4, MOV, MKV, WebM or AVI/);
+ response = await post({ ...SIMPLE, video: mp4(), picture: new File(["GIF89a......"], "photo.png", { type: "image/png" }) });
+ assert.equal(response.status, 400);
+ assert.match((await response.json()).error, /PNG, JPEG or WebP/);
+ response = await post({ mode: "mask", video: new File([], "clip.mp4", { type: "video/mp4" }) });
+ assert.equal((await response.json()).error, "Drop a source video.");
+ assert.equal(proxy.uploads.length, uploads);
+ assert.deepEqual(await leftovers(), []);
+});
+
+test("a job's uploads are deleted once it ends, and never its outputs", async () => {
+ const job = await (await post({ mode: "mask", video: mp4() })).json();
+ const [input] = proxy.jobs.get(job.id)!.inputs;
+ const result = proxy.output(job.id, "video/mp4");
+ await sleep(700);
+ assert.deepEqual(proxy.deleted, [], "a running job keeps its inputs");
+ proxy.finish(job.id, "failed", [result]);
+ await until(() => proxy.deleted.includes(input));
+ assert.ok(proxy.assets.has(result.id));
+});
+
+test("a shared upload is deleted after the last job using it, and one this app did not upload is kept", async () => {
+ const video = mp4();
+ const first = await (await post({ mode: "mask", video })).json();
+ const second = await (await post({ mode: "people", role: "the dancer", resolution: "480p", video })).json();
+ const [input] = proxy.jobs.get(first.id)!.inputs;
+ assert.deepEqual(proxy.jobs.get(second.id)!.inputs, [input], "the proxy hands identical bytes the same asset");
+ proxy.finish(first.id);
+ await sleep(1500);
+ assert.ok(!proxy.deleted.includes(input));
+ proxy.finish(second.id, "canceled");
+ await until(() => proxy.deleted.includes(input));
+ assert.equal(proxy.deleted.filter((id) => id === input).length, 1);
+
+ const bytes = new Uint8Array(await mp4().arrayBuffer());
+ const theirs = await client().assets.fromBytes(bytes, { filename: "clip.mp4" }).commit();
+ const reused = await (await post({ mode: "mask", video: new File([bytes], "clip.mp4", { type: "video/mp4" }) })).json();
+ assert.deepEqual(proxy.jobs.get(reused.id)!.inputs, [theirs]);
+ proxy.finish(reused.id);
+ await sleep(1500);
+ assert.ok(proxy.assets.has(theirs));
+});
+
+test("uploads for a submission the proxy rejects are deleted at once", async () => {
+ proxy.state.rejectJobs = true;
+ const response = await post({ mode: "mask", video: mp4() });
+ proxy.state.rejectJobs = false;
+ assert.equal(response.status, 502);
+ const upload = proxy.uploads.at(-1)!;
+ assert.ok(proxy.deleted.includes(upload.id));
+ assert.deepEqual(await leftovers(), []);
+});
+
+test("outputs are served only for their own job, as video, image or JSON, with nosniff", async () => {
+ const job = await (await post({ mode: "mask", video: mp4() })).json();
+ const other = await (await post({ mode: "mask", video: mp4() })).json();
+ const [input] = proxy.jobs.get(job.id)!.inputs;
+ const preview = proxy.output(job.id, "image/png", "png");
+ const foreign = proxy.output(other.id, "image/png", "theirs");
+ const page = proxy.output(job.id, "text/html", "<script>alert(1)</script>");
+ const loose = proxy.output(undefined, "image/png", "no job");
+ const fetchOutput = (outputId: string) => getOutput(new Request(`http://127.0.0.1:3000/api/jobs/${job.id}/outputs/${outputId}`), { params: Promise.resolve({ id: job.id, outputId }) });
+
+ let response = await fetchOutput(preview.id);
+ assert.equal(response.status, 200);
+ assert.equal(response.headers.get("Content-Type"), "image/png");
+ assert.equal(response.headers.get("X-Content-Type-Options"), "nosniff");
+ assert.equal(await response.text(), "png");
+ for (const id of [input, foreign.id, loose.id, "missing"]) assert.equal((await fetchOutput(id)).status, 404, id);
+ assert.equal((await fetchOutput(page.id)).status, 502);
+
+ const data = proxy.output(job.id, "application/json; charset=utf-8", "{}");
+ proxy.finish(job.id, "succeeded", [data]);
+ response = await fetchOutput(data.id);
+ assert.equal(response.headers.get("Content-Type"), "application/json");
+ assert.equal(response.headers.get("X-Content-Type-Options"), "nosniff");
+});
+
+test("COMFYUI_URL may end in a slash, and a failed model list is reported instead of skipping the check", async () => {
+ assert.deepEqual(await (await getModels(new Request("http://127.0.0.1:3000/api/models"))).json(), { models: ["base.safetensors"], loras: ["style.safetensors"] });
+ let response = await post({ ...SIMPLE, model: "other.safetensors", video: mp4(), picture: png() });
+ assert.equal((await response.json()).error, "Choose a base model ComfyUI has.");
+
+ const unet = proxy.models.UNETLoader;
+ Reflect.deleteProperty(proxy.models, "UNETLoader");
+ try {
+ response = await getModels(new Request("http://127.0.0.1:3000/api/models"));
+ assert.equal(response.status, 502);
+ assert.deepEqual({ ...(await response.json()), error: undefined }, { models: [], loras: [], error: undefined });
+ response = await post({ ...SIMPLE, video: mp4(), picture: png() });
+ assert.equal(response.status, 502);
+ assert.match((await response.json()).error, /COMFYUI_URL/);
+ } finally {
+ proxy.models.UNETLoader = unet;
+ }
+});
+
+test("three uploads can arrive at once, and a fourth is asked to wait", async () => {
+ const stalled = Array.from({ length: 3 }, () => {
+ let controller!: ReadableStreamDefaultController<Uint8Array>;
+ const body = new ReadableStream<Uint8Array>({ start(c) { controller = c; } });
+ const headers = { "Content-Type": "multipart/form-data; boundary=x" };
+ return { controller, response: POST(new Request("http://127.0.0.1:3000/api/jobs", { method: "POST", body, duplex: "half", headers } as RequestInit)) };
+ });
+ const busy = await post({ mode: "mask", video: mp4() });
+ assert.equal(busy.status, 429);
+ for (const { controller } of stalled) controller.close();
+ for (const { response } of stalled) assert.equal((await response).status, 400);
+ assert.equal((await post({ mode: "mask", video: mp4() })).status, 202);
+ assert.deepEqual(await leftovers(), []);
+});
diff --git a/test/proxy.ts b/test/proxy.ts
new file mode 100644
index 0000000..84fc5e9
--- /dev/null
+++ b/test/proxy.ts
@@ -0,0 +1,124 @@
+import { randomUUID } from "node:crypto";
+import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
+import type { AddressInfo } from "node:net";
+
+// Just enough of comfy-api-proxy 0.1.6 for the upload, job, output and delete paths. Like the proxy, identical bytes
+// map to one asset id, and uploads are counted rather than kept.
+type FakeAsset = { id: string; hash: string; size: number; contentType: string; filePath: string; jobId?: string; body?: string };
+type FakeJob = { id: string; status: string; inputs: string[]; outputs: FakeAsset[] };
+
+export async function fakeProxy() {
+ const assets = new Map<string, FakeAsset>();
+ const jobs = new Map<string, FakeJob>();
+ const deleted: string[] = [];
+ const uploads: FakeAsset[] = [];
+ const models = { UNETLoader: ["base.safetensors"], LoraLoaderModelOnly: ["style.safetensors"] };
+ const state = { rejectJobs: false };
+ let base = "";
+
+ const json = (response: ServerResponse, status: number, body: unknown) => {
+ response.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
+ };
+ const missing = (response: ServerResponse) => json(response, 404, { error: { code: "not_found", message: "Not found." } });
+ const assetJson = (asset: FakeAsset, createdNew?: boolean) => ({
+ id: asset.id, hash: asset.hash, size_bytes: asset.size, content_type: asset.contentType, file_path: asset.filePath,
+ created_at: new Date().toISOString(), url: `${base}/api/v2/assets/${asset.id}/content`, url_expires_at: new Date(Date.now() + 3600_000).toISOString(),
+ ...(createdNew === undefined ? {} : { created_new: createdNew }), ...(asset.jobId ? { job_id: asset.jobId } : {}),
+ });
+ const jobJson = (job: FakeJob) => ({
+ id: job.id, status: job.status, created_at: new Date().toISOString(), started_at: null, completed_at: null,
+ expires_at: new Date(Date.now() + 86400_000).toISOString(), queue_position: job.status === "queued" ? 0 : null, progress: null, error: null,
+ outputs: job.outputs.map((output) => ({ ...assetJson(output), node_id: "1", name: output.filePath, type: "video" })),
+ urls: { self: `${base}/api/v2/jobs/${job.id}`, events: `${base}/api/v2/jobs/${job.id}/events`, cancel: `${base}/api/v2/jobs/${job.id}/cancel` },
+ });
+ const text = async (request: IncomingMessage) => {
+ let body = "";
+ for await (const chunk of request) body += chunk;
+ return body;
+ };
+ // The SDK sends content_type, file_path and expected_hash before the file, so only the first bytes are decoded.
+ const upload = async (request: IncomingMessage) => {
+ let head = "";
+ let size = 0;
+ for await (const chunk of request as AsyncIterable<Buffer>) {
+ if (head.length < 4096) head += chunk.toString("latin1", 0, 4096);
+ size += chunk.length;
+ }
+ const field = (name: string) => head.match(new RegExp(`name="${name}"\\r\\n\\r\\n([^\\r]*)`))?.[1] ?? "";
+ const boundary = String(request.headers["content-type"]).match(/boundary=(.+)$/)![1];
+ const start = head.indexOf("\r\n\r\n", head.indexOf('name="file"')) + 4;
+ return { hash: field("expected_hash"), contentType: field("content_type"), filePath: field("file_path"), size: size - start - `\r\n--${boundary}--\r\n`.length };
+ };
+
+ const server = createServer(async (request, response) => {
+ const url = new URL(request.url!, base);
+ const path = url.pathname;
+ let match: RegExpMatchArray | null;
+ if ((match = path.match(/^\/object_info\/(\w+)$/))) {
+ const node = match[1] as keyof typeof models;
+ const input = node === "UNETLoader" ? "unet_name" : "lora_name";
+ return models[node] ? json(response, 200, { [node]: { input: { required: { [input]: [models[node]] } } } }) : missing(response);
+ }
+ if (request.method === "HEAD" && (match = path.match(/^\/api\/v2\/assets\/by-hash\/(.+)$/))) {
+ const known = [...assets.values()].some((asset) => asset.hash === decodeURIComponent(match![1]));
+ return response.writeHead(known ? 200 : 404).end();
+ }
+ if (request.method === "POST" && path === "/api/v2/assets/from-hash") {
+ const { hash } = JSON.parse(await text(request));
+ const asset = [...assets.values()].find((candidate) => candidate.hash === hash);
+ return asset ? json(response, 200, assetJson(asset, false)) : missing(response);
+ }
+ if (request.method === "POST" && path === "/api/v2/assets") {
+ const asset = { id: randomUUID(), ...(await upload(request)) };
+ assets.set(asset.id, asset);
+ uploads.push(asset);
+ return json(response, 201, assetJson(asset, true));
+ }
+ if ((match = path.match(/^\/api\/v2\/assets\/([^/]+)(\/content)?$/))) {
+ const asset = assets.get(decodeURIComponent(match[1]));
+ if (!asset) return missing(response);
+ if (request.method === "DELETE") {
+ assets.delete(asset.id);
+ deleted.push(asset.id);
+ return response.writeHead(204).end();
+ }
+ if (!match[2]) return json(response, 200, assetJson(asset));
+ return response.writeHead(200, { "Content-Type": asset.contentType }).end(asset.body ?? "");
+ }
+ if (request.method === "POST" && path === "/api/v2/jobs") {
+ const { workflow } = JSON.parse(await text(request));
+ const refs = [...JSON.stringify(workflow).matchAll(/"__type":"core\/ASSET","info":\{"id":"([^"]+)"/g)].map((ref) => ref[1]);
+ if (state.rejectJobs) return json(response, 422, { error: { code: "invalid_workflow", message: "Workflow rejected." } });
+ if (refs.some((id) => !assets.has(id))) return json(response, 422, { error: { code: "missing_asset", message: "Unresolvable core/ASSET reference(s)." } });
+ const job = { id: randomUUID(), status: "queued", inputs: refs, outputs: [] };
+ jobs.set(job.id, job);
+ return json(response, 201, jobJson(job));
+ }
+ if (request.method === "GET" && (match = path.match(/^\/api\/v2\/jobs\/([^/]+)$/))) {
+ const job = jobs.get(match[1]);
+ return job ? json(response, 200, jobJson(job)) : missing(response);
+ }
+ missing(response);
+ });
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
+
+ return {
+ url: base, assets, jobs, uploads, deleted, models, state,
+ // A job's own result or preview, or a file the proxy holds for no job.
+ output(jobId: string | undefined, contentType: string, body = "") {
+ const asset = { id: randomUUID(), hash: "", size: body.length, contentType, filePath: `out-${assets.size}`, jobId, body };
+ assets.set(asset.id, asset);
+ return asset;
+ },
+ finish(jobId: string, status = "succeeded", outputs: FakeAsset[] = []) {
+ Object.assign(jobs.get(jobId)!, { status, outputs });
+ },
+ close: () => new Promise<void>((resolve) => { server.closeAllConnections(); server.close(() => resolve()); }),
+ };
+}
+
+if (import.meta.main) {
+ const proxy = await fakeProxy();
+ console.log(proxy.url);
+}