-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ws.d.ts
57 lines (50 loc) · 1.66 KB
/
ws.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { ServerRequest, Params } from 'worktop/request';
declare global {
const WebSocketPair: {
new(): {
/** the `client` socket */
0: WebSocket,
/** the `server` socket */
1: WebSocket,
};
};
interface ResponseInit {
webSocket?: WebSocket;
}
}
export interface WebSocket {
accept(): void;
send(message: number | string): void;
close(code?: number, reason?: string): void;
addEventListener(type: 'error', listener: (this: WebSocket, ev: Event) => any): void;
addEventListener(type: 'close', listener: (this: WebSocket, ev: CloseEvent) => any): void;
addEventListener(type: 'message', listener: (this: WebSocket, ev: MessageEvent<string>) => any): void;
}
type Context = Record<string, any>;
export interface Socket<C extends Context = Context> {
send: WebSocket['send'];
close: WebSocket['close'];
context: C;
event:
| { type: 'close' } & CloseEvent
| { type: 'message' } & MessageEvent<string>
| { type: 'error' } & Event;
}
export type SocketHandler<
P extends Params = Params,
C extends Context = Context,
> = (req: ServerRequest<P>, socket: Socket<C>) => Promise<void>|void;
/**
* Ensure the incoming `Request` can be upgraded to a Websocket connection.
* @NOTE This is called automatically within the `listen()` method.
*/
export function connect<P extends Params = Params>(req: ServerRequest<P> | Request): Response | void;
/**
* Establish a Websocket connection.
* Attach the `handler` as the 'message' event listener.
* @NOTE Invokes the `connect()` middleware automatically.
*/
export function listen<
P extends Params = Params,
C extends Context = Context,
>(handler: SocketHandler<P, C>): (req: ServerRequest<P>) => Response;