Skip to content

Commit

Permalink
fix(bun/ws): supoort --hot
Browse files Browse the repository at this point in the history
  • Loading branch information
nakasyou committed Oct 28, 2024
1 parent f55dc45 commit 6a8b9cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
42 changes: 23 additions & 19 deletions src/adapter/bun/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,35 @@ describe('createBunWebSocket()', () => {
})
it('Should events are called', async () => {
const { websocket, upgradeWebSocket } = createBunWebSocket()
const ws = {
data: {
connId: 0,
},
} as BunServerWebSocket<BunWebSocketData>

const open = vi.fn()
const message = vi.fn()
const close = vi.fn()

let receivedArrayBuffer: ArrayBuffer | undefined = undefined
await upgradeWebSocket(() => ({
onOpen() {
open()
},
onMessage(evt) {
message()
if (evt.data instanceof ArrayBuffer) {
receivedArrayBuffer = evt.data
}
},
onClose() {
close()
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<BunWebSocketData>

let receivedArrayBuffer: ArrayBuffer | undefined = undefined
await upgradeWebSocket(() => ({}))(
new Context(new Request('http://localhost'), {
env: {
upgrade() {
Expand Down
13 changes: 5 additions & 8 deletions src/adapter/bun/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface CreateWebSocket<T> {
websocket: BunWebSocketHandler<BunWebSocketData>
}
export interface BunWebSocketData {
connId: number
events: WSEvents
url: URL
protocol: string
}
Expand All @@ -46,18 +46,15 @@ export const createWSContext = (ws: BunServerWebSocket<BunWebSocketData>): WSCon
}

export const createBunWebSocket = <T>(): CreateWebSocket<T> => {
const websocketConns: WSEvents[] = []

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const upgradeWebSocket: UpgradeWebSocket<any> = 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<BunWebSocketData>(c.req.raw, {
data: {
connId,
events,
url: new URL(c.req.url),
protocol: c.req.url,
},
Expand All @@ -69,13 +66,13 @@ export const createBunWebSocket = <T>(): CreateWebSocket<T> => {
})
const websocket: BunWebSocketHandler<BunWebSocketData> = {
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', {
Expand All @@ -87,7 +84,7 @@ export const createBunWebSocket = <T>(): CreateWebSocket<T> => {
}
},
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)
Expand Down

0 comments on commit 6a8b9cd

Please sign in to comment.