@libp2p/interface 0.0.1 → 0.1.0

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 (44) hide show
  1. package/dist/src/connection/index.d.ts +152 -53
  2. package/dist/src/connection/index.d.ts.map +1 -1
  3. package/dist/src/connection/index.js.map +1 -1
  4. package/dist/src/errors.d.ts.map +1 -1
  5. package/dist/src/errors.js.map +1 -1
  6. package/dist/src/index.d.ts +8 -4
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/keys/index.d.ts.map +1 -1
  9. package/dist/src/keys/index.js.map +1 -1
  10. package/dist/src/peer-store/tags.d.ts.map +1 -1
  11. package/dist/src/peer-store/tags.js.map +1 -1
  12. package/dist/src/pubsub/index.d.ts +236 -0
  13. package/dist/src/pubsub/index.d.ts.map +1 -0
  14. package/dist/src/pubsub/index.js +36 -0
  15. package/dist/src/pubsub/index.js.map +1 -0
  16. package/dist/src/startable.d.ts.map +1 -1
  17. package/dist/src/startable.js.map +1 -1
  18. package/dist/src/stream-handler/index.d.ts +5 -0
  19. package/dist/src/stream-handler/index.d.ts.map +1 -1
  20. package/dist/src/stream-muxer/index.d.ts +6 -2
  21. package/dist/src/stream-muxer/index.d.ts.map +1 -1
  22. package/dist/src/stream-muxer/stream.d.ts +80 -21
  23. package/dist/src/stream-muxer/stream.d.ts.map +1 -1
  24. package/dist/src/stream-muxer/stream.js +224 -168
  25. package/dist/src/stream-muxer/stream.js.map +1 -1
  26. package/dist/src/transport/index.d.ts +5 -0
  27. package/dist/src/transport/index.d.ts.map +1 -1
  28. package/package.json +8 -11
  29. package/src/connection/index.ts +180 -59
  30. package/src/errors.ts +0 -1
  31. package/src/index.ts +8 -4
  32. package/src/keys/index.ts +0 -1
  33. package/src/peer-store/tags.ts +0 -1
  34. package/src/pubsub/index.ts +269 -0
  35. package/src/startable.ts +0 -1
  36. package/src/stream-handler/index.ts +6 -0
  37. package/src/stream-muxer/index.ts +7 -2
  38. package/src/stream-muxer/stream.ts +295 -186
  39. package/src/transport/index.ts +6 -0
  40. package/dist/src/connection/status.d.ts +0 -4
  41. package/dist/src/connection/status.d.ts.map +0 -1
  42. package/dist/src/connection/status.js +0 -4
  43. package/dist/src/connection/status.js.map +0 -1
  44. package/src/connection/status.ts +0 -4
@@ -1,4 +1,3 @@
1
- import type * as Status from './status.js'
2
1
  import type { AbortOptions } from '../index.js'
3
2
  import type { PeerId } from '../peer-id/index.js'
4
3
  import type { Multiaddr } from '@multiformats/multiaddr'
@@ -6,43 +5,28 @@ import type { Duplex, Source } from 'it-stream-types'
6
5
  import type { Uint8ArrayList } from 'uint8arraylist'
7
6
 
8
7
  export interface ConnectionTimeline {
9
- open: number
10
- upgraded?: number
11
- close?: number
12
- }
13
-
14
- /**
15
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
16
- */
17
- export type Direction = 'inbound' | 'outbound'
18
-
19
- export interface ConnectionStat {
20
- /**
21
- * Outbound conections are opened by the local node, inbound streams are opened by the remote
22
- */
23
- direction: Direction
24
-
25
- /**
26
- * Lifecycle times for the connection
27
- */
28
- timeline: ConnectionTimeline
29
-
30
8
  /**
31
- * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
9
+ * When the connection was opened
32
10
  */
33
- multiplexer?: string
11
+ open: number
34
12
 
35
13
  /**
36
- * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
14
+ * When the MultiaddrConnection was upgraded to a Connection - e.g. the type
15
+ * of connection encryption and multiplexing was negotiated.
37
16
  */
38
- encryption?: string
17
+ upgraded?: number
39
18
 
40
19
  /**
41
- * The current status of the connection
20
+ * When the connection was closed.
42
21
  */
43
- status: keyof typeof Status
22
+ close?: number
44
23
  }
45
24
 
