@libp2p/interface 0.0.1-6fdaa7dc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +45 -0
  3. package/dist/index.min.js +3 -0
  4. package/dist/src/connection/index.d.ts +188 -0
  5. package/dist/src/connection/index.d.ts.map +1 -0
  6. package/dist/src/connection/index.js +5 -0
  7. package/dist/src/connection/index.js.map +1 -0
  8. package/dist/src/connection/status.d.ts +4 -0
  9. package/dist/src/connection/status.d.ts.map +1 -0
  10. package/dist/src/connection/status.js +4 -0
  11. package/dist/src/connection/status.js.map +1 -0
  12. package/dist/src/connection-encrypter/index.d.ts +27 -0
  13. package/dist/src/connection-encrypter/index.d.ts.map +1 -0
  14. package/dist/src/connection-encrypter/index.js +2 -0
  15. package/dist/src/connection-encrypter/index.js.map +1 -0
  16. package/dist/src/connection-gater/index.d.ts +117 -0
  17. package/dist/src/connection-gater/index.d.ts.map +1 -0
  18. package/dist/src/connection-gater/index.js +2 -0
  19. package/dist/src/connection-gater/index.js.map +1 -0
  20. package/dist/src/content-routing/index.d.ts +79 -0
  21. package/dist/src/content-routing/index.d.ts.map +1 -0
  22. package/dist/src/content-routing/index.js +21 -0
  23. package/dist/src/content-routing/index.js.map +1 -0
  24. package/dist/src/errors.d.ts +33 -0
  25. package/dist/src/errors.d.ts.map +1 -0
  26. package/dist/src/errors.js +51 -0
  27. package/dist/src/errors.js.map +1 -0
  28. package/dist/src/events.d.ts +28 -0
  29. package/dist/src/events.d.ts.map +1 -0
  30. package/dist/src/events.js +70 -0
  31. package/dist/src/events.js.map +1 -0
  32. package/dist/src/index.d.ts +575 -0
  33. package/dist/src/index.d.ts.map +1 -0
  34. package/dist/src/index.js +17 -0
  35. package/dist/src/index.js.map +1 -0
  36. package/dist/src/keychain/index.d.ts +154 -0
  37. package/dist/src/keychain/index.d.ts.map +1 -0
  38. package/dist/src/keychain/index.js +23 -0
  39. package/dist/src/keychain/index.js.map +1 -0
  40. package/dist/src/keys/index.d.ts +35 -0
  41. package/dist/src/keys/index.d.ts.map +1 -0
  42. package/dist/src/keys/index.js +4 -0
  43. package/dist/src/keys/index.js.map +1 -0
  44. package/dist/src/metrics/index.d.ts +165 -0
  45. package/dist/src/metrics/index.d.ts.map +1 -0
  46. package/dist/src/metrics/index.js +2 -0
  47. package/dist/src/metrics/index.js.map +1 -0
  48. package/dist/src/metrics/tracked-map.d.ts +17 -0
  49. package/dist/src/metrics/tracked-map.d.ts.map +1 -0
  50. package/dist/src/metrics/tracked-map.js +38 -0
  51. package/dist/src/metrics/tracked-map.js.map +1 -0
  52. package/dist/src/peer-discovery/index.d.ts +28 -0
  53. package/dist/src/peer-discovery/index.d.ts.map +1 -0
  54. package/dist/src/peer-discovery/index.js +21 -0
  55. package/dist/src/peer-discovery/index.js.map +1 -0
  56. package/dist/src/peer-id/index.d.ts +30 -0
  57. package/dist/src/peer-id/index.d.ts.map +1 -0
  58. package/dist/src/peer-id/index.js +5 -0
  59. package/dist/src/peer-id/index.js.map +1 -0
  60. package/dist/src/peer-info/index.d.ts +8 -0
  61. package/dist/src/peer-info/index.d.ts.map +1 -0
  62. package/dist/src/peer-info/index.js +2 -0
  63. package/dist/src/peer-info/index.js.map +1 -0
  64. package/dist/src/peer-routing/index.d.ts +51 -0
  65. package/dist/src/peer-routing/index.d.ts.map +1 -0
  66. package/dist/src/peer-routing/index.js +21 -0
  67. package/dist/src/peer-routing/index.js.map +1 -0
  68. package/dist/src/peer-store/index.d.ts +247 -0
  69. package/dist/src/peer-store/index.d.ts.map +1 -0
  70. package/dist/src/peer-store/index.js +2 -0
  71. package/dist/src/peer-store/index.js.map +1 -0
  72. package/dist/src/peer-store/tags.d.ts +2 -0
  73. package/dist/src/peer-store/tags.d.ts.map +1 -0
  74. package/dist/src/peer-store/tags.js +2 -0
  75. package/dist/src/peer-store/tags.js.map +1 -0
  76. package/dist/src/record/index.d.ts +33 -0
  77. package/dist/src/record/index.d.ts.map +1 -0
  78. package/dist/src/record/index.js +2 -0
  79. package/dist/src/record/index.js.map +1 -0
  80. package/dist/src/startable.d.ts +46 -0
  81. package/dist/src/startable.d.ts.map +1 -0
  82. package/dist/src/startable.js +46 -0
  83. package/dist/src/startable.js.map +1 -0
  84. package/dist/src/stream-handler/index.d.ts +23 -0
  85. package/dist/src/stream-handler/index.d.ts.map +1 -0
  86. package/dist/src/stream-handler/index.js +2 -0
  87. package/dist/src/stream-handler/index.js.map +1 -0
  88. package/dist/src/stream-muxer/index.d.ts +51 -0
  89. package/dist/src/stream-muxer/index.d.ts.map +1 -0
  90. package/dist/src/stream-muxer/index.js +2 -0
  91. package/dist/src/stream-muxer/index.js.map +1 -0
  92. package/dist/src/stream-muxer/stream.d.ts +85 -0
  93. package/dist/src/stream-muxer/stream.d.ts.map +1 -0
  94. package/dist/src/stream-muxer/stream.js +255 -0
  95. package/dist/src/stream-muxer/stream.js.map +1 -0
  96. package/dist/src/topology/index.d.ts +9 -0
  97. package/dist/src/topology/index.d.ts.map +1 -0
  98. package/dist/src/topology/index.js +2 -0
  99. package/dist/src/topology/index.js.map +1 -0
  100. package/dist/src/transport/index.d.ts +96 -0
  101. package/dist/src/transport/index.d.ts.map +1 -0
  102. package/dist/src/transport/index.js +19 -0
  103. package/dist/src/transport/index.js.map +1 -0
  104. package/package.json +177 -0
  105. package/src/connection/index.ts +221 -0
  106. package/src/connection/status.ts +4 -0
  107. package/src/connection-encrypter/index.ts +30 -0
  108. package/src/connection-gater/index.ts +128 -0
  109. package/src/content-routing/index.ts +83 -0
  110. package/src/errors.ts +68 -0
  111. package/src/events.ts +100 -0
  112. package/src/index.ts +627 -0
  113. package/src/keychain/index.ts +167 -0
  114. package/src/keys/index.ts +38 -0
  115. package/src/metrics/index.ts +187 -0
  116. package/src/metrics/tracked-map.ts +65 -0
  117. package/src/peer-discovery/index.ts +29 -0
  118. package/src/peer-id/index.ts +39 -0
  119. package/src/peer-info/index.ts +8 -0
  120. package/src/peer-routing/index.ts +53 -0
  121. package/src/peer-store/index.ts +272 -0
  122. package/src/peer-store/tags.ts +2 -0
  123. package/src/record/index.ts +35 -0
  124. package/src/startable.ts +117 -0
  125. package/src/stream-handler/index.ts +27 -0
  126. package/src/stream-muxer/index.ts +58 -0
  127. package/src/stream-muxer/stream.ts +365 -0
  128. package/src/topology/index.ts +10 -0
  129. package/src/transport/index.ts +111 -0
