0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

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

* Extends WSContext's types

* Add new generics on UpgradeWebSocket types

* fix with linter

* revert createWSContext types

* prettier fix
This commit is contained in:
hayatosc 2024-09-08 15:49:09 +09:00 committed by GitHub
parent 8e56989cde
commit 6f69bf04b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 21 deletions

View File

@ -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
@ -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) {

View File

@ -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')
@ -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() {

View File

@ -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()
@ -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() {

View File

@ -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,
@ -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