mirror of
https://github.com/honojs/hono.git
synced 2024-11-21 18:18:57 +01:00
fix(bun/ws): supoort --hot
(#3576)
This commit is contained in:
parent
745c2919b6
commit
cb8244aea9
@ -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() {
|
||||
const ws = {
|
||||
data: {
|
||||
events: {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onOpen(evt, ws) {
|
||||
open()
|
||||
},
|
||||
onMessage(evt) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onMessage(evt, ws) {
|
||||
message()
|
||||
if (evt.data instanceof ArrayBuffer) {
|
||||
receivedArrayBuffer = evt.data
|
||||
}
|
||||
},
|
||||
onClose() {
|
||||
// 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() {
|
||||
|
@ -22,7 +22,7 @@ interface CreateWebSocket<T> {
|
||||
websocket: BunWebSocketHandler<BunWebSocketData>
|
||||
}
|
||||
export interface BunWebSocketData {
|
||||
connId: number
|
||||
events: WSEvents
|
||||
url: URL
|
||||
protocol: string
|
||||
}
|
||||
@ -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,
|
||||
},
|
||||
@ -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', {
|
||||
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user