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
// Pictures for the worker's ComfyUI (docs/gpu-workers.md). Every picture of an account's job ticket, and of a finishing
// job, is made again from its pixels before anything on this PC reads it: turned upright by its EXIF orientation, then
// written as a fresh PNG with nothing else of the file (no EXIF, no GPS, no XMP, no colour profile's text, no comment).
// Only one short-lived process at low priority ever reads the picture as it came (picture-child.mjs, with sharp), with
// a cap on its pixels and a time limit. The studio sends an account's pictures as they came, so this is the only place
// they are decoded.
import { spawn, spawnSync } from "node:child_process";
import fs from "node:fs";
import { createRequire } from "node:module";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const PICTURE_EXT = new Set(["png", "jpg", "jpeg", "webp", "gif", "bmp", "tif", "tiff", "avif", "heic", "heif"]);
const extOf = (name) => (String(name).includes(".") ? String(name).split(".").pop().toLowerCase() : "");
/** Is this input a picture: by its type, else by its name. */
export function isPicture(input) {
const type = String(input?.contentType ?? "").split(";")[0].trim().toLowerCase();
if (type) return type.startsWith("image/");
return PICTURE_EXT.has(extOf(input?.name));
}
/**
* Can this checkout make pictures again: is sharp installed beside it (pnpm install in the worker's checkout), and does
* it really make one (a tiny test picture, through the same process a job's picture goes through)? A copy of the
* checkout whose libraries lost their own parts finds sharp but can't load it: without the trial, every account job
* with a picture would fail there instead of going to another worker.
*/
export function canCleanPictures(resolve = (name) => createRequire(import.meta.url).resolve(name), trial = trialPicture) {
try {
resolve("sharp");
} catch {
return false;
}
return trial();
}
/** A 1x1 PNG: the test picture of the start-up trial. */
const TRIAL_PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", "base64");
/** Make the test picture again with picture-child.mjs, in a folder of its own that is removed after. True when it made one. */
export function trialPicture({ spawnSyncFn = spawnSync, node = process.execPath, timeoutMs = 60_000 } = {}) {
let dir;
try {
dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-picture-"));
const src = path.join(dir, "in.png");
const dst = path.join(dir, "out.png");
fs.writeFileSync(src, TRIAL_PNG);
const r = spawnSyncFn(node, [path.join(here, "picture-child.mjs"), src, dst, String(PICTURE_MAX_PIXELS)], { stdio: "ignore", windowsHide: true, env: childEnv(), cwd: dir, timeout: timeoutMs });
return r.status === 0 && fs.statSync(dst).size > 0;
} catch {
return false;
} finally {
if (dir) fs.rmSync(dir, { recursive: true, force: true });
}
}
/** The most pixels a picture may have (the studio's own limit for a picture it makes again). */
export const PICTURE_MAX_PIXELS = 64 * 2 ** 20;
/** The environment of the process that reads a picture: where things are, and nothing else of this PC's. */
function childEnv(env = process.env) {
const out = {};
for (const k of ["PATH", "Path", "SYSTEMROOT", "SystemRoot", "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "LANG"]) if (env[k] !== undefined) out[k] = env[k];
return out;
}
/**
* Make a picture again (`src` as it came, `dst` the new PNG) in its own process at low priority, stopped after
* `timeoutMs`. True when it made one.
*/
export function cleanPicture({ src, dst, timeoutMs = 2 * 60_000, spawnFn = spawn, node = process.execPath }) {
return new Promise((resolve) => {
let child;
try {
child = spawnFn(node, [path.join(here, "picture-child.mjs"), src, dst, String(PICTURE_MAX_PIXELS)], { stdio: ["ignore", "ignore", "ignore"], windowsHide: true, env: childEnv(), cwd: path.dirname(dst) });
} catch {
return resolve(false);
}
try {
if (child.pid) os.setPriority(child.pid, os.constants.priority.PRIORITY_BELOW_NORMAL);
} catch {}
const timer = setTimeout(() => child.kill("SIGKILL"), timeoutMs);
child.once("error", () => (clearTimeout(timer), resolve(false)));
child.once("exit", (code) => {
clearTimeout(timer);
let ok = false;
try {
ok = code === 0 && fs.statSync(dst).size > 0;
} catch {}
resolve(ok);
});
});
}