@@ -0,0 +1,272 @@
1
+ import type { PeerId } from '../peer-id/index.js'
2
+ import type { Multiaddr } from '@multiformats/multiaddr'
3
+
4
+ /**
5
+ * A multiaddr with an optional flag that indicates if its trustworthy
6
+ */
7
+ export interface Address {
8
+ /**
9
+ * Peer multiaddr
10
+ */
11
+ multiaddr: Multiaddr
12
+
13
+ /**
14
+ * Obtained from a signed peer record
15
+ */
16
+ isCertified: boolean
17
+ }
18
+
19
+ /**
20
+ * Data stored in the peer store about peers
21
+ */
22
+ export interface Peer {
23
+ /**
24
+ * Peer's peer-id instance
25
+ */
26
+ id: PeerId
27
+
28
+ /**
29
+ * Peer's addresses containing a list of multiaddrs and a isCertified field
30
+ * indicating if the address was loaded from a signed peer record or not
31
+ */
32
+ addresses: Address[]
33
+
34
+ /**
35
+ * Peer's supported protocols
36
+ */
37
+ protocols: string[]
38
+
39
+ /**
40
+ * Peer's metadata map
41
+ */
42
+ metadata: Map<string, Uint8Array>
43
+
44
+ /**
45
+ * Tags a peer has
46
+ */
47
+ tags: Map<string, Tag>
48
+
49
+ /**
50
+ * The last peer record envelope received
51
+ */
52
+ peerRecordEnvelope?: Uint8Array
53
+ }
54
+
55
+ /**
56
+ * Peer data used to update the peer store
57
+ */
58
+ export interface PeerData {
59
+ /**
60
+ * Peer's addresses containing its multiaddrs and metadata - multiaddrs
61
+ * passed here can be treated as certified if the `isCertifed` value is
62
+ * set to true.
63
+ *
64
+ * If both addresses and multiaddrs are specified they will be merged
65
+ * together with entries in addresses taking precedence.
66
+ */
67
+ addresses?: Address[]
68
+
69
+ /**
70
+ * Peer's multiaddrs - any multiaddrs passed here will be treated as
71
+ * uncertified.
72
+ *
73
+ * If both addresses and multiaddrs are specified they will be merged
74
+ * together with entries in addresses taking precedence.
75
+ */
76
+ multiaddrs?: Multiaddr[]
77
+
78
+ /**
79
+ * Peer's supported protocols
80
+ */
81
+ protocols?: string[]
82
+
83
+ /**
84
+ * Peer's metadata map. When merging pass undefined as values to remove metadata.
85
+ */
86
+ metadata?: Map<string, Uint8Array | undefined> | Record<string, Uint8Array | undefined>
87
+
88
+ /**
89
+ * Peer tags. When merging pass undefined as values to remove tags.
90
+ */
91
+ tags?: Map<string, TagOptions | undefined> | Record<string, TagOptions | undefined>
92
+
93
+ /**
94
+ * If this Peer has an RSA key, it's public key can be set with this property
95
+ */
96
+ publicKey?: Uint8Array
97
+
98
+ /**
99
+ * The last peer record envelope received
100
+ */
101
+ peerRecordEnvelope?: Uint8Array
102
+ }
103
+
104
+ export interface TagOptions {
105
+ /**
106
+ * An optional tag value (1-100)
107
+ */
108
+ value?: number
109
+
110
+ /**
111
+ * An optional duration in ms after which the tag will expire
112
+ */
113
+ ttl?: number
114
+ }
115
+
116
+ export interface Tag {
117
+ /**
118
+ * The tag value
119
+ */
120
+ value: number
121
+ }
122
+
123
+ /**
124
+ * A predicate by which to filter lists of peers
125
+ */
126
+ export interface PeerQueryFilter { (peer: Peer): boolean }
127
+
128
+ /**
129
+ * A predicate by which to sort lists of peers
130
+ */
131
+ export interface PeerQueryOrder { (a: Peer, b: Peer): -1 | 0 | 1 }
132
+
133
+ /**
134
+ * A query for getting lists of peers
135
+ */
136
+ export interface PeerQuery {
137
+ filters?: PeerQueryFilter[]
138
+ orders?: PeerQueryOrder[]
139
+ limit?: number
140
+ offset?: number
141
+ }
142
+
143
+ export interface PeerStore {
144
+ /**
145
+ * Loop over every peer - the looping is async because we read from a
146
+ * datastore but the peer operation is sync, this is to prevent
147
+ * long-lived peer operations causing deadlocks over the datastore
148
+ * which can happen if they try to access the peer store during the
149
+ * loop
150
+ *
151
+ * @example
152
+ *
153
+ * ```js
154
+ * await peerStore.forEach(peer => {
155
+ * // ...
156
+ * })
157
+ * ```
158
+ */
159
+ forEach: (fn: (peer: Peer) => void, query?: PeerQuery) => Promise<void>
160
+
161
+ /**
162
+ * Returns all peers in the peer store.
163
+ *
164
+ * @example
165
+ *
166
+ * ```js
167
+ * for (const peer of await peerStore.all()) {
168
+ * // ...
169
+ * }
170
+ * ```
171
+ */
172
+ all: (query?: PeerQuery) => Promise<Peer[]>
173
+
174
+ /**
175
+ * Delete all data stored for the passed peer
176
+ *
177
+ * @example
178
+ *
179
+ * ```js
180
+ * await peerStore.addressBook.set(peerId, multiaddrs)
181
+ * await peerStore.addressBook.get(peerId)
182
+ * // multiaddrs[]
183
+ *
184
+ * await peerStore.delete(peerId)
185
+ *
186
+ * await peerStore.addressBook.get(peerId)
187
+ * // []
188
+ * ```
189
+ */
190
+ delete: (peerId: PeerId) => Promise<void>
191
+
192
+ /**
193
+ * Returns true if the passed PeerId is in the peer store
194
+ *
195
+ * @example
196
+ *
197
+ * ```js
198
+ * await peerStore.has(peerId)
199
+ * // false
200
+ * await peerStore.addressBook.add(peerId, multiaddrs)
201
+ * await peerStore.has(peerId)
202
+ * // true
203
+ * ```
204
+ */
205
+ has: (peerId: PeerId) => Promise<boolean>
206
+
207
+ /**
208
+ * Returns all data stored for the passed PeerId
209
+ *
210
+ * @example
211
+ *
212
+ * ```js
213
+ * const peer = await peerStore.get(peerId)
214
+ * // { .. }
215
+ * ```
216
+ */
217
+ get: (peerId: PeerId) => Promise<Peer>
218
+
219
+ /**
220
+ * Adds a peer to the peer store, overwriting any existing data
221
+ *
222
+ * @example
223
+ *
224
+ * ```js
225
+ * await peerStore.save(peerId, {
226
+ * multiaddrs
227
+ * })
228
+ * ```
229
+ */
230
+ save: (id: PeerId, data: PeerData) => Promise<Peer>
231
+
232
+ /**
233
+ * Adds a peer to the peer store, overwriting only the passed fields
234
+ *
235
+ * @example
236
+ *
237
+ * ```js
238
+ * await peerStore.patch(peerId, {
239
+ * multiaddrs
240
+ * })
241
+ * ```
242
+ */
243
+ patch: (id: PeerId, data: PeerData) => Promise<Peer>
244
+
245
+ /**
246
+ * Adds a peer to the peer store, deeply merging any existing data.
247
+ *
248
+ * @example
249
+ *
250
+ * ```js
251
+ * await peerStore.merge(peerId, {
252
+ * multiaddrs
253
+ * })
254
+ * ```
255
+ */
256
+ merge: (id: PeerId, data: PeerData) => Promise<Peer>
257
+
258
+ /**
259
+ * Unmarshal and verify a signed peer record, extract the multiaddrs and
260
+ * overwrite the stored addresses for the peer.
261
+ *
262
+ * Optionally pass an expected PeerId to verify that the peer record was
263
+ * signed by that peer.
264
+ *
265
+ * @example
266
+ *
267
+ * ```js
268
+ * await peerStore.consumePeerRecord(buf, expectedPeer)
269
+ * ```
270
+ */
271
+ consumePeerRecord: (buf: Uint8Array, expectedPeer?: PeerId) => Promise<boolean>
272
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export const KEEP_ALIVE = 'keep-alive'
@@ -0,0 +1,35 @@
1
+ import type { PeerId } from '../peer-id/index.js'
2
+ import type { Uint8ArrayList } from 'uint8arraylist'
3
+
4
+ /**
5
+ * Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
6
+ */
7
+ export interface Record {
8
+ /**
9
+ * signature domain.
10
+ */
11
+ domain: string
12
+ /**
13
+ * identifier of the type of record
14
+ */
15
+ codec: Uint8Array
16
+ /**
17
+ * Marshal a record to be used in an envelope.
18
+ */
19
+ marshal: () => Uint8Array
20
+ /**
21
+ * Verifies if the other provided Record is identical to this one.
22
+ */
23
+ equals: (other: Record) => boolean
24
+ }
25
+
26
+ export interface Envelope {
27
+ peerId: PeerId
28
+ payloadType: Uint8Array | Uint8ArrayList
29
+ payload: Uint8Array
30
+ signature: Uint8Array | Uint8ArrayList
31
+
32
+ marshal: () => Uint8Array
33
+ validate: (domain: string) => Promise<boolean>
34
+ equals: (other: Envelope) => boolean
35
+ }
@@ -0,0 +1,117 @@
1
+
2
+ /**
3
+ * Implemented by components that have a lifecycle
4
+ */
5
+ export interface Startable {
6
+ isStarted: () => boolean
7
+
8
+ /**
9
+ * If implemented, this method will be invoked before the start method.
10
+ *
11
+ * It should not assume any other components have been started.
12
+ */
13
+ beforeStart?: () => void | Promise<void>
14
+
15
+ /**
16
+ * This method will be invoked to start the component.
17
+ *
18
+ * It should not assume that any other components have been started.
19
+ */
20
+ start: () => void | Promise<void>
21
+
22
+ /**
23
+ * If implemented, this method will be invoked after the start method.
24
+ *
25
+ * All other components will have had their start method invoked before this method is called.
26
+ */
27
+ afterStart?: () => void | Promise<void>
28
+
29
+ /**
30
+ * If implemented, this method will be invoked before the stop method.
31
+ *
32
+ * Any other components will still be running when this method is called.
33
+ */
34
+ beforeStop?: () => void | Promise<void>
35
+
36
+ /**
37
+ * This method will be invoked to stop the component.
38
+ *
39
+ * It should not assume any other components are running when it is called.
40
+ */
41
+ stop: () => void | Promise<void>
42
+
43
+ /**
44
+ * If implemented, this method will be invoked after the stop method.
45
+ *
46
+ * All other components will have had their stop method invoked before this method is called.
47
+ */
48
+ afterStop?: () => void | Promise<void>
49
+ }
50
+
51
+ export function isStartable (obj: any): obj is Startable {
52
+ return obj != null && typeof obj.start === 'function' && typeof obj.stop === 'function'
53
+ }
54
+
55
+ export async function start (...objs: any[]): Promise<void> {
56
+ const startables: Startable[] = []
57
+
58
+ for (const obj of objs) {
59
+ if (isStartable(obj)) {
60
+ startables.push(obj)
61
+ }
62
+ }
63
+
64
+ await Promise.all(
65
+ startables.map(async s => {
66
+ if (s.beforeStart != null) {
67
+ await s.beforeStart()
68
+ }
69
+ })
70
+ )
71
+
72
+ await Promise.all(
73
+ startables.map(async s => {
74
+ await s.start()
75
+ })
76
+ )
77
+
78
+ await Promise.all(
79
+ startables.map(async s => {
80
+ if (s.afterStart != null) {
81
+ await s.afterStart()
82
+ }
83
+ })
84
+ )
85
+ }
86
+
87
+ export async function stop (...objs: any[]): Promise<void> {
88
+ const startables: Startable[] = []
89
+
90
+ for (const obj of objs) {
91
+ if (isStartable(obj)) {
92
+ startables.push(obj)
93
+ }
94
+ }
95
+
96
+ await Promise.all(
97
+ startables.map(async s => {
98
+ if (s.beforeStop != null) {
99
+ await s.beforeStop()
100
+ }
101
+ })
102
+ )
103
+
104
+ await Promise.all(
105
+ startables.map(async s => {
106
+ await s.stop()
107
+ })
108
+ )
109
+
110
+ await Promise.all(
111
+ startables.map(async s => {
112
+ if (s.afterStop != null) {
113
+ await s.afterStop()
114
+ }
115
+ })
116
+ )
117
+ }
@@ -0,0 +1,27 @@
1
+ import type { Connection, Stream } from '../connection/index.js'
2
+
3
+ export interface IncomingStreamData {
4
+ stream: Stream
5
+ connection: Connection
6
+ }
7
+
8
+ export interface StreamHandler {
9
+ (data: IncomingStreamData): void
10
+ }
11
+
12
+ export interface StreamHandlerOptions {
13
+ /**
14
+ * How many incoming streams can be open for this protocol at the same time on each connection (default: 32)
15
+ */
16
+ maxInboundStreams?: number
17
+
18
+ /**
19
+ * How many outgoing streams can be open for this protocol at the same time on each connection (default: 64)
20
+ */
21
+ maxOutboundStreams?: number
22
+ }
23
+
24
+ export interface StreamHandlerRecord {
25
+ handler: StreamHandler
26
+ options: StreamHandlerOptions
27
+ }
@@ -0,0 +1,58 @@
1
+ import type { Direction, Stream } from '../connection/index.js'
2
+ import type { AbortOptions } from '../index.js'
3
+ import type { Duplex, Source } from 'it-stream-types'
4
+ import type { Uint8ArrayList } from 'uint8arraylist'
5
+
6
+ export interface StreamMuxerFactory {
7
+ /**
8
+ * The protocol used to select this muxer during connection opening
9
+ */
10
+ protocol: string
11
+
12
+ /**
13
+ * Creates a new stream muxer to be used with a new connection
14
+ */
15
+ createStreamMuxer: (init?: StreamMuxerInit) => StreamMuxer
16
+ }
17
+
18
+ /**
19
+ * A libp2p stream muxer
20
+ */
21
+ export interface StreamMuxer extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8ArrayList | Uint8Array>, Promise<void>> {
22
+ /**
23
+ * The protocol used to select this muxer during connection opening
24
+ */
25
+ protocol: string
26
+
27
+ /**
28
+ * A list of streams that are currently open. Closed streams will not be returned.
29
+ */
30
+ readonly streams: Stream[]
31
+ /**
32
+ * Initiate a new stream with the given name. If no name is
33
+ * provided, the id of the stream will be used.
34
+ */
35
+ newStream: (name?: string) => Stream | Promise<Stream>
36
+
37
+ /**
38
+ * Close or abort all tracked streams and stop the muxer
39
+ */
40
+ close: (err?: Error) => void
41
+ }
42
+
43
+ export interface StreamMuxerInit extends AbortOptions {
44
+ /**
45
+ * A callback function invoked every time an incoming stream is opened
46
+ */
47
+ onIncomingStream?: (stream: Stream) => void
48
+
49
+ /**
50
+ * A callback function invoke every time a stream ends
51
+ */
52
+ onStreamEnd?: (stream: Stream) => void
53
+
54
+ /**
55
+ * Outbound stream muxers are opened by the local node, inbound stream muxers are opened by the remote
56
+ */
57
+ direction?: Direction
58
+ }