Urara-Blog/node_modules/.pnpm-store/v3/files/17/3062bc733335e4f2dc06f0d26b1ea016b3cee5700b1c1c794cf1696dfd24d8ab52645a9277f5784f8bdeba3ceafdb42c1d612bb6e47ffbd607f5170b6a9b2b
2022-08-14 01:14:53 +08:00

40 lines
1,015 B
Text

import type { Context, Params } from 'worktop';
import type { Dict, Strict, Promisable } from 'worktop/utils';
export type State = Dict<any>;
export interface Socket<S extends State = State> {
send: WebSocket['send'];
close: WebSocket['close'];
state: S; // todo: not happy w/ name
event:
| { type: 'open' } & Event
| { type: 'close' } & CloseEvent
| { type: 'message' } & MessageEvent<string>
| { type: 'error' } & Event;
}
export type SocketHandler<
C extends Context = Context,
P extends Params = Params,
S extends State = State,
> = (
request: Request,
context: C, // todo: omit
socket: Socket<S>
) => Promisable<void>;
/**
* Establish a Websocket connection.
* Attach the `handler` as the 'message' event listener.
* @NOTE Invokes the `ws.connect` middleware automatically.
*/
export function listen<
C extends Context = Context,
P extends Params = Params,
>(handler: SocketHandler<C, P>): (
request: Request,
context: Omit<C, 'params'> & {
params: Strict<P>;
}
) => Response;