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

            Line data    Source code
       1              : // Using a template string for this is resulting in complex type workarounds. No current benefit beyond dev reading.
       2              : import type { UUID } from '@comfyorg/graph-model/uuid'
       3              : import { zeroUuid } from '@comfyorg/graph-model/uuid'
       4              : export type { UUID } from '@comfyorg/graph-model/uuid'
       5              : 
       6              : /** Pre-allocated storage for uuid random values. */
       7         2356 : const randomStorage = new Uint32Array(31)
       8              : 
       9              : /** Thrown by {@link createUuidv4} when no Web Crypto implementation is available. */
      10         2356 : export class UuidGenerationError extends Error {
      11              :   constructor(message: string) {
      12            0 :     super(message)
      13            0 :     this.name = 'UuidGenerationError'
      14              :   }
      15              : }
      16              : 
      17        10629 : function getWebCrypto(): Crypto | undefined {
      18        10629 :   return globalThis.crypto
      19              : }
      20              : 
      21              : /**
      22              :  * Creates a UUIDv4 string.
      23              :  * @returns A new UUIDv4 string
      24              :  * @throws {UuidGenerationError} When neither {@link crypto.randomUUID} nor
      25              :  * {@link crypto.getRandomValues} is available.
      26              :  * @remarks
      27              :  * Original implementation from https://gist.github.com/jed/982883?permalink_comment_id=852670#gistcomment-852670
      28              :  *
      29              :  * Prefers the {@link crypto.randomUUID} method if available, falling back to
      30              :  * {@link crypto.getRandomValues} for insecure contexts.
      31              :  */
      32        10629 : export function createUuidv4(): UUID {
      33        10629 :   const webCrypto = getWebCrypto()
      34        10629 :   if (typeof webCrypto?.randomUUID === 'function') {
      35        10629 :     return webCrypto.randomUUID()
      36              :   }
      37        10629 :   if (typeof webCrypto?.getRandomValues === 'function') {
      38            0 :     const random = webCrypto.getRandomValues(randomStorage)
      39            0 :     let i = 0
      40            0 :     return '10000000-1000-4000-8000-100000000000'.replaceAll(/[018]/g, (a) =>
      41              :       (
      42              :         Number(a) ^
      43              :         ((random[i++] * 3.725_290_298_461_914e-9) >> (Number(a) * 0.25))
      44              :       ).toString(16)
      45              :     )
      46              :   }
      47            0 :   throw new UuidGenerationError('Web Crypto is required to generate a UUID')
      48              : }
      49              : 
      50              : /** Ensures an entity has a stable, non-zero UUID and returns it. */
      51         3915 : export function ensureNonZeroUuid(entity: { id: UUID }): UUID {
      52         3915 :   if (entity.id === zeroUuid) entity.id = createUuidv4()
      53         3915 :   return entity.id
      54              : }
        

Generated by: LCOV version 2.0-1