/** * Lifecycle hooks: onError sink + activity tracking. * All errors (validation, middleware, handler, pubsub) flow here. * Telemetry plugin subscribes internally to observe patterns. * Activity tracking enables heartbeat and connection monitoring. * * Two types of lifecycle handlers: * 1. Internal handlers (plugins): receive raw ws, used for infrastructure setup * 2. Router-level handlers (user code): receive full typed context via router.onOpen()/onClose() */ import type { ConnectionData, MinimalContext } from "../context/base-context.js"; import type { BaseCloseContext, BaseOpenContext, LifecycleErrorContext } from "../context/lifecycle-context.js"; import type { ServerWebSocket } from "../ws/platform-adapter.js"; export type ErrorHandler = (err: unknown, ctx: MinimalContext | LifecycleErrorContext | null) => void | Promise; /** * Internal open handler (for plugins). * Receives raw WebSocket for infrastructure setup. */ export type InternalOpenHandler = (ws: ServerWebSocket) => void | Promise; /** * Internal close handler (for plugins). * Receives raw WebSocket for cleanup. */ export type InternalCloseHandler = (ws: ServerWebSocket, code?: number, reason?: string) => void | Promise; /** * Router-level open handler (for user code). * Receives full typed context with capability-gated methods. */ export type RouterOpenHandler = (ctx: BaseOpenContext) => void | Promise; /** * Router-level close handler (for user code). * Receives full typed context with capability-gated methods. */ export type RouterCloseHandler = (ctx: BaseCloseContext) => void | Promise; /** * Managed lifecycle sink with error handling and open/close notifications. * - Tracks error handlers for the onError hook * - Tracks internal open/close handlers for plugin infrastructure * - Tracks router-level open/close handlers for user code (with full context) * - Tracks last activity timestamp per connection for heartbeat monitoring */ export declare class LifecycleManager { private errorHandlers; private internalOpenHandlers; private internalCloseHandlers; private routerOpenHandlers; private routerCloseHandlers; private activityMap; onError(handler: ErrorHandler): void; /** * Register an internal open handler (for plugins). * These receive raw ws and run before router-level handlers. */ onInternalOpen(handler: InternalOpenHandler): void; /** * Register an internal close handler (for plugins). * These receive raw ws and run before router-level handlers. */ onInternalClose(handler: InternalCloseHandler): void; /** * Register a router-level open handler (for user code). * These receive full typed context and run after internal handlers. */ onRouterOpen(handler: RouterOpenHandler): void; /** * Register a router-level close handler (for user code). * These receive full typed context and run after internal handlers. */ onRouterClose(handler: RouterCloseHandler): void; /** * Get router-level open handlers (used by RouterImpl to run with context). */ getRouterOpenHandlers(): readonly RouterOpenHandler[]; /** * Get router-level close handlers (used by RouterImpl to run with context). */ getRouterCloseHandlers(): readonly RouterCloseHandler[]; handleError(err: unknown, ctx: MinimalContext | LifecycleErrorContext | null): Promise; /** * Handle internal open (plugins only). * Router-level handlers are run separately by RouterImpl with full context. */ handleInternalOpen(ws: ServerWebSocket): Promise; /** * Handle internal close (plugins only). * Router-level handlers are run separately by RouterImpl with full context. */ handleInternalClose(ws: ServerWebSocket, code?: number, reason?: string): Promise; /** * Mark activity on a connection (update timestamp for heartbeat). * Called after each successful message or heartbeat ACK. */ markActivity(ws: ServerWebSocket, now: number): void; /** * Get last activity timestamp for a connection. * Returns undefined if no activity recorded or connection closed. */ lastActivity(ws: ServerWebSocket): number | undefined; } //# sourceMappingURL=lifecycle.d.ts.map