LCOV - code coverage report
Current view: top level - src/platform/customNodes - customNodeScripts.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 65.9 % 44 29
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { CpuServerReading } from '@/platform/customNodes/lateLoading/cpuServerStep'
       2              : import {
       3              :   importNoticingFetchFailure,
       4              :   loadCustomNodesWithDeadline
       5              : } from '@/platform/customNodes/lateLoading/lateLoading'
       6              : import {
       7              :   claimExtensionImportError,
       8              :   CUSTOM_NODE_FAILURES_TRUNCATED_FIELD,
       9              :   CUSTOM_NODE_LOAD_ERROR_TYPE
      10              : } from '@/platform/telemetry/customNodeLoadTelemetry'
      11              : import type { CustomNodeFailureDiagnostic } from '@/platform/telemetry/customNodeLoadTelemetry'
      12              : import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer'
      13              : import { redactTelemetryUrls } from '@comfyorg/telemetry/redactTelemetryUrls'
      14              : import { reportError } from '@comfyorg/telemetry/reportError'
      15              : import { api } from '@/scripts/api'
      16              : import {
      17              :   getErrorMessage,
      18              :   toError
      19              : } from '@comfyorg/shared-frontend-utils/errorUtil'
      20              : 
      21         2355 : const INLINED_CLOUD_EXTENSIONS = new Set([
      22              :   '/extensions/cloud/rum.js',
      23              :   '/extensions/cloud/sentry.js'
      24              : ])
      25              : 
      26        28182 : export function getExtensionPath(script: string): string | undefined {
      27        28182 :   try {
      28              :     const pathname = new URL(script, window.location.href).pathname
      29        28182 :     return pathname.match(/^\/extensions\/[^/]+\//)?.[0]
      30              :   } catch {
      31            0 :     return undefined
      32              :   }
      33              : }
      34              : 
      35       347368 : export function shouldLoadCustomNodeScript(
      36              :   script: string,
      37              :   {
      38              :     isCloudBuild,
      39              :     disabledExtensionPaths = [],
      40              :     disableAllCustomExtensions = false
      41              :   }: {
      42              :     isCloudBuild: boolean
      43              :     disabledExtensionPaths?: readonly string[]
      44              :     disableAllCustomExtensions?: boolean
      45              :   }
      46              : ): boolean {
      47       347368 :   if (script.includes('extensions/core')) return false
      48       347368 :   if (isCloudBuild && INLINED_CLOUD_EXTENSIONS.has(script)) return false
      49       347368 :   if (disableAllCustomExtensions) return false
      50              : 
      51        18788 :   const extensionPath = getExtensionPath(script)
      52       347368 :   return (
      53              :     extensionPath === undefined ||
      54              :     !disabledExtensionPaths.includes(extensionPath)
      55              :   )
      56              : }
      57              : 
      58         2355 : const MAX_CUSTOM_NODE_PATH_LENGTH = 512
      59         2355 : const MAX_CUSTOM_NODE_DIAGNOSTIC_LENGTH = 2048
      60         2355 : const MAX_REPORTED_FAILURES = 10
      61              : 
      62              : export interface CustomNodeLoadFailure {
      63              :   ext: string
      64              :   error: unknown
      65              : }
      66              : 
      67            0 : function toFailureDiagnostic({
      68              :   ext,
      69              :   error
      70              : }: CustomNodeLoadFailure): CustomNodeFailureDiagnostic {
      71            0 :   const path = ext.slice(0, MAX_CUSTOM_NODE_PATH_LENGTH)
      72            0 :   try {
      73            0 :     return {
      74              :       ext: path,
      75              :       message: (getErrorMessage(error) ?? toError(error).message).slice(
      76              :         0,
      77              :         MAX_CUSTOM_NODE_DIAGNOSTIC_LENGTH
      78              :       ),
      79              :       stack:
      80              :         error instanceof Error
      81              :           ? error.stack?.slice(0, MAX_CUSTOM_NODE_DIAGNOSTIC_LENGTH)
      82              :           : undefined
      83              :     }
      84              :   } catch {
      85            0 :     return { ext: path, message: 'Unknown extension load failure' }
      86              :   }
      87              : }
      88              : 
      89         9393 : async function importModule(url: string): Promise<unknown> {
      90         9393 :   try {
      91         9393 :     return await import(/* @vite-ignore */ url)
      92              :   } catch (error) {
      93            0 :     claimExtensionImportError(error)
      94            0 :     throw error
      95              :   }
      96              : }
      97              : 
      98         9393 : async function importCustomNodeScript(
      99              :   ext: string,
     100              :   { late = false, retry = false } = {}
     101              : ): Promise<CustomNodeLoadFailure | undefined> {
     102         9393 :   const base = api.fileURL(ext)
     103         9393 :   const url = retry ? `${base}${base.includes('?') ? '&' : '?'}retry=1` : base
     104         9393 :   try {
     105         9393 :     await (late
     106              :       ? importNoticingFetchFailure(url, importModule)
     107              :       : importModule(url))
     108              :   } catch (error) {
     109            0 :     return { ext, error }
     110              :   }
     111              : }
     112              : 
     113         2355 : export function reportCustomNodeLoadFailures(
     114              :   failures: CustomNodeLoadFailure[]
     115              : ): void {
     116         2355 :   if (failures.length === 0) return
     117              : 
     118            0 :   const reportedFailures = failures
     119              :     .slice(0, MAX_REPORTED_FAILURES)
     120              :     .map(toFailureDiagnostic)
     121            0 :   const errors = reportedFailures.map(({ message, stack }) =>
     122              :     Object.assign(new Error(message), stack ? { stack } : {})
     123              :   )
     124            0 :   const aggregate = new AggregateError(errors, 'Error loading extensions')
     125            0 :   const failureLines = reportedFailures.map(
     126              :     ({ ext, message }) =>
     127              :       `\nFailed extension: ${ext}: ${redactTelemetryUrls(message.split('\n')[0])}`
     128              :   )
     129         2355 :   aggregate.stack = `${aggregate.stack ?? aggregate.message}\nFailed extensions: ${reportedFailures.map(({ ext }) => ext).join(', ')}${failureLines.join('')}`
     130              : 
     131         2355 :   reportError(aggregate, {
     132              :     errorType: CUSTOM_NODE_LOAD_ERROR_TYPE,
     133              :     surface: 'platform',
     134              :     level: 'warning',
     135              :     tags: { failed_extension_count: failures.length },
     136              :     context: {
     137              :       failures: reportedFailures,
     138              :       ...(failures.length > MAX_REPORTED_FAILURES && {
     139              :         [CUSTOM_NODE_FAILURES_TRUNCATED_FIELD]: true
     140              :       })
     141              :     }
     142              :   })
     143              : }
     144              : 
     145              : /**
     146              :  * Imports the custom node scripts among `servedScripts` in parallel and reports the failures.
     147              :  * With a CPU server reading, scripts that miss the deadline turn on late until `signal` aborts.
     148              :  */
     149         2355 : export async function loadCustomNodeScripts(
     150              :   servedScripts: string[],
     151              :   signal: AbortSignal,
     152              :   firstCpuServerReading: Promise<CpuServerReading> | undefined,
     153              :   {
     154              :     disabledExtensionPaths = [],
     155              :     disableAllCustomExtensions = false
     156              :   }: {
     157              :     disabledExtensionPaths?: readonly string[]
     158              :     disableAllCustomExtensions?: boolean
     159              :   } = {}
     160              : ) {
     161         2355 :   const scripts = servedScripts.filter((script) =>
     162              :     shouldLoadCustomNodeScript(script, {
     163              :       isCloudBuild: __DISTRIBUTION__ === 'cloud',
     164              :       disabledExtensionPaths,
     165              :       disableAllCustomExtensions
     166              :     })
     167              :   )
     168         2355 :   if (firstCpuServerReading) {
     169            0 :     await bootstrapTracer.settle('bootstrap/extensions-load-custom', () =>
     170              :       loadCustomNodesWithDeadline(
     171              :         scripts,
     172              :         {
     173              :           importScript: (ext, { retry } = {}) =>
     174              :             importCustomNodeScript(ext, { late: true, retry }),
     175              :           reportFailures: reportCustomNodeLoadFailures
     176              :         },
     177              :         signal,
     178              :         firstCpuServerReading
     179              :       )
     180              :     )
     181            0 :     return
     182              :   }
     183              :   const outcomes = await bootstrapTracer.settle(
     184              :     'bootstrap/extensions-load-custom',
     185              :     () => Promise.all(scripts.map((ext) => importCustomNodeScript(ext)))
     186              :   )
     187         2355 :   reportCustomNodeLoadFailures(outcomes.filter((outcome) => !!outcome))
     188              : }
        

Generated by: LCOV version 2.0-1