-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathindex.ts
409 lines (350 loc) · 10.9 KB
/
index.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import type { AbortOptions, Logger } from '../index.js'
import type { PeerId } from '../peer-id/index.js'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { Duplex, Source } from 'it-stream-types'
import type { Uint8ArrayList } from 'uint8arraylist'
export interface ConnectionTimeline {
/**
* When the connection was opened
*/
open: number
/**
* When the MultiaddrConnection was upgraded to a Connection - e.g. the type
* of connection encryption and multiplexing was negotiated.
*/
upgraded?: number
/**
* When the connection was closed.
*/
close?: number
}
/**
* Outbound connections are opened by the local node, inbound streams are opened by the remote
*/
export type Direction = 'inbound' | 'outbound'
export interface StreamTimeline {
/**
* A timestamp of when the stream was opened
*/
open: number
/**
* A timestamp of when the stream was closed for both reading and writing
*/
close?: number
/**
* A timestamp of when the stream was closed for reading
*/
closeRead?: number
/**
* A timestamp of when the stream was closed for writing
*/
closeWrite?: number
/**
* A timestamp of when the stream was reset
*/
reset?: number
/**
* A timestamp of when the stream was aborted
*/
abort?: number
}
/**
* The states a stream can be in
*/
export type StreamStatus = 'open' | 'closing' | 'closed' | 'aborted' | 'reset'
/**
* The states the readable end of a stream can be in
*
* ready - the readable end is ready for reading
* closing - the readable end is closing
* closed - the readable end has closed
*/
export type ReadStatus = 'ready' | 'closing' | 'closed'
/**
* The states the writable end of a stream can be in
*
* ready - the writable end is ready for writing
* writing - the writable end is in the process of being written to
* done - the source passed to the `.sink` function yielded all values without error
* closing - the writable end is closing
* closed - the writable end has closed
*/
export type WriteStatus = 'ready' | 'writing' | 'done' | 'closing' | 'closed'
/**
* A Stream is a data channel between two peers that
* can be written to and read from at both ends.
*
* It may be encrypted and multiplexed depending on the
* configuration of the nodes.
*/
export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>, Promise<void>> {
/**
* Closes the stream for **reading** *and* **writing**.
*
* Any buffered data in the source can still be consumed and the stream will end normally.
*
* This will cause a `CLOSE` message to be sent to the remote, *unless* the sink has already ended.
*
* The sink and the source will return normally.
*/
close(options?: AbortOptions): Promise<void>
/**
* Closes the stream for **reading**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
*
* This function is called automatically by the muxer when it receives a `CLOSE` message from the remote.
*
* The source will return normally, the sink will continue to consume.
*/
closeRead(options?: AbortOptions): Promise<void>
/**
* Closes the stream for **writing**. If iterating over the source of this stream in a `for await of` loop, it will return (exit the loop) after any buffered data has been consumed.
*
* The source will return normally, the sink will continue to consume.
*/
closeWrite(options?: AbortOptions): Promise<void>
/**
* Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
*
* Note, if called without an error any buffered data in the source can still be consumed and the stream will end normally.
*
* This will cause a `RESET` message to be sent to the remote, *unless* the sink has already ended.
*
* The sink will return and the source will throw if an error is passed or return normally if not.
*/
abort(err: Error): void
/**
* Unique identifier for a stream. Identifiers are not unique across muxers.
*/
id: string
/**
* Outbound streams are opened by the local node, inbound streams are opened by the remote
*/
direction: Direction
/**
* Lifecycle times for the stream
*/
timeline: StreamTimeline
/**
* The protocol negotiated for this stream
*/
protocol?: string
/**
* User defined stream metadata
*/
metadata: Record<string, any>
/**
* The current status of the stream
*/
status: StreamStatus
/**
* The current status of the readable end of the stream
*/
readStatus: ReadStatus
/**
* The current status of the writable end of the stream
*/
writeStatus: WriteStatus
/**
* The stream logger
*/
log: Logger
}
export interface NewStreamOptions extends AbortOptions {
/**
* If specified, and no handler has been registered with the registrar for the
* successfully negotiated protocol, use this as the max outbound stream limit
* for the protocol
*/
maxOutboundStreams?: number
/**
* Opt-in to running over a limited connection - one that has restrictions
* on the amount of data that may be transferred or how long it may be open for.
*
* These limits are typically enforced by a relay server, if the protocol
* will be transferring a lot of data or the stream will be open for a long time
* consider upgrading to a direct connection before opening the stream.
*
* @default false
*/
runOnLimitedConnection?: boolean
/**
* By default when negotiating a protocol the dialer writes then protocol name
* then reads the response.
*
* When a only a single protocol is being negotiated on an outbound stream,
* and the stream is written to before being read from, we can optimistically
* write the protocol name and the first chunk of data together in the first
* message.
*
* Reading and handling the protocol response is done asynchronously, which
* means we can skip a round trip on writing to newly opened streams which
* significantly reduces the time-to-first-byte on a stream.
*
* The side-effect of this is that the underlying stream won't negotiate the
* protocol until either data is written to or read from the stream so it will
* not be opened on the remote until this is done.
*
* Pass `false` here to optimistically write the protocol name and first chunk
* of data in the first message.
*
* If multiple protocols are being negotiated, negotiation is always completed
* in full before the stream is returned so this option has no effect.
*
* @default true
*/
negotiateFully?: boolean
}
export type ConnectionStatus = 'open' | 'closing' | 'closed'
/**
* Connection limits are present on connections that are only allowed to
* transfer a certain amount of bytes or be open for a certain number
* of seconds.
*
* These limits are applied by Circuit Relay v2 servers, for example and
* the connection will normally be closed abruptly if the limits are
* exceeded.
*/
export interface ConnectionLimits {
/**
* If present this is the number of bytes remaining that may be
* transferred over this connection
*/
bytes?: bigint
/**
* If present this is the number of seconds that this connection will
* remain open for
*/
seconds?: number
}
/**
* A Connection is a high-level representation of a connection
* to a remote peer that may have been secured by encryption and
* multiplexed, depending on the configuration of the nodes
* between which the connection is made.
*/
export interface Connection {
/**
* The unique identifier for this connection
*/
id: string
/**
* The address of the remote end of the connection
*/
remoteAddr: Multiaddr
/**
* The id of the peer at the remote end of the connection
*/
remotePeer: PeerId
/**
* A list of tags applied to this connection
*/
tags: string[]
/**
* A list of open streams on this connection
*/
streams: Stream[]
/**
* Outbound conections are opened by the local node, inbound streams are opened by the remote
*/
direction: Direction
/**
* Lifecycle times for the connection
*/
timeline: ConnectionTimeline
/**
* The multiplexer negotiated for this connection
*/
multiplexer?: string
/**
* The encryption protocol negotiated for this connection
*/
encryption?: string
/**
* The current status of the connection
*/
status: ConnectionStatus
/**
* If present, this connection has limits applied to it, perhaps by an
* intermediate relay. Once the limits have been reached the connection will
* be closed by the relay.
*/
limits?: ConnectionLimits
/**
* The time in milliseconds it takes to make a round trip to the remote peer.
*
* This is updated periodically by the connection monitor.
*/
rtt?: number
/**
* Create a new stream on this connection and negotiate one of the passed protocols
*/
newStream(protocols: string | string[], options?: NewStreamOptions): Promise<Stream>
/**
* Gracefully close the connection. All queued data will be written to the
* underlying transport.
*/
close(options?: AbortOptions): Promise<void>
/**
* Immediately close the connection, any queued data will be discarded
*/
abort(err: Error): void
/**
* The connection logger
*/
log: Logger
}
export const connectionSymbol = Symbol.for('@libp2p/connection')
export function isConnection (other: any): other is Connection {
return other != null && Boolean(other[connectionSymbol])
}
export interface ConnectionProtector {
/**
* Takes a given Connection and creates a private encryption stream
* between its two peers from the PSK the Protector instance was
* created with.
*/
protect(connection: MultiaddrConnection): Promise<MultiaddrConnection>
}
export interface MultiaddrConnectionTimeline {
/**
* When the connection was opened
*/
open: number
/**
* When the MultiaddrConnection was upgraded to a Connection - the type of
* connection encryption and multiplexing was negotiated.
*/
upgraded?: number
/**
* When the connection was closed.
*/
close?: number
}
/**
* A MultiaddrConnection is returned by transports after dialing
* a peer. It is a low-level primitive and is the raw connection
* without encryption or stream multiplexing.
*/
export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> {
/**
* Gracefully close the connection. All queued data will be written to the
* underlying transport.
*/
close(options?: AbortOptions): Promise<void>
/**
* Immediately close the connection, any queued data will be discarded
*/
abort(err: Error): void
/**
* The address of the remote end of the connection
*/
remoteAddr: Multiaddr
/**
* When connection lifecycle events occurred
*/
timeline: MultiaddrConnectionTimeline
/**
* The multiaddr connection logger
*/
log: Logger
}