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
Provenance
- Commit
cb8244a- Tree
c570864- Author
Shotaro Nakamura <79000684+nakasyou@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
745c291
2 files changed+28 added−27 removed
modified:src/adapter/bun/websocket.test.ts+23 added-19 removed
70 unmodified lines71727374757677787975767778798586878880818283848586878889909192939495969798999091929394959697989910010110210310410510610720 unmodified lines70 unmodified lines delete globalThis.CloseEvent }) 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() 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() }, }, }, onMessage(evt) { message() if (evt.data instanceof ArrayBuffer) { receivedArrayBuffer = evt.data } }, onClose() { close() }, }))( } as BunServerWebSocket<BunWebSocketData>
let receivedArrayBuffer: ArrayBuffer | undefined = undefined await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { upgrade() { return true20 unmodified linesmodified:src/adapter/bun/websocket.ts+5 added-8 removed
20 unmodified lines21 upgradeWebSocket: UpgradeWebSocket<T>22 websocket: BunWebSocketHandler<BunWebSocketData>23 }24 export interface BunWebSocketData {25- connId: number25+ events: WSEvents26 url: URL27 protocol: string28 }29 15 unmodified lines45 })46 }47 48 export const createBunWebSocket = <T>(): CreateWebSocket<T> => {49- const websocketConns: WSEvents[] = []50-49 // 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 lines65 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 lines83 )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