Skip to content

Commit

Permalink
refactor(websocket): remove unused condition (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
fzn0x authored May 28, 2024
1 parent 0c7fb9a commit a3d9cc7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/adapter/cloudflare-workers/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('upgradeWebSocket middleware', () => {
upgradeWebSocket(() => ({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onMessage(evt, ws) {
console.log('evt')
resolve(evt.data)
},
}))
Expand Down
25 changes: 8 additions & 17 deletions src/adapter/cloudflare-workers/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,22 @@ export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, ne
send: (source) => server.send(source),
}
if (events.onOpen) {
server.addEventListener('open', (evt: Event) => events.onOpen && events.onOpen(evt, wsContext))
server.addEventListener('open', (evt: Event) => events.onOpen?.(evt, wsContext))
}
if (events.onClose) {
server.addEventListener(
'close',
(evt: CloseEvent) => events.onClose && events.onClose(evt, wsContext)
)
server.addEventListener('close', (evt: CloseEvent) => events.onClose?.(evt, wsContext))
}
if (events.onMessage) {
server.addEventListener(
'message',
(evt: MessageEvent) => events.onMessage && events.onMessage(evt, wsContext)
)
server.addEventListener('message', (evt: MessageEvent) => events.onMessage?.(evt, wsContext))
}
if (events.onError) {
server.addEventListener(
'error',
(evt: Event) => events.onError && events.onError(evt, wsContext)
)
server.addEventListener('error', (evt: Event) => events.onError?.(evt, wsContext))
}
// @ts-expect-error server.accept is not typed
server.accept()

// @ts-expect-error - server.accept is not typed
server.accept?.()
return new Response(null, {
status: 101,
// @ts-expect-error type not typed
// @ts-expect-error - webSocket is not typed
webSocket: client,
})
}
8 changes: 4 additions & 4 deletions src/adapter/deno/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, ne
url: socket.url ? new URL(socket.url) : null,
send: (source) => socket.send(source),
}
socket.onopen = (evt) => events.onOpen && events.onOpen(evt, wsContext)
socket.onmessage = (evt) => events.onMessage && events.onMessage(evt, wsContext)
socket.onclose = (evt) => events.onClose && events.onClose(evt, wsContext)
socket.onerror = (evt) => events.onError && events.onError(evt, wsContext)
socket.onopen = (evt) => events.onOpen?.(evt, wsContext)
socket.onmessage = (evt) => events.onMessage?.(evt, wsContext)
socket.onclose = (evt) => events.onClose?.(evt, wsContext)
socket.onerror = (evt) => events.onError?.(evt, wsContext)

return response
}

0 comments on commit a3d9cc7

Please sign in to comment.