LCOV - code coverage report
Current view: top level - src/utils - sessionFeatureFlagOverride.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 1 1
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { firebaseIdentity } from '@/platform/auth/firebaseIdentity'
       2              : import { webSessionUser } from '@/platform/auth/session/webSessionUser'
       3              : import { isCloud } from '@/platform/distribution/types'
       4              : 
       5              : const STORAGE_KEY = 'Comfy.FeatureFlagOverride'
       6              : const QUERY_PARAM = 'ff'
       7              : const EMPLOYEE_EMAIL_DOMAIN = '@comfy.org'
       8              : 
       9              : type OverrideMap = Record<string, unknown>
      10              : 
      11              : /**
      12              :  * Overrides apply only to a signed-in Comfy employee, so a link handed to a
      13              :  * customer stays inert on their machine. With every flag overridable, this is
      14              :  * the only thing standing between a `?ff=` link and the app's behaviour, so it
      15              :  * fails closed.
      16              :  *
      17              :  * Reads the identity modules rather than `authStore`, which imports
      18              :  * `useFeatureFlags` and would drag the whole app module graph into every
      19              :  * feature flag read. A web session sign-in has no Firebase user, so its
      20              :  * server-verified user counts too.
      21              :  */
      22              : function isComfyEmployee(): boolean {
      23              :   return [firebaseUser(), webSessionUser.value].some(
      24              :     (user) =>
      25              :       user?.emailVerified === true &&
      26              :       (user.email?.toLowerCase().endsWith(EMPLOYEE_EMAIL_DOMAIN) ?? false)
      27              :   )
      28              : }
      29              : 
      30              : function firebaseUser() {
      31              :   try {
      32              :     return firebaseIdentity.currentUser()
      33              :   } catch {
      34              :     return null
      35              :   }
      36              : }
      37              : 
      38              : /**
      39              :  * Reads the value with the same JSON semantics as the `ff:` localStorage
      40              :  * override, so `:false` is the boolean and `:12` the number rather than a
      41              :  * truthy string. Anything JSON rejects is taken as a plain string, which is
      42              :  * what makes `:enforce` work; quote it (`:"12"`) to force a numeric-looking
      43              :  * value to stay a string. A bare `?ff=name` means "turn this on".
      44              :  */
      45              : function parseOverrideValue(rawValue: string | undefined): unknown {
      46              :   if (rawValue === undefined) return true
      47              : 
      48              :   try {
      49              :     return JSON.parse(rawValue)
      50              :   } catch {
      51              :     return rawValue
      52              :   }
      53              : }
      54              : 
      55              : /**
      56              :  * The captured overrides plus the query string they came from. Remembering the
      57              :  * source makes capture idempotent: re-reading the same URL does not rewrite
      58              :  * storage, so a flag can be read as often as a render needs.
      59              :  */
      60              : type StoredState = { search: string; overrides: OverrideMap }
      61              : 
      62              : function emptyState(): StoredState {
      63              :   return { search: '', overrides: {} }
      64              : }
      65              : 
      66              : function coerceStoredOverrides(value: unknown): OverrideMap {
      67              :   if (typeof value !== 'object' || value === null || Array.isArray(value)) {
      68              :     return {}
      69              :   }
      70              :   return { ...value }
      71              : }
      72              : 
      73              : function readStoredState(): StoredState {
      74              :   let raw: string | null
      75              :   try {
      76              :     raw = sessionStorage.getItem(STORAGE_KEY)
      77              :   } catch {
      78              :     return emptyState()
      79              :   }
      80              :   if (!raw) return emptyState()
      81              : 
      82              :   let parsed: unknown
      83              :   try {
      84              :     parsed = JSON.parse(raw)
      85              :   } catch {
      86              :     return emptyState()
      87              :   }
      88              :   if (typeof parsed !== 'object' || parsed === null) return emptyState()
      89              : 
      90              :   const search = 'search' in parsed ? parsed.search : undefined
      91              :   const overrides = 'overrides' in parsed ? parsed.overrides : undefined
      92              :   return {
      93              :     search: typeof search === 'string' ? search : '',
      94              :     overrides: coerceStoredOverrides(overrides)
      95              :   }
      96              : }
      97              : 
      98              : function writeStoredState(state: StoredState): void {
      99              :   try {
     100              :     sessionStorage.setItem(STORAGE_KEY, JSON.stringify(state))
     101              :   } catch {
     102              :     console.warn('[ff] Failed to persist session feature flag overrides')
     103              :   }
     104              : }
     105              : 
     106              : function splitRequest(request: string): [name: string, value?: string] {
     107              :   const separator = request.indexOf(':')
     108              :   if (separator === -1) return [request]
     109              :   return [request.slice(0, separator), request.slice(separator + 1)]
     110              : }
     111              : 
     112              : function readOverrideRequests(search: string): string[] {
     113              :   return new URLSearchParams(search).getAll(QUERY_PARAM)
     114              : }
     115              : 
     116              : /**
     117              :  * Merges `?ff=` requests from the current URL into the overrides already held
     118              :  * for this tab. An `?ff=` with no name clears every override in the session.
     119              :  */
     120              : function captureRequests(
     121              :   requests: string[],
     122              :   stored: OverrideMap,
     123              :   search: string
     124              : ): OverrideMap {
     125              :   if (requests.includes('')) {
     126              :     writeStoredState({ search, overrides: {} })
     127              :     return {}
     128              :   }
     129              : 
     130              :   const overrides: OverrideMap = { ...stored }
     131              :   for (const request of requests) {
     132              :     const [name, rawValue] = splitRequest(request)
     133              :     overrides[name] = parseOverrideValue(rawValue)
     134              :   }
     135              : 
     136              :   writeStoredState({ search, overrides })
     137              :   return overrides
     138              : }
     139              : 
     140              : function loadSessionOverrides(): OverrideMap {
     141              :   const stored = readStoredState()
     142              :   const search = window.location.search
     143              :   if (search === stored.search) return stored.overrides
     144              : 
     145              :   const requests = readOverrideRequests(search)
     146              :   if (requests.length === 0) return stored.overrides
     147              : 
     148              :   return captureRequests(requests, stored.overrides, search)
     149              : }
     150              : 
     151              : /**
     152              :  * Gets a session override for any feature flag, requested via `?ff=name` to
     153              :  * turn it on or `?ff=name:value` for a specific value, repeatable to override
     154              :  * several flags at once. A nameless `?ff=` clears the session.
     155              :  *
     156              :  * The request is captured into `sessionStorage` on the first read, so it
     157              :  * survives reloads and in-app navigation but dies when the tab closes. Capture
     158              :  * happens before authentication resolves; the employee check is re-evaluated
     159              :  * on every read instead, against the SDK's current user. That read carries no
     160              :  * reactive dependency of its own, so a flag flips on the next read after the
     161              :  * user is known, not the moment it is.
     162              :  *
     163              :  * Returns undefined (not null) as the "no override" sentinel, matching
     164              :  * `getDevOverride`.
     165              :  */
     166      3528805 : export function getSessionOverride<T>(
     167              :   flagKey: string & { readonly valueType?: T }
     168              : ): T | undefined {
     169              :   if (!isCloud) return undefined
     170              : 
     171              :   const overrides = loadSessionOverrides()
     172              :   if (!Object.hasOwn(overrides, flagKey)) return undefined
     173              :   if (!isComfyEmployee()) return undefined
     174              : 
     175              :   return overrides[flagKey] as T
     176              : }
        

Generated by: LCOV version 2.0-1