-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpoint.d.ts
128 lines (97 loc) · 2.43 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* 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'
export type Meta
<
Key, // TODO: extends string,
Msg = string,
Data = string,
Endp extends Endpoint<any, any, any> = Endpoint
>
=
{
key: Key,
msg: Msg,
data: Data,
endp: Endp,
}
export type With_Meta
<
Key, // TODO: extends string,
Data,
Endp extends Endpoint<any, any, any> = Endpoint
>
= Data extends void
? { endp: Endp }
:
Key extends '@binary'
? Meta<Key, Data, Data, Endp>
: Meta<Key, string, Data, Endp>
export type Handler
<
Key, // TODO: extends string,
Data = string,
Endp extends Endpoint<any, any, any> = Endpoint
>
= (data: Data, meta: With_Meta<Key, Data, 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<Key, Data[Key], Endp> }
export type Aux_Base = Record<string, unknown>
/* 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 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<K, Protocol_All<In>[K], this> })
: Disposer,
on <Key extends keyof In>
(key: Key, handler: Handler<Key, In[Key], this>)
: Disposer,
on <Key extends keyof Protocol_Core>
(key: Key, handler: Handler<Key, Protocol_Core[Key], this>)
: Disposer,
send <Key extends keyof Out>
(key: Key, data?: Mixed_String<Out[Key]>)
: 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>