-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpoint.d.ts
98 lines (72 loc) · 2.08 KB
/
Endpoint.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// import type { ClientRequestArgs } from 'node:http'
import WebSocket = require('ws')
import type { Disposer } from '@streetstrider/emitter'
import type { Transport } from './Transport.js'
import type { Binary_Send } from './Transport.js'
import type { Binary_Recv } from './Transport.js'
/* TODO: combination set subtract */
export type Protocol <Keys extends string = string>
= Record<Keys, string>
export type Protocol_Core =
{
'@connect': void,
'@reconnect': void,
'@binary': Binary_Recv,
'@open': void,
'@close': void,
'@error': WebSocket.ErrorEvent,
}
export type Protocol_All <In>
= (In & Protocol_Core)
export type Handler
<
Data /* not extends */ = string,
Endp extends Endpoint<any, any, any> = Endpoint
>
= (data: Data, endp: Endp) => void
export type Handler_Composition
<
Data extends Record<string, any> = Record<string, string>,
Endp extends Endpoint<any, any, any> = Endpoint
>
= { [ Key in keyof Data ]: Handler<Data[Key], Endp> }
export type Aux_Base = Record<string, unknown>
export type Endpoint_Protocol_In <Endp extends Endpoint<any, any, any>>
= Endp extends Endpoint<infer In, any, any> ? In : never
export type Mixed_String <S>
= string extends S ? (S | number) : S
export interface Endpoint
<
In extends Protocol = Protocol,
Out extends Protocol = Protocol,
Aux extends Aux_Base = Aux_Base,
>
{
on <Key extends keyof Protocol_All<In>>
(map: { [ K in Key ]?: Handler<Protocol_All<In>[K], this> })
: Disposer,
on <Key extends keyof In>
(key: Key, handler: Handler<In[Key], this>)
: Disposer,
on <Key extends keyof Protocol_Core>
(key: Key, handler: Handler<Protocol_Core[Key], this>)
: Disposer,
send <Kind extends keyof Out>
(kind: Kind, data?: Mixed_String<Out[Kind]>)
: void,
send (data: Binary_Send): void,
close (): void,
aux: Aux,
}
export default function Endpoint
<
In extends Protocol = Protocol,
Out extends Protocol = Protocol,
Aux extends Aux_Base = Aux_Base,
>
(
// ws: WebSocket | WebSocket.ClientOptions | ClientRequestArgs | string
transport: ((() => Transport) | string),
)
:
Endpoint<In, Out, Aux>