export { ChannelMux, getChannelMux }; export type { ReconcileOutcome, ServerTransport }; import type { ReconciledPayload } from '../shared-ws.js'; import type { ServerChannel } from './channel.js'; /** A transport-owned connection handle. The mux never looks inside one — it only compares them by * identity and hands them back to the transport that created it. */ type Wire = unknown; type ServerTransport = { getSessionId(connection: TConnection): string | undefined; setSessionId(connection: TConnection, sessionId: string): void; /** Stable per-connection id, or `null` for transports that don't multiplex client→server * traffic across requests (WebSocket: every frame already lands on the same socket). */ getConnId(connection: TConnection): string | null; sendNow(connection: TConnection, frame: Uint8Array): void; terminateConnection(connection: TConnection): void; }; /** Emitted only when `handleFrame` consumed a reconcile. Caller threads the payload through * to `sendReconciled` and fires `finalizeUpgrade` once the new wire's reconciled has emitted. * `finalizeUpgrade` is null when this isn't an SSE→WS upgrade. */ type ReconcileOutcome = { sessionId: string; openList: ReconciledPayload['open']; finalizeUpgrade: (() => void) | null; /** The wire this RECONCILED belongs on — a barrier reconciles the staged WS, not the sender. */ deliverTo: Wire; upgradeId?: string; }; declare function getChannelMux(): ChannelMux; declare class ChannelMux { private readonly channels; /** Waiters registered by `attach` when a reconcile lands before the channel is registered. * Fired synchronously from `registerChannel`. */ private readonly pendingRegisterWaiters; private readonly sessions; private readonly sessionFinalizers; private readonly connectionEntries; /** Reverse index for transports with a stable connId (SSE). Lets data POSTs locate the * live stream connection, and catches a duplicate-connId reconnect racing teardown. */ private readonly connectionsByConnId; private readonly stagedUpgrades; private readonly stagedByPrevSession; private stagedBytes; /** Resolved lazily so the mux can be constructed at module-load (the globalObject factory * runs before `serverConfig` is initialized). */ private resolvedOptions; private get options(); /** Exposed for transport-level race timers (SSE's `waitForConnection`). */ get connectTtl(): number; /** Callers must not invoke `channel._registerChannel()` directly. */ registerChannel(channel: ServerChannel): void; unregisterChannel(channelId: string): void; hasChannels(): boolean; onConnectionOpen(connection: TConnection, transport: ServerTransport): void; onConnectionRawMessage(connection: Wire, rawFrame: Uint8Array): Promise; onConnectionRawMessageDeferredReconciled(connection: Wire, rawFrame: Uint8Array): Promise; sendReconciled(outcome: ReconcileOutcome): void; onConnectionClosed(connection: Wire, { permanent }: { permanent: boolean; }): void; readPermanentTermination(connection: Wire): boolean | null; /** SSE data POST: resolve the stream connection by its stable connId. Undefined when the * connection hasn't reconciled yet or has already torn down. */ getConnectionByConnId(connId: string): TConnection | undefined; /** PING bypasses the recv chain — serializing it would tie liveness to the slowest * awaitable on the connection. */ private dispatchInbound; /** Control frames are bounded by what the protocol itself can describe; only the data plane * carries user payloads, and only it gets the multi-megabyte allowance. */ private isOverBudget; private runInboundTurn; /** `connection` may be a wire other than the one that sent the offending frame — a barrier's * violation is the staged probe's. */ private terminateWire; /** Returns a `ReconcileOutcome` only on reconcile (so the caller decides when to send * `reconciled`). Anything but reconcile/ping before reconciliation is a violation. */ private handleFrame; /** An ordinary reconcile claims its session, abandoning any probe staged on it — unless a barrier * is mid-commit on that session, in which case the claim is refused instead. */ private claimSessionForReconcile; private handlePrepare; private handleBarrier; private settleBarrierCommit; /** Forgets the stage AND kills the probe holding it. No-op once the stage is committing. */ private abandonStage; /** Bookkeeping only — the probe wire is left alone. */ private clearStage; /** `isBarrier`: the old wire's session is retired by this commit, so it gets a finalizer that * FINs it once the new wire's RECONCILED has gone out. */ private reconcile; private reconcileSession; /** First reconcile (`initial:true`) races channel registration against `connectTtl`; later * reconciles fail fast if the channel is gone. */ private attach; /** Drains replay frames missed since `lastSeq` (sends are sync — see `send`), then * attaches an `IndexedPeer`. Returns null if the channel already shut down. */ private attachChannel; private waitForChannelRegistration; /** Transient: leave registry entries so the next reconcile's prev-comparison can fire * `recovery-failed` on abandoned channels. Permanent: remove outright — no resume. */ private detachSession; private detachHandle; /** Sole server→client send path; sync so wire order = call order. Per-channel * byte+msg credit (see `flow-control/`) bounds queue growth. */ private send; private chainRecv; private clearPingTimer; private resetPingTimer; }