From cb042bee24d76238db00d4388037fa2d021adf37 Mon Sep 17 00:00:00 2001 From: kishore Date: Fri, 8 May 2026 00:02:34 -0700 Subject: [PATCH] fix: use local API base in cloud dev Keep local cloud development on the Vite proxy path so user-scoped customer requests do not leak to staging. --- global.d.ts | 1 + scripts/vite-define-shim.ts | 3 ++ src/config/comfyApi.test.ts | 26 +++++++++++++++++ src/config/comfyApi.ts | 58 +++++++++++++++++++++++++++++-------- vite.config.mts | 6 ++++ vitest.setup.ts | 1 + 6 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 src/config/comfyApi.test.ts diff --git a/global.d.ts b/global.d.ts index e0a154c311..6744f428e6 100644 --- a/global.d.ts +++ b/global.d.ts @@ -5,6 +5,7 @@ declare const __SENTRY_DSN__: string declare const __ALGOLIA_APP_ID__: string declare const __ALGOLIA_API_KEY__: string declare const __USE_PROD_CONFIG__: boolean +declare const __DEV_SERVER_COMFYUI_URL__: string interface ImpactQueueFunction { (...args: unknown[]): void diff --git a/scripts/vite-define-shim.ts b/scripts/vite-define-shim.ts index 970d09aef0..978df7abb3 100644 --- a/scripts/vite-define-shim.ts +++ b/scripts/vite-define-shim.ts @@ -11,6 +11,7 @@ declare global { const __ALGOLIA_APP_ID__: string const __ALGOLIA_API_KEY__: string const __USE_PROD_CONFIG__: boolean + const __DEV_SERVER_COMFYUI_URL__: string const __DISTRIBUTION__: 'desktop' | 'localhost' | 'cloud' const __IS_NIGHTLY__: boolean } @@ -22,6 +23,7 @@ type GlobalWithDefines = typeof globalThis & { __ALGOLIA_APP_ID__: string __ALGOLIA_API_KEY__: string __USE_PROD_CONFIG__: boolean + __DEV_SERVER_COMFYUI_URL__: string __DISTRIBUTION__: 'desktop' | 'localhost' | 'cloud' __IS_NIGHTLY__: boolean window?: Record @@ -37,6 +39,7 @@ globalWithDefines.__SENTRY_DSN__ = '' globalWithDefines.__ALGOLIA_APP_ID__ = '' globalWithDefines.__ALGOLIA_API_KEY__ = '' globalWithDefines.__USE_PROD_CONFIG__ = false +globalWithDefines.__DEV_SERVER_COMFYUI_URL__ = '' globalWithDefines.__DISTRIBUTION__ = 'localhost' globalWithDefines.__IS_NIGHTLY__ = false diff --git a/src/config/comfyApi.test.ts b/src/config/comfyApi.test.ts new file mode 100644 index 0000000000..51155227bb --- /dev/null +++ b/src/config/comfyApi.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' + +import { getComfyApiBaseUrlForEnvironment } from '@/config/comfyApi' + +describe('comfy api config', () => { + it('uses same-origin API calls for cloud local development', () => { + expect( + getComfyApiBaseUrlForEnvironment({ + isCloudDistribution: true, + isDev: true, + devServerComfyUIUrl: 'http://127.0.0.1:8188', + useProdConfig: false + }) + ).toBe('') + }) + + it('keeps staging API for non-local staging builds', () => { + expect( + getComfyApiBaseUrlForEnvironment({ + isCloudDistribution: true, + isDev: false, + useProdConfig: false + }) + ).toBe('https://stagingapi.comfy.org') + }) +}) diff --git a/src/config/comfyApi.ts b/src/config/comfyApi.ts index 8efd651bfb..d6a20912e1 100644 --- a/src/config/comfyApi.ts +++ b/src/config/comfyApi.ts @@ -10,34 +10,68 @@ const STAGING_API_BASE_URL = 'https://stagingapi.comfy.org' const PROD_PLATFORM_BASE_URL = 'https://platform.comfy.org' const STAGING_PLATFORM_BASE_URL = 'https://stagingplatform.comfy.org' -const BUILD_TIME_API_BASE_URL = __USE_PROD_CONFIG__ - ? PROD_API_BASE_URL - : STAGING_API_BASE_URL +type ComfyApiEnvironment = { + isCloudDistribution: boolean + isDev: boolean + devServerComfyUIUrl?: string + useProdConfig: boolean +} -const BUILD_TIME_PLATFORM_BASE_URL = __USE_PROD_CONFIG__ - ? PROD_PLATFORM_BASE_URL - : STAGING_PLATFORM_BASE_URL +const localOriginPattern = + /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(?::\d+)?(?:\/|$)/ -export function getComfyApiBaseUrl(): string { - if (!isCloud) { - return BUILD_TIME_API_BASE_URL +function buildTimeApiBaseUrl(useProdConfig: boolean): string { + return useProdConfig ? PROD_API_BASE_URL : STAGING_API_BASE_URL +} + +function buildTimePlatformBaseUrl(useProdConfig: boolean): string { + return useProdConfig ? PROD_PLATFORM_BASE_URL : STAGING_PLATFORM_BASE_URL +} + +function isLocalDevServer(url?: string): boolean { + return url ? localOriginPattern.test(url) : false +} + +export function getComfyApiBaseUrlForEnvironment({ + isCloudDistribution, + isDev, + devServerComfyUIUrl, + useProdConfig +}: ComfyApiEnvironment): string { + const buildTimeApiBaseUrlValue = buildTimeApiBaseUrl(useProdConfig) + if (!isCloudDistribution) { + return buildTimeApiBaseUrlValue + } + if (isDev && isLocalDevServer(devServerComfyUIUrl)) { + return '' } return configValueOrDefault( remoteConfig.value, 'comfy_api_base_url', - BUILD_TIME_API_BASE_URL + buildTimeApiBaseUrlValue ) } +export function getComfyApiBaseUrl(): string { + return getComfyApiBaseUrlForEnvironment({ + isCloudDistribution: isCloud, + isDev: import.meta.env.DEV, + devServerComfyUIUrl: __DEV_SERVER_COMFYUI_URL__, + useProdConfig: __USE_PROD_CONFIG__ + }) +} + export function getComfyPlatformBaseUrl(): string { + const buildTimePlatformBaseUrlValue = + buildTimePlatformBaseUrl(__USE_PROD_CONFIG__) if (!isCloud) { - return BUILD_TIME_PLATFORM_BASE_URL + return buildTimePlatformBaseUrlValue } return configValueOrDefault( remoteConfig.value, 'comfy_platform_base_url', - BUILD_TIME_PLATFORM_BASE_URL + buildTimePlatformBaseUrlValue ) } diff --git a/vite.config.mts b/vite.config.mts index 795c043807..8defb339e8 100644 --- a/vite.config.mts +++ b/vite.config.mts @@ -219,6 +219,11 @@ export default defineConfig({ ...cloudProxyConfig }, + '/customers': { + target: DEV_SERVER_COMFYUI_URL, + ...cloudProxyConfig + }, + '/ws': { target: DEV_SERVER_COMFYUI_URL, ws: true, @@ -630,6 +635,7 @@ export default defineConfig({ __ALGOLIA_APP_ID__: JSON.stringify(process.env.ALGOLIA_APP_ID || ''), __ALGOLIA_API_KEY__: JSON.stringify(process.env.ALGOLIA_API_KEY || ''), __USE_PROD_CONFIG__: process.env.USE_PROD_CONFIG === 'true', + __DEV_SERVER_COMFYUI_URL__: JSON.stringify(DEV_SERVER_COMFYUI_URL), __DISTRIBUTION__: JSON.stringify(DISTRIBUTION), __IS_NIGHTLY__: JSON.stringify(IS_NIGHTLY) }, diff --git a/vitest.setup.ts b/vitest.setup.ts index a22e823b49..8d119f0cb4 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -46,6 +46,7 @@ globalThis.__SENTRY_DSN__ = '' globalThis.__ALGOLIA_APP_ID__ = '' globalThis.__ALGOLIA_API_KEY__ = '' globalThis.__USE_PROD_CONFIG__ = false +globalThis.__DEV_SERVER_COMFYUI_URL__ = '' globalThis.__DISTRIBUTION__ = 'localhost' globalThis.__IS_NIGHTLY__ = false