Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ export function getFirebaseConfig(): FirebaseOptions {
if (remoteConfig.value.firebase_env === 'dev') return DEV_CONFIG
return BUILD_TIME_CONFIG
}

/** Whether {@link getFirebaseConfig} names the project from the server rather than the build. */
export function hasServerFirebaseConfig(): boolean {
return (
Boolean(remoteConfig.value.firebase_config) ||
remoteConfig.value.firebase_env === 'dev'
)
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const hasHostTelemetryBridge = Boolean(window.__comfyDesktop2?.Telemetry)

async function loadRemoteConfigBeforeFirebase(): Promise<void> {
await bootstrapTracer.settle('startup/remote-config', async () => {
const { refreshRemoteConfig } =
const { loadRemoteConfigUntilReady, refreshRemoteConfig } =
await import('@/platform/remoteConfig/refreshRemoteConfig')
await refreshRemoteConfig({ useAuth: false })
if (isCloud) await loadRemoteConfigUntilReady()
else await refreshRemoteConfig({ useAuth: false })
})
}

Expand Down
97 changes: 97 additions & 0 deletions src/platform/remoteConfig/refreshRemoteConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { api } from '@/scripts/api'

import {
invalidateRemoteConfig,
loadRemoteConfigUntilReady,
refreshRemoteConfig
} from './refreshRemoteConfig'
import {
Expand Down Expand Up @@ -478,4 +479,100 @@ describe('refreshRemoteConfig', () => {
expect(remoteConfigErrorStatus.value).toBeNull()
})
})

describe('loadRemoteConfigUntilReady', () => {
const BOOTSTRAP_RELEASE_MS = 30_000

it('retries until /features loads instead of leaving startup on build-time defaults', async () => {
vi.useFakeTimers()
const serverConfig = { firebase_config: { projectId: 'dreamboothy' } }
vi.mocked(global.fetch)
.mockRejectedValueOnce(new DOMException('Aborted', 'AbortError'))
.mockResolvedValueOnce(mockErrorResponse(503, 'Service Unavailable'))
.mockResolvedValueOnce(mockSuccessResponse(serverConfig))

const loading = loadRemoteConfigUntilReady()
await vi.runAllTimersAsync()
await loading

expect(global.fetch).toHaveBeenCalledTimes(3)
expect(remoteConfigState.value).toBe('anonymous')
expect(remoteConfig.value).toEqual(serverConfig)
vi.useRealTimers()
})

it('keeps retrying a 200 that omits firebase_config instead of booting on the build default', async () => {
vi.useFakeTimers()
const serverConfig = { firebase_config: { projectId: 'dreamboothy' } }
vi.mocked(global.fetch)
.mockResolvedValueOnce(
mockSuccessResponse({ subscription_required: true })
)
.mockResolvedValueOnce(mockSuccessResponse(serverConfig))

const loading = loadRemoteConfigUntilReady()
await vi.runAllTimersAsync()
await loading

expect(global.fetch).toHaveBeenCalledTimes(2)
expect(remoteConfig.value).toEqual(serverConfig)
vi.useRealTimers()
})

it('stops waiting on a loopback host, whose server never names a project', async () => {
vi.useFakeTimers()
vi.stubGlobal('location', { hostname: '127.0.0.1' })
vi.mocked(global.fetch).mockResolvedValue(
mockSuccessResponse({ subscription_required: true })
)

const loading = loadRemoteConfigUntilReady()
await vi.runAllTimersAsync()
await loading

expect(global.fetch).toHaveBeenCalledTimes(2)
vi.unstubAllGlobals()
vi.useRealTimers()
})

it('never reaches Firebase startup on deployed cloud that keeps answering without a project', async () => {
vi.useFakeTimers()
vi.stubGlobal('location', { hostname: 'cloud.comfy.org' })
vi.mocked(global.fetch).mockResolvedValue(
mockSuccessResponse({ subscription_required: true })
)
const reachedFirebaseStartup = vi.fn()

void loadRemoteConfigUntilReady().then(reachedFirebaseStartup)
await vi.advanceTimersByTimeAsync(5 * 60_000)

expect(reachedFirebaseStartup).not.toHaveBeenCalled()
expect(vi.mocked(global.fetch).mock.calls.length).toBeGreaterThan(5)

vi.mocked(global.fetch).mockResolvedValue(
mockSuccessResponse({ firebase_config: { projectId: 'dreamboothy' } })
)
await vi.advanceTimersByTimeAsync(BOOTSTRAP_RELEASE_MS)
expect(reachedFirebaseStartup).toHaveBeenCalledOnce()

vi.unstubAllGlobals()
vi.useRealTimers()
})

it('accepts a 200 that names the dev project through firebase_env', async () => {
vi.useFakeTimers()
const serverConfig = { firebase_env: 'dev' as const }
vi.mocked(global.fetch).mockResolvedValue(
mockSuccessResponse(serverConfig)
)

const loading = loadRemoteConfigUntilReady()
await vi.runAllTimersAsync()
await loading

expect(global.fetch).toHaveBeenCalledTimes(1)
expect(remoteConfig.value).toEqual(serverConfig)
vi.useRealTimers()
})
})
})
49 changes: 49 additions & 0 deletions src/platform/remoteConfig/refreshRemoteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { delay } from 'es-toolkit'

