Skip to content

Commit 5de2e22

Browse files
committed
chore: address review
1 parent cfa0136 commit 5de2e22

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

API.md

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* [`peerRouting.findPeer`](#peerRouting.findPeer)
1414
* [`contentRouting.findProviders`](#contentRouting.findProviders)
1515
* [`contentRouting.provide`](#contentRouting.provide)
16-
* [`_dht.put`](#_dht.put)
17-
* [`_dht.get`](#_dht.get)
18-
* [`_dht.getMany`](#_dht.getMany)
16+
* [`contentRouting.put`](#contentRouting.put)
17+
* [`contentRouting.get`](#contentRouting.get)
18+
* [`contentRouting.getMany`](#contentRouting.getMany)
1919
* [`pubsub.getPeersSubscribed`](#pubsub.getPeersSubscribed)
2020
* [`pubsub.getTopics`](#pubsub.getTopics)
2121
* [`pubsub.publish`](#pubsub.publish)
@@ -55,6 +55,7 @@ const libp2p = await Libp2p.create(options)
5555

5656
Note: The `PeerInfo` option is not required and will be generated if it is not provided.
5757

58+
<details><summary>Alternative</summary>
5859
As an alternative, it is possible to create a Libp2p instance with the constructor:
5960

6061
#### Example
@@ -71,9 +72,11 @@ const libp2p = new Libp2p(options)
7172

7273
Required keys in the `options` object:
7374

74-
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
75+
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node (optional when using `.create`).
7576
- `modules.transport`: An array that must include at least 1 compliant transport. See [modules that implement the transport interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport#modules-that-implement-the-interface).
7677

78+
</details>
79+
7780
## Libp2p Instance Methods
7881

7982
### start
@@ -151,11 +154,19 @@ Dials to another peer in the network and establishes the connection.
151154
```js
152155
// ...
153156
const conn = await libp2p.dial(remotePeerInfo)
157+
158+
// create a new stream within the connection
159+
const { stream, protocol } = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])
160+
161+
// protocol negotiated: 'echo/1.0.0' means that the other party only supports the older version
162+
163+
// ...
164+
await conn.close()
154165
```
155166

156167
### dialProtocol
157168

158-
Dials to another peer in the network and selects a protocol to communicate with that peer.
169+
Dials to another peer in the network and selects a protocol to communicate with that peer. The stream between both parties is returned, together with the negotiated protocol.
159170

160171
`dialProtocol(peer, protocols, options)`
161172

@@ -172,13 +183,18 @@ Dials to another peer in the network and selects a protocol to communicate with
172183

173184
| Type | Description |
174185
|------|-------------|
175-
| `Promise<Connection>` | Promise resolves with the [Connection](https://github.com/libp2p/js-interfaces/tree/master/src/connection) instance |
186+
| `Promise<{ stream:*, protocol:string }>` | Promise resolves with a [duplex stream](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) and the protocol used |
176187

177188
#### Example
178189

179190
```js
180191
// ...
192+
const pipe = require('it-pipe')
193+
181194
const { stream, protocol } = await libp2p.dialProtocol(remotePeerInfo, protocols)
195+
196+
// Use this new stream like any other duplex stream
197+
pipe([1, 2, 3], stream, consume)
182198
```
183199

184200
### hangUp
@@ -212,6 +228,8 @@ Sets up [multistream-select routing](https://github.com/multiformats/multistream
212228

213229
`libp2p.handle(protocols, handler)`
214230

231+
In the event of a new handler for the same protocol being added, the first one is discarded.
232+
215233
#### Parameters
216234

217235
| Name | Type | Description |
@@ -297,13 +315,15 @@ Once a content router succeeds, the iteration will stop. If the DHT is enabled,
297315

298316
| Type | Description |
299317
|------|-------------|
300-
| `Promise<Array<PeerInfo>>` | array of [`PeerInfo`](https://github.com/libp2p/js-peer-info) |
318+
| `AsyncIterator<PeerInfo>` | Async iterator for [`PeerInfo`](https://github.com/libp2p/js-peer-info) |
301319

302320
#### Example
303321

304322
```js
305-
// ...
306-
const providers = await libp2p.contentRouting.findProviders(cid)
323+
// Iterate over the providers found for the given cid
324+
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
325+
console.log(provider)
326+
}
307327
```
308328

309329
### contentRouting.provide
@@ -331,11 +351,11 @@ Iterates over all content routers in parallel, in order to notify it is a provid
331351
await libp2p.contentRouting.provide(cid)
332352
```
333353

334-
### _dht.put
354+
### contentRouting.put
335355

336356
Writes a value to a key in the DHT.
337357

338-
`libp2p._dht.put(key, value, options)`
358+
`libp2p.contentRouting.put(key, value, options)`
339359

340360
#### Parameters
341361

@@ -359,14 +379,14 @@ Writes a value to a key in the DHT.
359379
const key = '/key'
360380
const value = Buffer.from('oh hello there')
361381

362-
await libp2p._dht.put(key, value)
382+
await libp2p.contentRouting.put(key, value)
363383
```
364384

365-
### _dht.get
385+
### contentRouting.get
366386

367387
Queries the DHT for a value stored for a given key.
368388

369-
`libp2p._dht.get(key, options)`
389+
`libp2p.contentRouting.get(key, options)`
370390

371391
#### Parameters
372392

@@ -388,14 +408,14 @@ Queries the DHT for a value stored for a given key.
388408
// ...
389409

390410
const key = '/key'
391-
const value = await libp2p._dht.get(key)
411+
const value = await libp2p.contentRouting.get(key)
392412
```
393413

394-
### _dht.getMany
414+
### contentRouting.getMany
395415

396416
Queries the DHT for the n values stored for the given key (without sorting).
397417

398-
`libp2p._dht.getMany(key, nvals, options)`
418+
`libp2p.contentRouting.getMany(key, nvals, options)`
399419

400420
#### Parameters
401421

@@ -418,7 +438,7 @@ Queries the DHT for the n values stored for the given key (without sorting).
418438
// ...
419439

420440
const key = '/key'
421-
const { from, val } = await libp2p._dht.get(key)
441+
const { from, val } = await libp2p.contentRouting.get(key)
422442
```
423443

424444
### pubsub.getPeersSubscribed
@@ -442,7 +462,7 @@ Gets a list of the peer-ids that are subscribed to one topic.
442462
#### Example
443463

444464
```js
445-
const peerIds = libp2p.pubsub.getPeersSubscribed(topic)
465+
const peerIds = libp2p.pubsub.getSubscribers(topic)
446466
```
447467

448468
### pubsub.getTopics
@@ -502,7 +522,7 @@ Subscribes the given handler to a pubsub topic.
502522
| Name | Type | Description |
503523
|------|------|-------------|
504524
| topic | `string` | topic to subscribe |
505-
| handler | `function(<Object>)` | handler for new data on topic |
525+
| handler | `function({ from: String, data: Buffer, seqno: Buffer, topicIDs: Array<String>, signature: Buffer, key: Buffer })` | handler for new data on topic |
506526

507527
#### Returns
508528

0 commit comments

Comments
 (0)