diff --git a/README.md b/README.md index d5d7294..896a01a 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ const { websocket } = bunAdapter({ onMessage: console.log }); Bun.serve({ port: 3000, + websocket, fetch(req, server) { if (server.upgrade(req)) { return; @@ -87,7 +88,6 @@ Bun.serve({ { headers: { "content-type": "text/html" } }, ); }, - websocket, }); ``` diff --git a/package.json b/package.json index e74cd86..bb1968c 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "@types/web": "^0.0.135", "@types/ws": "^8.5.10", "@vitest/coverage-v8": "^1.2.2", + "bun-types": "^1.0.25", "changelogen": "^0.5.5", "eslint": "^8.56.0", "eslint-config-unjs": "^0.2.1", diff --git a/playground/bun.ts b/playground/bun.ts index 8aa8142..e8b7d58 100644 --- a/playground/bun.ts +++ b/playground/bun.ts @@ -1,5 +1,7 @@ // You can run this demo using `bun --bun ./bun.ts` or `npm run play:bun` in repo +/// + import bunAdapter from "../src/adapters/bun"; import { createDemo, getIndexHTMLURL } from "./_common"; @@ -7,6 +9,7 @@ const adapter = createDemo(bunAdapter); Bun.serve({ port: 3001, + websocket: adapter.websocket, fetch(req, server) { if (server.upgrade(req)) { return; @@ -15,5 +18,4 @@ Bun.serve({ headers: { "Content-Type": "text/html" }, }); }, - websocket: adapter.websocket, }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 74532a1..5ee581a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ devDependencies: '@vitest/coverage-v8': specifier: ^1.2.2 version: 1.2.2(vitest@1.2.2) + bun-types: + specifier: ^1.0.25 + version: 1.0.25 changelogen: specifier: ^0.5.5 version: 0.5.5 @@ -1571,6 +1574,14 @@ packages: semver: 7.5.4 dev: true + /bun-types@1.0.25: + resolution: {integrity: sha512-9lxeUR/OJsvlZH4GOWteiAdx7ikrSxCUX7Rr0JJux+DrR3LejouVLxIZnTeQ3UPAZovvSgKivWeHPJ2wlo7/Kg==} + dependencies: + '@types/node': 20.11.10 + '@types/ws': 8.5.10 + undici-types: 5.26.5 + dev: true + /bundle-name@3.0.0: resolution: {integrity: sha512-PKA4BeSvBpQKQ8iPOGCSiell+N8P+Tf1DlwqmYhpe2gAhKPHn8EYOxVT+ShuGmhg8lN8XiSlS80yiExKXrURlw==} engines: {node: '>=12'} diff --git a/src/adapters/bun.ts b/src/adapters/bun.ts index af9af8f..fb31e70 100644 --- a/src/adapters/bun.ts +++ b/src/adapters/bun.ts @@ -1,8 +1,9 @@ // https://bun.sh/docs/api/websockets -import type { BunWSOptions, BunServerWebSocket } from "../../types/bun"; +/// + import { WebSocketMessage } from "../message"; -import { WebSocketError } from "../error"; +// import { WebSocketError } from "../error"; import { WebSocketPeer } from "../peer"; import { defineWebSocketAdapter } from "../adapter"; @@ -10,8 +11,15 @@ export const WebSocket = globalThis.WebSocket; export interface AdapterOptions {} +type WebSocketHandler = Extract< + Parameters>[0], + { websocket: any } +>["websocket"]; + +type ServerWebSocket = Parameters[0]; + export interface Adapter { - websocket: BunWSOptions; + websocket: WebSocketHandler; } export default defineWebSocketAdapter( @@ -33,11 +41,12 @@ export default defineWebSocketAdapter( const peer = new BunWebSocketPeer(ws); handler.onClose?.(peer, 0, ""); }, - error: (ws, error) => { - handler.onEvent?.("bun:error", ws, error); - const peer = new BunWebSocketPeer(ws); - handler.onError?.(peer, new WebSocketError(error)); - }, + // TODO + // error: (ws, error) => { + // handler.onEvent?.("bun:error", ws, error); + // const peer = new BunWebSocketPeer(ws); + // handler.onError?.(peer, new WebSocketError(error)); + // }, drain: (ws) => { handler.onEvent?.("bun:drain", ws); }, @@ -47,7 +56,7 @@ export default defineWebSocketAdapter( ); class BunWebSocketPeer extends WebSocketPeer { - constructor(private _ws: BunServerWebSocket) { + constructor(private _ws: ServerWebSocket) { super(); } diff --git a/types/bun.ts b/types/bun.ts deleted file mode 100644 index 7b33d19..0000000 --- a/types/bun.ts +++ /dev/null @@ -1,60 +0,0 @@ -// Reference: https://bun.sh/docs/api/websockets - -export interface BunWSOptions { - message: ( - ws: BunServerWebSocket, - message: string | ArrayBuffer | Uint8Array, - ) => void; - open?: (ws: BunServerWebSocket) => void; - close?: (ws: BunServerWebSocket) => void; - error?: (ws: BunServerWebSocket, error: Error) => void; - drain?: (ws: BunServerWebSocket) => void; - perMessageDeflate?: - | boolean - | { - compress?: boolean | BunWSCompressor; - decompress?: boolean | BunWSCompressor; - }; -} - -type BunWSCompressor = - | `"disable"` - | `"shared"` - | `"dedicated"` - | `"3KB"` - | `"4KB"` - | `"8KB"` - | `"16KB"` - | `"32KB"` - | `"64KB"` - | `"128KB"` - | `"256KB"`; - -export interface BunWSServer { - pendingWebsockets: number; - publish( - topic: string, - data: string | ArrayBufferView | ArrayBuffer, - compress?: boolean, - ): number; - upgrade( - req: Request, - options?: { - headers?: HeadersInit; - data?: any; - }, - ): boolean; -} - -export interface BunServerWebSocket { - readonly data: any; - readonly readyState: number; - readonly remoteAddress: string; - send(message: string | ArrayBuffer | Uint8Array, compress?: boolean): number; - close(code?: number, reason?: string): void; - subscribe(topic: string): void; - unsubscribe(topic: string): void; - publish(topic: string, message: string | ArrayBuffer | Uint8Array): void; - isSubscribed(topic: string): boolean; - cork(cb: (ws: BunServerWebSocket) => void): void; -}