import { resolveUnifiedWebSession } from '@comfyorg/account-core/webSessionFlag'

import { hasServerFirebaseConfig } from '@/config/firebase'

Check warning on line 5 in src/platform/remoteConfig/refreshRemoteConfig.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'remote-config/infrastructure' to zone 'authentication/infrastructure/outer-1' is not allowed. src/platform/remoteConfig/refreshRemoteConfig.ts -> src/config/firebase.ts Route the import through an allowed zone or restructure the dependency.
import { isCloud } from '@/platform/distribution/types'
import { reportError } from '@/platform/telemetry/reportError'

Check warning on line 7 in src/platform/remoteConfig/refreshRemoteConfig.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'remote-config/infrastructure' to zone 'error-reporting/application' is not allowed. src/platform/remoteConfig/refreshRemoteConfig.ts -> src/platform/telemetry/reportError.ts Route the import through an allowed zone or restructure the dependency.
import { isLoopbackHost } from '@/utils/hostWhitelist'

Check warning on line 8 in src/platform/remoteConfig/refreshRemoteConfig.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'remote-config/infrastructure' to zone 'authentication/domain' is not allowed. src/platform/remoteConfig/refreshRemoteConfig.ts -> src/utils/hostWhitelist.ts Route the import through an allowed zone or restructure the dependency.

import {
authenticatedRemoteConfigState,
Expand Down Expand Up @@ -250,3 +255,47 @@
activeRefreshControllers.delete(controller)
}
}

const BOOTSTRAP_RETRY_BASE_MS = 1_000
const BOOTSTRAP_RETRY_MAX_MS = 10_000
const BOOTSTRAP_UNNAMED_PROJECT_READS = 1

function servesLocalCloud(): boolean {
return isLoopbackHost(window.location.hostname)
}

/**
* Blocks until the server names the Firebase project; a loopback harness,
* whose server never sends one, stops after a further read instead.
*/
export async function loadRemoteConfigUntilReady(): Promise<void> {
let unnamedProjectReads = 0
for (let attempt = 0; ; attempt++) {
await refreshRemoteConfig({ useAuth: false })
if (remoteConfigState.value !== 'error') {
if (hasServerFirebaseConfig()) return
unnamedProjectReads++
if (unnamedProjectReads === 1) {
reportError(
new Error(
'Startup read answered without naming the Firebase project'
),
{
surface: 'workspace',
errorType: 'startup_remote_config_without_firebase_project',
level: 'warning',
context: { localCloud: servesLocalCloud() }
}
)
}
if (
servesLocalCloud() &&
unnamedProjectReads > BOOTSTRAP_UNNAMED_PROJECT_READS
)
return
}
await delay(
Math.min(BOOTSTRAP_RETRY_BASE_MS * 2 ** attempt, BOOTSTRAP_RETRY_MAX_MS)
)
}
}
Loading