25
+ /**
26
+ * Outbound connections are opened by the local node, inbound streams are opened by the remote
27
+ */
28
+ export type Direction = 'inbound' | 'outbound'
29
+
46
30
  export interface StreamTimeline {
47
31
  /**
48
32
  * A timestamp of when the stream was opened
@@ -68,24 +52,37 @@ export interface StreamTimeline {
68
52
  * A timestamp of when the stream was reset
69
53
  */
70
54
  reset?: number
71
- }
72
55
 
73
- export interface StreamStat {
74
56
  /**
75
- * Outbound streams are opened by the local node, inbound streams are opened by the remote
57
+ * A timestamp of when the stream was aborted
76
58
  */
77
- direction: Direction
59
+ abort?: number
60
+ }
78
61
 
79
- /**
80
- * Lifecycle times for the stream
81
- */
82
- timeline: StreamTimeline
62
+ /**
63
+ * The states a stream can be in
64
+ */
65
+ export type StreamStatus = 'open' | 'closing' | 'closed' | 'aborted' | 'reset'
83
66
 
84
- /**
85
- * Once a protocol has been negotiated for this stream, it will be set on the stat object
86
- */
87
- protocol?: string
88
- }
67
+ /**
68
+ * The states the readable end of a stream can be in
69
+ *
70
+ * ready - the readable end is ready for reading
71
+ * closing - the readable end is closing
72
+ * closed - the readable end has closed
73
+ */
74
+ export type ReadStatus = 'ready' | 'closing' | 'closed'
75
+
76
+ /**
77
+ * The states the writable end of a stream can be in
78
+ *
79
+ * ready - the writable end is ready for writing
80
+ * writing - the writable end is in the process of being written to
81
+ * done - the source passed to the `.sink` function yielded all values without error
82
+ * closing - the writable end is closing
83
+ * closed - the writable end has closed
84
+ */
85
+ export type WriteStatus = 'ready' | 'writing' | 'done' | 'closing' | 'closed'
89
86
 
90
87
  /**
91
88
  * A Stream is a data channel between two peers that
@@ -104,7 +101,7 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
104
101
  *
105
102
  * The sink and the source will return normally.
106
103
  */
107
- close: () => void
104
+ close: (options?: AbortOptions) => Promise<void>
108
105
 
109
106
  /**
110
107
  * 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.
@@ -113,14 +110,14 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
113
110
  *
114
111
  * The source will return normally, the sink will continue to consume.
115
112
  */
116
- closeRead: () => void
113
+ closeRead: (options?: AbortOptions) => Promise<void>
117
114
 
118
115
  /**
119
116
  * 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.
120
117
  *
121
118
  * The source will return normally, the sink will continue to consume.
122
119
  */
123
- closeWrite: () => void
120
+ closeWrite: (options?: AbortOptions) => Promise<void>
124
121
 
125
122
  /**
126
123
  * Closes the stream for **reading** *and* **writing**. This should be called when a *local error* has occurred.
@@ -134,28 +131,44 @@ export interface Stream extends Duplex<AsyncGenerator<Uint8ArrayList>, Source<Ui
134
131
  abort: (err: Error) => void
135
132
 
136
133
  /**
137
- * Closes the stream *immediately* for **reading** *and* **writing**. This should be called when a *remote error* has occurred.
138
- *
139
- * This function is called automatically by the muxer when it receives a `RESET` message from the remote.
140
- *
141
- * The sink will return and the source will throw.
134
+ * Unique identifier for a stream. Identifiers are not unique across muxers.
142
135
  */
143
- reset: () => void
136
+ id: string
144
137
 
145
138
  /**
146
- * Unique identifier for a stream. Identifiers are not unique across muxers.
139
+ * Outbound streams are opened by the local node, inbound streams are opened by the remote
147
140
  */
148
- id: string
141
+ direction: Direction
142
+
143
+ /**
144
+ * Lifecycle times for the stream
145
+ */
146
+ timeline: StreamTimeline
149
147
 
150
148
  /**
151
- * Stats about this stream
149
+ * Once a protocol has been negotiated for this stream, it will be set on the stat object
152
150
  */
153
- stat: StreamStat
151
+ protocol?: string
154
152
 
155
153
  /**
156
154
  * User defined stream metadata
157
155
  */
158
156
  metadata: Record<string, any>
