LCOV - code coverage report
Current view: top level - packages/account-ui/src/auth - TurnstileWidget.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 4.1 % 49 2
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1           12 : <template>
       2            0 :   <div class="flex flex-col gap-2">
       3              :     <div ref="containerRef"></div>
       4              :     <small
       5              :       v-if="errorMessage"
       6              :       role="alert"
       7              :       aria-live="assertive"
       8              :       :class="errorClass"
       9              :       >{{ errorMessage }}</small
      10              :     >
      11              :   </div>
      12              : </template>
      13              : 
      14              : <script setup lang="ts">
      15              : import { useTimeoutFn } from '@vueuse/core'
      16              : import { onBeforeUnmount, onMounted, ref } from 'vue'
      17              : 
      18              : import type { TurnstileApi } from '@comfyorg/account-core/turnstileScript'
      19              : import { loadTurnstile } from '@comfyorg/account-core/turnstileScript'
      20              : 
      21              : const {
      22              :   siteKey,
      23              :   theme = 'auto',
      24              :   expiredMessage,
      25              :   failedMessage,
      26              :   errorClass,
      27              :   loader = loadTurnstile
      28              : } = defineProps<{
      29              :   siteKey: string
      30              :   theme?: 'light' | 'dark' | 'auto'
      31              :   /** Host-translated copy for a challenge that expired on its own. */
      32              :   expiredMessage: string
      33              :   /** Host-translated copy for a widget that failed to load or errored. */
      34              :   failedMessage: string
      35              :   /** Host styling for the inline error line; the package ships no colors. */
      36              :   errorClass?: string
      37              :   /** Injectable so hosts and tests control the script load. */
      38              :   loader?: () => Promise<TurnstileApi>
      39              : }>()
      40              : 
      41            0 : const token = defineModel<string>('token', { default: '' })
      42              : /**
      43              :  * Set true whenever the widget cannot be relied on to ever produce a token:
      44              :  * the Cloudflare script failed to load, the rendered challenge errored out,
      45              :  * or it simply hasn't resolved within `TURNSTILE_LOAD_TIMEOUT_MS`. The parent
      46              :  * uses this to stop waiting on a token so a broken/slow widget (network
      47              :  * issue, ad-blocker, CDN outage) can never permanently block signup.
      48              :  */
      49            0 : const unavailable = defineModel<boolean>('unavailable', { default: false })
      50              : 
      51            0 : const containerRef = ref<HTMLDivElement>()
      52            0 : const errorMessage = ref('')
      53            0 : let widgetId: string | undefined
      54              : /** The API the loader resolved; the one that rendered the widget owns it. */
      55            0 : let turnstile: TurnstileApi | undefined
      56              : 
      57              : /** How long to wait for the widget to resolve before falling back. */
      58           13 : const TURNSTILE_LOAD_TIMEOUT_MS = 9_000
      59            0 : const { start: armTimeout, stop: clearLoadTimeout } = useTimeoutFn(
      60              :   () => {
      61            0 :     unavailable.value = true
      62              :   },
      63              :   TURNSTILE_LOAD_TIMEOUT_MS,
      64              :   { immediate: false }
      65              : )
      66              : 
      67            0 : const clearToken = () => {
      68            0 :   token.value = ''
      69              : }
      70              : 
      71              : /**
      72              :  * Fetch a fresh challenge and clear the current token.
      73              :  *
      74              :  * Turnstile tokens are single-use, so after a token is consumed by a submit
      75              :  * attempt that did not succeed, the spent token must be discarded and a new
      76              :  * challenge requested. Clearing the model re-blocks submission until the user
      77              :  * solves the fresh challenge; clearing the error drops any stale failure text
      78              :  * so it can't linger over the new challenge.
      79              :  */
      80            0 : const reset = () => {
      81            0 :   clearToken()
      82            0 :   errorMessage.value = ''
      83            0 :   if (widgetId && turnstile) {
      84            0 :     turnstile.reset(widgetId)
      85              :     // A widget that renders can request a fresh challenge, so give it
      86              :     // another chance before falling back again.
      87            0 :     unavailable.value = false
      88            0 :     armTimeout()
      89              :   }
      90              : }
      91              : 
      92            0 : defineExpose({ reset })
      93              : 
      94            0 : onMounted(async () => {
      95            0 :   armTimeout()
      96              : 
      97            0 :   try {
      98            0 :     turnstile = await loader()
      99            0 :     if (!containerRef.value) return
     100              : 
     101            0 :     widgetId = turnstile.render(containerRef.value, {
     102              :       sitekey: siteKey,
     103              :       theme,
     104              :       callback: (newToken: string) => {
     105            0 :         clearLoadTimeout()
     106            0 :         errorMessage.value = ''
     107            0 :         unavailable.value = false
     108            0 :         token.value = newToken
     109              :       },
     110              :       'expired-callback': () => {
     111            0 :         clearToken()
     112            0 :         errorMessage.value = expiredMessage
     113            0 :         if (widgetId && turnstile) {
     114            0 :           turnstile.reset(widgetId)
     115              :           // A solved token can expire on its own (e.g. the tab was
     116              :           // backgrounded past the token's ~300s lifetime) without the widget
     117              :           // ever erroring, so proactively request a fresh challenge and
     118              :           // re-arm the load timeout in case it doesn't resolve in time.
     119            0 :           armTimeout()
     120              :         }
     121              :       },
     122              :       'error-callback': () => {
     123            0 :         clearToken()
     124            0 :         clearLoadTimeout()
     125            0 :         console.warn('Turnstile challenge failed')
     126            0 :         errorMessage.value = failedMessage
     127            0 :         unavailable.value = true
     128            0 :         if (widgetId && turnstile) turnstile.reset(widgetId)
     129              :       }
     130              :     })
     131              :   } catch (error) {
     132            0 :     clearLoadTimeout()
     133            0 :     console.warn('Turnstile failed to load', error)
     134            0 :     errorMessage.value = failedMessage
     135            0 :     unavailable.value = true
     136              :   }
     137              : })
     138              : 
     139            0 : onBeforeUnmount(() => {
     140            0 :   if (widgetId && turnstile) {
     141            0 :     turnstile.remove(widgetId)
     142              :   }
     143              : })
     144              : </script>
        

Generated by: LCOV version 2.0-1