You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-316Lines changed: 1 addition & 316 deletions
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,6 @@ We've come a long way, but this project is still in Alpha, lots of development i
50
50
-[Install](#install)
51
51
-[Usage](#usage)
52
52
-[API](#api)
53
-
-[Events](#events)
54
53
-[Development](#development)
55
54
-[Tests](#tests)
56
55
-[Packages](#packages)
@@ -209,321 +208,7 @@ class Node extends Libp2p {
209
208
210
209
### API
211
210
212
-
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
213
-
214
-
#### Create a Node - `Libp2p.create(options)`
215
-
216
-
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead
217
-
218
-
```js
219
-
const { create } =require('libp2p')
220
-
constlibp2p=awaitcreate(options)
221
-
222
-
awaitlibp2p.start()
223
-
```
224
-
225
-
-`options`: Object of libp2p configuration options
226
-
227
-
#### Create a Node alternative - `new Libp2p(options)`
228
-
229
-
> Creates an instance of Libp2p with a custom `PeerInfo` provided via `options.peerInfo`.
230
-
231
-
Required keys in the `options` object:
232
-
233
-
-`peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
234
-
-`modules.transport`: An array that must include at least 1 transport, such as `libp2p-tcp`.
235
-
236
-
#### `libp2p.start(callback)`
237
-
238
-
> Start the libp2p Node.
239
-
240
-
`callback` following signature `function (err) {}`, where `err` is an Error in case starting the node fails.
241
-
242
-
#### `libp2p.stop(callback)`
243
-
244
-
> Stop the libp2p Node.
245
-
246
-
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
247
-
248
-
#### `libp2p.dial(peer, callback)`
249
-
250
-
> Dials to another peer in the network, establishes the connection.
251
-
252
-
-`peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
253
-
-`callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
> Dials to another peer in the network and selects a protocol to talk with that peer.
258
-
259
-
-`peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
260
-
-`protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
261
-
-`callback`: Function with signature `function (err, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
262
-
263
-
`callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
264
-
265
-
#### `libp2p.dialFSM(peer, protocol, callback)`
266
-
267
-
> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
268
-
269
-
-`peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
270
-
-`protocol`: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
271
-
-`callback`: following signature `function (err, connFSM) {}`, where `connFSM` is a [Connection State Machine](https://github.com/libp2p/js-libp2p-switch#connection-state-machine)
272
-
273
-
#### `libp2p.hangUp(peer, callback)`
274
-
275
-
> Closes an open connection with a peer, graciously.
276
-
277
-
-`peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
278
-
279
-
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
-`protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
305
-
-`handlerFunc`: following signature `function (protocol, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
306
-
-`matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.
307
-
308
-
#### `libp2p.unhandle(protocol)`
309
-
310
-
> Stop handling protocol
311
-
312
-
-`protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
313
-
314
-
#### Events
315
-
316
-
##### `libp2p.on('start', () => {})`
317
-
318
-
> Libp2p has started, along with all its services.
319
-
320
-
##### `libp2p.on('stop', () => {})`
321
-
322
-
> Libp2p has stopped, along with all its services.
323
-
324
-
##### `libp2p.on('error', (err) => {})`
325
-
326
-
> An error has occurred
327
-
328
-
-`err`: instance of `Error`
329
-
330
-
##### `libp2p.on('peer:discovery', (peer) => {})`
331
-
332
-
> Peer has been discovered.
333
-
334
-
If `autoDial` is `true`, applications should **not** attempt to connect to the peer
335
-
unless they are performing a specific action. See [peer discovery and auto dial](./doc/PEER_DISCOVERY.md) for more information.
> Same API as IPFS PubSub, defined in the [CORE API Spec](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md). Just replace `ipfs` by `libp2p` and you are golden.
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` milliseconds).
510
-
511
-
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
512
-
513
-
#### Stats update interval
514
-
515
-
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout`, defined in milliseconds.
516
-
517
-
### Private Networks
518
-
519
-
#### Enforcement
520
-
521
-
Libp2p provides support for connection protection, such as for private networks. You can enforce network protection by setting the environment variable `LIBP2P_FORCE_PNET=1`. When this variable is on, if no protector is set via `options.connProtector`, Libp2p will throw an error upon creation.
Copy file name to clipboardExpand all lines: doc/API.md
+39Lines changed: 39 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,12 @@ Creates an instance of Libp2p.
34
34
| Name | Type | Description |
35
35
|------|------|-------------|
36
36
| options |`Object`| libp2p options |
37
+
| options.modules |`Array<Object>`| libp2p modules to use |
38
+
|[options.config]|`Object`| libp2p modules configuration and core configuration |
39
+
|[options.datastore]|`Object`| must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
40
+
|[options.peerInfo]|[PeerInfo](https://github.com/libp2p/js-peer-info)| peerInfo instance (it will be created if not provided) |
41
+
42
+
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
37
43
38
44
#### Returns
39
45
@@ -77,6 +83,39 @@ Required keys in the `options` object:
77
83
78
84
</details>
79
85
86
+
Once you have a libp2p instance, you are able to listen to several events it emmits, so that you can be noticed of relevant network events.
87
+
88
+
<details><summary>Events</summary>
89
+
90
+
#### An error has occurred
91
+
92
+
`libp2p.on('error', (err) => {})`
93
+
94
+
-`err`: instance of `Error`
95
+
96
+
#### A peer has been discovered
97
+
98
+
`libp2p.on('peer:discovery', (peer) => {})`
99
+
100
+
If `autoDial` option is `true`, applications should **not** attempt to connect to the peer
101
+
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
102
+
103
+
-`peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
104
+
105
+
#### We have a new connection to a peer
106
+
107
+
`libp2p.on('peer:connect', (peer) => {})`
108
+
109
+
-`peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
110
+
111
+
#### We have closed a connection to a peer
112
+
113
+
`libp2p.on('peer:disconnect', (peer) => {})`
114
+
115
+
-`peer`: instance of [PeerInfo][https://github.com/libp2p/js-peer-info]
0 commit comments