Skip to content
Myr

fix(bun/ws): supoort `--hot` (#3576)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Shotaro Nakamura · committed by GitHub · parent 745c291 · Browse files · 2 files+28 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−27 removed

  • tests:+23 added−19 removed
  • other:+5 added−8 removed

2 files+28 added−27 removed

modified:src/adapter/bun/websocket.test.ts+23 added-19 removed
modified:src/adapter/bun/websocket.ts+5 added-8 removed
20 unmodified lines
21 upgradeWebSocket: UpgradeWebSocket<T>22 websocket: BunWebSocketHandler<BunWebSocketData>23}24export interface BunWebSocketData {25 connId: number25 events: WSEvents26 url: URL27 protocol: string28}29
15 unmodified lines
45 })46}4748export const createBunWebSocket = <T>(): CreateWebSocket<T> => {49 const websocketConns: WSEvents[] = []5049 // eslint-disable-next-line @typescript-eslint/no-explicit-any50 const upgradeWebSocket: UpgradeWebSocket<any> = defineWebSocketHelper((c, events) => {51 const server = getBunServer(c)52 if (!server) {53 throw new TypeError('env has to include the 2nd argument of fetch.')54 }57 const connId = websocketConns.push(events) - 155 const upgradeResult = server.upgrade<BunWebSocketData>(c.req.raw, {56 data: {60 connId,57 events,58 url: new URL(c.req.url),59 protocol: c.req.url,60 },61 })
3 unmodified lines
65 return // failed66 })67 const websocket: BunWebSocketHandler<BunWebSocketData> = {68 open(ws) {72 const websocketListeners = websocketConns[ws.data.connId]69 const websocketListeners = ws.data.events70 if (websocketListeners.onOpen) {71 websocketListeners.onOpen(new Event('open'), createWSContext(ws))72 }73 },74 close(ws, code, reason) {78 const websocketListeners = websocketConns[ws.data.connId]75 const websocketListeners = ws.data.events76 if (websocketListeners.onClose) {77 websocketListeners.onClose(78 new CloseEvent('close', {79 code,
3 unmodified lines
83 )84 }85 },86 message(ws, message) {90 const websocketListeners = websocketConns[ws.data.connId]87 const websocketListeners = ws.data.events88 if (websocketListeners.onMessage) {89 const normalizedReceiveData =90 typeof message === 'string' ? message : (message.buffer satisfies WSMessageReceive)91
12 unmodified lines