Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading