diff --git a/src/adapter/bun/websocket.test.ts b/src/adapter/bun/websocket.test.ts index e7606342..58604f2c 100644 --- a/src/adapter/bun/websocket.test.ts +++ b/src/adapter/bun/websocket.test.ts @@ -72,31 +72,35 @@ describe('createBunWebSocket()', () => { }) it('Should events are called', async () => { const { websocket, upgradeWebSocket } = createBunWebSocket() - const ws = { - data: { - connId: 0, - }, - } as BunServerWebSocket const open = vi.fn() const message = vi.fn() const close = vi.fn() + const ws = { + data: { + events: { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + onOpen(evt, ws) { + open() + }, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + onMessage(evt, ws) { + message() + if (evt.data instanceof ArrayBuffer) { + receivedArrayBuffer = evt.data + } + }, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + onClose(evt, ws) { + close() + }, + }, + }, + } as BunServerWebSocket + let receivedArrayBuffer: ArrayBuffer | undefined = undefined - await upgradeWebSocket(() => ({ - onOpen() { - open() - }, - onMessage(evt) { - message() - if (evt.data instanceof ArrayBuffer) { - receivedArrayBuffer = evt.data - } - }, - onClose() { - close() - }, - }))( + await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { upgrade() { diff --git a/src/adapter/bun/websocket.ts b/src/adapter/bun/websocket.ts index 9c9dc8b0..b15f8584 100644 --- a/src/adapter/bun/websocket.ts +++ b/src/adapter/bun/websocket.ts @@ -22,7 +22,7 @@ interface CreateWebSocket { websocket: BunWebSocketHandler } export interface BunWebSocketData { - connId: number + events: WSEvents url: URL protocol: string } @@ -46,18 +46,15 @@ export const createWSContext = (ws: BunServerWebSocket): WSCon } export const createBunWebSocket = (): CreateWebSocket => { - const websocketConns: WSEvents[] = [] - // eslint-disable-next-line @typescript-eslint/no-explicit-any const upgradeWebSocket: UpgradeWebSocket = defineWebSocketHelper((c, events) => { const server = getBunServer(c) if (!server) { throw new TypeError('env has to include the 2nd argument of fetch.') } - const connId = websocketConns.push(events) - 1 const upgradeResult = server.upgrade(c.req.raw, { data: { - connId, + events, url: new URL(c.req.url), protocol: c.req.url, }, @@ -69,13 +66,13 @@ export const createBunWebSocket = (): CreateWebSocket => { }) const websocket: BunWebSocketHandler = { open(ws) { - const websocketListeners = websocketConns[ws.data.connId] + const websocketListeners = ws.data.events if (websocketListeners.onOpen) { websocketListeners.onOpen(new Event('open'), createWSContext(ws)) } }, close(ws, code, reason) { - const websocketListeners = websocketConns[ws.data.connId] + const websocketListeners = ws.data.events if (websocketListeners.onClose) { websocketListeners.onClose( new CloseEvent('close', { @@ -87,7 +84,7 @@ export const createBunWebSocket = (): CreateWebSocket => { } }, message(ws, message) { - const websocketListeners = websocketConns[ws.data.connId] + const websocketListeners = ws.data.events if (websocketListeners.onMessage) { const normalizedReceiveData = typeof message === 'string' ? message : (message.buffer satisfies WSMessageReceive)