157
+
158
+ /**
159
+ * The current status of the stream
160
+ */
161
+ status: StreamStatus
162
+
163
+ /**
164
+ * The current status of the readable end of the stream
165
+ */
166
+ readStatus: ReadStatus
167
+
168
+ /**
169
+ * The current status of the writable end of the stream
170
+ */
171
+ writeStatus: WriteStatus
159
172
  }
160
173
 
161
174
  export interface NewStreamOptions extends AbortOptions {
@@ -165,8 +178,16 @@ export interface NewStreamOptions extends AbortOptions {
165
178
  * for the protocol
166
179
  */
167
180
  maxOutboundStreams?: number
181
+
182
+ /**
183
+ * Opt-in to running over a transient connection - one that has time/data limits
184
+ * placed on it.
185
+ */
186
+ runOnTransientConnection?: boolean
168
187
  }
169
188
 
189
+ export type ConnectionStatus = 'open' | 'closing' | 'closed'
190
+
170
191
  /**
171
192
  * A Connection is a high-level representation of a connection
172
193
  * to a remote peer that may have been secured by encryption and
@@ -174,17 +195,89 @@ export interface NewStreamOptions extends AbortOptions {
174
195
  * between which the connection is made.
175
196
  */
176
197
  export interface Connection {
198
+ /**
199
+ * The unique identifier for this connection
200
+ */
177
201
  id: string
178
- stat: ConnectionStat
202
+
203
+ /**
204
+ * The address of the remote end of the connection
205
+ */
179
206
  remoteAddr: Multiaddr
207
+
208
+ /**
209
+ * The id of the peer at the remote end of the connection
210
+ */
180
211
  remotePeer: PeerId
212
+
213
+ /**
214
+ * A list of tags applied to this connection
215
+ */
181
216
  tags: string[]
217
+
218
+ /**
219
+ * A list of open streams on this connection
220
+ */
182
221
  streams: Stream[]
183
222
 
184
- newStream: (multicodecs: string | string[], options?: NewStreamOptions) => Promise<Stream>
223
+ /**
224
+ * Outbound conections are opened by the local node, inbound streams are opened by the remote
225
+ */
226
+ direction: Direction
227
+
228
+ /**
229
+ * Lifecycle times for the connection
230
+ */
231
+ timeline: ConnectionTimeline
232
+
233
+ /**
234
+ * Once a multiplexer has been negotiated for this stream, it will be set on the stat object
235
+ */
236
+ multiplexer?: string
237
+
238
+ /**
239
+ * Once a connection encrypter has been negotiated for this stream, it will be set on the stat object
240
+ */
241
+ encryption?: string
242
+
243
+ /**
244
+ * The current status of the connection
245
+ */
246
+ status: ConnectionStatus
247
+
248
+ /**
249
+ * A transient connection is one that is not expected to be open for very long
250
+ * or one that cannot transfer very much data, such as one being used as a
251
+ * circuit relay connection. Protocols need to explicitly opt-in to being run
252
+ * over transient connections.
253
+ */
254
+ transient: boolean
255
+
256
+ /**
257
+ * Create a new stream on this connection and negotiate one of the passed protocols
258
+ */
259
+ newStream: (protocols: string | string[], options?: NewStreamOptions) => Promise<Stream>
260
+
261
+ /**
262
+ * Add a stream to this connection
263
+ */
185
264
  addStream: (stream: Stream) => void
265
+
266
+ /**
267
+ * Remove a stream from this connection
268
+ */
186
269
  removeStream: (id: string) => void
187
- close: () => Promise<void>
270
+
271
+ /**
272
+ * Gracefully close the connection. All queued data will be written to the
273
+ * underlying transport.
274
+ */
275
+ close: (options?: AbortOptions) => Promise<void>
276
+
277
+ /**
278
+ * Immediately close the connection, any queued data will be discarded
279
+ */
280
+ abort: (err: Error) => void
188
281
  }
189
282
 
190
283
  export const symbol = Symbol.for('@libp2p/connection')
@@ -194,7 +287,6 @@ export function isConnection (other: any): other is Connection {
194
287
  }
195
288
 
196
289
  export interface ConnectionProtector {
197
-
198
290
  /**
199
291
  * Takes a given Connection and creates a private encryption stream
200
292
  * between its two peers from the PSK the Protector instance was
@@ -204,8 +296,20 @@ export interface ConnectionProtector {
204
296
  }
205
297
 
206
298
  export interface MultiaddrConnectionTimeline {
299
+ /**
300
+ * When the connection was opened
301
+ */
207
302
  open: number
303
+
304
+ /**
305
+ * When the MultiaddrConnection was upgraded to a Connection - the type of
306
+ * connection encryption and multiplexing was negotiated.
307
+ */
208
308
  upgraded?: number
309
+
310
+ /**
311
+ * When the connection was closed.
312
+ */
209
313
  close?: number
210
314
  }
211
315
 
@@ -215,7 +319,24 @@ export interface MultiaddrConnectionTimeline {
215
319
  * without encryption or stream multiplexing.
216
320
  */
217
321
  export interface MultiaddrConnection extends Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>> {
218
- close: (err?: Error) => Promise<void>
322
+ /**
323
+ * Gracefully close the connection. All queued data will be written to the
324
+ * underlying transport.
325
+ */
326
+ close: (options?: AbortOptions) => Promise<void>
327
+
328
+ /**
329
+ * Immediately close the connection, any queued data will be discarded
330
+ */
331
+ abort: (err: Error) => void
332
+
333
+ /**
334
+ * The address of the remote end of the connection
335
+ */
219
336
  remoteAddr: Multiaddr
337
+
338
+ /**
339
+ * When connection lifecycle events occurred
340
+ */
220
341
  timeline: MultiaddrConnectionTimeline
221
342
  }
package/src/errors.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * When this error is thrown it means an operation was aborted,
4
3
  * usually in response to the `abort` event being emitted by an
package/src/index.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  * ```
15
15
  */
16
16
 
17
- import type { Connection, Stream } from './connection/index.js'
17
+ import type { Connection, NewStreamOptions, Stream } from './connection/index.js'
18
18
  import type { ContentRouting } from './content-routing/index.js'
19
19
  import type { EventEmitter } from './events.js'
20
20
  import type { KeyChain } from './keychain/index.js'
@@ -503,10 +503,14 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ev
503
503
  * pipe([1, 2, 3], stream, consume)
504
504
  * ```
505
505
  */
506
- dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: AbortOptions) => Promise<Stream>
506
+ dialProtocol: (peer: PeerId | Multiaddr | Multiaddr[], protocols: string | string[], options?: NewStreamOptions) => Promise<Stream>
507
507
 
508
508
  /**
509
- * Attempts to gracefully close an open connection to the given peer. If the connection is not closed in the grace period, it will be forcefully closed.
509
+ * Attempts to gracefully close an open connection to the given peer. If the
510
+ * connection is not closed in the grace period, it will be forcefully closed.
511
+ *
512
+ * An AbortSignal can optionally be passed to control when the connection is
513
+ * forcefully closed.
510
514
  *
511
515
  * @example
512
516
  *
@@ -514,7 +518,7 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ev
514
518
  * await libp2p.hangUp(remotePeerId)
515
519
  * ```
516
520
  */
517
- hangUp: (peer: PeerId | Multiaddr) => Promise<void>
521
+ hangUp: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<void>
518
522
 
519
523
  /**
520
524
  * Sets up [multistream-select routing](https://github.com/multiformats/multistream-select) of protocols to their application handlers. Whenever a stream is opened on one of the provided protocols, the handler will be called. `handle` must be called in order to register a handler and support for a given protocol. This also informs other peers of the protocols you support.
package/src/keys/index.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  export interface PublicKey {
3
2
  readonly bytes: Uint8Array
4
3
  verify: (data: Uint8Array, sig: Uint8Array) => Promise<boolean>
@@ -1,2 +1 @@
1
-
2
1
  export const KEEP_ALIVE = 'keep-alive'
@@ -0,0 +1,269 @@
1
+ import type { Stream } from '../connection/index.js'
2
+ import type { EventEmitter } from '../events.js'
3
+ import type { PeerId } from '../peer-id/index.js'
4
+ import type { Pushable } from 'it-pushable'
5
+ import type { Uint8ArrayList } from 'uint8arraylist'
6
+
7
+ /**
8
+ * On the producing side:
9
+ * * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
10
+ *
11
+ * On the consuming side:
12
+ * * Enforce the fields to be present, reject otherwise.
13
+ * * Propagate only if the fields are valid and signature can be verified, reject otherwise.
14
+ */
15
+ export const StrictSign = 'StrictSign'
16
+
17
+ /**
18
+ * On the producing side:
19
+ * * Build messages without the signature, key, from and seqno fields.
20
+ * * The corresponding protobuf key-value pairs are absent from the marshalled message, not just empty.
21
+ *
22
+ * On the consuming side:
23
+ * * Enforce the fields to be absent, reject otherwise.
24
+ * * Propagate only if the fields are absent, reject otherwise.
25
+ * * A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
26
+ */
27
+ export const StrictNoSign = 'StrictNoSign'
28
+
29
+ export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
30
+
31
+ export interface SignedMessage {
32
+ type: 'signed'
33
+ from: PeerId
34
+ topic: string
35
+ data: Uint8Array
36
+ sequenceNumber: bigint
37
+ signature: Uint8Array
38
+ key: Uint8Array
39
+ }
40
+
41
+ export interface UnsignedMessage {
42
+ type: 'unsigned'
43
+ topic: string
44
+ data: Uint8Array
45
+ }
46
+
47
+ export type Message = SignedMessage | UnsignedMessage
48
+
49
+ export interface PubSubRPCMessage {
50
+ from?: Uint8Array
51
+ topic?: string
52
+ data?: Uint8Array
53
+ sequenceNumber?: Uint8Array
54
+ signature?: Uint8Array
55
+ key?: Uint8Array
56
+ }
57
+
58
+ export interface PubSubRPCSubscription {
59
+ subscribe?: boolean
60
+ topic?: string
61
+ }
62
+
63
+ export interface PubSubRPC {
64
+ subscriptions: PubSubRPCSubscription[]
65
+ messages: PubSubRPCMessage[]
66
+ }
67
+
68
+ export interface PeerStreams extends EventEmitter<PeerStreamEvents> {
69
+ id: PeerId
70
+ protocol: string
71
+ outboundStream?: Pushable<Uint8ArrayList>
72
+ inboundStream?: AsyncIterable<Uint8ArrayList>
73
+ isWritable: boolean
74
+
75
+ close: () => void
76
+ write: (buf: Uint8Array | Uint8ArrayList) => void
77
+ attachInboundStream: (stream: Stream) => AsyncIterable<Uint8ArrayList>
78
+ attachOutboundStream: (stream: Stream) => Promise<Pushable<Uint8ArrayList>>
79
+ }
80
+
81
+ export interface PubSubInit {
82
+ enabled?: boolean
83
+
84
+ multicodecs?: string[]
85
+
86
+ /**
87
+ * defines how signatures should be handled
88
+ */
89
+ globalSignaturePolicy?: SignaturePolicy
90
+
91
+ /**
92
+ * if can relay messages not subscribed
93
+ */
94
+ canRelayMessage?: boolean
95
+
96
+ /**
97
+ * if publish should emit to self, if subscribed
98
+ */
99
+ emitSelf?: boolean
100
+
101
+ /**
102
+ * handle this many incoming pubsub messages concurrently
103
+ */
104
+ messageProcessingConcurrency?: number
105
+
106
+ /**
107
+ * How many parallel incoming streams to allow on the pubsub protocol per-connection
108
+ */
109
+ maxInboundStreams?: number
110
+
111
+ /**
112
+ * How many parallel outgoing streams to allow on the pubsub protocol per-connection
113
+ */
114
+ maxOutboundStreams?: number
115
+ }
116
+
117
+ interface Subscription {
118
+ topic: string
119
+ subscribe: boolean
120
+ }
121
+
122
+ export interface SubscriptionChangeData {
123
+ peerId: PeerId
124
+ subscriptions: Subscription[]
125
+ }
126
+
127
+ export interface PubSubEvents {
128
+ 'subscription-change': CustomEvent<SubscriptionChangeData>
129
+ 'message': CustomEvent<Message>
130
+ }
131
+
132
+ export interface PublishResult {
133
+ recipients: PeerId[]
134
+ }
135
+
136
+ export enum TopicValidatorResult {
137
+ /**
138
+ * The message is considered valid, and it should be delivered and forwarded to the network
139
+ */
140
+ Accept = 'accept',
141
+ /**
142
+ * The message is neither delivered nor forwarded to the network
143
+ */
144
+ Ignore = 'ignore',
145
+ /**
146
+ * The message is considered invalid, and it should be rejected
147
+ */
148
+ Reject = 'reject'
149
+ }
150
+
151
+ export interface TopicValidatorFn {
152
+ (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
153
+ }
154
+
155
+ export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> {
156
+ /**
157
+ * The global signature policy controls whether or not we sill send and receive
158
+ * signed or unsigned messages.
159
+ *
160
+ * Signed messages prevent spoofing message senders and should be preferred to
161
+ * using unsigned messages.
162
+ */
163
+ globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
164
+
165
+ /**
166
+ * A list of multicodecs that contain the pubsub protocol name.
167
+ */
168
+ multicodecs: string[]
169
+
170
+ /**
171
+ * Pubsub routers support message validators per topic, which will validate the message
172
+ * before its propagations. They are stored in a map where keys are the topic name and
173
+ * values are the validators.
174
+ *
175
+ * @example
176
+ *
177
+ * ```js
178
+ * const topic = 'topic'
179
+ * const validateMessage = (msgTopic, msg) => {
180
+ * const input = uint8ArrayToString(msg.data)
181
+ * const validInputs = ['a', 'b', 'c']
182
+ *
183
+ * if (!validInputs.includes(input)) {
184
+ * throw new Error('no valid input received')
185
+ * }
186
+ * }
187
+ * libp2p.pubsub.topicValidators.set(topic, validateMessage)
188
+ * ```
189
+ */
190
+ topicValidators: Map<string, TopicValidatorFn>
191
+
192
+ getPeers: () => PeerId[]
193
+
194
+ /**
195
+ * Gets a list of topics the node is subscribed to.
196
+ *
197
+ * ```js
198
+ * const topics = libp2p.pubsub.getTopics()
199
+ * ```
200
+ */
201
+ getTopics: () => string[]
202
+
203
+ /**
204
+ * Subscribes to a pubsub topic.
205
+ *
206
+ * @example
207
+ *
208
+ * ```js
209
+ * const topic = 'topic'
210
+ * const handler = (msg) => {
211
+ * if (msg.topic === topic) {
212
+ * // msg.data - pubsub data received
213
+ * }
214
+ * }
215
+ *
216
+ * libp2p.pubsub.addEventListener('message', handler)
217
+ * libp2p.pubsub.subscribe(topic)
218
+ * ```
219
+ */
220
+ subscribe: (topic: string) => void
221
+
222
+ /**
223
+ * Unsubscribes from a pubsub topic.
224
+ *
225
+ * @example
226
+ *
227
+ * ```js
228
+ * const topic = 'topic'
229
+ * const handler = (msg) => {
230
+ * // msg.data - pubsub data received
231
+ * }
232
+ *
233
+ * libp2p.pubsub.removeEventListener(topic handler)
234
+ * libp2p.pubsub.unsubscribe(topic)
235
+ * ```
236
+ */
237
+ unsubscribe: (topic: string) => void
238
+
239
+ /**
240
+ * Gets a list of the PeerIds that are subscribed to one topic.
241
+ *
242
+ * @example
243
+ *
244
+ * ```js
245
+ * const peerIds = libp2p.pubsub.getSubscribers(topic)
246
+ * ```
247
+ */
248
+ getSubscribers: (topic: string) => PeerId[]
249
+
250
+ /**
251
+ * Publishes messages to the given topic.
252
+ *
253
+ * @example
254
+ *
255
+ * ```js
256
+ * const topic = 'topic'
257
+ * const data = uint8ArrayFromString('data')
258
+ *
259
+ * await libp2p.pubsub.publish(topic, data)
260
+ * ```
261
+ */
262
+ publish: (topic: string, data: Uint8Array) => Promise<PublishResult>
263
+ }
264
+
265
+ export interface PeerStreamEvents {
266
+ 'stream:inbound': CustomEvent<never>
267
+ 'stream:outbound': CustomEvent<never>
268
+ 'close': CustomEvent<never>
269
+ }
package/src/startable.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  /**
3
2
  * Implemented by components that have a lifecycle
4
3
  */
@@ -19,6 +19,12 @@ export interface StreamHandlerOptions {
19
19
  * How many outgoing streams can be open for this protocol at the same time on each connection (default: 64)
20
20
  */
21
21
  maxOutboundStreams?: number
22
+
23
+ /**
24
+ * Opt-in to running over a transient connection - one that has time/data limits
25
+ * placed on it.
26
+ */
27
+ runOnTransientConnection?: boolean
22
28
  }
23
29
 
24
30
  export interface StreamHandlerRecord {