-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
205 lines (173 loc) · 5.73 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type { EventEmitter } from 'tsee';
import type WebSocket from 'isomorphic-ws';
import type KBucket from 'k-bucket';
import type crypto from 'libp2p-crypto';
import type debug from 'debug';
declare module 'unamed-network' {
type UnamedNetworkEvents = {
'new-known-service-addr': (event: { addr: MultiAddrStr }) => void,
'new-member': (event: { memberPeer: Peer, room: Room }) => void,
'member-left': (event: { memberPeer: Peer, room: Room }) => void,
'room-message': (event: { room: Room, fromMember: Peer, message: any }) => void,
}
export default class UnamedNetwork extends EventEmitter<UnamedNetworkEvents> {
readonly id: PeerId;
readonly addrs: MultiAddrStr[];
readonly nodeType: NodeType;
readonly rooms: Map<RoomNameHash, Room>;
readonly primaryRoomNameHash: RoomNameHash | null; // for kBucket's localNodeId, which determine this node's location in Kademlia DHT network
readonly peers: Map<PeerId, Peer>;
readonly kBucket: KBucket<KBucketContact>; // rooms without other peers should not be added
readonly knownServiceAddrs: MultiAddrStr[];
readonly started: boolean;
readonly config: Config;
readonly iceServers: RTCIceServers;
readonly providing?: Providing;
private requestResolveRejects: Map<ReqId, [(payload: any) => void, (error: any) => void]>;
private broadcastedMessages: Map<string, () => void>; // value is a fn to clearTimeout
private pubsubPollLostInterval: ReturnType<typeof setInterval>;
constructor(config?: Config);
start(knownServiceAddrs: MultiAddrStr[], myAddrs?: MultiAddrStr[]): Promise<void>;
receiveAddrConn(ws: WebSocket);
/** @returns if room has other peers */
join(roomName: string, makePrimary?: boolean): Promise<Room>;
getRoom(roomName: string): Promise<Room>;
leave(roomName: string): Promise<void>;
broadcast(roomName: string, message: any, recipients?: PeerId[]): void;
}
type RTCIceServers = ConstructorParameters<typeof RTCPeerConnection>[0]['iceServers']
interface Config {
id?: PeerId;
iceServers?: RTCIceServers;
providing?: Providing;
}
type RoomNameHash = string;
type RoomNameHashBuffer = Uint8Array;
type PeerId = string;
type MultiAddrStr = string;
type ReqId = string;
type NodeType = 'serviceNode' | 'clientNode';
type RoomMemberState = 'pending' | 'connected' | 'joined' | 'failed' | 'left-connected';
type PeerConnState = 'pending' | 'connected' | 'closing';
interface Room {
roomNameHash: RoomNameHash;
joined: boolean;
name?: string; // not hashed, only room member knows
members: Map<PeerId, RoomMemberState>;
outOfKBucket?: boolean;
}
interface Peer {
peerId: PeerId;
state: PeerConnState;
addrs: MultiAddrStr[];
nodeType: NodeType;
roomNameHashes: RoomNameHash[];
connectedResolves: ((peer: Peer) => void)[];
closingAndRejects: ((peer: Peer) => void)[];
ws?: WebSocket;
rtc?: Rtc;
pending?: Pick<Peer, 'ws' | 'connectedResolves'>;
routineTimer?: ReturnType<typeof setTimeout>;
ping?: number;
}
interface KBucketContact {
id: RoomNameHashBuffer;
roomNameHash: RoomNameHash;
vectorClock: number;
}
type Ephemeral = Awaited<ReturnType<typeof crypto.keys.generateEphemeralKeyPair>>;
type Encrypter = Awaited<ReturnType<typeof crypto.aes.create>>;
type Decrypter = Awaited<ReturnType<typeof crypto.aes.create>>;
type Chunk = Uint8Array;
interface Rtc {
simplePeer: SimpleRtcPeer;
ephemeral: Ephemeral;
encrypter: Encrypter;
decrypter: Decrypter;
chunksReceived: Chunk[];
}
interface Providing {
iceServers?: RTCIceServers;
}
// Packets (just as memo)
interface Packet { // abstract
type: string;
protocol: string;
packetId: string;
}
interface Request { // abstract
reqId: ReqId;
}
interface Response { // abstract
reqId: ReqId;
}
interface PingPacket extends Packet, Request {
type: 'ping';
id: string;
addrs: MultiAddrStr[];
roomNameHashes: RoomNameHash[];
providing?: Providing;
}
interface PongPacket extends Packet, Response {
type: 'pong';
addrs: MultiAddrStr[];
roomNameHashes: RoomNameHash[];
providing?: Providing;
}
interface FindPacket extends Packet, Request {
type: 'find';
targetHash: string;
fromPath: PeerId[];
lastDistance: number;
}
interface NetworkPacket extends Packet { // abstract
route: PeerId[]; // include sender
hop: number; // index in toPath
}
interface NetworkNoPeerPacket extends NetworkPacket {
type: 'noPeer';
oriPacketId: string;
}
interface FindResPacket extends NetworkPacket, Response {
type: 'findRes';
found: boolean;
}
interface ConnectPacket extends NetworkPacket, Request {
type: 'connect';
ephemeralKey: string;
addrs: MultiAddrStr[];
}
interface ConnectResPacket extends NetworkPacket, Response {
type: 'connectRes';
accepted: boolean;
method?: 'myAddr' | 'yourAddr' | 'webrtc';
// for myAddr method
addrs?: MultiAddrStr[];
// for webrtc method's further connectSignal encryption
ephemeralKey?: string;
}
interface ConnectSignalPacket extends NetworkPacket {
type: 'connectSignal';
encryptedSignal?: any;
}
interface JoinPacket extends Packet, Request {
type: 'join';
roomNameHash: RoomNameHash;
roomName: string; // not hashed, can be kind of a passphrase
}
interface JoinResPacket extends Packet, Response {
type: 'joinRes';
ok: boolean;
members?: PeerId[];
}
interface RoomMessagePacket extends Packet {
type: 'roomMessage';
roomNameHash: RoomNameHash;
author: PeerId;
message: any;
recipients: []; // empty means everyone
}
// 3rd package that don't have typings:
class SimpleRtcPeer {
}
}