@ws-kit/bun
Version:
Bun platform adapter for WS-Kit leveraging native WebSocket API with built-in pub/sub and low-latency message routing
42 lines • 1.61 kB
JavaScript
// SPDX-FileCopyrightText: 2025-present Kriasoft
// SPDX-License-Identifier: MIT
/**
* Adapts Bun's ServerWebSocket to conform to the core interface.
*
* Bun's ServerWebSocket already implements all required methods and properties,
* so this adapter is primarily for type safety and documentation.
*
* **Note**: This adapter has no runtime overhead—it's purely for TypeScript
* type checking. At runtime, we pass through to Bun's native WebSocket.
*
* This function is for Bun-specific application code. The router itself receives
* platform-specific WebSockets and handles the adaptation internally.
*
* @param ws - Bun's ServerWebSocket instance (can have generic TData)
* @returns The same WebSocket (no-op at runtime) cast to core's non-generic ServerWebSocket
*/
export function adaptBunWebSocket(ws) {
// Verify the interface at compile time
// At runtime, this is a no-op—we just return the WebSocket as-is
const socket = ws;
return socket;
}
/**
* Type guard to check if an object is a Bun ServerWebSocket.
*
* Useful for platform detection or conditional logic.
*
* @param ws - Unknown WebSocket-like object
* @returns true if ws has Bun ServerWebSocket methods
*/
export function isBunWebSocket(ws) {
if (!ws || typeof ws !== "object")
return false;
const socket = ws;
return (typeof socket.send === "function" &&
typeof socket.close === "function" &&
typeof socket.subscribe === "function" &&
typeof socket.unsubscribe === "function" &&
socket.data !== undefined);
}
//# sourceMappingURL=websocket.js.map