diff --git a/e2e/apps.test.ts b/e2e/apps.test.ts new file mode 100644 index 000000000..67d3564e8 --- /dev/null +++ b/e2e/apps.test.ts @@ -0,0 +1,352 @@ +import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http' +import type { AddressInfo } from 'node:net' +import { mkdtemp, realpath, rm } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { expect, test, type ElectronApplication } from '@playwright/test' +import { launchApp, type AppContext } from './launchApp' +import { WebContentsPage } from './support/cdpPages' +import { getShellOpenExternalCalls } from './support/devHooks' +import { claimsOf, FakeCloud, reply, type BearerClaims } from './support/fakeCloud' + +const SIDEBAR_APPS = '[data-testid="app-sidebar-apps"]' +const APPS_GRID = '[data-testid="apps-grid"]' +const CUTOUT_CARD = '[data-testid="app-card-remove-background"]' +const DOWN_CARD = '[data-testid="app-card-down-for-now"]' +const APP_HOST = '[data-testid="app-page-host"]' +const SIDEBAR = '[data-testid="app-sidebar"]' +const APP_FAILED = '[data-testid="app-page-failed"]' +const APP_RETRY = '[data-testid="app-page-retry"]' +const CUTOUT_PAGE = '/apps/remove-background/desktop.html' +const DOWN_PAGE = '/apps/down-for-now/desktop.html' +const OUTSIDE_LINK = 'https://docs.example.test/pricing' +const LONG_WAIT = { timeout: 20_000 } + +function appPageHtml(title: string, cloudUrl: string): string { + return ` + + ${title} + + All apps +

${title}

+

pending

+ + +

+ Pricing + + +` +} + +class FakeAppsSite { + downPageAnswers = 503 + port = 0 + private server: Server | null = null + + constructor(private readonly cloudUrl: () => string) {} + + get url(): string { + return `http://127.0.0.1:${this.port}` + } + + async start(): Promise { + const server = createServer((req, res) => this.route(req, res)) + this.server = server + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) + this.port = (server.address() as AddressInfo).port + } + + async stop(): Promise { + const server = this.server + if (!server) return + this.server = null + server.closeAllConnections() + await new Promise((resolve) => server.close(resolve)) + } + + private html(res: ServerResponse, status: number, body: string): void { + res.writeHead(status, { 'content-type': 'text/html; charset=utf-8' }) + res.end(body) + } + + private route(req: IncomingMessage, res: ServerResponse): void { + const { pathname } = new URL(req.url ?? '/', this.url) + if (pathname === '/apps/apps.json') { + return reply(res, 200, [ + { + id: 'remove-background', + page: CUTOUT_PAGE, + title: 'Remove Background', + blurb: 'Cut the subject out of a picture or video.' + }, + { + id: 'down-for-now', + page: DOWN_PAGE, + title: 'Down For Now', + blurb: 'Its page is not answering yet.' + }, + { id: 'elsewhere', page: 'https://evil.example/apps/x', title: 'Elsewhere' } + ]) + } + if (pathname === CUTOUT_PAGE) { + return this.html(res, 200, appPageHtml('Remove Background page', this.cloudUrl())) + } + if (pathname === DOWN_PAGE) { + const status = this.downPageAnswers + return this.html(res, status, appPageHtml('Down For Now page', this.cloudUrl())) + } + if (pathname === '/apps/') return this.html(res, 200, '

The website list

