-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(helper/websocket): allow custom events to be passed to generics (#…
…3616) * fix(helper/websocket): allow custom events to be passed to generics * chore: disable any * add tests * patch * test patch
- Loading branch information
Showing
3 changed files
with
50 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
import { WSContext, defineWebSocketHelper } from '../../helper/websocket' | ||
import type { UpgradeWebSocket, WSReadyState } from '../../helper/websocket' | ||
import type { UpgradeWebSocket, WSEvents, WSReadyState } from '../../helper/websocket' | ||
|
||
// Based on https://github.com/honojs/hono/issues/1153#issuecomment-1767321332 | ||
export const upgradeWebSocket: UpgradeWebSocket<WebSocket> = defineWebSocketHelper( | ||
async (c, events) => { | ||
const upgradeHeader = c.req.header('Upgrade') | ||
if (upgradeHeader !== 'websocket') { | ||
return | ||
} | ||
export const upgradeWebSocket: UpgradeWebSocket< | ||
WebSocket, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
any, | ||
Omit<WSEvents<WebSocket>, 'onOpen'> | ||
> = defineWebSocketHelper(async (c, events) => { | ||
const upgradeHeader = c.req.header('Upgrade') | ||
if (upgradeHeader !== 'websocket') { | ||
return | ||
} | ||
|
||
// @ts-expect-error WebSocketPair is not typed | ||
const webSocketPair = new WebSocketPair() | ||
const client: WebSocket = webSocketPair[0] | ||
const server: WebSocket = webSocketPair[1] | ||
// @ts-expect-error WebSocketPair is not typed | ||
const webSocketPair = new WebSocketPair() | ||
const client: WebSocket = webSocketPair[0] | ||
const server: WebSocket = webSocketPair[1] | ||
|
||
const wsContext = new WSContext<WebSocket>({ | ||
close: (code, reason) => server.close(code, reason), | ||
get protocol() { | ||
return server.protocol | ||
}, | ||
raw: server, | ||
get readyState() { | ||
return server.readyState as WSReadyState | ||
}, | ||
url: server.url ? new URL(server.url) : null, | ||
send: (source) => server.send(source), | ||
}) | ||
const wsContext = new WSContext<WebSocket>({ | ||
close: (code, reason) => server.close(code, reason), | ||
get protocol() { | ||
return server.protocol | ||
}, | ||
raw: server, | ||
get readyState() { | ||
return server.readyState as WSReadyState | ||
}, | ||
url: server.url ? new URL(server.url) : null, | ||
send: (source) => server.send(source), | ||
}) | ||
|
||
if (events.onOpen) { | ||
server.addEventListener('open', (evt: Event) => events.onOpen?.(evt, wsContext)) | ||
} | ||
if (events.onClose) { | ||
server.addEventListener('close', (evt: CloseEvent) => events.onClose?.(evt, wsContext)) | ||
} | ||
if (events.onMessage) { | ||
server.addEventListener('message', (evt: MessageEvent) => events.onMessage?.(evt, wsContext)) | ||
} | ||
if (events.onError) { | ||
server.addEventListener('error', (evt: Event) => events.onError?.(evt, wsContext)) | ||
} | ||
// note: cloudflare workers doesn't support 'open' event | ||
|
||
// @ts-expect-error - server.accept is not typed | ||
server.accept?.() | ||
return new Response(null, { | ||
status: 101, | ||
// @ts-expect-error - webSocket is not typed | ||
webSocket: client, | ||
}) | ||
if (events.onClose) { | ||
server.addEventListener('close', (evt: CloseEvent) => events.onClose?.(evt, wsContext)) | ||
} | ||
if (events.onMessage) { | ||
server.addEventListener('message', (evt: MessageEvent) => events.onMessage?.(evt, wsContext)) | ||
} | ||
) | ||
if (events.onError) { | ||
server.addEventListener('error', (evt: Event) => events.onError?.(evt, wsContext)) | ||
} | ||
|
||
// @ts-expect-error - server.accept is not typed | ||
server.accept?.() | ||
return new Response(null, { | ||
status: 101, | ||
// @ts-expect-error - webSocket is not typed | ||
webSocket: client, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters