bun-ws-router
Version:
Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.
64 lines • 2.64 kB
JavaScript
// SPDX-FileCopyrightText: 2025-present Kriasoft
// SPDX-License-Identifier: MIT
/**
* Handles WebSocket connection lifecycle events (open/close).
*
* DESIGN: Multiple handlers can be registered for the same event,
* executing in registration order. Errors in one handler don't affect others.
*/
export class ConnectionHandler {
openHandlers = [];
closeHandlers = [];
addOpenHandler(handler) {
this.openHandlers.push(handler);
}
addCloseHandler(handler) {
this.closeHandlers.push(handler);
}
handleOpen(ws, send) {
const clientId = ws.data.clientId;
console.log(`[ws] Connection opened: ${clientId}`);
const context = { ws, send };
// Execute all registered open handlers
// BEHAVIOR: Handlers run sequentially but don't await promises.
// Errors are logged but don't prevent other handlers from running.
this.openHandlers.forEach((handler) => {
try {
const result = handler(context);
// Fire-and-forget async handlers - errors logged but not propagated
if (result instanceof Promise) {
result.catch((error) => {
console.error(`Unhandled promise rejection in open handler for ${clientId}:`, error);
});
}
}
catch (error) {
console.error(`Error in open handler for ${clientId}:`, error);
}
});
}
handleClose(ws, code, reason, send) {
const clientId = ws.data.clientId;
console.log(`[ws] Connection closed: ${clientId} (Code: ${code}, Reason: ${reason || "N/A"})`);
const context = { ws, code, reason, send };
// Execute all registered close handlers
// NOTE: Close handlers still execute even if connection is already closed.
// This ensures cleanup code always runs.
this.closeHandlers.forEach((handler) => {
try {
const result = handler(context);
// Fire-and-forget async handlers - errors logged but not propagated
if (result instanceof Promise) {
result.catch((error) => {
console.error(`[ws] Unhandled promise rejection in close handler for ${clientId}:`, error);
});
}
}
catch (error) {
// Catch synchronous errors in handlers
console.error(`[ws] Error in close handler for ${clientId}:`, error);
}
});
}
}
//# sourceMappingURL=connection.js.map