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

feat(websocket): Add generics type to WSContext #3337

Merged
merged 5 commits into from
Sep 8, 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
13 changes: 6 additions & 7 deletions src/adapter/bun/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ interface BunWebSocketHandler<T> {
close(ws: BunServerWebSocket<T>, code?: number, reason?: string): void
message(ws: BunServerWebSocket<T>, message: string | Uint8Array): void
}
interface CreateWebSocket {
(): {
upgradeWebSocket: UpgradeWebSocket
websocket: BunWebSocketHandler<BunWebSocketData>
}
interface CreateWebSocket<T> {
upgradeWebSocket: UpgradeWebSocket<T>
websocket: BunWebSocketHandler<BunWebSocketData>
}
export interface BunWebSocketData {
connId: number
Expand All @@ -49,10 +47,11 @@ const createWSContext = (ws: BunServerWebSocket<BunWebSocketData>): WSContext =>
}
}

export const createBunWebSocket: CreateWebSocket = () => {
export const createBunWebSocket = <T>(): CreateWebSocket<T> => {
const websocketConns: WSEvents[] = []

const upgradeWebSocket: UpgradeWebSocket = (createEvents) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const upgradeWebSocket: UpgradeWebSocket<any> = (createEvents) => {
return async (c, next) => {
const server = getBunServer(c)
if (!server) {
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/cloudflare-workers/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { UpgradeWebSocket, WSContext, WSReadyState } from '../../helper/websocket'

// Based on https://github.com/honojs/hono/issues/1153#issuecomment-1767321332
export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, next) => {
export const upgradeWebSocket: UpgradeWebSocket<WebSocket> = (createEvents) => async (c, next) => {
const events = await createEvents(c)

const upgradeHeader = c.req.header('Upgrade')
Expand All @@ -14,7 +14,7 @@ export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, ne
const client: WebSocket = webSocketPair[0]
const server: WebSocket = webSocketPair[1]

const wsContext: WSContext = {
const wsContext: WSContext<WebSocket> = {
binaryType: 'arraybuffer',
close: (code, reason) => server.close(code, reason),
get protocol() {
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/deno/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface UpgradeWebSocketOptions {
idleTimeout?: number
}

export const upgradeWebSocket: UpgradeWebSocket<UpgradeWebSocketOptions> =
export const upgradeWebSocket: UpgradeWebSocket<WebSocket, UpgradeWebSocketOptions> =
(createEvents, options) => async (c, next) => {
if (c.req.header('upgrade') !== 'websocket') {
return await next()
Expand All @@ -29,7 +29,7 @@ export const upgradeWebSocket: UpgradeWebSocket<UpgradeWebSocketOptions> =
const events = await createEvents(c)
const { response, socket } = Deno.upgradeWebSocket(c.req.raw, options || {})

const wsContext: WSContext = {
const wsContext: WSContext<WebSocket> = {
binaryType: 'arraybuffer',
close: (code, reason) => socket.close(code, reason),
get protocol() {
Expand Down
20 changes: 10 additions & 10 deletions src/helper/websocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import type { MiddlewareHandler } from '../../types'
/**
* WebSocket Event Listeners type
*/
export interface WSEvents {
onOpen?: (evt: Event, ws: WSContext) => void
onMessage?: (evt: MessageEvent<WSMessageReceive>, ws: WSContext) => void
onClose?: (evt: CloseEvent, ws: WSContext) => void
onError?: (evt: Event, ws: WSContext) => void
export interface WSEvents<T = unknown> {
onOpen?: (evt: Event, ws: WSContext<T>) => void
onMessage?: (evt: MessageEvent<WSMessageReceive>, ws: WSContext<T>) => void
onClose?: (evt: CloseEvent, ws: WSContext<T>) => void
onError?: (evt: Event, ws: WSContext<T>) => void
}

/**
* Upgrade WebSocket Type
*/
export type UpgradeWebSocket<T = any> = (
createEvents: (c: Context) => WSEvents | Promise<WSEvents>,
options?: T
export type UpgradeWebSocket<T = unknown, U = any> = (
createEvents: (c: Context) => WSEvents<T> | Promise<WSEvents<T>>,
options?: U
) => MiddlewareHandler<
any,
string,
Expand All @@ -33,14 +33,14 @@ export type UpgradeWebSocket<T = any> = (

export type WSReadyState = 0 | 1 | 2 | 3

export type WSContext = {
export type WSContext<T = unknown> = {
send(
source: string | ArrayBuffer | Uint8Array,
options?: {
compress: boolean
}
): void
raw?: unknown
raw?: T
binaryType: BinaryType
readyState: WSReadyState
url: URL | null
Expand Down