LCOV - code coverage report
Current view: top level - src/platform/workflow/deploy/api - installationBundleApi.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 10.7 % 28 3
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   CreateDeployBundleData,
       3              :   DeployToCloudError
       4              : } from '@comfyorg/ingest-types'
       5              : import {
       6              :   zCreateDeployBundleResponse,
       7              :   zDeployToCloudError
       8              : } from '@comfyorg/ingest-types/zod'
       9              : import { z } from 'zod'
      10              : 
      11              : import { api } from '@/scripts/api'
      12              : import type { AuthHeader } from '@comfyorg/api-client/authTypes'
      13              : 
      14            1 : const BUNDLE_TIMEOUT_MS = 90_000
      15              : 
      16              : type BundleRequest = CreateDeployBundleData['body']
      17              : type BundleWorkflow = NonNullable<BundleRequest['workflow']>
      18              : 
      19            1 : const zBundleCreated = zCreateDeployBundleResponse.extend({
      20              :   url: z.url({ protocol: /^https$/ })
      21              : })
      22              : 
      23              : export type BundleFailureReason =
      24              :   | 'unauthorized'
      25              :   | 'too_many_bundles'
      26              :   | 'too_large'
      27              :   | 'offline'
      28              :   | 'busy'
      29              :   | 'unavailable'
      30              :   | 'invalid_request'
      31              : 
      32              : type BundlePost =
      33              :   | { ok: true; response: Response }
      34              :   | { ok: false; cause: unknown }
      35              : 
      36              : type BundleResult =
      37              :   | { ok: true; url: string }
      38              :   | { ok: false; reason: BundleFailureReason; cause?: unknown }
      39              : 
      40            1 : const REASON_BY_ERROR_CODE = {
      41              :   missing_credentials: 'unauthorized',
      42              :   unauthorized: 'unauthorized',
      43              :   invalid_api_key_format: 'unauthorized',
      44              :   too_many_bundles: 'too_many_bundles',
      45              :   too_large: 'too_large',
      46              :   offline: 'offline',
      47              :   builder_busy: 'busy',
      48              :   builder_unavailable: 'unavailable',
      49              :   builder_rejected: 'invalid_request',
      50              :   invalid_body: 'invalid_request',
      51              :   multiple_credentials: 'invalid_request'
      52              : } satisfies Record<DeployToCloudError['error'], BundleFailureReason>
      53              : 
      54            0 : async function readJson(response: Response): Promise<unknown> {
      55            0 :   try {
      56            0 :     return await response.json()
      57              :   } catch {
      58            0 :     return undefined
      59              :   }
      60              : }
      61              : 
      62            0 : function failureReason(status: number, body: unknown): BundleFailureReason {
      63            0 :   const parsed = zDeployToCloudError.safeParse(body)
      64            0 :   if (parsed.success) return REASON_BY_ERROR_CODE[parsed.data.error]
      65            0 :   if (status === 413) return 'too_large'
      66            0 :   return status < 500 ? 'invalid_request' : 'unavailable'
      67              : }
      68              : 
      69            0 : async function postBundle(
      70              :   credential: AuthHeader,
      71              :   request: BundleRequest
      72              : ): Promise<BundlePost> {
      73            0 :   try {
      74              :     const response = await api.fetchApi('/deploy/bundle', {
      75              :       method: 'POST',
      76              :       headers: { ...credential, 'Content-Type': 'application/json' },
      77              :       body: JSON.stringify(request),
      78              :       timeoutMs: BUNDLE_TIMEOUT_MS
      79              :     })
      80            0 :     return { ok: true, response }
      81              :   } catch (cause) {
      82            0 :     return { ok: false, cause }
      83              :   }
      84              : }
      85              : 
      86            0 : async function uploadBundle(
      87              :   credential: AuthHeader,
      88              :   request: BundleRequest
      89              : ): Promise<BundleResult> {
      90            0 :   const sent = await postBundle(credential, request)
      91            0 :   if (!sent.ok) return { ok: false, reason: 'unavailable', cause: sent.cause }
      92              : 
      93            0 :   const body = await readJson(sent.response)
      94            0 :   if (!sent.response.ok) {
      95            0 :     return {
      96              :       ok: false,
      97              :       reason: failureReason(sent.response.status, body),
      98              :       cause: body
      99              :     }
     100              :   }
     101              : 
     102            0 :   const created = zBundleCreated.safeParse(body)
     103            0 :   return created.success
     104              :     ? { ok: true, url: created.data.url }
     105              :     : { ok: false, reason: 'unavailable', cause: created.error }
     106              : }
     107              : 
     108            0 : export async function createInstallationBundle(
     109              :   credential: AuthHeader,
     110              :   workflow?: BundleWorkflow
     111              : ): Promise<BundleResult> {
     112            0 :   const bundle = await uploadBundle(credential, { workflow })
     113            0 :   if (!workflow || bundle.ok || bundle.reason !== 'too_large') return bundle
     114            0 :   return uploadBundle(credential, {})
     115              : }
        

Generated by: LCOV version 2.0-1