Skip to content

PreviewService

Defined in: src/preview.ts:445

new PreviewService(opts): PreviewService

Defined in: src/preview.ts:456

PreviewServiceOptions

PreviewService

converge(active): void

Defined in: src/preview.ts:532

Reconcile to active: ensure each entry, release any bound session absent from it.

object[]

void


devPortFor(sessionId): number | null

Defined in: src/preview.ts:639

Returns the live dev port that the bound listener for sessionId is currently targeting, or null when the session is not bound.

string

number | null


ensure(sessionId, devPort): number | null

Defined in: src/preview.ts:473

Ensure a listener exists for sessionId proxying to devPort.

  • Already bound → UPDATE the stored devPort live (no rebind, no onChange); returns the existing preview port.
  • Not bound → allocate a free slot, bind a loopback Bun.serve, fire onChange(sessionId, previewPort), return the port.
  • Range exhausted → log + return null (never throws).
  • Bind error on a slot → try the next free slot; null if all fail.

string

number

number | null


idleSince(sessionId, now): number | null

Defined in: src/preview.ts:629

Returns the number of milliseconds since the last proxied HTTP request or relayed WS frame for sessionId, measured against now. Returns null when the session is not bound (no listener exists).

CALLER CONTRACT: now MUST share a time base with the clock this service stamps lastActivityAt with (the injectable now in PreviewServiceOptions). In production both are Date.now, so callers (the poller) just pass their own Date.now(). In tests, inject the SAME clock into PreviewService AND pass its value here — mixing a fake service clock with a real Date.now() here yields a nonsense delta (a test footgun, not a prod bug).

string

number

number | null


release(sessionId): void

Defined in: src/preview.ts:514

Tear down a session’s listener and reclaim its slot. Idempotent.

string

void


snapshot(): Record<string, SessionPreviewState>

Defined in: src/preview.ts:541

Live preview snapshot for client bootstrap: bound sessions only.

Record<string, SessionPreviewState>


stopAll(): void

Defined in: src/preview.ts:550

Stop every listener (shutdown / tests). Does NOT fire onChange.

void