LCOV - code coverage report
Current view: top level - src/stores - bootstrapStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 92.9 % 28 26
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { until, useAsyncState } from '@vueuse/core'
       2              : import axios from 'axios'
       3              : import { defineStore, storeToRefs } from 'pinia'
       4              : 
       5              : import { bootCloudIdentity } from '@/platform/auth/session/cloudIdentityBoot'
       6              : import { isCloud } from '@/platform/distribution/types'
       7              : import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer'
       8              : import { useSettingStore } from '@/platform/settings/settingStore'
       9              : import { reportError } from '@comfyorg/telemetry/reportError'
      10              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
      11              : import { api } from '@/scripts/api'
      12              : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
      13              : import { useAuthStore } from '@/stores/authStore'
      14              : import { useUserStore } from '@/stores/userStore'
      15              : 
      16              : /**
      17              :  * Backends that vendor no custom-node locale files do not implement
      18              :  * `/api/i18n`, so a 404 means "no custom-node translations", not a failure.
      19              :  */
      20         2355 : async function fetchCustomNodesI18n() {
      21         2355 :   try {
      22         2355 :     return await api.getCustomNodesI18n()
      23              :   } catch (error) {
      24            0 :     if (axios.isAxiosError(error) && error.response?.status === 404) return
      25            0 :     throw error
      26              :   }
      27              : }
      28              : 
      29              : // Matches the Firebase-auth-wait timeout used elsewhere (router.ts,
      30              : // WorkspaceAuthGate.vue) so a broken/stale session fails this bounded wait
      31              : // on the same schedule those already fail theirs.
      32              : const AUTH_WAIT_TIMEOUT_MS = 16_000
      33              : const AUTH_WAIT_RETRY_DELAY_MS = 3_000
      34              : 
      35              : /**
      36              :  * Waits for Firebase auth initialization to complete, bounded so a stale
      37              :  * token or a broken auth response can never hang bootstrap forever.
      38              :  *
      39              :  * Only isInitialized is awaited — onAuthStateChanged fires with null for
      40              :  * signed-out users, so a signed-out load resolves this wait immediately
      41              :  * instead of burning the timeout and firing a false Sentry report. Waiting
      42              :  * for an actual user is a separate, unbounded wait in the caller.
      43              :  *
      44              :  * Retries once after a short delay; if auth is still unresolved, reports it
      45              :  * to every observability sink and lets bootstrap continue rather than leaving
      46              :  * the caller stuck.
      47              :  */
      48              : async function waitForCloudAuth(): Promise<void> {
      49              :   const { isInitialized } = storeToRefs(useAuthStore())
      50              :   const waitForResolution = () =>
      51              :     until(isInitialized).toBe(true, {
      52              :       timeout: AUTH_WAIT_TIMEOUT_MS,
      53              :       throwOnTimeout: true
      54              :     })
      55              : 
      56              :   try {
      57              :     await waitForResolution()
      58              :   } catch (error) {
      59              :     console.warn(
      60              :       '[bootstrapStore] Auth did not resolve in time, retrying once',
      61              :       error
      62              :     )
      63              :     await new Promise((resolve) =>
      64              :       setTimeout(resolve, AUTH_WAIT_RETRY_DELAY_MS)
      65              :     )
      66              :     try {
      67              :       await waitForResolution()
      68              :     } catch (retryError) {
      69              :       reportError(retryError, {
      70              :         surface: 'platform',
      71              :         errorType: 'bootstrap_auth_wait_timeout'
      72              :       })
      73              :     }
      74              :   }
      75              : }
      76              : 
      77         2356 : export const useBootstrapStore = defineStore('bootstrap', () => {
      78         2355 :   const settingStore = useSettingStore()
      79         2355 :   const workflowStore = useWorkflowStore()
      80              : 
      81         2355 :   const {
      82              :     isReady: isI18nReady,
      83              :     error: i18nError,
      84              :     execute: loadI18n
      85              :   } = useAsyncState(
      86              :     () =>
      87              :       bootstrapTracer.settle('bootstrap/i18n', async () => {
      88         2355 :         const { mergeCustomNodesI18n } = await import('@/i18n')
      89         2355 :         const i18nData = await fetchCustomNodesI18n()
      90         2355 :         if (i18nData) mergeCustomNodesI18n(i18nData)
      91              :       }),
      92              :     undefined,
      93              :     { immediate: false }
      94              :   )
      95              : 
      96         2355 :   let storesLoaded = false
      97              : 
      98         2355 :   function loadAuthenticatedStores(): Promise<void>[] {
      99         2355 :     if (storesLoaded) return []
     100         2355 :     storesLoaded = true
     101              : 
     102         2355 :     return [
     103              :       bootstrapTracer.settle('bootstrap/settings', () => settingStore.load()),
     104              :       bootstrapTracer.settle('bootstrap/workflows', () =>
     105              :         workflowStore.loadWorkflows()
     106              :       )
     107              :     ]
     108              :   }
     109              : 
     110         2355 :   async function startStoreBootstrap() {
     111         2355 :     void loadI18n()
     112              : 
     113              :     if (isCloud) {
     114              :       await bootstrapTracer.settle('auth-gate/initialized', waitForCloudAuth)
     115              :       await bootstrapTracer.settle('auth-gate/identity', bootCloudIdentity)
     116              : 
     117              :       const authStore = useAuthStore()
     118              :       const apiKeyAuthStore = useApiKeyAuthStore()
     119              :       await bootstrapTracer.settle('auth-gate/sign-in', () =>
     120              :         until(
     121              :           () => authStore.isAuthenticated || apiKeyAuthStore.isAuthenticated
     122              :         ).toBe(true)
     123              :       )
     124              :     }
     125              : 
     126         2355 :     const userStore = useUserStore()
     127         2355 :     await bootstrapTracer.settle('auth-gate/user-store', () =>
     128              :       userStore.initialize()
     129              :     )
     130              : 
     131         2355 :     const { needsLogin } = storeToRefs(userStore)
     132         2355 :     await bootstrapTracer.settle('auth-gate/needs-login', () =>
     133              :       until(needsLogin).toBe(false)
     134              :     )
     135              : 
     136         2355 :     if (!isCloud) api.prefetchNodeDefs()
     137         2355 :     const storeLoads = loadAuthenticatedStores()
     138         2355 :     void Promise.allSettled(storeLoads).then(() => {
     139         2355 :       bootstrapTracer.milestone('stores-ready')
     140              :     })
     141              :   }
     142              : 
     143         2355 :   return {
     144              :     isI18nReady,
     145              :     i18nError,
     146              :     startStoreBootstrap
     147              :   }
     148              : })
        

Generated by: LCOV version 2.0-1