comfy-ts 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comfy-ts",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "The complete TypeScript toolkit for ComfyUI and generative AI. Fully type-safe SDK with autocomplete on every node, companion CLI, TUI, agentic docs, and 50+ examples. Design, run and deploy creative pipelines for image, video, audio, 3d and text on any ComfyUI, local, cloud or hosted.",
5
5
  "keywords": [
6
6
  "comfyui",
@@ -358,6 +358,10 @@ export class ServeApp {
358
358
  } catch (e) {
359
359
  // name, not instanceof: same two-copies problem as the var classes
360
360
  if (e instanceof Error && e.name === 'ImageVarEmptyError') return { status: 400, error: e.message }
361
+ // the ComfyUI host is upstream of this bridge: 502, not "serve crashed".
362
+ // The connect deadline (ConnectOptions.timeoutMs) is what makes this
363
+ // reachable at all — it used to hang here and block the module mutex
364
+ if (e instanceof Error && e.name === 'ComfyHostUnreachableError') return { status: 502, error: e.message }
361
365
  return { status: 500, error: extractErrorMessage(e) }
362
366
  }
363
367
  }
@@ -11,6 +11,7 @@ export { ComfyTS } from 'src/state.ts'
11
11
  export {
12
12
  ComfyHost,
13
13
  ComfyHostAuthError,
14
+ ComfyHostUnreachableError,
14
15
  type ComfyHostData,
15
16
  type ComfyHostID,
16
17
  type ConnectOptions,
@@ -74,8 +74,24 @@ export type ConnectOptions = {
74
74
  schema?: 'auto' | 'refresh' | 'cache'
75
75
  /** max cache age for 'auto' (default 24h) */
76
76
  maxAgeMs?: number
77
+ /**
78
+ * deadline for the FIRST connect, default 30s. 0 or Infinity waits forever.
79
+ * A refused TCP connection is a ws CLOSE, not a dead transport, so without a
80
+ * deadline `connect()` sat in the 2s reconnect loop and never settled: scripts
81
+ * hung with no error and `comfy-ts serve` wedged the module behind its mutex.
82
+ * Reconnects AFTER a successful connect are untouched and still retry forever.
83
+ */
84
+ timeoutMs?: number
85
+ }
86
+
87
+ /** the host never came up within ConnectOptions.timeoutMs */
88
+ export class ComfyHostUnreachableError extends Error {
89
+ /** name, not instanceof: the cli bundle and dist/index.js each define this class */
90
+ override readonly name = 'ComfyHostUnreachableError'
77
91
  }
78
92
 
93
+ const DEFAULT_CONNECT_TIMEOUT_MS = 30_000
94
+
79
95
  // biome-ignore format: misc
80
96
  type SchemaUpdateResult = Maybe<{ type: 'success' } | { type: 'error'; error: unknown }>
81
97
 
@@ -407,29 +423,61 @@ export class ComfyHost<ID extends string = string> {
407
423
  this._readyResolve = res
408
424
  this._readyReject = rej
409
425
  })
426
+ const timeoutMs = p.timeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS
427
+ if (timeoutMs > 0 && Number.isFinite(timeoutMs)) {
428
+ this._connectTimer = setTimeout(() => {
429
+ this.markFailed(
430
+ new ComfyHostUnreachableError(
431
+ `host '${this.data.id}' did not come up within ${timeoutMs}ms at ${this.getWSUrl()} — is ComfyUI running there ?`,
432
+ ),
433
+ )
434
+ }, timeoutMs)
435
+ this._connectTimer.unref?.()
436
+ }
410
437
  this.initWebsocket()
411
438
  }
412
439
  return this._ready
413
440
  }
414
441
 
442
+ private _connectTimer: Maybe<ReturnType<typeof setTimeout>> = null
443
+ private clearConnectTimer(): void {
444
+ if (this._connectTimer != null) clearTimeout(this._connectTimer)
445
+ this._connectTimer = null
446
+ }
447
+
415
448
  /** close the websocket (e.g. to let a script exit) */
416
449
  disconnect = (): void => {
450
+ this.clearConnectTimer()
417
451
  this.ws?.disconnectPermanently()
418
452
  this.ws = null
419
453
  this._ready = null
454
+ this._readyResolve = null
455
+ this._readyReject = null
420
456
  }
421
457
 
422
458
  private markReady(): void {
459
+ this.clearConnectTimer()
423
460
  this._readyResolve?.()
424
461
  this._readyResolve = null
425
462
  this._readyReject = null
426
463
  }
427
464
 
428
465
  private markFailed(error: unknown): void {
429
- // reject the pending connect() so callers fail fast instead of hanging
430
- this._readyReject?.(error)
466
+ this.clearConnectTimer()
467
+ const reject = this._readyReject
468
+ // no pending connect = this is a RECONNECT failing on an already-live host.
469
+ // Its retry loop is the resilience; never tear that down.
470
+ if (reject == null) return
431
471
  this._readyResolve = null
432
472
  this._readyReject = null
473
+ // a failed connect stays RETRYABLE: keeping the rejected promise on the
474
+ // instance made every later connect() replay the same stale error, so a host
475
+ // that came back was unreachable for the life of the process
476
+ this._ready = null
477
+ // nobody is waiting anymore: stop the 2s retry loop instead of leaking it
478
+ this.ws?.disconnectPermanently()
479
+ this.ws = null
480
+ reject(error)
433
481
  }
434
482
 
435
483
  private async writeSDKToDisk(): Promise<void> {