diff --git a/.gitignore b/.gitignore index 6c39835f6..732faaa58 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,5 @@ docs/telemetry.md __pycache__/ *.pyc coverage/ + +.apps-url diff --git a/e2e/assets.test.ts b/e2e/assets.test.ts new file mode 100644 index 000000000..fadc07a0d --- /dev/null +++ b/e2e/assets.test.ts @@ -0,0 +1,52 @@ +import { copyFile, mkdir, mkdtemp, rm } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { test, expect } from '@playwright/test' +import { launchApp, type AppContext } from './launchApp' + +const SIDEBAR_ASSETS = '[data-testid="app-sidebar-assets"]' +const ASSETS_GRID = '[data-testid="assets-grid"]' +const SAMPLE_IMAGE = path.resolve('src/renderer/src/assets/sidebar/hills-sunset.jpg') + +let ctx: AppContext +let outputDir: string + +test.describe.configure({ mode: 'serial' }) + +test.beforeAll(async () => { + outputDir = await mkdtemp(path.join(os.tmpdir(), 'comfy-assets-output-')) + await mkdir(path.join(outputDir, 'landscapes'), { recursive: true }) + await copyFile(SAMPLE_IMAGE, path.join(outputDir, 'landscapes', 'ComfyUI_00001_.jpg')) + ctx = await launchApp( + { settings: { firstUseCompleted: true, telemetryEnabled: false, outputDir } }, + { stayOnHome: true } + ) +}) + +test.afterAll(async () => { + await ctx.cleanup() + await rm(outputDir, { recursive: true, force: true }) +}) + +test('Assets shows the images in the output folder with no build running and no login @windows @macos @linux', async () => { + expect(await ctx.panel.click(SIDEBAR_ASSETS)).toBe(true) + + await ctx.panel.waitForVisible(ASSETS_GRID) + await expect + .poll( + () => + ctx.panel.evaluate( + `(() => { + const img = document.querySelector('${ASSETS_GRID} img') + return !!img && img.complete && img.naturalWidth > 0 + })()` + ), + { message: 'the first image from disk loads', timeout: 20_000 } + ) + .toBe(true) + const src = await ctx.panel.evaluate( + `document.querySelector('${ASSETS_GRID} img').getAttribute('src')` + ) + expect(src.startsWith('comfy-output://file/')).toBe(true) + expect(decodeURIComponent(src)).toContain('ComfyUI_00001_.jpg') +}) diff --git a/electron.vite.config.ts b/electron.vite.config.ts index c818da178..9661b2a02 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -32,7 +32,8 @@ export default defineConfig({ comfyTitleBarPreload: resolve(__dirname, 'src/preload/comfyTitleBarPreload.ts'), comfyTitlePopupPreload: resolve(__dirname, 'src/preload/comfyTitlePopupPreload.ts'), comfyTitleTooltipPreload: resolve(__dirname, 'src/preload/comfyTitleTooltipPreload.ts'), - comfySystemModalPreload: resolve(__dirname, 'src/preload/comfySystemModalPreload.ts') + comfySystemModalPreload: resolve(__dirname, 'src/preload/comfySystemModalPreload.ts'), + appsPreload: resolve(__dirname, 'src/preload/appsPreload.ts') } } } diff --git a/locales/en.json b/locales/en.json index 3f22fa11b..c148b2ac0 100644 --- a/locales/en.json +++ b/locales/en.json @@ -19,6 +19,8 @@ "builds": "Builds", "navigation": "Navigation", "home": "Home", + "apps": "Apps", + "assets": "Assets", "collapse": "Collapse sidebar", "expand": "Expand sidebar", "signInTitle": "Log in to Comfy", @@ -30,6 +32,46 @@ "crashedWithSignal": "Crashed ({signal})", "failedToStartReason": "Failed to start: {reason}" }, + "assets": { + "kindLabel": "Asset type", + "kind": { + "all": "All", + "image": "Images", + "video": "Videos", + "3d": "3D", + "audio": "Audio" + }, + "refresh": "Refresh assets", + "search": "Search by name...", + "viewOptions": "View options", + "layoutLabel": "Layout", + "layout": { + "masonry": "Masonry", + "grid": "Grid" + }, + "sortLabel": "Sort by", + "sort": { + "newest": "Newest first", + "oldest": "Oldest first" + }, + "showLabel": "Show", + "showName": "File name", + "showDetails": "File details", + "emptyTitle": "No assets yet", + "emptyBody": "Open an app to make assets.", + "loadFailed": "Couldn't load assets", + "download": "Download", + "downloadFailed": "Couldn't download asset", + "noMatches": "No matching assets", + "noMatchesHint": "Try another type or search.", + "filter": "Filter", + "signedOutTitle": "Log in to see your assets", + "signedOutBody": "Assets that you make in Comfy Cloud show here.", + "signIn": "Log in to Comfy Cloud" + }, + "apps": { + "loadFailed": "The apps page at {url} could not load." + }, "home": { "title": "Welcome. Let's create!", "openComfyUI": "Open ComfyUI", diff --git a/locales/zh.json b/locales/zh.json index dbf971861..03e0a9519 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -19,6 +19,8 @@ "builds": "构建", "navigation": "导航", "home": "主页", + "apps": "应用", + "assets": "资产", "collapse": "收起侧边栏", "expand": "展开侧边栏", "signInTitle": "登录 Comfy", @@ -30,6 +32,46 @@ "crashedWithSignal": "已崩溃({signal})", "failedToStartReason": "启动失败:{reason}" }, + "assets": { + "kindLabel": "资产类型", + "kind": { + "all": "全部", + "image": "图片", + "video": "视频", + "3d": "3D", + "audio": "音频" + }, + "refresh": "刷新资产", + "search": "按名称搜索...", + "viewOptions": "视图选项", + "layoutLabel": "布局", + "layout": { + "masonry": "瀑布流", + "grid": "网格" + }, + "sortLabel": "排序方式", + "sort": { + "newest": "最新优先", + "oldest": "最早优先" + }, + "showLabel": "显示", + "showName": "文件名", + "showDetails": "文件详情", + "emptyTitle": "暂无资产", + "emptyBody": "打开一个应用来生成资产。", + "loadFailed": "无法加载资产", + "download": "下载", + "downloadFailed": "无法下载资产", + "noMatches": "没有匹配的资产", + "noMatchesHint": "试试其他类型或搜索词。", + "filter": "筛选", + "signedOutTitle": "登录以查看你的资产", + "signedOutBody": "你在 Comfy Cloud 中生成的资产会显示在这里。", + "signIn": "登录 Comfy Cloud" + }, + "apps": { + "loadFailed": "无法加载应用页面:{url}" + }, "home": { "title": "欢迎。开始创作吧!", "openComfyUI": "打开 ComfyUI", diff --git a/package.json b/package.json index ba145589c..84c36d053 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "dev:bootstrap": "node -e \"process.env.COMFY_FORCE_BOOTSTRAP_GIT='1';require('child_process').execSync('electron-vite dev',{stdio:'inherit',env:process.env})\"", "build": "pnpm run typecheck && electron-vite build", "start": "electron-vite preview", + "apps": "node scripts/apps.mjs", "postinstall": "node ./scripts/postinstall.mjs", "todesktop:beforeInstall": "./scripts/todesktop-beforeInstall.cjs", "todesktop:beforeBuild": "./scripts/todesktop-beforeBuild.cjs", diff --git a/scripts/apps.mjs b/scripts/apps.mjs new file mode 100644 index 000000000..e1998fc61 --- /dev/null +++ b/scripts/apps.mjs @@ -0,0 +1,41 @@ +#!/usr/bin/env node +import { spawn, spawnSync } from 'node:child_process' +import { readFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import path from 'node:path' +import { fileURLToPath } from 'node:url' + +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') +const require = createRequire(import.meta.url) + +function urlFromFile() { + try { + return readFileSync(path.join(root, '.apps-url'), 'utf8').trim() + } catch { + return '' + } +} + +const appsUrl = process.env.COMFY_APPS_URL || urlFromFile() +const env = appsUrl ? { ...process.env, COMFY_APPS_URL: appsUrl } : { ...process.env } + +const electronVite = path.join( + path.dirname(require.resolve('electron-vite/package.json')), + 'bin', + 'electron-vite.js' +) +const build = spawnSync(process.execPath, [electronVite, 'build'], { + cwd: root, + stdio: 'inherit', + env +}) +if (build.status !== 0) process.exit(build.status ?? 1) + +console.log( + appsUrl + ? `[apps] Apps page: ${appsUrl}` + : "[apps] No apps address set (COMFY_APPS_URL or a .apps-url file); using Desktop's built-in one." +) +const electron = require('electron') +const app = spawn(electron, ['.'], { cwd: root, stdio: 'inherit', env }) +app.on('exit', (code, signal) => process.exit(code ?? (signal ? 1 : 0))) diff --git a/src/main/apps/appsBuild.ts b/src/main/apps/appsBuild.ts new file mode 100644 index 000000000..30cfc5f23 --- /dev/null +++ b/src/main/apps/appsBuild.ts @@ -0,0 +1,307 @@ +import type { IpcMainInvokeEvent, WebContents } from 'electron' +import { promises as fsp } from 'fs' +import os from 'os' +import path from 'path' + +import { getEntryByInstallationId } from '../host/registry' +import * as installations from '../installations' +import type { InstallationRecord } from '../installations' +import { detectGPUCached } from '../lib/gpu' +import { addInstallation, installInstance } from '../lib/ipc/registerInstallationHandlers' +import { dispatchSessionAction } from '../lib/ipc/sessionActions' +import { + _onLaunch, + _operationAborts, + _pendingPorts, + _runningSessions, + MARKER_FILE, + sourceMap +} from '../lib/ipc/shared' +import { installModelsDir } from '../lib/models' +import { dataDir, defaultDataRoot, defaultInstallDir } from '../lib/paths' +import { findAvailablePort } from '../lib/process' +import * as settings from '../settings' +import { buildPinnedVariant } from '../sources/standalone' +import { DEFAULT_LAUNCH_ARGS, variantMatchesHost } from '../sources/standalone/envPaths' +import { fetchR2Latest, fetchR2VendorReleases } from '../sources/standalone/r2Catalog' +import type { FieldOption } from '../types/sources' +import { + APPS_BUILD_FIRST_PORT, + APPS_BUILD_LAST_PORT, + APPS_BUILD_NAME, + APPS_BUILD_RELEASE, + appsLaunchArgs, + appsModelDirs, + appsOutputDir, + downloadBytes, + findAppsBuild, + localOff, + pickVariant, + type SetupEvent, + setupEnd, + setupEvent +} from './appsBuildRules' + +export function categoryOf(inst: InstallationRecord): string | undefined { + return sourceMap[inst.sourceId]?.category +} + +export function launchInstall( + install: InstallationRecord, + actionData?: Record +): Promise<{ ok: boolean; message?: string }> { + const sender = { isDestroyed: () => false, send: () => {} } as unknown as WebContents + const event = { sender } as unknown as IpcMainInvokeEvent + const ctx = { event, installationId: install.id, inst: install, actionData } + return dispatchSessionAction(ctx, 'launch').catch((err: unknown) => ({ + ok: false, + message: (err as Error)?.message + })) +} + +let supported: Promise | null = null + +export function appsLocalOff(): Promise { + const check = (supported ??= detectGPUCached().then((gpu) => localOff(process.platform, gpu?.id))) + return check.then((off) => { + if (off) supported = null + return off + }) +} + +interface Setup { + id: string | null + cancelled: boolean + promise: Promise +} + +let setup: Setup | null = null + +export function appsSetupRunning(): boolean { + return setup !== null +} + +let appsId: string | null = null + +export function appsBuildId(): string | null { + return appsId +} + +export async function findApps(): Promise { + const inst = findAppsBuild(await installations.list()) + if (inst) appsId = inst.id + else if (!setup) appsId = null + return inst +} + +interface Plan { + data: Record + bytes: number +} + +let plan: Promise | null = null + +export function planAppsBuild(): Promise { + plan ??= makePlan().catch((err: unknown) => { + plan = null + throw err + }) + return plan +} + +async function makePlan(): Promise { + const source = sourceMap['standalone'] + if (!source?.getFieldOptions) throw new Error('Comfy Desktop cannot set up ComfyUI here.') + const gpu = (await detectGPUCached())?.id + const latest = await fetchR2Latest() + const ids = Object.keys(latest).filter((id) => variantMatchesHost(id)) + const vendorReleases = Object.fromEntries( + await Promise.all(ids.map(async (id) => [id, await fetchR2VendorReleases(id)] as const)) + ) + const release: FieldOption = { + value: APPS_BUILD_RELEASE, + label: APPS_BUILD_RELEASE, + data: { tag: APPS_BUILD_RELEASE, vendorReleases } + } + const picked = pickVariant(await source.getFieldOptions('variant', { release }, { gpu })) + const variantId = picked?.data?.['variantId'] + const pinned = + typeof variantId === 'string' + ? buildPinnedVariant(release, variantId, APPS_BUILD_RELEASE, gpu) + : null + if (!pinned) { + throw new Error(`Comfy Desktop has no ComfyUI ${APPS_BUILD_RELEASE} for this computer.`) + } + const data = { + sourceId: source.id, + sourceLabel: source.label, + ...source.buildInstallation({ release, variant: pinned }) + } + return { data, bytes: downloadBytes((data as Record)['downloadFiles']) } +} + +async function storageFor(inst: InstallationRecord): Promise> { + const sharedOutput = settings.get('outputDir') || settings.defaults.outputDir + return { + useSharedModels: true, + useSharedInput: true, + useSharedOutput: false, + outputDir: appsOutputDir({ + dataRoot: defaultDataRoot(), + appData: dataDir(), + sharedOutput, + installPath: inst.installPath + }), + modelDirs: appsModelDirs(await installations.list(), categoryOf, installModelsDir), + launchArgs: appsLaunchArgs( + inst.launchArgs, + DEFAULT_LAUNCH_ARGS, + process.platform, + os.totalmem() + ) + } +} + +class SetupCancelled extends Error { + constructor() { + super('You cancelled setting up ComfyUI for apps.') + } +} + +function progressSender( + s: Setup, + totalBytes: number, + onEvent: (e: SetupEvent) => void +): WebContents { + let lastAt = 0 + let lastStep = '' + const send = (channel: string, payload: unknown) => { + if (s.cancelled && s.id) _operationAborts.get(s.id)?.abort() + if (channel !== 'install-progress' || !payload || typeof payload !== 'object') return + const { phase, ...detail } = payload as Record + const event = setupEvent(String(phase), detail, totalBytes) + if (!event) return + const now = Date.now() + if (event.step === lastStep && now - lastAt < 250) return + lastAt = now + lastStep = event.step + onEvent(event) + } + return { isDestroyed: () => false, send } as unknown as WebContents +} + +const STALE_STATUSES = new Set(['failed', 'installing', 'partial-delete']) + +async function dropStale(inst: InstallationRecord): Promise { + if (_operationAborts.has(inst.id) || !STALE_STATUSES.has(inst.status ?? '')) { + throw new Error('The Apps build is busy in Comfy Desktop. Try again in a minute.') + } + const marker = await fsp + .readFile(path.join(inst.installPath, MARKER_FILE), 'utf8') + .catch(() => null) + if (marker?.trim() === inst.id) { + await fsp.rm(inst.installPath, { recursive: true, force: true }).catch(() => { + throw new Error( + `Comfy Desktop could not remove the old Apps build at ${inst.installPath}. Delete that folder and try again.` + ) + }) + } + await installations.remove(inst.id) +} + +async function runSetup( + s: Setup, + found: InstallationRecord | null, + onEvent: (e: SetupEvent) => void +): Promise { + try { + if (found) await dropStale(found) + const { data, bytes } = await planAppsBuild() + if (s.cancelled) throw new SetupCancelled() + onEvent(setupEvent('download', { percent: 0 }, bytes)!) + const added = await addInstallation({ + ...data, + name: APPS_BUILD_NAME, + installPath: defaultInstallDir(), + appsBuild: true, + status: 'installing' + }) + if (!('entry' in added)) throw new Error(added.message) + const id = added.entry.id + s.id = id + appsId = id + await installations.update(id, await storageFor(added.entry)) + if (s.cancelled) { + await installations.remove(id) + throw new SetupCancelled() + } + const result = (await installInstance(progressSender(s, bytes, onEvent), id)) as { + message?: string + } + const inst = await installations.get(id) + if (s.cancelled) throw new SetupCancelled() + if (inst?.status === 'installed') { + onEvent(setupEnd('done')) + return inst + } + if (!inst) throw new SetupCancelled() + throw new Error(result.message || 'ComfyUI for apps did not install.') + } catch (err) { + const cancelled = err instanceof SetupCancelled + onEvent(setupEnd(cancelled ? 'cancelled' : 'failed', (err as Error)?.message)) + throw err + } +} + +export async function ensureAppsBuild( + onEvent: (e: SetupEvent) => void +): Promise { + const found = await findApps() + if (found?.status === 'installed') return found + if (!setup) { + const s = { id: null, cancelled: false } as unknown as Setup + s.promise = runSetup(s, found, onEvent).finally(() => { + if (setup === s) setup = null + }) + setup = s + } + return setup.promise +} + +export function cancelAppsSetup(): { cancelled: boolean } { + if (!setup) return { cancelled: false } + setup.cancelled = true + if (setup.id) _operationAborts.get(setup.id)?.abort() + return { cancelled: true } +} + +export async function launchAppsBuild( + inst: InstallationRecord, + withWindow = false +): Promise<{ ok: boolean; message?: string }> { + const fresh = (await installations.update(inst.id, await storageFor(inst))) ?? inst + const taken = new Set([ + ..._pendingPorts.keys(), + ...[..._runningSessions.values()].map((s) => s.port) + ]) + const port = await findAvailablePort( + '127.0.0.1', + APPS_BUILD_FIRST_PORT, + APPS_BUILD_LAST_PORT, + taken + ).catch(() => null) + if (!port) return { ok: false, message: 'No free port for ComfyUI for apps.' } + return launchInstall(fresh, { + portOverride: port, + ...(withWindow ? {} : { launchModeOverride: 'console' }) + }) +} + +export function showComfyWindow(inst: InstallationRecord): boolean { + const session = _runningSessions.get(inst.id) + if (!session || !_onLaunch) return false + const entry = getEntryByInstallationId(inst.id) + if (entry && !entry.window.isDestroyed()) return true + _onLaunch({ port: session.port, process: session.proc, installation: inst, mode: 'window' }) + return true +} diff --git a/src/main/apps/appsBuildRules.test.ts b/src/main/apps/appsBuildRules.test.ts new file mode 100644 index 000000000..3487d2b0d --- /dev/null +++ b/src/main/apps/appsBuildRules.test.ts @@ -0,0 +1,368 @@ +// @vitest-environment node +import path from 'path' +import { describe, expect, it } from 'vitest' + +import { + APPS_BUILD_IDLE_MS, + APPS_BUILD_RELEASE, + APPS_SETUP_STAGE, + appsLaunchArgs, + appsModelDirs, + appsOutputDir, + downloadBytes, + FREE_IDLE_MS, + FREE_JSON, + findAppsBuild, + freeWait, + isRunEnd, + LINUX_NO_GPU, + localOff, + MAC_GPU_ONLY_ARG, + MAC_GPU_ONLY_MIN_BYTES, + MAC_NO_GPU, + pickOpenTarget, + pickVariant, + queueEmpty, + setupEnd, + setupEvent, + shouldStopIdle +} from './appsBuildRules' +import { localInstalls } from './guards' + +const category = (inst: { sourceId: string }) => + ({ standalone: 'local', git: 'local', desktop: 'local', cloud: 'cloud' })[inst.sourceId] + +describe('the pinned release', () => { + it('is one ComfyUI bundle tag', () => { + expect(APPS_BUILD_RELEASE).toMatch(/^v\d+\.\d+\.\d+-env\d+$/) + }) +}) + +describe('localOff', () => { + it('turns This computer off on Linux with no supported GPU', () => { + expect(localOff('linux', null)).toBe(LINUX_NO_GPU) + expect(localOff('linux', undefined)).toBe(LINUX_NO_GPU) + expect(localOff('linux', 'mps')).toBe(LINUX_NO_GPU) + }) + + it('keeps This computer on Linux with NVIDIA, AMD or Intel Arc', () => { + for (const gpu of ['nvidia', 'amd', 'intel']) expect(localOff('linux', gpu), gpu).toBeNull() + }) + + it('says why in one line', () => { + expect(LINUX_NO_GPU).toBe('This computer needs an NVIDIA, AMD or Intel Arc GPU on Linux.') + }) + + it('turns it off on a Mac without Apple Silicon', () => { + expect(localOff('darwin', null)).toBe(MAC_NO_GPU) + expect(localOff('darwin', 'mps')).toBeNull() + }) + + it('leaves Windows on, with or without a GPU', () => { + expect(localOff('win32', null)).toBeNull() + expect(localOff('win32', 'nvidia')).toBeNull() + }) +}) + +describe('findAppsBuild', () => { + it('finds the Apps build by its record mark, not its name', () => { + const list = [ + { id: 'mine', name: 'Apps', sourceId: 'standalone' }, + { id: 'apps', name: 'Renamed by the person', sourceId: 'standalone', appsBuild: true } + ] + expect(findAppsBuild(list)?.id).toBe('apps') + }) + + it('finds none when no record is marked', () => { + expect(findAppsBuild([{ id: 'a', appsBuild: 'true' }, { id: 'b' }])).toBeNull() + }) +}) + +describe('appsOutputDir', () => { + const places = { + dataRoot: '/Users/v', + appData: '/Users/v/Library/Application Support/comfyui-desktop-2', + sharedOutput: '/Users/v/ComfyUI-Shared/output', + installPath: '/Users/v/ComfyUI-Installs/Apps' + } + + it('puts results next to the shared folders, in their own folder', () => { + expect(appsOutputDir(places, path.posix)).toBe('/Users/v/ComfyUI-Apps/output') + }) + + it('is never the shared output folder or inside the build folder', () => { + const dir = appsOutputDir(places, path.posix) + expect(dir.startsWith(places.sharedOutput)).toBe(false) + expect(dir.startsWith(places.installPath + '/')).toBe(false) + }) + + it('moves to the app data folder when the shared output folder would hold it', () => { + expect(appsOutputDir({ ...places, sharedOutput: '/Users/v/ComfyUI-Apps' }, path.posix)).toBe( + '/Users/v/Library/Application Support/comfyui-desktop-2/ComfyUI-Apps/output' + ) + }) + + it('moves when the build folder would hold it', () => { + expect(appsOutputDir({ ...places, installPath: '/Users/v/ComfyUI-Apps' }, path.posix)).toBe( + '/Users/v/Library/Application Support/comfyui-desktop-2/ComfyUI-Apps/output' + ) + }) + + it('works with Windows paths', () => { + const dir = appsOutputDir( + { + dataRoot: 'C:\\', + appData: 'C:\\Users\\v\\AppData\\Roaming\\comfyui-desktop-2', + sharedOutput: 'C:\\ComfyUI-Shared\\output', + installPath: 'C:\\ComfyUI-Installs\\Apps' + }, + path.win32 + ) + expect(dir).toBe('C:\\ComfyUI-Apps\\output') + }) +}) + +describe('appsModelDirs', () => { + const modelsDirOf = (installPath: string) => `${installPath}/ComfyUI/models` + + it('adds the model folders of builds that do not share models', () => { + const list = [ + { + id: 'own', + installPath: '/i/Own', + sourceId: 'standalone', + status: 'installed', + useSharedModels: false, + modelDirs: ['/big/models'] + }, + { id: 'shared', installPath: '/i/Shared', sourceId: 'standalone', status: 'installed' }, + { + id: 'apps', + installPath: '/i/Apps', + sourceId: 'standalone', + appsBuild: true, + useSharedModels: false + } + ] + expect(appsModelDirs(list, category, modelsDirOf)).toEqual([ + '/i/Own/ComfyUI/models', + '/big/models' + ]) + }) + + it('leaves out Cloud, legacy Desktop, unfinished builds and repeats', () => { + const list = [ + { id: 'c', installPath: '', sourceId: 'cloud', useSharedModels: false }, + { id: 'd', installPath: '/i/D', sourceId: 'desktop', useSharedModels: false }, + { + id: 'f', + installPath: '/i/F', + sourceId: 'standalone', + status: 'failed', + useSharedModels: false + }, + { + id: 'a', + installPath: '/i/A', + sourceId: 'git', + useSharedModels: false, + modelDirs: ['/m', '/m'] + }, + { id: 'b', installPath: '/i/B', sourceId: 'git', useSharedModels: false, modelDirs: ['/m'] } + ] + expect(appsModelDirs(list, category, modelsDirOf)).toEqual([ + '/i/A/ComfyUI/models', + '/m', + '/i/B/ComfyUI/models' + ]) + }) +}) + +describe('pickVariant', () => { + it('takes the variant Desktop recommends for this machine, else the first', () => { + const options: Array<{ value: string; recommended?: boolean }> = [ + { value: 'cpu' }, + { value: 'nvidia', recommended: true } + ] + expect(pickVariant(options)?.value).toBe('nvidia') + expect(pickVariant(options.map((o) => ({ ...o, recommended: false })))?.value).toBe('cpu') + expect(pickVariant([])).toBeNull() + }) +}) + +describe('downloadBytes', () => { + it('adds up the bundle files', () => { + expect(downloadBytes([{ size: 810_000_000 }, { size: 5 }])).toBe(810_000_005) + expect(downloadBytes([{ size: 'x' }, null])).toBe(0) + expect(downloadBytes(undefined)).toBe(0) + }) +}) + +describe('setup progress for the app page', () => { + it('turns a download percent into bytes when Desktop knows the size', () => { + expect(setupEvent('download', { percent: 25, status: 'x' }, 800)).toEqual({ + stage: APPS_SETUP_STAGE, + step: 'Downloading ComfyUI', + percent: 25, + state: 'running', + done: 200, + total: 800 + }) + }) + + it('gives no bytes when the size or percent is unknown', () => { + expect(setupEvent('download', { percent: 25 }, 0)).not.toHaveProperty('total') + expect(setupEvent('setup', { percent: -1 }, 800)).toEqual({ + stage: APPS_SETUP_STAGE, + step: 'Setting up Python', + percent: -1, + state: 'running' + }) + }) + + it('passes no install text or paths from Desktop to the page', () => { + const e = setupEvent('cleanup', { percent: 50, status: '/Users/v/secret', path: '/x' }, 0) + expect(Object.keys(e!).sort()).toEqual(['percent', 'stage', 'state', 'step']) + }) + + it('skips the step list and the final done message', () => { + expect(setupEvent('steps', { steps: [] }, 0)).toBeNull() + expect(setupEvent('done', { percent: 100 }, 0)).toBeNull() + }) + + it('ends with done, failed or cancelled', () => { + expect(setupEnd('done')).toMatchObject({ state: 'done', percent: 100 }) + expect(setupEnd('failed', 'No disk space')).toMatchObject({ + state: 'failed', + error: 'No disk space' + }) + expect(setupEnd('cancelled')).toMatchObject({ state: 'cancelled' }) + }) +}) + +describe('freeing memory after an app run', () => { + it('frees after a run ends, however it ended', () => { + for (const type of ['execution_success', 'execution_error', 'execution_interrupted']) { + expect(isRunEnd(type), type).toBe(true) + } + for (const type of ['executing', 'progress', 'status', 'execution_start', 7]) { + expect(isRunEnd(type), String(type)).toBe(false) + } + }) + + it('frees only when nothing else waits in the queue', () => { + expect(queueEmpty({ exec_info: { queue_remaining: 0 } })).toBe(true) + expect(queueEmpty({ exec_info: { queue_remaining: 2 } })).toBe(false) + expect(queueEmpty({})).toBe(false) + expect(queueEmpty(null)).toBe(false) + }) + + it('asks ComfyUI to unload models and free memory', () => { + expect(JSON.parse(FREE_JSON)).toEqual({ unload_models: true, free_memory: true }) + }) + + it('keeps models loaded until the build has been idle for a few minutes', () => { + const now = 1_000_000_000 + expect(freeWait(now, now)).toBe(FREE_IDLE_MS) + expect(freeWait(now - 60_000, now)).toBe(FREE_IDLE_MS - 60_000) + expect(freeWait(now - FREE_IDLE_MS, now)).toBe(0) + expect(freeWait(now - FREE_IDLE_MS - 1, now)).toBe(0) + }) + + it('waits long enough for the next run to reuse the loaded models, and frees before the build stops', () => { + expect(FREE_IDLE_MS).toBeGreaterThanOrEqual(2 * 60_000) + expect(FREE_IDLE_MS).toBeLessThan(APPS_BUILD_IDLE_MS) + }) +}) + +describe('Apps build launch arguments', () => { + const big = 48 * 1024 ** 3 + const small = 16 * 1024 ** 3 + + it('runs text encoders on the GPU on a Mac with room for them', () => { + expect(appsLaunchArgs('--enable-manager', '', 'darwin', big)).toBe( + `--enable-manager ${MAC_GPU_ONLY_ARG}` + ) + expect(appsLaunchArgs(undefined, '--enable-manager', 'darwin', MAC_GPU_ONLY_MIN_BYTES)).toBe( + `--enable-manager ${MAC_GPU_ONLY_ARG}` + ) + expect(appsLaunchArgs('', '--enable-manager', 'darwin', big)).toBe(MAC_GPU_ONLY_ARG) + }) + + it('adds the flag once', () => { + const once = appsLaunchArgs('--enable-manager', '', 'darwin', big) + expect(appsLaunchArgs(once, '', 'darwin', big)).toBe(once) + }) + + it('leaves smaller Macs and other systems as they are', () => { + expect(appsLaunchArgs('--enable-manager', '', 'darwin', small)).toBe('--enable-manager') + expect(appsLaunchArgs('--enable-manager', '', 'linux', big)).toBe('--enable-manager') + expect(appsLaunchArgs('--enable-manager', '', 'win32', big)).toBe('--enable-manager') + expect(appsLaunchArgs(42, '--enable-manager', 'linux', big)).toBe('--enable-manager') + }) +}) + +describe('stopping the Apps build when idle', () => { + const now = 1_000_000_000 + + it('stops a console Apps build after the idle time', () => { + expect( + shouldStopIdle({ mode: 'console', hasWindow: false, lastUse: now - APPS_BUILD_IDLE_MS, now }) + ).toBe(true) + }) + + it('keeps it while it was used recently', () => { + expect(shouldStopIdle({ mode: 'console', hasWindow: false, lastUse: now - 1000, now })).toBe( + false + ) + }) + + it('keeps it when the person has it open in a window', () => { + expect(shouldStopIdle({ mode: 'console', hasWindow: true, lastUse: 0, now })).toBe(false) + expect(shouldStopIdle({ mode: 'window', hasWindow: false, lastUse: 0, now })).toBe(false) + }) +}) + +describe('which build Open in ComfyUI picks', () => { + const list = [ + { id: 'apps', name: 'Apps', sourceId: 'standalone', appsBuild: true, lastLaunchedAt: 99 }, + { id: 'old', name: 'Old', sourceId: 'standalone', status: 'installed', lastLaunchedAt: 1 }, + { id: 'new', name: 'New', sourceId: 'git', lastLaunchedAt: 5 } + ] + const own = localInstalls(list, category) + + it('never offers the Apps build as the person own build', () => { + expect(own.map((i) => i.id)).toEqual(['new', 'old']) + }) + + it('picks the person own build launched last, skipping the Apps build', () => { + expect(pickOpenTarget(own, new Map())?.id).toBe('new') + }) + + it('prefers one of their builds that is running', () => { + expect(pickOpenTarget(own, new Map([['old', 10]]))?.id).toBe('old') + expect( + pickOpenTarget( + own, + new Map([ + ['old', 10], + ['new', 20] + ]) + )?.id + ).toBe('new') + }) + + it('ignores a running Apps build', () => { + expect(pickOpenTarget(own, new Map([['apps', 50]]))?.id).toBe('new') + }) + + it('opens the build the person picked, only if it is theirs', () => { + expect(pickOpenTarget(own, new Map(), 'old')?.id).toBe('old') + expect(pickOpenTarget(own, new Map(), 'apps')).toBeNull() + expect(pickOpenTarget(own, new Map(), 'gone')).toBeNull() + }) + + it('has nothing to pick when the person has no build of their own', () => { + expect(pickOpenTarget([], new Map([['apps', 50]]))).toBeNull() + }) +}) diff --git a/src/main/apps/appsBuildRules.ts b/src/main/apps/appsBuildRules.ts new file mode 100644 index 000000000..00182290e --- /dev/null +++ b/src/main/apps/appsBuildRules.ts @@ -0,0 +1,199 @@ +import path from 'path' +import { isAppsBuild } from '../../shared/appsBuild' + +export const APPS_BUILD_NAME = 'Apps' +export const APPS_BUILD_RELEASE = 'v0.42.1-env1' +export const APPS_BUILD_FIRST_PORT = 8288 +export const APPS_BUILD_LAST_PORT = 8388 +export const APPS_BUILD_IDLE_MS = 10 * 60_000 +export const APPS_SETUP_STAGE = 'Setting up ComfyUI for apps' +export const FREE_JSON = JSON.stringify({ unload_models: true, free_memory: true }) +export const FREE_IDLE_MS = 5 * 60_000 +export const MAC_GPU_ONLY_ARG = '--gpu-only' +export const MAC_GPU_ONLY_MIN_BYTES = 36 * 1024 ** 3 + +export const LINUX_NO_GPU = 'This computer needs an NVIDIA, AMD or Intel Arc GPU on Linux.' +export const MAC_NO_GPU = 'This computer needs a Mac with Apple Silicon.' + +const LINUX_GPUS = ['nvidia', 'amd', 'intel'] + +export function localOff(platform: string, gpu: string | null | undefined): string | null { + if (platform === 'linux' && !LINUX_GPUS.includes(gpu ?? '')) return LINUX_NO_GPU + if (platform === 'darwin' && gpu !== 'mps') return MAC_NO_GPU + return null +} + +export function appsLaunchArgs( + launchArgs: unknown, + fallback: string, + platform: string, + totalBytes: number +): string { + const args = typeof launchArgs === 'string' ? launchArgs.trim() : fallback + const wanted = platform === 'darwin' && totalBytes >= MAC_GPU_ONLY_MIN_BYTES + if (!wanted || args.split(/\s+/).includes(MAC_GPU_ONLY_ARG)) return args + return `${args} ${MAC_GPU_ONLY_ARG}`.trim() +} + +export function findAppsBuild(list: readonly T[]): T | null { + return list.find(isAppsBuild) ?? null +} + +function within(dir: string, root: string, p: typeof path): boolean { + const rel = p.relative(root, dir) + return rel === '' || (!rel.startsWith('..') && !p.isAbsolute(rel)) +} + +export interface OutputDirPlaces { + dataRoot: string + appData: string + sharedOutput: string + installPath: string +} + +export function appsOutputDir(places: OutputDirPlaces, p: typeof path = path): string { + const candidates = [ + p.join(places.dataRoot, 'ComfyUI-Apps', 'output'), + p.join(places.appData, 'ComfyUI-Apps', 'output') + ] + const clashes = (dir: string) => + [places.sharedOutput, places.installPath].some( + (other) => within(dir, other, p) || within(other, dir, p) + ) + return candidates.find((dir) => !clashes(dir)) ?? candidates[1]! +} + +export interface ModelSharingInstall { + id: string + installPath: string + sourceId: string + status?: string + appsBuild?: unknown + useSharedModels?: boolean + modelDirs?: string[] +} + +export function appsModelDirs( + list: readonly T[], + categoryOf: (inst: T) => string | undefined, + modelsDirOf: (installPath: string) => string +): string[] { + const dirs: string[] = [] + for (const inst of list) { + if (isAppsBuild(inst) || categoryOf(inst) !== 'local') continue + if (inst.sourceId === 'desktop' || inst.useSharedModels !== false) continue + if (inst.status !== undefined && inst.status !== 'installed') continue + const own = inst.installPath ? [modelsDirOf(inst.installPath)] : [] + for (const dir of [...own, ...(inst.modelDirs ?? [])]) { + if (typeof dir === 'string' && dir && !dirs.includes(dir)) dirs.push(dir) + } + } + return dirs +} + +export function pickVariant(options: readonly T[]): T | null { + return options.find((o) => o.recommended) ?? options[0] ?? null +} + +export function downloadBytes(files: unknown): number { + if (!Array.isArray(files)) return 0 + return files.reduce( + (n: number, f: unknown) => + n + (typeof (f as { size?: unknown })?.size === 'number' ? (f as { size: number }).size : 0), + 0 + ) +} + +export interface SetupEvent { + stage: string + step: string + percent: number + state: 'running' | 'done' | 'failed' | 'cancelled' + done?: number + total?: number + error?: string +} + +const SETUP_STEPS: Record = { + download: 'Downloading ComfyUI', + extract: 'Unpacking it', + setup: 'Setting up Python', + cleanup: 'Finishing up' +} + +export function setupEvent( + phase: string, + detail: Record, + totalBytes: number +): SetupEvent | null { + if (phase === 'steps' || phase === 'done') return null + const raw = detail['percent'] + const percent = + typeof raw === 'number' && Number.isFinite(raw) && raw >= 0 ? Math.min(100, raw) : -1 + const event: SetupEvent = { + stage: APPS_SETUP_STAGE, + step: SETUP_STEPS[phase] ?? 'Setting up', + percent, + state: 'running' + } + if (phase === 'download' && totalBytes > 0 && percent >= 0) { + event.done = Math.round((totalBytes * percent) / 100) + event.total = totalBytes + } + return event +} + +export function setupEnd(state: 'done' | 'failed' | 'cancelled', error?: string): SetupEvent { + return { + stage: APPS_SETUP_STAGE, + step: state === 'done' ? 'Ready' : state === 'cancelled' ? 'Cancelled' : 'Failed', + percent: state === 'done' ? 100 : -1, + state, + ...(error ? { error } : {}) + } +} + +const RUN_ENDS = ['execution_success', 'execution_error', 'execution_interrupted'] + +export function isRunEnd(type: unknown): boolean { + return typeof type === 'string' && RUN_ENDS.includes(type) +} + +export function freeWait(lastUse: number, now: number, idleMs = FREE_IDLE_MS): number { + return Math.max(0, lastUse + idleMs - now) +} + +export function queueEmpty(json: unknown): boolean { + const info = (json as { exec_info?: { queue_remaining?: unknown } } | null)?.exec_info + return info?.queue_remaining === 0 +} + +export interface IdleCheck { + mode: string + hasWindow: boolean + lastUse: number + now: number + idleMs?: number +} + +export function shouldStopIdle({ + mode, + hasWindow, + lastUse, + now, + idleMs = APPS_BUILD_IDLE_MS +}: IdleCheck): boolean { + return mode === 'console' && !hasWindow && now - lastUse >= idleMs +} + +export function pickOpenTarget( + own: readonly T[], + startedAt: ReadonlyMap, + wanted?: string +): T | null { + if (wanted !== undefined) return own.find((inst) => inst.id === wanted) ?? null + const running = own + .filter((inst) => startedAt.has(inst.id)) + .sort((a, b) => startedAt.get(b.id)! - startedAt.get(a.id)!) + return running[0] ?? own[0] ?? null +} diff --git a/src/main/apps/appsView.ts b/src/main/apps/appsView.ts new file mode 100644 index 000000000..de86e90c4 --- /dev/null +++ b/src/main/apps/appsView.ts @@ -0,0 +1,600 @@ +import { app, ipcMain, session, shell, WebContentsView } from 'electron' +import type { IpcMainEvent, IpcMainInvokeEvent, Session, WebContents } from 'electron' +import { randomUUID } from 'crypto' +import { promises as fsp } from 'fs' +import path from 'path' + +import { CLOUD_CONFIG } from '../cloud/config' +import { getCloudSession } from '../devplatform/session' +import { comfyWindows } from '../host/registry' +import { signInToCloud } from '../lib/ipc/registerDevPlatformHandlers' +import * as settings from '../settings' +import { + appsOrigin, + appsUrl, + cleanSaveName, + collectOutputs, + isAppOpen, + isAllowedAppsPermission, + isAppsFrameUrl, + isDocumentFailure, + isInside, + isPageLoadFailure, + isRunId, + isVersionsIndex, + isWorkflow, + inJobGrace, + jobsAnswer, + MAX_RESULT_BYTES, + MAX_SAVE_BYTES, + MAX_UPLOAD_BYTES, + noteSubmitted, + readCapped, + type OutputFile, + readable, + type OutputKind, + saveExt, + savedFileName, + toOutputFile, + uploadForm, + VERSIONS_FILE +} from './guards' +import { stallGuard, streamToFile, writeNewFile } from './download' +import { + cancelSetup, + getLocal, + initLocalComfy, + localDownload, + localFetch, + startLocal, + syncLocalSocket +} from './localComfy' +import { localBuilds, openInComfy } from './openInComfy' + +const APPS_URL = appsUrl(process.env, app.isPackaged) +const APPS_ORIGIN = appsOrigin(APPS_URL) +const APPS_PARTITION = 'persist:comfy-apps' + +const views = new Map() + +const failedViews = new WeakSet() + +export const APPS_LOAD_FAILED_CHANNEL = 'comfy-apps:load-failed' +export const APPS_APP_OPEN_CHANNEL = 'comfy-apps:app-open' + +const appOpen = new Map() + +export interface AppsRect { + x: number + y: number + width: number + height: number + fresh?: boolean +} + +export type AppsAuthState = { signedIn: false } | { signedIn: true; email?: string } + +function openOutside(url: string): void { + if (url.startsWith('https://')) void shell.openExternal(url) +} + +function tellPanelLoadFailed(panel: WebContents | undefined, url: string): void { + if (panel && !panel.isDestroyed()) panel.send(APPS_LOAD_FAILED_CHANNEL, { url }) +} + +function tellPanelAppOpen(key: number): void { + const panel = comfyWindows.get(key)?.panelView?.webContents + if (panel && !panel.isDestroyed()) + panel.send(APPS_APP_OPEN_CHANNEL, { open: appOpen.get(key) === true }) +} + +function trackAppOpen(key: number, url: string): void { + appOpen.set(key, isAppOpen(url, APPS_URL)) + tellPanelAppOpen(key) +} + +function markLoadFailed(key: number, view: WebContentsView, url: string): void { + failedViews.add(view) + view.setVisible(false) + tellPanelLoadFailed(comfyWindows.get(key)?.panelView?.webContents, url || APPS_URL) +} + +let securedSession: Session | null = null + +function secureAppsSession(): void { + const appsSession = session.fromPartition(APPS_PARTITION) + if (securedSession === appsSession) return + appsSession.setPermissionCheckHandler((_contents, permission, requestingOrigin) => + isAllowedAppsPermission(permission, requestingOrigin, APPS_ORIGIN) + ) + appsSession.setPermissionRequestHandler((_contents, permission, callback, details) => + callback(isAllowedAppsPermission(permission, details.requestingUrl, APPS_ORIGIN)) + ) + securedSession = appsSession +} + +function keepOnAppsOrigin(event: { preventDefault(): void }, url: string): void { + if (isAppsFrameUrl(url, APPS_ORIGIN)) return + event.preventDefault() + openOutside(url) +} + +function createAppsView(key: number): WebContentsView { + secureAppsSession() + const view = new WebContentsView({ + webPreferences: { + preload: path.join(__dirname, '../preload/appsPreload.js'), + contextIsolation: true, + nodeIntegration: false, + sandbox: true, + partition: APPS_PARTITION + } + }) + view.setBorderRadius(14) + const wc = view.webContents + wc.on('will-navigate', keepOnAppsOrigin) + wc.on('will-redirect', (event, url, _isInPlace, isMainFrame) => { + if (isMainFrame) keepOnAppsOrigin(event, url) + }) + wc.setWindowOpenHandler(({ url }) => { + openOutside(url) + return { action: 'deny' } + }) + wc.on('did-fail-load', (_event, errorCode, _description, url, isMainFrame) => { + if (isMainFrame && isPageLoadFailure(errorCode)) markLoadFailed(key, view, url) + }) + wc.on('did-navigate', (_event, url, httpResponseCode) => { + if (isDocumentFailure(httpResponseCode)) markLoadFailed(key, view, url) + trackAppOpen(key, url) + }) + wc.on('did-navigate-in-page', (_event, url, isMainFrame) => { + if (isMainFrame) trackAppOpen(key, url) + }) + wc.loadURL(APPS_URL).catch(() => {}) + return view +} + +function entryForPanel( + sender: WebContents +): [number, NonNullable>] | null { + for (const [key, entry] of comfyWindows) { + if (entry.panelView?.webContents === sender) return [key, entry] + } + return null +} + +function showApps(event: IpcMainEvent, rect: unknown): void { + const found = entryForPanel(event.sender) + if (!found) return + if (!APPS_ORIGIN) return tellPanelLoadFailed(event.sender, APPS_URL) + const [key, entry] = found + const r = rect as Partial | null + if (!r || ![r.x, r.y, r.width, r.height].every((n) => typeof n === 'number' && isFinite(n))) + return + let view = views.get(key) + if (view?.webContents.isDestroyed()) view = undefined + if (view && failedViews.has(view)) { + if (r.fresh !== true) return + failedViews.delete(view) + view.webContents.loadURL(APPS_URL).catch(() => {}) + } + if (!view) { + view = createAppsView(key) + views.set(key, view) + entry.window.once('closed', () => { + views.delete(key) + appOpen.delete(key) + if (!view!.webContents.isDestroyed()) view!.webContents.close() + syncLocalSocket() + }) + } + const panel = entry.panelView!.getBounds() + view.setBounds({ + x: Math.round(panel.x + r.x!), + y: Math.round(panel.y + r.y!), + width: Math.round(r.width!), + height: Math.round(r.height!) + }) + entry.window.contentView.addChildView(view) + view.setVisible(true) + if (r.fresh === true) tellPanelAppOpen(key) +} + +function hideApps(event: IpcMainEvent): void { + const found = entryForPanel(event.sender) + if (!found) return + views.get(found[0])?.setVisible(false) +} + +function fromAppsPage(event: IpcMainInvokeEvent): boolean { + const ours = [...views.values()].some((v) => v.webContents === event.sender) + const frame = event.senderFrame + return ( + ours && !!frame && frame === event.sender.mainFrame && isAppsFrameUrl(frame.url, APPS_ORIGIN) + ) +} + +function authState(): AppsAuthState { + const status = getCloudSession().status() + return status.signedIn ? { signedIn: true, email: status.email } : { signedIn: false } +} + +const CLOUD_TIMEOUT_MS = 120_000 +const DOWNLOAD_STALL_MS = 60_000 + +async function cloudFetch(pathAndQuery: string, init: RequestInit = {}): Promise { + const token = await getCloudSession().getAccessToken() + if (!token) throw new Error('Sign in to Comfy to use apps.') + const headers = new Headers(init.headers) + headers.set('Authorization', `Bearer ${token}`) + const signal = init.signal ?? AbortSignal.timeout(CLOUD_TIMEOUT_MS) + return fetch(`${CLOUD_CONFIG.apiBase}${pathAndQuery}`, { ...init, headers, signal }).catch( + (err: unknown) => { + if ((err as Error)?.name === 'TimeoutError') { + throw new Error('Comfy Cloud took too long to answer.') + } + throw err + } + ) +} + +async function failure(res: Response): Promise { + if (res.status === 429) return new Error('Comfy Cloud is busy. Try again in a minute.') + const body = await res.text().catch(() => '') + const text = res.headers.get('content-type')?.includes('html') ? '' : `: ${body.slice(0, 300)}` + return new Error(`Comfy Cloud answered ${res.status}${text}`) +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) + +const MAX_BUSY_RETRIES = 4 + +function busyDelay(res: Response, attempt: number): number { + const asked = Number(res.headers.get('retry-after')) + return asked > 0 ? Math.min(asked, 30) * 1000 : 2000 * (attempt + 1) +} + +async function cloudFetchRetrying(pathAndQuery: string, init: RequestInit = {}): Promise { + for (let attempt = 0; ; attempt++) { + const res = await cloudFetch(pathAndQuery, init) + if (res.status !== 429 || attempt === MAX_BUSY_RETRIES) return res + await sleep(busyDelay(res, attempt)) + } +} + +async function cloudDownload( + pathAndQuery: string, + read: (res: Response, alive: () => void) => Promise +): Promise { + for (let attempt = 0; ; attempt++) { + const stall = stallGuard(DOWNLOAD_STALL_MS) + let res: Response | undefined + let delay: number + try { + res = await cloudFetch(pathAndQuery, { signal: stall.signal }) + if (res.status !== 429 || attempt === MAX_BUSY_RETRIES) { + if (!res.ok) throw await failure(res) + return await read(res, stall.alive) + } + delay = busyDelay(res, attempt) + await res.body?.cancel().catch(() => {}) + } catch (err) { + await res?.body?.cancel().catch(() => {}) + if (stall.signal.aborted) + throw new Error('Comfy Cloud stopped sending the result.', { cause: err }) + throw err + } finally { + stall.stop() + } + await sleep(delay) + } +} + +interface NodeError { + class_type?: string + errors?: Array<{ message?: string; details?: string }> +} + +async function run(workflow: unknown): Promise<{ runId: string }> { + if (!isWorkflow(workflow)) throw new Error('That is not a workflow.') + const token = await getCloudSession().getAccessToken() + if (!token) throw new Error('Sign in to Comfy to use apps.') + const res = await cloudFetch('/prompt', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ prompt: workflow, extra_data: { auth_token_comfy_org: token } }) + }) + if (!res.ok) throw await failure(res) + const body = (await res.json()) as { prompt_id?: string; node_errors?: Record } + const refused = Object.entries(body.node_errors ?? {})[0] + if (refused) { + const [node, e] = refused + const why = e.errors?.[0]?.details || e.errors?.[0]?.message || 'refused' + throw new Error(`Comfy Cloud would not run node ${node} (${e.class_type ?? 'unknown'}): ${why}`) + } + if (!isRunId(body.prompt_id)) throw new Error('Comfy Cloud did not start the run.') + noteSubmitted(submittedRuns, body.prompt_id, Date.now()) + return { runId: body.prompt_id } +} + +export interface RunStatus { + state: 'waiting' | 'done' | 'failed' + error?: string + running?: boolean + createdAt?: number + startedAt?: number + outputs?: Array + text?: Record +} + +const submittedRuns = new Map() + +async function runWithoutHistory(runId: string): Promise { + const res = await cloudFetch(`/jobs/${runId}`) + const answer = jobsAnswer(res.status, inJobGrace(submittedRuns, runId, Date.now())) + if (answer !== 'read') { + if (answer === 'refused') return { state: 'failed', error: (await failure(res)).message } + await res.body?.cancel().catch(() => {}) + return answer === 'wait' ? { state: 'waiting' } : { state: 'failed', error: answer.error } + } + submittedRuns.delete(runId) + const rec = (await res.json()) as { + status?: string + outputs_count?: number + create_time?: number + execution_start_time?: number + execution_error?: { exception_message?: string } + } + if (rec.status === 'completed' && !rec.outputs_count) + return { state: 'done', outputs: [], text: {} } + if (rec.status === 'failed' || rec.status === 'cancelled') { + return { + state: 'failed', + error: + readable(rec.execution_error?.exception_message) ?? `The run ${rec.status} on Comfy Cloud.` + } + } + return { + state: 'waiting', + running: rec.status === 'in_progress', + createdAt: typeof rec.create_time === 'number' ? rec.create_time : undefined, + startedAt: typeof rec.execution_start_time === 'number' ? rec.execution_start_time : undefined + } +} + +async function getRun(runId: unknown): Promise { + if (!isRunId(runId)) throw new Error('Unknown run.') + const res = await cloudFetch(`/history_v2/${runId}`) + if (res.status === 429) return { state: 'waiting' } + if (res.status === 404) return runWithoutHistory(runId) + if (!res.ok) throw await failure(res) + const body = (await res.json()) as Record + const entry = body[runId] + if (!entry) return { state: 'waiting' } + submittedRuns.delete(runId) + const status = entry.status ?? {} + if (status.status_str === 'error') { + const msg = status.messages?.find((m) => m[0] === 'execution_error')?.[1] as + | { exception_message?: string } + | undefined + return { + state: 'failed', + error: readable(msg?.exception_message) ?? 'The run failed on Comfy Cloud.' + } + } + if (!status.completed) return { state: 'waiting' } + const { files, text } = collectOutputs(entry.outputs) + return { state: 'done', outputs: files, text } +} + +async function cancelRun(runId: unknown): Promise<{ ok: boolean; status: number }> { + if (!isRunId(runId)) throw new Error('Unknown run.') + const res = await cloudFetch(`/jobs/${runId}/cancel`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: '{}' + }) + await res.body?.cancel().catch(() => {}) + return { ok: res.ok, status: res.status } +} + +interface HistoryEntry { + status?: { status_str?: string; completed?: boolean; messages?: Array<[string, unknown]> } + outputs?: unknown +} + +async function appsRoot(): Promise { + const root = path.join(settings.get('outputDir'), 'apps') + await fsp.mkdir(root, { recursive: true }) + return fsp.realpath(root) +} + +async function saveResult( + app: unknown, + label: unknown, + ext: string, + write: (file: string) => Promise +): Promise { + const slug = cleanSaveName(app) + const name = cleanSaveName(label) + if (!slug || !name) throw new Error('Name a result with letters, digits, dashes and spaces only.') + const dir = path.join(await appsRoot(), slug) + await fsp.mkdir(dir, { recursive: true }) + const base = savedFileName(name, ext) + for (let n = 1; ; n++) { + const file = path.join(dir, n === 1 ? base : base.replace(/(\.[a-z0-9]+)$/, ` ${n}$1`)) + try { + await write(file) + return file + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== 'EEXIST' || n > 50) throw err + } + } +} + +async function savedPath(file: unknown): Promise { + if (typeof file !== 'string' || !path.isAbsolute(file)) throw new Error('Unknown file.') + const real = await fsp.realpath(file).catch(() => null) + if (!real || !isInside(real, await appsRoot(), path.sep)) throw new Error('Unknown file.') + return real +} + +async function getOutput( + file: unknown, + save?: unknown +): Promise<{ type: string; bytes: Uint8Array; path?: string }> { + const f = toOutputFile(file) + if (!f) throw new Error('Unknown output.') + const ext = saveExt(f.filename.split('.').pop()) + if (save !== undefined && !ext) throw new Error('Apps cannot save that kind of file.') + const query = new URLSearchParams({ filename: f.filename, subfolder: f.subfolder, type: f.type }) + const view = `/view?${query}` + if (save === undefined || !ext) { + return cloudDownload(view, async (res, alive) => { + const bytes = await readCapped(res, MAX_RESULT_BYTES, alive) + if (!bytes) throw new Error('That result is larger than 100 MB, so Apps cannot show it.') + return { type: contentType(res), bytes } + }) + } + const s = (save ?? {}) as { app?: unknown; label?: unknown } + const saved = await cloudDownload(view, async (res, alive) => ({ + type: contentType(res), + path: await saveResult(s.app, s.label, ext, async (file) => { + if ((await streamToFile(res.body, file, MAX_SAVE_BYTES, alive)) === null) + throw new Error('That result is larger than 2 GB, so Apps cannot save it.') + }) + })) + if ((await fsp.stat(saved.path)).size > MAX_RESULT_BYTES) { + throw new Error(`Saved to ${saved.path}. It is larger than 100 MB, so Apps cannot show it.`) + } + return { ...saved, bytes: new Uint8Array(await fsp.readFile(saved.path)) } +} + +function contentType(res: Response): string { + return res.headers.get('content-type') ?? 'application/octet-stream' +} + +async function saveFile( + bytes: unknown, + app: unknown, + label: unknown, + ext: unknown +): Promise<{ path: string }> { + const e = saveExt(ext) + if (!(bytes instanceof Uint8Array) || !e) throw new Error('Apps cannot save that file.') + if (bytes.byteLength > MAX_RESULT_BYTES) throw new Error('Apps can save files up to 100 MB here.') + return { path: await saveResult(app, label, e, (file) => writeNewFile(file, bytes)) } +} + +async function readSaved(file: unknown): Promise { + const real = await savedPath(file) + if ((await fsp.stat(real)).size > MAX_RESULT_BYTES) + throw new Error('That file is larger than 100 MB, so Apps cannot open it.') + return new Uint8Array(await fsp.readFile(real)) +} + +async function saveVersions(app: unknown, bytes: unknown): Promise { + const slug = cleanSaveName(app) + if (!slug || !isVersionsIndex(bytes)) throw new Error('Apps cannot save that list.') + const dir = path.join(await appsRoot(), slug) + await fsp.mkdir(dir, { recursive: true }) + const file = path.join(dir, VERSIONS_FILE) + const tmp = `${file}.${randomUUID()}.tmp` + await fsp.writeFile(tmp, bytes) + await fsp.rename(tmp, file).catch(async (err: unknown) => { + await fsp.rm(tmp, { force: true }).catch(() => {}) + throw err + }) +} + +async function readVersions(app: unknown): Promise { + const slug = cleanSaveName(app) + if (!slug) throw new Error('Unknown app.') + const bytes = await fsp + .readFile(path.join(await appsRoot(), slug, VERSIONS_FILE)) + .catch(() => null) + return bytes ? new Uint8Array(bytes) : null +} + +let priceBadges: { at: number; badges: Promise> } | null = null + +async function readPriceBadges(): Promise> { + const res = await cloudFetchRetrying('/object_info') + if (!res.ok) throw await failure(res) + const info = (await res.json()) as Record + const badges = new Map() + for (const [name, node] of Object.entries(info)) + if (node?.price_badge) badges.set(name, node.price_badge) + return badges +} + +async function getNodePrice(node: unknown): Promise { + if (typeof node !== 'string' || !/^\w{1,80}$/.test(node)) throw new Error('Unknown node.') + if (!priceBadges || Date.now() - priceBadges.at > 60 * 60 * 1000) { + const badges = readPriceBadges() + priceBadges = { at: Date.now(), badges } + badges.catch(() => (priceBadges = null)) + } + return (await priceBadges.badges).get(node) ?? null +} + +async function showInFolder(file: unknown): Promise { + shell.showItemInFolder(await savedPath(file)) +} + +async function uploadImage(bytes: unknown, name: unknown): Promise<{ name: string }> { + if (!(bytes instanceof Uint8Array) || bytes.byteLength > MAX_UPLOAD_BYTES) { + throw new Error('Pick a file under 50 MB.') + } + const res = await cloudFetch('/upload/image', { method: 'POST', body: uploadForm(bytes, name) }) + if (!res.ok) throw await failure(res) + const body = (await res.json()) as { name?: string } + if (typeof body.name !== 'string') throw new Error('Comfy Cloud did not keep the file.') + return { name: body.name } +} + +function handleFromApps( + channel: string, + fn: (...args: A) => Promise | R +): void { + ipcMain.handle(channel, async (event, ...args) => { + if (!fromAppsPage(event)) throw new Error('Not allowed.') + return fn(...(args as A)) + }) +} + +function appsPages(): WebContents[] { + return [...views.values()] + .map((v) => v.webContents) + .filter((wc) => !wc.isDestroyed() && isAppsFrameUrl(wc.getURL(), APPS_ORIGIN)) +} + +export function registerAppsIpc(): void { + ipcMain.on('comfy-apps:show', showApps) + ipcMain.on('comfy-apps:hide', hideApps) + initLocalComfy(appsPages) + + handleFromApps('comfy-apps:get-auth-state', authState) + handleFromApps('comfy-apps:sign-in', async () => { + await signInToCloud() + return authState() + }) + handleFromApps('comfy-apps:run', run) + handleFromApps('comfy-apps:get-run', getRun) + handleFromApps('comfy-apps:cancel-run', cancelRun) + handleFromApps('comfy-apps:get-output', getOutput) + handleFromApps('comfy-apps:upload-image', uploadImage) + handleFromApps('comfy-apps:save-file', saveFile) + handleFromApps('comfy-apps:read-saved', readSaved) + handleFromApps('comfy-apps:show-in-folder', showInFolder) + handleFromApps('comfy-apps:save-versions', saveVersions) + handleFromApps('comfy-apps:read-versions', readVersions) + handleFromApps('comfy-apps:get-node-price', getNodePrice) + handleFromApps('comfy-apps:get-local', getLocal) + handleFromApps('comfy-apps:start-local', startLocal) + handleFromApps('comfy-apps:cancel-setup', cancelSetup) + handleFromApps('comfy-apps:local-builds', localBuilds) + handleFromApps('comfy-apps:local-fetch', localFetch) + handleFromApps('comfy-apps:local-download', localDownload) + handleFromApps('comfy-apps:open-in-comfy', openInComfy) +} diff --git a/src/main/apps/download.test.ts b/src/main/apps/download.test.ts new file mode 100644 index 000000000..0219d728d --- /dev/null +++ b/src/main/apps/download.test.ts @@ -0,0 +1,144 @@ +// @vitest-environment node +import { promises as fsp } from 'fs' +import os from 'os' +import path from 'path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { stallGuard, streamToFile, writeNewFile } from './download' + +const MB = 1024 * 1024 + +function chunked(count: number, size: number): ReadableStream { + let sent = 0 + return new ReadableStream({ + pull(controller) { + if (sent === count) return controller.close() + controller.enqueue(new Uint8Array(size).fill(sent % 256)) + sent++ + } + }) +} + +describe('writeNewFile', () => { + let dir: string + + beforeEach(async () => { + dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'apps-write-')) + }) + + afterEach(async () => { + vi.restoreAllMocks() + await fsp.rm(dir, { recursive: true, force: true }) + }) + + it('writes a new file', async () => { + const file = path.join(dir, 'still.png') + await writeNewFile(file, new Uint8Array([1, 2, 3])) + expect((await fsp.stat(file)).size).toBe(3) + }) + + it('removes the half-written file when the write fails', async () => { + const file = path.join(dir, 'still.png') + vi.spyOn(fsp, 'writeFile').mockImplementationOnce(async (target) => { + await fsp.appendFile(target as string, 'part') + throw Object.assign(new Error('no space left'), { code: 'ENOSPC' }) + }) + + await expect(writeNewFile(file, new Uint8Array(8))).rejects.toMatchObject({ code: 'ENOSPC' }) + await expect(fsp.stat(file)).rejects.toMatchObject({ code: 'ENOENT' }) + }) + + it('leaves a file that was already there alone', async () => { + const file = path.join(dir, 'taken.png') + await fsp.writeFile(file, 'keep') + + await expect(writeNewFile(file, new Uint8Array(8))).rejects.toMatchObject({ code: 'EEXIST' }) + expect(await fsp.readFile(file, 'utf8')).toBe('keep') + }) +}) + +describe('streamToFile', () => { + let dir: string + + beforeEach(async () => { + dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'apps-download-')) + }) + + afterEach(async () => { + await fsp.rm(dir, { recursive: true, force: true }) + }) + + it('streams a big file to disk chunk by chunk', async () => { + const file = path.join(dir, 'big.mp4') + const onChunk = vi.fn() + const size = await streamToFile(chunked(64, MB), file, 2_000_000_000, onChunk) + expect(size).toBe(64 * MB) + expect((await fsp.stat(file)).size).toBe(64 * MB) + expect(onChunk).toHaveBeenCalledTimes(64) + const handle = await fsp.open(file, 'r') + const last = Buffer.alloc(1) + await handle.read(last, 0, 1, 64 * MB - 1) + await handle.close() + expect(last[0]).toBe(63) + }) + + it('stops and removes the file when it goes over the limit', async () => { + const file = path.join(dir, 'over.mp4') + expect(await streamToFile(chunked(4, MB), file, 3 * MB)).toBeNull() + await expect(fsp.stat(file)).rejects.toMatchObject({ code: 'ENOENT' }) + }) + + it('never writes over a file that is already there', async () => { + const file = path.join(dir, 'taken.mp4') + await fsp.writeFile(file, 'keep') + await expect(streamToFile(chunked(1, 10), file, MB)).rejects.toMatchObject({ code: 'EEXIST' }) + expect(await fsp.readFile(file, 'utf8')).toBe('keep') + }) + + it('removes the partial file when the download breaks', async () => { + const file = path.join(dir, 'broken.mp4') + const body = new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array(10)) + controller.error(new Error('connection reset')) + } + }) + await expect(streamToFile(body, file, MB)).rejects.toThrow('connection reset') + await expect(fsp.stat(file)).rejects.toMatchObject({ code: 'ENOENT' }) + }) +}) + +describe('stallGuard', () => { + beforeEach(() => { + vi.useFakeTimers() + }) + + afterEach(() => { + vi.useRealTimers() + }) + + it('does not abort while data keeps coming', () => { + const guard = stallGuard(1000) + for (let i = 0; i < 10; i++) { + vi.advanceTimersByTime(900) + guard.alive() + } + expect(guard.signal.aborted).toBe(false) + guard.stop() + }) + + it('aborts when no data comes for the whole stall time', () => { + const guard = stallGuard(1000) + vi.advanceTimersByTime(900) + guard.alive() + vi.advanceTimersByTime(1000) + expect(guard.signal.aborted).toBe(true) + }) + + it('does not abort after it is stopped', () => { + const guard = stallGuard(1000) + guard.stop() + vi.advanceTimersByTime(5000) + expect(guard.signal.aborted).toBe(false) + }) +}) diff --git a/src/main/apps/download.ts b/src/main/apps/download.ts new file mode 100644 index 000000000..7fdc0eab3 --- /dev/null +++ b/src/main/apps/download.ts @@ -0,0 +1,66 @@ +import { promises as fsp } from 'fs' +import { Readable } from 'stream' +import { pipeline } from 'stream/promises' +import type { ReadableStream as WebReadableStream } from 'stream/web' + +export interface StallGuard { + signal: AbortSignal + alive: () => void + stop: () => void +} + +export function stallGuard(ms: number): StallGuard { + const controller = new AbortController() + let timer = setTimeout(() => controller.abort(), ms) + return { + signal: controller.signal, + alive: () => { + clearTimeout(timer) + timer = setTimeout(() => controller.abort(), ms) + }, + stop: () => clearTimeout(timer) + } +} + +class TooLarge extends Error {} + +export async function writeNewFile(file: string, bytes: Uint8Array): Promise { + try { + await fsp.writeFile(file, bytes, { flag: 'wx' }) + } catch (err) { + if ((err as NodeJS.ErrnoException).code !== 'EEXIST') { + await fsp.rm(file, { force: true }).catch(() => {}) + } + throw err + } +} + +export async function streamToFile( + body: ReadableStream | null, + file: string, + max: number, + onChunk: () => void = () => {} +): Promise { + const handle = await fsp.open(file, 'wx') + let size = 0 + try { + await pipeline( + body ? Readable.fromWeb(body as WebReadableStream) : Readable.from([]), + async function* (source: AsyncIterable) { + for await (const chunk of source) { + size += chunk.byteLength + if (size > max) throw new TooLarge() + onChunk() + yield chunk + } + }, + handle.createWriteStream() + ) + return size + } catch (err) { + await handle.close().catch(() => {}) + await fsp.rm(file, { force: true }).catch(() => {}) + if (err instanceof TooLarge) return null + throw err + } +} diff --git a/src/main/apps/guards.test.ts b/src/main/apps/guards.test.ts new file mode 100644 index 000000000..25f7db1ce --- /dev/null +++ b/src/main/apps/guards.test.ts @@ -0,0 +1,974 @@ +// @vitest-environment node +import { runInNewContext } from 'vm' +import { describe, expect, it, vi } from 'vitest' + +import { + isAllowedAppsPermission, + appsOrigin, + appsUrl, + cleanSaveName, + COMFY_READY_SCRIPT, + collectOutputs, + downloadRequest, + inJobGrace, + interruptJson, + JOB_GRACE_MS, + jobsAnswer, + DEFAULT_APPS_URL, + isAppOpen, + isAppsFrameUrl, + isBuildId, + isComfyPage, + isDocumentFailure, + isInside, + isPageLoadFailure, + isRunId, + isVersionsIndex, + isWorkflow, + localEvent, + localInstalls, + localRequest, + noteRun, + noteSubmitted, + openRequest, + openWorkflowScript, + outputKind, + queueAnswer, + queueJson, + queueView, + readable, + readCapped, + reconnectDelay, + type RunSeen, + safeUploadName, + saveExt, + savedFileName, + textAnswer, + toOutputFile, + uploadForm, + viewQuery, + workflowName +} from './guards' + +describe('appsUrl', () => { + it('defaults to the hosted site and takes an override in a development build', () => { + expect(appsUrl({}, false)).toBe(DEFAULT_APPS_URL) + expect(appsUrl({ COMFY_APPS_URL: 'http://127.0.0.1:5310/' }, false)).toBe( + 'http://127.0.0.1:5310/' + ) + }) + + it('ignores the override in a packaged build', () => { + expect(appsUrl({ COMFY_APPS_URL: 'https://evil.example/' }, true)).toBe(DEFAULT_APPS_URL) + }) +}) + +describe('isAppOpen', () => { + const list = 'https://apps.example/apps/' + + it('is open on an app page or an app route of the list page', () => { + expect(isAppOpen('https://apps.example/apps/text-to-image/desktop.html', list)).toBe(true) + expect(isAppOpen('https://apps.example/apps/#/make-image', list)).toBe(true) + }) + + it('is closed on the list itself and on other origins', () => { + expect(isAppOpen('https://apps.example/apps/', list)).toBe(false) + expect(isAppOpen('https://apps.example/apps/#/', list)).toBe(false) + expect(isAppOpen('https://other.example/apps/x/desktop.html', list)).toBe(false) + expect(isAppOpen('not a url', list)).toBe(false) + }) +}) + +describe('appsOrigin', () => { + it('accepts https and loopback http only', () => { + expect(appsOrigin('https://apps.comfy.org/x')).toBe('https://apps.comfy.org') + expect(appsOrigin('http://127.0.0.1:5310/')).toBe('http://127.0.0.1:5310') + expect(appsOrigin('http://apps.comfy.org/')).toBeNull() + expect(appsOrigin('file:///tmp/a.html')).toBeNull() + expect(appsOrigin('not a url')).toBeNull() + }) +}) + +describe('isAllowedAppsPermission', () => { + const origin = 'https://apps.comfy.org' + + it.each(['clipboard-sanitized-write', 'fullscreen'])('lets an app page use %s', (permission) => { + expect(isAllowedAppsPermission(permission, 'https://apps.comfy.org/app/x', origin)).toBe(true) + expect(isAllowedAppsPermission(permission, origin, origin)).toBe(true) + }) + + it.each(['media', 'geolocation', 'notifications', 'clipboard-read', 'openExternal'])( + 'denies %s', + (permission) => { + expect(isAllowedAppsPermission(permission, origin, origin)).toBe(false) + } + ) + + it('denies a frame from another origin inside the app page', () => { + expect(isAllowedAppsPermission('fullscreen', 'https://ads.example.com/frame', origin)).toBe( + false + ) + expect(isAllowedAppsPermission('fullscreen', '', origin)).toBe(false) + expect(isAllowedAppsPermission('fullscreen', origin, null)).toBe(false) + }) +}) + +describe('isAppsFrameUrl', () => { + it('matches the exact origin', () => { + const origin = 'https://apps.comfy.org' + expect(isAppsFrameUrl('https://apps.comfy.org/upscale', origin)).toBe(true) + expect(isAppsFrameUrl('https://apps.comfy.org.evil.com/', origin)).toBe(false) + expect(isAppsFrameUrl('https://evil.com/?https://apps.comfy.org', origin)).toBe(false) + expect(isAppsFrameUrl('https://apps.comfy.org/', null)).toBe(false) + }) +}) + +describe('isWorkflow', () => { + it('takes an API-format graph and nothing else', () => { + expect(isWorkflow({ '1': { class_type: 'SaveImage', inputs: {} } })).toBe(true) + expect(isWorkflow({ '1': { inputs: {} } })).toBe(false) + expect(isWorkflow([])).toBe(false) + expect(isWorkflow(null)).toBe(false) + expect(isWorkflow({ '1': { class_type: 'X', inputs: { t: 'a'.repeat(1_100_000) } } })).toBe( + false + ) + }) +}) + +describe('isRunId', () => { + it('takes a uuid and rejects path tricks', () => { + expect(isRunId('3f2a7c4e-1b2d-4e5f-9a8b-7c6d5e4f3a2b')).toBe(true) + expect(isRunId('../billing')).toBe(false) + expect(isRunId('')).toBe(false) + }) +}) + +describe('toOutputFile', () => { + it('keeps output and temp files, refuses climbing out', () => { + expect(toOutputFile({ filename: 'a.png', subfolder: '', type: 'output' })).toEqual({ + filename: 'a.png', + subfolder: '', + type: 'output' + }) + expect(toOutputFile({ filename: 'a.png', subfolder: '../x', type: 'output' })).toBeNull() + expect(toOutputFile({ filename: 'a.png', type: 'input' })).toBeNull() + }) + it('refuses a file name that is a path', () => { + expect(toOutputFile({ filename: '../../secret.png', type: 'output' })).toBeNull() + expect(toOutputFile({ filename: 'a\\b.png', type: 'output' })).toBeNull() + expect(toOutputFile({ filename: '..', type: 'output' })).toBeNull() + }) +}) + +describe('safeUploadName', () => { + it('keeps a plain image name', () => { + expect(safeUploadName('/Users/me/cat photo.jpg')).toBe('cat_photo.jpg') + expect(safeUploadName('script.sh')).toBe('upload.png') + expect(safeUploadName(undefined)).toBe('upload.png') + }) + it('keeps a video name', () => { + expect(safeUploadName('shot.mp4')).toBe('shot.mp4') + expect(safeUploadName('shot.webm')).toBe('shot.webm') + }) + it('keeps a song name', () => { + expect(safeUploadName('My Song.mp3')).toBe('My_Song.mp3') + expect(safeUploadName('take.flac')).toBe('take.flac') + expect(safeUploadName('voice.m4a')).toBe('voice.m4a') + }) + it('keeps the extension of a long name', () => { + const name = safeUploadName(`${'a'.repeat(150)}.webm`) + expect(name).toBe(`${'a'.repeat(95)}.webm`) + }) +}) + +describe('collectOutputs', () => { + const outputs = { + save: { + animated: [true], + images: [ + { filename: 'a.mp4', subfolder: 'apps', type: 'output', metadata: { kind: 'video' } } + ] + }, + glb: { '3d': [{ filename: 'b.glb', subfolder: 'apps', type: 'output' }], camera_info: [] }, + lens: { files: [{ filename: 'c.txt', subfolder: 'apps', type: 'output' }], text: ['43.6'] }, + show: { text: ['{"keys": []}'] }, + load: { images: [{ filename: 'in.mp4', subfolder: '', type: 'input' }] } + } + it('lists every saved file with its node and kind, and the text by node', () => { + const { files, text } = collectOutputs(outputs) + expect(files.map((f) => [f.node, f.filename, f.kind])).toEqual([ + ['save', 'a.mp4', 'video'], + ['glb', 'b.glb', '3d'], + ['lens', 'c.txt', 'text'] + ]) + expect(text).toEqual({ lens: ['43.6'], show: ['{"keys": []}'] }) + }) + it('takes junk without throwing', () => { + expect(collectOutputs(null)).toEqual({ files: [], text: {} }) + expect(collectOutputs({ a: 1, b: { images: 'x' } })).toEqual({ files: [], text: {} }) + }) + it('names kinds by extension', () => { + expect(outputKind('x.PNG')).toBe('image') + expect(outputKind('x.bin')).toBe('file') + expect(outputKind('hdr.avif')).toBe('image') + expect(outputKind('master.exr')).toBe('file') + expect(outputKind('voice.mp3')).toBe('audio') + }) +}) + +describe('saving results', () => { + it('takes letters, digits, dashes and spaces only', () => { + expect(cleanSaveName('motion-to-video')).toBe('motion-to-video') + expect(cleanSaveName(' camera v3 ')).toBe('camera v3') + expect(cleanSaveName('../etc')).toBeNull() + expect(cleanSaveName('a/b')).toBeNull() + expect(cleanSaveName('')).toBeNull() + expect(cleanSaveName(3)).toBeNull() + }) + it('allows known extensions only', () => { + expect(saveExt('.MP4')).toBe('mp4') + expect(saveExt('glb')).toBe('glb') + expect(saveExt('sh')).toBeNull() + expect(saveExt('mp3')).toBe('mp3') + expect(saveExt('avif')).toBe('avif') + expect(saveExt('EXR')).toBe('exr') + }) + it('names a file by date, time and label', () => { + expect(savedFileName('film', 'mp4', new Date(2026, 10, 4, 11, 41))).toBe( + '2026-11-04 11-41 film.mp4' + ) + }) + it('knows what is inside the apps folder', () => { + expect(isInside('/o/apps/m/a.mp4', '/o/apps')).toBe(true) + expect(isInside('/o/apps-evil/a.mp4', '/o/apps')).toBe(false) + expect(isInside('/o/apps', '/o/apps')).toBe(false) + }) +}) + +describe('readable', () => { + it("keeps the provider's message out of a partner node's JSON", () => { + const raw = + 'Polling aborted due to error: Task failed: {"id": "cgt-1", "status": "failed", "error": {"code": "OutputVideoSensitiveContentDetected.PolicyViolation", "message": "The request failed because the output video may be related to copyright restrictions."}}\n' + expect(readable(raw)).toBe( + 'The request failed because the output video may be related to copyright restrictions.' + ) + }) + it('keeps a plain message as it is', () => { + expect(readable(' Out of memory. ')).toBe('Out of memory.') + expect(readable(undefined)).toBeUndefined() + }) +}) + +describe('isVersionsIndex', () => { + const bytes = (s: string) => new TextEncoder().encode(s) + it('takes a JSON object', () => { + expect(isVersionsIndex(bytes('{"steps":{}}'))).toBe(true) + }) + it('refuses anything else', () => { + expect(isVersionsIndex(bytes('[1]'))).toBe(false) + expect(isVersionsIndex(bytes('not json'))).toBe(false) + expect(isVersionsIndex('{}')).toBe(false) + expect(isVersionsIndex(new Uint8Array(2_000_001))).toBe(false) + }) +}) + +describe('localRequest', () => { + const graph = { '1': { class_type: 'SaveImage', inputs: {} } } + + it('passes the GET paths apps use, rebuilt by main', () => { + expect(localRequest({ method: 'GET', path: '/system_stats' })).toEqual({ + kind: 'get', + path: '/system_stats' + }) + expect(localRequest({ method: 'GET', path: '/object_info/KSampler_Advanced2' })).toEqual({ + kind: 'get', + path: '/object_info/KSampler_Advanced2' + }) + expect(localRequest({ method: 'GET', path: '/history/9f1c-ab12' })).toEqual({ + kind: 'get', + path: '/history/9f1c-ab12' + }) + expect( + localRequest({ method: 'GET', path: '/view?filename=a%20b.png&subfolder=apps&type=output' }) + ).toEqual({ kind: 'get', path: '/view?filename=a+b.png&subfolder=apps&type=output' }) + }) + + it('refuses other paths, methods and hosts with a 4xx', () => { + const refused = [ + { method: 'GET', path: '/prompt' }, + { method: 'GET', path: '/queue?x=1' }, + { method: 'GET', path: '/object_info' }, + { method: 'GET', path: '/userdata/workflows%2Fa.json' }, + { method: 'GET', path: '/system_stats/' }, + { method: 'GET', path: '/system_stats?x=1' }, + { method: 'GET', path: '/object_info/Bad%20Name' }, + { method: 'GET', path: '/object_info/Bad.Name' }, + { method: 'GET', path: '/history/' + 'a'.repeat(65) }, + { method: 'GET', path: '/history/a_b' }, + { method: 'POST', path: '/interrupt' }, + { method: 'POST', path: '/interrupt', body: {} }, + { method: 'POST', path: '/queue', body: { clear: true } }, + { method: 'POST', path: '/free' }, + { method: 'DELETE', path: '/history/abc' }, + { method: 'GET', path: '//evil.example/system_stats' }, + { method: 'GET', path: 'system_stats' }, + { method: 'GET', path: 'http://127.0.0.1:8188/system_stats' }, + { method: 'GET' }, + null, + 'GET /system_stats' + ] + for (const req of refused) { + const out = localRequest(req) + expect(out, JSON.stringify(req)).not.toHaveProperty('kind') + const status = (out as { status: number }).status + expect(status).toBeGreaterThanOrEqual(400) + expect(status).toBeLessThan(500) + } + expect(localRequest({ method: 'PUT', path: '/prompt' })).toMatchObject({ status: 405 }) + }) + + it('takes a /prompt body as an object or JSON text with a graph in prompt', () => { + const body = { prompt: graph, client_id: 'c1' } + const asObject = localRequest({ method: 'POST', path: '/prompt', body }) + expect(asObject).toEqual({ kind: 'prompt', path: '/prompt', json: JSON.stringify(body) }) + const asText = localRequest({ method: 'POST', path: '/prompt', body: JSON.stringify(body) }) + expect(asText).toEqual(asObject) + }) + + it('refuses a /prompt body without a graph, or over 2 MB', () => { + for (const body of [undefined, 'not json', [], { prompt: 'x' }, { prompt: { '1': {} } }]) { + expect(localRequest({ method: 'POST', path: '/prompt', body })).toMatchObject({ status: 400 }) + } + const big = { prompt: graph, extra_data: { pad: 'x'.repeat(2_000_001) } } + expect(localRequest({ method: 'POST', path: '/prompt', body: big })).toMatchObject({ + status: 400 + }) + expect(localRequest({ method: 'POST', path: '/prompt?x=1', body: { prompt: graph } })).toEqual({ + status: 404, + error: expect.any(String) + }) + }) + + it('takes an upload as bytes and a cleaned file name, up to 50 MB', () => { + const bytes = new Uint8Array([1, 2, 3]) + expect( + localRequest({ method: 'POST', path: '/upload/image', body: bytes, fileName: '../x/cat.png' }) + ).toEqual({ kind: 'upload', path: '/upload/image', bytes, fileName: 'cat.png' }) + expect( + localRequest({ method: 'POST', path: '/upload/image', body: 'abc', fileName: 'a.png' }) + ).toMatchObject({ status: 400 }) + expect( + localRequest({ + method: 'POST', + path: '/upload/image', + body: new Uint8Array(50_000_001), + fileName: 'a.png' + }) + ).toMatchObject({ status: 413 }) + }) +}) + +describe('viewQuery', () => { + const q = (s: string) => viewQuery(new URLSearchParams(s)) + + it('keeps filename, subfolder and type, in that order', () => { + expect(q('type=temp&filename=a.png')).toBe('filename=a.png&subfolder=&type=temp') + expect(q('filename=a.png&subfolder=x%2Fy&type=input')).toBe( + 'filename=a.png&subfolder=x%2Fy&type=input' + ) + }) + + it('refuses other keys, repeated keys, other types and paths out of the folder', () => { + expect(q('filename=a.png&type=output&preview=webp')).toBeNull() + expect(q('filename=a.png&type=output&channel=rgba')).toBeNull() + expect(q('filename=a.png&filename=b.png&type=output')).toBeNull() + expect(q('filename=a.png&type=models')).toBeNull() + expect(q('filename=a.png')).toBeNull() + expect(q('type=output')).toBeNull() + expect(q('filename=..&type=output')).toBeNull() + expect(q('filename=x%2Fa.png&type=output')).toBeNull() + expect(q('filename=x%5Ca.png&type=output')).toBeNull() + expect(q('filename=a.png&subfolder=..%2Fsecret&type=output')).toBeNull() + expect(q('filename=a.png&subfolder=%2Fetc&type=output')).toBeNull() + expect(q('filename=a.png&subfolder=C%3A%5Cx&type=output')).toBeNull() + }) +}) + +describe('uploadForm', () => { + it('puts the file under image, with overwrite on', async () => { + const form = uploadForm(new Uint8Array([7, 8]), 'my cat.png') + const file = form.get('image') as File + expect(file.name).toBe('my_cat.png') + expect(new Uint8Array(await file.arrayBuffer())).toEqual(new Uint8Array([7, 8])) + expect(form.get('overwrite')).toBe('true') + expect(form.getAll('image')).toHaveLength(1) + expect(form.getAll('overwrite')).toEqual(['true']) + }) +}) + +describe('readCapped', () => { + it('reads an answer up to the cap and drops one over it', async () => { + expect(await readCapped(new Response(new Uint8Array([1, 2, 3])), 3)).toEqual( + new Uint8Array([1, 2, 3]) + ) + expect(await readCapped(new Response(new Uint8Array(4)), 3)).toBeNull() + const declared = new Response('x', { headers: { 'content-length': '10' } }) + expect(await readCapped(declared, 3)).toBeNull() + }) + + it('calls each chunk it reads', async () => { + const onChunk = vi.fn() + await readCapped(new Response(new Uint8Array([1, 2, 3])), 3, onChunk) + expect(onChunk).toHaveBeenCalled() + }) +}) + +describe('jobs answers after a submit', () => { + const runId = '11111111-2222-3333-4444-555555555555' + const pollAt = (submitted: Map, status: number, now: number) => + jobsAnswer(status, inJobGrace(submitted, runId, now)) + + it('keeps polling a 404 inside the grace period and reads the 200 that follows', () => { + const submitted = new Map() + noteSubmitted(submitted, runId, 1000) + expect(pollAt(submitted, 404, 1000 + 2_000)).toBe('wait') + expect(pollAt(submitted, 200, 1000 + 4_000)).toBe('read') + }) + + it('ends the run on a 404 after the grace period', () => { + const submitted = new Map() + noteSubmitted(submitted, runId, 1000) + const answer = pollAt(submitted, 404, 1000 + JOB_GRACE_MS) + expect(answer).toEqual({ error: expect.stringMatching(/cannot find this run/) }) + expect(submitted.has(runId)).toBe(false) + }) + + it('ends the run on a 404 for a run this app did not submit', () => { + expect(pollAt(new Map(), 404, 1000)).toEqual({ error: expect.any(String) }) + }) + + it('names sign-in for 401 and access for 403, even inside the grace period', () => { + expect(jobsAnswer(401, true)).toEqual({ error: expect.stringMatching(/Sign in again/) }) + expect(jobsAnswer(403, true)).toEqual({ error: expect.stringMatching(/access/) }) + }) + + it('refuses other 4xx answers and waits on busy and server errors', () => { + expect(jobsAnswer(400, true)).toBe('refused') + expect(jobsAnswer(410, false)).toBe('refused') + expect(jobsAnswer(429, false)).toBe('wait') + expect(jobsAnswer(503, false)).toBe('wait') + }) + + it('forgets old submits when a new run starts', () => { + const submitted = new Map([['old', 0]]) + noteSubmitted(submitted, runId, JOB_GRACE_MS + 1) + expect([...submitted.keys()]).toEqual([runId]) + }) +}) + +describe('textAnswer', () => { + it('is shaped like a ComfyUI answer', () => { + const a = textAnswer(404, 'no') + expect(a.status).toBe(404) + expect(a.contentType).toMatch(/^text\/plain/) + expect(new TextDecoder().decode(a.body)).toBe('no') + }) +}) + +describe('localEvent', () => { + it('passes the run messages and the queue status', () => { + const frame = JSON.stringify({ type: 'progress', data: { value: 3, max: 20, prompt_id: 'p' } }) + expect(localEvent(frame)).toEqual({ + type: 'progress', + data: { value: 3, max: 20, prompt_id: 'p' } + }) + expect(localEvent(JSON.stringify({ type: 'status', data: { sid: 'c1' } }))).toMatchObject({ + type: 'status' + }) + }) + + it('drops binary previews, other types and text that is not JSON', () => { + expect(localEvent(new ArrayBuffer(8))).toBeNull() + expect(localEvent(JSON.stringify({ type: 'crystools.monitor', data: {} }))).toBeNull() + expect(localEvent(JSON.stringify({ data: {} }))).toBeNull() + expect(localEvent('not json')).toBeNull() + expect(localEvent('null')).toBeNull() + }) +}) + +describe('reconnectDelay', () => { + it('doubles from 1 s and stops at 30 s', () => { + expect([0, 1, 2, 3, 4, 5, 9].map(reconnectDelay)).toEqual([ + 1000, 2000, 4000, 8000, 16000, 30000, 30000 + ]) + }) +}) + +describe('localInstalls', () => { + const category = (inst: { sourceId: string }) => + ({ standalone: 'local', git: 'local', desktop: 'local', cloud: 'cloud', remote: 'remote' })[ + inst.sourceId + ] + + it('lists local installs, the one launched last first', () => { + const list = [ + { id: 'a', name: 'A', sourceId: 'standalone', status: 'installed', lastLaunchedAt: 1 }, + { id: 'c', name: 'Cloud', sourceId: 'cloud', lastLaunchedAt: 9 }, + { + id: 'b', + name: 'B', + sourceId: 'git', + lastLaunchedAt: 9, + lastLaunchedAtByCategory: { local: 5 } + }, + { id: 'n', name: 'Never', sourceId: 'standalone', status: 'installed' } + ] + expect(localInstalls(list, category).map((i) => i.id)).toEqual(['b', 'a', 'n']) + }) + + it('leaves out legacy Desktop, unfinished and failed installs', () => { + const list = [ + { id: 'd', name: 'Legacy', sourceId: 'desktop', status: 'installed', lastLaunchedAt: 9 }, + { id: 'i', name: 'Installing', sourceId: 'standalone', status: 'installing' }, + { id: 'f', name: 'Failed', sourceId: 'standalone', status: 'failed' }, + { id: 'r', name: 'Remote', sourceId: 'remote' } + ] + expect(localInstalls(list, category)).toEqual([]) + }) + + it('leaves out the Apps build, even when it was launched last', () => { + const list = [ + { id: 'mine', name: 'Mine', sourceId: 'standalone', status: 'installed', lastLaunchedAt: 1 }, + { + id: 'apps', + name: 'Apps', + sourceId: 'standalone', + status: 'installed', + appsBuild: true, + lastLaunchedAt: 9, + lastLaunchedAtByCategory: { local: 9 } + } + ] + expect(localInstalls(list, category).map((i) => i.id)).toEqual(['mine']) + }) +}) + +describe('downloadRequest', () => { + const url = 'https://huggingface.co/org/repo/resolve/main/model.safetensors' + const ok = { url, folder: 'checkpoints', name: 'model.safetensors' } + + it('passes a Hugging Face model into a known folder', () => { + expect(downloadRequest(ok)).toEqual(ok) + expect(downloadRequest({ ...ok, folder: 'upscale_models', name: '4x-Ultra_v2.pth' })).toEqual({ + ...ok, + folder: 'upscale_models', + name: '4x-Ultra_v2.pth' + }) + for (const folder of [ + 'controlnet', + 'loras', + 'vae', + 'clip_vision', + 'text_encoders', + 'model_patches', + 'background_removal', + 'frame_interpolation', + 'geometry_estimation', + 'audio_encoders' + ]) { + expect(downloadRequest({ ...ok, folder })).not.toHaveProperty('error') + } + expect(downloadRequest({ ...ok, folder: 'diffusion_models' })).not.toHaveProperty('error') + }) + + it('refuses other hosts, plain http, ports and credentials', () => { + for (const bad of [ + 'http://huggingface.co/org/repo/resolve/main/model.safetensors', + 'https://huggingface.co.evil.example/x.safetensors', + 'https://evil.example/huggingface.co/x.safetensors', + 'https://cdn-lfs.huggingface.co/x.safetensors', + 'https://huggingface.co:8443/org/repo/x.safetensors', + 'https://user:pass@huggingface.co/org/repo/x.safetensors', + 'https://token@huggingface.co/org/repo/x.safetensors', + 'https://huggingface.co/', + 'file:///etc/passwd', + 'not a url', + 42 + ]) { + expect(downloadRequest({ ...ok, url: bad }), String(bad)).toHaveProperty('error') + } + }) + + it('refuses other folders', () => { + for (const folder of ['custom_nodes', '../checkpoints', 'checkpoints/sub', 'clip', '', 1]) { + expect(downloadRequest({ ...ok, folder }), String(folder)).toHaveProperty('error') + } + }) + + it('refuses names that are not plain model files', () => { + for (const name of [ + 'model.ckpt', + 'model.pt', + 'pytorch_model.bin', + 'model.safetensors.exe', + 'model', + '../model.safetensors', + 'sub/model.safetensors', + 'my model.safetensors', + '.safetensors', + '..safetensors', + 'a'.repeat(129) + '.safetensors', + undefined + ]) { + expect(downloadRequest({ ...ok, name }), String(name)).toHaveProperty('error') + } + }) + + it('refuses a missing request', () => { + expect(downloadRequest(null)).toHaveProperty('error') + expect(downloadRequest('x')).toHaveProperty('error') + }) +}) + +describe('stopping local runs', () => { + it('passes /interrupt with one run id, as an object or JSON text', () => { + expect( + localRequest({ method: 'POST', path: '/interrupt', body: { prompt_id: 'ab-12' } }) + ).toEqual({ kind: 'json', path: '/interrupt', json: '{"prompt_id":"ab-12"}' }) + expect(interruptJson('{"prompt_id":"ab-12"}')).toBe('{"prompt_id":"ab-12"}') + }) + + it('refuses an /interrupt that would stop whatever runs, or carries more', () => { + for (const body of [ + undefined, + {}, + { prompt_id: '' }, + { prompt_id: 'a b' }, + { prompt_id: 'a'.repeat(65) }, + { prompt_id: 7 }, + { prompt_id: 'ab', extra: 1 }, + [], + 'not json', + JSON.stringify({ prompt_id: 'ab', pad: 'x'.repeat(10_001) }) + ]) { + expect(interruptJson(body), JSON.stringify(body)).toBeNull() + } + expect( + localRequest({ method: 'POST', path: '/interrupt?x=1', body: { prompt_id: 'a' } }) + ).toMatchObject({ status: 404 }) + }) + + it('passes /queue deleting up to 50 run ids', () => { + expect( + localRequest({ method: 'POST', path: '/queue', body: { delete: ['a1', 'b-2'] } }) + ).toEqual({ kind: 'json', path: '/queue', json: '{"delete":["a1","b-2"]}' }) + const fifty = Array.from({ length: 50 }, (_, i) => `id-${i}`) + expect(queueJson({ delete: fifty })).toBe(JSON.stringify({ delete: fifty })) + }) + + it('refuses a /queue clear, other keys, bad ids and more than 50', () => { + for (const body of [ + { clear: true }, + { delete: ['a'], clear: true }, + { delete: [] }, + { delete: 'a' }, + { delete: ['a', 'b c'] }, + { delete: ['a', 3] }, + { delete: Array.from({ length: 51 }, (_, i) => `id-${i}`) }, + {} + ]) { + expect(queueJson(body), JSON.stringify(body)).toBeNull() + } + }) + + it('leaves /free to Desktop: a page cannot unload models', () => { + for (const body of [undefined, {}, { unload_models: true, free_memory: true }]) { + expect(localRequest({ method: 'POST', path: '/free', body })).toMatchObject({ status: 404 }) + } + expect(localRequest({ method: 'GET', path: '/free' })).toMatchObject({ status: 404 }) + }) +}) + +describe('apps page load failures', () => { + it('counts a failed main-frame load, but not one replaced by another navigation', () => { + expect(isPageLoadFailure(-105)).toBe(true) + expect(isPageLoadFailure(-106)).toBe(true) + expect(isPageLoadFailure(-3)).toBe(false) + expect(isPageLoadFailure(0)).toBe(false) + }) + + it('counts a main document that is not 2xx', () => { + expect([200, 204, 299].map(isDocumentFailure)).toEqual([false, false, false]) + expect([404, 500, 503, 199, 304].map(isDocumentFailure)).toEqual([true, true, true, true, true]) + expect(isDocumentFailure(-1)).toBe(false) + expect(isDocumentFailure(0)).toBe(false) + }) +}) + +describe('opening a workflow in ComfyUI', () => { + const prompt = { + pos: { class_type: 'CLIPTextEncode', inputs: { text: 'a fox', clip: ['clip', 0] } }, + save: { class_type: 'SaveImage', inputs: { images: ['decode', 0] } } + } + + it('takes a graph, where it ran and a readable name', () => { + expect(openRequest({ prompt, where: 'local', name: 'Text to Image - 2026-11-09' })).toEqual({ + prompt, + where: 'local', + name: 'Text to Image - 2026-11-09' + }) + expect(openRequest({ prompt, where: 'cloud', name: ' Same Face ' })).toMatchObject({ + where: 'cloud', + name: 'Same Face' + }) + }) + + it('names it Comfy app when the name is odd', () => { + for (const name of [undefined, '', 'a/b', '../x', '\u2028' } + } + } + const req = openRequest({ prompt: sneaky, where: 'local', name: 'Text to Image' }) + if ('error' in req) throw new Error(req.error) + const calls: unknown[][] = [] + const app = { + graph: {}, + canvas: {}, + loadApiJson: (...args: unknown[]) => { + calls.push(args) + } + } + const ctx = page(app) as Record + expect(await runInNewContext(openWorkflowScript(req), ctx)).toBe(true) + expect(calls).toEqual([[sneaky, 'Text to Image']]) + expect(ctx['hacked']).toBeUndefined() + }) + + it('says it did not open when ComfyUI refuses the graph', async () => { + const req = openRequest({ prompt, where: 'local', name: 'x' }) + if ('error' in req) throw new Error(req.error) + const app = { graph: {}, canvas: {}, loadApiJson: () => false } + expect(await runInNewContext(openWorkflowScript(req), page(app))).toBe(false) + }) + + it('loads only into the ComfyUI page itself', () => { + expect(isComfyPage('http://127.0.0.1:8188/?x=1', 'http://127.0.0.1:8188')).toBe(true) + expect(isComfyPage('https://cloud.comfy.org/', 'https://cloud.comfy.org/?utm_source=a')).toBe( + true + ) + expect(isComfyPage('data:text/html,splash', 'http://127.0.0.1:8188')).toBe(false) + expect(isComfyPage('http://127.0.0.1:8189/', 'http://127.0.0.1:8188')).toBe(false) + expect(isComfyPage('https://cloud.comfy.org.evil.com/', 'https://cloud.comfy.org/')).toBe(false) + expect(isComfyPage('not a url', 'http://127.0.0.1:8188')).toBe(false) + }) +}) + +describe('reading the local queue', () => { + const graph = { '1': { class_type: 'SaveImage', inputs: { filename_prefix: 'secret' } } } + const item = (n: number, id: unknown, extra: unknown) => [ + n, + id, + graph, + extra, + ['1'], + { auth_token_comfy_org: 'k' } + ] + + it('passes GET /queue with no query', () => { + expect(localRequest({ method: 'GET', path: '/queue' })).toEqual({ + kind: 'queue', + path: '/queue' + }) + }) + + it('keeps only run ids, their order and the app tag, never graphs or extra data', () => { + const raw = { + queue_running: [ + item(3, 'run-a', { + client_id: 'c', + comfy_app: { name: 'Rotoscope', seconds: 420.4 }, + extra_pnginfo: { workflow: graph } + }) + ], + queue_pending: [ + item(9, 'run-c', { comfy_app: { name: 'Doodle to image', seconds: 30 } }), + item(5, 'run-b', { client_id: 'other' }) + ] + } + const view = queueView(raw) + expect(view).toEqual({ + running: [{ id: 'run-a', app: 'Rotoscope', seconds: 420 }], + pending: [ + { id: 'run-b', app: null, seconds: null }, + { id: 'run-c', app: 'Doodle to image', seconds: 30 } + ] + }) + const text = JSON.stringify(view) + for (const leak of [ + 'secret', + 'SaveImage', + 'client_id', + 'extra_pnginfo', + 'auth_token', + 'other' + ]) { + expect(text, leak).not.toContain(leak) + } + }) + + it('drops app tags that are not a short plain name or a sane number of seconds', () => { + for (const comfy_app of [ + { name: 'x', seconds: -1 }, + { name: 'a'.repeat(41), seconds: 86_401 }, + { name: 7, seconds: '60' }, + 'Rotoscope', + null + ]) { + const view = queueView({ queue_running: [item(1, 'r', { comfy_app })] }) + expect(view.running[0], JSON.stringify(comfy_app)).toEqual({ + id: 'r', + app: null, + seconds: null + }) + } + }) + + it('counts runs with odd ids without naming them, and reads a broken queue as empty', () => { + const view = queueView({ queue_running: [], queue_pending: [item(1, 'a b', {}), 'x'] }) + expect(view.pending).toEqual([{ id: '', app: null, seconds: null }]) + expect(queueView(null)).toEqual({ running: [], pending: [] }) + expect(queueView({ queue_running: 'x' })).toEqual({ running: [], pending: [] }) + }) + + it('caps each list at 100 runs', () => { + const many = Array.from({ length: 150 }, (_, i) => item(i, `r${i}`, {})) + expect(queueView({ queue_pending: many }).pending).toHaveLength(100) + }) + + it('adds when a run started and its progress, from the socket', () => { + const seen = new Map() + noteRun(seen, { type: 'execution_start', data: { prompt_id: 'run-a' } }, 1000) + noteRun(seen, { type: 'progress', data: { prompt_id: 'run-a', value: 12, max: 30 } }, 2000) + const view = queueView({ queue_running: [item(1, 'run-a', {})] }, seen) + expect(view.running[0]).toEqual({ + id: 'run-a', + app: null, + seconds: null, + startedAt: 1000, + done: 12, + of: 30 + }) + }) + + it('forgets a run when it ends, ignores progress for runs it did not see start, and keeps 50', () => { + const seen = new Map() + noteRun(seen, { type: 'progress', data: { prompt_id: 'x', value: 1, max: 2 } }, 1) + expect(seen.size).toBe(0) + noteRun(seen, { type: 'execution_start', data: { prompt_id: 'x' } }, 1) + noteRun(seen, { type: 'progress', data: { prompt_id: 'x', value: 'a', max: 0 } }, 2) + expect(seen.get('x')).toEqual({ startedAt: 1 }) + noteRun(seen, { type: 'execution_success', data: { prompt_id: 'x' } }, 3) + expect(seen.size).toBe(0) + for (let i = 0; i < 60; i++) + noteRun(seen, { type: 'execution_start', data: { prompt_id: `r${i}` } }, i) + expect(seen.size).toBe(50) + expect(seen.has('r0')).toBe(false) + expect(seen.has('r59')).toBe(true) + noteRun(seen, { type: 'execution_start', data: { prompt_id: 'a b' } }, 1) + expect(seen.has('a b')).toBe(false) + }) + + it('answers the cut-down queue as JSON, and refuses what ComfyUI did not answer well', () => { + const bytes = new TextEncoder().encode( + JSON.stringify({ queue_running: [item(1, 'r', {})], queue_pending: [] }) + ) + const ok = queueAnswer(200, bytes, new Map()) + expect(ok.status).toBe(200) + expect(ok.contentType).toBe('application/json') + expect(JSON.parse(new TextDecoder().decode(ok.body))).toEqual({ + running: [{ id: 'r', app: null, seconds: null }], + pending: [] + }) + expect(queueAnswer(500, bytes, new Map()).status).toBe(500) + expect(queueAnswer(500, bytes, new Map()).body).not.toEqual(bytes) + expect(queueAnswer(200, new TextEncoder().encode('not json'), new Map()).status).toBe(502) + }) +}) diff --git a/src/main/apps/guards.ts b/src/main/apps/guards.ts new file mode 100644 index 000000000..4ce59c0fd --- /dev/null +++ b/src/main/apps/guards.ts @@ -0,0 +1,715 @@ +export const DEFAULT_APPS_URL = 'https://apps.comfy.org/' + +export function appsUrl(env: NodeJS.ProcessEnv, packaged: boolean): string { + return (!packaged && env['COMFY_APPS_URL']) || DEFAULT_APPS_URL +} + +export function appsOrigin(url: string): string | null { + let parsed: URL + try { + parsed = new URL(url) + } catch { + return null + } + if (parsed.protocol === 'https:') return parsed.origin + const loopback = parsed.hostname === '127.0.0.1' || parsed.hostname === 'localhost' + return parsed.protocol === 'http:' && loopback ? parsed.origin : null +} + +const ALLOWED_APPS_PERMISSIONS: ReadonlySet = new Set([ + 'clipboard-sanitized-write', + 'fullscreen' +]) + +export function isAllowedAppsPermission( + permission: string, + requestingUrl: string, + origin: string | null +): boolean { + return ALLOWED_APPS_PERMISSIONS.has(permission) && isAppsFrameUrl(requestingUrl, origin) +} + +export function isAppsFrameUrl(frameUrl: string, origin: string | null): boolean { + if (!origin) return false + try { + return new URL(frameUrl).origin === origin + } catch { + return false + } +} + +export function isAppOpen(pageUrl: string, listUrl: string): boolean { + let page: URL + let list: URL + try { + page = new URL(pageUrl) + list = new URL(listUrl) + } catch { + return false + } + if (page.origin !== list.origin) return false + if (page.pathname !== list.pathname) return true + return page.hash.startsWith('#/') && page.hash.length > 2 +} + +const MAX_WORKFLOW_BYTES = 1_000_000 +export const MAX_UPLOAD_BYTES = 50_000_000 +export const MAX_RESULT_BYTES = 100_000_000 +export const MAX_SAVE_BYTES = 2_000_000_000 + +export function isWorkflow(value: unknown): value is Record { + if (!value || typeof value !== 'object' || Array.isArray(value)) return false + if (JSON.stringify(value).length > MAX_WORKFLOW_BYTES) return false + return Object.values(value).every( + (node) => + !!node && + typeof node === 'object' && + typeof (node as { class_type?: unknown }).class_type === 'string' && + typeof (node as { inputs?: unknown }).inputs === 'object' + ) +} + +const MAX_OPEN_NODES = 500 +const CREDENTIAL_INPUTS = /^(api_key_comfy_org|auth_token_comfy_org)$/i + +export interface OpenRequest { + prompt: Record + where: 'local' | 'cloud' + name: string + build?: string +} + +export function isBuildId(value: unknown): value is string { + return typeof value === 'string' && /^[A-Za-z0-9_-]{1,64}$/.test(value) +} + +export function workflowName(value: unknown): string { + const v = typeof value === 'string' ? value.trim().replace(/\s+/g, ' ') : '' + return /^[A-Za-z0-9 .,()-]{1,80}$/.test(v) ? v : 'Comfy app' +} + +function carriesCredentials(prompt: Record): boolean { + return Object.values(prompt).some((node) => + Object.keys((node as { inputs: object | null }).inputs ?? {}).some((k) => + CREDENTIAL_INPUTS.test(k) + ) + ) +} + +export function openRequest(input: unknown): OpenRequest | { error: string } { + if (!input || typeof input !== 'object' || Array.isArray(input)) { + return { error: 'That is not a workflow.' } + } + const { prompt, where, name, build } = input as Record + if (where !== 'local' && where !== 'cloud') { + return { error: 'Open it on Comfy Cloud or this computer.' } + } + if (build !== undefined && (where !== 'local' || !isBuildId(build))) { + return { error: 'Pick one of your builds on this computer.' } + } + if (!isWorkflow(prompt)) return { error: 'That is not a workflow.' } + const count = Object.keys(prompt).length + if (count < 1 || count > MAX_OPEN_NODES) return { error: 'That is not a workflow.' } + if (carriesCredentials(prompt)) return { error: 'Leave keys out of the workflow.' } + return { prompt, where, name: workflowName(name), ...(build !== undefined ? { build } : {}) } +} + +const FIND_COMFY_APP = + `var app=window.app||(window.comfyAPI&&window.comfyAPI.app&&window.comfyAPI.app.app);` + + `if(!app||!app.graph||!app.canvas||typeof app.loadApiJson!=='function')return false;` + + `var busy=document.querySelector('[data-testid="app-loading-overlay"]');` + + `if(busy&&busy.getAttribute('aria-busy')==='true')return false;` + +export const COMFY_READY_SCRIPT = `(function(){${FIND_COMFY_APP}return true})()` + +export function openWorkflowScript(req: OpenRequest): string { + const prompt = JSON.stringify(req.prompt) + const name = JSON.stringify(req.name) + return ( + `(function(prompt,name){` + + FIND_COMFY_APP + + `return Promise.resolve(app.loadApiJson(prompt,name)).then(function(r){return r!==false},function(){return false});` + + `})(${prompt},${name})` + ) +} + +export function isComfyPage(pageUrl: string, comfyUrl: string): boolean { + try { + const page = new URL(pageUrl) + const comfy = new URL(comfyUrl) + return /^https?:$/.test(page.protocol) && page.origin === comfy.origin + } catch { + return false + } +} + +export function isRunId(value: unknown): value is string { + return typeof value === 'string' && /^[A-Za-z0-9-]{1,64}$/.test(value) +} + +export interface OutputFile { + filename: string + subfolder: string + type: 'output' | 'temp' +} + +export function toOutputFile(value: unknown): OutputFile | null { + if (!value || typeof value !== 'object') return null + const v = value as Record + const filename = v['filename'] + if (typeof filename !== 'string' || !filename || filename.length > 255) return null + if (/[\\/\0]/.test(filename) || filename === '.' || filename === '..') return null + const subfolder = typeof v['subfolder'] === 'string' ? v['subfolder'] : '' + if (subfolder.length > 255 || subfolder.includes('..')) return null + const type = v['type'] === 'temp' ? 'temp' : v['type'] === 'output' ? 'output' : null + if (!type) return null + return { filename, subfolder, type } +} + +export function safeUploadName(name: unknown): string { + const base = typeof name === 'string' ? (name.split(/[\\/]/).pop() ?? '') : '' + const cleaned = base.replace(/[^A-Za-z0-9._-]/g, '_') + const ext = /\.(png|jpe?g|webp|mp4|webm|mov|mp3|wav|flac|ogg|m4a|opus)$/i.exec(cleaned)?.[0] + return ext ? cleaned.slice(0, Math.min(cleaned.length, 100) - ext.length) + ext : 'upload.png' +} + +export type OutputKind = 'image' | 'video' | 'audio' | '3d' | 'text' | 'file' + +export function outputKind(filename: string): OutputKind { + const ext = filename.split('.').pop()?.toLowerCase() ?? '' + if (['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif'].includes(ext)) return 'image' + if (['mp4', 'webm', 'mov'].includes(ext)) return 'video' + if (['mp3', 'wav', 'flac', 'ogg', 'm4a', 'opus'].includes(ext)) return 'audio' + if (['glb', 'gltf', 'obj', 'fbx', 'ply'].includes(ext)) return '3d' + if (['txt', 'json', 'md', 'csv'].includes(ext)) return 'text' + return 'file' +} + +export function collectOutputs(outputs: unknown): { + files: Array + text: Record +} { + const files: Array = [] + const text: Record = {} + if (!outputs || typeof outputs !== 'object') return { files, text } + for (const [node, out] of Object.entries(outputs as Record)) { + if (!out || typeof out !== 'object') continue + for (const [key, list] of Object.entries(out as Record)) { + if (!Array.isArray(list)) continue + if (key === 'text') { + const t = list.filter((x): x is string => typeof x === 'string') + if (t.length) text[node] = t + continue + } + for (const item of list) { + const f = toOutputFile(item) + if (f) files.push({ ...f, node, kind: outputKind(f.filename) }) + } + } + } + return { files, text } +} + +export function cleanSaveName(value: unknown): string | null { + if (typeof value !== 'string') return null + const v = value.trim().replace(/\s+/g, ' ') + return /^[A-Za-z0-9 -]{1,60}$/.test(v) ? v : null +} + +const SAVE_EXTS = [ + 'png', + 'jpg', + 'jpeg', + 'webp', + 'gif', + 'avif', + 'exr', + 'mp4', + 'webm', + 'mov', + 'mp3', + 'wav', + 'glb', + 'txt', + 'json' +] + +export function saveExt(value: unknown): string | null { + const ext = typeof value === 'string' ? value.toLowerCase().replace(/^\./, '') : '' + return SAVE_EXTS.includes(ext) ? ext : null +} + +export function savedFileName(label: string, ext: string, now: Date = new Date()): string { + const p = (n: number) => String(n).padStart(2, '0') + const stamp = `${now.getFullYear()}-${p(now.getMonth() + 1)}-${p(now.getDate())} ${p(now.getHours())}-${p(now.getMinutes())}` + return `${stamp} ${label}.${ext}` +} + +export function isInside(file: string, root: string, sep = '/'): boolean { + return file.startsWith(root.endsWith(sep) ? root : root + sep) +} + +export function readable(text: string | undefined): string | undefined { + if (!text) return undefined + const said = text.match(/"message":\s*"((?:[^"\\]|\\.)*)"/)?.[1] + return (said ?? text).trim() +} + +export const VERSIONS_FILE = 'versions.json' + +export function isVersionsIndex(bytes: unknown): bytes is Uint8Array { + if (!(bytes instanceof Uint8Array) || bytes.byteLength > 2_000_000) return false + try { + const v = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes)) as unknown + return !!v && typeof v === 'object' && !Array.isArray(v) + } catch { + return false + } +} + +export function uploadForm(bytes: Uint8Array, name: unknown): FormData { + const form = new FormData() + form.append('image', new Blob([new Uint8Array(bytes)]), safeUploadName(name)) + form.append('overwrite', 'true') + return form +} + +export interface LocalAnswer { + status: number + contentType: string + body: Uint8Array +} + +export function textAnswer(status: number, text: string): LocalAnswer { + return { status, contentType: 'text/plain; charset=utf-8', body: new TextEncoder().encode(text) } +} + +export type LocalRequest = + | { kind: 'get'; path: string } + | { kind: 'queue'; path: '/queue' } + | { kind: 'prompt'; path: '/prompt'; json: string } + | { kind: 'json'; path: '/interrupt' | '/queue'; json: string } + | { kind: 'upload'; path: '/upload/image'; bytes: Uint8Array; fileName: string } + +export interface LocalRefusal { + status: number + error: string +} + +const MAX_PROMPT_BODY_BYTES = 2_000_000 +const VIEW_TYPES = ['output', 'input', 'temp'] +const LOCAL_BASE = 'http://127.0.0.1' + +export function viewQuery(params: URLSearchParams): string | null { + const keys = [...params.keys()] + if (new Set(keys).size !== keys.length) return null + if (keys.some((k) => !['filename', 'subfolder', 'type'].includes(k))) return null + const filename = params.get('filename') ?? '' + const subfolder = params.get('subfolder') ?? '' + const type = params.get('type') ?? '' + if (!filename || filename.length > 255 || /[\\/\0]/.test(filename)) return null + if (filename === '.' || filename === '..') return null + if (subfolder.length > 255 || subfolder.includes('..') || subfolder.includes('\0')) return null + if (/^[\\/]/.test(subfolder) || /^[A-Za-z]:/.test(subfolder)) return null + if (!VIEW_TYPES.includes(type)) return null + return new URLSearchParams({ filename, subfolder, type }).toString() +} + +function smallObject(body: unknown): Record | null { + let value = body + if (typeof body === 'string') { + if (body.length > 10_000) return null + try { + value = JSON.parse(body) + } catch { + return null + } + } + if (!value || typeof value !== 'object' || Array.isArray(value)) return null + return JSON.stringify(value).length > 10_000 ? null : (value as Record) +} + +export function interruptJson(body: unknown): string | null { + const v = smallObject(body) + if (!v || Object.keys(v).join() !== 'prompt_id' || !isRunId(v['prompt_id'])) return null + return JSON.stringify({ prompt_id: v['prompt_id'] }) +} + +export function queueJson(body: unknown): string | null { + const v = smallObject(body) + if (!v || Object.keys(v).join() !== 'delete') return null + const ids = v['delete'] + if (!Array.isArray(ids) || ids.length < 1 || ids.length > 50 || !ids.every(isRunId)) return null + return JSON.stringify({ delete: ids }) +} + +export interface RunSeen { + startedAt: number + done?: number + of?: number +} + +const MAX_RUNS_SEEN = 50 +const RUN_END_TYPES = ['execution_success', 'execution_error', 'execution_interrupted'] + +export function noteRun( + seen: Map, + msg: { type: string; data: unknown }, + now: number +): void { + const data = (msg.data ?? {}) as { prompt_id?: unknown; value?: unknown; max?: unknown } + const id = data.prompt_id + if (!isRunId(id)) return + if (msg.type === 'execution_start') { + seen.delete(id) + seen.set(id, { startedAt: now }) + for (const old of seen.keys()) { + if (seen.size <= MAX_RUNS_SEEN) break + seen.delete(old) + } + return + } + if (msg.type === 'progress') { + const run = seen.get(id) + const { value, max } = data + if (!run || typeof value !== 'number' || typeof max !== 'number') return + if (!Number.isFinite(value) || !Number.isFinite(max) || max <= 0) return + run.done = Math.max(0, Math.min(value, max)) + run.of = max + return + } + if (RUN_END_TYPES.includes(msg.type)) seen.delete(id) +} + +export interface QueueRun { + id: string + app: string | null + seconds: number | null + startedAt?: number + done?: number + of?: number +} + +export interface QueueView { + running: QueueRun[] + pending: QueueRun[] +} + +const MAX_QUEUE_RUNS = 100 +const APP_TAG_NAME = /^[A-Za-z0-9 .,()&'-]{1,40}$/ + +function appTag(extra: unknown): { app: string | null; seconds: number | null } { + const tag = (extra as { comfy_app?: unknown } | null)?.comfy_app + if (!tag || typeof tag !== 'object') return { app: null, seconds: null } + const { name, seconds } = tag as { name?: unknown; seconds?: unknown } + return { + app: typeof name === 'string' && APP_TAG_NAME.test(name) ? name : null, + seconds: + typeof seconds === 'number' && Number.isFinite(seconds) && seconds > 0 && seconds <= 86_400 + ? Math.round(seconds) + : null + } +} + +function queueRuns(list: unknown, seen: ReadonlyMap): QueueRun[] { + if (!Array.isArray(list)) return [] + const runs: Array<{ n: number; run: QueueRun }> = [] + for (const item of list) { + if (!Array.isArray(item)) continue + const n = typeof item[0] === 'number' && Number.isFinite(item[0]) ? item[0] : 0 + const id = isRunId(item[1]) ? item[1] : '' + const s = id ? seen.get(id) : undefined + runs.push({ + n, + run: { + id, + ...appTag(item[3]), + ...(s ? { startedAt: s.startedAt } : {}), + ...(s?.of ? { done: s.done ?? 0, of: s.of } : {}) + } + }) + } + return runs + .sort((a, b) => a.n - b.n) + .slice(0, MAX_QUEUE_RUNS) + .map((r) => r.run) +} + +export function queueView(raw: unknown, seen: ReadonlyMap = new Map()): QueueView { + const q = (raw && typeof raw === 'object' ? raw : {}) as { + queue_running?: unknown + queue_pending?: unknown + } + return { running: queueRuns(q.queue_running, seen), pending: queueRuns(q.queue_pending, seen) } +} + +export function queueAnswer( + status: number, + body: Uint8Array, + seen: ReadonlyMap +): LocalAnswer { + if (status < 200 || status >= 300) return textAnswer(status, 'ComfyUI did not show its queue.') + let raw: unknown + try { + raw = JSON.parse(new TextDecoder().decode(body)) + } catch { + return textAnswer(502, 'ComfyUI sent a queue Desktop could not read.') + } + return { + status: 200, + contentType: 'application/json', + body: new TextEncoder().encode(JSON.stringify(queueView(raw, seen))) + } +} + +function promptJson(body: unknown): string | null { + let value = body + if (typeof body === 'string') { + if (body.length > MAX_PROMPT_BODY_BYTES) return null + try { + value = JSON.parse(body) + } catch { + return null + } + } + if (!value || typeof value !== 'object' || Array.isArray(value)) return null + if (!isWorkflow((value as { prompt?: unknown }).prompt)) return null + const json = JSON.stringify(value) + return json.length > MAX_PROMPT_BODY_BYTES ? null : json +} + +export function localRequest(input: unknown): LocalRequest | LocalRefusal { + if (!input || typeof input !== 'object') return { status: 400, error: 'Not a request.' } + const { method, path, body, fileName } = input as Record + if (typeof path !== 'string' || !path.startsWith('/') || path.length > 2048) { + return { status: 400, error: 'Not a path.' } + } + let url: URL + try { + url = new URL(path, LOCAL_BASE) + } catch { + return { status: 400, error: 'Not a path.' } + } + if (url.origin !== LOCAL_BASE) return { status: 400, error: 'Not a path.' } + const p = url.pathname + const query = url.search !== '' + if (method === 'GET') { + if (p === '/system_stats' && !query) return { kind: 'get', path: p } + if (p === '/queue' && !query) return { kind: 'queue', path: p } + if (/^\/object_info\/[A-Za-z0-9_]{1,200}$/.test(p) && !query) return { kind: 'get', path: p } + if (/^\/history\/[A-Za-z0-9-]{1,64}$/.test(p) && !query) return { kind: 'get', path: p } + if (p === '/view') { + const q = viewQuery(url.searchParams) + return q ? { kind: 'get', path: `/view?${q}` } : { status: 400, error: 'Not a file.' } + } + return { status: 404, error: 'Apps cannot ask ComfyUI for that.' } + } + if (method === 'POST') { + if (p === '/prompt' && !query) { + const json = promptJson(body) + return json + ? { kind: 'prompt', path: p, json } + : { status: 400, error: 'That is not a workflow.' } + } + if (p === '/interrupt' && !query) { + const json = interruptJson(body) + return json + ? { kind: 'json', path: p, json } + : { status: 400, error: 'Name the run to stop.' } + } + if (p === '/queue' && !query) { + const json = queueJson(body) + return json + ? { kind: 'json', path: p, json } + : { status: 400, error: 'Name the runs to remove.' } + } + if (p === '/upload/image' && !query) { + if (!(body instanceof Uint8Array)) return { status: 400, error: 'Not a file.' } + if (body.byteLength > MAX_UPLOAD_BYTES) + return { status: 413, error: 'Pick a file under 50 MB.' } + return { kind: 'upload', path: p, bytes: body, fileName: safeUploadName(fileName) } + } + return { status: 404, error: 'Apps cannot send ComfyUI that.' } + } + return { status: 405, error: 'Apps can only GET and POST.' } +} + +export async function readCapped( + res: Response, + max: number, + onChunk: () => void = () => {} +): Promise { + if (Number(res.headers.get('content-length')) > max) { + await res.body?.cancel().catch(() => {}) + return null + } + if (!res.body) return new Uint8Array() + const reader = res.body.getReader() + const chunks: Uint8Array[] = [] + let size = 0 + for (;;) { + const { done, value } = await reader.read() + if (done) break + size += value.byteLength + if (size > max) { + await reader.cancel().catch(() => {}) + return null + } + chunks.push(value) + onChunk() + } + const out = new Uint8Array(size) + let at = 0 + for (const c of chunks) { + out.set(c, at) + at += c.byteLength + } + return out +} + +export const JOB_GRACE_MS = 30_000 + +export function noteSubmitted(submitted: Map, runId: string, now: number): void { + for (const [id, at] of submitted) if (now - at >= JOB_GRACE_MS) submitted.delete(id) + submitted.set(runId, now) +} + +export function inJobGrace(submitted: Map, runId: string, now: number): boolean { + const at = submitted.get(runId) + if (at === undefined) return false + if (now - at < JOB_GRACE_MS) return true + submitted.delete(runId) + return false +} + +export type JobsAnswer = 'read' | 'wait' | 'refused' | { error: string } + +export function jobsAnswer(status: number, inGrace: boolean): JobsAnswer { + if (status >= 200 && status < 300) return 'read' + if (status === 429 || status >= 500) return 'wait' + if (status === 404) { + return inGrace ? 'wait' : { error: 'Comfy Cloud cannot find this run. Run the app again.' } + } + if (status === 401) return { error: 'Your Comfy sign-in ended. Sign in again, then run the app.' } + if (status === 403) return { error: 'Your Comfy account does not have access to this run.' } + return 'refused' +} + +export const LOCAL_EVENT_TYPES = [ + 'execution_start', + 'execution_cached', + 'executing', + 'progress', + 'progress_state', + 'executed', + 'execution_success', + 'execution_error', + 'execution_interrupted', + 'status' +] + +export function localEvent(frame: unknown): { type: string; data: unknown } | null { + if (typeof frame !== 'string') return null + let msg: unknown + try { + msg = JSON.parse(frame) + } catch { + return null + } + if (!msg || typeof msg !== 'object') return null + const { type, data } = msg as { type?: unknown; data?: unknown } + return typeof type === 'string' && LOCAL_EVENT_TYPES.includes(type) ? { type, data } : null +} + +export function reconnectDelay(attempt: number): number { + return Math.min(30_000, 1000 * 2 ** Math.max(0, attempt)) +} + +export interface LocalInstall { + id: string + name: string + sourceId: string + status?: string + lastLaunchedAt?: number + lastLaunchedAtByCategory?: Record + appsBuild?: unknown +} + +export function localInstalls( + list: readonly T[], + categoryOf: (inst: T) => string | undefined +): T[] { + const usable = list.filter( + (inst) => + categoryOf(inst) === 'local' && + inst.sourceId !== 'desktop' && + inst.appsBuild !== true && + (inst.status === undefined || inst.status === 'installed') + ) + const at = (inst: T) => inst.lastLaunchedAtByCategory?.['local'] ?? inst.lastLaunchedAt ?? -1 + return usable + .map((inst, i) => ({ inst, i })) + .sort((a, b) => at(b.inst) - at(a.inst) || a.i - b.i) + .map(({ inst }) => inst) +} + +export const DOWNLOAD_FOLDERS = [ + 'checkpoints', + 'controlnet', + 'loras', + 'vae', + 'upscale_models', + 'clip_vision', + 'text_encoders', + 'diffusion_models', + 'model_patches', + 'background_removal', + 'frame_interpolation', + 'geometry_estimation', + 'audio_encoders' +] + +export interface DownloadRequest { + url: string + folder: string + name: string +} + +export function downloadRequest(input: unknown): DownloadRequest | { error: string } { + if (!input || typeof input !== 'object') return { error: 'Name a model to download.' } + const { url, folder, name } = input as Record + let parsed: URL + try { + parsed = new URL(typeof url === 'string' ? url : '') + } catch { + return { error: 'Models download only from huggingface.co.' } + } + if ( + parsed.protocol !== 'https:' || + parsed.hostname !== 'huggingface.co' || + parsed.port !== '' || + parsed.username !== '' || + parsed.password !== '' || + parsed.pathname.length < 2 + ) { + return { error: 'Models download only from huggingface.co.' } + } + if (typeof folder !== 'string' || !DOWNLOAD_FOLDERS.includes(folder)) { + return { error: 'Apps cannot put a model in that folder.' } + } + if (typeof name !== 'string' || !/^[A-Za-z0-9._-]{1,128}\.(safetensors|pth)$/.test(name)) { + return { error: 'A model name ends in .safetensors or .pth.' } + } + if (name.startsWith('.')) return { error: 'A model name ends in .safetensors or .pth.' } + return { url: parsed.href, folder, name } +} + +const ERR_ABORTED = -3 + +export function isPageLoadFailure(errorCode: number): boolean { + return errorCode !== 0 && errorCode !== ERR_ABORTED +} + +export function isDocumentFailure(httpResponseCode: number): boolean { + return httpResponseCode > 0 && (httpResponseCode < 200 || httpResponseCode >= 300) +} diff --git a/src/main/apps/localComfy.ts b/src/main/apps/localComfy.ts new file mode 100644 index 000000000..d8dac5c05 --- /dev/null +++ b/src/main/apps/localComfy.ts @@ -0,0 +1,413 @@ +import { randomUUID } from 'crypto' +import type { WebContents } from 'electron' +import { promises as fsp } from 'fs' + +import { getEntryByInstallationId } from '../host/registry' +import type { InstallationRecord } from '../installations' +import { + _getActiveLaunchIds, + _getLaunchingInstances, + _runningSessions, + sessionLifecycleEvents, + stopRunning +} from '../lib/ipc/shared' +import { startManagedModelJob } from '../lib/comfyDownloadManager' +import { COMFY_BOOT_TIMEOUT_MS } from '../lib/process' +import { + appsBuildId, + appsLocalOff, + appsSetupRunning, + cancelAppsSetup, + ensureAppsBuild, + findApps, + launchAppsBuild, + planAppsBuild +} from './appsBuild' +import { + APPS_BUILD_NAME, + FREE_IDLE_MS, + FREE_JSON, + freeWait, + isRunEnd, + queueEmpty, + type SetupEvent, + shouldStopIdle +} from './appsBuildRules' +import { + type DownloadRequest, + downloadRequest, + type LocalAnswer, + localEvent, + localRequest, + MAX_UPLOAD_BYTES, + noteRun, + queueAnswer, + readCapped, + reconnectDelay, + type RunSeen, + textAnswer, + uploadForm +} from './guards' + +export interface LocalState { + state: 'running' | 'starting' | 'stopped' | 'none' | 'unsupported' + name?: string + clientId?: string + error?: string + setup?: boolean + download?: number +} + +export const LOCAL_EVENT_CHANNEL = 'comfy-apps:local-event' + +const CLIENT_ID = randomUUID() +const FETCH_TIMEOUT_MS = 60_000 +const IDLE_CHECK_MS = 60_000 + +let appsPages: () => WebContents[] = () => [] +let starting: Promise | null = null +let lastUse = Date.now() +const runsSeen = new Map() + +function touch(): void { + lastUse = Date.now() +} + +export function appsRunning(): { id: string; name: string; port: number } | null { + const id = appsBuildId() + const s = id ? _runningSessions.get(id) : undefined + return id && s && !s.url && s.port > 0 ? { id, name: s.installationName, port: s.port } : null +} + +function launching(id: string): boolean { + return ( + _getLaunchingInstances().some((l) => l.installationId === id) || + _getActiveLaunchIds().includes(id) + ) +} + +async function currentState(): Promise<{ state: LocalState; install?: InstallationRecord }> { + const off = await appsLocalOff() + if (off) return { state: { state: 'unsupported', error: off } } + const inst = await findApps() + const run = appsRunning() + if (run) { + return { + state: { state: 'running', name: run.name, clientId: CLIENT_ID }, + ...(inst ? { install: inst } : {}) + } + } + if (appsSetupRunning()) + return { state: { state: 'starting', name: APPS_BUILD_NAME, setup: true } } + if (!inst || inst.status !== 'installed') { + const download = await planAppsBuild().then( + (p) => p.bytes, + () => 0 + ) + return { state: { state: 'none', name: APPS_BUILD_NAME, ...(download ? { download } : {}) } } + } + if (launching(inst.id)) return { state: { state: 'starting', name: inst.name }, install: inst } + return { state: { state: 'stopped', name: inst.name }, install: inst } +} + +export async function getLocal(): Promise { + syncLocalSocket() + const { state } = await currentState() + return state.state === 'stopped' && starting ? { ...state, state: 'starting' } : state +} + +async function answers(port: number): Promise { + try { + const res = await fetch(`http://127.0.0.1:${port}/system_stats`, { + signal: AbortSignal.timeout(5000) + }) + await res.body?.cancel().catch(() => {}) + return res.ok + } catch { + return false + } +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) + +async function untilAnswering(name: string | undefined): Promise { + const deadline = Date.now() + COMFY_BOOT_TIMEOUT_MS + while (Date.now() < deadline) { + const run = appsRunning() + if (run && (await answers(run.port))) { + touch() + syncLocalSocket() + return { state: 'running', name: run.name, clientId: CLIENT_ID } + } + if (!run) { + const { state } = await currentState() + if (state.state !== 'starting') { + return { state: 'stopped', name, error: 'ComfyUI stopped before it was ready.' } + } + } + await sleep(1000) + } + return { state: 'stopped', name, error: 'ComfyUI did not answer within 5 minutes.' } +} + +function shortError(message: string | undefined): string { + const line = (message ?? '').split('\n')[0]!.trim() + return line ? line.slice(0, 200) : 'ComfyUI did not start.' +} + +function sendSetup(event: SetupEvent): void { + forward({ type: 'setup', data: event }) +} + +async function start(): Promise { + const { state } = await currentState() + if (state.state === 'unsupported' || state.state === 'running') return state + let inst: InstallationRecord + try { + inst = await ensureAppsBuild(sendSetup) + } catch (err) { + return { state: 'none', name: APPS_BUILD_NAME, error: shortError((err as Error)?.message) } + } + touch() + if (!appsRunning() && !launching(inst.id)) { + const result = await launchAppsBuild(inst) + if (!result.ok && !appsRunning()) { + return { state: 'stopped', name: inst.name, error: shortError(result.message) } + } + } + return untilAnswering(inst.name) +} + +export function startLocal(): Promise { + starting ??= start().finally(() => (starting = null)) + return starting +} + +export function cancelSetup(): { cancelled: boolean } { + return cancelAppsSetup() +} + +export async function localFetch(input: unknown): Promise { + const req = localRequest(input) + if (!('kind' in req)) return textAnswer(req.status, req.error) + const run = appsRunning() + if (!run) return textAnswer(503, 'ComfyUI for apps is not running on this computer.') + touch() + syncLocalSocket() + const signal = AbortSignal.timeout(FETCH_TIMEOUT_MS) + const init: RequestInit = + req.kind === 'prompt' || req.kind === 'json' + ? { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: req.json, + signal + } + : req.kind === 'upload' + ? { method: 'POST', body: uploadForm(req.bytes, req.fileName), signal } + : { method: 'GET', signal } + try { + const res = await fetch(`http://127.0.0.1:${run.port}${req.path}`, init) + const body = await readCapped(res, MAX_UPLOAD_BYTES) + if (!body) return textAnswer(502, 'ComfyUI answered more than 50 MB.') + if (req.kind === 'queue') return queueAnswer(res.status, body, runsSeen) + return { + status: res.status, + contentType: res.headers.get('content-type') ?? 'application/octet-stream', + body + } + } catch (err) { + return (err as Error)?.name === 'TimeoutError' + ? textAnswer(504, 'ComfyUI took too long to answer.') + : textAnswer(502, 'ComfyUI did not answer.') + } +} + +export type LocalDownload = { state: 'done' } | { state: 'failed'; error: string } + +const downloads = new Map>() + +async function runDownload(installationId: string, req: DownloadRequest): Promise { + let done = 0 + let total = 0 + let moved = false + const send = (state: 'running' | 'done' | 'failed', error?: string) => + forward({ + type: 'download', + data: { ...req, done, total, state, ...(error ? { error } : {}) } + }) + const failed = (error: string): LocalDownload => { + send('failed', error) + return { state: 'failed', error } + } + const handle = await startManagedModelJob({ + url: req.url, + filename: req.name, + directory: req.folder, + installationId, + onProgress: (received, size) => { + done = received + total = size + moved = true + } + }).catch(() => null) + if (!handle) return failed('Desktop could not start the download.') + const timer = setInterval(() => { + if (!moved) return + moved = false + send('running') + }, 250) + let outcome: Awaited + try { + outcome = await handle.completion + } finally { + clearInterval(timer) + handle.release() + } + if (outcome.status === 'cancelled') return failed('The download was cancelled.') + if (outcome.status === 'error') return failed(shortError(outcome.error)) + const size = await fsp.stat(outcome.savePath).then( + (st) => st.size, + () => total + ) + done = total = size + send('done') + return { state: 'done' } +} + +export async function localDownload(input: unknown): Promise { + const req = downloadRequest(input) + if ('error' in req) return { state: 'failed', error: req.error } + const installationId = appsRunning()?.id ?? (await findApps())?.id + if (!installationId) return { state: 'failed', error: 'Start ComfyUI for apps first.' } + touch() + const key = [installationId, req.url, req.folder, req.name].join('\n') + let job = downloads.get(key) + if (!job) { + job = runDownload(installationId, req).finally(() => downloads.delete(key)) + downloads.set(key, job) + } + return job +} + +interface Socket { + port: number + ws: WebSocket | null + timer: ReturnType | null + attempt: number + closed: boolean +} + +let socket: Socket | null = null + +function forward(msg: { type: string; data: unknown }): void { + for (const wc of appsPages()) { + if (!wc.isDestroyed()) wc.send(LOCAL_EVENT_CHANNEL, msg) + } +} + +function connect(s: Socket): void { + if (s.closed) return + const ws = new WebSocket(`ws://127.0.0.1:${s.port}/ws?clientId=${CLIENT_ID}`) + ws.binaryType = 'arraybuffer' + s.ws = ws + ws.onopen = () => { + s.attempt = 0 + } + ws.onmessage = (ev) => { + const msg = localEvent(ev.data) + if (!msg) return + touch() + noteRun(runsSeen, msg, Date.now()) + if (isRunEnd(msg.type)) scheduleFree(s.port) + forward(msg) + } + ws.onerror = () => {} + ws.onclose = () => { + if (s.closed) return + s.ws = null + s.timer = setTimeout(() => connect(s), reconnectDelay(s.attempt++)) + } +} + +function closeSocket(): void { + if (!socket) return + socket.closed = true + if (socket.timer) clearTimeout(socket.timer) + socket.ws?.close() + socket = null + runsSeen.clear() +} + +export function syncLocalSocket(): void { + const run = appsPages().length ? appsRunning() : null + if (socket && run && socket.port === run.port) return + closeSocket() + if (!run) return + socket = { port: run.port, ws: null, timer: null, attempt: 0, closed: false } + connect(socket) +} + +async function queueIsEmpty(port: number): Promise { + try { + const res = await fetch(`http://127.0.0.1:${port}/prompt`, { + signal: AbortSignal.timeout(5000) + }) + return res.ok && queueEmpty(await res.json()) + } catch { + return false + } +} + +export async function freeIfDone(port: number): Promise { + const seen = lastUse + if (appsRunning()?.port !== port || !(await queueIsEmpty(port))) return false + if (lastUse !== seen || appsRunning()?.port !== port) return false + try { + const res = await fetch(`http://127.0.0.1:${port}/free`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: FREE_JSON, + signal: AbortSignal.timeout(10_000) + }) + await res.body?.cancel().catch(() => {}) + return res.ok + } catch { + return false + } +} + +let freeTimer: ReturnType | null = null + +function scheduleFree(port: number, wait = FREE_IDLE_MS): void { + if (freeTimer) clearTimeout(freeTimer) + freeTimer = setTimeout(() => { + freeTimer = null + const left = freeWait(lastUse, Date.now()) + if (left > 0) return scheduleFree(port, left) + void freeIfDone(port) + }, wait) +} + +async function stopWhenIdle(): Promise { + const run = appsRunning() + if (!run) return + const entry = getEntryByInstallationId(run.id) + const idle = shouldStopIdle({ + mode: _runningSessions.get(run.id)?.mode ?? '', + hasWindow: !!entry && !entry.window.isDestroyed(), + lastUse, + now: Date.now() + }) + if (!idle) return + const seen = lastUse + if (downloads.size > 0 || starting || !(await queueIsEmpty(run.port))) return touch() + if (lastUse !== seen || downloads.size > 0 || starting) return + await stopRunning(run.id) +} + +export function initLocalComfy(pages: () => WebContents[]): void { + appsPages = pages + sessionLifecycleEvents.on('changed', syncLocalSocket) + setInterval(() => void stopWhenIdle(), IDLE_CHECK_MS).unref?.() +} diff --git a/src/main/apps/openInComfy.test.ts b/src/main/apps/openInComfy.test.ts new file mode 100644 index 000000000..1ee4d4888 --- /dev/null +++ b/src/main/apps/openInComfy.test.ts @@ -0,0 +1,62 @@ +// @vitest-environment node +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const listInstalls = vi.fn() +const launchInstall = vi.fn() +const runningSessions = new Map() + +vi.mock('../host/panelView', () => ({ setActivePanel: vi.fn() })) +vi.mock('../host/registry', () => ({ getEntryByInstallationId: () => null })) +vi.mock('../installations', () => ({ CLOUD_SOURCE_ID: 'cloud', list: () => listInstalls() })) +vi.mock('../lib/ipc/shared', () => ({ _runningSessions: runningSessions })) +vi.mock('./appsBuild', () => ({ + categoryOf: () => 'local', + findApps: vi.fn(), + launchAppsBuild: vi.fn(), + launchInstall: (...args: unknown[]) => launchInstall(...args), + showComfyWindow: vi.fn() +})) + +const { openInComfy } = await import('./openInComfy') + +const onCloud = { + where: 'cloud', + prompt: { '1': { class_type: 'SaveImage', inputs: {} } } +} + +beforeEach(() => { + vi.clearAllMocks() + runningSessions.clear() +}) + +describe('openInComfy on Comfy Cloud', () => { + it('asks the user to add Comfy Cloud when there is none', async () => { + listInstalls.mockResolvedValue([]) + + expect(await openInComfy(onCloud)).toEqual({ + opened: false, + error: 'Add Comfy Cloud in Comfy Desktop first.' + }) + expect(launchInstall).not.toHaveBeenCalled() + }) + + it('reports why Comfy Cloud did not start instead of asking to add it again', async () => { + listInstalls.mockResolvedValue([{ id: 'cloud-1', sourceId: 'cloud', status: 'installed' }]) + launchInstall.mockResolvedValue({ ok: false, message: 'Sign in to Comfy Cloud first.' }) + + expect(await openInComfy(onCloud)).toEqual({ + opened: false, + error: 'Sign in to Comfy Cloud first.' + }) + }) + + it('says Comfy Cloud could not open when the launch gives no reason', async () => { + listInstalls.mockResolvedValue([{ id: 'cloud-1', sourceId: 'cloud' }]) + launchInstall.mockResolvedValue({ ok: false }) + + expect(await openInComfy(onCloud)).toEqual({ + opened: false, + error: 'Comfy Desktop could not open Comfy Cloud.' + }) + }) +}) diff --git a/src/main/apps/openInComfy.ts b/src/main/apps/openInComfy.ts new file mode 100644 index 000000000..76a5a9a5f --- /dev/null +++ b/src/main/apps/openInComfy.ts @@ -0,0 +1,136 @@ +import { setActivePanel } from '../host/panelView' +import { getEntryByInstallationId } from '../host/registry' +import type { ComfyWindowEntry } from '../host/registry' +import * as installations from '../installations' +import { _runningSessions } from '../lib/ipc/shared' +import { categoryOf, findApps, launchAppsBuild, launchInstall, showComfyWindow } from './appsBuild' +import { pickOpenTarget } from './appsBuildRules' +import { + COMFY_READY_SCRIPT, + isComfyPage, + localInstalls, + type OpenRequest, + openRequest, + openWorkflowScript +} from './guards' + +export type OpenResult = { opened: true } | { opened: false; error: string } + +const WINDOW_TIMEOUT_MS = 20_000 +const READY_TIMEOUT_MS = 180_000 + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) + +export type LocalBuild = { id: string; name: string } + +async function ownBuilds(): Promise { + return localInstalls(await installations.list(), categoryOf) +} + +export async function localBuilds(): Promise { + return (await ownBuilds()).map((inst) => ({ id: inst.id, name: inst.name })) +} + +function startedAt(): Map { + const at = new Map() + for (const [id, s] of _runningSessions) { + if (!s.url && (s.sourceInstallationId ?? id) === id) at.set(id, s.startedAt) + } + return at +} + +async function running(inst: installations.InstallationRecord): Promise { + if (!_runningSessions.has(inst.id)) { + const result = await launchInstall(inst) + if (!result.ok && !_runningSessions.has(inst.id)) return null + } + if (_runningSessions.get(inst.id)?.mode === 'console' && !showComfyWindow(inst)) return null + return inst.id +} + +async function appsBuildWindow(): Promise { + const apps = await findApps() + if (apps?.status !== 'installed') return null + if (_runningSessions.has(apps.id)) return showComfyWindow(apps) ? apps.id : null + const result = await launchAppsBuild(apps, true) + return result.ok || _runningSessions.has(apps.id) ? apps.id : null +} + +async function localInstallId(build?: string): Promise { + const target = pickOpenTarget(await ownBuilds(), startedAt(), build) + if (target) return running(target) + return build === undefined ? appsBuildWindow() : null +} + +type InstallTarget = { id: string } | { error: string } + +async function cloudInstallTarget(): Promise { + const usable = (i: installations.InstallationRecord) => + i.sourceId === installations.CLOUD_SOURCE_ID && + (i.status === undefined || i.status === 'installed') + const inst = (await installations.list()).find(usable) + if (!inst) return { error: 'Add Comfy Cloud in Comfy Desktop first.' } + if (_runningSessions.has(inst.id)) return { id: inst.id } + const result = await launchInstall(inst) + if (result.ok || _runningSessions.has(inst.id)) return { id: inst.id } + return { error: result.message || 'Comfy Desktop could not open Comfy Cloud.' } +} + +async function localInstallTarget(build?: string): Promise { + const id = await localInstallId(build) + return id ? { id } : { error: 'Comfy Desktop could not start your ComfyUI.' } +} + +function liveEntry(installationId: string): ComfyWindowEntry | null { + const entry = getEntryByInstallationId(installationId) + return entry && !entry.window.isDestroyed() ? entry : null +} + +async function windowFor(installationId: string): Promise { + const deadline = Date.now() + WINDOW_TIMEOUT_MS + for (;;) { + const entry = liveEntry(installationId) + if (entry || Date.now() > deadline) return entry + await sleep(250) + } +} + +function bringForward(entry: ComfyWindowEntry): void { + if (entry.window.isMinimized()) entry.window.restore() + entry.window.show() + entry.window.focus() + if (entry.activePanel !== 'progress') setActivePanel(entry.windowKey, 'comfy') +} + +async function loadInto(installationId: string, req: OpenRequest): Promise { + const deadline = Date.now() + READY_TIMEOUT_MS + while (Date.now() < deadline) { + const entry = liveEntry(installationId) + if (!entry) return false + const wc = entry.comfyView.webContents + if (wc.isDestroyed()) return false + if (!wc.isLoading() && isComfyPage(wc.getURL(), entry.comfyUrl)) { + const ready = await wc.executeJavaScript(COMFY_READY_SCRIPT).catch(() => false) + if (ready === true) { + const opened = await wc.executeJavaScript(openWorkflowScript(req), true).catch(() => false) + return opened === true + } + } + await sleep(500) + } + return false +} + +export async function openInComfy(input: unknown): Promise { + const req = openRequest(input) + if ('error' in req) throw new Error(req.error) + const target = + req.where === 'local' ? await localInstallTarget(req.build) : await cloudInstallTarget() + if ('error' in target) return { opened: false, error: target.error } + const { id } = target + const entry = await windowFor(id) + if (!entry) return { opened: false, error: 'Comfy Desktop has no window for that ComfyUI.' } + bringForward(entry) + if (await loadInto(id, req)) return { opened: true } + return { opened: false, error: 'ComfyUI did not take the workflow.' } +} diff --git a/src/main/apps/wiring.test.ts b/src/main/apps/wiring.test.ts new file mode 100644 index 000000000..a81fad95b --- /dev/null +++ b/src/main/apps/wiring.test.ts @@ -0,0 +1,51 @@ +// @vitest-environment node +import { readFileSync } from 'fs' +import { resolve } from 'path' +import { describe, expect, it } from 'vitest' + +const preload = readFileSync(resolve(__dirname, '../../preload/appsPreload.ts'), 'utf8') +const main = readFileSync(resolve(__dirname, 'appsView.ts'), 'utf8') +const local = readFileSync(resolve(__dirname, 'localComfy.ts'), 'utf8') + +const all = (text: string, re: RegExp) => [...text.matchAll(re)].map((m) => m[1]!) + +const calls = all(preload.match(/const CALLS = \[([\s\S]*?)\]/)![1]!, /'(\w+)'/g) +const exposed = all( + preload.match(/exposeInMainWorld\('comfyDesktopApps', \{([\s\S]*)\}\)/)![1]!, + /^ {2}(\w+):/gm +) +const invoked = all(preload, /ipcRenderer\.invoke\('([\w:-]+)'/g) +const handled = all(main, /handleFromApps\('([\w:-]+)'/g) + +describe('Apps bridge wiring', () => { + it('getInfo names exactly the calls the page gets', () => { + expect([...calls].sort()).toEqual([...exposed].sort()) + }) + + it('has the local ComfyUI calls', () => { + for (const c of [ + 'getLocal', + 'startLocal', + 'cancelSetup', + 'localBuilds', + 'localFetch', + 'onLocalEvent', + 'localDownload', + 'cancelRun', + 'openInComfy' + ]) { + expect(calls).toContain(c) + } + }) + + it('every channel the page invokes is answered behind the apps-page check', () => { + for (const channel of invoked) expect(handled).toContain(channel) + expect(main).not.toMatch(/ipcMain\.handle\('comfy-apps:/) + }) + + it('local events go out on the channel the preload listens to', () => { + const sent = local.match(/LOCAL_EVENT_CHANNEL = '([\w:-]+)'/)![1] + expect(preload).toContain(`ipcRenderer.on('${sent}'`) + expect(preload).toContain(`ipcRenderer.removeListener('${sent}'`) + }) +}) diff --git a/src/main/host/panelView.test.ts b/src/main/host/panelView.test.ts index 89adc9148..5770cd278 100644 --- a/src/main/host/panelView.test.ts +++ b/src/main/host/panelView.test.ts @@ -214,6 +214,8 @@ describe('comfy-window:navigate-panel', () => { it.each([ ['chooser', 'comfy', 'benchmarks'], + ['apps', 'comfy', 'benchmarks'], + ['assets', 'comfy', 'benchmarks'], ['benchmarks', 'benchmarks', 'comfy'], ['performance-test', 'performance-test', 'comfy'] ] as const)('opens %s in a dashboard window', (page, panel, startingPanel) => { diff --git a/src/main/host/panelView.ts b/src/main/host/panelView.ts index 13d193696..4d7efbbf2 100644 --- a/src/main/host/panelView.ts +++ b/src/main/host/panelView.ts @@ -22,6 +22,8 @@ import type { BodyMode, ComfyPanelKey, ComfyWindowEntry } from './registry' const DASHBOARD_PAGE_PANELS: ReadonlyMap = new Map([ ['home', 'comfy'], + ['apps', 'comfy'], + ['assets', 'comfy'], ['chooser', 'comfy'], ['benchmarks', 'benchmarks'], ['performance-test', 'performance-test'] diff --git a/src/main/host/registry.test.ts b/src/main/host/registry.test.ts index cb5c56d20..3c2a63667 100644 --- a/src/main/host/registry.test.ts +++ b/src/main/host/registry.test.ts @@ -24,6 +24,7 @@ import { consumeAttachClaim, dropAttachClaimsForWindow, dropInstallationIndex, + findHostWindowByPanelSender, findPreferredHostByVisibility, getEntryByInstallationId, hostInstallEvents, @@ -467,6 +468,20 @@ describe('findPreferredHostByVisibility', () => { }) }) +describe('findHostWindowByPanelSender', () => { + it('returns the window whose panel sent the message', () => { + const panelSender = {} + const other = makeEntry({ installationId: 'inst-A' }) + const owner = makeEntry({ installationId: null }) + owner.panelView = { webContents: panelSender } as unknown as ComfyWindowEntry['panelView'] + registerHostEntry(other) + registerHostEntry(owner) + + expect(findHostWindowByPanelSender(panelSender as never)).toBe(owner.window) + expect(findHostWindowByPanelSender({} as never)).toBeNull() + }) +}) + describe('raiseAllHostWindows', () => { function fakeWin(entry: ComfyWindowEntry): FakeWindow { return entry.window as unknown as FakeWindow diff --git a/src/main/host/registry.ts b/src/main/host/registry.ts index 0228362a9..43bd1c99b 100644 --- a/src/main/host/registry.ts +++ b/src/main/host/registry.ts @@ -196,6 +196,13 @@ export function isPanelSender(wc: WebContents): boolean { return false } +export function findHostWindowByPanelSender(wc: WebContents): BrowserWindow | null { + for (const entry of comfyWindows.values()) { + if (entry.panelView?.webContents === wc) return entry.window + } + return null +} + export function findEntryByComfySender(wc: WebContents): ComfyWindowEntry | null { for (const entry of comfyWindows.values()) { if (entry.comfyView.webContents === wc) return entry diff --git a/src/main/index.ts b/src/main/index.ts index 829fe4463..d33612455 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -30,6 +30,8 @@ import { migrateXdgPaths, persistWinDataRootChoice } from './lib/paths' import { saveWindowBounds } from './lib/windowState' import { flushLastSessionSync, recordDashboardSurface } from './lib/lastSession' import { registerProcessErrorHandlers } from './lib/processErrorHandlers' +import { registerAppsIpc } from './apps/appsView' +import { registerOutputFilesScheme, registerOutputFolderHandlers } from './lib/outputFolders' import { initAppLog, flushOperationOutput } from './lib/appLog' import { pruneCrashDumps } from './lib/crashDumps' import { createStartupReentryGate } from './lib/startupReentryGate' @@ -194,6 +196,8 @@ if (settings.get('hardwareAcceleration') === false) { app.disableHardwareAcceleration() } +registerOutputFilesScheme() + todesktop.init({ autoUpdater: false }) const APP_VERSION = getAppVersion() @@ -936,6 +940,8 @@ if (app.isPackaged && !app.requestSingleInstanceLock()) { }) setHostFactories({ createChooser: openChooserHostWindow }) registerPanelViewIpc() + registerAppsIpc() + registerOutputFolderHandlers() migrateXdgPaths() persistWinDataRootChoice() diff --git a/src/main/installations.test.ts b/src/main/installations.test.ts index 92703af85..b5394dda5 100644 --- a/src/main/installations.test.ts +++ b/src/main/installations.test.ts @@ -310,6 +310,29 @@ describe('installations.getRecent', () => { expect(recent!.id).toBe(b.id) expect(recent!.id).not.toBe(a.id) }) + + it('never picks the Apps build, so it is not opened on startup', async () => { + const installations = await loadInstallations() + const mine = await installations.add({ + name: 'Mine', + installPath: path.join(tmpRoot, 'mine'), + sourceId: 'standalone', + status: 'installed', + lastLaunchedAt: 100 + }) + await installations.add({ + name: 'Apps', + installPath: path.join(tmpRoot, 'apps'), + sourceId: 'standalone', + status: 'installed', + appsBuild: true, + lastLaunchedAt: 900, + lastLaunchedAtByCategory: { local: 900 } + }) + expect((await installations.getRecent())!.id).toBe(mine.id) + expect((await installations.resolveAutoLaunchInstall('last'))!.id).toBe(mine.id) + expect((await installations.getRecentByCategory('local', resolveCategory))!.id).toBe(mine.id) + }) }) describe('installations.resolveAutoLaunchInstall', () => { diff --git a/src/main/installations.ts b/src/main/installations.ts index 1250b9842..2c8cf5529 100644 --- a/src/main/installations.ts +++ b/src/main/installations.ts @@ -5,6 +5,7 @@ import { app } from 'electron' import { dataDir } from './lib/paths' import { readFileSafeAsync, writeFileSafe, writeFileSafeAsync } from './lib/safe-file' import type { ComfyVersion } from './lib/version' +import { isAppsBuild } from '../shared/appsBuild' export const installationEvents = new EventEmitter() @@ -380,6 +381,7 @@ export async function getRecent(): Promise { let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { + if (isAppsBuild(inst)) continue const ts = typeof inst.lastLaunchedAt === 'number' ? inst.lastLaunchedAt : -Infinity if (ts > bestTs) { bestTs = ts @@ -397,7 +399,7 @@ export async function getRecentByCategory( let best: InstallationRecord | null = null let bestTs = -Infinity for (const inst of list) { - if (resolveCategory(inst) !== category) continue + if (resolveCategory(inst) !== category || isAppsBuild(inst)) continue const byCat = inst.lastLaunchedAtByCategory as Record | undefined const perCategoryTs = byCat?.[category] const ts = diff --git a/src/main/lib/assets.test.ts b/src/main/lib/assets.test.ts new file mode 100644 index 000000000..172073750 --- /dev/null +++ b/src/main/lib/assets.test.ts @@ -0,0 +1,463 @@ +import { Writable } from 'stream' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const getInstallation = vi.fn() +const getAccessToken = vi.fn() +const outputFile = vi.fn() +const panelHostWindow = vi.fn() +const handlers = new Map unknown>() + +const showSaveDialog = vi.fn() +const written = new Map() +const createWriteStream = vi.fn((filePath: string, _options?: { flags: string }) => { + const chunks: Buffer[] = [] + return new Writable({ + write(chunk: Buffer, _encoding, done) { + chunks.push(chunk) + done() + }, + final(done) { + written.set(filePath, Buffer.concat(chunks).toString()) + done() + } + }) +}) + +vi.mock('electron', () => ({ + app: { getPath: () => '/downloads' }, + BrowserWindow: { fromWebContents: () => null, getFocusedWindow: () => ({ focused: true }) }, + dialog: { showSaveDialog: (...args: unknown[]) => showSaveDialog(...args) }, + ipcMain: { + handle: (channel: string, handler: (...args: unknown[]) => unknown) => + handlers.set(channel, handler) + } +})) +const removeFile = vi.fn(async (_path: string, _options: { force: boolean }) => {}) +const renameFile = vi.fn(async (from: string, to: string) => { + written.set(to, written.get(from) ?? '') + written.delete(from) +}) +const copyFile = vi.fn(async (from: string, to: string, _mode: number) => { + written.set(to, `copy of ${from}`) +}) +const savedSettings = new Map() +vi.mock('fs', () => ({ + default: { + constants: { COPYFILE_EXCL: 1 }, + existsSync: (dir: string) => dir === '/remembered', + createWriteStream: (filePath: string, options?: { flags: string }) => + createWriteStream(filePath, options), + promises: { + copyFile: (from: string, to: string, mode: number) => copyFile(from, to, mode), + rm: (path: string, options: { force: boolean }) => removeFile(path, options), + rename: (from: string, to: string) => renameFile(from, to) + } + } +})) +vi.mock('../settings', () => ({ + get: (key: string) => savedSettings.get(key), + set: (key: string, value: string) => savedSettings.set(key, value) +})) +vi.mock('./comfyDownloadManager', () => ({ + buildSaveDialogFilters: (name: string) => [{ name, extensions: ['*'] }] +})) +vi.mock('../host/registry', () => ({ + findHostWindowByPanelSender: (sender: unknown) => panelHostWindow(sender) +})) +vi.mock('./outputFolders', () => ({ outputFilePath: (target: string) => outputFile(target) })) +vi.mock('../installations', () => ({ get: (id: string) => getInstallation(id) })) +vi.mock('../devplatform/session', () => ({ + getCloudSession: () => ({ getAccessToken }) +})) +vi.mock('./ipc/shared', () => ({ + sourceMap: { + cloud: { category: 'cloud' }, + standalone: { category: 'local' }, + remote: { category: 'remote' } + } +})) + +const { assetKind, downloadAsset, listAssets, registerAssetsHandlers, signedStorageUrl } = + await import('./assets') + +const fetchMock = vi.fn() + +function jsonResponse(body: unknown): Response { + return new Response(JSON.stringify(body), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }) +} + +beforeEach(() => { + vi.stubGlobal('fetch', fetchMock) + written.clear() + savedSettings.clear() +}) + +afterEach(() => { + vi.unstubAllGlobals() + vi.clearAllMocks() +}) + +describe('assetKind', () => { + it.each([ + ['clip.bin', 'video/mp4', 'video'], + ['still.png', undefined, 'image'], + ['narration.WAV', undefined, 'audio'], + ['mesh.glb', undefined, '3d'], + ['notes.txt', undefined, null], + ['shader.constructor', undefined, null], + ['png', undefined, null] + ])('classifies %s (%s) as %s', (name, mimeType, kind) => { + expect(assetKind(name, mimeType)).toBe(kind) + }) +}) + +describe('signedStorageUrl', () => { + it.each([ + ['https://storage.googleapis.com/bucket/a.png?sig=1', true], + ['https://evil.example.com/a.png', false], + ['http://storage.googleapis.com/a.png', false], + [null, false] + ])('accepts only https Google storage links: %s', (location, accepted) => { + expect(signedStorageUrl(location) !== null).toBe(accepted) + }) +}) + +describe('listAssets', () => { + it('never asks a local or remote build over HTTP', async () => { + getInstallation.mockResolvedValue({ id: 'local-1', sourceId: 'standalone' }) + expect(await listAssets('local-1')).toEqual([]) + getInstallation.mockResolvedValue({ id: 'remote-1', sourceId: 'remote' }) + expect(await listAssets('remote-1')).toEqual([]) + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('returns nothing for Cloud when signed out', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue(null) + + expect(await listAssets('cloud-1')).toEqual([]) + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('drops Cloud rows whose fields have the wrong type and keeps an empty size', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: [ + { id: 'bad', name: 'bad.png', mime_type: 1 }, + { id: 'good', name: 'good.png', mime_type: null, created_at: null, size: 0 } + ] + }) + ) + .mockResolvedValue(new Response(null, { status: 404 })) + + const assets = await listAssets('cloud-1') + + expect(assets.map((a) => [a.id, a.sizeBytes])).toEqual([['good', 0]]) + }) + + it('keeps one page of Cloud rows when the server sends more than it was asked for', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: Array.from({ length: 150 }, (_, i) => ({ id: `a${i}`, name: `a${i}.bin` })) + }) + ) + .mockResolvedValue(new Response(null, { status: 404 })) + + const assets = await listAssets('cloud-1') + + expect(assets).toHaveLength(100) + expect(assets.at(-1)!.id).toBe('a99') + }) + + it('lists the signed-in cloud workspace and resolves signed media links for playable kinds', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock + .mockResolvedValueOnce( + jsonResponse({ + assets: [ + { id: 'v1', name: 'clip.mp4', created_at: '2026-11-09T10:00:00Z' }, + { id: 's1', name: 'audio/voice.wav', created_at: '2026-11-09T09:00:00Z' }, + { id: 'm1', name: 'mesh.glb', created_at: '2026-11-09T08:00:00Z' } + ] + }) + ) + .mockImplementation( + async (url: string) => + new Response(null, { + status: 302, + headers: { + Location: `https://storage.googleapis.com/bucket/${/\/assets\/([^/]+)\/content/.exec(url)?.[1]}?sig=1` + } + }) + ) + + const assets = await listAssets('cloud-1') + + expect(fetchMock.mock.calls[0]![1].headers.Authorization).toBe('Bearer token') + expect(assets.map((a) => [a.name, a.kind, a.previewUrl])).toEqual([ + ['clip.mp4', 'video', 'https://storage.googleapis.com/bucket/v1?sig=1'], + ['voice.wav', 'audio', 'https://storage.googleapis.com/bucket/s1?sig=1'], + ['mesh.glb', '3d', null] + ]) + expect(fetchMock).toHaveBeenCalledTimes(3) + }) +}) + +describe('downloadAsset', () => { + const win = {} as Parameters[0] + const signedUrl = 'https://storage.googleapis.com/bucket/v1?sig=1' + const partFilePath = (): string => createWriteStream.mock.calls[0]![0] + + function fileOnThisComputer(file: string): void { + outputFile.mockImplementation(async (target: string) => (target === file ? file : null)) + } + + function cloudFile(storage: (init: { signal: AbortSignal }) => Response): void { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue('token') + fetchMock.mockImplementation(async (url: string, init: { signal: AbortSignal }) => + url === signedUrl + ? storage(init) + : new Response(null, { status: 302, headers: { Location: signedUrl } }) + ) + } + + it('copies a file on this computer to where the user picks, without asking a build', async () => { + fileOnThisComputer('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/still.png' }) + + expect(await downloadAsset(win, 'local', '/out/still.png', '../still.png')).toBe(true) + expect(showSaveDialog.mock.calls[0]![1].defaultPath).toBe('/downloads/still.png') + expect(copyFile.mock.calls[0]![0]).toBe('/out/still.png') + expect(copyFile.mock.calls[0]![1]).toMatch(/^\/picked\/still\.png\..+\.part$/) + expect(copyFile.mock.calls[0]![2], 'never overwrites a partial file').toBe(1) + expect(written.get('/picked/still.png')).toBe('copy of /out/still.png') + expect(savedSettings.get('lastSaveDialogDir')).toBe('/picked') + expect(fetchMock).not.toHaveBeenCalled() + }) + + it('refuses a file outside the output folders before asking where to save it', async () => { + outputFile.mockResolvedValue(null) + + await expect(downloadAsset(win, 'local', '/etc/passwd', 'passwd')).rejects.toThrow( + 'not available' + ) + expect(showSaveDialog).not.toHaveBeenCalled() + expect(copyFile).not.toHaveBeenCalled() + }) + + it('removes the partial copy when copying a file on this computer fails', async () => { + fileOnThisComputer('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/still.png' }) + copyFile.mockRejectedValueOnce(new Error('disk full')) + + await expect(downloadAsset(win, 'local', '/out/still.png', 'still.png')).rejects.toThrow( + 'disk full' + ) + expect(removeFile).toHaveBeenCalledWith(copyFile.mock.calls[0]![1], { force: true }) + expect(renameFile, 'the file the user picked is never overwritten').not.toHaveBeenCalled() + }) + + it('offers only a plain file name in the save dialog when the name is unusable', async () => { + fileOnThisComputer('/out/a1') + showSaveDialog.mockResolvedValue({ canceled: true }) + + await downloadAsset(win, 'local', '/out/a1', '..') + + expect(showSaveDialog.mock.calls[0]![1].defaultPath).toBe('/downloads/a1') + }) + + it('starts the save dialog in the folder the user picked last time', async () => { + savedSettings.set('lastSaveDialogDir', '/remembered') + fileOnThisComputer('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: true }) + + await downloadAsset(win, 'local', '/out/still.png', 'still.png') + + expect(showSaveDialog.mock.calls[0]![1].defaultPath).toBe('/remembered/still.png') + }) + + it('writes nothing when the save dialog is cancelled', async () => { + fileOnThisComputer('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: true }) + + expect(await downloadAsset(win, 'local', '/out/still.png', 'still.png')).toBe(false) + expect(copyFile).not.toHaveBeenCalled() + expect(createWriteStream).not.toHaveBeenCalled() + }) + + it('saves a Cloud asset through its signed storage link', async () => { + cloudFile(() => new Response('video')) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + + expect(await downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4')).toBe(true) + expect(fetchMock.mock.calls[0]![0]).toContain('/api/assets/v1/content?disposition=attachment') + expect(fetchMock.mock.calls[1]![0]).toBe(signedUrl) + expect(written.get('/picked/clip.mp4')).toBe('video') + }) + + it('does not follow a redirect away from the signed storage link', async () => { + cloudFile(() => new Response('video')) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + + await downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4') + + expect(fetchMock.mock.calls[1]![1].redirect).toBe('error') + }) + + it('remembers the save folder only after the file is saved', async () => { + fileOnThisComputer('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/still.png' }) + copyFile.mockRejectedValueOnce(new Error('disk full')) + + await expect(downloadAsset(win, 'local', '/out/still.png', 'still.png')).rejects.toThrow( + 'disk full' + ) + expect(savedSettings.has('lastSaveDialogDir')).toBe(false) + }) + + it('fails before asking where to save when signed out of Cloud', async () => { + getInstallation.mockResolvedValue({ id: 'cloud-1', sourceId: 'cloud' }) + getAccessToken.mockResolvedValue(null) + + await expect(downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4')).rejects.toThrow('not available') + expect(showSaveDialog).not.toHaveBeenCalled() + }) + + it('writes each Cloud download to its own new partial file next to the picked file', async () => { + cloudFile(() => new Response('bytes')) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + + await downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4') + await downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4') + + const [first, second] = createWriteStream.mock.calls + expect(first![0]).toMatch(/^\/picked\/clip\.mp4\..+\.part$/) + expect(second![0]).not.toBe(first![0]) + expect(first![1]).toEqual({ flags: 'wx' }) + }) + + it('fails without writing a file when storage cannot serve the asset', async () => { + let signal: AbortSignal | undefined + cloudFile((init) => { + signal = init.signal + return new Response('Service Unavailable', { status: 503 }) + }) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + + await expect(downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4')).rejects.toThrow('HTTP 503') + expect(createWriteStream).not.toHaveBeenCalled() + expect(signal?.aborted, 'the failed response is released').toBe(true) + }) + + it('removes the half-written file when the download breaks off', async () => { + cloudFile( + () => + new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('part')) + controller.error(new Error('connection reset')) + } + }) + ) + ) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + + await expect(downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4')).rejects.toThrow( + 'connection reset' + ) + expect(removeFile).toHaveBeenCalledWith(partFilePath(), { force: true }) + expect(renameFile, 'the file the user picked is never overwritten').not.toHaveBeenCalled() + }) + + it('keeps a Cloud download going while a slow disk catches up', async () => { + cloudFile( + (init) => + new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('a')) + controller.enqueue(new TextEncoder().encode('b')) + setTimeout(() => controller.close(), 50_000) + init.signal.addEventListener('abort', () => controller.error(new Error('aborted'))) + } + }) + ) + ) + createWriteStream.mockImplementationOnce((filePath: string) => { + const chunks: Buffer[] = [] + return new Writable({ + highWaterMark: 1, + write(chunk: Buffer, _encoding, done) { + chunks.push(chunk) + setTimeout(done, 30_000) + }, + final(done) { + written.set(filePath, Buffer.concat(chunks).toString()) + done() + } + }) + }) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) + try { + const download = downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4') + await vi.advanceTimersByTimeAsync(90_000) + expect(await download).toBe(true) + } finally { + vi.useRealTimers() + } + expect(written.get('/picked/clip.mp4')).toBe('ab') + }) + + it('gives up and removes the partial file when the download stops sending data', async () => { + cloudFile( + (init) => + new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('part')) + init.signal.addEventListener('abort', () => controller.error(new Error('stalled'))) + } + }) + ) + ) + showSaveDialog.mockResolvedValue({ canceled: false, filePath: '/picked/clip.mp4' }) + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) + try { + const download = downloadAsset(win, 'cloud-1', 'v1', 'clip.mp4') + const failed = expect(download).rejects.toThrow('stalled') + await vi.advanceTimersByTimeAsync(20_000) + await failed + } finally { + vi.useRealTimers() + } + expect(removeFile).toHaveBeenCalledWith(partFilePath(), { force: true }) + }) +}) + +describe('the download handler', () => { + it('opens the save dialog over the window that holds the Assets page', async () => { + const owner = { owner: true } + const sender = {} + panelHostWindow.mockImplementation((wc: unknown) => (wc === sender ? owner : null)) + outputFile.mockResolvedValue('/out/still.png') + showSaveDialog.mockResolvedValue({ canceled: true }) + registerAssetsHandlers() + + await handlers.get('assets:download')!({ sender }, 'local', '/out/still.png', 'still.png') + + expect(showSaveDialog.mock.calls[0]![0]).toBe(owner) + }) +}) diff --git a/src/main/lib/assets.ts b/src/main/lib/assets.ts new file mode 100644 index 000000000..499d40d68 --- /dev/null +++ b/src/main/lib/assets.ts @@ -0,0 +1,280 @@ +import { randomUUID } from 'crypto' +import fs from 'fs' +import path from 'path' +import { Readable } from 'stream' +import { pipeline } from 'stream/promises' +import type { ReadableStream as WebReadableStream } from 'stream/web' +import { app, BrowserWindow, dialog, ipcMain } from 'electron' +import { CLOUD_CONFIG } from '../cloud/config' +import { getCloudSession } from '../devplatform/session' +import { findHostWindowByPanelSender } from '../host/registry' +import * as installations from '../installations' +import * as settings from '../settings' +import { buildSaveDialogFilters } from './comfyDownloadManager' +import { sourceMap } from './ipc/shared' +import { outputFilePath } from './outputFolders' +import { runPool } from '../sources/standalone/templateDownloadCore' +import { LOCAL_ASSETS_SOURCE } from '../../types/ipc' +import type { AssetKind, AssetSummary } from '../../types/ipc' + +const ASSETS_LIST_CHANNEL = 'assets:list' +const ASSETS_DOWNLOAD_CHANNEL = 'assets:download' + +const PAGE_SIZE = 100 +const LIST_QUERY = `tags_any=output&sort=created_at&order=desc&limit=${PAGE_SIZE}` +const REQUEST_TIMEOUT_MS = 20_000 +const PREVIEW_BATCH_TIMEOUT_MS = 30_000 +const SIGNED_URL_CONCURRENCY = 6 +const SIGNED_STORAGE_HOST = 'storage.googleapis.com' + +const KIND_BY_EXTENSION = new Map([ + ...['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'tif', 'tiff', 'svg'].map( + (ext) => [ext, 'image'] as const + ), + ...['mp4', 'm4v', 'webm', 'mov', 'avi', 'mkv'].map((ext) => [ext, 'video'] as const), + ...['mp3', 'wav', 'ogg', 'flac', 'opus', 'm4a'].map((ext) => [ext, 'audio'] as const), + ...['obj', 'fbx', 'gltf', 'glb', 'stl', 'usdz', 'ply', 'spz', 'splat', 'ksplat'].map( + (ext) => [ext, '3d'] as const + ) +]) + +interface AssetRow { + id: string + name: string + size?: number | null + mime_type?: string | null + created_at?: string | null + preview_url?: string | null + tags?: unknown +} + +type ContentDisposition = 'inline' | 'attachment' + +function isOptional(value: unknown, type: 'string' | 'number'): boolean { + return value === undefined || value === null || typeof value === type +} + +function isAssetRow(value: unknown): value is AssetRow { + if (!value || typeof value !== 'object') return false + const row = value as Record + return ( + typeof row.id === 'string' && + typeof row.name === 'string' && + isOptional(row.size, 'number') && + isOptional(row.mime_type, 'string') && + isOptional(row.created_at, 'string') && + isOptional(row.preview_url, 'string') + ) +} + +export function assetKind(name: string, mimeType?: string | null): AssetKind | null { + const mimeKind = mimeType?.split('/')[0] + if (mimeKind === 'image' || mimeKind === 'video' || mimeKind === 'audio') return mimeKind + if (mimeType?.startsWith('model/')) return '3d' + const dot = name.lastIndexOf('.') + if (dot < 0) return null + return KIND_BY_EXTENSION.get(name.slice(dot + 1).toLowerCase()) ?? null +} + +function toSummary(row: AssetRow, previewUrl: string | null): AssetSummary { + return { + id: row.id, + name: row.name.slice(row.name.lastIndexOf('/') + 1), + kind: assetKind(row.name, row.mime_type), + sizeBytes: typeof row.size === 'number' ? row.size : null, + createdAt: row.created_at ?? null, + mimeType: row.mime_type ?? null, + tags: Array.isArray(row.tags) + ? row.tags.filter((tag): tag is string => typeof tag === 'string') + : [], + previewUrl, + thumbnailUrl: null + } +} + +async function fetchAssetRows(url: string, headers: Record): Promise { + const res = await fetch(url, { + headers: { Accept: 'application/json', ...headers }, + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) + }) + if (!res.ok) throw new Error(`List assets failed: HTTP ${res.status}`) + const body: unknown = await res.json().catch(() => null) + const rows = body && typeof body === 'object' ? (body as { assets?: unknown }).assets : undefined + if (!Array.isArray(rows)) throw new Error('List assets returned an invalid page') + return rows.filter(isAssetRow).slice(0, PAGE_SIZE) +} + +export function signedStorageUrl(location: string | null): string | null { + if (!location) return null + try { + const url = new URL(location) + return url.protocol === 'https:' && url.hostname.toLowerCase() === SIGNED_STORAGE_HOST + ? url.toString() + : null + } catch { + return null + } +} + +function contentPath(assetId: string, disposition: ContentDisposition): string { + return `/api/assets/${encodeURIComponent(assetId)}/content?disposition=${disposition}` +} + +async function resolveCloudContentUrl( + assetId: string, + accessToken: string, + disposition: ContentDisposition, + deadline: AbortSignal +): Promise { + try { + const res = await fetch(`${CLOUD_CONFIG.issuer}${contentPath(assetId, disposition)}`, { + headers: { Authorization: `Bearer ${accessToken}` }, + redirect: 'manual', + signal: AbortSignal.any([AbortSignal.timeout(REQUEST_TIMEOUT_MS), deadline]) + }) + return signedStorageUrl(res.headers.get('location')) + } catch { + return null + } +} + +async function cloudAccessToken(installationId: string): Promise { + const inst = await installations.get(installationId) + if (!inst || sourceMap[inst.sourceId]?.category !== 'cloud') return null + return getCloudSession().getAccessToken() +} + +async function listCloudAssets(accessToken: string): Promise { + const rows = await fetchAssetRows( + `${CLOUD_CONFIG.apiBase}/assets?${LIST_QUERY}&include_public=false`, + { Authorization: `Bearer ${accessToken}` } + ) + const summaries = rows.map((row) => toSummary(row, null)) + const deadline = AbortSignal.timeout(PREVIEW_BATCH_TIMEOUT_MS) + await runPool( + summaries, + SIGNED_URL_CONCURRENCY, + async (summary) => { + if (summary.kind === null || summary.kind === '3d') return + summary.previewUrl = await resolveCloudContentUrl(summary.id, accessToken, 'inline', deadline) + }, + deadline + ) + return summaries +} + +export async function listAssets(installationId: string): Promise { + const accessToken = await cloudAccessToken(installationId) + return accessToken ? listCloudAssets(accessToken) : [] +} + +type SaveTo = (filePath: string) => Promise + +async function writeThroughPartFile( + filePath: string, + write: (partPath: string) => Promise +): Promise { + const partPath = `${filePath}.${randomUUID()}.part` + try { + await write(partPath) + await fs.promises.rename(partPath, filePath) + } catch (error) { + await fs.promises.rm(partPath, { force: true }).catch(() => {}) + throw error + } +} + +async function saveDownload(url: string, filePath: string): Promise { + const controller = new AbortController() + let timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS) + const keepAlive = (): void => { + clearTimeout(timer) + timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS) + } + try { + const res = await fetch(url, { redirect: 'error', signal: controller.signal }) + if (!res.ok || !res.body) throw new Error(`Download failed: HTTP ${res.status}`) + const body = Readable.fromWeb(res.body as WebReadableStream) + body.on('data', keepAlive) + body.on('pause', () => clearTimeout(timer)) + await writeThroughPartFile(filePath, (partPath) => { + const file = fs.createWriteStream(partPath, { flags: 'wx' }) + file.on('drain', keepAlive) + return pipeline(body, file) + }) + } finally { + clearTimeout(timer) + controller.abort() + } +} + +async function cloudSaveTo(installationId: string, assetId: string): Promise { + const accessToken = await cloudAccessToken(installationId) + if (!accessToken) return null + return async (filePath) => { + const url = await resolveCloudContentUrl( + assetId, + accessToken, + 'attachment', + AbortSignal.timeout(REQUEST_TIMEOUT_MS) + ) + if (!url) throw new Error('The asset has no download link.') + await saveDownload(url, filePath) + } +} + +async function localSaveTo(assetId: string): Promise { + const file = await outputFilePath(assetId) + if (!file) return null + return (filePath) => + writeThroughPartFile(filePath, (partPath) => + fs.promises.copyFile(file, partPath, fs.constants.COPYFILE_EXCL) + ) +} + +function isUsableFileName(name: string): boolean { + return name !== '' && name !== '.' && name !== '..' +} + +function safeFileName(fileName: string, assetId: string): string { + return [fileName, assetId].map((name) => path.basename(name)).find(isUsableFileName) ?? 'download' +} + +export async function downloadAsset( + win: BrowserWindow, + source: string, + assetId: string, + fileName: string +): Promise { + const saveTo = + source === LOCAL_ASSETS_SOURCE ? await localSaveTo(assetId) : await cloudSaveTo(source, assetId) + if (!saveTo) throw new Error('This asset is not available.') + const name = safeFileName(fileName, assetId) + const remembered = settings.get('lastSaveDialogDir') + const startDir = remembered && fs.existsSync(remembered) ? remembered : app.getPath('downloads') + const { canceled, filePath } = await dialog.showSaveDialog(win, { + defaultPath: path.join(startDir, name), + filters: buildSaveDialogFilters(name) + }) + if (canceled || !filePath) return false + await saveTo(filePath) + settings.set('lastSaveDialogDir', path.dirname(filePath)) + return true +} + +export function registerAssetsHandlers(): void { + ipcMain.handle(ASSETS_LIST_CHANNEL, (_event, installationId: unknown) => + typeof installationId === 'string' ? listAssets(installationId) : [] + ) + ipcMain.handle( + ASSETS_DOWNLOAD_CHANNEL, + (event, source: unknown, assetId: unknown, fileName: unknown) => { + const win = + findHostWindowByPanelSender(event.sender) ?? + BrowserWindow.fromWebContents(event.sender) ?? + BrowserWindow.getFocusedWindow() + if (!win || typeof source !== 'string' || typeof assetId !== 'string') return false + return downloadAsset(win, source, assetId, typeof fileName === 'string' ? fileName : assetId) + } + ) +} diff --git a/src/main/lib/firstUseDetection.test.ts b/src/main/lib/firstUseDetection.test.ts new file mode 100644 index 000000000..4691d840a --- /dev/null +++ b/src/main/lib/firstUseDetection.test.ts @@ -0,0 +1,27 @@ +// @vitest-environment node +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const list = vi.fn() +vi.mock('../installations', () => ({ list: () => list() })) + +import { detectFirstUseState } from './firstUseDetection' + +describe('detectFirstUseState', () => { + beforeEach(() => list.mockReset()) + + it('still shows the build picker when the only local build is the Apps build', async () => { + list.mockResolvedValue([ + { id: 'apps', sourceId: 'standalone', appsBuild: true }, + { id: 'cloud', sourceId: 'cloud' } + ]) + expect(await detectFirstUseState()).toEqual({ skipPick: false, hasLegacyDesktop: false }) + }) + + it('skips the picker once the person has a build of their own', async () => { + list.mockResolvedValue([ + { id: 'apps', sourceId: 'standalone', appsBuild: true }, + { id: 'mine', sourceId: 'standalone' } + ]) + expect(await detectFirstUseState()).toEqual({ skipPick: true, hasLegacyDesktop: false }) + }) +}) diff --git a/src/main/lib/firstUseDetection.ts b/src/main/lib/firstUseDetection.ts index 6f91740cb..6c5c6315b 100644 --- a/src/main/lib/firstUseDetection.ts +++ b/src/main/lib/firstUseDetection.ts @@ -1,4 +1,5 @@ import * as installations from '../installations' +import { isAppsBuild } from '../../shared/appsBuild' export interface FirstUseDetection { skipPick: boolean @@ -14,7 +15,7 @@ export async function detectFirstUseState(): Promise { hasLegacyDesktop = true continue } - if (inst.sourceId === 'cloud') continue + if (inst.sourceId === 'cloud' || isAppsBuild(inst)) continue skipPick = true } return { skipPick, hasLegacyDesktop } diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index 092fc132d..cacb24984 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -35,6 +35,7 @@ import { registerLogsHandlers } from './registerLogsHandlers' import { registerCrashHandlers } from './registerCrashHandlers' import { registerTelemetryHandlers } from './registerTelemetryHandlers' import { registerDevPlatformHandlers } from './registerDevPlatformHandlers' +import { registerAssetsHandlers } from '../assets' import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandlers' import { registerCatalogHandlers } from './registerCatalogHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' @@ -279,6 +280,7 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerCrashHandlers() registerTelemetryHandlers() registerDevPlatformHandlers() + registerAssetsHandlers() registerEmbeddedSessionHandlers() registerCatalogHandlers() return startupRecovery.then(() => undefined) diff --git a/src/main/lib/ipc/registerInstallationHandlers.ts b/src/main/lib/ipc/registerInstallationHandlers.ts index 132468ab3..8cc5dda11 100644 --- a/src/main/lib/ipc/registerInstallationHandlers.ts +++ b/src/main/lib/ipc/registerInstallationHandlers.ts @@ -31,6 +31,7 @@ import { COMFYUI_REPO, snapshotRestoreFailureResult } from './shared' +import type { WebContents } from 'electron' import type { ComfyVersion, ComfyArgDef, InstallationRecord } from './shared' import { allocateInstallIdentity } from './installIdentity' import * as releaseCache from '../release-cache' @@ -138,6 +139,224 @@ export function enrichInstallationsForRenderer(allInstalls: InstallationRecord[] return { visible, enriched } } +export async function addInstallation(data: Record) { + const identity = await allocateInstallIdentity( + (data.name as string) || DEFAULT_INSTALL_NAME, + (data.installPath as string | undefined) || undefined + ) + if (!identity.ok) return identity + data.name = identity.name + if (identity.installPath) data.installPath = identity.installPath + const entry = await installations.add({ + ...data, + status: data.status ?? 'installing', + seen: false + }) + return { ok: true, entry } +} + +export async function installInstance( + sender: WebContents, + installationId: string, + express?: boolean +) { + const inst = await installations.get(installationId) + if (!inst) return { ok: false, message: 'Installation not found.' } + const source = sourceMap[inst.sourceId] + if (!source) return { ok: false, message: i18n.t('errors.unknownSource') } + if (_operationAborts.has(installationId)) { + return { ok: false, message: 'Another operation is already running for this installation.' } + } + + const sendProgress = (phase: string, detail: Record): void => { + if (!sender.isDestroyed()) { + sender.send('install-progress', { installationId, phase, ...detail }) + } + } + + const priorComfyVersion = inst.comfyVersion as ComfyVersion | undefined + const isComfyUpdate = priorComfyVersion != null + + if (source.install) { + if (!isComfyUpdate) { + mainTelemetry.registerPersonPropertiesOnce({ + first_local_install_dispatched_at: new Date().toISOString() + }) + } + fs.mkdirSync(inst.installPath, { recursive: true }) + fs.writeFileSync(path.join(inst.installPath, MARKER_FILE), installationId) + if (source.installSteps) { + let steps = [...source.installSteps] + if (!inst.autoUpdateComfyUI) { + steps = steps.filter((s) => s.phase !== 'update') + } + if (inst.pendingSnapshotRestore) { + steps.push( + { phase: 'restore-nodes', label: i18n.t('standalone.snapshotRestoreNodesPhase') }, + { phase: 'restore-pip', label: i18n.t('standalone.snapshotRestorePipPhase') } + ) + } + sendProgress('steps', { steps }) + } + const abort = new AbortController() + _operationAborts.set(installationId, abort) + + invalidateModelDownloadStartupPass() + + let snapshotRestoreError: string | null = null + + if (inst.bundledTemplateId && inst.downloadTemplateModels && inst.pendingTemplateOpen) { + const sendTemplateOutput = (text: string): void => { + try { + if (!sender.isDestroyed()) sender.send('comfy-output', { installationId, text }) + } catch {} + appendLog(installationId, text) + } + const sizeBytes = inst.bundledTemplateSizeBytes ?? 0 + startTemplateDownload(inst, sizeBytes, { sendOutput: sendTemplateOutput }) + } + + const cache = createCache( + settings.get('cacheDir') as string, + settings.get('maxCachedDownloads') as number + ) + try { + await source.install(inst, { sendProgress, download, cache, extract, signal: abort.signal }) + if (source.postInstall) { + const update = (data: Record): Promise => + installations.update(installationId, data).then(() => {}) + await source.postInstall(inst, { sendProgress, update, signal: abort.signal }) + } + + const freshInst = await installations.get(installationId) + const pendingFile = freshInst?.pendingSnapshotRestore as string | undefined + if (freshInst && pendingFile) { + const sendOutput = (text: string): void => { + try { + if (!sender.isDestroyed()) sender.send('comfy-output', { installationId, text }) + } catch {} + appendLog(installationId, text) + } + const update = (data: Record): Promise => + installations.update(installationId, data).then(() => {}) + try { + await restoreSnapshotIntoInstallation( + freshInst, + pendingFile, + true, + { sendProgress, sendOutput, signal: abort.signal }, + update + ) + } catch (err) { + if (abort.signal.aborted) throw err + snapshotRestoreError = (err as Error).message + await installations.update(installationId, { pendingSnapshotRestore: undefined }) + await fs.promises.unlink(pendingFile).catch(() => {}) + } + } + + if (!snapshotRestoreError) sendProgress('done', { percent: 100, status: 'Complete' }) + } catch (err) { + _operationAborts.delete(installationId) + abortTemplateDownload(installationId) + abortModelStaging(installationId) + if (abort.signal.aborted) { + if (isComfyUpdate) { + mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { + installation_id: installationId, + from_version: formatComfyVersion(priorComfyVersion, 'short'), + to_version: null, + result: 'cancelled' + }) + } + let cleaned = !fs.existsSync(inst.installPath) + if (!cleaned) { + try { + fs.rmSync(inst.installPath, { recursive: true, force: true }) + cleaned = true + } catch {} + } + if (cleaned) { + await installations.remove(installationId) + return { ok: true, navigate: 'list' } + } + const markerPath = path.join(inst.installPath, MARKER_FILE) + try { + fs.writeFileSync(markerPath, installationId) + } catch {} + await installations.update(installationId, { status: 'partial-delete' }) + const deleteAbort = new AbortController() + _operationAborts.set(installationId, deleteAbort) + sendProgress('delete', { percent: 0, status: 'Counting files…' }) + try { + await deleteDir( + inst.installPath, + (p) => { + sendProgress('delete', { percent: p.percent, status: formatDeleteStatus(p) }) + }, + { signal: deleteAbort.signal } + ) + _operationAborts.delete(installationId) + await installations.remove(installationId) + } catch (_delErr) { + _operationAborts.delete(installationId) + if (deleteAbort.signal.aborted) { + if (isEffectivelyEmptyInstallDir(inst.installPath)) { + try { + fs.rmSync(inst.installPath, { recursive: true, force: true }) + } catch {} + await installations.remove(installationId) + } else { + try { + fs.writeFileSync(markerPath, installationId) + } catch {} + await installations.update(installationId, { status: 'partial-delete' }) + } + } + } + return { ok: true, navigate: 'list' } + } + await installations.update(installationId, { status: 'failed' }) + if (isComfyUpdate) { + mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { + installation_id: installationId, + from_version: formatComfyVersion(priorComfyVersion, 'short'), + to_version: null, + result: 'error', + ...buildErrorFields(err) + }) + } + return { ok: false, message: (err as Error).message } + } + _operationAborts.delete(installationId) + await installations.update(installationId, { status: 'installed' }) + await syncOemSeedBestEffort() + if (isComfyUpdate) { + const freshInst = await installations.get(installationId) + const newComfyVersion = freshInst?.comfyVersion as ComfyVersion | undefined + mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { + installation_id: installationId, + from_version: formatComfyVersion(priorComfyVersion, 'short'), + to_version: newComfyVersion ? formatComfyVersion(newComfyVersion, 'short') : null, + result: 'success' + }) + } else { + mainTelemetry.captureInstallCompleted({ + installationId, + method: express ? 'express' : 'manual', + express: !!express + }) + } + if (snapshotRestoreError) { + return snapshotRestoreFailureResult(installationId, snapshotRestoreError) + } + return { ok: true } + } + + await installations.update(installationId, { status: 'failed' }) + return { ok: false, message: 'This source does not support installation.' } +} + export function registerInstallationHandlers(): void { ipcMain.handle('get-installations', async () => { const allInstalls = await installations.list() @@ -165,21 +384,9 @@ export function registerInstallationHandlers(): void { } }) - ipcMain.handle('add-installation', async (_event, data: Record) => { - const identity = await allocateInstallIdentity( - (data.name as string) || DEFAULT_INSTALL_NAME, - (data.installPath as string | undefined) || undefined - ) - if (!identity.ok) return identity - data.name = identity.name - if (identity.installPath) data.installPath = identity.installPath - const entry = await installations.add({ - ...data, - status: data.status ?? 'installing', - seen: false - }) - return { ok: true, entry } - }) + ipcMain.handle('add-installation', (_event, data: Record) => + addInstallation(data) + ) ipcMain.handle('reorder-installations', async (_event, orderedIds: string[]) => { await installations.reorder(orderedIds) @@ -216,204 +423,9 @@ export function registerInstallationHandlers(): void { return { ok: true, entry } }) - ipcMain.handle('install-instance', async (_event, installationId: string, express?: boolean) => { - const inst = await installations.get(installationId) - if (!inst) return { ok: false, message: 'Installation not found.' } - const source = sourceMap[inst.sourceId] - if (!source) return { ok: false, message: i18n.t('errors.unknownSource') } - if (_operationAborts.has(installationId)) { - return { ok: false, message: 'Another operation is already running for this installation.' } - } - const sender = _event.sender - - const sendProgress = (phase: string, detail: Record): void => { - if (!sender.isDestroyed()) { - sender.send('install-progress', { installationId, phase, ...detail }) - } - } - - const priorComfyVersion = inst.comfyVersion as ComfyVersion | undefined - const isComfyUpdate = priorComfyVersion != null - - if (source.install) { - if (!isComfyUpdate) { - mainTelemetry.registerPersonPropertiesOnce({ - first_local_install_dispatched_at: new Date().toISOString() - }) - } - fs.mkdirSync(inst.installPath, { recursive: true }) - fs.writeFileSync(path.join(inst.installPath, MARKER_FILE), installationId) - if (source.installSteps) { - let steps = [...source.installSteps] - if (!inst.autoUpdateComfyUI) { - steps = steps.filter((s) => s.phase !== 'update') - } - if (inst.pendingSnapshotRestore) { - steps.push( - { phase: 'restore-nodes', label: i18n.t('standalone.snapshotRestoreNodesPhase') }, - { phase: 'restore-pip', label: i18n.t('standalone.snapshotRestorePipPhase') } - ) - } - sendProgress('steps', { steps }) - } - const abort = new AbortController() - _operationAborts.set(installationId, abort) - - invalidateModelDownloadStartupPass() - - let snapshotRestoreError: string | null = null - - if (inst.bundledTemplateId && inst.downloadTemplateModels && inst.pendingTemplateOpen) { - const sendTemplateOutput = (text: string): void => { - try { - if (!sender.isDestroyed()) sender.send('comfy-output', { installationId, text }) - } catch {} - appendLog(installationId, text) - } - const sizeBytes = inst.bundledTemplateSizeBytes ?? 0 - startTemplateDownload(inst, sizeBytes, { sendOutput: sendTemplateOutput }) - } - - const cache = createCache( - settings.get('cacheDir') as string, - settings.get('maxCachedDownloads') as number - ) - try { - await source.install(inst, { sendProgress, download, cache, extract, signal: abort.signal }) - if (source.postInstall) { - const update = (data: Record): Promise => - installations.update(installationId, data).then(() => {}) - await source.postInstall(inst, { sendProgress, update, signal: abort.signal }) - } - - const freshInst = await installations.get(installationId) - const pendingFile = freshInst?.pendingSnapshotRestore as string | undefined - if (freshInst && pendingFile) { - const sendOutput = (text: string): void => { - try { - if (!sender.isDestroyed()) sender.send('comfy-output', { installationId, text }) - } catch {} - appendLog(installationId, text) - } - const update = (data: Record): Promise => - installations.update(installationId, data).then(() => {}) - try { - await restoreSnapshotIntoInstallation( - freshInst, - pendingFile, - true, - { sendProgress, sendOutput, signal: abort.signal }, - update - ) - } catch (err) { - if (abort.signal.aborted) throw err - snapshotRestoreError = (err as Error).message - await installations.update(installationId, { pendingSnapshotRestore: undefined }) - await fs.promises.unlink(pendingFile).catch(() => {}) - } - } - - if (!snapshotRestoreError) sendProgress('done', { percent: 100, status: 'Complete' }) - } catch (err) { - _operationAborts.delete(installationId) - abortTemplateDownload(installationId) - abortModelStaging(installationId) - if (abort.signal.aborted) { - if (isComfyUpdate) { - mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { - installation_id: installationId, - from_version: formatComfyVersion(priorComfyVersion, 'short'), - to_version: null, - result: 'cancelled' - }) - } - let cleaned = !fs.existsSync(inst.installPath) - if (!cleaned) { - try { - fs.rmSync(inst.installPath, { recursive: true, force: true }) - cleaned = true - } catch {} - } - if (cleaned) { - await installations.remove(installationId) - return { ok: true, navigate: 'list' } - } - const markerPath = path.join(inst.installPath, MARKER_FILE) - try { - fs.writeFileSync(markerPath, installationId) - } catch {} - await installations.update(installationId, { status: 'partial-delete' }) - const deleteAbort = new AbortController() - _operationAborts.set(installationId, deleteAbort) - sendProgress('delete', { percent: 0, status: 'Counting files…' }) - try { - await deleteDir( - inst.installPath, - (p) => { - sendProgress('delete', { percent: p.percent, status: formatDeleteStatus(p) }) - }, - { signal: deleteAbort.signal } - ) - _operationAborts.delete(installationId) - await installations.remove(installationId) - } catch (_delErr) { - _operationAborts.delete(installationId) - if (deleteAbort.signal.aborted) { - if (isEffectivelyEmptyInstallDir(inst.installPath)) { - try { - fs.rmSync(inst.installPath, { recursive: true, force: true }) - } catch {} - await installations.remove(installationId) - } else { - try { - fs.writeFileSync(markerPath, installationId) - } catch {} - await installations.update(installationId, { status: 'partial-delete' }) - } - } - } - return { ok: true, navigate: 'list' } - } - await installations.update(installationId, { status: 'failed' }) - if (isComfyUpdate) { - mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { - installation_id: installationId, - from_version: formatComfyVersion(priorComfyVersion, 'short'), - to_version: null, - result: 'error', - ...buildErrorFields(err) - }) - } - return { ok: false, message: (err as Error).message } - } - _operationAborts.delete(installationId) - await installations.update(installationId, { status: 'installed' }) - await syncOemSeedBestEffort() - if (isComfyUpdate) { - const freshInst = await installations.get(installationId) - const newComfyVersion = freshInst?.comfyVersion as ComfyVersion | undefined - mainTelemetry.emit('comfy.desktop.comfyui.update.applied', { - installation_id: installationId, - from_version: formatComfyVersion(priorComfyVersion, 'short'), - to_version: newComfyVersion ? formatComfyVersion(newComfyVersion, 'short') : null, - result: 'success' - }) - } else { - mainTelemetry.captureInstallCompleted({ - installationId, - method: express ? 'express' : 'manual', - express: !!express - }) - } - if (snapshotRestoreError) { - return snapshotRestoreFailureResult(installationId, snapshotRestoreError) - } - return { ok: true } - } - - await installations.update(installationId, { status: 'failed' }) - return { ok: false, message: 'This source does not support installation.' } - }) + ipcMain.handle('install-instance', (_event, installationId: string, express?: boolean) => + installInstance(_event.sender, installationId, express) + ) ipcMain.handle('get-list-actions', async (_event, installationId: string) => { const inst = await installations.get(installationId) diff --git a/src/main/lib/ipc/sessionActions/launch.ts b/src/main/lib/ipc/sessionActions/launch.ts index ec4028df4..f400ad996 100644 --- a/src/main/lib/ipc/sessionActions/launch.ts +++ b/src/main/lib/ipc/sessionActions/launch.ts @@ -167,6 +167,7 @@ import { writeCampaignRecord } from '../../coreBetaCampaignFlags' import { getIdClass } from '../../deviceId' +import { outputDirOverride } from '../../outputFolders' import type { ComfyArgsSchema } from '../../comfy-args' export function desktopFeatureFlags( @@ -512,7 +513,6 @@ export function applyStorageLaunchArgs( } if (argsAvailable) { const useSharedInput = (inst.useSharedInput as boolean | undefined) !== false - const useSharedOutput = (inst.useSharedOutput as boolean | undefined) !== false if (useSharedInput) { const inputDir = storage.inputDir || settings.defaults.inputDir fs.mkdirSync(inputDir, { recursive: true }) @@ -524,16 +524,10 @@ export function applyStorageLaunchArgs( launchCmd.args!.push('--input-directory', perInstallInput) } } - if (useSharedOutput) { - const outputDir = storage.outputDir || settings.defaults.outputDir + const outputDir = outputDirOverride(inst, storage.outputDir || settings.defaults.outputDir) + if (outputDir) { fs.mkdirSync(outputDir, { recursive: true }) launchCmd.args!.push('--output-directory', outputDir) - } else { - const perInstallOutput = inst.outputDir as string | undefined - if (perInstallOutput) { - fs.mkdirSync(perInstallOutput, { recursive: true }) - launchCmd.args!.push('--output-directory', perInstallOutput) - } } } diff --git a/src/main/lib/outputFolders.test.ts b/src/main/lib/outputFolders.test.ts new file mode 100644 index 000000000..a793cc7f0 --- /dev/null +++ b/src/main/lib/outputFolders.test.ts @@ -0,0 +1,334 @@ +import fs from 'node:fs' +import path from 'node:path' +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' + +const { + root, + sharedDir, + ownDir, + installDir, + outsideDir, + installs, + fetchFile, + thumbnailImage, + fullImage +} = await vi.hoisted(async () => { + const nodeFs = await import('node:fs') + const nodeOs = await import('node:os') + const nodePath = await import('node:path') + const base = nodeFs.realpathSync( + nodeFs.mkdtempSync(nodePath.join(nodeOs.tmpdir(), 'output-folders-')) + ) + return { + root: base, + sharedDir: nodePath.join(base, 'shared-output'), + ownDir: nodePath.join(base, 'own-output'), + installDir: nodePath.join(base, 'install'), + outsideDir: nodePath.join(base, 'outside'), + installs: vi.fn(), + fetchFile: vi.fn(async (url: string) => new Response(`file ${url}`)), + thumbnailImage: vi.fn(), + fullImage: vi.fn() + } +}) + +vi.mock('electron', () => ({ + ipcMain: { handle: vi.fn() }, + protocol: { handle: vi.fn(), registerSchemesAsPrivileged: vi.fn() }, + net: { fetch: (url: string) => fetchFile(url) }, + nativeImage: { + createThumbnailFromPath: (file: string) => thumbnailImage(file), + createFromPath: (file: string) => fullImage(file) + } +})) +vi.mock('../installations', () => ({ list: () => installs() })) +vi.mock('../settings', () => ({ + get: (key: string) => (key === 'outputDir' ? sharedDir : undefined), + defaults: { outputDir: path.join(root, 'default-output') } +})) +vi.mock('./ipc/shared', () => ({ + sourceMap: { + standalone: { category: 'local', getLaunchCommand: () => ({ cmd: 'python' }) }, + desktop: { + category: 'local', + getLaunchCommand: () => ({ cmd: 'open', skipSharedPaths: true }) + }, + cloud: { category: 'cloud' } + } +})) +vi.mock('./models', () => ({ + installOutputDir: (installPath: string) => path.join(installPath, 'ComfyUI', 'output') +})) +vi.mock('./assets', () => ({ + assetKind: (name: string) => + ({ png: 'image', mp4: 'video', wav: 'audio', glb: '3d' })[name.split('.').pop() ?? ''] ?? null +})) + +import type { InstallationRecord } from '../installations' +import { + MAX_FOLDER_DEPTH, + MAX_LOCAL_ASSETS, + THUMBNAIL_WAIT_MS, + listLocalAssets, + listOutputFolders, + outputDirOverride, + outputFilePath, + outputFileUrl, + serveOutputFile +} from './outputFolders' + +function install(name: string, extra: Record = {}): InstallationRecord { + return { + id: name, + name, + sourceId: 'standalone', + installPath: installDir, + createdAt: '2026-11-01T00:00:00Z', + ...extra + } +} + +function write(file: string, body = 'x', modified = new Date('2026-11-01T00:00:00Z')): void { + fs.mkdirSync(path.dirname(file), { recursive: true }) + fs.writeFileSync(file, body) + fs.utimesSync(file, modified, modified) +} + +beforeEach(() => { + vi.clearAllMocks() + thumbnailImage.mockReset() + fullImage.mockReset() + for (const dir of [sharedDir, ownDir, outsideDir, installDir]) + fs.rmSync(dir, { recursive: true, force: true }) + for (const dir of [sharedDir, ownDir, outsideDir, path.join(installDir, 'ComfyUI', 'output')]) + fs.mkdirSync(dir, { recursive: true }) + installs.mockResolvedValue([ + install('Test Instance'), + install('My Local 1'), + install('Apps', { useSharedOutput: false, outputDir: ownDir }), + install('Bare', { useSharedOutput: false }), + install('Cloud', { sourceId: 'cloud' }), + install('Old Desktop', { sourceId: 'desktop' }) + ]) +}) + +afterAll(() => fs.rmSync(root, { recursive: true, force: true })) + +describe('outputDirOverride', () => { + it('uses the shared folder unless the build turned sharing off', () => { + expect(outputDirOverride(install('a'), sharedDir)).toBe(sharedDir) + expect( + outputDirOverride(install('a', { useSharedOutput: false, outputDir: ownDir }), sharedDir) + ).toBe(ownDir) + expect(outputDirOverride(install('a', { useSharedOutput: false }), sharedDir)).toBeNull() + }) +}) + +describe('listOutputFolders', () => { + it('lists each real output folder once, leaving out Cloud and builds that manage their own paths', async () => { + expect(await listOutputFolders()).toEqual([ + sharedDir, + ownDir, + path.join(installDir, 'ComfyUI', 'output') + ]) + }) + + it('leaves out folders that do not exist yet', async () => { + installs.mockResolvedValue([ + install('Gone', { useSharedOutput: false, outputDir: path.join(root, 'missing') }) + ]) + expect(await listOutputFolders()).toEqual([sharedDir]) + }) +}) + +describe('listLocalAssets', () => { + it('lists media from every output folder and its subfolders, newest first', async () => { + write(path.join(sharedDir, 'old.png'), 'x', new Date('2026-10-01T00:00:00Z')) + write(path.join(sharedDir, 'video', 'new.mp4'), 'xx', new Date('2026-11-05T00:00:00Z')) + write(path.join(ownDir, 'voice.wav'), 'xxx', new Date('2026-11-03T00:00:00Z')) + write( + path.join(installDir, 'ComfyUI', 'output', 'mesh.glb'), + 'x', + new Date('2026-11-02T00:00:00Z') + ) + write(path.join(sharedDir, 'notes.txt')) + write(path.join(sharedDir, '.hidden.png')) + write(path.join(sharedDir, '.cache', 'inside-hidden.png')) + + const assets = await listLocalAssets() + + expect(assets.map((a) => a.name)).toEqual(['new.mp4', 'voice.wav', 'mesh.glb', 'old.png']) + const newVideo = path.join(sharedDir, 'video', 'new.mp4') + expect(assets[0]).toEqual({ + id: newVideo, + name: 'new.mp4', + kind: 'video', + sizeBytes: 2, + createdAt: '2026-11-05T00:00:00.000Z', + mimeType: null, + tags: ['output'], + previewUrl: outputFileUrl(newVideo), + thumbnailUrl: null + }) + const oldPicture = path.join(sharedDir, 'old.png') + expect(assets[3]!.thumbnailUrl, 'an image thumbnail link changes when the file does').toBe( + outputFileUrl(oldPicture, fs.statSync(oldPicture).mtimeMs) + ) + expect(assets[2]!.previewUrl, '3D files have no preview').toBeNull() + }) + + it('finds a file once when two builds save to the same folder', async () => { + write(path.join(sharedDir, 'shared.png')) + + expect((await listLocalAssets()).map((a) => a.name)).toEqual(['shared.png']) + }) + + it('keeps only the newest files when the folders are very large', async () => { + for (let i = 0; i <= MAX_LOCAL_ASSETS; i++) + write(path.join(sharedDir, `f${i}.png`), '', new Date(Date.UTC(2026, 0, 1, 0, 0, i))) + + const assets = await listLocalAssets() + + expect(assets).toHaveLength(MAX_LOCAL_ASSETS) + expect(assets.map((a) => a.name)).not.toContain('f0.png') + }) + + it('does not follow links out of an output folder or walk without end', async () => { + write(path.join(outsideDir, 'secret.png')) + fs.symlinkSync(outsideDir, path.join(sharedDir, 'escape')) + const deep = path.join( + sharedDir, + ...Array.from({ length: MAX_FOLDER_DEPTH + 1 }, (_, i) => `d${i}`) + ) + write(path.join(deep, 'too-deep.png')) + + expect(await listLocalAssets()).toEqual([]) + }) +}) + +describe('outputFilePath', () => { + it('finds a file inside an output folder and refuses folders and files outside', async () => { + const inside = path.join(ownDir, 'nested', 'still.png') + const outside = path.join(outsideDir, 'secret.png') + write(inside) + write(outside) + + expect(await outputFilePath(inside)).toBe(inside) + expect(await outputFilePath(path.join(ownDir, 'nested'))).toBeNull() + expect(await outputFilePath(outside)).toBeNull() + expect(await outputFilePath(path.join(ownDir, '..', 'outside', 'secret.png'))).toBeNull() + expect(await outputFilePath('still.png')).toBeNull() + }) +}) + +describe('serveOutputFile', () => { + it('serves the file itself when the OS thumbnail is slow, then the thumbnail once it is made', async () => { + write(path.join(sharedDir, 'slow.png')) + await listOutputFolders() + let finishThumbnail: (image: unknown) => void = () => {} + thumbnailImage.mockReturnValueOnce(new Promise((resolve) => (finishThumbnail = resolve))) + const url = outputFileUrl(path.join(sharedDir, 'slow.png'), 1) + const platform = process.platform + Object.defineProperty(process, 'platform', { value: 'darwin' }) + try { + const startedAt = Date.now() + const slow = await serveOutputFile(new Request(url)) + + expect( + Date.now() - startedAt, + 'it waits for the thumbnail before giving up' + ).toBeGreaterThanOrEqual(THUMBNAIL_WAIT_MS - 50) + expect(await slow.text()).toMatch(/^file file:\/\/.*slow\.png$/) + + finishThumbnail({ + isEmpty: () => false, + getSize: () => ({ width: 100, height: 100 }), + toJPEG: () => Buffer.from('late') + }) + await vi.waitFor(async () => { + const later = await serveOutputFile(new Request(url)) + expect(later.headers.get('Content-Type')).toBe('image/jpeg') + expect(await later.text()).toBe('late') + }) + expect(thumbnailImage, 'the slow thumbnail is made once, not again').toHaveBeenCalledOnce() + } finally { + Object.defineProperty(process, 'platform', { value: platform }) + } + }) + + it('serves a file inside an output folder and refuses one outside', async () => { + write(path.join(sharedDir, 'picture.png')) + write(path.join(outsideDir, 'secret.png')) + await listOutputFolders() + + const ok = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'picture.png'))) + ) + expect(ok.status).toBe(200) + expect(fetchFile).toHaveBeenCalledWith(expect.stringMatching(/^file:\/\/.*picture\.png$/)) + + expect(ok.headers.get('X-Content-Type-Options')).toBe('nosniff') + expect(ok.headers.get('Content-Security-Policy')).toBe('sandbox') + + const refused = await serveOutputFile( + new Request(outputFileUrl(path.join(outsideDir, 'secret.png'))) + ) + expect(refused.status).toBe(404) + expect(fetchFile).toHaveBeenCalledOnce() + }) + + it('answers 404 for files it does not show, broken addresses and files that are gone', async () => { + write(path.join(sharedDir, 'page.html')) + write(path.join(sharedDir, 'gone.png')) + await listOutputFolders() + + const html = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'page.html'))) + ) + expect(html.status).toBe(404) + const broken = await serveOutputFile(new Request('comfy-output://file/%')) + expect(broken.status).toBe(404) + fetchFile.mockRejectedValueOnce(new Error('ENOENT')) + const gone = await serveOutputFile(new Request(outputFileUrl(path.join(sharedDir, 'gone.png')))) + expect(gone.status).toBe(404) + }) + + it('serves a small JPEG thumbnail, or the file itself when none can be made', async () => { + write(path.join(sharedDir, 'thumb.png')) + await listOutputFolders() + const resize = vi.fn(() => ({ toJPEG: () => Buffer.from('small') })) + const big = { isEmpty: () => false, getSize: () => ({ width: 2000, height: 1000 }), resize } + thumbnailImage.mockResolvedValueOnce(big) + fullImage.mockReturnValueOnce(big) + + const thumb = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'thumb.png'), 1)) + ) + expect(thumb.headers.get('Content-Type')).toBe('image/jpeg') + expect(await thumb.text()).toBe('small') + expect(resize).toHaveBeenCalledWith({ width: 480, height: 240 }) + expect(thumb.headers.get('X-Content-Type-Options')).toBe('nosniff') + + write(path.join(sharedDir, 'blank.png')) + const blank = { + isEmpty: () => false, + getSize: () => ({ width: 10, height: 10 }), + toJPEG: () => Buffer.alloc(0) + } + thumbnailImage.mockResolvedValueOnce(blank) + fullImage.mockReturnValueOnce(blank) + const empty = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'blank.png'), 1)) + ) + expect(empty.headers.get('Content-Type')).not.toBe('image/jpeg') + expect(await empty.text()).toMatch(/^file file:\/\/.*blank\.png$/) + + write(path.join(sharedDir, 'broken.png')) + thumbnailImage.mockRejectedValueOnce(new Error('no thumbnail')) + fullImage.mockReturnValueOnce({ isEmpty: () => true }) + const none = await serveOutputFile( + new Request(outputFileUrl(path.join(sharedDir, 'broken.png'), 1)) + ) + expect(await none.text()).toMatch(/^file file:\/\/.*broken\.png$/) + }) +}) diff --git a/src/main/lib/outputFolders.ts b/src/main/lib/outputFolders.ts new file mode 100644 index 000000000..bd956a0c9 --- /dev/null +++ b/src/main/lib/outputFolders.ts @@ -0,0 +1,272 @@ +import fsp from 'fs/promises' +import type { Dirent } from 'fs' +import path from 'path' +import { pathToFileURL } from 'url' +import { ipcMain, nativeImage, net, protocol } from 'electron' +import * as installations from '../installations' +import type { InstallationRecord } from '../installations' +import * as settings from '../settings' +import { assetKind } from './assets' +import { runPool } from '../sources/standalone/templateDownloadCore' +import { installOutputDir } from './models' +import { sourceMap } from './ipc/shared' +import type { AssetSummary } from '../../types/ipc' + +export const OUTPUT_FILES_SCHEME = 'comfy-output' + +const LIST_LOCAL_CHANNEL = 'assets:list-local' +export const MAX_LOCAL_ASSETS = 500 +export const MAX_FOLDER_DEPTH = 6 +const THUMBNAIL_SIZE = 480 +const MAX_CACHED_THUMBNAILS = 400 +const THUMBNAILS_AT_ONCE = 2 +export const THUMBNAIL_WAIT_MS = 1500 +const STATS_AT_ONCE = 32 +const ROOTS_FRESH_MS = 5000 +const MAX_LINUX_THUMBNAIL_SOURCE_BYTES = 40 * 1024 * 1024 +const SERVED_KINDS = new Set(['image', 'video', 'audio']) + +const thumbnails = new Map() +const thumbnailsInFlight = new Map>() +const thumbnailQueue: (() => void)[] = [] +let thumbnailsRunning = 0 +let allowedRoots: string[] = [] +let allowedRootsAt = 0 + +export function sharedOutputDir(): string { + return (settings.get('outputDir') as string | undefined) || settings.defaults.outputDir +} + +export function outputDirOverride(inst: InstallationRecord, sharedDir: string): string | null { + if ((inst.useSharedOutput as boolean | undefined) !== false) return sharedDir + return (inst.outputDir as string | undefined) || null +} + +function outputDirFor(inst: InstallationRecord, sharedDir: string): string | null { + const source = sourceMap[inst.sourceId] + if (source?.category !== 'local' || source.getLaunchCommand(inst)?.skipSharedPaths) return null + return outputDirOverride(inst, sharedDir) ?? installOutputDir(inst.installPath) +} + +async function realDir(dir: string): Promise { + const real = await fsp.realpath(dir).catch(() => null) + if (!real) return null + const stat = await fsp.stat(real).catch(() => null) + return stat?.isDirectory() ? real : null +} + +export async function listOutputFolders(): Promise { + const shared = sharedOutputDir() + const folders = new Set() + const sharedReal = await realDir(shared) + if (sharedReal) folders.add(sharedReal) + for (const inst of await installations.list()) { + const dir = outputDirFor(inst, shared) + const real = dir ? await realDir(dir) : null + if (real) folders.add(real) + } + allowedRoots = [...folders] + allowedRootsAt = Date.now() + return allowedRoots +} + +function isInsideRoot(target: string, root: string): boolean { + const relative = path.relative(root, target) + return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative)) +} + +async function allowedPath(target: string): Promise { + if (!path.isAbsolute(target)) return null + if (Date.now() - allowedRootsAt > ROOTS_FRESH_MS) await listOutputFolders() + const real = await fsp.realpath(target).catch(() => null) + if (!real) return null + return allowedRoots.some((root) => isInsideRoot(real, root)) ? real : null +} + +export async function outputFilePath(target: string): Promise { + const file = await allowedPath(target) + if (!file) return null + const stat = await fsp.stat(file).catch(() => null) + return stat?.isFile() ? file : null +} + +export function outputFileUrl(file: string, thumbnailOf?: number): string { + const url = `${OUTPUT_FILES_SCHEME}://file/${encodeURIComponent(file)}` + return thumbnailOf === undefined ? url : `${url}?thumbnail=${Math.round(thumbnailOf)}` +} + +async function collectMediaFiles(root: string): Promise { + const files: string[] = [] + const walk = async (dir: string, depth: number): Promise => { + const dirents: Dirent[] = await fsp.readdir(dir, { withFileTypes: true }).catch(() => []) + const subfolders: string[] = [] + for (const dirent of dirents) { + if (dirent.name.startsWith('.')) continue + const full = path.join(dir, dirent.name) + if (dirent.isDirectory()) subfolders.push(full) + else if (dirent.isFile() && assetKind(dirent.name) !== null) files.push(full) + } + if (depth < MAX_FOLDER_DEPTH) { + for (const sub of subfolders) await walk(sub, depth + 1) + } + } + await walk(root, 0) + return files +} + +function localSummary(file: string, sizeBytes: number, modifiedMs: number): AssetSummary { + const kind = assetKind(file) + return { + id: file, + name: path.basename(file), + kind, + sizeBytes, + createdAt: new Date(modifiedMs).toISOString(), + mimeType: null, + tags: ['output'], + previewUrl: kind && SERVED_KINDS.has(kind) ? outputFileUrl(file) : null, + thumbnailUrl: kind === 'image' ? outputFileUrl(file, modifiedMs) : null + } +} + +export async function listLocalAssets(): Promise { + const folders = await listOutputFolders() + const files = [...new Set((await Promise.all(folders.map(collectMediaFiles))).flat())] + const found: AssetSummary[] = [] + await runPool(files, STATS_AT_ONCE, async (file) => { + const stat = await fsp.stat(file).catch(() => null) + if (!stat?.isFile()) return + const modifiedMs = Number.isFinite(stat.mtimeMs) ? stat.mtimeMs : 0 + found.push(localSummary(file, stat.size, modifiedMs)) + }) + return found.sort((a, b) => b.createdAt!.localeCompare(a.createdAt!)).slice(0, MAX_LOCAL_ASSETS) +} + +function linuxThumbnailSource(file: string, sizeBytes: number): Electron.NativeImage | null { + if (assetKind(file) !== 'image' || sizeBytes > MAX_LINUX_THUMBNAIL_SOURCE_BYTES) return null + try { + return nativeImage.createFromPath(file) + } catch { + return null + } +} + +async function makeThumbnail(file: string, sizeBytes: number): Promise { + const size = { width: THUMBNAIL_SIZE, height: THUMBNAIL_SIZE } + const image = + process.platform === 'linux' + ? linuxThumbnailSource(file, sizeBytes) + : await nativeImage.createThumbnailFromPath(file, size).catch(() => null) + if (!image || image.isEmpty()) return null + const { width, height } = image.getSize() + const scale = Math.min(1, THUMBNAIL_SIZE / Math.max(width, height)) + const scaled = + scale < 1 + ? image.resize({ + width: Math.max(1, Math.round(width * scale)), + height: Math.max(1, Math.round(height * scale)) + }) + : image + const jpeg = scaled.toJPEG(82) + return jpeg.length > 0 ? jpeg : null +} + +async function fewAtATime(work: () => Promise): Promise { + while (thumbnailsRunning >= THUMBNAILS_AT_ONCE) + await new Promise((resolve) => thumbnailQueue.push(resolve)) + thumbnailsRunning++ + try { + return await work() + } finally { + thumbnailsRunning-- + thumbnailQueue.shift()?.() + } +} + +async function thumbnailFor(file: string): Promise { + const stat = await fsp.stat(file).catch(() => null) + if (!stat) return null + const key = `${file}\n${stat.mtimeMs}` + const cached = thumbnails.get(key) + if (cached) return cached + let making = thumbnailsInFlight.get(key) + if (!making) { + making = fewAtATime(() => makeThumbnail(file, stat.size)) + .then((made) => { + if (made) { + thumbnails.set(key, made) + if (thumbnails.size > MAX_CACHED_THUMBNAILS) + thumbnails.delete(thumbnails.keys().next().value!) + } + return made + }) + .catch(() => null) + .finally(() => thumbnailsInFlight.delete(key)) + thumbnailsInFlight.set(key, making) + } + let timer: ReturnType | undefined + const tooSlow = new Promise((resolve) => { + timer = setTimeout(() => resolve(null), THUMBNAIL_WAIT_MS) + }) + try { + return await Promise.race([making, tooSlow]) + } finally { + clearTimeout(timer) + } +} + +function decodedPath(url: URL): string | null { + if (url.host !== 'file') return null + try { + return decodeURIComponent(url.pathname.slice(1)) + } catch { + return null + } +} + +function notFound(): Response { + return new Response(null, { status: 404 }) +} + +async function fetchFile(file: string, request: Request): Promise { + const res = await net + .fetch(pathToFileURL(file).toString(), { headers: request.headers }) + .catch(() => null) + if (!res) return notFound() + const headers = new Headers(res.headers) + headers.set('X-Content-Type-Options', 'nosniff') + headers.set('Content-Security-Policy', 'sandbox') + return new Response(res.body, { status: res.status, statusText: res.statusText, headers }) +} + +export async function serveOutputFile(request: Request): Promise { + const url = new URL(request.url) + const target = decodedPath(url) + const kind = target ? assetKind(target) : null + const file = target && kind && SERVED_KINDS.has(kind) ? await allowedPath(target) : null + if (!file) return notFound() + if (!url.searchParams.has('thumbnail')) return fetchFile(file, request) + const jpeg = await thumbnailFor(file) + if (!jpeg) return fetchFile(file, request) + return new Response(new Uint8Array(jpeg), { + headers: { + 'Content-Type': 'image/jpeg', + 'Cache-Control': 'private, max-age=3600', + 'X-Content-Type-Options': 'nosniff' + } + }) +} + +export function registerOutputFilesScheme(): void { + protocol.registerSchemesAsPrivileged([ + { + scheme: OUTPUT_FILES_SCHEME, + privileges: { standard: true, secure: true, stream: true } + } + ]) +} + +export function registerOutputFolderHandlers(): void { + protocol.handle(OUTPUT_FILES_SCHEME, serveOutputFile) + ipcMain.handle(LIST_LOCAL_CHANNEL, () => listLocalAssets()) +} diff --git a/src/preload/api.ts b/src/preload/api.ts index 11121193b..27d6ac5d2 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -98,6 +98,24 @@ export function buildElectronApi(): ElectronApi { closeAnnouncement: () => ipcRenderer.send('comfy-window:close-announcement'), signalOverlayReady: () => ipcRenderer.send('comfy-window:overlay-ready'), navigatePanel: (page) => ipcRenderer.send('comfy-window:navigate-panel', page), + listAssets: (installationId) => ipcRenderer.invoke('assets:list', installationId), + listLocalAssets: () => ipcRenderer.invoke('assets:list-local'), + downloadAsset: (source, assetId, fileName) => + ipcRenderer.invoke('assets:download', source, assetId, fileName), + showApps: (rect) => ipcRenderer.send('comfy-apps:show', rect), + hideApps: () => ipcRenderer.send('comfy-apps:hide'), + onAppsLoadFailed: (callback) => { + const handler = (_event: IpcRendererEvent, info: unknown) => + callback(info as Parameters[0]) + ipcRenderer.on('comfy-apps:load-failed', handler) + return () => ipcRenderer.removeListener('comfy-apps:load-failed', handler) + }, + onAppsAppOpen: (callback) => { + const handler = (_event: IpcRendererEvent, info: unknown) => + callback(info as Parameters[0]) + ipcRenderer.on('comfy-apps:app-open', handler) + return () => ipcRenderer.removeListener('comfy-apps:app-open', handler) + }, listHomeWorkflows: () => ipcRenderer.invoke('catalog:list-home-workflows'), openCatalogWorkflow: (id) => ipcRenderer.invoke('catalog:open-workflow', id), openCatalogWorkflowHub: () => ipcRenderer.invoke('catalog:open-workflow-hub'), diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts new file mode 100644 index 000000000..fa281bede --- /dev/null +++ b/src/preload/appsPreload.ts @@ -0,0 +1,71 @@ +import { contextBridge, ipcRenderer } from 'electron' +import type { IpcRendererEvent } from 'electron' + +const BRIDGE = 2 +const CALLS = [ + 'getInfo', + 'getAuthState', + 'signIn', + 'run', + 'getRun', + 'getOutput', + 'uploadImage', + 'uploadFile', + 'saveFile', + 'readSaved', + 'showInFolder', + 'saveVersions', + 'readVersions', + 'getNodePrice', + 'getLocal', + 'startLocal', + 'cancelSetup', + 'localBuilds', + 'localFetch', + 'onLocalEvent', + 'localDownload', + 'cancelRun', + 'openInComfy' +] + +contextBridge.exposeInMainWorld('comfyDesktopApps', { + getInfo: () => ({ bridge: BRIDGE, calls: CALLS }), + getAuthState: () => ipcRenderer.invoke('comfy-apps:get-auth-state'), + signIn: () => ipcRenderer.invoke('comfy-apps:sign-in'), + run: (workflow: unknown) => ipcRenderer.invoke('comfy-apps:run', workflow), + getRun: (runId: string) => ipcRenderer.invoke('comfy-apps:get-run', runId), + cancelRun: (runId: string) => ipcRenderer.invoke('comfy-apps:cancel-run', runId), + openInComfy: (req: { prompt: unknown; where: 'local' | 'cloud'; name: string; build?: string }) => + ipcRenderer.invoke('comfy-apps:open-in-comfy', req), + getOutput: (file: unknown, save?: { app: string; label: string }) => + save === undefined + ? ipcRenderer.invoke('comfy-apps:get-output', file) + : ipcRenderer.invoke('comfy-apps:get-output', file, save), + uploadImage: (bytes: Uint8Array, name: string) => + ipcRenderer.invoke('comfy-apps:upload-image', bytes, name), + uploadFile: (bytes: Uint8Array, name: string) => + ipcRenderer.invoke('comfy-apps:upload-image', bytes, name), + saveFile: (bytes: Uint8Array, app: string, label: string, ext: string) => + ipcRenderer.invoke('comfy-apps:save-file', bytes, app, label, ext), + readSaved: (path: string) => ipcRenderer.invoke('comfy-apps:read-saved', path), + showInFolder: (path: string) => ipcRenderer.invoke('comfy-apps:show-in-folder', path), + saveVersions: (app: string, bytes: Uint8Array) => + ipcRenderer.invoke('comfy-apps:save-versions', app, bytes), + readVersions: (app: string) => ipcRenderer.invoke('comfy-apps:read-versions', app), + getNodePrice: (node: string) => ipcRenderer.invoke('comfy-apps:get-node-price', node), + getLocal: () => ipcRenderer.invoke('comfy-apps:get-local'), + startLocal: () => ipcRenderer.invoke('comfy-apps:start-local'), + cancelSetup: () => ipcRenderer.invoke('comfy-apps:cancel-setup'), + localBuilds: () => ipcRenderer.invoke('comfy-apps:local-builds'), + localFetch: (req: { method: string; path: string; body?: unknown; fileName?: string }) => + ipcRenderer.invoke('comfy-apps:local-fetch', req), + localDownload: (req: { url: string; folder: string; name: string }) => + ipcRenderer.invoke('comfy-apps:local-download', req), + onLocalEvent: (callback: (event: unknown) => void) => { + const listener = (_e: IpcRendererEvent, msg: unknown) => callback(msg) + ipcRenderer.on('comfy-apps:local-event', listener) + return () => { + ipcRenderer.removeListener('comfy-apps:local-event', listener) + } + } +}) diff --git a/src/renderer/csp.test.ts b/src/renderer/csp.test.ts index 9c9e0d0a1..ad4376fc7 100644 --- a/src/renderer/csp.test.ts +++ b/src/renderer/csp.test.ts @@ -47,6 +47,13 @@ describe('Content-Security-Policy: panel.html', () => { expect(csp['script-src']).toBe("'self'") }) + it('shows output files from this computer only through the output-folder protocol', () => { + expect(csp['img-src']).toContain('comfy-output:') + expect(csp['media-src']).toContain('comfy-output:') + expect(csp['img-src']).not.toContain('127.0.0.1') + expect(csp['media-src']).not.toContain('127.0.0.1') + }) + it('restricts default-src to self only', () => { expect(csp['default-src']).toBe("'self'") }) diff --git a/src/renderer/panel.html b/src/renderer/panel.html index a97228cc1..e0ee5a255 100644 --- a/src/renderer/panel.html +++ b/src/renderer/panel.html @@ -5,7 +5,7 @@ Comfy Desktop — Panel