refactor: use crossws interface to call hooks

This commit is contained in:
Pooya Parsa
2024-02-24 15:07:12 +01:00
parent 5f72d42a05
commit 7e36eba244
10 changed files with 133 additions and 66 deletions

View File

@@ -7,8 +7,14 @@ type WSHook<ArgsT extends Array<any> = []> = (
...args: ArgsT
) => void | Promise<void>;
export function defineWebSocketHooks(
hooks: Partial<WebSocketHooks>,
type WSGlobalHook<ArgsT extends Array<any> = []> = (
...args: ArgsT
) => void | Promise<void>;
export type UserHooks = Partial<WebSocketHooks & AdapterHooks>;
export function defineWebSocketHooks<T extends UserHooks = UserHooks>(
hooks: T,
): Partial<WebSocketHooks> {
return hooks;
}
@@ -25,7 +31,9 @@ export interface WebSocketHooks {
/** An error occurs */
error: WSHook<[WebSocketError]>;
}
export interface AdapterHooks {
// Bun
"bun:message": WSHook<[ws: any, message: any]>;
"bun:open": WSHook<[ws: any]>;
@@ -56,6 +64,10 @@ export interface WebSocketHooks {
"node:pong": WSHook<[data: Buffer]>;
"node:unexpected-response": WSHook<[req: any, res: any]>;
"node:upgrade": WSHook<[req: any]>;
"node:server-error": WSGlobalHook<[error: any]>;
"node:server-listening": WSGlobalHook<[]>;
"node:server-close": WSGlobalHook<[]>;
"node:server-headers": WSGlobalHook<[headers: any, request: any]>;
// uws (Node)
"uws:open": WSHook<[ws: any]>;