') + this.html(res, 404, 'Not found') + } +} + +interface Box { + x: number + y: number + width: number + height: number +} + +interface HostedView { + visible: boolean + bounds: Box +} + +function viewShowing(app: ElectronApplication, marker: string): Promise { + return app.evaluate(({ BrowserWindow, WebContentsView }, urlPart) => { + for (const win of BrowserWindow.getAllWindows()) { + for (const child of win.contentView.children) { + if (!(child instanceof WebContentsView)) continue + if (!child.webContents.getURL().includes(urlPart)) continue + return { visible: child.getVisible(), bounds: child.getBounds() } + } + } + return null + }, marker) +} + +let site: FakeAppsSite +let cloud: FakeCloud +let ctx: AppContext +let userDataDir: string +let promptBearers: BearerClaims[] +let stopBrowser: () => Promise = async () => {} +let inheritedEnv: Record + +function cutoutPage(): WebContentsPage { + return new WebContentsPage(ctx.app, CUTOUT_PAGE) +} + +function sidebarHasFoldedToItsRail(): Promise { + return ctx.panel.evaluate( + `(() => { const sidebar = document.querySelector('${SIDEBAR}'); return sidebar.dataset.collapsed === 'true' && sidebar.getAnimations().length === 0 })()` + ) +} + +async function appPageAreaInWindow(): Promise { + const area = await ctx.panel.evaluate( + `(() => { const r = document.querySelector('${APP_HOST}').getBoundingClientRect(); return { x: r.x, y: r.y, width: r.width, height: r.height } })()` + ) + const panel = await viewShowing(ctx.app, 'panel.html') + return { + x: (panel?.bounds.x ?? 0) + Math.round(area.x), + y: (panel?.bounds.y ?? 0) + Math.round(area.y), + width: Math.round(area.width), + height: Math.round(area.height) + } +} + +function allowPageToCallCloud(res: ServerResponse): void { + res.setHeader('access-control-allow-origin', '*') + res.setHeader('access-control-allow-headers', 'authorization, content-type') +} + +function completeSignInsLikeABrowser(app: ElectronApplication): () => Promise { + let followed = 0 + let stopped = false + const loop = (async () => { + while (!stopped) { + const opened = await getShellOpenExternalCalls(app).catch(() => [] as string[]) + for (const url of opened.slice(followed)) { + followed += 1 + if (url.startsWith(cloud.url)) await fetch(url).catch(() => {}) + } + await new Promise((resolve) => setTimeout(resolve, 100)) + } + })() + return async () => { + stopped = true + await loop + } +} + +test.describe.configure({ mode: 'serial' }) +test.setTimeout(120_000) + +test.beforeAll(async () => { + inheritedEnv = { + COMFY_APPS_URL: process.env['COMFY_APPS_URL'], + COMFY_CLOUD_ISSUER: process.env['COMFY_CLOUD_ISSUER'], + COMFY_BUILDER_BASE_URL: process.env['COMFY_BUILDER_BASE_URL'] + } + promptBearers = [] + cloud = new FakeCloud((req, res, url) => { + if (url.pathname !== '/api/prompt') return false + allowPageToCallCloud(res) + if (req.method === 'OPTIONS') { + res.writeHead(204) + res.end() + return true + } + promptBearers.push(claimsOf(req)) + reply(res, 200, { prompt_id: 'run-1' }) + return true + }) + await cloud.start() + site = new FakeAppsSite(() => cloud.url) + await site.start() + process.env['COMFY_APPS_URL'] = `${site.url}/apps/` + process.env['COMFY_CLOUD_ISSUER'] = cloud.url + process.env['COMFY_BUILDER_BASE_URL'] = cloud.url + userDataDir = await realpath(await mkdtemp(path.join(os.tmpdir(), 'comfy-apps-e2e-'))) + ctx = await launchApp( + { userDataDir, settings: { firstUseCompleted: true, telemetryEnabled: false } }, + { stayOnHome: true } + ) + stopBrowser = completeSignInsLikeABrowser(ctx.app) +}) + +test.afterAll(async () => { + await stopBrowser() + await ctx?.cleanup() + await site?.stop() + await cloud?.stop() + if (userDataDir) await rm(userDataDir, { recursive: true, force: true }) + for (const [key, value] of Object.entries(inheritedEnv ?? {})) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } +}) + +test("Apps lists the website's apps as cards, without the one that opens another site @macos @windows @linux", async () => { + await ctx.panel.waitForVisible(SIDEBAR_APPS) + expect(await ctx.panel.click(SIDEBAR_APPS)).toBe(true) + + await ctx.panel.waitForVisible(APPS_GRID) + await expect + .poll(() => ctx.panel.allText(`${APPS_GRID} button span.font-medium`), LONG_WAIT) + .toEqual(['Remove Background', 'Down For Now']) +}) + +test('A card opens its app page inside Desktop, exactly over the Apps page beside the folded sidebar @macos @windows @linux', async () => { + expect(await ctx.panel.click(CUTOUT_CARD)).toBe(true) + + await expect + .poll(async () => (await viewShowing(ctx.app, CUTOUT_PAGE))?.visible ?? false, LONG_WAIT) + .toBe(true) + expect(await cutoutPage().textOf('#title')).toBe('Remove Background page') + + await expect.poll(sidebarHasFoldedToItsRail, LONG_WAIT).toBe(true) + await expect(async () => { + const area = await appPageAreaInWindow() + expect((await viewShowing(ctx.app, CUTOUT_PAGE))?.bounds).toEqual(area) + }).toPass(LONG_WAIT) +}) + +test("The app page sees Desktop's sign-in, signed out, and none of Desktop's other controls @macos @windows @linux", async () => { + await expect.poll(() => cutoutPage().textOf('#who'), LONG_WAIT).toBe('signed_out') + + const exposed = await cutoutPage().evaluate( + `Object.keys(window.comfyDesktopAuth).sort()` + ) + expect(exposed).toEqual([ + 'getPartnerNodeToken', + 'getState', + 'getWorkspaceToken', + 'onChanged', + 'requestSignIn' + ]) + expect( + await cutoutPage().evaluate( + `[typeof window.api, typeof window.__comfyDesktop2, typeof window.require].join()` + ) + ).toBe('undefined,undefined,undefined') +}) + +test('A link to another site goes to the browser and the app stays open @macos @windows @linux', async () => { + expect(await cutoutPage().click('#outside')).toBe(true) + + await expect.poll(() => getShellOpenExternalCalls(ctx.app), LONG_WAIT).toContain(OUTSIDE_LINK) + expect(await cutoutPage().url()).toBe(`${site.url}${CUTOUT_PAGE}`) + expect((await viewShowing(ctx.app, CUTOUT_PAGE))?.visible).toBe(true) +}) + +test('Signing in from the app page signs Desktop in, and a run reaches Comfy Cloud with a workspace token @macos @windows', async () => { + expect(await cutoutPage().click('#signin')).toBe(true) + + await expect.poll(() => cutoutPage().textOf('#who'), LONG_WAIT).toBe('e2e@example.com in A') + expect(cloud.authorizeUrls, 'one consent in the browser').toHaveLength(1) + + expect(await cutoutPage().click('#run')).toBe(true) + + await expect + .poll(() => cutoutPage().textOf('#result'), LONG_WAIT) + .toBe('Comfy Cloud answered 200') + expect(promptBearers).toHaveLength(1) + expect(promptBearers[0]?.workspace_id).toBe('A') + expect(promptBearers[0]?.scope, 'the page never holds the token that can mint others').toBe( + undefined + ) +}) + +test('"All apps" on the app page returns to the cards without loading the website list @macos @windows @linux', async () => { + expect(await cutoutPage().click('#back')).toBe(true) + + await ctx.panel.waitForVisible(APPS_GRID) + await expect + .poll(async () => (await viewShowing(ctx.app, CUTOUT_PAGE))?.visible, LONG_WAIT) + .toBe(false) + expect(await cutoutPage().textOf('#title')).toBe('Remove Background page') +}) + +test('An app whose page is down says so, and Try again opens it once the site answers @macos @windows @linux', async () => { + expect(await ctx.panel.click(DOWN_CARD)).toBe(true) + + await ctx.panel.waitForVisible(APP_FAILED) + expect(await ctx.panel.textOf(`${APP_FAILED} h1`)).toBe("Down For Now didn't load") + expect((await viewShowing(ctx.app, DOWN_PAGE))?.visible ?? false).toBe(false) + + site.downPageAnswers = 200 + expect(await ctx.panel.click(APP_RETRY)).toBe(true) + + await expect + .poll(async () => (await viewShowing(ctx.app, DOWN_PAGE))?.visible ?? false, LONG_WAIT) + .toBe(true) + expect(await new WebContentsPage(ctx.app, DOWN_PAGE).textOf('#title')).toBe('Down For Now page') +}) diff --git a/e2e/support/fakeCloud.ts b/e2e/support/fakeCloud.ts new file mode 100644 index 000000000..e2add4d54 --- /dev/null +++ b/e2e/support/fakeCloud.ts @@ -0,0 +1,165 @@ +import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http' +import type { AddressInfo } from 'node:net' + +const SUBJECT = 'user-e2e' +const WORKSPACES = ['A', 'B', 'C'] + +export type ExtraRoute = ( + req: IncomingMessage, + res: ServerResponse, + url: URL +) => boolean | Promise + +export type MintBehavior = (workspaceId: string, bearerWorkspace: string) => FakeReply | undefined + +export interface FakeReply { + status: number + body: unknown +} + +export interface BearerClaims { + workspace_id?: string + scope?: string +} + +function jwt(claims: Record): string { + const b64 = (value: unknown): string => Buffer.from(JSON.stringify(value)).toString('base64url') + return `${b64({ alg: 'none' })}.${b64(claims)}.fake-signature` +} + +export function claimsOf(req: IncomingMessage): BearerClaims { + const segment = req.headers.authorization?.replace('Bearer ', '').split('.')[1] + if (!segment) return {} + return JSON.parse(Buffer.from(segment, 'base64url').toString('utf-8')) as BearerClaims +} + +function tokenFor(workspaceId: string, lifetimeSeconds: number, scope?: string): string { + const iat = Math.floor(Date.now() / 1000) + return jwt({ + sub: SUBJECT, + email: 'e2e@example.com', + workspace_id: workspaceId, + workspace_type: 'team', + role: 'member', + ...(scope ? { scope } : {}), + iat, + exp: iat + lifetimeSeconds + }) +} + +export function reply(res: ServerResponse, status: number, body: unknown): void { + res.writeHead(status, { 'content-type': 'application/json' }) + res.end(JSON.stringify(body)) +} + +async function bodyOf(req: IncomingMessage): Promise { + const chunks: Buffer[] = [] + for await (const chunk of req) chunks.push(chunk as Buffer) + return Buffer.concat(chunks).toString('utf-8') +} + +export class FakeCloud { + readonly authorizeUrls: URL[] = [] + readonly mints: { workspaceId: string; bearerWorkspace: string }[] = [] + readonly builds: { workspace: string; canMint: boolean }[] = [] + readonly members: string[] = [] + mintBehavior: MintBehavior = () => undefined + consentOverride: string | null = null + port = 0 + private server: Server | null = null + private readonly codes = new Map() + private nextId = 0 + + constructor(private readonly extraRoute: ExtraRoute = () => false) {} + + get url(): string { + return `http://127.0.0.1:${this.port}` + } + + async start(): Promise { + const server = createServer((req, res) => { + void this.route(req, res) + }) + this.server = server + await new Promise((resolve) => server.listen(this.port, '127.0.0.1', resolve)) + this.port = (server.address() as AddressInfo).port + } + + async stop(): Promise { + const server = this.server + if (!server) return + this.server = null + server.closeAllConnections() + await new Promise((resolve) => server.close(resolve)) + } + + private async route(req: IncomingMessage, res: ServerResponse): Promise { + const url = new URL(req.url ?? '/', this.url) + if (await this.extraRoute(req, res, url)) return + if (url.pathname === '/oauth/authorize') return this.authorize(url, res) + if (url.pathname === '/oauth/token') return this.token(req, res) + if (url.pathname === '/api/auth/token') return this.mint(req, res) + if (url.pathname === '/api/workspaces') { + return reply(res, 200, { + workspaces: WORKSPACES.map((id) => ({ + id, + name: `Workspace ${id}`, + type: 'team', + role: 'member' + })) + }) + } + if (url.pathname === '/api/workspace/members') { + this.members.push(claimsOf(req).workspace_id ?? '') + return reply(res, 200, { members: [], pagination: { has_more: false } }) + } + if (url.pathname === '/v1/builds') { + const claims = claimsOf(req) + this.builds.push({ + workspace: claims.workspace_id ?? '', + canMint: claims.scope !== undefined + }) + return reply(res, 200, { builds: [] }) + } + reply(res, 404, { code: 'NOT_FOUND' }) + } + + private authorize(url: URL, res: ServerResponse): void { + this.authorizeUrls.push(url) + const code = `code-${++this.nextId}` + this.codes.set(code, { + workspaceId: this.consentOverride ?? url.searchParams.get('workspace_id') ?? 'A', + scope: url.searchParams.get('scope') ?? '' + }) + const redirect = new URL(url.searchParams.get('redirect_uri') ?? '') + redirect.searchParams.set('code', code) + redirect.searchParams.set('state', url.searchParams.get('state') ?? '') + res.writeHead(302, { location: redirect.toString() }) + res.end() + } + + private async token(req: IncomingMessage, res: ServerResponse): Promise { + const form = new URLSearchParams(await bodyOf(req)) + const grant = this.codes.get(form.get('code') ?? form.get('refresh_token') ?? '') + if (!grant) return reply(res, 400, { error: 'invalid_grant' }) + const refreshToken = `refresh-${++this.nextId}` + this.codes.set(refreshToken, grant) + reply(res, 200, { + access_token: tokenFor(grant.workspaceId, 3600, grant.scope), + refresh_token: refreshToken, + expires_in: 3600 + }) + } + + private async mint(req: IncomingMessage, res: ServerResponse): Promise { + const workspaceId = (JSON.parse(await bodyOf(req)) as { workspace_id: string }).workspace_id + const bearerWorkspace = claimsOf(req).workspace_id ?? '' + this.mints.push({ workspaceId, bearerWorkspace }) + const scripted = this.mintBehavior(workspaceId, bearerWorkspace) + if (scripted) return reply(res, scripted.status, scripted.body) + reply(res, 200, { + token: tokenFor(workspaceId, 900), + expires_at: new Date(Date.now() + 900_000).toISOString() + }) + } +} diff --git a/e2e/workspace-switch-no-browser.test.ts b/e2e/workspace-switch-no-browser.test.ts index 93158cd1b..1bb8d8b59 100644 --- a/e2e/workspace-switch-no-browser.test.ts +++ b/e2e/workspace-switch-no-browser.test.ts @@ -1,5 +1,3 @@ -import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http' -import type { AddressInfo } from 'node:net' import { existsSync } from 'node:fs' import { mkdir, mkdtemp, readFile, realpath, rm } from 'node:fs/promises' import os from 'node:os' @@ -8,6 +6,7 @@ import { expect, test } from '@playwright/test' import en from '../locales/en.json' import { launchApp, type AppContext } from './launchApp' import { getShellOpenExternalCalls } from './support/devHooks' +import { FakeCloud, type FakeReply } from './support/fakeCloud' import { captureHostWindow } from './support/windowCapture' const UNAVAILABLE_COPY = en.devPlatform.workspace.switchUnavailable @@ -17,161 +16,8 @@ const SELECTOR = '[data-testid="devplatform-workspace-selector"]' const REFRESH = '[data-testid="chooser-workspace-refresh"]' const SIGN_IN = '[data-testid="devplatform-account-signin"]' const SETTLE_MS = 1_500 -const SUBJECT = 'user-e2e' -const WORKSPACES = ['A', 'B', 'C'] const AUTH_VAULT = 'comfy-cloud-auth.bin' -type MintBehavior = (workspaceId: string, bearerWorkspace: string) => FakeReply | undefined - -interface FakeReply { - status: number - body: unknown -} - -interface BearerClaims { - workspace_id?: string - scope?: string -} - -function jwt(claims: Record): string { - const b64 = (value: unknown): string => Buffer.from(JSON.stringify(value)).toString('base64url') - return `${b64({ alg: 'none' })}.${b64(claims)}.fake-signature` -} - -function claimsOf(req: IncomingMessage): BearerClaims { - const segment = req.headers.authorization?.replace('Bearer ', '').split('.')[1] - if (!segment) return {} - return JSON.parse(Buffer.from(segment, 'base64url').toString('utf-8')) as BearerClaims -} - -function tokenFor(workspaceId: string, lifetimeSeconds: number, scope?: string): string { - const iat = Math.floor(Date.now() / 1000) - return jwt({ - sub: SUBJECT, - email: 'e2e@example.com', - workspace_id: workspaceId, - workspace_type: 'team', - role: 'member', - ...(scope ? { scope } : {}), - iat, - exp: iat + lifetimeSeconds - }) -} - -function reply(res: ServerResponse, status: number, body: unknown): void { - res.writeHead(status, { 'content-type': 'application/json' }) - res.end(JSON.stringify(body)) -} - -async function bodyOf(req: IncomingMessage): Promise { - const chunks: Buffer[] = [] - for await (const chunk of req) chunks.push(chunk as Buffer) - return Buffer.concat(chunks).toString('utf-8') -} - -class FakeCloud { - readonly authorizeUrls: URL[] = [] - readonly mints: { workspaceId: string; bearerWorkspace: string }[] = [] - readonly builds: { workspace: string; canMint: boolean }[] = [] - readonly members: string[] = [] - mintBehavior: MintBehavior = () => undefined - consentOverride: string | null = null - port = 0 - private server: Server | null = null - private readonly codes = new Map() - private nextId = 0 - - get url(): string { - return `http://127.0.0.1:${this.port}` - } - - async start(): Promise { - const server = createServer((req, res) => { - void this.route(req, res) - }) - this.server = server - await new Promise((resolve) => server.listen(this.port, '127.0.0.1', resolve)) - this.port = (server.address() as AddressInfo).port - } - - async stop(): Promise { - const server = this.server - if (!server) return - this.server = null - server.closeAllConnections() - await new Promise((resolve) => server.close(resolve)) - } - - private async route(req: IncomingMessage, res: ServerResponse): Promise { - const url = new URL(req.url ?? '/', this.url) - if (url.pathname === '/oauth/authorize') return this.authorize(url, res) - if (url.pathname === '/oauth/token') return this.token(req, res) - if (url.pathname === '/api/auth/token') return this.mint(req, res) - if (url.pathname === '/api/workspaces') { - return reply(res, 200, { - workspaces: WORKSPACES.map((id) => ({ - id, - name: `Workspace ${id}`, - type: 'team', - role: 'member' - })) - }) - } - if (url.pathname === '/api/workspace/members') { - this.members.push(claimsOf(req).workspace_id ?? '') - return reply(res, 200, { members: [], pagination: { has_more: false } }) - } - if (url.pathname === '/v1/builds') { - const claims = claimsOf(req) - this.builds.push({ - workspace: claims.workspace_id ?? '', - canMint: claims.scope !== undefined - }) - return reply(res, 200, { builds: [] }) - } - reply(res, 404, { code: 'NOT_FOUND' }) - } - - private authorize(url: URL, res: ServerResponse): void { - this.authorizeUrls.push(url) - const code = `code-${++this.nextId}` - this.codes.set(code, { - workspaceId: this.consentOverride ?? url.searchParams.get('workspace_id') ?? 'A', - scope: url.searchParams.get('scope') ?? '' - }) - const redirect = new URL(url.searchParams.get('redirect_uri') ?? '') - redirect.searchParams.set('code', code) - redirect.searchParams.set('state', url.searchParams.get('state') ?? '') - res.writeHead(302, { location: redirect.toString() }) - res.end() - } - - private async token(req: IncomingMessage, res: ServerResponse): Promise { - const form = new URLSearchParams(await bodyOf(req)) - const grant = this.codes.get(form.get('code') ?? form.get('refresh_token') ?? '') - if (!grant) return reply(res, 400, { error: 'invalid_grant' }) - const refreshToken = `refresh-${++this.nextId}` - this.codes.set(refreshToken, grant) - reply(res, 200, { - access_token: tokenFor(grant.workspaceId, 3600, grant.scope), - refresh_token: refreshToken, - expires_in: 3600 - }) - } - - private async mint(req: IncomingMessage, res: ServerResponse): Promise { - const workspaceId = (JSON.parse(await bodyOf(req)) as { workspace_id: string }).workspace_id - const bearerWorkspace = claimsOf(req).workspace_id ?? '' - this.mints.push({ workspaceId, bearerWorkspace }) - const scripted = this.mintBehavior(workspaceId, bearerWorkspace) - if (scripted) return reply(res, scripted.status, scripted.body) - reply(res, 200, { - token: tokenFor(workspaceId, 900), - expires_at: new Date(Date.now() + 900_000).toISOString() - }) - } -} - let cloud: FakeCloud let ctx: AppContext | null = null let userDataDir: string diff --git a/electron.vite.config.ts b/electron.vite.config.ts index c818da178..ed2b873db 100644 --- a/electron.vite.config.ts +++ b/electron.vite.config.ts @@ -28,6 +28,7 @@ export default defineConfig({ input: { index: resolve(__dirname, 'src/preload/index.ts'), comfyPreload: resolve(__dirname, 'src/preload/comfyPreload.ts'), + appsPreload: resolve(__dirname, 'src/preload/appsPreload.ts'), terminalPreload: resolve(__dirname, 'src/preload/terminalPreload.ts'), comfyTitleBarPreload: resolve(__dirname, 'src/preload/comfyTitleBarPreload.ts'), comfyTitlePopupPreload: resolve(__dirname, 'src/preload/comfyTitlePopupPreload.ts'), diff --git a/locales/en.json b/locales/en.json index 14703fe6c..3a04d1660 100644 --- a/locales/en.json +++ b/locales/en.json @@ -19,6 +19,7 @@ "builds": "Builds", "navigation": "Navigation", "home": "Home", + "apps": "Apps", "assets": "Assets", "collapse": "Collapse sidebar", "expand": "Expand sidebar", @@ -31,6 +32,18 @@ "crashedWithSignal": "Crashed ({signal})", "failedToStartReason": "Failed to start: {reason}" }, + "apps": { + "search": "Search apps", + "failedTitle": "Apps didn't load", + "failedBody": "Check your internet connection, then try again.", + "retry": "Try again", + "emptyTitle": "No apps yet", + "emptyBody": "New apps will show up here.", + "noMatches": "No apps match “{query}”", + "allApps": "All apps", + "pageLoading": "Loading {app}", + "pageFailedTitle": "{app} didn't load" + }, "assets": { "kindLabel": "Asset type", "kind": { diff --git a/locales/zh.json b/locales/zh.json index ad28f6365..160650679 100644 --- a/locales/zh.json +++ b/locales/zh.json @@ -19,6 +19,7 @@ "builds": "构建", "navigation": "导航", "home": "主页", + "apps": "应用", "assets": "资产", "collapse": "收起侧边栏", "expand": "展开侧边栏", @@ -31,6 +32,18 @@ "crashedWithSignal": "已崩溃({signal})", "failedToStartReason": "启动失败:{reason}" }, + "apps": { + "search": "搜索应用", + "failedTitle": "应用未能加载", + "failedBody": "请检查网络连接,然后重试。", + "retry": "重试", + "emptyTitle": "暂无应用", + "emptyBody": "新应用会显示在这里。", + "noMatches": "没有与“{query}”匹配的应用", + "allApps": "全部应用", + "pageLoading": "正在加载{app}", + "pageFailedTitle": "{app}未能加载" + }, "assets": { "kindLabel": "资产类型", "kind": { diff --git a/packages/comfyui-desktop-bridge-types/README.md b/packages/comfyui-desktop-bridge-types/README.md index 16e0cfbd9..57d38f99e 100644 --- a/packages/comfyui-desktop-bridge-types/README.md +++ b/packages/comfyui-desktop-bridge-types/README.md @@ -39,6 +39,27 @@ async function openModelAccessPage(url: string): Promise { every top-level member required, so Desktop's preload cannot claim the contract while omitting part of it. +## Comfy pages hosted in Desktop's Apps view + +A Comfy page that Desktop shows in its Apps view gets `window.comfyDesktopAuth` +instead: Desktop's account session, and nothing else of the bridge above. + +```ts +import type { ComfyDesktopHostedAuthBridge } from '@comfyorg/comfyui-desktop-bridge-types' + +declare global { + interface Window { + comfyDesktopAuth?: ComfyDesktopHostedAuthBridge + } +} +``` + +The page reads the session with `getState()`, follows it with `onChanged()`, +asks for the workspace credential with `getWorkspaceToken(workspaceId)` each +time it calls Comfy Cloud, and starts a sign-in with `requestSignIn()`. It +cannot sign Desktop out or switch workspace. The object is absent in a +browser, so a page keeps its own sign-in as the fallback. + ## Provenance Generated from `src/types/comfyDesktopBridge.ts` in diff --git a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts index 5e1ca2a9b..de1d49231 100644 --- a/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts +++ b/packages/comfyui-desktop-bridge-types/comfyDesktopBridge.d.ts @@ -174,6 +174,19 @@ export interface ComfyDesktop2AuthBridge { amountCents?: number ): Promise } +/** + * Desktop's own Comfy account session, as a Comfy page hosted in Desktop's + * Apps view sees it. Desktop exposes it to that page as + * `window.comfyDesktopAuth`. The page can read the session, hold the + * workspace and partner-node credentials, and start a sign-in; it cannot sign + * Desktop out or switch workspace. + */ +export type ComfyDesktopHostedAuthBridge = Required< + Pick< + ComfyDesktop2AuthBridge, + 'getState' | 'getWorkspaceToken' | 'getPartnerNodeToken' | 'requestSignIn' | 'onChanged' + > +> export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise diff --git a/packages/comfyui-desktop-bridge-types/package.json b/packages/comfyui-desktop-bridge-types/package.json index c98d67a91..acdb84d2c 100644 --- a/packages/comfyui-desktop-bridge-types/package.json +++ b/packages/comfyui-desktop-bridge-types/package.json @@ -1,6 +1,6 @@ { "name": "@comfyorg/comfyui-desktop-bridge-types", - "version": "0.8.0", + "version": "0.9.0", "description": "TypeScript definitions for the Comfy Desktop hosted frontend bridge", "type": "module", "main": "./index.js", diff --git a/src/main/apps/appPageView.test.ts b/src/main/apps/appPageView.test.ts new file mode 100644 index 000000000..c8bffe08d --- /dev/null +++ b/src/main/apps/appPageView.test.ts @@ -0,0 +1,672 @@ +import { EventEmitter } from 'node:events' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { WebContents, WebFrameMain } from 'electron' + +type PermissionCheck = (contents: unknown, permission: string, requestingOrigin: string) => boolean +type PermissionRequest = ( + contents: unknown, + permission: string, + callback: (granted: boolean) => void, + details: { requestingUrl: string } +) => void +type WindowOpenHandler = (details: { url: string }) => { action: string } + +const mocks = vi.hoisted(() => ({ + appsEnabled: true, + windows: new Map(), + createView: (_options: unknown): unknown => null, + session: null as unknown, + openExternal: vi.fn((_url: string) => Promise.resolve()), + attachDownloads: vi.fn(), + capture: vi.fn(), + pages: new Map() +})) + +vi.mock('electron', () => ({ + WebContentsView: class { + constructor(options: unknown) { + return mocks.createView(options) as object + } + }, + app: { isPackaged: false }, + session: { fromPartition: () => mocks.session }, + shell: { openExternal: mocks.openExternal } +})) +vi.mock('../host/registry', () => ({ + get comfyWindows() { + return mocks.windows + } +})) +vi.mock('../lib/comfyDownloadManager', () => ({ + attachSessionDownloadHandler: mocks.attachDownloads +})) +vi.mock('../lib/contextMenu', () => ({ attachContextMenu: vi.fn() })) +vi.mock('../lib/telemetry', () => ({ capture: mocks.capture })) +vi.mock('./appsCatalog', () => ({ + pageUrlForListedApp: (id: string) => mocks.pages.get(id) ?? null +})) +vi.mock('./appsConfig', () => ({ + areAppsEnabled: async () => mocks.appsEnabled, + currentAppsLocation: () => ({ + url: 'https://comfy.org/apps/', + origin: 'https://comfy.org', + scopePath: '/apps/' + }) +})) + +import { _resetForTest as forgetAppsPages, isAppsPageSender, liveAppsPages } from './appsPages' +import { + APP_OPENED_EVENT, + APP_PAGE_CLOSED_CHANNEL, + APP_PAGE_FAILED_EVENT, + APP_PAGE_STATUS_CHANNEL, + _resetForTest, + captureAppPage, + hideAppPage, + reloadAppPage, + showAppPage +} from './appPageView' + +const APPS_URL = 'https://comfy.org/apps/' +const CUTOUT = 'remove-background' +const CUTOUT_URL = `${APPS_URL}remove-background/desktop.html` +const UPSCALE = 'upscale' +const UPSCALE_URL = `${APPS_URL}#/upscale` +const WINDOW_KEY = 7 +const PANEL_FRAME = { x: 0, y: 41, width: 1200, height: 760 } +const PLACEMENT = { x: 256, y: 8, width: 936, height: 744, cornerRadius: 14 } + +class FakeContents extends EventEmitter { + url = '' + destroyed = false + zoom = 1 + windowOpenHandler: WindowOpenHandler | null = null + mainFrame = { processId: 4, routingId: 1 } + send = vi.fn() + focus = vi.fn() + capturePage = vi.fn() + loadURL = vi.fn((url: string) => { + this.url = url + return Promise.resolve() + }) + close = vi.fn(() => { + this.destroyed = true + }) + getURL(): string { + return this.url + } + isDestroyed(): boolean { + return this.destroyed + } + getZoomFactor(): number { + return this.zoom + } + setWindowOpenHandler(handler: WindowOpenHandler): void { + this.windowOpenHandler = handler + } +} + +class FakeView { + webContents = new FakeContents() + visible = true + bounds = { x: 0, y: 0, width: 0, height: 0 } + cornerRadius = 0 + setBackgroundColor = vi.fn() + setVisible(visible: boolean): void { + this.visible = visible + } + setBounds(bounds: typeof this.bounds): void { + this.bounds = bounds + } + setBorderRadius(radius: number): void { + this.cornerRadius = radius + } + getBounds(): typeof this.bounds { + return this.bounds + } +} + +class FakeWindow extends EventEmitter { + stack: unknown[] = [] + contentView = { + get children(): unknown[] { + return [...stackOf(this)] + }, + addChildView: (view: unknown) => { + this.stack = [...this.stack.filter((child) => child !== view), view] + }, + removeChildView: (view: unknown) => { + this.stack = this.stack.filter((child) => child !== view) + } + } + isDestroyed(): boolean { + return false + } +} + +const windowsByContentView = new WeakMap() +function stackOf(contentView: object): unknown[] { + return windowsByContentView.get(contentView)?.stack ?? [] +} + +interface FakeSession { + check: PermissionCheck | null + request: PermissionRequest | null + setPermissionCheckHandler(handler: PermissionCheck): void + setPermissionRequestHandler(handler: PermissionRequest): void +} + +let hostWindow: FakeWindow +let panel: FakeView +let created: FakeView[] +let appsSession: FakeSession + +function panelContents(): WebContents { + return panel.webContents as unknown as WebContents +} + +function hostedPage(): FakeView { + const view = created[0] + if (!view) throw new Error('The hosted Apps page was never created') + return view +} + +function finishLoading(url = CUTOUT_URL, httpStatus = 200): void { + const contents = hostedPage().webContents + contents.url = url + contents.emit('did-navigate', {}, url, httpStatus) + contents.emit('dom-ready') +} + +function navigationEvent(): { preventDefault: ReturnType } { + return { preventDefault: vi.fn() } +} + +function sentToPanel(channel: string): unknown[][] { + return panel.webContents.send.mock.calls.filter(([sent]) => sent === channel) +} + +function lastStatusSentToPanel(): unknown { + return sentToPanel(APP_PAGE_STATUS_CHANNEL).at(-1)?.[1] +} + +function openCutout(placement: Record = PLACEMENT): Promise { + return showAppPage(panelContents(), CUTOUT, placement) +} + +beforeEach(() => { + vi.clearAllMocks() + _resetForTest() + forgetAppsPages() + mocks.appsEnabled = true + mocks.pages = new Map([ + [CUTOUT, CUTOUT_URL], + [UPSCALE, UPSCALE_URL] + ]) + created = [] + hostWindow = new FakeWindow() + windowsByContentView.set(hostWindow.contentView, hostWindow) + panel = new FakeView() + panel.bounds = { ...PANEL_FRAME } + hostWindow.contentView.addChildView(panel) + appsSession = { + check: null, + request: null, + setPermissionCheckHandler(handler) { + this.check = handler + }, + setPermissionRequestHandler(handler) { + this.request = handler + } + } + mocks.session = appsSession + mocks.createView = () => { + const view = new FakeView() + created.push(view) + return view + } + mocks.windows = new Map([[WINDOW_KEY, { window: hostWindow, panelView: panel }]]) +}) + +describe('opening an app', () => { + it("loads the app's page hidden, then shows it where the panel placed it", async () => { + expect(await openCutout()).toBe('loading') + expect(hostedPage().webContents.loadURL).toHaveBeenCalledWith(CUTOUT_URL) + expect(hostedPage().visible).toBe(false) + + finishLoading() + + expect(hostedPage().visible).toBe(true) + expect(hostedPage().bounds).toEqual({ x: 256, y: 49, width: 936, height: 744 }) + expect(hostedPage().cornerRadius).toBe(14) + expect(lastStatusSentToPanel()).toBe('ready') + }) + + it('loads in a sandboxed page with its own storage and no Node access', async () => { + const options: unknown[] = [] + const createView = mocks.createView + mocks.createView = (viewOptions) => { + options.push(viewOptions) + return createView(viewOptions) + } + + await openCutout() + + expect(options).toEqual([ + { + webPreferences: expect.objectContaining({ + session: appsSession, + sandbox: true, + contextIsolation: true, + nodeIntegration: false + }) + } + ]) + }) + + it('keeps the open app as it was when the person comes back to it', async () => { + await openCutout() + finishLoading() + hideAppPage(panelContents()) + expect(hostedPage().visible).toBe(false) + + expect(await openCutout()).toBe('ready') + + expect(created).toHaveLength(1) + expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(1) + expect(hostedPage().visible).toBe(true) + }) + + it('loads another app in the same page and hides it until that app is ready', async () => { + await openCutout() + finishLoading() + + expect(await showAppPage(panelContents(), UPSCALE, PLACEMENT)).toBe('loading') + + expect(created).toHaveLength(1) + expect(hostedPage().webContents.loadURL).toHaveBeenLastCalledWith(UPSCALE_URL) + expect(hostedPage().visible).toBe(false) + expect(lastStatusSentToPanel()).toBe('loading') + + finishLoading(UPSCALE_URL) + expect(hostedPage().visible).toBe(true) + }) + + it('scales the placement by the panel zoom and keeps it inside the panel', async () => { + panel.webContents.zoom = 1.25 + await openCutout({ x: 200, y: 0, width: 2000, height: 2000, cornerRadius: 8 }) + + expect(hostedPage().bounds).toEqual({ x: 250, y: 41, width: 950, height: 760 }) + expect(hostedPage().cornerRadius).toBe(10) + }) + + it.each([ + null, + 'left', + { x: 0, y: 0, width: 100 }, + { x: Number.NaN, y: 0, width: 100, height: 100, cornerRadius: 0 }, + { x: 0, y: 0, width: -1, height: 100, cornerRadius: 0 } + ])('creates nothing for the malformed placement %j', async (placement) => { + expect(await showAppPage(panelContents(), CUTOUT, placement)).toBe('failed') + expect(created).toHaveLength(0) + }) + + it.each(['not-in-the-list', '', 42, null, { id: CUTOUT }])( + 'creates nothing for %j, which the apps list does not name', + async (appId) => { + expect(await showAppPage(panelContents(), appId, PLACEMENT)).toBe('failed') + expect(created).toHaveLength(0) + } + ) + + it('creates nothing while Apps is switched off', async () => { + mocks.appsEnabled = false + + expect(await openCutout()).toBe('failed') + expect(created).toHaveLength(0) + }) + + it('creates nothing for a sender that is not a panel', async () => { + const stranger = new FakeContents() as unknown as WebContents + + expect(await showAppPage(stranger, CUTOUT, PLACEMENT)).toBe('failed') + expect(created).toHaveLength(0) + }) + + it('takes the keyboard and counts one opening when the person opens an app, not on every resize', async () => { + await openCutout({ ...PLACEMENT, justOpened: true }) + finishLoading() + await openCutout() + + expect(hostedPage().webContents.focus).toHaveBeenCalledTimes(1) + expect(mocks.capture.mock.calls.filter(([name]) => name === APP_OPENED_EVENT)).toEqual([ + [APP_OPENED_EVENT, { app_id: CUTOUT }] + ]) + }) +}) + +describe('going back to all apps', () => { + beforeEach(async () => { + await openCutout() + finishLoading() + }) + + it('closes the app in Desktop instead of loading the website list, and keeps the app as it was', async () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-navigate', event, APPS_URL) + + expect(event.preventDefault).toHaveBeenCalled() + expect(hostedPage().visible).toBe(false) + expect(sentToPanel(APP_PAGE_CLOSED_CHANNEL)).toHaveLength(1) + expect(mocks.openExternal).not.toHaveBeenCalled() + + expect(await openCutout()).toBe('ready') + expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(1) + }) + + it('closes an app drawn by the list page when it routes back to the list, and loads it fresh next time', async () => { + await showAppPage(panelContents(), UPSCALE, PLACEMENT) + finishLoading(UPSCALE_URL) + + hostedPage().webContents.emit('did-navigate-in-page', {}, `${APPS_URL}#/`, true) + + expect(hostedPage().visible).toBe(false) + expect(sentToPanel(APP_PAGE_CLOSED_CHANNEL)).toHaveLength(1) + + expect(await showAppPage(panelContents(), UPSCALE, PLACEMENT)).toBe('loading') + expect(hostedPage().webContents.loadURL).toHaveBeenLastCalledWith(UPSCALE_URL) + }) + + it('closes the app when the site redirects its page to the list', () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-redirect', event, APPS_URL, false, true) + + expect(event.preventDefault).toHaveBeenCalled() + expect(sentToPanel(APP_PAGE_CLOSED_CHANNEL)).toHaveLength(1) + }) + + it('stays open while an embedded frame changes its own hash', () => { + hostedPage().webContents.emit('did-navigate-in-page', {}, `${APPS_URL}#/`, false) + + expect(hostedPage().visible).toBe(true) + expect(sentToPanel(APP_PAGE_CLOSED_CHANNEL)).toHaveLength(0) + }) +}) + +describe('leaving the apps site', () => { + beforeEach(async () => { + await openCutout() + finishLoading() + }) + + it('follows links inside the apps site', () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-navigate', event, `${APPS_URL}genjutsu/desktop.html`) + + expect(event.preventDefault).not.toHaveBeenCalled() + expect(mocks.openExternal).not.toHaveBeenCalled() + }) + + it('opens a link to another site in the browser and stays on the page', () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-navigate', event, 'https://docs.comfy.org/pricing') + + expect(event.preventDefault).toHaveBeenCalled() + expect(mocks.openExternal).toHaveBeenCalledWith('https://docs.comfy.org/pricing') + }) + + it('opens the rest of comfy.org in the browser too', () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-navigate', event, 'https://comfy.org/login/') + + expect(event.preventDefault).toHaveBeenCalled() + expect(mocks.openExternal).toHaveBeenCalledWith('https://comfy.org/login/') + }) + + it('stops a redirect of the page to another site', () => { + const event = navigationEvent() + hostedPage().webContents.emit('will-redirect', event, 'https://evil.example/apps/', false, true) + + expect(event.preventDefault).toHaveBeenCalled() + }) + + it('lets an embedded frame redirect on its own', () => { + const event = navigationEvent() + hostedPage().webContents.emit( + 'will-redirect', + event, + 'https://player.example/embed', + false, + false + ) + + expect(event.preventDefault).not.toHaveBeenCalled() + }) + + it('never opens a new window, and sends a web link from one to the browser', () => { + const handler = hostedPage().webContents.windowOpenHandler + expect(handler?.({ url: 'https://comfy.org/hub/' })).toEqual({ action: 'deny' }) + expect(mocks.openExternal).toHaveBeenCalledWith('https://comfy.org/hub/') + }) + + it.each(['file:///etc/passwd', 'javascript:alert(1)', 'smb://share/x', 'comfy://open'])( + 'does not hand %s to the system', + (url) => { + const event = navigationEvent() + hostedPage().webContents.emit('will-navigate', event, url) + hostedPage().webContents.windowOpenHandler?.({ url }) + + expect(event.preventDefault).toHaveBeenCalled() + expect(mocks.openExternal).not.toHaveBeenCalled() + } + ) +}) + +describe('an app page that cannot load', () => { + it('hides the page and tells the panel when the site answers 404', async () => { + await openCutout() + finishLoading(CUTOUT_URL, 404) + + expect(hostedPage().visible).toBe(false) + expect(lastStatusSentToPanel()).toBe('failed') + expect(mocks.capture).toHaveBeenCalledWith(APP_PAGE_FAILED_EVENT, { + app_id: CUTOUT, + reason: 'http_status' + }) + }) + + it('reports a dropped connection, and loads the same app again when the person retries', async () => { + await openCutout() + hostedPage().webContents.emit('did-fail-load', {}, -106, 'ERR_INTERNET_DISCONNECTED', '', true) + expect(lastStatusSentToPanel()).toBe('failed') + expect(mocks.capture).toHaveBeenCalledWith(APP_PAGE_FAILED_EVENT, { + app_id: CUTOUT, + reason: 'network' + }) + + reloadAppPage(panelContents()) + expect(lastStatusSentToPanel()).toBe('loading') + expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(2) + expect(hostedPage().webContents.loadURL).toHaveBeenLastCalledWith(CUTOUT_URL) + + finishLoading() + expect(hostedPage().visible).toBe(true) + expect(lastStatusSentToPanel()).toBe('ready') + }) + + it('tries a failed app again when the person opens it once more', async () => { + await openCutout({ ...PLACEMENT, justOpened: true }) + finishLoading(CUTOUT_URL, 503) + hideAppPage(panelContents()) + + expect(await openCutout({ ...PLACEMENT, justOpened: true })).toBe('loading') + expect(hostedPage().webContents.loadURL).toHaveBeenCalledTimes(2) + }) + + it('ignores an aborted navigation and a failing embedded frame', async () => { + await openCutout() + finishLoading() + hostedPage().webContents.emit('did-fail-load', {}, -3, 'ERR_ABORTED', CUTOUT_URL, true) + hostedPage().webContents.emit('did-fail-load', {}, -105, 'ERR_NAME_NOT_RESOLVED', '', false) + + expect(hostedPage().visible).toBe(true) + expect(lastStatusSentToPanel()).toBe('ready') + }) + + it('reports a crashed page once', async () => { + await openCutout() + finishLoading() + hostedPage().webContents.emit('render-process-gone', {}, { reason: 'crashed' }) + hostedPage().webContents.emit('render-process-gone', {}, { reason: 'crashed' }) + + expect(hostedPage().visible).toBe(false) + expect(mocks.capture.mock.calls.filter(([name]) => name === APP_PAGE_FAILED_EVENT)).toEqual([ + [APP_PAGE_FAILED_EVENT, { app_id: CUTOUT, reason: 'crashed' }] + ]) + }) +}) + +describe('what an app page may do on this computer', () => { + beforeEach(async () => { + await openCutout() + }) + + function requested(permission: string, requestingUrl: string): boolean { + let granted: boolean | null = null + appsSession.request?.(null, permission, (answer) => (granted = answer), { requestingUrl }) + if (granted === null) throw new Error('The permission request was never answered') + return granted + } + + it.each(['clipboard-sanitized-write', 'fullscreen'])('may use %s', (permission) => { + expect(appsSession.check?.(null, permission, 'https://comfy.org/')).toBe(true) + expect(requested(permission, CUTOUT_URL)).toBe(true) + }) + + it.each([ + 'media', + 'geolocation', + 'notifications', + 'clipboard-read', + 'openExternal', + 'display-capture', + 'usb' + ])('may not use %s', (permission) => { + expect(appsSession.check?.(null, permission, 'https://comfy.org/')).toBe(false) + expect(requested(permission, CUTOUT_URL)).toBe(false) + }) + + it('grants nothing to a frame from another site', () => { + expect(appsSession.check?.(null, 'fullscreen', 'https://player.example/')).toBe(false) + expect(requested('fullscreen', 'https://player.example/embed')).toBe(false) + }) + + it('sends downloads through the downloads shelf', () => { + expect(mocks.attachDownloads).toHaveBeenCalledWith(appsSession) + }) +}) + +describe('staying in step with the panel', () => { + beforeEach(async () => { + await openCutout() + finishLoading() + }) + + it('hides when the panel reloads, and when it crashes', async () => { + panel.webContents.emit('did-start-navigation', { isMainFrame: true, isSameDocument: false }) + expect(hostedPage().visible).toBe(false) + + await openCutout() + expect(hostedPage().visible).toBe(true) + + panel.webContents.emit('render-process-gone', {}, { reason: 'crashed' }) + expect(hostedPage().visible).toBe(false) + }) + + it('stays up while the panel only changes its hash', () => { + panel.webContents.emit('did-start-navigation', { isMainFrame: true, isSameDocument: true }) + + expect(hostedPage().visible).toBe(true) + }) + + it('goes back above a panel that was rebuilt over it', async () => { + hostWindow.contentView.addChildView(panel) + expect(hostWindow.stack.at(-1)).toBe(panel) + + await openCutout() + + expect(hostWindow.stack.at(-1)).toBe(hostedPage()) + }) + + it('does not climb over a popup that opened above it', async () => { + const popup = new FakeView() + hostWindow.contentView.addChildView(popup) + + await openCutout() + + expect(hostWindow.stack.at(-1)).toBe(popup) + }) + + it('hands the panel a frame of the page only while it is on screen', async () => { + hostedPage().webContents.capturePage.mockResolvedValue({ + isEmpty: () => false, + toDataURL: () => 'data:image/png;base64,frame' + }) + expect(await captureAppPage(panelContents())).toBe('data:image/png;base64,frame') + + hideAppPage(panelContents()) + expect(await captureAppPage(panelContents())).toBeNull() + }) + + it('closes the page with its window', () => { + hostWindow.emit('closed') + + expect(hostedPage().webContents.close).toHaveBeenCalled() + expect(liveAppsPages()).toEqual([]) + }) +}) + +describe('isAppsPageSender', () => { + function frame(url: string, routingId = 1): WebFrameMain { + return { url, processId: 4, routingId } as unknown as WebFrameMain + } + + beforeEach(async () => { + await openCutout() + finishLoading() + }) + + it('trusts the top frame of the hosted page while it is on the apps site', () => { + const page = hostedPage().webContents as unknown as WebContents + + expect(isAppsPageSender(page, frame(CUTOUT_URL))).toBe(true) + expect(liveAppsPages()).toEqual([page]) + }) + + it('does not trust a frame embedded in the hosted page', () => { + const page = hostedPage().webContents as unknown as WebContents + + expect(isAppsPageSender(page, frame(CUTOUT_URL, 9))).toBe(false) + }) + + it('does not trust the hosted page once it shows anything outside the apps site', () => { + const page = hostedPage().webContents as unknown as WebContents + hostedPage().webContents.url = 'https://comfy.org/hub/' + + expect(isAppsPageSender(page, frame('https://comfy.org/hub/'))).toBe(false) + expect(liveAppsPages()).toEqual([]) + }) + + it('does not trust another page that happens to show the apps site', () => { + const other = new FakeContents() as unknown as WebContents + + expect(isAppsPageSender(other, frame(CUTOUT_URL))).toBe(false) + expect(isAppsPageSender(panelContents(), frame(CUTOUT_URL))).toBe(false) + }) + + it('does not trust a sender with no frame', () => { + const page = hostedPage().webContents as unknown as WebContents + + expect(isAppsPageSender(page, null)).toBe(false) + }) +}) diff --git a/src/main/apps/appPageView.ts b/src/main/apps/appPageView.ts new file mode 100644 index 000000000..ca3ae4656 --- /dev/null +++ b/src/main/apps/appPageView.ts @@ -0,0 +1,324 @@ +import { WebContentsView, app, session, shell } from 'electron' +import type { Event, Session, WebContents } from 'electron' +import path from 'path' + +import { isAppsListUrl, isInAppsScope, isWebUrl } from '../../shared/appsLocation' +import type { AppsLocation } from '../../shared/appsLocation' +import type { AppPagePlacement, AppPageStatus } from '../../types/ipc' +import { comfyWindows } from '../host/registry' +import type { ComfyWindowEntry } from '../host/registry' +import { attachSessionDownloadHandler } from '../lib/comfyDownloadManager' +import { attachContextMenu } from '../lib/contextMenu' +import { recordShellOpenExternal } from '../lib/e2eOverrides' +import * as mainTelemetry from '../lib/telemetry' +import { COMFY_BG } from '../lib/theme' +import { pageUrlForListedApp } from './appsCatalog' +import { areAppsEnabled, currentAppsLocation } from './appsConfig' +import { registerAppsPage } from './appsPages' + +export const APPS_PARTITION = 'persist:comfy-apps' +export const APP_PAGE_STATUS_CHANNEL = 'apps:page-status-changed' +export const APP_PAGE_CLOSED_CHANNEL = 'apps:page-closed' +export const APP_OPENED_EVENT = 'comfy.desktop.apps.app_opened' +export const APP_PAGE_FAILED_EVENT = 'comfy.desktop.apps.app_page_failed' + +const NAVIGATION_ABORTED = -3 +const FIRST_HTTP_ERROR_STATUS = 400 +const PAGE_PERMISSIONS: ReadonlySet = new Set(['clipboard-sanitized-write', 'fullscreen']) + +type PageFailure = 'http_status' | 'network' | 'crashed' + +interface HostedPage { + view: WebContentsView + appId: string | null + status: AppPageStatus + wanted: boolean + focusWhenReady: boolean + forget: () => void +} + +interface PageHost { + windowKey: number + entry: ComfyWindowEntry +} + +const hostedByWindow = new Map() +const securedSessions = new WeakSet() +const watchedPanels = new WeakSet() + +function originOf(value: string | undefined): string | null { + if (!value) return null + try { + return new URL(value).origin + } catch { + return null + } +} + +function secureSession(appsSession: Session, location: AppsLocation): void { + if (securedSessions.has(appsSession)) return + securedSessions.add(appsSession) + appsSession.setPermissionCheckHandler( + (_contents, permission, requestingOrigin) => + PAGE_PERMISSIONS.has(permission) && originOf(requestingOrigin) === location.origin + ) + appsSession.setPermissionRequestHandler((_contents, permission, callback, details) => { + callback(PAGE_PERMISSIONS.has(permission) && isInAppsScope(location, details.requestingUrl)) + }) + attachSessionDownloadHandler(appsSession) +} + +function openOutside(url: string): void { + if (!isWebUrl(url)) return + if (process.env['E2E'] === '1' && !app.isPackaged) { + recordShellOpenExternal(url) + return + } + void shell.openExternal(url).catch(() => {}) +} + +function panelOf(windowKey: number): WebContents | null { + const panel = comfyWindows.get(windowKey)?.panelView?.webContents + return panel && !panel.isDestroyed() ? panel : null +} + +function isOnScreen(hosted: HostedPage): boolean { + return hosted.wanted && hosted.status === 'ready' +} + +function applyVisibility(hosted: HostedPage): void { + const contents = hosted.view.webContents + if (contents.isDestroyed()) return + hosted.view.setVisible(isOnScreen(hosted)) + if (!isOnScreen(hosted) || !hosted.focusWhenReady) return + hosted.focusWhenReady = false + contents.focus() +} + +function conceal(hosted: HostedPage): void { + hosted.wanted = false + hosted.focusWhenReady = false + applyVisibility(hosted) +} + +function setStatus(windowKey: number, hosted: HostedPage, status: AppPageStatus): void { + if (hosted.status === status) return + hosted.status = status + applyVisibility(hosted) + panelOf(windowKey)?.send(APP_PAGE_STATUS_CHANNEL, status) +} + +function fail(windowKey: number, hosted: HostedPage, reason: PageFailure): void { + if (hosted.status !== 'failed') { + mainTelemetry.capture(APP_PAGE_FAILED_EVENT, { app_id: hosted.appId, reason }) + } + setStatus(windowKey, hosted, 'failed') +} + +function load(windowKey: number, hosted: HostedPage, url: string): void { + setStatus(windowKey, hosted, 'loading') + void hosted.view.webContents.loadURL(url).catch(() => {}) +} + +function closeForList(windowKey: number, hosted: HostedPage, pageStillShowsApp: boolean): void { + if (!pageStillShowsApp) hosted.appId = null + conceal(hosted) + panelOf(windowKey)?.send(APP_PAGE_CLOSED_CHANNEL) +} + +function disposeHostedPage(windowKey: number): void { + const hosted = hostedByWindow.get(windowKey) + if (!hosted) return + hostedByWindow.delete(windowKey) + hosted.forget() + if (!hosted.view.webContents.isDestroyed()) hosted.view.webContents.close() +} + +function createHostedPage({ windowKey, entry }: PageHost, location: AppsLocation): HostedPage { + const appsSession = session.fromPartition(APPS_PARTITION) + secureSession(appsSession, location) + const view = new WebContentsView({ + webPreferences: { + session: appsSession, + preload: path.join(__dirname, '../preload/appsPreload.js'), + nodeIntegration: false, + contextIsolation: true, + sandbox: true + } + }) + view.setBackgroundColor(COMFY_BG) + view.setVisible(false) + entry.window.contentView.addChildView(view) + + const contents = view.webContents + const hosted: HostedPage = { + view, + appId: null, + status: 'loading', + wanted: false, + focusWhenReady: false, + forget: registerAppsPage(contents) + } + const steer = (event: Event, url: string, pageStillShowsApp: boolean): void => { + if (isAppsListUrl(location, url)) { + event.preventDefault() + closeForList(windowKey, hosted, pageStillShowsApp) + return + } + if (isInAppsScope(location, url)) return + event.preventDefault() + openOutside(url) + } + contents.on('will-navigate', (event, url) => steer(event, url, true)) + contents.on('will-redirect', (event, url, _isInPlace, isMainFrame) => { + if (isMainFrame) steer(event, url, false) + }) + contents.setWindowOpenHandler(({ url }) => { + openOutside(url) + return { action: 'deny' } + }) + contents.on('did-navigate', (_event, url, httpResponseCode) => { + if (isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + else if (httpResponseCode >= FIRST_HTTP_ERROR_STATUS) fail(windowKey, hosted, 'http_status') + }) + contents.on('did-navigate-in-page', (_event, url, isMainFrame) => { + if (isMainFrame && isAppsListUrl(location, url)) closeForList(windowKey, hosted, false) + }) + contents.on('dom-ready', () => { + if (hosted.status !== 'loading' || !isInAppsScope(location, contents.getURL())) return + setStatus(windowKey, hosted, 'ready') + }) + contents.on('did-fail-load', (_event, errorCode, _description, _url, isMainFrame) => { + if (isMainFrame && errorCode !== NAVIGATION_ABORTED) fail(windowKey, hosted, 'network') + }) + contents.on('render-process-gone', () => fail(windowKey, hosted, 'crashed')) + attachContextMenu(entry.window, contents) + entry.window.once('closed', () => disposeHostedPage(windowKey)) + + hostedByWindow.set(windowKey, hosted) + return hosted +} + +function hostOf(panel: WebContents): PageHost | null { + for (const [windowKey, entry] of comfyWindows) { + if (entry.panelView?.webContents === panel && !entry.window.isDestroyed()) { + return { windowKey, entry } + } + } + return null +} + +function hostedFor(host: PageHost): HostedPage | null { + const hosted = hostedByWindow.get(host.windowKey) + if (!hosted) return null + if (!hosted.view.webContents.isDestroyed()) return hosted + disposeHostedPage(host.windowKey) + return null +} + +function isMeasure(value: unknown): value is number { + return typeof value === 'number' && Number.isFinite(value) && value >= 0 +} + +function parsePlacement(raw: unknown): AppPagePlacement | null { + if (!raw || typeof raw !== 'object') return null + const { x, y, width, height, cornerRadius, justOpened } = raw as Record + if (!isMeasure(x) || !isMeasure(y) || !isMeasure(width) || !isMeasure(height)) return null + if (!isMeasure(cornerRadius)) return null + return { x, y, width, height, cornerRadius, justOpened: justOpened === true } +} + +function place(entry: ComfyWindowEntry, view: WebContentsView, placement: AppPagePlacement): void { + const panel = entry.panelView + if (!panel) return + const zoom = panel.webContents.getZoomFactor() + const frame = panel.getBounds() + const left = Math.min(Math.round(placement.x * zoom), frame.width) + const top = Math.min(Math.round(placement.y * zoom), frame.height) + view.setBounds({ + x: frame.x + left, + y: frame.y + top, + width: Math.min(Math.round(placement.width * zoom), frame.width - left), + height: Math.min(Math.round(placement.height * zoom), frame.height - top) + }) + view.setBorderRadius(Math.round(placement.cornerRadius * zoom)) +} + +function raiseAbovePanel(entry: ComfyWindowEntry, view: WebContentsView): void { + const panel = entry.panelView + if (!panel) return + const stack = entry.window.contentView.children + if (stack.indexOf(view) > stack.indexOf(panel)) return + entry.window.contentView.removeChildView(view) + entry.window.contentView.addChildView(view) +} + +function concealWhenPanelGoes(windowKey: number, panel: WebContents): void { + if (watchedPanels.has(panel)) return + watchedPanels.add(panel) + const concealHosted = (): void => { + const hosted = hostedByWindow.get(windowKey) + if (hosted) conceal(hosted) + } + panel.on('render-process-gone', concealHosted) + panel.on('destroyed', concealHosted) + panel.on('did-start-navigation', (details) => { + if (details.isMainFrame && !details.isSameDocument) concealHosted() + }) +} + +export async function showAppPage( + panel: WebContents, + appId: unknown, + rawPlacement: unknown +): Promise { + const placement = parsePlacement(rawPlacement) + if (typeof appId !== 'string' || !placement || !(await areAppsEnabled())) return 'failed' + const pageUrl = pageUrlForListedApp(appId) + const host = hostOf(panel) + if (!pageUrl || !host) return 'failed' + const hosted = hostedFor(host) ?? createHostedPage(host, currentAppsLocation()) + concealWhenPanelGoes(host.windowKey, panel) + const retriesFailedPage = placement.justOpened === true && hosted.status === 'failed' + if (hosted.appId !== appId || retriesFailedPage) { + hosted.appId = appId + load(host.windowKey, hosted, pageUrl) + } + if (placement.justOpened) mainTelemetry.capture(APP_OPENED_EVENT, { app_id: appId }) + hosted.wanted = true + hosted.focusWhenReady = hosted.focusWhenReady || placement.justOpened === true + place(host.entry, hosted.view, placement) + raiseAbovePanel(host.entry, hosted.view) + applyVisibility(hosted) + return hosted.status +} + +export function hideAppPage(panel: WebContents): void { + const host = hostOf(panel) + const hosted = host && hostedFor(host) + if (hosted) conceal(hosted) +} + +export async function captureAppPage(panel: WebContents): Promise { + const host = hostOf(panel) + const hosted = host && hostedFor(host) + if (!hosted || !isOnScreen(hosted)) return null + try { + const frame = await hosted.view.webContents.capturePage() + return frame.isEmpty() ? null : frame.toDataURL() + } catch { + return null + } +} + +export function reloadAppPage(panel: WebContents): void { + const host = hostOf(panel) + const hosted = host && hostedFor(host) + const pageUrl = hosted?.appId ? pageUrlForListedApp(hosted.appId) : null + if (!host || !hosted || !pageUrl) return + load(host.windowKey, hosted, pageUrl) +} + +export function _resetForTest(): void { + hostedByWindow.clear() +} diff --git a/src/main/apps/appsCatalog.test.ts b/src/main/apps/appsCatalog.test.ts new file mode 100644 index 000000000..30eae6b20 --- /dev/null +++ b/src/main/apps/appsCatalog.test.ts @@ -0,0 +1,164 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +vi.mock('../lib/fetch', () => ({ fetchJSON: vi.fn() })) +vi.mock('./appsConfig', () => ({ + currentAppsLocation: () => ({ + url: 'https://comfy.org/apps/', + origin: 'https://comfy.org', + scopePath: '/apps/' + }) +})) + +import { fetchJSON } from '../lib/fetch' +import { _resetAppsCatalogForTest, listApps, pageUrlForListedApp } from './appsCatalog' + +const mockedFetchJSON = vi.mocked(fetchJSON) + +function websiteApp(over: Record = {}): Record { + return { + id: 'remove-background', + page: '/apps/remove-background/desktop.html', + title: 'Remove Background', + blurb: 'Cut the subject out of a picture or video.', + cover: '/catalog-media/cover-1/v2', + order: 3, + examples: [], + source: 'catalog', + ...over + } +} + +beforeEach(() => { + vi.clearAllMocks() + _resetAppsCatalogForTest() +}) + +afterEach(() => { + vi.useRealTimers() +}) + +describe('the apps Desktop lists', () => { + it("come from the website's own list, in its order, with each cover on the site", async () => { + mockedFetchJSON.mockResolvedValue([ + websiteApp(), + websiteApp({ id: 'sdr-to-hdr', page: '/apps/sdr-to-hdr/desktop.html', title: 'SDR to HDR' }) + ]) + + expect(await listApps()).toEqual([ + { + id: 'remove-background', + title: 'Remove Background', + description: 'Cut the subject out of a picture or video.', + coverUrl: 'https://comfy.org/catalog-media/cover-1/v2' + }, + { + id: 'sdr-to-hdr', + title: 'SDR to HDR', + description: 'Cut the subject out of a picture or video.', + coverUrl: 'https://comfy.org/catalog-media/cover-1/v2' + } + ]) + expect(mockedFetchJSON).toHaveBeenCalledWith('https://comfy.org/apps/apps.json', { + refresh: true + }) + }) + + it("open at the page the website names, or at the list's own route for an app it draws itself", async () => { + mockedFetchJSON.mockResolvedValue([ + websiteApp(), + websiteApp({ id: 'upscale', page: undefined, title: 'Upscale 4x' }) + ]) + await listApps() + + expect(pageUrlForListedApp('remove-background')).toBe( + 'https://comfy.org/apps/remove-background/desktop.html' + ) + expect(pageUrlForListedApp('upscale')).toBe('https://comfy.org/apps/#/upscale') + expect(pageUrlForListedApp('never-listed')).toBeNull() + }) + + it('show a card without a picture when the cover is missing or on another site', async () => { + mockedFetchJSON.mockResolvedValue([ + websiteApp({ id: 'no-cover', cover: undefined }), + websiteApp({ id: 'foreign-cover', cover: 'https://tracker.example/pixel.png' }), + websiteApp({ id: 'no-blurb', blurb: 7 }) + ]) + + expect(await listApps()).toEqual([ + expect.objectContaining({ id: 'no-cover', coverUrl: null }), + expect.objectContaining({ id: 'foreign-cover', coverUrl: null }), + expect.objectContaining({ id: 'no-blurb', description: '' }) + ]) + }) + + it.each([ + ['has no title', websiteApp({ title: ' ' })], + ['has an id that is not a slug', websiteApp({ id: '../login' })], + ['has no id', websiteApp({ id: undefined })], + ['opens a page outside the apps site', websiteApp({ page: 'https://evil.example/apps/x' })], + ['opens the rest of comfy.org', websiteApp({ page: '/login/' })], + ['opens the list itself', websiteApp({ page: '/apps/' })], + ['names a page that is not an address', websiteApp({ page: 42 })], + ['is not an object', 'remove-background'] + ])('leave out an entry that %s', async (_why, entry) => { + mockedFetchJSON.mockResolvedValue([entry, websiteApp({ id: 'kept', title: 'Kept' })]) + + expect((await listApps()).map((app) => app.id)).toEqual(['kept']) + }) + + it('keep the first of two entries with the same id', async () => { + mockedFetchJSON.mockResolvedValue([ + websiteApp({ title: 'First' }), + websiteApp({ title: 'Second' }) + ]) + + expect((await listApps()).map((app) => app.title)).toEqual(['First']) + }) + + it('are asked for once while a request is in flight, and again only after five minutes', async () => { + vi.useFakeTimers() + mockedFetchJSON.mockResolvedValue([websiteApp()]) + + await Promise.all([listApps(), listApps()]) + await listApps() + expect(mockedFetchJSON).toHaveBeenCalledTimes(1) + + vi.advanceTimersByTime(5 * 60 * 1000 + 1) + await listApps() + expect(mockedFetchJSON).toHaveBeenCalledTimes(2) + }) + + it('stay as last seen when the website cannot be reached later', async () => { + vi.useFakeTimers() + mockedFetchJSON.mockResolvedValueOnce([websiteApp()]) + await listApps() + vi.advanceTimersByTime(5 * 60 * 1000 + 1) + mockedFetchJSON.mockRejectedValueOnce(new Error('offline')) + + expect((await listApps()).map((app) => app.id)).toEqual(['remove-background']) + expect(pageUrlForListedApp('remove-background')).not.toBeNull() + }) + + it('fail to load when the website has never answered', async () => { + mockedFetchJSON.mockRejectedValue(new Error('offline')) + + await expect(listApps()).rejects.toThrow('offline') + }) + + it('fail to load when the website answers with something that is not a list', async () => { + mockedFetchJSON.mockResolvedValue({ apps: [] }) + + await expect(listApps()).rejects.toThrow('not a list') + }) + + it('fail to load when the website takes longer than eight seconds', async () => { + vi.useFakeTimers() + mockedFetchJSON.mockReturnValue(new Promise(() => {})) + const pending = listApps() + const outcome = expect(pending).rejects.toThrow('timed out') + + await vi.advanceTimersByTimeAsync(8000) + + await outcome + }) +}) diff --git a/src/main/apps/appsCatalog.ts b/src/main/apps/appsCatalog.ts new file mode 100644 index 000000000..fcfa48f36 --- /dev/null +++ b/src/main/apps/appsCatalog.ts @@ -0,0 +1,141 @@ +import { isAppsListUrl, isInAppsScope } from '../../shared/appsLocation' +import type { AppsLocation } from '../../shared/appsLocation' +import type { AppCard } from '../../types/ipc' +import { fetchJSON } from '../lib/fetch' +import { currentAppsLocation } from './appsConfig' + +const LIST_FILE = 'apps.json' +const FETCH_TIMEOUT_MS = 8000 +const CACHE_TTL_MS = 5 * 60 * 1000 +const MAX_APPS = 200 +const MAX_TEXT_LENGTH = 4096 +const APP_ID_PATTERN = /^[a-z0-9][a-z0-9_-]{0,127}$/i + +interface ListedApp { + card: AppCard + pageUrl: string +} + +type JsonRecord = Record + +function asRecord(value: unknown): JsonRecord | null { + return value && typeof value === 'object' && !Array.isArray(value) ? (value as JsonRecord) : null +} + +function text(value: unknown): string | null { + if (typeof value !== 'string' || value.length > MAX_TEXT_LENGTH) return null + return value.trim() || null +} + +function resolved(value: unknown, base: string): URL | null { + if (typeof value !== 'string' || value.length > MAX_TEXT_LENGTH) return null + try { + return new URL(value, base) + } catch { + return null + } +} + +export function appsListUrl(location: AppsLocation): string { + return new URL(LIST_FILE, location.url).toString() +} + +function coverUrlFor(cover: unknown, location: AppsLocation): string | null { + const url = resolved(cover, location.url) + return url && url.origin === location.origin ? url.href : null +} + +function pageUrlFor(id: string, page: unknown, location: AppsLocation): string | null { + const url = page === undefined ? resolved(`#/${id}`, location.url) : resolved(page, location.url) + if (!url || !isInAppsScope(location, url.href) || isAppsListUrl(location, url.href)) return null + return url.href +} + +export function toListedApp(entry: unknown, location: AppsLocation): ListedApp | null { + const record = asRecord(entry) + const id = text(record?.id) + const title = text(record?.title) + if (!record || !id || !title || !APP_ID_PATTERN.test(id)) return null + const pageUrl = pageUrlFor(id, record.page, location) + if (!pageUrl) return null + return { + pageUrl, + card: { + id, + title, + description: text(record.blurb) ?? '', + coverUrl: coverUrlFor(record.cover, location) + } + } +} + +export function parseAppsList(raw: unknown, location: AppsLocation): ListedApp[] { + if (!Array.isArray(raw)) throw new Error('The apps list is not a list') + const seen = new Set() + const apps: ListedApp[] = [] + for (const entry of raw.slice(0, MAX_APPS)) { + const app = toListedApp(entry, location) + if (!app || seen.has(app.card.id)) continue + seen.add(app.card.id) + apps.push(app) + } + return apps +} + +function fetchList(url: string): Promise { + return Promise.race([ + fetchJSON(url, { refresh: true }), + new Promise((_resolve, reject) => { + setTimeout( + () => reject(new Error('The apps list request timed out')), + FETCH_TIMEOUT_MS + ).unref?.() + }) + ]) +} + +interface CacheEntry { + listUrl: string + loadedAt: number + apps: ListedApp[] +} + +let cache: CacheEntry | null = null +let inFlight: { listUrl: string; request: Promise } | null = null + +function loadApps(): Promise { + const location = currentAppsLocation() + const listUrl = appsListUrl(location) + if (cache && cache.listUrl === listUrl && Date.now() - cache.loadedAt < CACHE_TTL_MS) { + return Promise.resolve(cache.apps) + } + if (inFlight?.listUrl === listUrl) return inFlight.request + const request = fetchList(listUrl) + .then((raw) => { + const apps = parseAppsList(raw, location) + cache = { listUrl, loadedAt: Date.now(), apps } + return apps + }) + .catch((err: unknown) => { + if (cache?.listUrl === listUrl) return cache.apps + throw err + }) + .finally(() => { + if (inFlight?.request === request) inFlight = null + }) + inFlight = { listUrl, request } + return request +} + +export async function listApps(): Promise { + return (await loadApps()).map((app) => app.card) +} + +export function pageUrlForListedApp(id: string): string | null { + return cache?.apps.find((app) => app.card.id === id)?.pageUrl ?? null +} + +export function _resetAppsCatalogForTest(): void { + cache = null + inFlight = null +} diff --git a/src/main/apps/appsConfig.ts b/src/main/apps/appsConfig.ts new file mode 100644 index 000000000..c9fbcaa95 --- /dev/null +++ b/src/main/apps/appsConfig.ts @@ -0,0 +1,18 @@ +import { app } from 'electron' + +import { resolveAppsLocation } from '../../shared/appsLocation' +import type { AppsLocation } from '../../shared/appsLocation' +import { isAppsFlagOn } from './appsFlag' + +function developerAppsUrl(): string | null { + if (app.isPackaged) return null + return process.env['COMFY_APPS_URL']?.trim() || null +} + +export function currentAppsLocation(): AppsLocation { + return resolveAppsLocation(developerAppsUrl()) +} + +export async function areAppsEnabled(): Promise { + return developerAppsUrl() !== null || (await isAppsFlagOn()) +} diff --git a/src/main/apps/appsFlag.ts b/src/main/apps/appsFlag.ts new file mode 100644 index 000000000..15ece9d19 --- /dev/null +++ b/src/main/apps/appsFlag.ts @@ -0,0 +1,16 @@ +import { makeOpsFlag } from '../lib/opsFlag' + +export const APPS_FLAG_KEY = 'desktop_apps_enabled' + +const flag = makeOpsFlag({ + key: APPS_FLAG_KEY, + fallback: false, + parse: (value) => value === true || value === 'on', + persist: true +}) + +export const initAppsFlag = flag.init + +export const isAppsFlagOn = flag.get + +export const _resetForTest = flag._resetForTest diff --git a/src/main/apps/appsPages.ts b/src/main/apps/appsPages.ts new file mode 100644 index 000000000..76296e906 --- /dev/null +++ b/src/main/apps/appsPages.ts @@ -0,0 +1,35 @@ +import type { WebContents, WebFrameMain } from 'electron' + +import { isInAppsScope } from '../../shared/appsLocation' +import { currentAppsLocation } from './appsConfig' + +const hostedPages = new Set() + +export function registerAppsPage(contents: WebContents): () => void { + hostedPages.add(contents) + return () => { + hostedPages.delete(contents) + } +} + +export function isAppsPageSender(sender: WebContents, senderFrame: WebFrameMain | null): boolean { + if (!senderFrame || !hostedPages.has(sender)) return false + const mainFrame = sender.mainFrame + if ( + senderFrame.processId !== mainFrame.processId || + senderFrame.routingId !== mainFrame.routingId + ) + return false + return isInAppsScope(currentAppsLocation(), senderFrame.url) +} + +export function liveAppsPages(): WebContents[] { + const location = currentAppsLocation() + return [...hostedPages].filter( + (contents) => !contents.isDestroyed() && isInAppsScope(location, contents.getURL()) + ) +} + +export function _resetForTest(): void { + hostedPages.clear() +} diff --git a/src/main/host/panelView.ts b/src/main/host/panelView.ts index 0d263411a..4d7efbbf2 100644 --- a/src/main/host/panelView.ts +++ b/src/main/host/panelView.ts @@ -22,6 +22,7 @@ import type { BodyMode, ComfyPanelKey, ComfyWindowEntry } from './registry' const DASHBOARD_PAGE_PANELS: ReadonlyMap = new Map([ ['home', 'comfy'], + ['apps', 'comfy'], ['assets', 'comfy'], ['chooser', 'comfy'], ['benchmarks', 'benchmarks'], diff --git a/src/main/index.ts b/src/main/index.ts index 2396fdc83..eb6639141 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -124,6 +124,7 @@ import { initExperiments } from './lib/experiments' import { initCloudFreeRuns } from './lib/cloudFreeRuns' import { initCoreBetaFlags, refreshCoreBetaFlags } from './lib/coreBetaCampaignFlags' import { initEmbeddedSessionFlag } from './lib/embeddedSessionFlag' +import { initAppsFlag } from './apps/appsFlag' import { initWorkspaceMintSwitchFlag } from './lib/workspaceMintSwitchFlag' import { getCloudSession } from './devplatform/session' import { initStaffFlagTargeting } from './lib/staffFlagTargeting' @@ -1022,6 +1023,8 @@ if (app.isPackaged && !app.requestSingleInstanceLock()) { void initEmbeddedSessionFlag({ distinctId: installationId }) + void initAppsFlag({ distinctId: installationId }) + void initWorkspaceMintSwitchFlag({ distinctId: installationId }) getCloudSession().resumeRevocations() diff --git a/src/main/lib/embeddedSession.test.ts b/src/main/lib/embeddedSession.test.ts index 84e0188d1..34f04c2fc 100644 --- a/src/main/lib/embeddedSession.test.ts +++ b/src/main/lib/embeddedSession.test.ts @@ -10,13 +10,20 @@ const mocks = vi.hoisted(() => ({ mint: vi.fn(), partnerMint: vi.fn(), current: 'ws-1' as string | null, - entries: [] as unknown[] + entries: [] as unknown[], + appsEnabled: false, + appsPages: [] as unknown[] })) vi.mock('./telemetry', () => ({ emit: vi.fn() })) vi.mock('./embeddedSessionFlag', () => ({ isEmbeddedSessionEnabled: async () => (mocks.flagRead ? mocks.flagRead : mocks.enabled) })) +vi.mock('../apps/appsConfig', () => ({ areAppsEnabled: async () => mocks.appsEnabled })) +vi.mock('../apps/appsPages', () => ({ + isAppsPageSender: (sender: unknown) => mocks.appsPages.includes(sender), + liveAppsPages: () => mocks.appsPages +})) vi.mock('../devplatform/session', () => ({ getCloudSession: () => ({ getGrantAccessToken: mocks.getGrantAccessToken, @@ -104,11 +111,26 @@ function view(comfyUrl: string, options: ViewOptions = {}) { return { event, webContents } } +function appsPage() { + const mainFrame = { processId: 9, routingId: 1, url: 'https://comfy.org/apps/' } + const webContents = { + mainFrame, + isDestroyed: () => false, + getURL: () => mainFrame.url, + send: vi.fn() + } + mocks.appsPages.push(webContents) + const event = { sender: webContents, senderFrame: mainFrame } as unknown as EmbeddedSessionSender + return { event, webContents } +} + beforeEach(() => { mocks.enabled = true mocks.generation = 0 mocks.flagRead = null mocks.entries = [] + mocks.appsEnabled = false + mocks.appsPages = [] mocks.getGrantAccessToken.mockReset().mockResolvedValue(ACCESS) mocks.mint.mockReset().mockImplementation(async (_grant: string, workspaceId: string) => ({ kind: 'minted', @@ -719,3 +741,88 @@ describe('billingHandoffForSender', () => { expect(fetch).not.toHaveBeenCalled() }) }) + +describe('the hosted Apps page', () => { + const SIGNED_IN = { + status: 'signed_in', + userId: 'user-1', + email: 'a@example.com', + workspaceId: 'ws-1' + } + + it('gets the session while Apps is on, whatever the local-view flag says', async () => { + mocks.appsEnabled = true + mocks.enabled = false + const { event } = appsPage() + + await expect(stateForSender(event)).resolves.toEqual(SIGNED_IN) + await expect(workspaceTokenForSender(event, 'ws-1')).resolves.toBe(MINTED) + await expect(partnerNodeTokenForSender(event, 'ws-1')).resolves.toBe(partnerNodeFor('ws-1')) + expect(disabledEvents()).toEqual([]) + }) + + it('gets nothing while Apps is switched off', async () => { + const { event } = appsPage() + + await expect(stateForSender(event)).resolves.toEqual({ status: 'disabled' }) + await expect(workspaceTokenForSender(event, 'ws-1')).resolves.toBeNull() + await expect(partnerNodeTokenForSender(event, 'ws-1')).resolves.toBeNull() + expect(mocks.getGrantAccessToken).not.toHaveBeenCalled() + }) + + it('gets no token for a workspace Desktop is not in', async () => { + mocks.appsEnabled = true + const { event } = appsPage() + + await expect(workspaceTokenForSender(event, 'ws-2')).resolves.toBeNull() + await expect(partnerNodeTokenForSender(event, 'ws-2')).resolves.toBeNull() + }) + + it('cannot move Desktop to another workspace', async () => { + mocks.appsEnabled = true + const { event } = appsPage() + const switchWorkspace = vi.fn() + + await expect(switchWorkspaceForSender(event, 'ws-2', switchWorkspace)).resolves.toEqual( + SIGNED_IN + ) + expect(switchWorkspace).not.toHaveBeenCalled() + }) + + it('gets no billing handoff, which only a local ComfyUI view may ask for', async () => { + mocks.appsEnabled = true + vi.stubGlobal('fetch', vi.fn()) + const { event } = appsPage() + + await expect( + billingHandoffForSender(event, 'ws-1', 'billing_web', undefined, undefined, '1.2.3') + ).resolves.toEqual({ status: 'disabled' }) + expect(fetch).not.toHaveBeenCalled() + vi.unstubAllGlobals() + }) + + it('hears about a sign-in or sign-out, also while local views are left out', async () => { + mocks.appsEnabled = true + mocks.enabled = false + const page = appsPage() + const local = view('http://127.0.0.1:8000') + + await broadcastEmbeddedSessionChanged() + + expect(page.webContents.send).toHaveBeenCalledWith(EMBEDDED_SESSION_CHANNELS.changed, SIGNED_IN) + expect(local.webContents.send).not.toHaveBeenCalled() + }) + + it('hears nothing while Apps is switched off', async () => { + const page = appsPage() + const local = view('http://127.0.0.1:8000') + + await broadcastEmbeddedSessionChanged() + + expect(page.webContents.send).not.toHaveBeenCalled() + expect(local.webContents.send).toHaveBeenCalledWith( + EMBEDDED_SESSION_CHANNELS.changed, + SIGNED_IN + ) + }) +}) diff --git a/src/main/lib/embeddedSession.ts b/src/main/lib/embeddedSession.ts index e0355204b..200d9d7dd 100644 --- a/src/main/lib/embeddedSession.ts +++ b/src/main/lib/embeddedSession.ts @@ -12,6 +12,8 @@ import type { ComfyDesktop2BillingHandoffIntent, ComfyDesktop2BillingHandoffReceiver } from '../../types/comfyDesktopBridge' +import { areAppsEnabled } from '../apps/appsConfig' +import { isAppsPageSender, liveAppsPages } from '../apps/appsPages' import { isEmbeddedSessionEnabled } from './embeddedSessionFlag' import * as mainTelemetry from './telemetry' import { getViewWorkspaceGrants } from './viewWorkspaceGrants' @@ -104,10 +106,19 @@ async function sessionState( } } +type SessionGate = () => Promise + +function gateForSender(event: EmbeddedSessionSender): SessionGate | null { + if (isTrustedSender(event)) return enabledForLocalView + if (isAppsPageSender(event.sender, event.senderFrame)) return areAppsEnabled + return null +} + export async function stateForSender( event: EmbeddedSessionSender ): Promise { - return isTrustedSender(event) ? sessionState(enabledForLocalView) : DISABLED + const gate = gateForSender(event) + return gate ? sessionState(gate) : DISABLED } export async function workspaceTokenForSender( @@ -115,8 +126,9 @@ export async function workspaceTokenForSender( workspaceId: unknown ): Promise { if (typeof workspaceId !== 'string' || workspaceId === '') return null - if (!isTrustedSender(event)) return null - return getViewWorkspaceGrants().tokenFor(event.sender, workspaceId, enabledForLocalView) + const gate = gateForSender(event) + if (!gate) return null + return getViewWorkspaceGrants().tokenFor(event.sender, workspaceId, gate) } export async function partnerNodeTokenForSender( @@ -124,12 +136,9 @@ export async function partnerNodeTokenForSender( workspaceId: unknown ): Promise { if (typeof workspaceId !== 'string' || workspaceId === '') return null - if (!isTrustedSender(event)) return null - return getViewWorkspaceGrants().partnerNodeTokenFor( - event.sender, - workspaceId, - enabledForLocalView - ) + const gate = gateForSender(event) + if (!gate) return null + return getViewWorkspaceGrants().partnerNodeTokenFor(event.sender, workspaceId, gate) } export async function switchWorkspaceForSender( @@ -138,6 +147,7 @@ export async function switchWorkspaceForSender( switchWorkspace: (workspaceId: string) => Promise ): Promise { const state = await stateForSender(event) + if (!isTrustedSender(event)) return state if (state.status !== 'signed_in' || typeof workspaceId !== 'string' || workspaceId === '') return state const grants = getViewWorkspaceGrants() @@ -215,12 +225,18 @@ let broadcastGeneration = 0 export async function broadcastEmbeddedSessionChanged(): Promise { const generation = ++broadcastGeneration - const state = await sessionState() - if (generation !== broadcastGeneration || state.status === 'disabled') return - for (const entry of comfyWindows.values()) { - const contents = entry.comfyView.webContents - if (contents.isDestroyed() || !servesEntry(entry, contents.getURL())) continue - contents.send(EMBEDDED_SESSION_CHANNELS.changed, state) + const [localState, appsState] = await Promise.all([sessionState(), sessionState(areAppsEnabled)]) + if (generation !== broadcastGeneration) return + if (localState.status !== 'disabled') { + for (const entry of comfyWindows.values()) { + const contents = entry.comfyView.webContents + if (contents.isDestroyed() || !servesEntry(entry, contents.getURL())) continue + contents.send(EMBEDDED_SESSION_CHANNELS.changed, localState) + } + } + if (appsState.status === 'disabled') return + for (const contents of liveAppsPages()) { + contents.send(EMBEDDED_SESSION_CHANNELS.changed, appsState) } } diff --git a/src/main/lib/ipc/index.ts b/src/main/lib/ipc/index.ts index cacb24984..4bfd4c3e7 100644 --- a/src/main/lib/ipc/index.ts +++ b/src/main/lib/ipc/index.ts @@ -38,6 +38,7 @@ import { registerDevPlatformHandlers } from './registerDevPlatformHandlers' import { registerAssetsHandlers } from '../assets' import { registerEmbeddedSessionHandlers } from './registerEmbeddedSessionHandlers' import { registerCatalogHandlers } from './registerCatalogHandlers' +import { registerAppsHandlers } from './registerAppsHandlers' import { reconcileAdoptedSettings } from '../desktopAdopt' import { finalizeComfyBuilderRecovery, @@ -283,5 +284,6 @@ export function register(callbacks: RegisterCallbacks = {}): Promise { registerAssetsHandlers() registerEmbeddedSessionHandlers() registerCatalogHandlers() + registerAppsHandlers() return startupRecovery.then(() => undefined) } diff --git a/src/main/lib/ipc/registerAppsHandlers.ts b/src/main/lib/ipc/registerAppsHandlers.ts new file mode 100644 index 000000000..bd3e6f7b2 --- /dev/null +++ b/src/main/lib/ipc/registerAppsHandlers.ts @@ -0,0 +1,25 @@ +import { ipcMain } from 'electron' + +import { captureAppPage, hideAppPage, reloadAppPage, showAppPage } from '../../apps/appPageView' +import { listApps } from '../../apps/appsCatalog' +import { areAppsEnabled } from '../../apps/appsConfig' +import { isPanelSender } from '../../host/registry' + +export function registerAppsHandlers(): void { + ipcMain.handle('apps:get-enabled', () => areAppsEnabled()) + + ipcMain.handle('apps:list', async (event) => { + if (!isPanelSender(event.sender) || !(await areAppsEnabled())) return [] + return listApps() + }) + + ipcMain.handle('apps:show-page', (event, appId: unknown, placement: unknown) => + showAppPage(event.sender, appId, placement) + ) + + ipcMain.handle('apps:capture-page', (event) => captureAppPage(event.sender)) + + ipcMain.on('apps:hide-page', (event) => hideAppPage(event.sender)) + + ipcMain.on('apps:reload-page', (event) => reloadAppPage(event.sender)) +} diff --git a/src/main/lib/ipc/registerDevPlatformHandlers.test.ts b/src/main/lib/ipc/registerDevPlatformHandlers.test.ts index 77cad6b79..5fb65977f 100644 --- a/src/main/lib/ipc/registerDevPlatformHandlers.test.ts +++ b/src/main/lib/ipc/registerDevPlatformHandlers.test.ts @@ -57,6 +57,8 @@ vi.mock('../viewWorkspaceGrants', () => ({ }) })) +vi.mock('../../apps/appsConfig', () => ({ areAppsEnabled: async () => false })) + vi.mock('electron', () => ({ BrowserWindow: { getAllWindows: mocks.getAllWindows }, ipcMain: { handle: mocks.handle }, diff --git a/src/main/lib/ipc/registerEmbeddedSessionHandlers.test.ts b/src/main/lib/ipc/registerEmbeddedSessionHandlers.test.ts index 7ff329d35..7c681ebdf 100644 --- a/src/main/lib/ipc/registerEmbeddedSessionHandlers.test.ts +++ b/src/main/lib/ipc/registerEmbeddedSessionHandlers.test.ts @@ -7,6 +7,8 @@ const mocks = vi.hoisted(() => ({ emit: vi.fn(), handle: vi.fn(), signInToCloud: vi.fn(), + signOutOfCloud: vi.fn(), + isTrustedSender: vi.fn(), stateForSender: vi.fn(), switchWorkspaceForSender: vi.fn(), switchCloudWorkspace: vi.fn() @@ -32,6 +34,7 @@ vi.mock('../embeddedSession', () => ({ issueBillingHandoff: 'desktop2-auth:issue-billing-handoff' }, billingHandoffForSender: vi.fn(), + isTrustedSender: mocks.isTrustedSender, partnerNodeTokenForSender: vi.fn(), stateForSender: mocks.stateForSender, switchWorkspaceForSender: mocks.switchWorkspaceForSender, @@ -40,7 +43,7 @@ vi.mock('../embeddedSession', () => ({ vi.mock('./registerDevPlatformHandlers', () => ({ signInToCloud: mocks.signInToCloud, - signOutOfCloud: vi.fn(), + signOutOfCloud: mocks.signOutOfCloud, switchCloudWorkspace: mocks.switchCloudWorkspace })) @@ -219,3 +222,37 @@ describe('desktop2-auth:issue-billing-handoff', () => { ) }) }) + +describe('desktop2-auth:sign-out', () => { + type SignOut = (event: unknown) => Promise + + function signOut(): SignOut { + const call = mocks.handle.mock.calls.find(([channel]) => channel === 'desktop2-auth:sign-out') + expect(call).toBeDefined() + return call![1] as SignOut + } + + it('signs Desktop out for a local ComfyUI view', async () => { + mocks.isTrustedSender.mockReturnValue(true) + mocks.stateForSender.mockResolvedValueOnce(SIGNED_IN).mockResolvedValueOnce(SIGNED_OUT) + + await expect(signOut()({ sender: {} })).resolves.toEqual(SIGNED_OUT) + expect(mocks.signOutOfCloud).toHaveBeenCalledTimes(1) + }) + + it('leaves Desktop signed in when the hosted Apps page asks', async () => { + mocks.isTrustedSender.mockReturnValue(false) + mocks.stateForSender.mockResolvedValue(SIGNED_IN) + + await expect(signOut()({ sender: {} })).resolves.toEqual(SIGNED_IN) + expect(mocks.signOutOfCloud).not.toHaveBeenCalled() + }) + + it('does nothing for a view Desktop does not share its session with', async () => { + mocks.isTrustedSender.mockReturnValue(false) + mocks.stateForSender.mockResolvedValue({ status: 'disabled' }) + + await expect(signOut()({ sender: {} })).resolves.toEqual({ status: 'disabled' }) + expect(mocks.signOutOfCloud).not.toHaveBeenCalled() + }) +}) diff --git a/src/main/lib/ipc/registerEmbeddedSessionHandlers.ts b/src/main/lib/ipc/registerEmbeddedSessionHandlers.ts index 2ba669fcd..82ef9b02d 100644 --- a/src/main/lib/ipc/registerEmbeddedSessionHandlers.ts +++ b/src/main/lib/ipc/registerEmbeddedSessionHandlers.ts @@ -5,6 +5,7 @@ import type { ComfyDesktop2AuthState } from '../../../types/comfyDesktopBridge' import { billingHandoffForSender, EMBEDDED_SESSION_CHANNELS, + isTrustedSender, partnerNodeTokenForSender, stateForSender, switchWorkspaceForSender, @@ -38,7 +39,8 @@ export function registerEmbeddedSessionHandlers(): void { ipcMain.handle( EMBEDDED_SESSION_CHANNELS.signOut, async (event): Promise => { - if ((await stateForSender(event)).status === 'disabled') return { status: 'disabled' } + const state = await stateForSender(event) + if (state.status === 'disabled' || !isTrustedSender(event)) return state await signOutOfCloud(findEntryByComfySender(event.sender) ?? undefined) return stateForSender(event) } diff --git a/src/preload/api.ts b/src/preload/api.ts index 5ec4d16cf..3d5b6b50e 100644 --- a/src/preload/api.ts +++ b/src/preload/api.ts @@ -1,6 +1,6 @@ import { ipcRenderer, webUtils } from 'electron' import type { IpcRendererEvent } from 'electron' -import type { ElectronApi, ResolvedTheme, AdoptPromptRequest } from '../types/ipc' +import type { AppPageStatus, ElectronApi, ResolvedTheme, AdoptPromptRequest } from '../types/ipc' export function buildElectronApi(): ElectronApi { return { @@ -108,6 +108,22 @@ export function buildElectronApi(): ElectronApi { listHomeWorkflows: () => ipcRenderer.invoke('catalog:list-home-workflows'), openCatalogWorkflow: (id) => ipcRenderer.invoke('catalog:open-workflow', id), openCatalogWorkflowHub: () => ipcRenderer.invoke('catalog:open-workflow-hub'), + getAppsEnabled: () => ipcRenderer.invoke('apps:get-enabled'), + listApps: () => ipcRenderer.invoke('apps:list'), + showAppPage: (appId, placement) => ipcRenderer.invoke('apps:show-page', appId, placement), + captureAppPage: () => ipcRenderer.invoke('apps:capture-page'), + hideAppPage: () => ipcRenderer.send('apps:hide-page'), + reloadAppPage: () => ipcRenderer.send('apps:reload-page'), + onAppPageStatusChanged: (callback) => { + const handler = (_event: IpcRendererEvent, status: AppPageStatus): void => callback(status) + ipcRenderer.on('apps:page-status-changed', handler) + return () => ipcRenderer.removeListener('apps:page-status-changed', handler) + }, + onAppPageClosed: (callback) => { + const handler = (): void => callback() + ipcRenderer.on('apps:page-closed', handler) + return () => ipcRenderer.removeListener('apps:page-closed', handler) + }, getBuildStorageSnapshot: () => ipcRenderer.invoke('comfy-window:build-storage-snapshot'), resolveStartupRestoreReveal: (result) => ipcRenderer.send('comfy-window:startup-restore-reveal', { result }), diff --git a/src/preload/appsPreload.ts b/src/preload/appsPreload.ts new file mode 100644 index 000000000..7015af8d0 --- /dev/null +++ b/src/preload/appsPreload.ts @@ -0,0 +1,23 @@ +import { contextBridge, ipcRenderer } from 'electron' +import type { IpcRendererEvent } from 'electron' +import type { + ComfyDesktop2AuthState, + ComfyDesktopHostedAuthBridge +} from '../types/comfyDesktopBridge' + +const auth: ComfyDesktopHostedAuthBridge = { + getState: () => ipcRenderer.invoke('desktop2-auth:get-state'), + getWorkspaceToken: (workspaceId) => + ipcRenderer.invoke('desktop2-auth:get-workspace-token', workspaceId), + getPartnerNodeToken: (workspaceId) => + ipcRenderer.invoke('desktop2-auth:get-partner-node-token', workspaceId), + requestSignIn: () => ipcRenderer.invoke('desktop2-auth:request-sign-in'), + onChanged: (callback) => { + const handler = (_event: IpcRendererEvent, state: ComfyDesktop2AuthState): void => + callback(state) + ipcRenderer.on('desktop2-auth:changed', handler) + return () => ipcRenderer.removeListener('desktop2-auth:changed', handler) + } +} + +contextBridge.exposeInMainWorld('comfyDesktopAuth', auth) diff --git a/src/renderer/src/composables/useCoveredByPage.test.ts b/src/renderer/src/composables/useCoveredByPage.test.ts new file mode 100644 index 000000000..1829c4bff --- /dev/null +++ b/src/renderer/src/composables/useCoveredByPage.test.ts @@ -0,0 +1,73 @@ +import { afterEach, beforeAll, describe, expect, it } from 'vitest' +import { page } from 'vitest/browser' + +import { isCoveredByPage } from './useCoveredByPage' + +beforeAll(() => page.viewport(1280, 800)) + +const added: HTMLElement[] = [] + +function place(css: string, parent: HTMLElement = document.body): HTMLElement { + const element = document.createElement('div') + element.style.cssText = `position: fixed; ${css}` + parent.append(element) + added.push(element) + return element +} + +afterEach(() => { + for (const element of added.splice(0)) element.remove() +}) + +describe('isCoveredByPage', () => { + it('is false for an area with only its own content in it', () => { + const area = place('left: 300px; top: 20px; width: 600px; height: 400px') + place('left: 320px; top: 40px; width: 100px; height: 100px', area) + + expect(isCoveredByPage(area)).toBe(false) + }) + + it('is false for a rounded area whose corners show the surface behind it', () => { + const surface = place('left: 280px; top: 0; width: 640px; height: 440px') + const area = place( + 'left: 300px; top: 20px; width: 600px; height: 400px; border-radius: 14px', + surface + ) + + expect(isCoveredByPage(area)).toBe(false) + }) + + it('is true when a small menu sits anywhere over the area', () => { + const area = place('left: 300px; top: 20px; width: 600px; height: 400px') + place('left: 610px; top: 230px; width: 90px; height: 60px; z-index: 5') + + expect(isCoveredByPage(area)).toBe(true) + }) + + it('is true when something overlaps only the very edge of the area', () => { + const area = place('left: 300px; top: 20px; width: 600px; height: 400px') + place('left: 100px; top: 380px; width: 202px; height: 41px; z-index: 5') + + expect(isCoveredByPage(area)).toBe(true) + }) + + it('is false for something that stops just short of the area', () => { + const area = place('left: 300px; top: 20px; width: 600px; height: 400px') + place('left: 100px; top: 380px; width: 200px; height: 40px; z-index: 5') + + expect(isCoveredByPage(area)).toBe(false) + }) + + it('is true under a full-window dialog backdrop', () => { + const area = place('left: 300px; top: 20px; width: 600px; height: 400px') + place('inset: 0; z-index: 50') + + expect(isCoveredByPage(area)).toBe(true) + }) + + it('is false for an area with no size', () => { + const area = place('left: 300px; top: 20px; width: 0; height: 0') + + expect(isCoveredByPage(area)).toBe(false) + }) +}) diff --git a/src/renderer/src/composables/useCoveredByPage.ts b/src/renderer/src/composables/useCoveredByPage.ts new file mode 100644 index 000000000..7481cfc3f --- /dev/null +++ b/src/renderer/src/composables/useCoveredByPage.ts @@ -0,0 +1,61 @@ +import { onBeforeUnmount, onMounted, ref, toValue } from 'vue' +import type { MaybeRefOrGetter, Ref } from 'vue' + +const PROBE_SPACING_PX = 40 +const EDGE_INSET_PX = 1 +const SETTLED_EVENTS = ['transitionend', 'animationend'] as const + +function probeOffsets(length: number): number[] { + const last = length - EDGE_INSET_PX + if (last <= EDGE_INSET_PX) return [] + const offsets: number[] = [] + for (let offset = EDGE_INSET_PX; offset < last; offset += PROBE_SPACING_PX) offsets.push(offset) + offsets.push(last) + return offsets +} + +export function isCoveredByPage(target: HTMLElement): boolean { + const rect = target.getBoundingClientRect() + const columns = probeOffsets(rect.width) + for (const top of probeOffsets(rect.height)) { + for (const left of columns) { + const hit = document.elementFromPoint(rect.left + left, rect.top + top) + if (hit && !target.contains(hit) && !hit.contains(target)) return true + } + } + return false +} + +export function useCoveredByPage(target: MaybeRefOrGetter): Ref { + const covered = ref(false) + let frame = 0 + let observer: MutationObserver | null = null + + function measure(): void { + frame = 0 + const element = toValue(target) + covered.value = element ? isCoveredByPage(element) : false + } + + function scheduleMeasure(): void { + if (frame === 0) frame = requestAnimationFrame(measure) + } + + onMounted(() => { + observer = new MutationObserver(scheduleMeasure) + observer.observe(document.body, { childList: true, subtree: true, attributes: true }) + for (const settled of SETTLED_EVENTS) document.addEventListener(settled, scheduleMeasure, true) + window.addEventListener('resize', scheduleMeasure) + measure() + }) + + onBeforeUnmount(() => { + observer?.disconnect() + for (const settled of SETTLED_EVENTS) + document.removeEventListener(settled, scheduleMeasure, true) + window.removeEventListener('resize', scheduleMeasure) + if (frame !== 0) cancelAnimationFrame(frame) + }) + + return covered +} diff --git a/src/renderer/src/panel/AppPageHost.test.ts b/src/renderer/src/panel/AppPageHost.test.ts new file mode 100644 index 000000000..1d027fb4c --- /dev/null +++ b/src/renderer/src/panel/AppPageHost.test.ts @@ -0,0 +1,240 @@ +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { page as browserPage } from 'vitest/browser' +import { flushPromises, mount } from '@vue/test-utils' + +import '../assets/main.css' +import AppPageHost from './AppPageHost.vue' +import type { AppPagePlacement, AppPageStatus } from '../types/ipc' + +const PAGE_LEFT = 260 +const PAGE_TOP = 12 +const PAGE_WIDTH = 900 +const PAGE_HEIGHT = 600 +const FRAME = + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==' + +const showAppPage = vi.fn<(appId: string, placement: AppPagePlacement) => Promise>() +const captureAppPage = vi.fn<() => Promise>() +const hideAppPage = vi.fn() +const reloadAppPage = vi.fn() + +const mountPointFillsThePage = document.createElement('style') +mountPointFillsThePage.textContent = '[data-apps-page] > div { display: flex; flex: 1 1 auto; }' +document.head.append(mountPointFillsThePage) + +let page: HTMLElement +let tellStatus: (status: AppPageStatus) => void = () => {} +let stoppedListening = false + +async function nextFrame(): Promise { + await new Promise((resolve) => requestAnimationFrame(() => resolve())) + await flushPromises() +} + +const CUTOUT = { appId: 'remove-background', title: 'Remove Background' } + +async function mountHost(status: AppPageStatus = 'ready') { + showAppPage.mockResolvedValue(status) + const wrapper = mount(AppPageHost, { attachTo: page, props: CUTOUT }) + await flushPromises() + await nextFrame() + return wrapper +} + +function coverPartOfThePage(): HTMLElement { + const menu = document.createElement('div') + menu.dataset.testid = 'sidebar-menu' + menu.style.cssText = `position: fixed; left: ${PAGE_LEFT - 40}px; top: ${PAGE_TOP + 200}px; width: 200px; height: 120px; background: #222` + document.body.append(menu) + return menu +} + +beforeAll(() => browserPage.viewport(1280, 800)) + +beforeEach(() => { + vi.clearAllMocks() + stoppedListening = false + page = document.createElement('div') + page.dataset.appsPage = '' + page.style.cssText = `position: fixed; display: flex; left: ${PAGE_LEFT}px; top: ${PAGE_TOP}px; width: ${PAGE_WIDTH}px; height: ${PAGE_HEIGHT}px` + document.body.append(page) + captureAppPage.mockResolvedValue(FRAME) + ;(window as unknown as { api: unknown }).api = { + showAppPage, + captureAppPage, + hideAppPage, + reloadAppPage, + onAppPageStatusChanged: (listener: (status: AppPageStatus) => void) => { + tellStatus = listener + return () => { + stoppedListening = true + } + } + } +}) + +afterEach(() => { + page.remove() + document.querySelector('[data-testid="sidebar-menu"]')?.remove() +}) + +describe('AppPageHost', () => { + it("asks Desktop to show the app's page exactly over the host, with its rounded corners", async () => { + await mountHost() + + expect(showAppPage).toHaveBeenCalledWith('remove-background', { + x: PAGE_LEFT, + y: PAGE_TOP, + width: PAGE_WIDTH, + height: PAGE_HEIGHT, + cornerRadius: 14, + justOpened: true + }) + }) + + it('shows a placeholder while the app loads, and nothing of its own once the app is up', async () => { + const wrapper = await mountHost('loading') + + expect(wrapper.get('[data-testid="app-page-loading"]').isVisible()).toBe(true) + expect(wrapper.get('[data-testid="app-page-loading"]').text()).toBe('Loading Remove Background') + expect(wrapper.get('[data-testid="app-page-host"]').attributes('aria-busy')).toBe('true') + + tellStatus('ready') + await flushPromises() + + expect(wrapper.find('[data-testid="app-page-loading"]').exists()).toBe(false) + expect(wrapper.find('[data-testid="app-page-failed"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="app-page-host"]').attributes('aria-busy')).toBe('false') + }) + + it('lays the placeholder out where the app page puts its two panes: 360px, an 8px gap, the rest', async () => { + const wrapper = await mountHost('loading') + const [controls, results] = Array.from( + wrapper.get('[data-testid="app-page-loading"]').element.querySelectorAll(':scope > div') + ).map((pane) => pane.getBoundingClientRect()) + + expect(controls).toMatchObject({ x: PAGE_LEFT, y: PAGE_TOP, width: 360, height: PAGE_HEIGHT }) + expect(results).toMatchObject({ + x: PAGE_LEFT + 368, + y: PAGE_TOP, + width: PAGE_WIDTH - 368, + height: PAGE_HEIGHT + }) + }) + + it('says the app did not load, loads it again on Try again, and offers the way back', async () => { + const wrapper = await mountHost('failed') + + expect(wrapper.get('[data-testid="app-page-failed"]').text()).toContain( + "Remove Background didn't load" + ) + + await wrapper.get('[data-testid="app-page-retry"]').trigger('click') + + expect(reloadAppPage).toHaveBeenCalledTimes(1) + expect(wrapper.get('[data-testid="app-page-loading"]').isVisible()).toBe(true) + + tellStatus('failed') + await flushPromises() + await wrapper.get('[data-testid="app-page-back"]').trigger('click') + + expect(wrapper.emitted('close')).toHaveLength(1) + }) + + it('follows the host when it is resized, without counting another opening', async () => { + await mountHost() + showAppPage.mockClear() + + page.style.width = `${PAGE_WIDTH - 180}px` + await nextFrame() + await nextFrame() + + expect(showAppPage).toHaveBeenLastCalledWith( + 'remove-background', + expect.objectContaining({ width: PAGE_WIDTH - 180, justOpened: false }) + ) + }) + + it('counts one opening when the host is resized before Desktop has answered', async () => { + let answer: (status: AppPageStatus) => void = () => {} + showAppPage.mockReturnValue(new Promise((resolve) => (answer = resolve))) + mount(AppPageHost, { attachTo: page, props: CUTOUT }) + await flushPromises() + await nextFrame() + + page.style.width = `${PAGE_WIDTH - 180}px` + await nextFrame() + await nextFrame() + answer('ready') + await flushPromises() + + expect(showAppPage.mock.calls.filter(([, placement]) => placement.justOpened)).toHaveLength(1) + expect(showAppPage.mock.calls.length).toBeGreaterThan(1) + }) + + it('opens another app in the same host as a new opening', async () => { + const wrapper = await mountHost() + showAppPage.mockClear() + showAppPage.mockResolvedValue('loading') + + await wrapper.setProps({ appId: 'sdr-to-hdr', title: 'SDR to HDR' }) + await flushPromises() + + expect(showAppPage).toHaveBeenLastCalledWith( + 'sdr-to-hdr', + expect.objectContaining({ justOpened: true }) + ) + expect(wrapper.get('[data-testid="app-page-loading"]').text()).toBe('Loading SDR to HDR') + }) + + it('swaps the app for a still frame while a menu covers part of it, then brings it back', async () => { + const wrapper = await mountHost() + showAppPage.mockClear() + + const menu = coverPartOfThePage() + await nextFrame() + await nextFrame() + + expect(captureAppPage).toHaveBeenCalledTimes(1) + expect(hideAppPage).toHaveBeenCalledTimes(1) + expect(wrapper.get('[data-testid="app-page-frozen-frame"]').attributes('src')).toBe(FRAME) + expect(showAppPage).not.toHaveBeenCalled() + + menu.remove() + await nextFrame() + await nextFrame() + + expect(showAppPage).toHaveBeenCalledTimes(1) + }) + + it('leaves the app up for a menu that opens beside the host', async () => { + await mountHost() + const menu = document.createElement('div') + menu.dataset.testid = 'sidebar-menu' + menu.style.cssText = `position: fixed; left: 20px; top: ${PAGE_TOP + 200}px; width: 200px; height: 120px` + document.body.append(menu) + await nextFrame() + await nextFrame() + + expect(hideAppPage).not.toHaveBeenCalled() + }) + + it('does not ask for the app while a dialog already covers the host', async () => { + coverPartOfThePage() + showAppPage.mockResolvedValue('ready') + mount(AppPageHost, { attachTo: page, props: CUTOUT }) + await flushPromises() + await nextFrame() + + expect(showAppPage).not.toHaveBeenCalled() + }) + + it('hides the app and stops listening when the host goes away', async () => { + const wrapper = await mountHost() + + wrapper.unmount() + + expect(hideAppPage).toHaveBeenCalledTimes(1) + expect(stoppedListening).toBe(true) + }) +}) diff --git a/src/renderer/src/panel/AppPageHost.vue b/src/renderer/src/panel/AppPageHost.vue new file mode 100644 index 000000000..7971167b7 --- /dev/null +++ b/src/renderer/src/panel/AppPageHost.vue @@ -0,0 +1,183 @@ + + + diff --git a/src/renderer/src/panel/AppSidebar.test.ts b/src/renderer/src/panel/AppSidebar.test.ts index 69f002dea..525e361b8 100644 --- a/src/renderer/src/panel/AppSidebar.test.ts +++ b/src/renderer/src/panel/AppSidebar.test.ts @@ -25,12 +25,15 @@ const NEW = build({ id: 'new', name: 'Video Lab', lastLaunchedAt: 300 }) const TEAM = build({ id: 'team', name: 'Team Build', lastLaunchedAt: 400, workspaceId: 'ws-team' }) const navigatePanel = vi.fn() +const getAppsEnabled = vi.fn<() => Promise>() beforeEach(() => { vi.clearAllMocks() window.localStorage.clear() + getAppsEnabled.mockResolvedValue(false) ;(window as unknown as { api: unknown }).api = { navigatePanel, + getAppsEnabled, getInstallations: vi.fn().mockResolvedValue([]), onInstallationsChanged: vi.fn(() => () => {}), onInstallationsVersionsUpdated: vi.fn(() => () => {}), @@ -73,6 +76,51 @@ describe('AppSidebar', () => { ) }) + it('adds Apps between Home and Assets once Apps is switched on', async () => { + getAppsEnabled.mockResolvedValue(true) + const wrapper = await mountSidebar('apps') + + expect(wrapper.findAll('nav button').map((row) => row.text())).toEqual([ + 'Home', + 'Apps', + 'Assets', + 'Builds', + '' + ]) + expect(wrapper.findAll('[aria-current="page"]').map((row) => row.text())).toEqual(['Apps']) + + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + + expect(navigatePanel).toHaveBeenCalledExactlyOnceWith('apps') + expect(wrapper.emitted('navigate')).toEqual([['apps']]) + }) + + it('folds to the rail while an app is open, lets the toggle reopen it, and keeps the saved choice', async () => { + const wrapper = await mountSidebar('apps') + const sidebar = wrapper.get('[data-testid="app-sidebar"]') + expect(sidebar.attributes('data-collapsed')).toBe('false') + + await wrapper.setProps({ makeRoom: true }) + expect(sidebar.attributes('data-collapsed')).toBe('true') + + await wrapper.get('[data-testid="app-sidebar-toggle"]').trigger('click') + expect(sidebar.attributes('data-collapsed')).toBe('false') + + await wrapper.setProps({ makeRoom: false }) + expect(sidebar.attributes('data-collapsed')).toBe('false') + expect(window.localStorage.getItem('comfy.desktop.sidebar.collapsed')).not.toBe('true') + + await wrapper.setProps({ makeRoom: true }) + expect(sidebar.attributes('data-collapsed')).toBe('true') + }) + + it('leaves Apps out when the switch cannot be read', async () => { + getAppsEnabled.mockRejectedValue(new Error('main is busy')) + const wrapper = await mountSidebar() + + expect(wrapper.find('[data-testid="app-sidebar-apps"]').exists()).toBe(false) + }) + it('marks only the current page', async () => { const wrapper = await mountSidebar('chooser') diff --git a/src/renderer/src/panel/AppSidebar.vue b/src/renderer/src/panel/AppSidebar.vue index 83c41b8ba..17a932ed1 100644 --- a/src/renderer/src/panel/AppSidebar.vue +++ b/src/renderer/src/panel/AppSidebar.vue @@ -1,8 +1,9 @@ + + diff --git a/src/renderer/src/panel/PanelApp.test.ts b/src/renderer/src/panel/PanelApp.test.ts index 45f30aca0..b9c647d5d 100644 --- a/src/renderer/src/panel/PanelApp.test.ts +++ b/src/renderer/src/panel/PanelApp.test.ts @@ -386,6 +386,14 @@ function installMockApi(initial?: { listHomeWorkflows: vi.fn(async () => []), openCatalogWorkflow: vi.fn(async () => true), openCatalogWorkflowHub: vi.fn(async () => true), + getAppsEnabled: vi.fn(async () => false), + listApps: vi.fn(async () => []), + showAppPage: vi.fn(async () => 'loading'), + captureAppPage: vi.fn(async () => null), + hideAppPage: vi.fn(), + reloadAppPage: vi.fn(), + onAppPageStatusChanged: vi.fn(() => () => {}), + onAppPageClosed: vi.fn(() => () => {}), getListActions: vi.fn(async () => []), runAction: vi.fn(async () => ({ ok: true })), getFieldOptions: vi.fn(async () => []), @@ -1350,6 +1358,70 @@ describe('PanelApp', () => { expect(wrapper.get('[data-testid="chooser-view"]').attributes('data-visible')).toBe('false') }) + it('opens Apps from the sidebar in place of Home, and leaves it again', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + const wrapper = mountPanel() + await flushPromises() + + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="apps-view"]').isVisible()).toBe(true) + expect(wrapper.find('[data-testid="home-view"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="chooser-view"]').attributes('data-visible')).toBe('false') + expect(wrapper.get('[data-testid="app-sidebar-apps"]').attributes('aria-current')).toBe('page') + expect(window.api.navigatePanel).toHaveBeenCalledWith('apps') + + await wrapper.get('[data-testid="app-sidebar-home"]').trigger('click') + await flushPromises() + + expect(wrapper.find('[data-testid="apps-view"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="home-view"]').isVisible()).toBe(true) + }) + + it('goes back to all apps when Apps is picked again while an app is open', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + vi.mocked(window.api.listApps).mockResolvedValue([ + { id: 'upscale', title: 'Upscale 4x', description: 'Enlarge a picture.', coverUrl: null } + ]) + const wrapper = mountPanel() + await flushPromises() + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + await wrapper.get('[data-testid="app-card-upscale"]').trigger('click') + expect(wrapper.find('[data-testid="app-page-host"]').exists()).toBe(true) + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('true') + + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + + expect(wrapper.find('[data-testid="app-page-host"]').exists()).toBe(false) + expect(wrapper.get('[data-testid="app-card-upscale"]').isVisible()).toBe(true) + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('false') + }) + + it('gives the sidebar its width back when the person leaves Apps with an app open', async () => { + window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') + vi.mocked(window.api.getAppsEnabled).mockResolvedValue(true) + vi.mocked(window.api.listApps).mockResolvedValue([ + { id: 'upscale', title: 'Upscale 4x', description: 'Enlarge a picture.', coverUrl: null } + ]) + const wrapper = mountPanel() + await flushPromises() + await wrapper.get('[data-testid="app-sidebar-apps"]').trigger('click') + await flushPromises() + await wrapper.get('[data-testid="app-card-upscale"]').trigger('click') + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('true') + + await wrapper.get('[data-testid="app-sidebar-home"]').trigger('click') + await flushPromises() + + expect(wrapper.get('[data-testid="home-view"]').isVisible()).toBe(true) + expect(wrapper.get('[data-testid="app-sidebar"]').attributes('data-collapsed')).toBe('false') + }) + it('opens Home from the sidebar and returns to the list of builds', async () => { window.history.replaceState({}, '', '/?panel=chooser&firstUseCompleted=true') const wrapper = mountPanel() diff --git a/src/renderer/src/panel/PanelApp.vue b/src/renderer/src/panel/PanelApp.vue index ae175e9be..27f9b0893 100644 --- a/src/renderer/src/panel/PanelApp.vue +++ b/src/renderer/src/panel/PanelApp.vue @@ -12,6 +12,7 @@ import BuildDetailsView from './BuildDetailsView.vue' import HomeView from './HomeView.vue' import ComfyLifecycleView from './ComfyLifecycleView.vue' import AssetsView from './AssetsView.vue' +import AppsView from './AppsView.vue' import ChooserView from '../views/ChooserView.vue' import PerformanceTestView from '../views/PerformanceTestView.vue' import BenchmarksView from '../views/BenchmarksView.vue' @@ -172,7 +173,7 @@ if (params.get('e2e') === '1') { }) } -type DashboardHomePage = Extract +type DashboardHomePage = Extract const dashboardHomePage = ref('home') @@ -225,9 +226,14 @@ async function showHome(): Promise { window.api.navigatePanel('home') } +const appsVisits = ref(0) +const appIsOpen = ref(false) + function handleSidebarNavigate(page: DashboardPage): void { closeBuildDetails() - if (page === 'home' || page === 'chooser' || page === 'assets') dashboardHomePage.value = page + if (page === 'apps') appsVisits.value += 1 + if (page === 'home' || page === 'apps' || page === 'chooser' || page === 'assets') + dashboardHomePage.value = page } function retryLaunch(id: string): void { @@ -488,6 +494,7 @@ onUnmounted(() => { v-if="showSidebar && sidebarPage" :active-page="sidebarPage" :active-build-id="sidebarBuildId" + :make-room="appIsOpen" @navigate="handleSidebarNavigate" @open-build="openBuildFromSidebar" @new-build="handleChooserShowNewInstall" @@ -518,6 +525,11 @@ onUnmounted(() => { @open-build="openBuildFromSidebar" @show-builds="dashboardHomePage = 'chooser'" /> + { + it('points at the apps site on comfy.org when nothing overrides it', () => { + expect(resolveAppsLocation()).toEqual({ + url: DEFAULT_APPS_URL, + origin: 'https://comfy.org', + scopePath: '/apps/' + }) + }) + + it('follows an override to a preview deploy or a local dev server', () => { + expect(resolveAppsLocation('http://localhost:4321/apps/')).toEqual({ + url: 'http://localhost:4321/apps/', + origin: 'http://localhost:4321', + scopePath: '/apps/' + }) + }) + + it('treats an override without a trailing slash as a folder', () => { + expect(resolveAppsLocation('https://preview.example/apps').scopePath).toBe('/apps/') + }) + + it.each(['', null, 'not a url', 'file:///etc/passwd', 'javascript:alert(1)'])( + 'falls back to comfy.org for the unusable override %j', + (override) => { + expect(resolveAppsLocation(override).url).toBe(DEFAULT_APPS_URL) + } + ) +}) + +describe('isInAppsScope', () => { + const location = resolveAppsLocation() + + it.each([ + 'https://comfy.org/apps/', + 'https://comfy.org/apps', + 'https://comfy.org/apps/#/remove-background', + 'https://comfy.org/apps/remove-background/desktop.html?from=list', + 'https://comfy.org/apps/session/' + ])('keeps %s inside the apps site', (url) => { + expect(isInAppsScope(location, url)).toBe(true) + }) + + it.each([ + 'https://comfy.org/', + 'https://comfy.org/login/?returnTo=%2Fapps%2F', + 'https://comfy.org/appsfoo/', + 'https://comfy.org/apps/../login/', + 'https://comfy.org/apps/%2e%2e/hub/', + 'http://comfy.org/apps/', + 'https://comfy.org:8443/apps/', + 'https://comfy.org.evil.example/apps/', + 'https://comfy.org@evil.example/apps/', + 'https://user:secret@comfy.org/apps/', + 'https://evil.example/apps/', + 'javascript:alert(1)', + 'file:///apps/', + 'about:blank', + 'not a url' + ])('keeps %s outside the apps site', (url) => { + expect(isInAppsScope(location, url)).toBe(false) + }) + + it('scopes a root override to its whole origin and nothing else', () => { + const preview = resolveAppsLocation('http://127.0.0.1:4331/') + expect(isInAppsScope(preview, 'http://127.0.0.1:4331/anything/here')).toBe(true) + expect(isInAppsScope(preview, 'http://127.0.0.1:4332/anything/here')).toBe(false) + }) +}) + +describe('isAppsListUrl', () => { + const location = resolveAppsLocation() + + it.each([ + 'https://comfy.org/apps/', + 'https://comfy.org/apps', + 'https://comfy.org/apps/#', + 'https://comfy.org/apps/#/', + 'https://comfy.org/apps/?from=app' + ])('reads %s as the list of apps', (url) => { + expect(isAppsListUrl(location, url)).toBe(true) + }) + + it.each([ + 'https://comfy.org/apps/#/upscale', + 'https://comfy.org/apps/remove-background/desktop.html', + 'https://comfy.org/apps/session/', + 'https://comfy.org/', + 'https://evil.example/apps/', + 'not a url' + ])('reads %s as something other than the list', (url) => { + expect(isAppsListUrl(location, url)).toBe(false) + }) +}) + +describe('isWebUrl', () => { + it.each(['https://example.com/', 'http://example.com/docs'])('accepts %s', (url) => { + expect(isWebUrl(url)).toBe(true) + }) + + it.each(['mailto:someone@example.com', 'file:///tmp/x', 'javascript:alert(1)', 'comfy://open'])( + 'rejects %s', + (url) => { + expect(isWebUrl(url)).toBe(false) + } + ) +}) diff --git a/src/shared/appsLocation.ts b/src/shared/appsLocation.ts new file mode 100644 index 000000000..e4f342ad4 --- /dev/null +++ b/src/shared/appsLocation.ts @@ -0,0 +1,44 @@ +export const DEFAULT_APPS_URL = 'https://comfy.org/apps/' + +export interface AppsLocation { + url: string + origin: string + scopePath: string +} + +function parseWebUrl(value: string): URL | null { + try { + const url = new URL(value) + return url.protocol === 'https:' || url.protocol === 'http:' ? url : null + } catch { + return null + } +} + +function asDirectory(pathname: string): string { + return pathname.endsWith('/') ? pathname : `${pathname}/` +} + +export function resolveAppsLocation(override?: string | null): AppsLocation { + const url = (override ? parseWebUrl(override) : null) ?? new URL(DEFAULT_APPS_URL) + return { url: url.href, origin: url.origin, scopePath: asDirectory(url.pathname) } +} + +export function isInAppsScope(location: AppsLocation, candidate: string): boolean { + const url = parseWebUrl(candidate) + if (!url || url.origin !== location.origin) return false + if (url.username !== '' || url.password !== '') return false + return asDirectory(url.pathname).startsWith(location.scopePath) +} + +const LIST_ROUTES: ReadonlySet = new Set(['', '#', '#/']) + +export function isAppsListUrl(location: AppsLocation, candidate: string): boolean { + const url = parseWebUrl(candidate) + if (!url || url.origin !== location.origin) return false + return asDirectory(url.pathname) === location.scopePath && LIST_ROUTES.has(url.hash) +} + +export function isWebUrl(candidate: string): boolean { + return parseWebUrl(candidate) !== null +} diff --git a/src/types/comfyDesktopBridge.ts b/src/types/comfyDesktopBridge.ts index 9f5f4c0dc..b5d71c456 100644 --- a/src/types/comfyDesktopBridge.ts +++ b/src/types/comfyDesktopBridge.ts @@ -166,6 +166,20 @@ export interface ComfyDesktop2AuthBridge { ): Promise } +/** + * Desktop's own Comfy account session, as a Comfy page hosted in Desktop's + * Apps view sees it. Desktop exposes it to that page as + * `window.comfyDesktopAuth`. The page can read the session, hold the + * workspace and partner-node credentials, and start a sign-in; it cannot sign + * Desktop out or switch workspace. + */ +export type ComfyDesktopHostedAuthBridge = Required< + Pick< + ComfyDesktop2AuthBridge, + 'getState' | 'getWorkspaceToken' | 'getPartnerNodeToken' | 'requestSignIn' | 'onChanged' + > +> + export interface ComfyDesktop2TerminalBridge { subscribe(installationId?: string): Promise unsubscribe(installationId?: string): Promise diff --git a/src/types/ipc.ts b/src/types/ipc.ts index 6a702bf73..7e21ba80d 100644 --- a/src/types/ipc.ts +++ b/src/types/ipc.ts @@ -126,7 +126,31 @@ export interface Installation { [key: string]: unknown } -export type DashboardPage = 'home' | 'assets' | 'chooser' | 'benchmarks' | 'performance-test' +export type DashboardPage = + | 'home' + | 'apps' + | 'assets' + | 'chooser' + | 'benchmarks' + | 'performance-test' + +export interface AppCard { + id: string + title: string + description: string + coverUrl: string | null +} + +export type AppPageStatus = 'loading' | 'ready' | 'failed' + +export interface AppPagePlacement { + x: number + y: number + width: number + height: number + cornerRadius: number + justOpened?: boolean +} export interface CatalogWorkflow { id: string @@ -1153,6 +1177,14 @@ export interface ElectronApi { listHomeWorkflows(): Promise openCatalogWorkflow(id: string): Promise openCatalogWorkflowHub(): Promise + getAppsEnabled(): Promise + listApps(): Promise + showAppPage(appId: string, placement: AppPagePlacement): Promise + captureAppPage(): Promise + hideAppPage(): void + reloadAppPage(): void + onAppPageStatusChanged(callback: (status: AppPageStatus) => void): () => void + onAppPageClosed(callback: () => void): () => void getBuildStorageSnapshot(): Promise resolveStartupRestoreReveal(result: 'takeover-ready' | 'dashboard-fallback'): void openGlobalSettings(