LCOV - code coverage report
Current view: top level - src/platform/openOnCloud - desktopWorkflowHandoff.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 11.9 % 59 7
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { z } from 'zod'
       2              : 
       3              : import {
       4              :   cloudEditorUrl,
       5              :   LOCAL_HANDOFF_PARAM
       6              : } from '@/platform/openOnCloud/localHandoffProtocol'
       7              : 
       8         2356 : export const DESKTOP_RELAY_PARAM = 'desktop_workflow'
       9         2356 : export const DESKTOP_HANDOFF_HASH = '#desktop_workflow='
      10         2356 : export const DESKTOP_HANDOFF_LIFETIME_MS = 10 * 60_000
      11         2356 : const MAX_HANDOFF_BYTES = 2 * 1024 * 1024
      12         2356 : const MAX_ENCODED_LENGTH = 1024 * 1024
      13         2356 : const STORAGE_PREFIX = 'Comfy.OpenOnCloud.Desktop.'
      14              : 
      15         2356 : const zDesktopWorkflowHandoff = z.object({
      16              :   filename: z.string().min(1).max(256),
      17              :   workflow: z.unknown(),
      18              :   expiresAt: z.number()
      19              : })
      20              : 
      21              : export type DesktopWorkflowHandoff = z.infer<typeof zDesktopWorkflowHandoff>
      22              : 
      23            0 : export async function desktopWorkflowUrl(
      24              :   nonce: string,
      25              :   handoff: DesktopWorkflowHandoff
      26              : ): Promise<string | undefined> {
      27            0 :   const body = new Blob([JSON.stringify(handoff)])
      28            0 :   if (body.size > MAX_HANDOFF_BYTES) return undefined
      29            0 :   const compressed = body.stream().pipeThrough(new CompressionStream('gzip'))
      30            0 :   const bytes = new Uint8Array(await new Response(compressed).arrayBuffer())
      31            0 :   const encoded = btoa(
      32              :     Array.from(bytes, (byte) => String.fromCharCode(byte)).join('')
      33              :   )
      34            0 :   if (encoded.length > MAX_ENCODED_LENGTH) return undefined
      35            0 :   const url = new URL(cloudEditorUrl(nonce))
      36            0 :   url.hash = `${DESKTOP_HANDOFF_HASH}${encoded}`
      37            0 :   return url.href
      38              : }
      39              : 
      40            0 : async function readBoundedHandoff(
      41              :   stream: ReadableStream<Uint8Array<ArrayBuffer>>
      42              : ) {
      43            0 :   const reader = stream.getReader()
      44            0 :   const chunks: Uint8Array<ArrayBuffer>[] = []
      45            0 :   let size = 0
      46            0 :   try {
      47            0 :     for (;;) {
      48            0 :       const { done, value } = await reader.read()
      49            0 :       if (done) return await new Blob(chunks).text()
      50            0 :       size += value.byteLength
      51            0 :       if (size > MAX_HANDOFF_BYTES) {
      52            0 :         await reader.cancel()
      53            0 :         return undefined
      54              :       }
      55            0 :       chunks.push(value)
      56              :     }
      57              :   } finally {
      58            0 :     reader.releaseLock()
      59              :   }
      60              : }
      61              : 
      62            0 : function readStoredHandoff(
      63              :   value: string | null
      64              : ): DesktopWorkflowHandoff | undefined {
      65            0 :   if (!value) return undefined
      66            0 :   try {
      67            0 :     const parsed = zDesktopWorkflowHandoff.safeParse(JSON.parse(value))
      68            0 :     return parsed.success && parsed.data.expiresAt > Date.now()
      69              :       ? parsed.data
      70              :       : undefined
      71              :   } catch {
      72            0 :     return undefined
      73              :   }
      74              : }
      75              : 
      76            0 : export async function captureDesktopWorkflowHandoff(
      77              :   url = new URL(window.location.href)
      78              : ): Promise<boolean> {
      79            0 :   if (!url.hash.startsWith(DESKTOP_HANDOFF_HASH)) return false
      80            0 :   const encoded = url.hash.slice(DESKTOP_HANDOFF_HASH.length)
      81            0 :   url.hash = ''
      82            0 :   try {
      83            0 :     window.history.replaceState(null, '', url.href)
      84            0 :     const nonce = url.searchParams.get(LOCAL_HANDOFF_PARAM)
      85            0 :     if (
      86              :       !nonce ||
      87              :       !z.uuid().safeParse(nonce).success ||
      88              :       encoded.length > MAX_ENCODED_LENGTH
      89              :     )
      90            0 :       return false
      91            0 :     const bytes = Uint8Array.from(atob(encoded), (character) =>
      92              :       character.charCodeAt(0)
      93              :     )
      94              :     const stream = new Blob([bytes])
      95              :       .stream()
      96              :       .pipeThrough(new DecompressionStream('gzip'))
      97              :     const value = await readBoundedHandoff(stream)
      98            0 :     const handoff = readStoredHandoff(value ?? null)
      99            0 :     if (!handoff) return false
     100            0 :     sessionStorage.setItem(`${STORAGE_PREFIX}${nonce}`, JSON.stringify(handoff))
     101            0 :     return true
     102              :   } catch {
     103            0 :     return false
     104              :   }
     105              : }
     106              : 
     107            0 : export function takeDesktopWorkflowHandoff(
     108              :   nonce: string
     109              : ): DesktopWorkflowHandoff | undefined {
     110            0 :   try {
     111            0 :     const key = `${STORAGE_PREFIX}${nonce}`
     112            0 :     const value = sessionStorage.getItem(key)
     113            0 :     sessionStorage.removeItem(key)
     114            0 :     return readStoredHandoff(value)
     115              :   } catch {
     116            0 :     return undefined
     117              :   }
     118              : }
        

Generated by: LCOV version 2.0-1