mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 13:21:34 +00:00
Compare commits
68 Commits
Author | SHA1 | Date | |
---|---|---|---|
3bde9c8bed | |||
14e12ee1f1 | |||
2374929990 | |||
26de739bb1 | |||
0f8d6afd8f | |||
daa26859e0 | |||
fdfb7b4e86 | |||
15bdb795a4 | |||
7d78728f54 | |||
53ed3bdb99 | |||
ae513887f5 | |||
7c78faa171 | |||
7d12eb9e26 | |||
581a1de472 | |||
288ac17954 | |||
2e4459b315 | |||
2a5232b541 | |||
44915b3723 | |||
64bba57255 | |||
88ebd1fc09 | |||
92cd591da4 | |||
320d84f541 | |||
970deec2a4 | |||
714b6ec2b9 | |||
f71fdfdf35 | |||
e92053da9a | |||
17b5f73b3d | |||
c18d2a4147 | |||
f1baa7e0b1 | |||
4abc868ab3 | |||
40e840d5fd | |||
9518eb44b3 | |||
0b75f99d75 | |||
686379efb0 | |||
a95389a28e | |||
3226632d83 | |||
dd934b9690 | |||
cef3c8b5cc | |||
eedb20e9a3 | |||
e51260434c | |||
7e6c9eeb38 | |||
c537140fbc | |||
3b7c4b5eb0 | |||
4460e8246c | |||
a63432e24b | |||
69f7264123 | |||
e052021397 | |||
fdd714ee60 | |||
65e7223ce0 | |||
eddec7d2e4 | |||
7b6c921d36 | |||
05c16e4262 | |||
c8a86db310 | |||
ce29902691 | |||
0b729621db | |||
e6e5b872dc | |||
550af3cbde | |||
5aa9ebbbe8 | |||
7f68a13433 | |||
2b7cc55c88 | |||
40739e9639 | |||
6106915923 | |||
d9059dbad9 | |||
187d584086 | |||
7502ba86a5 | |||
cc51fa59f9 | |||
1c10842bd3 | |||
c07ffa1317 |
81
CHANGELOG.md
81
CHANGELOG.md
@ -1,3 +1,84 @@
|
||||
<a name="0.24.4"></a>
|
||||
## [0.24.4](https://github.com/libp2p/js-libp2p/compare/v0.24.3...v0.24.4) (2019-01-04)
|
||||
|
||||
|
||||
|
||||
<a name="0.24.3"></a>
|
||||
## [0.24.3](https://github.com/libp2p/js-libp2p/compare/v0.24.2...v0.24.3) (2018-12-14)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* not started yet ([#297](https://github.com/libp2p/js-libp2p/issues/297)) ([fdfb7b4](https://github.com/libp2p/js-libp2p/commit/fdfb7b4))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.2"></a>
|
||||
## [0.24.2](https://github.com/libp2p/js-libp2p/compare/v0.24.1...v0.24.2) (2018-12-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* use symbol instead of constructor name ([#292](https://github.com/libp2p/js-libp2p/issues/292)) ([53ed3bd](https://github.com/libp2p/js-libp2p/commit/53ed3bd))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.1"></a>
|
||||
## [0.24.1](https://github.com/libp2p/js-libp2p/compare/v0.24.0...v0.24.1) (2018-12-03)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* allow configurable validators and selectors to the dht ([#288](https://github.com/libp2p/js-libp2p/issues/288)) ([7d12eb9](https://github.com/libp2p/js-libp2p/commit/7d12eb9))
|
||||
|
||||
|
||||
|
||||
<a name="0.24.0"></a>
|
||||
# [0.24.0](https://github.com/libp2p/js-libp2p/compare/v0.24.0-rc.3...v0.24.0) (2018-11-16)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add maxtimeout to dht get ([#248](https://github.com/libp2p/js-libp2p/issues/248)) ([69f7264](https://github.com/libp2p/js-libp2p/commit/69f7264))
|
||||
* dht get options ([4460e82](https://github.com/libp2p/js-libp2p/commit/4460e82))
|
||||
* dont call callback before it's properly set ([17b5f73](https://github.com/libp2p/js-libp2p/commit/17b5f73))
|
||||
* improve get peer info errors ([714b6ec](https://github.com/libp2p/js-libp2p/commit/714b6ec))
|
||||
* start kad dht random walk ([#251](https://github.com/libp2p/js-libp2p/issues/251)) ([dd934b9](https://github.com/libp2p/js-libp2p/commit/dd934b9))
|
||||
|
||||
### Features
|
||||
|
||||
* add datastore to config ([40e840d](https://github.com/libp2p/js-libp2p/commit/40e840d))
|
||||
* add delegated peer and content routing support ([#242](https://github.com/libp2p/js-libp2p/issues/242)) ([a95389a](https://github.com/libp2p/js-libp2p/commit/a95389a))
|
||||
* add maxNumProviders to findprovs ([#283](https://github.com/libp2p/js-libp2p/issues/283)) ([970deec](https://github.com/libp2p/js-libp2p/commit/970deec))
|
||||
* conditionally emit errors ([f71fdfd](https://github.com/libp2p/js-libp2p/commit/f71fdfd))
|
||||
* enable relay by default (no hop) ([#254](https://github.com/libp2p/js-libp2p/issues/254)) ([686379e](https://github.com/libp2p/js-libp2p/commit/686379e))
|
||||
* make libp2p a state machine ([#257](https://github.com/libp2p/js-libp2p/issues/257)) ([0b75f99](https://github.com/libp2p/js-libp2p/commit/0b75f99))
|
||||
* use package-table vs custom script ([a63432e](https://github.com/libp2p/js-libp2p/commit/a63432e))
|
||||
|
||||
<a name="0.23.1"></a>
|
||||
## [0.23.1](https://github.com/libp2p/js-libp2p/compare/v0.23.0...v0.23.1) (2018-08-13)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* callback with error for invalid or non-peer multiaddr ([#232](https://github.com/libp2p/js-libp2p/issues/232)) ([c8a86db](https://github.com/libp2p/js-libp2p/commit/c8a86db))
|
||||
|
||||
|
||||
|
||||
<a name="0.23.0"></a>
|
||||
# [0.23.0](https://github.com/libp2p/js-libp2p/compare/v0.22.0...v0.23.0) (2018-07-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* start and stop connection manager with libp2p ([6106915](https://github.com/libp2p/js-libp2p/commit/6106915))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add check for protector and enforced pnet ([2b7cc55](https://github.com/libp2p/js-libp2p/commit/2b7cc55))
|
||||
|
||||
|
||||
|
||||
<a name="0.22.0"></a>
|
||||
# [0.22.0](https://github.com/libp2p/js-libp2p/compare/v0.21.0...v0.22.0) (2018-06-29)
|
||||
|
||||
|
36
MGMT.md
36
MGMT.md
@ -1,36 +0,0 @@
|
||||
# Core Dev Team Work Tracking & Managment
|
||||
|
||||
## How work gets organized (a tl;dr;)
|
||||
|
||||
The js-ipfs core working group follows the OKR structure established for the IPFS project to set the quarterly targets. Within each quarter, work gets tracked using Github and Waffle.
|
||||
|
||||
- Github is used for discussions and track current endeavours.
|
||||
- Waffle gives us a [Kanban](https://en.wikipedia.org/wiki/Kanban) view over the work at hand.
|
||||
|
||||

|
||||
|
||||
In the Waffle board, we have 4 columns:
|
||||
|
||||
- **Inbox** - New issues or PRs that haven't been evaluated yet
|
||||
- **Backlog** - Issues that are blocked or discussion threads that are not currently active
|
||||
- **Ready** - Issues Ready to be worked on
|
||||
- **In Progress** - Issues that someone is already tackling. Contributors should focus on a few things rather than many at once.
|
||||
- **Done** - Issues are automatically moved here when the issue is closed or the PR merged.
|
||||
|
||||
We track work for the JavaScript implementation of the IPFS protocol in 3 separate waffle boards:
|
||||
|
||||
- [js-ipfs](http://waffle.io/ipfs/js-ipfs)
|
||||
- [js-libp2p](http://waffle.io/libp2p/js-libp2p)
|
||||
- [js-ipld](http://waffle.io/ipld/js-ipld)
|
||||
|
||||
## Issue labels and how to use filters
|
||||
|
||||
We use labels to tag urgency and the difficulty of an issue. The current label system has:
|
||||
|
||||
- `difficulty:{easy, moderate, hard}` - This is an instinctive measure give by the project lead or leads. It is a subjective best guess, however the current golden rule is that an issue with difficulty:easy should not require more than a morning (3~4 hours) to do and it should not require having to mess with multiple modules to complete. Issues with difficulty moderate or hard might require some discussion around the problem or even request that another team (i.e go-ipfs) makes some changes. The length of moderate or hard issue might be a day to ad-aeternum.
|
||||
- `priority (P0, P1, P2, P3, P4)` - P0 is the most important while P4 is the least.
|
||||
- `help wanted` - Issues perfect for new contributors. They will have the information necessary or the pointers for a new contributor to figure out what is required. These issues are never blocked on some other issue be done first.
|
||||
|
||||
## Weekly Core Dev Team Calls
|
||||
|
||||
[⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽](https://github.com/ipfs/pm/issues/650)
|
19
OKR.md
Normal file
19
OKR.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Quarterly Objectives and Key Results
|
||||
|
||||
We try to frame our ongoing work using a process based on quarterly Objectives and Key Results (OKRs). Objectives reflect outcomes that are challenging, but realistic. Results are tangible and measurable.
|
||||
|
||||
## 2019 Q1
|
||||
|
||||
Find the js-libp2p OKRs for 2019 Q1 at the [2019 Q1 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/11GKG1DBRIIAiQnHvLD7_IqWxDGsVdaZFpxJM6NWtXe8/edit#gid=1271182838)
|
||||
|
||||
## 2018 Q4
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q4 at the [2018 Q4 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1BYwmbVicgo6_tOHAbgiUXWge8Ej0qR1M_gAUulazmrg/edit#gid=1241853194)
|
||||
|
||||
## 2018 Q3
|
||||
|
||||
Find the js-libp2p OKRs for 2018 Q3 at the [2018 Q3 libp2p OKRs Spreadsheet](https://docs.google.com/spreadsheets/d/1HTXfgR5FyPTFhsTkFPRThkeMvHvCgJOaAs7BSl_vQ_0/edit#gid=1241853194)
|
||||
|
||||
## Previous Quarters
|
||||
|
||||
For the quarters before 2018 Q3, js-libp2p shared their KRs with the [IPFS OKRs](https://github.com/ipfs/js-ipfs/blob/master/OKR.md).
|
231
README.md
231
README.md
@ -1,5 +1,5 @@
|
||||
<h1 align="center">
|
||||
<a href="libp2p.io"><img width="250" src="https://github.com/libp2p/libp2p/blob/master/logo/alternates/libp2p-logo-alt-2.png?raw=true" alt="libp2p hex logo" /></a>
|
||||
<a href="libp2p.io"><img width="250" src="https://github.com/libp2p/libp2p/blob/master/logo/black-bg-2.png?raw=true" alt="libp2p hex logo" /></a>
|
||||
</h1>
|
||||
|
||||
<h3 align="center">The JavaScript implementation of the libp2p Networking Stack.</h3>
|
||||
@ -12,8 +12,8 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://circleci.com/gh/libp2p/js-libp2p"><img src="https://circleci.com/gh/libp2p/js-libp2p.svg?style=svg" /></a>
|
||||
<a href="https://coveralls.io/github/libp2p/js-libp2p?branch=master"><img src="https://coveralls.io/repos/github/libp2p/js-libp2p/badge.svg?branch=master"></a>
|
||||
<a href="https://ci.ipfs.team/job/libp2p/job/js-libp2p/job/master/"><img src="https://ci.ipfs.team/buildStatus/icon?job=libp2p/js-libp2p/master" /></a>
|
||||
<a href="https://codecov.io/gh/libp2p/js-libp2p"><img src="https://codecov.io/gh/libp2p/js-libp2p/branch/master/graph/badge.svg"></a>
|
||||
<br>
|
||||
<a href="https://david-dm.org/libp2p/js-libp2p"><img src="https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square" /></a>
|
||||
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
|
||||
@ -39,7 +39,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
|
||||
|
||||
## Lead Maintainer
|
||||
|
||||
[David Dias](https://github.com/diasdavid/)
|
||||
[Jacob Heun](https://github.com/jacobheun/)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@ -49,6 +49,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
|
||||
- [Install](#install)
|
||||
- [Usage](#usage)
|
||||
- [API](#api)
|
||||
- [Events](#events)
|
||||
- [Development](#development)
|
||||
- [Tests](#tests)
|
||||
- [Packages](#packages)
|
||||
@ -93,9 +94,9 @@ npm install --save libp2p
|
||||
|
||||
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios.
|
||||
|
||||
### Extending libp2p skeleton
|
||||
### Creating your own libp2p bundle
|
||||
|
||||
libp2p becomes very simple and basically acts as a glue for every module that compose this library. Since it can be highly customized, it requires some setup. What we recommend is to have a libp2p build for the system you are developing taking into account in your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built and minified version that browsers can require).
|
||||
The libp2p module acts as a glue for every libp2p module that you can use top create your own libp2p bundle. Creating your own libp2p bundle gives you a lot of freedom when it comes to customize it with features and default setup. We recommend creating your own libp2p bundle for the app you are developing that takes in account your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built one that leverages the Browser transports).
|
||||
|
||||
**Example:**
|
||||
|
||||
@ -115,13 +116,14 @@ const SECIO = require('libp2p-secio')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const Protector = require('libp2p-pnet')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_peerInfo, _peerBook, _options) {
|
||||
constructor (_options) {
|
||||
const peerInfo = _options.peerInfo
|
||||
const defaults = {
|
||||
peerInfo: _peerInfo // The Identity of your Peer
|
||||
peerBook: _peerBook, // Where peers get tracked, if undefined libp2p will create one instance
|
||||
|
||||
// The libp2p modules for this libp2p bundle
|
||||
modules: {
|
||||
transport: [
|
||||
@ -134,12 +136,20 @@ class Node extends libp2p {
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
]
|
||||
],
|
||||
/** Encryption for private networks. Needs additional private key to work **/
|
||||
// connProtector: new Protector(/*protector specific opts*/),
|
||||
/** Enable custom content routers, such as delegated routing **/
|
||||
// contentRouting: [
|
||||
// new DelegatedContentRouter(peerInfo.id)
|
||||
// ],
|
||||
/** Enable custom peer routers, such as delegated routing **/
|
||||
// peerRouting: [
|
||||
// new DelegatedPeerRouter()
|
||||
// ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS
|
||||
],
|
||||
peerRouting: {}, // Currently both peerRouting and contentRouting are patched through the DHT,
|
||||
contentRouting: {} // this will change once we factor that into two modules, for now do the following line:
|
||||
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components
|
||||
},
|
||||
|
||||
@ -147,30 +157,32 @@ class Node extends libp2p {
|
||||
config: { // The config object is the part of the config that can go into a file, config.json.
|
||||
peerDiscovery: {
|
||||
mdns: { // mdns options
|
||||
interval: 1000 // ms
|
||||
interval: 1000, // ms
|
||||
enabled: true
|
||||
},
|
||||
webrtcStar: { // webrtc-star options
|
||||
interval: 1000 // ms
|
||||
interval: 1000, // ms
|
||||
enabled: false
|
||||
}
|
||||
// .. other discovery module options.
|
||||
},
|
||||
peerRouting: {},
|
||||
contentRouting: {},
|
||||
relay: { // Circuit Relay options
|
||||
enabled: false,
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabledDiscovery: true // Allows to disable discovery (enabled by default)
|
||||
},
|
||||
// Enable/Disable Experimental features
|
||||
EXPERIMENTAL: { // Experimental features ("behind a flag")
|
||||
pubsub: false,
|
||||
dht: false
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep
|
||||
@ -183,34 +195,32 @@ class Node extends libp2p {
|
||||
|
||||
### API
|
||||
|
||||
#### Create a Node - `new libp2p.Node([peerInfo, peerBook, options])`
|
||||
#### Create a Node - `new libp2p.Node(options)`
|
||||
|
||||
> Creates an instance of the libp2p.Node.
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node. Optional.
|
||||
- `peerBook`: instance of [PeerBook][] that contains the [PeerInfo][] of known peers. Optional.
|
||||
- `options`: Object containing custom options for the bundle.
|
||||
Required keys in the `options` object:
|
||||
|
||||
- `peerInfo`: instance of [PeerInfo][] that contains the [PeerId][], Keys and [multiaddrs][multiaddr] of the libp2p Node.
|
||||
|
||||
#### `libp2p.start(callback)`
|
||||
|
||||
> Start the libp2p Node.
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case starting the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case starting the node fails.
|
||||
|
||||
#### `libp2p.stop(callback)`
|
||||
|
||||
> Stop the libp2p Node.
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.dial(peer, callback)`
|
||||
|
||||
> Dials to another peer in the network, establishes the connection.
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
|
||||
`callback` is a function with the following `function (err, conn) {}` signature, 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.
|
||||
- `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.
|
||||
|
||||
#### `libp2p.dialProtocol(peer, protocol, callback)`
|
||||
|
||||
@ -218,9 +228,17 @@ class Node extends libp2p {
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `callback`: Function with signature `function (err, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
|
||||
`callback` is a function with the following `function (err, conn) {}` signature, 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.
|
||||
`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.
|
||||
|
||||
#### `libp2p.dialFSM(peer, protocol, callback)`
|
||||
|
||||
> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||
- `protocol`: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `callback`: following signature `function (err, connFSM) {}`, where `connFSM` is a [Connection State Machine](https://github.com/libp2p/js-libp2p-switch#connection-state-machine)
|
||||
|
||||
#### `libp2p.hangUp(peer, callback)`
|
||||
|
||||
@ -228,30 +246,33 @@ class Node extends libp2p {
|
||||
|
||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
||||
|
||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
||||
`callback` following signature `function (err) {}`, where `err` is an Error in case stopping the node fails.
|
||||
|
||||
#### `libp2p.peerRouting.findPeer(id, callback)`
|
||||
#### `libp2p.peerRouting.findPeer(id, options, callback)`
|
||||
|
||||
> Looks up for multiaddrs of a peer in the DHT
|
||||
|
||||
- `id`: instance of [PeerId][]
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.contentRouting.findProviders(key, timeout, callback)`
|
||||
#### `libp2p.contentRouting.findProviders(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `timeout`: Number miliseconds
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
- `options.maxNumProviders` maximum number of providers to find
|
||||
|
||||
#### `libp2p.contentRouting.provide(key, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
|
||||
|
||||
#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`
|
||||
|
||||
> Handle new protocol
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
- `handlerFunc`: Function with signature `function (protocol, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `handlerFunc`: following signature `function (protocol, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.
|
||||
|
||||
#### `libp2p.unhandle(protocol)`
|
||||
@ -260,19 +281,35 @@ class Node extends libp2p {
|
||||
|
||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||
|
||||
#### `libp2p.on('peer:discovery', (peer) => {})`
|
||||
#### Events
|
||||
|
||||
##### `libp2p.on('start', () => {})`
|
||||
|
||||
> Libp2p has started, along with all its services.
|
||||
|
||||
##### `libp2p.on('stop', () => {})`
|
||||
|
||||
> Libp2p has stopped, along with all its services.
|
||||
|
||||
##### `libp2p.on('error', (err) => {})`
|
||||
|
||||
> An error has occurred
|
||||
|
||||
- `err`: instance of `Error`
|
||||
|
||||
##### `libp2p.on('peer:discovery', (peer) => {})`
|
||||
|
||||
> Peer has been discovered.
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
#### `libp2p.on('peer:connect', (peer) => {})`
|
||||
##### `libp2p.on('peer:connect', (peer) => {})`
|
||||
|
||||
> We connected to a new peer
|
||||
|
||||
- `peer`: instance of [PeerInfo][]
|
||||
|
||||
#### `libp2p.on('peer:disconnect', (peer) => {})`
|
||||
##### `libp2p.on('peer:disconnect', (peer) => {})`
|
||||
|
||||
> We disconnected from Peer
|
||||
|
||||
@ -307,14 +344,18 @@ class Node extends libp2p {
|
||||
- `key`: Buffer
|
||||
- `value`: Buffer
|
||||
|
||||
#### `libp2p.dht.get(key, callback)`
|
||||
#### `libp2p.dht.get(key, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
#### `libp2p.dht.getMany(key, nVals, callback)`
|
||||
#### `libp2p.dht.getMany(key, nVals, options, callback)`
|
||||
|
||||
- `key`: Buffer
|
||||
- `nVals`: Number
|
||||
- `options`: object of options
|
||||
- `options.maxTimeout`: Number milliseconds
|
||||
|
||||
[PeerInfo]: https://github.com/libp2p/js-peer-info
|
||||
[PeerId]: https://github.com/libp2p/js-peer-id
|
||||
@ -428,6 +469,16 @@ Each one of these values is [an exponential moving-average instance](https://git
|
||||
|
||||
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 miliseconds.
|
||||
|
||||
### Private Networks
|
||||
|
||||
#### Enforcement
|
||||
|
||||
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.
|
||||
|
||||
#### Protectors
|
||||
|
||||
Some available network protectors:
|
||||
* [libp2p-pnet](https://github.com/libp2p/js-libp2p-pnet)
|
||||
|
||||
## Development
|
||||
|
||||
@ -470,39 +521,71 @@ N/A
|
||||
|
||||
List of packages currently in existence for libp2p
|
||||
|
||||
| Package | Version | Dependencies | DevDependencies |
|
||||
|---------|---------|--------------|-----------------|
|
||||
| **Transports** |
|
||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://david-dm.org/libp2p/js-libp2p-utp?type=dev) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://david-dm.org/libp2p/js-libp2p-websockets?type=dev) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star?type=dev) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star?type=dev) |
|
||||
| [`libp2p-websocket-star-rendezvous`](//github.com/libp2p/js-libp2p-websocket-star-rendezvous) | [](//github.com/libp2p/js-libp2p-websocket-star-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous?type=dev) |
|
||||
| **Connection Upgrades** |
|
||||
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://david-dm.org/libp2p/interface-connection?type=dev) |
|
||||
| **Stream Muxers** |
|
||||
| [`interface-stream-muxer`](//github.com/libp2p/interface-stream-muxer) | [](//github.com/libp2p/interface-stream-muxer/releases) | [](https://david-dm.org/libp2p/interface-stream-muxer) | [](https://david-dm.org/libp2p/interface-stream-muxer?type=dev) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://david-dm.org/libp2p/js-libp2p-spdy?type=dev) |
|
||||
| [`libp2p-multiplex`](https://github.com/libp2p/js-libp2p-multiplex)
|
||||
| **Discovery** |
|
||||
| [`libp2p-mdns-discovery`](//github.com/libp2p/js-libp2p-mdns-discovery) | [](//github.com/libp2p/js-libp2p-mdns-discovery/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns-discovery) | [](https://david-dm.org/libp2p/js-libp2p-mdns-discovery?type=dev) |
|
||||
| [`libp2p-railing`](//github.com/libp2p/js-libp2p-railing) | [](//github.com/libp2p/js-libp2p-railing/releases) | [](https://david-dm.org/libp2p/js-libp2p-railing) | [](https://david-dm.org/libp2p/js-libp2p-railing?type=dev) |
|
||||
> This table is generated using the module `package-table` with `package-table --data=package-list.json`.
|
||||
|
||||
| Package | Version | Deps | CI | Coverage | Lead Maintainer |
|
||||
| ---------|---------|---------|---------|---------|--------- |
|
||||
| **Libp2p** |
|
||||
| [`interface-libp2p`](//github.com/libp2p/interface-libp2p) | [](//github.com/libp2p/interface-libp2p/releases) | [](https://david-dm.org/libp2p/interface-libp2p) | N/A | [](https://codecov.io/gh/libp2p/interface-libp2p) | N/A |
|
||||
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//github.com/libp2p/js-libp2p/releases) | [](https://david-dm.org/libp2p/js-libp2p) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Connection** |
|
||||
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | N/A | [](https://codecov.io/gh/libp2p/interface-connection) | N/A |
|
||||
| **Transport** |
|
||||
| [`interface-transport`](//github.com/libp2p/interface-transport) | [](//github.com/libp2p/interface-transport/releases) | [](https://david-dm.org/libp2p/interface-transport) | N/A | [](https://codecov.io/gh/libp2p/interface-transport) | N/A |
|
||||
| [`libp2p-tcp`](//github.com/libp2p/js-libp2p-tcp) | [](//github.com/libp2p/js-libp2p-tcp/releases) | [](https://david-dm.org/libp2p/js-libp2p-tcp) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-tcp/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-tcp) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-udp`](//github.com/libp2p/js-libp2p-udp) | [](//github.com/libp2p/js-libp2p-udp/releases) | [](https://david-dm.org/libp2p/js-libp2p-udp) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-udp) | N/A |
|
||||
| [`libp2p-udt`](//github.com/libp2p/js-libp2p-udt) | [](//github.com/libp2p/js-libp2p-udt/releases) | [](https://david-dm.org/libp2p/js-libp2p-udt) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-udt) | N/A |
|
||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-utp/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-utp) | N/A |
|
||||
| [`libp2p-webrtc-direct`](//github.com/libp2p/js-libp2p-webrtc-direct) | [](//github.com/libp2p/js-libp2p-webrtc-direct/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-direct) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-direct/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-direct) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-websockets) | N/A |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-websocket-star-rendezvous`](//github.com/libp2p/js-libp2p-websocket-star-rendezvous) | [](//github.com/libp2p/js-libp2p-websocket-star-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star-rendezvous) | N/A |
|
||||
| **Crypto Channels** |
|
||||
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | [](https://david-dm.org/libp2p/js-libp2p-secio?type=dev) |
|
||||
| **Peer Routing** |
|
||||
| [`libp2p-kad-routing`](//github.com/libp2p/js-libp2p-kad-routing) | [](//github.com/libp2p/js-libp2p-kad-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-routing) | [](https://david-dm.org/libp2p/js-libp2p-kad-routing?type=dev) |
|
||||
| **Content Routing** |
|
||||
| [`interface-record-store`](//github.com/libp2p/interface-record-store) | [](//github.com/libp2p/interface-record-store/releases) | [](https://david-dm.org/libp2p/interface-record-store) | [](https://david-dm.org/libp2p/interface-record-store?type=dev) |
|
||||
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [](//github.com/libp2p/js-libp2p-record/releases) | [](https://david-dm.org/libp2p/js-libp2p-record) | [](https://david-dm.org/libp2p/js-libp2p-record?type=dev) |
|
||||
| [`libp2p-distributed-record-store`](//github.com/libp2p/js-libp2p-distributed-record-store) | [](//github.com/libp2p/js-libp2p-distributed-record-store/releases) | [](https://david-dm.org/libp2p/js-libp2p-distributed-record-store) | [](https://david-dm.org/libp2p/js-libp2p-distributed-record-store?type=dev) |
|
||||
| [`libp2p-kad-record-store`](//github.com/libp2p/js-libp2p-kad-record-store) | [](//github.com/libp2p/js-libp2p-kad-record-store/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-record-store) | [](https://david-dm.org/libp2p/js-libp2p-kad-record-store?type=dev) |
|
||||
| **Generics** |
|
||||
| [`libp2p-switch`](//github.com/libp2p/js-libp2p-switch) | [](//github.com/libp2p/js-libp2p-switch/releases) | [](https://david-dm.org/libp2p/js-libp2p-switch) | [](https://david-dm.org/libp2p/js-libp2p-switch?type=dev) |
|
||||
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [](//github.com/libp2p/js-libp2p-ping/releases) | [](https://david-dm.org/libp2p/js-libp2p-ping) | [](https://david-dm.org/libp2p/js-libp2p-ping?type=dev) |
|
||||
| [`multistream-select`](//github.com/libp2p/js-multistream) | [](//github.com/libp2p/js-multistream/releases) | [](https://david-dm.org/libp2p/js-multistream) | [](https://david-dm.org/libp2p/js-multistream?type=dev) |
|
||||
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [](//github.com/libp2p/js-libp2p-secio/releases) | [](https://david-dm.org/libp2p/js-libp2p-secio) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-secio) | N/A |
|
||||
| **Stream Muxers** |
|
||||
| [`interface-stream-muxer`](//github.com/libp2p/interface-stream-muxer) | [](//github.com/libp2p/interface-stream-muxer/releases) | [](https://david-dm.org/libp2p/interface-stream-muxer) | N/A | [](https://codecov.io/gh/libp2p/interface-stream-muxer) | N/A |
|
||||
| [`libp2p-mplex`](//github.com/libp2p/js-libp2p-mplex) | [](//github.com/libp2p/js-libp2p-mplex/releases) | [](https://david-dm.org/libp2p/js-libp2p-mplex) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-mplex) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [](//github.com/libp2p/js-libp2p-spdy/releases) | [](https://david-dm.org/libp2p/js-libp2p-spdy) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-spdy/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-spdy) | N/A |
|
||||
| **Discovery** |
|
||||
| [`interface-peer-discovery`](//github.com/libp2p/interface-peer-discovery) | [](//github.com/libp2p/interface-peer-discovery/releases) | [](https://david-dm.org/libp2p/interface-peer-discovery) | N/A | [](https://codecov.io/gh/libp2p/interface-peer-discovery) | N/A |
|
||||
| [`libp2p-bootstrap`](//github.com/libp2p/js-libp2p-bootstrap) | [](//github.com/libp2p/js-libp2p-bootstrap/releases) | [](https://david-dm.org/libp2p/js-libp2p-bootstrap) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-bootstrap/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-bootstrap) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [](//github.com/libp2p/js-libp2p-mdns/releases) | [](https://david-dm.org/libp2p/js-libp2p-mdns) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-mdns/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-mdns) | N/A |
|
||||
| [`libp2p-rendezvous`](//github.com/libp2p/js-libp2p-rendezvous) | [](//github.com/libp2p/js-libp2p-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-rendezvous) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-rendezvous) | N/A |
|
||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-webrtc-star/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-websocket-star) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **NAT Traversal** |
|
||||
| [`libp2p-circuit`](//github.com/libp2p/js-libp2p-circuit) | [](//github.com/libp2p/js-libp2p-circuit/releases) | [](https://david-dm.org/libp2p/js-libp2p-circuit) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-circuit/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-circuit) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-nat-mngr`](//github.com/libp2p/js-libp2p-nat-mngr) | [](//github.com/libp2p/js-libp2p-nat-mngr/releases) | [](https://david-dm.org/libp2p/js-libp2p-nat-mngr) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-nat-mngr) | N/A |
|
||||
| **Data Types** |
|
||||
| [`peer-book`](//github.com/libp2p/js-peer-book) | [](//github.com/libp2p/js-peer-book/releases) | [](https://david-dm.org/libp2p/js-peer-book) | [](https://david-dm.org/libp2p/js-peer-book?type=dev) |
|
||||
| [`peer-id`](https://github.com/libp2p/js-peer-id)
|
||||
| [`peer-book`](//github.com/libp2p/js-peer-book) | [](//github.com/libp2p/js-peer-book/releases) | [](https://david-dm.org/libp2p/js-peer-book) | N/A | [](https://codecov.io/gh/libp2p/js-peer-book) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`peer-id`](//github.com/libp2p/js-peer-id) | [](//github.com/libp2p/js-peer-id/releases) | [](https://david-dm.org/libp2p/js-peer-id) | [](https://ci.ipfs.team/job/libp2p/job/js-peer-id/job/master/) | [](https://codecov.io/gh/libp2p/js-peer-id) | [Pedro Teixeira](mailto:i@pgte.me) |
|
||||
| [`peer-info`](//github.com/libp2p/js-peer-info) | [](//github.com/libp2p/js-peer-info/releases) | [](https://david-dm.org/libp2p/js-peer-info) | [](https://ci.ipfs.team/job/libp2p/job/js-peer-info/job/master/) | [](https://codecov.io/gh/libp2p/js-peer-info) | N/A |
|
||||
| **Content Routing** |
|
||||
| [`interface-content-routing`](//github.com/libp2p/interface-content-routing) | [](//github.com/libp2p/interface-content-routing/releases) | [](https://david-dm.org/libp2p/interface-content-routing) | N/A | [](https://codecov.io/gh/libp2p/interface-content-routing) | N/A |
|
||||
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **Peer Routing** |
|
||||
| [`interface-peer-routing`](//github.com/libp2p/interface-peer-routing) | [](//github.com/libp2p/interface-peer-routing/releases) | [](https://david-dm.org/libp2p/interface-peer-routing) | N/A | [](https://codecov.io/gh/libp2p/interface-peer-routing) | N/A |
|
||||
| [`libp2p-delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/releases) | [](https://david-dm.org/libp2p/js-libp2p-delegated-peer-routing) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [](//github.com/libp2p/js-libp2p-kad-dht/releases) | [](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-kad-dht/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| **Record Store** |
|
||||
| [`interface-record-store`](//github.com/libp2p/interface-record-store) | [](//github.com/libp2p/interface-record-store/releases) | [](https://david-dm.org/libp2p/interface-record-store) | N/A | [](https://codecov.io/gh/libp2p/interface-record-store) | N/A |
|
||||
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [](//github.com/libp2p/js-libp2p-record/releases) | [](https://david-dm.org/libp2p/js-libp2p-record) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-record/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-record) | N/A |
|
||||
| **Generics** |
|
||||
| [`libp2p-connection-manager`](//github.com/libp2p/js-libp2p-connection-manager) | [](//github.com/libp2p/js-libp2p-connection-manager/releases) | [](https://david-dm.org/libp2p/js-libp2p-connection-manager) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-connection-manager) | N/A |
|
||||
| [`libp2p-crypto`](//github.com/libp2p/js-libp2p-crypto) | [](//github.com/libp2p/js-libp2p-crypto/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-crypto/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto) | [Friedel Ziegelmayer](mailto:dignifiedquire@gmail.com) |
|
||||
| [`libp2p-crypto-secp256k1`](//github.com/libp2p/js-libp2p-crypto-secp256k1) | [](//github.com/libp2p/js-libp2p-crypto-secp256k1/releases) | [](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-crypto-secp256k1/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1) | N/A |
|
||||
| [`libp2p-switch`](//github.com/libp2p/js-libp2p-switch) | [](//github.com/libp2p/js-libp2p-switch/releases) | [](https://david-dm.org/libp2p/js-libp2p-switch) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-switch/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-switch) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Extensions** |
|
||||
| [`libp2p-floodsub`](//github.com/libp2p/js-libp2p-floodsub) | [](//github.com/libp2p/js-libp2p-floodsub/releases) | [](https://david-dm.org/libp2p/js-libp2p-floodsub) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-floodsub) | N/A |
|
||||
| [`libp2p-identify`](//github.com/libp2p/js-libp2p-identify) | [](//github.com/libp2p/js-libp2p-identify/releases) | [](https://david-dm.org/libp2p/js-libp2p-identify) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-identify) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| [`libp2p-keychain`](//github.com/libp2p/js-libp2p-keychain) | [](//github.com/libp2p/js-libp2p-keychain/releases) | [](https://david-dm.org/libp2p/js-libp2p-keychain) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-keychain) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
|
||||
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [](//github.com/libp2p/js-libp2p-ping/releases) | [](https://david-dm.org/libp2p/js-libp2p-ping) | [](https://ci.ipfs.team/job/libp2p/job/js-libp2p-ping/job/master/) | [](https://codecov.io/gh/libp2p/js-libp2p-ping) | N/A |
|
||||
| [`libp2p-pnet`](//github.com/libp2p/js-libp2p-pnet) | [](//github.com/libp2p/js-libp2p-pnet/releases) | [](https://david-dm.org/libp2p/js-libp2p-pnet) | N/A | [](https://codecov.io/gh/libp2p/js-libp2p-pnet) | [Jacob Heun](mailto:jacobheun@gmail.com) |
|
||||
| **Utilities** |
|
||||
| [`p2pcat`](//github.com/libp2p/js-p2pcat) | [](//github.com/libp2p/js-p2pcat/releases) | [](https://david-dm.org/libp2p/js-p2pcat) | N/A | [](https://codecov.io/gh/libp2p/js-p2pcat) | N/A |
|
||||
|
||||
## Contribute
|
||||
|
||||
@ -514,4 +597,4 @@ The libp2p implementation in JavaScript is a work in progress. As such, there ar
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © David Dias
|
||||
[MIT](LICENSE) © Protocol Labs
|
||||
|
41
RELEASE.md
Normal file
41
RELEASE.md
Normal file
@ -0,0 +1,41 @@
|
||||
# Release Template
|
||||
|
||||
> short tl;dr; of the release
|
||||
|
||||
# 🗺 What's left for release
|
||||
|
||||
# 🔦 Highlights
|
||||
|
||||
# 🏗 API Changes
|
||||
|
||||
# ✅ Release Checklist
|
||||
|
||||
- Robustness and quality
|
||||
- [ ] Ensure that all tests are passing, this includes:
|
||||
- [ ] unit
|
||||
- [ ] Run tests of the following projects with the new release:
|
||||
- [ ] [js-ipfs](https://github.com/ipfs/js-ipfs)
|
||||
- Documentation
|
||||
- [ ] Ensure that README.md is up to date
|
||||
- [ ] Ensure that all the examples run
|
||||
- Communication
|
||||
- [ ] Create the release issue
|
||||
- [ ] Announcements (both pre-release and post-release)
|
||||
- [ ] Twitter
|
||||
- [ ] IRC
|
||||
- [ ] Reddit
|
||||
- [ ] Blog post
|
||||
|
||||
# 🙌🏽 Want to contribute?
|
||||
|
||||
Would you like to contribute to the libp2p project and don't know how? Well, there are a few places you can get started:
|
||||
|
||||
- Check the issues with the `help wanted` label at the Ready column in our waffle board - https://waffle.io/libp2p/js-libp2p?label=help%20wanted
|
||||
- Join an IPFS All Hands, introduce yourself and let us know where you would like to contribute - https://github.com/ipfs/pm/#all-hands-call
|
||||
- Hack with IPFS and show us what you made! The All Hands call is also the perfect venue for demos, join in and show us what you built
|
||||
- Join the discussion at http://discuss.ipfs.io/ and help users finding their answers.
|
||||
- Join the [⚡️ⒿⓈ Core Dev Team Weekly Sync 🙌🏽 ](https://github.com/ipfs/pm/issues/650) and be part of the Sprint action!
|
||||
|
||||
# ⁉️ Do you have questions?
|
||||
|
||||
The best place to ask your questions about libp2p, how it works and what you can do with it is at [discuss.ipfs.io](http://discuss.ipfs.io). We are also available at the #libp2p channel on Freenode.
|
29
appveyor.yml
29
appveyor.yml
@ -1,29 +0,0 @@
|
||||
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
|
||||
version: "{build}"
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: "6"
|
||||
- nodejs_version: "8"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
|
||||
install:
|
||||
# Install Node.js
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
||||
# Upgrade npm
|
||||
- npm install -g npm
|
||||
|
||||
# Output our current versions for debugging
|
||||
- node --version
|
||||
- npm --version
|
||||
|
||||
# Install our package dependencies
|
||||
- npm install
|
||||
|
||||
test_script:
|
||||
- npm run test:node
|
||||
|
||||
build: off
|
22
circle.yml
22
circle.yml
@ -1,22 +0,0 @@
|
||||
machine:
|
||||
node:
|
||||
version: 8.11.3
|
||||
|
||||
test:
|
||||
post:
|
||||
- npm run coverage -- --upload --providers coveralls
|
||||
|
||||
dependencies:
|
||||
pre:
|
||||
- google-chrome --version
|
||||
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||||
- for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb
|
||||
- sudo dpkg -i google-chrome.deb || true
|
||||
- sudo dpkg -i libnss3*.deb || true
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -f || true
|
||||
- sudo dpkg -i libnss3*.deb
|
||||
- sudo apt-get install -f
|
||||
- sudo apt-get install --only-upgrade lsb-base
|
||||
- sudo dpkg -i google-chrome.deb
|
||||
- google-chrome --version
|
@ -10,8 +10,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
|
||||
- [Protocol and Stream Muxing](./protocol-and-stream-muxing)
|
||||
- [Encrypted Communications](./encrypted-communications)
|
||||
- [Discovery Mechanisms](./discovery-mechanisms)
|
||||
- [Peer Routing](./peer-and-content-routing)
|
||||
- [Content Routing](./peer-and-content-routing)
|
||||
- [Peer and Content Routing](./peer-and-content-routing)
|
||||
- [PubSub](./pubsub)
|
||||
- [NAT Traversal](./nat-traversal)
|
||||
- Circuit Relay (future)
|
||||
|
@ -10,4 +10,4 @@ This example creates a simple chat app in your terminal.
|
||||
1. Run the listener in window 1, `node listener.js`
|
||||
2. Run the dialer in window 2, `node dialer.js`
|
||||
3. Type a message in either window and hit _enter_
|
||||
4. Tell youself secrets to your hearts content!
|
||||
4. Tell yourself secrets to your hearts content!
|
||||
|
@ -3,7 +3,7 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
|
49
examples/delegated-routing/README.md
Normal file
49
examples/delegated-routing/README.md
Normal file
@ -0,0 +1,49 @@
|
||||
# Delegated Routing with Libp2p and IPFS
|
||||
|
||||
This example shows how to use delegated peer and content routing. The [Peer and Content Routing Example](../peer-and-content-routing) focuses
|
||||
on the DHT implementation. This example takes that a step further and introduces delegated routing. Delegated routing is
|
||||
especially useful when your libp2p node will have limited resources, making running a DHT impractical. It's
|
||||
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
|
||||
to provide content on your behalf.
|
||||
|
||||
The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
|
||||
Once you've completed the example, you should try enabled the DHT and see what kind of results you get! You can also enable the
|
||||
various Peer Discovery modules and see the impact it has on your Peer count.
|
||||
|
||||
## Prerequisite
|
||||
**NOTE**: This example is currently dependent on a clone of the [delegated routing support branch of go-ipfs](https://github.com/ipfs/go-ipfs/pull/4595).
|
||||
|
||||
## Running this example
|
||||
|
||||
1. Install IPFS locally if you dont already have it. [Install Guide](https://docs.ipfs.io/introduction/install/)
|
||||
2. Run the IPFS daemon: `ipfs daemon`
|
||||
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
|
||||
4. In another window output the addresses of the node: `ipfs id`. Make note of the websocket address, it will contain `/ws/` in the address.
|
||||
5. In `./src/libp2p-bundle.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
|
||||
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
|
||||
7. Start this example:
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm start
|
||||
```
|
||||
|
||||
This should open your browser to http://localhost:3000. If it does not, go ahead and do that now.
|
||||
|
||||
8. Your browser should show you connected to at least 1 peer.
|
||||
|
||||
### Finding Content via the Delegate
|
||||
1. Add a file to your IPFS node. From this example root you can do `ipfs add ./README.md` to add the example readme.
|
||||
2. Copy the hash from line 5, it will look something like *Qmf33vz4HJFkqgH7XPP1uA6atYKTX1BWQEQthzpKcAdeyZ*.
|
||||
3. In the browser, paste the hash into the *Hash* field and hit `Find`. The readme contents should display.
|
||||
|
||||
This will do a few things:
|
||||
* The delegate nodes api will be queried to find providers of the content
|
||||
* The content will be fetched from the providers
|
||||
* Since we now have the content, we tell the delegate node to fetch the content from us and become a provider
|
||||
|
||||
### Finding Peers via the Delegate
|
||||
1. Get a list of your delegate nodes peer by querying the IPFS daemon: `ipfs swarm peers`
|
||||
2. Copy one of the CIDs from the list of peer addresses, this will be the last portion of the address and will look something like `QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8`.
|
||||
3. In your browser, paste the CID into the *Peer* field and hit `Find`.
|
||||
4. You should see information about the peer including its addresses.
|
23
examples/delegated-routing/package.json
Normal file
23
examples/delegated-routing/package.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "delegated-routing-example",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"ipfs": "~0.32.2",
|
||||
"libp2p": "../../",
|
||||
"libp2p-delegated-content-routing": "~0.2.2",
|
||||
"libp2p-delegated-peer-routing": "~0.2.2",
|
||||
"libp2p-kad-dht": "~0.10.4",
|
||||
"libp2p-mplex": "~0.8.0",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-webrtc-star": "~0.15.5",
|
||||
"libp2p-websocket-star": "~0.8.1",
|
||||
"libp2p-websockets": "~0.12.0",
|
||||
"react": "^16.5.2",
|
||||
"react-dom": "^16.5.2",
|
||||
"react-scripts": "1.1.5"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start"
|
||||
}
|
||||
}
|
BIN
examples/delegated-routing/public/favicon.ico
Normal file
BIN
examples/delegated-routing/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
16
examples/delegated-routing/public/index.html
Normal file
16
examples/delegated-routing/public/index.html
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="theme-color" content="#000000">
|
||||
<title>Delegated Routing</title>
|
||||
<link rel="stylesheet" type="text/css" href="main.css">
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
67
examples/delegated-routing/public/main.css
Normal file
67
examples/delegated-routing/public/main.css
Normal file
@ -0,0 +1,67 @@
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
|
||||
section * {
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
header {
|
||||
background-color: #222;
|
||||
height: 150px;
|
||||
padding: 20px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.center {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: bisque;
|
||||
min-height: 100px;
|
||||
margin: 0px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.loader {
|
||||
text-align: center;
|
||||
height: 64px;
|
||||
margin-bottom: -64px;
|
||||
}
|
||||
|
||||
.loading .lds-ripple {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
}
|
||||
.loading .lds-ripple div {
|
||||
position: absolute;
|
||||
border: 4px solid #000;
|
||||
opacity: 1;
|
||||
border-radius: 50%;
|
||||
animation: lds-ripple 1s cubic-bezier(0, 0.2, 0.8, 1) infinite;
|
||||
margin: auto;
|
||||
}
|
||||
.loading .lds-ripple div:nth-child(2) {
|
||||
animation-delay: -0.5s;
|
||||
}
|
||||
@keyframes lds-ripple {
|
||||
0% {
|
||||
top: 28px;
|
||||
left: 28px;
|
||||
width: 0;
|
||||
height: 0;
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
top: -1px;
|
||||
left: -1px;
|
||||
width: 58px;
|
||||
height: 58px;
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
153
examples/delegated-routing/src/App.js
Normal file
153
examples/delegated-routing/src/App.js
Normal file
@ -0,0 +1,153 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const React = require('react')
|
||||
const Component = React.Component
|
||||
const Ipfs = require('ipfs')
|
||||
const libp2pBundle = require('./libp2p-bundle')
|
||||
// require('./App.css')
|
||||
|
||||
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/ipfs/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
|
||||
|
||||
class App extends Component {
|
||||
constructor (props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
peers: 0,
|
||||
// This hash is the IPFS readme
|
||||
hash: 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB',
|
||||
// This peer is one of the Bootstrap nodes for IPFS
|
||||
peer: 'QmV6kA2fB8kTr6jc3pL5zbNsjKbmPUHAPKKHRBYe1kDEyc',
|
||||
isLoading: 0
|
||||
}
|
||||
this.peerInterval = null
|
||||
|
||||
this.handleHashChange = this.handleHashChange.bind(this)
|
||||
this.handleHashSubmit = this.handleHashSubmit.bind(this)
|
||||
this.handlePeerChange = this.handlePeerChange.bind(this)
|
||||
this.handlePeerSubmit = this.handlePeerSubmit.bind(this)
|
||||
}
|
||||
|
||||
handleHashChange (event) {
|
||||
this.setState({
|
||||
hash: event.target.value
|
||||
})
|
||||
}
|
||||
handlePeerChange (event) {
|
||||
this.setState({
|
||||
peer: event.target.value
|
||||
})
|
||||
}
|
||||
|
||||
handleHashSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.files.cat(this.state.hash, (err, data) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: data.toString(),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
handlePeerSubmit (event) {
|
||||
event.preventDefault()
|
||||
this.setState({
|
||||
isLoading: this.state.isLoading + 1
|
||||
})
|
||||
|
||||
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
|
||||
if (err) console.log('Error', err)
|
||||
|
||||
this.setState({
|
||||
response: JSON.stringify(results, null, 2),
|
||||
isLoading: this.state.isLoading - 1
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
componentDidMount () {
|
||||
window.ipfs = this.ipfs = new Ipfs({
|
||||
config: {
|
||||
Addresses: {
|
||||
Swarm: []
|
||||
},
|
||||
Discovery: {
|
||||
MDNS: {
|
||||
Enabled: false
|
||||
},
|
||||
webRTCStar: {
|
||||
Enabled: false
|
||||
}
|
||||
},
|
||||
Bootstrap: [
|
||||
BootstrapNode
|
||||
]
|
||||
},
|
||||
preload: {
|
||||
enabled: false
|
||||
},
|
||||
libp2p: libp2pBundle
|
||||
})
|
||||
this.ipfs.on('ready', () => {
|
||||
if (this.peerInterval) {
|
||||
clearInterval(this.peerInterval)
|
||||
}
|
||||
|
||||
this.ipfs.swarm.connect(BootstrapNode, (err) => {
|
||||
if (err) {
|
||||
console.log('Error connecting to the node', err)
|
||||
}
|
||||
console.log('Connected!')
|
||||
})
|
||||
|
||||
this.peerInterval = setInterval(() => {
|
||||
this.ipfs.swarm.peers((err, peers) => {
|
||||
if (err) console.log(err)
|
||||
if (peers) this.setState({peers: peers.length})
|
||||
})
|
||||
}, 2500)
|
||||
})
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<header className="center">
|
||||
<h1>Delegated Routing</h1>
|
||||
<h2>There are currently {this.state.peers} peers.</h2>
|
||||
</header>
|
||||
<section className="center">
|
||||
<form onSubmit={this.handleHashSubmit}>
|
||||
<label>
|
||||
Hash:
|
||||
<input type="text" value={this.state.hash} onChange={this.handleHashChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
<form onSubmit={this.handlePeerSubmit}>
|
||||
<label>
|
||||
Peer:
|
||||
<input type="text" value={this.state.peer} onChange={this.handlePeerChange} />
|
||||
<input type="submit" value="Find" />
|
||||
</label>
|
||||
</form>
|
||||
</section>
|
||||
<section className={[this.state.isLoading > 0 ? 'loading' : '', 'loader'].join(' ')}>
|
||||
<div className="lds-ripple"><div></div><div></div></div>
|
||||
</section>
|
||||
<section>
|
||||
<pre>
|
||||
{this.state.response}
|
||||
</pre>
|
||||
</section>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = App
|
9
examples/delegated-routing/src/index.js
Normal file
9
examples/delegated-routing/src/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const React = require('react') // eslint-disable-line no-unused-vars
|
||||
const ReactDOM = require('react-dom')
|
||||
const App = require('./App') // eslint-disable-line no-unused-vars
|
||||
// require('index.css')
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'))
|
78
examples/delegated-routing/src/libp2p-bundle.js
Normal file
78
examples/delegated-routing/src/libp2p-bundle.js
Normal file
@ -0,0 +1,78 @@
|
||||
// eslint-disable-next-line
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('libp2p')
|
||||
const Websockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
|
||||
module.exports = ({peerInfo, peerBook}) => {
|
||||
const wrtcstar = new WebRTCStar({id: peerInfo.id})
|
||||
const wsstar = new WebSocketStar({id: peerInfo.id})
|
||||
const delegatedApiOptions = {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: '8080'
|
||||
}
|
||||
|
||||
return new Libp2p({
|
||||
peerInfo,
|
||||
peerBook,
|
||||
// Lets limit the connection managers peers and have it check peer health less frequently
|
||||
connectionManager: {
|
||||
maxPeers: 10,
|
||||
pollInterval: 5000
|
||||
},
|
||||
modules: {
|
||||
contentRouting: [
|
||||
new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)
|
||||
],
|
||||
peerRouting: [
|
||||
new DelegatedPeerRouter(delegatedApiOptions)
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcstar.discovery,
|
||||
wsstar.discovery
|
||||
],
|
||||
transport: [
|
||||
wrtcstar,
|
||||
wsstar,
|
||||
Websockets
|
||||
],
|
||||
streamMuxer: [
|
||||
MPLEX
|
||||
],
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
webrtcStar: {
|
||||
enabled: false
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
},
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
@ -5,7 +6,7 @@ const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -3,7 +3,7 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const mplex = require('libp2p-mplex')
|
||||
|
@ -37,4 +37,4 @@ And that's it, from now on, all your libp2p communications are encrypted. Try ru
|
||||
|
||||
If you want to want to learn more about how SECIO works, you can read the [great write up done by Dominic Tarr](https://github.com/auditdrivencrypto/secure-channel/blob/master/prior-art.md#ipfss-secure-channel).
|
||||
|
||||
Importante note: SECIO hasn't been audited and so, we do not recommend to trust its security. We intent to move to TLS 1.3 once the specification is finalized and an implementation exists that we can use.
|
||||
Important note: SECIO hasn't been audited and so, we do not recommend to trust its security. We intent to move to TLS 1.3 once the specification is finalized and an implementation exists that we can use.
|
||||
|
@ -17,11 +17,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"detect-dom-ready": "^1.0.2",
|
||||
"libp2p-bootstrap": "~0.9.3",
|
||||
"libp2p-mplex": "~0.8.0",
|
||||
"libp2p-railing": "~0.9.1",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-spdy": "~0.12.1",
|
||||
"libp2p-webrtc-star": "~0.15.3",
|
||||
"libp2p-websocket-star": "~0.8.1",
|
||||
"libp2p-websockets": "~0.12.0",
|
||||
"peer-info": "~0.14.1"
|
||||
}
|
||||
|
@ -2,15 +2,16 @@
|
||||
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../../../')
|
||||
|
||||
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
|
||||
const bootstrapers = [
|
||||
const bootstrapList = [
|
||||
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
|
||||
'/dns4/sfo-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
|
||||
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
|
||||
@ -19,19 +20,21 @@ const bootstrapers = [
|
||||
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
|
||||
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
|
||||
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
|
||||
'/dns4/wss0.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
|
||||
'/dns4/wss1.bootstrap.libp2p.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
|
||||
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
|
||||
]
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
|
||||
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
|
||||
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
wrtcStar,
|
||||
new WebSockets()
|
||||
WebSockets,
|
||||
wsstar
|
||||
],
|
||||
streamMuxer: [
|
||||
Mplex,
|
||||
@ -42,6 +45,7 @@ class Node extends libp2p {
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcStar.discovery,
|
||||
wsstar.discovery,
|
||||
Bootstrap
|
||||
]
|
||||
},
|
||||
@ -55,14 +59,14 @@ class Node extends libp2p {
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: bootstrapers
|
||||
enabled: true,
|
||||
list: bootstrapList
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: false,
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: false,
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
@ -70,6 +74,9 @@ class Node extends libp2p {
|
||||
dht: false,
|
||||
pubsub: false
|
||||
}
|
||||
},
|
||||
connectionManager: {
|
||||
maxPeers: 50
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
/* eslint no-console: ["error", { allow: ["log"] }] */
|
||||
/* eslint max-nested-callbacks: ["error", 5] */
|
||||
'use strict'
|
||||
|
||||
const domReady = require('detect-dom-ready')
|
||||
@ -12,13 +14,25 @@ domReady(() => {
|
||||
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
|
||||
}
|
||||
|
||||
node.on('peer:discovery', (peerInfo) => {
|
||||
console.log('Discovered a peer')
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
console.log('Discovered: ' + idStr)
|
||||
let connections = {}
|
||||
|
||||
node.on('peer:discovery', (peerInfo) => {
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
if (connections[idStr]) {
|
||||
// If we're already trying to connect to this peer, dont dial again
|
||||
return
|
||||
}
|
||||
console.log('Discovered a peer:', idStr)
|
||||
|
||||
connections[idStr] = true
|
||||
node.dial(peerInfo, (err, conn) => {
|
||||
if (err) { return console.log('Failed to dial:', idStr) }
|
||||
if (err) {
|
||||
// Prevent immediate connection retries from happening
|
||||
// and include a 10s jitter
|
||||
const timeToNextDial = 25 * 1000 + (Math.random(0) * 10000).toFixed(0)
|
||||
console.log('Failed to dial:', idStr)
|
||||
setTimeout(() => delete connections[idStr], timeToNextDial)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@ -33,8 +47,10 @@ domReady(() => {
|
||||
|
||||
node.on('peer:disconnect', (peerInfo) => {
|
||||
const idStr = peerInfo.id.toB58String()
|
||||
delete connections[idStr]
|
||||
console.log('Lost connection to: ' + idStr)
|
||||
document.getElementById(idStr).remove()
|
||||
const el = document.getElementById(idStr)
|
||||
el && el.remove()
|
||||
})
|
||||
|
||||
node.start((err) => {
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
|
86
package-list.json
Normal file
86
package-list.json
Normal file
@ -0,0 +1,86 @@
|
||||
{
|
||||
"columns": [
|
||||
"Package",
|
||||
"Version",
|
||||
"Deps",
|
||||
"CI",
|
||||
"Coverage",
|
||||
"Lead Maintainer"
|
||||
],
|
||||
"rows": [
|
||||
"Libp2p",
|
||||
["libp2p/interface-libp2p", "interface-libp2p"],
|
||||
["libp2p/js-libp2p", "libp2p"],
|
||||
|
||||
"Connection",
|
||||
["libp2p/interface-connection", "interface-connection"],
|
||||
|
||||
"Transport",
|
||||
["libp2p/interface-transport", "interface-transport"],
|
||||
["libp2p/js-libp2p-tcp", "libp2p-tcp"],
|
||||
["libp2p/js-libp2p-udp", "libp2p-udp"],
|
||||
["libp2p/js-libp2p-udt", "libp2p-udt"],
|
||||
["libp2p/js-libp2p-utp", "libp2p-utp"],
|
||||
["libp2p/js-libp2p-webrtc-direct", "libp2p-webrtc-direct"],
|
||||
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
|
||||
["libp2p/js-libp2p-websockets", "libp2p-websockets"],
|
||||
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
|
||||
["libp2p/js-libp2p-websocket-star-rendezvous", "libp2p-websocket-star-rendezvous"],
|
||||
|
||||
"Crypto Channels",
|
||||
["libp2p/js-libp2p-secio", "libp2p-secio"],
|
||||
|
||||
"Stream Muxers",
|
||||
["libp2p/interface-stream-muxer", "interface-stream-muxer"],
|
||||
["libp2p/js-libp2p-mplex", "libp2p-mplex"],
|
||||
["libp2p/js-libp2p-spdy", "libp2p-spdy"],
|
||||
|
||||
"Discovery",
|
||||
["libp2p/interface-peer-discovery", "interface-peer-discovery"],
|
||||
["libp2p/js-libp2p-bootstrap", "libp2p-bootstrap"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
["libp2p/js-libp2p-mdns", "libp2p-mdns"],
|
||||
["libp2p/js-libp2p-rendezvous", "libp2p-rendezvous"],
|
||||
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
|
||||
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
|
||||
|
||||
"NAT Traversal",
|
||||
["libp2p/js-libp2p-circuit", "libp2p-circuit"],
|
||||
["libp2p/js-libp2p-nat-mngr", "libp2p-nat-mngr"],
|
||||
|
||||
"Data Types",
|
||||
["libp2p/js-peer-book", "peer-book"],
|
||||
["libp2p/js-peer-id", "peer-id"],
|
||||
["libp2p/js-peer-info", "peer-info"],
|
||||
|
||||
"Content Routing",
|
||||
["libp2p/interface-content-routing", "interface-content-routing"],
|
||||
["libp2p/js-libp2p-delegated-content-routing", "libp2p-delegated-content-routing"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
|
||||
"Peer Routing",
|
||||
["libp2p/interface-peer-routing", "interface-peer-routing"],
|
||||
["libp2p/js-libp2p-delegated-peer-routing", "libp2p-delegated-peer-routing"],
|
||||
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
|
||||
|
||||
"Record Store",
|
||||
["libp2p/interface-record-store", "interface-record-store"],
|
||||
["libp2p/js-libp2p-record", "libp2p-record"],
|
||||
|
||||
"Generics",
|
||||
["libp2p/js-libp2p-connection-manager", "libp2p-connection-manager"],
|
||||
["libp2p/js-libp2p-crypto", "libp2p-crypto"],
|
||||
["libp2p/js-libp2p-crypto-secp256k1", "libp2p-crypto-secp256k1"],
|
||||
["libp2p/js-libp2p-switch", "libp2p-switch"],
|
||||
|
||||
"Extensions",
|
||||
["libp2p/js-libp2p-floodsub", "libp2p-floodsub"],
|
||||
["libp2p/js-libp2p-identify", "libp2p-identify"],
|
||||
["libp2p/js-libp2p-keychain", "libp2p-keychain"],
|
||||
["libp2p/js-libp2p-ping", "libp2p-ping"],
|
||||
["libp2p/js-libp2p-pnet", "libp2p-pnet"],
|
||||
|
||||
"Utilities",
|
||||
["libp2p/js-p2pcat", "p2pcat"]
|
||||
]
|
||||
}
|
75
package.json
75
package.json
@ -1,9 +1,13 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.22.0",
|
||||
"version": "0.24.4",
|
||||
"description": "JavaScript base class for libp2p bundles",
|
||||
"leadMaintainer": "David Dias <daviddias@ipfs.io>",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "aegir lint",
|
||||
"build": "aegir build",
|
||||
@ -33,49 +37,60 @@
|
||||
},
|
||||
"homepage": "https://github.com/libp2p/js-libp2p",
|
||||
"browser": {
|
||||
"joi": "joi-browser"
|
||||
"joi": "joi-browser",
|
||||
"./test/utils/bundle-nodejs": "./test/utils/bundle-browser"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"joi": "^13.4.0",
|
||||
"debug": "^4.1.0",
|
||||
"err-code": "^1.1.2",
|
||||
"fsm-event": "^2.1.0",
|
||||
"joi": "^14.0.6",
|
||||
"joi-browser": "^13.4.0",
|
||||
"libp2p-connection-manager": "~0.0.2",
|
||||
"libp2p-floodsub": "~0.15.0",
|
||||
"libp2p-ping": "~0.8.0",
|
||||
"libp2p-switch": "~0.40.4",
|
||||
"libp2p-floodsub": "~0.15.1",
|
||||
"libp2p-ping": "~0.8.3",
|
||||
"libp2p-switch": "~0.41.3",
|
||||
"libp2p-websockets": "~0.12.0",
|
||||
"mafmt": "^6.0.0",
|
||||
"multiaddr": "^5.0.0",
|
||||
"peer-book": "~0.8.0",
|
||||
"peer-id": "~0.10.7",
|
||||
"peer-info": "~0.14.1"
|
||||
"mafmt": "^6.0.2",
|
||||
"multiaddr": "^6.0.2",
|
||||
"peer-book": "~0.9.0",
|
||||
"peer-id": "~0.12.0",
|
||||
"peer-info": "~0.15.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodeutils/defaults-deep": "^1.1.0",
|
||||
"aegir": "^14.0.0",
|
||||
"chai": "^4.1.2",
|
||||
"cids": "~0.5.3",
|
||||
"aegir": "^18.0.2",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"cids": "~0.5.5",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"electron-webrtc": "~0.3.0",
|
||||
"libp2p-circuit": "~0.2.0",
|
||||
"libp2p-kad-dht": "~0.10.0",
|
||||
"interface-datastore": "~0.6.0",
|
||||
"libp2p-bootstrap": "~0.9.3",
|
||||
"libp2p-circuit": "~0.3.0",
|
||||
"libp2p-delegated-content-routing": "~0.2.2",
|
||||
"libp2p-delegated-peer-routing": "~0.2.2",
|
||||
"libp2p-kad-dht": "~0.14.2",
|
||||
"libp2p-mdns": "~0.12.0",
|
||||
"libp2p-mplex": "~0.8.0",
|
||||
"libp2p-railing": "~0.9.2",
|
||||
"libp2p-secio": "~0.10.0",
|
||||
"libp2p-spdy": "~0.12.1",
|
||||
"libp2p-tcp": "~0.12.0",
|
||||
"libp2p-webrtc-star": "~0.15.3",
|
||||
"libp2p-websocket-star": "~0.8.1",
|
||||
"libp2p-websocket-star-rendezvous": "~0.2.3",
|
||||
"libp2p-mplex": "~0.8.4",
|
||||
"libp2p-secio": "~0.11.0",
|
||||
"libp2p-spdy": "~0.13.0",
|
||||
"libp2p-tcp": "~0.13.0",
|
||||
"libp2p-webrtc-star": "~0.15.5",
|
||||
"libp2p-websocket-star": "~0.10.1",
|
||||
"libp2p-websocket-star-rendezvous": "~0.3.0",
|
||||
"lodash.times": "^4.3.2",
|
||||
"nock": "^10.0.2",
|
||||
"pull-goodbye": "0.0.2",
|
||||
"pull-serializer": "~0.3.2",
|
||||
"pull-stream": "^3.6.8",
|
||||
"sinon": "^6.0.1",
|
||||
"wrtc": "~0.1.6"
|
||||
"pull-stream": "^3.6.9",
|
||||
"sinon": "^7.1.1",
|
||||
"webrtcsupport": "^2.2.0",
|
||||
"wrtc": "~0.3.2"
|
||||
},
|
||||
"contributors": [
|
||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||
"Alan Shaw <alan@tableflip.io>",
|
||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||
@ -87,6 +102,7 @@
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Hugo Dias <hugomrdias@gmail.com>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>",
|
||||
"Jacob Heun <jacobheun@gmail.com>",
|
||||
@ -96,6 +112,7 @@
|
||||
"Kevin Kwok <antimatter15@gmail.com>",
|
||||
"Lars Gierth <lgierth@users.noreply.github.com>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Marcin Tojek <mtojek@users.noreply.github.com>",
|
||||
"Nuno Nogueira <nunofmn@gmail.com>",
|
||||
"Pedro Teixeira <pedro@protocol.ai>",
|
||||
"Pedro Teixeira <i@pgte.me>",
|
||||
@ -104,6 +121,8 @@
|
||||
"Ryan Bell <ryan@piing.net>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"Vasco Santos <vasco.santos@ua.pt>",
|
||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||
"Volker Mische <volker.mische@gmail.com>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
|
@ -7,27 +7,36 @@ const ModuleSchema = Joi.alternatives().try(Joi.func(), Joi.object())
|
||||
const OptionsSchema = Joi.object({
|
||||
// TODO: create proper validators for the generics
|
||||
connectionManager: Joi.object(),
|
||||
datastore: Joi.object(),
|
||||
peerInfo: Joi.object().required(),
|
||||
peerBook: Joi.object(),
|
||||
modules: Joi.object().keys({
|
||||
transport: Joi.array().items(ModuleSchema).min(1).required(),
|
||||
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
|
||||
connEncryption: Joi.array().items(ModuleSchema).allow(null),
|
||||
connProtector: Joi.object().keys({
|
||||
protect: Joi.func().required()
|
||||
}).unknown(),
|
||||
contentRouting: Joi.array().items(Joi.object()).allow(null),
|
||||
dht: ModuleSchema.allow(null),
|
||||
peerDiscovery: Joi.array().items(ModuleSchema).allow(null),
|
||||
dht: ModuleSchema.allow(null)
|
||||
peerRouting: Joi.array().items(Joi.object()).allow(null),
|
||||
streamMuxer: Joi.array().items(ModuleSchema).allow(null),
|
||||
transport: Joi.array().items(ModuleSchema).min(1).required()
|
||||
}).required(),
|
||||
config: Joi.object().keys({
|
||||
peerDiscovery: Joi.object().allow(null),
|
||||
relay: Joi.object().keys({
|
||||
enabled: Joi.boolean().default(false),
|
||||
enabled: Joi.boolean().default(true),
|
||||
hop: Joi.object().keys({
|
||||
enabled: Joi.boolean().default(false),
|
||||
active: Joi.boolean().default(false)
|
||||
})
|
||||
}).default(),
|
||||
dht: Joi.object().keys({
|
||||
kBucketSize: Joi.number().allow(null)
|
||||
}),
|
||||
kBucketSize: Joi.number().default(20),
|
||||
enabledDiscovery: Joi.boolean().default(true),
|
||||
validators: Joi.object().allow(null),
|
||||
selectors: Joi.object().allow(null)
|
||||
}).default(),
|
||||
EXPERIMENTAL: Joi.object().keys({
|
||||
dht: Joi.boolean().default(false),
|
||||
pubsub: Joi.boolean().default(false)
|
||||
|
@ -1,20 +1,83 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const parallel = require('async/parallel')
|
||||
const errCode = require('err-code')
|
||||
|
||||
module.exports = (node) => {
|
||||
const routers = node._modules.contentRouting || []
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (node._dht) {
|
||||
routers.unshift(node._dht)
|
||||
}
|
||||
|
||||
return {
|
||||
findProviders: (key, timeout, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
/**
|
||||
* Iterates over all content routers in series to find providers of the given key.
|
||||
* Once a content router succeeds, iteration will stop.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {number} options.maxNumProviders - maximum number of providers to find
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findProviders: (key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
} else if (typeof options === 'number') { // This can be deprecated in a future release
|
||||
options = {
|
||||
maxTimeout: options
|
||||
}
|
||||
}
|
||||
|
||||
node._dht.findProviders(key, timeout, callback)
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findProviders(key, options, (err, results) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
// If we don't have any results, we need to provide an error to keep trying
|
||||
if (!results || Object.keys(results).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
}
|
||||
|
||||
cb(null, results)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err && err.code !== 'NOT_FOUND') {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Iterates over all content routers in parallel to notify it is
|
||||
* a provider of the given key.
|
||||
*
|
||||
* @param {CID} key The CID key of the content to find
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
provide: (key, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
if (!routers.length) {
|
||||
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
node._dht.provide(key, callback)
|
||||
parallel(routers.map((router) => {
|
||||
return (cb) => router.provide(key, cb)
|
||||
}), callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
src/dht.js
18
src/dht.js
@ -9,19 +9,29 @@ module.exports = (node) => {
|
||||
|
||||
node._dht.put(key, value, callback)
|
||||
},
|
||||
get: (key, callback) => {
|
||||
get: (key, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
}
|
||||
|
||||
node._dht.get(key, callback)
|
||||
node._dht.get(key, options, callback)
|
||||
},
|
||||
getMany (key, nVals, callback) {
|
||||
getMany: (key, nVals, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
}
|
||||
|
||||
node._dht.getMany(key, nVals, callback)
|
||||
node._dht.getMany(key, nVals, options, callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const multiaddr = require('multiaddr')
|
||||
const errCode = require('err-code')
|
||||
|
||||
module.exports = (node) => {
|
||||
/*
|
||||
@ -16,12 +17,24 @@ module.exports = (node) => {
|
||||
// Multiaddr instance or Multiaddr String
|
||||
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
||||
if (typeof peer === 'string') {
|
||||
try {
|
||||
peer = multiaddr(peer)
|
||||
} catch (err) {
|
||||
return callback(
|
||||
errCode(err, 'ERR_INVALID_MULTIADDR')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const peerIdB58Str = peer.getPeerId()
|
||||
|
||||
if (!peerIdB58Str) {
|
||||
throw new Error(`peer multiaddr instance or string must include peerId`)
|
||||
return callback(
|
||||
errCode(
|
||||
new Error('peer multiaddr instance or string must include peerId'),
|
||||
'ERR_INVALID_MULTIADDR'
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
@ -40,9 +53,14 @@ module.exports = (node) => {
|
||||
return node.peerRouting.findPeer(peer, callback)
|
||||
}
|
||||
} else {
|
||||
return setImmediate(() => callback(new Error('peer type not recognized')))
|
||||
return callback(
|
||||
errCode(
|
||||
new Error(`${p} is not a valid peer type`),
|
||||
'ERR_INVALID_PEER_TYPE'
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
setImmediate(() => callback(null, p))
|
||||
callback(null, p)
|
||||
}
|
||||
}
|
||||
|
352
src/index.js
352
src/index.js
@ -1,7 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const FSM = require('fsm-event')
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const assert = require('assert')
|
||||
const debug = require('debug')
|
||||
const log = debug('libp2p')
|
||||
log.error = debug('libp2p:error')
|
||||
const errCode = require('err-code')
|
||||
|
||||
const each = require('async/each')
|
||||
const series = require('async/series')
|
||||
@ -20,10 +24,21 @@ const pubsub = require('./pubsub')
|
||||
const getPeerInfo = require('./get-peer-info')
|
||||
const validateConfig = require('./config').validate
|
||||
|
||||
exports = module.exports
|
||||
|
||||
const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
|
||||
const notStarted = (action, state) => {
|
||||
return errCode(
|
||||
new Error(`libp2p cannot ${action} when not started; state is ${state}`),
|
||||
'ERR_NODE_NOT_STARTED'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @fires Node#error Emitted when an error occurs
|
||||
* @fires Node#peer:connect Emitted when a peer is connected to this node
|
||||
* @fires Node#peer:disconnect Emitted when a peer disconnects from this node
|
||||
* @fires Node#peer:discovery Emitted when a peer is discovered
|
||||
* @fires Node#start Emitted when the node and its services has started
|
||||
* @fires Node#stop Emitted when the node and its services has stopped
|
||||
*/
|
||||
class Node extends EventEmitter {
|
||||
constructor (_options) {
|
||||
super()
|
||||
@ -31,6 +46,7 @@ class Node extends EventEmitter {
|
||||
// and add default values where appropriate
|
||||
_options = validateConfig(_options)
|
||||
|
||||
this.datastore = _options.datastore
|
||||
this.peerInfo = _options.peerInfo
|
||||
this.peerBook = _options.peerBook || new PeerBook()
|
||||
|
||||
@ -40,7 +56,10 @@ class Node extends EventEmitter {
|
||||
this._transport = [] // Transport instances/references
|
||||
this._discovery = [] // Discovery service instances/references
|
||||
|
||||
// create the switch, and listen for errors
|
||||
this._switch = new Switch(this.peerInfo, this.peerBook, _options.switch)
|
||||
this._switch.on('error', (...args) => this.emit('error', ...args))
|
||||
|
||||
this.stats = this._switch.stats
|
||||
this.connectionManager = new ConnectionManager(this, _options.connectionManager)
|
||||
|
||||
@ -75,14 +94,24 @@ class Node extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
// Attach private network protector
|
||||
if (this._modules.connProtector) {
|
||||
this._switch.protector = this._modules.connProtector
|
||||
} else if (process.env.LIBP2P_FORCE_PNET) {
|
||||
throw new Error('Private network is enforced, but no protector was provided')
|
||||
}
|
||||
|
||||
// dht provided components (peerRouting, contentRouting, dht)
|
||||
if (this._config.EXPERIMENTAL.dht) {
|
||||
const DHT = this._modules.dht
|
||||
const enabledDiscovery = this._config.dht.enabledDiscovery !== false
|
||||
|
||||
this._dht = new DHT(this._switch, {
|
||||
kBucketSize: this._config.dht.kBucketSize || 20,
|
||||
// TODO make datastore an option of libp2p itself so
|
||||
// that other things can use it as well
|
||||
datastore: dht.datastore
|
||||
kBucketSize: this._config.dht.kBucketSize,
|
||||
enabledDiscovery,
|
||||
datastore: this.datastore,
|
||||
validators: this._config.dht.validators,
|
||||
selectors: this._config.dht.selectors
|
||||
})
|
||||
}
|
||||
|
||||
@ -92,6 +121,7 @@ class Node extends EventEmitter {
|
||||
}
|
||||
|
||||
// Attach remaining APIs
|
||||
// peer and content routing will automatically get modules from _modules and _dht
|
||||
this.peerRouting = peerRouting(this)
|
||||
this.contentRouting = contentRouting(this)
|
||||
this.dht = dht(this)
|
||||
@ -100,15 +130,196 @@ class Node extends EventEmitter {
|
||||
|
||||
// Mount default protocols
|
||||
Ping.mount(this._switch)
|
||||
|
||||
this.state = new FSM('STOPPED', {
|
||||
STOPPED: {
|
||||
start: 'STARTING',
|
||||
stop: 'STOPPED'
|
||||
},
|
||||
STARTING: {
|
||||
done: 'STARTED',
|
||||
abort: 'STOPPED',
|
||||
stop: 'STOPPING'
|
||||
},
|
||||
STARTED: {
|
||||
stop: 'STOPPING',
|
||||
start: 'STARTED'
|
||||
},
|
||||
STOPPING: {
|
||||
stop: 'STOPPING',
|
||||
done: 'STOPPED'
|
||||
}
|
||||
})
|
||||
this.state.on('STARTING', () => {
|
||||
log('libp2p is starting')
|
||||
this._onStarting()
|
||||
})
|
||||
this.state.on('STOPPING', () => {
|
||||
log('libp2p is stopping')
|
||||
this._onStopping()
|
||||
})
|
||||
this.state.on('STARTED', () => {
|
||||
log('libp2p has started')
|
||||
this.emit('start')
|
||||
})
|
||||
this.state.on('STOPPED', () => {
|
||||
log('libp2p has stopped')
|
||||
this.emit('stop')
|
||||
})
|
||||
this.state.on('error', (err) => {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
* Start the libp2p node
|
||||
* - create listeners on the multiaddrs the Peer wants to listen
|
||||
/**
|
||||
* Overrides EventEmitter.emit to conditionally emit errors
|
||||
* if there is a handler. If not, errors will be logged.
|
||||
* @param {string} eventName
|
||||
* @param {...any} args
|
||||
* @returns {void}
|
||||
*/
|
||||
start (callback) {
|
||||
emit (eventName, ...args) {
|
||||
if (eventName === 'error' && !this._events.error) {
|
||||
log.error(...args)
|
||||
} else {
|
||||
super.emit(eventName, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the libp2p node and all sub services
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
start (callback = () => {}) {
|
||||
this.once('start', callback)
|
||||
this.state('start')
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
stop (callback = () => {}) {
|
||||
this.once('stop', callback)
|
||||
this.state('stop')
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
return this.state ? this.state._state === 'STARTED' : false
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials to the provided peer. If successful, the `PeerInfo` of the
|
||||
* peer will be added to the nodes `PeerBook`
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {function(Error)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dial (peer, callback) {
|
||||
this.dialProtocol(peer, null, callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Dials to the provided peer and handshakes with the given protocol.
|
||||
* If successful, the `PeerInfo` of the peer will be added to the nodes `PeerBook`,
|
||||
* and the `Connection` will be sent in the callback
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, Connection)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialProtocol (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dial(peerInfo, protocol, (err, conn) => {
|
||||
if (err) { return callback(err) }
|
||||
this.peerBook.put(peerInfo)
|
||||
callback(null, conn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `dial` and `dialProtocol`, but the callback will contain a
|
||||
* Connection State Machine.
|
||||
*
|
||||
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
|
||||
* @param {string} protocol
|
||||
* @param {function(Error, ConnectionFSM)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
dialFSM (peer, protocol, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('dial', this.state._state))
|
||||
}
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
const connFSM = this._switch.dialFSM(peerInfo, protocol, (err) => {
|
||||
if (!err) {
|
||||
this.peerBook.put(peerInfo)
|
||||
}
|
||||
})
|
||||
|
||||
callback(null, connFSM)
|
||||
})
|
||||
}
|
||||
|
||||
hangUp (peer, callback) {
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
})
|
||||
}
|
||||
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(notStarted('ping', this.state._state))
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
})
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
|
||||
_onStarting () {
|
||||
if (!this._modules.transport) {
|
||||
return callback(new Error('no transports were present'))
|
||||
this.emit('error', new Error('no transports were present'))
|
||||
return this.state('abort')
|
||||
}
|
||||
|
||||
let ws
|
||||
@ -136,7 +347,7 @@ class Node extends EventEmitter {
|
||||
}
|
||||
|
||||
if (t.filter(multiaddrs).length > 0) {
|
||||
this._switch.transport.add(t.tag || t.constructor.name, t)
|
||||
this._switch.transport.add(t.tag || t[Symbol.toStringTag], t)
|
||||
} else if (WebSockets.isWebSockets(t)) {
|
||||
// TODO find a cleaner way to signal that a transport is always used
|
||||
// for dialing, even if no listener
|
||||
@ -146,7 +357,10 @@ class Node extends EventEmitter {
|
||||
})
|
||||
|
||||
series([
|
||||
(cb) => this._switch.start(cb),
|
||||
(cb) => {
|
||||
this.connectionManager.start()
|
||||
this._switch.start(cb)
|
||||
},
|
||||
(cb) => {
|
||||
if (ws) {
|
||||
// always add dialing on websockets
|
||||
@ -202,12 +416,10 @@ class Node extends EventEmitter {
|
||||
// TODO: chicken-and-egg problem #2:
|
||||
// have to set started here because FloodSub requires libp2p is already started
|
||||
if (this._floodSub) {
|
||||
this._floodSub.start(cb)
|
||||
} else {
|
||||
cb()
|
||||
return this._floodSub.start(cb)
|
||||
}
|
||||
cb()
|
||||
},
|
||||
|
||||
(cb) => {
|
||||
// detect which multiaddrs we don't have a transport for and remove them
|
||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||
@ -219,18 +431,18 @@ class Node extends EventEmitter {
|
||||
}
|
||||
})
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
this.emit('start')
|
||||
cb()
|
||||
}
|
||||
], callback)
|
||||
], (err) => {
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
return this.state('stop')
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*/
|
||||
stop (callback) {
|
||||
_onStopping () {
|
||||
series([
|
||||
(cb) => {
|
||||
if (this._modules.peerDiscovery) {
|
||||
@ -256,84 +468,22 @@ class Node extends EventEmitter {
|
||||
}
|
||||
cb()
|
||||
},
|
||||
(cb) => this._switch.stop(cb),
|
||||
(cb) => {
|
||||
this.emit('stop')
|
||||
cb()
|
||||
this.connectionManager.stop()
|
||||
this._switch.stop(cb)
|
||||
},
|
||||
(cb) => {
|
||||
// Ensures idempotent restarts
|
||||
this._switch.transport.removeAll(cb)
|
||||
}
|
||||
], (err) => {
|
||||
this._isStarted = false
|
||||
callback(err)
|
||||
if (err) {
|
||||
log.error(err)
|
||||
this.emit('error', err)
|
||||
}
|
||||
this.state('done')
|
||||
})
|
||||
}
|
||||
|
||||
isStarted () {
|
||||
return this._isStarted
|
||||
}
|
||||
|
||||
dial (peer, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dial(peerInfo, (err) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this.peerBook.put(peerInfo)
|
||||
callback()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
dialProtocol (peer, protocol, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.dial(peerInfo, protocol, (err, conn) => {
|
||||
if (err) { return callback(err) }
|
||||
this.peerBook.put(peerInfo)
|
||||
callback(null, conn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
hangUp (peer, callback) {
|
||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
this._switch.hangUp(peerInfo, callback)
|
||||
})
|
||||
}
|
||||
|
||||
ping (peer, callback) {
|
||||
if (!this.isStarted()) {
|
||||
return callback(new Error(NOT_STARTED_ERROR_MESSAGE))
|
||||
}
|
||||
|
||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||
if (err) { return callback(err) }
|
||||
|
||||
callback(null, new Ping(this._switch, peerInfo))
|
||||
})
|
||||
}
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this._switch.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this._switch.unhandle(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
||||
|
@ -1,13 +1,58 @@
|
||||
'use strict'
|
||||
|
||||
const tryEach = require('async/tryEach')
|
||||
const errCode = require('err-code')
|
||||
|
||||
module.exports = (node) => {
|
||||
return {
|
||||
findPeer: (id, callback) => {
|
||||
if (!node._dht) {
|
||||
return callback(new Error('DHT is not available'))
|
||||
const routers = node._modules.peerRouting || []
|
||||
|
||||
// If we have the dht, make it first
|
||||
if (node._dht) {
|
||||
routers.unshift(node._dht)
|
||||
}
|
||||
|
||||
node._dht.findPeer(id, callback)
|
||||
return {
|
||||
/**
|
||||
* Iterates over all peer routers in series to find the given peer.
|
||||
*
|
||||
* @param {String} id The id of the peer to find
|
||||
* @param {object} options
|
||||
* @param {number} options.maxTimeout How long the query should run
|
||||
* @param {function(Error, Result<Array>)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
findPeer: (id, options, callback) => {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
if (!routers.length) {
|
||||
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
|
||||
}
|
||||
|
||||
const tasks = routers.map((router) => {
|
||||
return (cb) => router.findPeer(id, options, (err, result) => {
|
||||
if (err) {
|
||||
return cb(err)
|
||||
}
|
||||
|
||||
// If we don't have a result, we need to provide an error to keep trying
|
||||
if (!result || Object.keys(result).length === 0) {
|
||||
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
|
||||
}
|
||||
|
||||
cb(null, result)
|
||||
})
|
||||
})
|
||||
|
||||
tryEach(tasks, (err, results) => {
|
||||
if (err && err.code !== 'NOT_FOUND') {
|
||||
return callback(err)
|
||||
}
|
||||
results = results || []
|
||||
callback(null, results)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,10 @@ const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const waterfall = require('async/waterfall')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const DHT = require('libp2p-kad-dht')
|
||||
|
||||
const validateConfig = require('../src/config').validate
|
||||
|
||||
@ -89,8 +92,12 @@ describe('configuration', () => {
|
||||
pubsub: false,
|
||||
dht: false
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabledDiscovery: true
|
||||
},
|
||||
relay: {
|
||||
enabled: false
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -98,6 +105,34 @@ describe('configuration', () => {
|
||||
expect(validateConfig(options)).to.deep.equal(expected)
|
||||
})
|
||||
|
||||
it('should allow for delegated content and peer routing', () => {
|
||||
const peerRouter = new DelegatedPeerRouter()
|
||||
const contentRouter = new DelegatedContentRouter(peerInfo)
|
||||
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ],
|
||||
peerDiscovery: [ Bootstrap ],
|
||||
peerRouting: [ peerRouter ],
|
||||
contentRouting: [ contentRouter ]
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
bootstrap: {
|
||||
interval: 1000,
|
||||
enabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(validateConfig(options).modules).to.deep.include({
|
||||
peerRouting: [ peerRouter ],
|
||||
contentRouting: [ contentRouter ]
|
||||
})
|
||||
})
|
||||
|
||||
it('should not allow for dht to be enabled without it being provided', () => {
|
||||
const options = {
|
||||
peerInfo,
|
||||
@ -113,4 +148,49 @@ describe('configuration', () => {
|
||||
|
||||
expect(() => validateConfig(options)).to.throw()
|
||||
})
|
||||
|
||||
it('should add defaults, validators and selectors for dht', () => {
|
||||
const selectors = {}
|
||||
const validators = {}
|
||||
|
||||
const options = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WS],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
},
|
||||
dht: {
|
||||
selectors,
|
||||
validators
|
||||
}
|
||||
}
|
||||
}
|
||||
const expected = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [WS],
|
||||
dht: DHT
|
||||
},
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
pubsub: false,
|
||||
dht: true
|
||||
},
|
||||
relay: {
|
||||
enabled: true
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabledDiscovery: true,
|
||||
selectors,
|
||||
validators
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(validateConfig(options)).to.deep.equal(expected)
|
||||
})
|
||||
})
|
||||
|
@ -7,12 +7,20 @@ const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const parallel = require('async/parallel')
|
||||
const waterfall = require('async/waterfall')
|
||||
const _times = require('lodash.times')
|
||||
const CID = require('cids')
|
||||
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
|
||||
const sinon = require('sinon')
|
||||
const nock = require('nock')
|
||||
const ma = require('multiaddr')
|
||||
const Node = require('./utils/bundle-nodejs')
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
const createPeerInfo = createNode.createPeerInfo
|
||||
|
||||
describe('.contentRouting', () => {
|
||||
describe('via the dht', () => {
|
||||
let nodeA
|
||||
let nodeB
|
||||
let nodeC
|
||||
@ -62,6 +70,24 @@ describe('.contentRouting', () => {
|
||||
], done)
|
||||
})
|
||||
|
||||
it('should use the nodes dht to provide', (done) => {
|
||||
const stub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
|
||||
it('should use the nodes dht to find providers', (done) => {
|
||||
const stub = sinon.stub(nodeA._dht, 'findProviders').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
|
||||
nodeA.contentRouting.findProviders()
|
||||
})
|
||||
|
||||
describe('le ring', () => {
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||
|
||||
@ -74,17 +100,33 @@ describe('.contentRouting', () => {
|
||||
})
|
||||
|
||||
it('nodeE.contentRouting.findProviders for existing record', (done) => {
|
||||
nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
||||
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length.above(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeE.contentRouting.findProviders with limited number of providers', (done) => {
|
||||
parallel([
|
||||
(cb) => nodeA.contentRouting.provide(cid, cb),
|
||||
(cb) => nodeB.contentRouting.provide(cid, cb),
|
||||
(cb) => nodeC.contentRouting.provide(cid, cb)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeE.contentRouting.findProviders(cid, { maxNumProviders: 2 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length(2)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeC.contentRouting.findProviders for non existing record (timeout)', (done) => {
|
||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')
|
||||
|
||||
nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
||||
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(providers).to.have.length(0)
|
||||
done()
|
||||
@ -92,3 +134,278 @@ describe('.contentRouting', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => {
|
||||
createPeerInfo(cb)
|
||||
},
|
||||
// Create the node using the delegate
|
||||
(peerInfo, cb) => {
|
||||
delegate = new DelegatedContentRouter(peerInfo.id, {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: 60197
|
||||
}, [
|
||||
ma('/ip4/0.0.0.0/tcp/60194')
|
||||
])
|
||||
nodeA = new Node({
|
||||
peerInfo,
|
||||
modules: {
|
||||
contentRouting: [ delegate ]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
nodeA.start(cb)
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
afterEach(() => nock.cleanAll())
|
||||
|
||||
describe('provide', () => {
|
||||
it('should use the delegate router to provide', (done) => {
|
||||
const stub = sinon.stub(delegate, 'provide').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
|
||||
it('should be able to register as a provider', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
// mock the swarm connect
|
||||
.post('/api/v0/swarm/connect')
|
||||
.query({
|
||||
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, {
|
||||
Strings: [`connect ${nodeA.peerInfo.id.toB58String()} success`]
|
||||
}, ['Content-Type', 'application/json'])
|
||||
// mock the refs call
|
||||
.post('/api/v0/refs')
|
||||
.query({
|
||||
recursive: true,
|
||||
arg: cid.toBaseEncodedString(),
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, null, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.provide(cid, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors when registering as a provider', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
// mock the swarm connect
|
||||
.post('/api/v0/swarm/connect')
|
||||
.query({
|
||||
arg: `/ip4/0.0.0.0/tcp/60194/p2p-circuit/ipfs/${nodeA.peerInfo.id.toB58String()}`,
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(502, 'Bad Gateway', ['Content-Type', 'application/json'])
|
||||
|
||||
nodeA.contentRouting.provide(cid, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('find providers', () => {
|
||||
it('should use the delegate router to find providers', (done) => {
|
||||
const stub = sinon.stub(delegate, 'findProviders').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.findProviders()
|
||||
})
|
||||
|
||||
it('should be able to find providers', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const provider = 'QmZNgCqZCvTsi3B4Vt7gsSqpkqDpE7M2Y9TDmEhbDb4ceF'
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
.post('/api/v0/dht/findprovs')
|
||||
.query({
|
||||
arg: cid.toBaseEncodedString(),
|
||||
timeout: '1000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, `{"Extra":"","ID":"QmWKqWXCtRXEeCQTo3FoZ7g4AfnGiauYYiczvNxFCHicbB","Responses":[{"Addrs":["/ip4/0.0.0.0/tcp/0"],"ID":"${provider}"}],"Type":1}\n`, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.findProviders(cid, 1000, (err, response) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(response).to.have.length(1)
|
||||
expect(response[0].id.toB58String()).to.equal(provider)
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors when finding providers', (done) => {
|
||||
const cid = new CID('QmU621oD8AhHw6t25vVyfYKmL9VV3PTgc52FngEhTGACFB')
|
||||
const mockApi = nock('http://0.0.0.0:60197')
|
||||
.post('/api/v0/dht/findprovs')
|
||||
.query({
|
||||
arg: cid.toBaseEncodedString(),
|
||||
timeout: '30000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(502, 'Bad Gateway', [
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.contentRouting.findProviders(cid, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via the dht and a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => {
|
||||
createPeerInfo(cb)
|
||||
},
|
||||
// Create the node using the delegate
|
||||
(peerInfo, cb) => {
|
||||
delegate = new DelegatedContentRouter(peerInfo.id, {
|
||||
host: '0.0.0.0',
|
||||
protocol: 'http',
|
||||
port: 60197
|
||||
}, [
|
||||
ma('/ip4/0.0.0.0/tcp/60194')
|
||||
])
|
||||
nodeA = new Node({
|
||||
peerInfo,
|
||||
modules: {
|
||||
contentRouting: [ delegate ]
|
||||
},
|
||||
config: {
|
||||
relay: {
|
||||
enabled: true,
|
||||
hop: {
|
||||
enabled: true,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
}
|
||||
}
|
||||
})
|
||||
nodeA.start(cb)
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
|
||||
describe('provide', () => {
|
||||
it('should use both the dht and delegate router to provide', (done) => {
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'provide').callsFake(() => {})
|
||||
const delegateStub = sinon.stub(delegate, 'provide').callsFake(() => {
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.calledOnce).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.contentRouting.provide()
|
||||
})
|
||||
})
|
||||
|
||||
describe('findProviders', () => {
|
||||
it('should only use the dht if it finds providers', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, results)
|
||||
const delegateStub = sinon.stub(delegate, 'findProviders').throws(() => {
|
||||
return new Error('the delegate should not have been called')
|
||||
})
|
||||
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.notCalled).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should use the delegate if the dht fails to find providers', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, [])
|
||||
const delegateStub = sinon.stub(delegate, 'findProviders').callsArgWith(2, null, results)
|
||||
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.deep.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.calledOnce).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('no routers', () => {
|
||||
let nodeA
|
||||
before((done) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findProviders should return an error with no options', (done) => {
|
||||
nodeA.contentRouting.findProviders('a cid', (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findProviders should return an error with options', (done) => {
|
||||
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
104
test/create.spec.js
Normal file
104
test/create.spec.js
Normal file
@ -0,0 +1,104 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const series = require('async/series')
|
||||
const createNode = require('./utils/create-node')
|
||||
const sinon = require('sinon')
|
||||
|
||||
describe('libp2p creation', () => {
|
||||
it('should be able to start and stop successfully', (done) => {
|
||||
createNode([], {
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: true,
|
||||
pubsub: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
let sw = node._switch
|
||||
let cm = node.connectionManager
|
||||
let dht = node._dht
|
||||
let pub = node._floodSub
|
||||
|
||||
sinon.spy(sw, 'start')
|
||||
sinon.spy(cm, 'start')
|
||||
sinon.spy(dht, 'start')
|
||||
sinon.spy(dht.randomWalk, 'start')
|
||||
sinon.spy(pub, 'start')
|
||||
sinon.spy(sw, 'stop')
|
||||
sinon.spy(cm, 'stop')
|
||||
sinon.spy(dht, 'stop')
|
||||
sinon.spy(dht.randomWalk, 'stop')
|
||||
sinon.spy(pub, 'stop')
|
||||
sinon.spy(node, 'emit')
|
||||
|
||||
series([
|
||||
(cb) => node.start(cb),
|
||||
(cb) => {
|
||||
expect(sw.start.calledOnce).to.equal(true)
|
||||
expect(cm.start.calledOnce).to.equal(true)
|
||||
expect(dht.start.calledOnce).to.equal(true)
|
||||
expect(dht.randomWalk.start.calledOnce).to.equal(true)
|
||||
expect(pub.start.calledOnce).to.equal(true)
|
||||
expect(node.emit.calledWith('start')).to.equal(true)
|
||||
|
||||
cb()
|
||||
},
|
||||
(cb) => node.stop(cb)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(sw.stop.calledOnce).to.equal(true)
|
||||
expect(cm.stop.calledOnce).to.equal(true)
|
||||
expect(dht.stop.calledOnce).to.equal(true)
|
||||
expect(dht.randomWalk.stop.called).to.equal(true)
|
||||
expect(pub.stop.calledOnce).to.equal(true)
|
||||
expect(node.emit.calledWith('stop')).to.equal(true)
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should not create disabled modules', (done) => {
|
||||
createNode([], {
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: false,
|
||||
pubsub: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(node._dht).to.not.exist()
|
||||
expect(node._floodSub).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not throw errors from switch if node has no error listeners', (done) => {
|
||||
createNode([], {}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
node._switch.emit('error', new Error('bad things'))
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should emit errors from switch if node has error listeners', (done) => {
|
||||
const error = new Error('bad things')
|
||||
createNode([], {}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
node.once('error', (err) => {
|
||||
expect(err).to.eql(error)
|
||||
done()
|
||||
})
|
||||
node._switch.emit('error', error)
|
||||
})
|
||||
})
|
||||
})
|
170
test/dht.node.js
Normal file
170
test/dht.node.js
Normal file
@ -0,0 +1,170 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
|
||||
const MemoryStore = require('interface-datastore').MemoryDatastore
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
|
||||
describe('.dht', () => {
|
||||
describe('enabled', () => {
|
||||
let nodeA
|
||||
const datastore = new MemoryStore()
|
||||
|
||||
before(function (done) {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
datastore,
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
|
||||
// Rewrite validators
|
||||
nodeA._dht.validators.v = {
|
||||
func (key, publicKey, callback) {
|
||||
setImmediate(callback)
|
||||
},
|
||||
sign: false
|
||||
}
|
||||
|
||||
// Rewrite selectors
|
||||
nodeA._dht.selectors.v = () => 0
|
||||
|
||||
// Start
|
||||
nodeA.start(done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
nodeA.stop(done)
|
||||
})
|
||||
|
||||
it('should be able to dht.put a value to the DHT', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
const value = Buffer.from('value')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.get a value from the DHT with options', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.get(key, { maxTimeout: 3000 }, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.eql(value)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.get a value from the DHT with no options defined', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.get(key, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.eql(value)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.getMany a value from the DHT with options', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.getMany(key, 1, { maxTimeout: 3000 }, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to dht.getMany a value from the DHT with no options defined', (done) => {
|
||||
const key = Buffer.from('/v/hello')
|
||||
const value = Buffer.from('world')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
nodeA.dht.getMany(key, 1, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(res).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('disabled', () => {
|
||||
let nodeA
|
||||
|
||||
before(function (done) {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: false
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
nodeA.start(done)
|
||||
})
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
nodeA.stop(done)
|
||||
})
|
||||
|
||||
it('should receive an error on dht.put if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
const value = Buffer.from('value')
|
||||
|
||||
nodeA.dht.put(key, value, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should receive an error on dht.get if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
|
||||
nodeA.dht.get(key, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should receive an error on dht.getMany if the dht is disabled', (done) => {
|
||||
const key = Buffer.from('key')
|
||||
|
||||
nodeA.dht.getMany(key, 10, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
142
test/fsm.spec.js
Normal file
142
test/fsm.spec.js
Normal file
@ -0,0 +1,142 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
chai.use(require('chai-checkmark'))
|
||||
const expect = chai.expect
|
||||
const sinon = require('sinon')
|
||||
const series = require('async/series')
|
||||
const createNode = require('./utils/create-node')
|
||||
|
||||
describe('libp2p state machine (fsm)', () => {
|
||||
describe('starting and stopping', () => {
|
||||
let node
|
||||
beforeEach((done) => {
|
||||
createNode([], (err, _node) => {
|
||||
node = _node
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
afterEach(() => {
|
||||
node.removeAllListeners()
|
||||
})
|
||||
after((done) => {
|
||||
node.stop(done)
|
||||
node = null
|
||||
})
|
||||
|
||||
it('should be able to start and stop several times', (done) => {
|
||||
node.on('start', (err) => {
|
||||
expect(err).to.not.exist().mark()
|
||||
})
|
||||
node.on('stop', (err) => {
|
||||
expect(err).to.not.exist().mark()
|
||||
})
|
||||
|
||||
expect(4).checks(done)
|
||||
|
||||
series([
|
||||
(cb) => node.start(cb),
|
||||
(cb) => node.stop(cb),
|
||||
(cb) => node.start(cb),
|
||||
(cb) => node.stop(cb)
|
||||
], () => {})
|
||||
})
|
||||
|
||||
it('should noop when stopping a stopped node', (done) => {
|
||||
node.once('start', node.stop)
|
||||
node.once('stop', () => {
|
||||
node.state.on('STOPPING', () => {
|
||||
throw new Error('should not stop a stopped node')
|
||||
})
|
||||
node.once('stop', done)
|
||||
|
||||
// stop the stopped node
|
||||
node.stop()
|
||||
})
|
||||
node.start()
|
||||
})
|
||||
|
||||
it('should noop when starting a started node', (done) => {
|
||||
node.once('start', () => {
|
||||
node.state.on('STARTING', () => {
|
||||
throw new Error('should not start a started node')
|
||||
})
|
||||
node.once('start', () => {
|
||||
node.once('stop', done)
|
||||
node.stop()
|
||||
})
|
||||
|
||||
// start the started node
|
||||
node.start()
|
||||
})
|
||||
node.start()
|
||||
})
|
||||
|
||||
it('should error on start with no transports', (done) => {
|
||||
let transports = node._modules.transport
|
||||
node._modules.transport = null
|
||||
|
||||
node.on('stop', () => {
|
||||
node._modules.transport = transports
|
||||
expect(node._modules.transport).to.exist().mark()
|
||||
})
|
||||
node.on('error', (err) => {
|
||||
expect(err).to.exist().mark()
|
||||
})
|
||||
node.on('start', () => {
|
||||
throw new Error('should not start')
|
||||
})
|
||||
|
||||
expect(2).checks(done)
|
||||
|
||||
node.start()
|
||||
})
|
||||
|
||||
it('should not start if the switch fails to start', (done) => {
|
||||
const error = new Error('switch didnt start')
|
||||
const stub = sinon.stub(node._switch, 'start')
|
||||
.callsArgWith(0, error)
|
||||
|
||||
node.on('stop', () => {
|
||||
expect(stub.calledOnce).to.eql(true).mark()
|
||||
stub.restore()
|
||||
})
|
||||
node.on('error', (err) => {
|
||||
expect(err).to.eql(error).mark()
|
||||
})
|
||||
node.on('start', () => {
|
||||
throw new Error('should not start')
|
||||
})
|
||||
|
||||
expect(2).checks(done)
|
||||
|
||||
node.start()
|
||||
})
|
||||
|
||||
it('should not dial when the node is stopped', (done) => {
|
||||
node.on('stop', () => {
|
||||
node.dial(null, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql('ERR_NODE_NOT_STARTED')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
node.stop()
|
||||
})
|
||||
|
||||
it('should not dial (fsm) when the node is stopped', (done) => {
|
||||
node.on('stop', () => {
|
||||
node.dialFSM(null, null, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql('ERR_NODE_NOT_STARTED')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
node.stop()
|
||||
})
|
||||
})
|
||||
})
|
34
test/get-peer-info.spec.js
Normal file
34
test/get-peer-info.spec.js
Normal file
@ -0,0 +1,34 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
|
||||
const getPeerInfo = require('../src/get-peer-info')
|
||||
|
||||
describe('getPeerInfo', () => {
|
||||
it('should callback with error for invalid string multiaddr', (done) => {
|
||||
getPeerInfo(null)('INVALID MULTIADDR', (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql('ERR_INVALID_MULTIADDR')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should callback with error for invalid non-peer multiaddr', (done) => {
|
||||
getPeerInfo(null)('/ip4/8.8.8.8/tcp/1080', (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.equal('ERR_INVALID_MULTIADDR')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should callback with error for invalid non-peer multiaddr', (done) => {
|
||||
getPeerInfo(null)(undefined, (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql('ERR_INVALID_PEER_TYPE')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
@ -29,10 +29,12 @@ describe('multiaddr trim', () => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
const multiaddrs = node.peerInfo.multiaddrs.toArray()
|
||||
expect(multiaddrs.length).to.be.at.least(2)
|
||||
// ensure the p2p-webrtc-direct address has been trimmed
|
||||
multiaddrs.forEach((addr) => {
|
||||
expect(() => addr.decapsulate('/ip4/0.0.0.0/tcp/999/wss/p2p-webrtc-direct')).to.throw()
|
||||
})
|
||||
|
||||
expect(multiaddrs.length).to.at.least(2)
|
||||
expect(multiaddrs[0].toString())
|
||||
.to.match(/^\/ip4\/127\.0\.0\.1\/tcp\/[0-9]+\/ws\/ipfs\/\w+$/)
|
||||
node.stop(done)
|
||||
})
|
||||
})
|
||||
|
@ -1,11 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
require('./pnet.node')
|
||||
require('./transports.node')
|
||||
require('./stream-muxing.node')
|
||||
require('./peer-discovery.node')
|
||||
require('./pubsub.node')
|
||||
require('./peer-routing.node')
|
||||
require('./ping.node')
|
||||
require('./pubsub.node')
|
||||
require('./content-routing.node')
|
||||
require('./circuit-relay.node')
|
||||
require('./multiaddr-trim.node')
|
||||
require('./stats')
|
||||
require('./dht.node')
|
||||
|
@ -8,19 +8,21 @@ chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const parallel = require('async/parallel')
|
||||
const _times = require('lodash.times')
|
||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||
const sinon = require('sinon')
|
||||
const nock = require('nock')
|
||||
|
||||
const createNode = require('./utils/create-node')
|
||||
|
||||
describe('.peerRouting', () => {
|
||||
describe('via the dht', () => {
|
||||
let nodeA
|
||||
let nodeB
|
||||
let nodeC
|
||||
let nodeD
|
||||
let nodeE
|
||||
|
||||
before(function (done) {
|
||||
this.timeout(5 * 1000)
|
||||
|
||||
before('create the outer ring of connections', (done) => {
|
||||
const tasks = _times(5, () => (cb) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
config: {
|
||||
@ -48,7 +50,11 @@ describe('.peerRouting', () => {
|
||||
(cb) => nodeC.dial(nodeD.peerInfo, cb),
|
||||
(cb) => nodeD.dial(nodeE.peerInfo, cb),
|
||||
(cb) => nodeE.dial(nodeA.peerInfo, cb)
|
||||
], done)
|
||||
], (err) => {
|
||||
expect(err).to.not.exist()
|
||||
// Give the kbucket time to fill in the dht
|
||||
setTimeout(done, 250)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -62,33 +68,24 @@ describe('.peerRouting', () => {
|
||||
], done)
|
||||
})
|
||||
|
||||
describe('el ring', () => {
|
||||
it('let kbucket get filled', (done) => {
|
||||
setTimeout(() => done(), 250)
|
||||
})
|
||||
|
||||
it('nodeA.dial by Id to node C', (done) => {
|
||||
nodeA.dial(nodeC.peerInfo.id, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
it('should use the nodes dht', (done) => {
|
||||
const stub = sinon.stub(nodeA._dht, 'findPeer').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
|
||||
nodeA.peerRouting.findPeer()
|
||||
})
|
||||
|
||||
it('nodeB.dial by Id to node D', (done) => {
|
||||
nodeB.dial(nodeD.peerInfo.id, (err) => {
|
||||
describe('connected in an el ring', () => {
|
||||
it('should be able to find a peer we are not directly connected to', (done) => {
|
||||
parallel([
|
||||
(cb) => nodeA.dial(nodeC.peerInfo.id, cb),
|
||||
(cb) => nodeB.dial(nodeD.peerInfo.id, cb),
|
||||
(cb) => nodeC.dial(nodeE.peerInfo.id, cb)
|
||||
], (err) => {
|
||||
if (err) throw err
|
||||
expect(err).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeC.dial by Id to node E', (done) => {
|
||||
nodeC.dial(nodeE.peerInfo.id, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('nodeB.peerRouting.findPeer(nodeE.peerInfo.id)', (done) => {
|
||||
nodeB.peerRouting.findPeer(nodeE.peerInfo.id, (err, peerInfo) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(nodeE.peerInfo.id.toB58String()).to.equal(peerInfo.id.toB58String())
|
||||
@ -97,3 +94,201 @@ describe('.peerRouting', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
parallel([
|
||||
// Create the node using the delegate
|
||||
(cb) => {
|
||||
delegate = new DelegatedPeerRouter({
|
||||
host: 'ipfs.io',
|
||||
protocol: 'https',
|
||||
port: '443'
|
||||
})
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
modules: {
|
||||
peerRouting: [ delegate ]
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
nodeA.start(cb)
|
||||
})
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
afterEach(() => nock.cleanAll())
|
||||
|
||||
it('should use the delegate router to find peers', (done) => {
|
||||
const stub = sinon.stub(delegate, 'findPeer').callsFake(() => {
|
||||
stub.restore()
|
||||
done()
|
||||
})
|
||||
nodeA.peerRouting.findPeer()
|
||||
})
|
||||
|
||||
it('should be able to find a peer', (done) => {
|
||||
const peerKey = 'QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL'
|
||||
const mockApi = nock('https://ipfs.io')
|
||||
.post('/api/v0/dht/findpeer')
|
||||
.query({
|
||||
arg: peerKey,
|
||||
timeout: '30000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, `{"Extra":"","ID":"some other id","Responses":null,"Type":0}\n{"Extra":"","ID":"","Responses":[{"Addrs":["/ip4/127.0.0.1/tcp/4001"],"ID":"${peerKey}"}],"Type":2}\n`, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.peerRouting.findPeer(peerKey, (err, peerInfo) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(peerInfo.id.toB58String()).to.equal(peerKey)
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should error when a peer cannot be found', (done) => {
|
||||
const peerKey = 'key of a peer not on the network'
|
||||
const mockApi = nock('https://ipfs.io')
|
||||
.post('/api/v0/dht/findpeer')
|
||||
.query({
|
||||
arg: peerKey,
|
||||
timeout: '30000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(200, `{"Extra":"","ID":"some other id","Responses":null,"Type":6}\n{"Extra":"","ID":"yet another id","Responses":null,"Type":0}\n{"Extra":"routing:not found","ID":"","Responses":null,"Type":3}\n`, [
|
||||
'Content-Type', 'application/json',
|
||||
'X-Chunked-Output', '1'
|
||||
])
|
||||
|
||||
nodeA.peerRouting.findPeer(peerKey, (err, peerInfo) => {
|
||||
expect(err).to.exist()
|
||||
expect(peerInfo).to.not.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle errors from the api', (done) => {
|
||||
const peerKey = 'key of a peer not on the network'
|
||||
const mockApi = nock('https://ipfs.io')
|
||||
.post('/api/v0/dht/findpeer')
|
||||
.query({
|
||||
arg: peerKey,
|
||||
timeout: '30000ms',
|
||||
'stream-channels': true
|
||||
})
|
||||
.reply(502)
|
||||
|
||||
nodeA.peerRouting.findPeer(peerKey, (err, peerInfo) => {
|
||||
expect(err).to.exist()
|
||||
expect(peerInfo).to.not.exist()
|
||||
expect(mockApi.isDone()).to.equal(true)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('via the dht and a delegate', () => {
|
||||
let nodeA
|
||||
let delegate
|
||||
|
||||
before((done) => {
|
||||
parallel([
|
||||
// Create the node using the delegate
|
||||
(cb) => {
|
||||
delegate = new DelegatedPeerRouter({
|
||||
host: 'ipfs.io',
|
||||
protocol: 'https',
|
||||
port: '443'
|
||||
})
|
||||
createNode('/ip4/0.0.0.0/tcp/0', {
|
||||
modules: {
|
||||
peerRouting: [ delegate ]
|
||||
},
|
||||
config: {
|
||||
EXPERIMENTAL: {
|
||||
dht: true
|
||||
}
|
||||
}
|
||||
}, (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
nodeA.start(cb)
|
||||
})
|
||||
}
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => nodeA.stop(done))
|
||||
|
||||
describe('findPeer', () => {
|
||||
it('should only use the dht if it finds the peer', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findPeer').callsArgWith(2, null, results)
|
||||
const delegateStub = sinon.stub(delegate, 'findPeer').throws(() => {
|
||||
return new Error('the delegate should not have been called')
|
||||
})
|
||||
|
||||
nodeA.peerRouting.findPeer('a peer id', (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.notCalled).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should use the delegate if the dht fails to find the peer', (done) => {
|
||||
const results = [true]
|
||||
const dhtStub = sinon.stub(nodeA._dht, 'findPeer').callsArgWith(2, null, undefined)
|
||||
const delegateStub = sinon.stub(delegate, 'findPeer').callsArgWith(2, null, results)
|
||||
|
||||
nodeA.peerRouting.findPeer('a peer id', (err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.deep.equal(results)
|
||||
expect(dhtStub.calledOnce).to.equal(true)
|
||||
expect(delegateStub.calledOnce).to.equal(true)
|
||||
delegateStub.restore()
|
||||
dhtStub.restore()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('no routers', () => {
|
||||
let nodeA
|
||||
before((done) => {
|
||||
createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findPeer should return an error with no options', (done) => {
|
||||
nodeA.peerRouting.findPeer('a cid', (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('.findPeer should return an error with options', (done) => {
|
||||
nodeA.peerRouting.findPeer('a cid', { maxTimeout: 5000 }, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
61
test/ping.node.js
Normal file
61
test/ping.node.js
Normal file
@ -0,0 +1,61 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const parallel = require('async/parallel')
|
||||
|
||||
const createNode = require('./utils/create-node.js')
|
||||
const echo = require('./utils/echo')
|
||||
|
||||
describe('ping', () => {
|
||||
let nodeA
|
||||
let nodeB
|
||||
|
||||
before((done) => {
|
||||
parallel([
|
||||
(cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeA = node
|
||||
node.handle('/echo/1.0.0', echo)
|
||||
node.start(cb)
|
||||
}),
|
||||
(cb) => createNode('/ip4/0.0.0.0/tcp/0', (err, node) => {
|
||||
expect(err).to.not.exist()
|
||||
nodeB = node
|
||||
node.handle('/echo/1.0.0', echo)
|
||||
node.start(cb)
|
||||
})
|
||||
], done)
|
||||
})
|
||||
|
||||
after((done) => {
|
||||
parallel([
|
||||
(cb) => nodeA.stop(cb),
|
||||
(cb) => nodeB.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('should be able to ping another node', (done) => {
|
||||
nodeA.ping(nodeB.peerInfo, (err, ping) => {
|
||||
expect(err).to.not.exist()
|
||||
ping.once('ping', (time) => {
|
||||
expect(time).to.exist()
|
||||
ping.stop()
|
||||
done()
|
||||
})
|
||||
|
||||
ping.start()
|
||||
})
|
||||
})
|
||||
|
||||
it('should be not be able to ping when stopped', (done) => {
|
||||
nodeA.stop(() => {
|
||||
nodeA.ping(nodeB.peerInfo, (err) => {
|
||||
expect(err).to.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
90
test/pnet.node.js
Normal file
90
test/pnet.node.js
Normal file
@ -0,0 +1,90 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const waterfall = require('async/waterfall')
|
||||
const WS = require('libp2p-websockets')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
const Libp2p = require('../src')
|
||||
|
||||
describe('private network', () => {
|
||||
let config
|
||||
|
||||
before((done) => {
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 512 }, cb),
|
||||
(peerId, cb) => PeerInfo.create(peerId, cb),
|
||||
(peerInfo, cb) => {
|
||||
config = {
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [ WS ]
|
||||
}
|
||||
}
|
||||
cb()
|
||||
}
|
||||
], () => done())
|
||||
})
|
||||
|
||||
describe('enforced network protection', () => {
|
||||
before(() => {
|
||||
process.env.LIBP2P_FORCE_PNET = 1
|
||||
})
|
||||
|
||||
after(() => {
|
||||
delete process.env.LIBP2P_FORCE_PNET
|
||||
})
|
||||
|
||||
it('should throw an error without a provided protector', () => {
|
||||
expect(() => {
|
||||
return new Libp2p(config)
|
||||
}).to.throw('Private network is enforced, but no protector was provided')
|
||||
})
|
||||
|
||||
it('should create a libp2p node with a provided protector', () => {
|
||||
let node
|
||||
let protector = {
|
||||
psk: '123',
|
||||
tag: '/psk/1.0.0',
|
||||
protect: () => { }
|
||||
}
|
||||
|
||||
expect(() => {
|
||||
let options = defaultsDeep(config, {
|
||||
modules: {
|
||||
connProtector: protector
|
||||
}
|
||||
})
|
||||
|
||||
node = new Libp2p(options)
|
||||
return node
|
||||
}).to.not.throw()
|
||||
expect(node._switch.protector).to.deep.equal(protector)
|
||||
})
|
||||
|
||||
it('should throw an error if the protector does not have a protect method', () => {
|
||||
expect(() => {
|
||||
let options = defaultsDeep(config, {
|
||||
modules: {
|
||||
connProtector: { }
|
||||
}
|
||||
})
|
||||
|
||||
return new Libp2p(options)
|
||||
}).to.throw()
|
||||
})
|
||||
})
|
||||
|
||||
describe('network protection not enforced', () => {
|
||||
it('should not throw an error with no provided protector', () => {
|
||||
expect(() => {
|
||||
return new Libp2p(config)
|
||||
}).to.not.throw()
|
||||
})
|
||||
})
|
||||
})
|
@ -28,8 +28,6 @@ function teardown (nodeA, nodeB, callback) {
|
||||
|
||||
describe('stream muxing', () => {
|
||||
it('spdy only', function (done) {
|
||||
this.timeout(5 * 1000)
|
||||
|
||||
let nodeA
|
||||
let nodeB
|
||||
|
||||
@ -217,7 +215,7 @@ describe('stream muxing', () => {
|
||||
|
||||
nodeA.dial(nodeB.peerInfo, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
})
|
||||
},
|
||||
|
@ -102,7 +102,7 @@ describe('transports', () => {
|
||||
function check () {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
@ -142,12 +142,49 @@ describe('transports', () => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(err).to.not.exist()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('.dialFSM check conn and close', (done) => {
|
||||
nodeA.dialFSM(peerB, (err, connFSM) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
connFSM.once('muxed', () => {
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(peerB.id.toB58String())
|
||||
).to.have.length(1)
|
||||
|
||||
connFSM.once('error', done)
|
||||
connFSM.once('close', () => {
|
||||
// ensure the connection is closed
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(peerB.id.toB58String())
|
||||
).to.have.length(0)
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
connFSM.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('.dialFSM with a protocol, do an echo and close', (done) => {
|
||||
nodeA.dialFSM(peerB, '/echo/1.0.0', (err, connFSM) => {
|
||||
expect(err).to.not.exist()
|
||||
connFSM.once('connection', (conn) => {
|
||||
tryEcho(conn, () => {
|
||||
connFSM.close()
|
||||
})
|
||||
})
|
||||
connFSM.once('error', done)
|
||||
connFSM.once('close', done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('stress', () => {
|
||||
it('one big write', (done) => {
|
||||
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
|
||||
@ -168,7 +205,9 @@ describe('transports', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('many writes', (done) => {
|
||||
it('many writes', function (done) {
|
||||
this.timeout(10000)
|
||||
|
||||
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
@ -192,12 +231,22 @@ describe('transports', () => {
|
||||
})
|
||||
|
||||
describe('webrtc-star', () => {
|
||||
/* eslint-disable-next-line no-console */
|
||||
if (!w.support) { return console.log('NO WEBRTC SUPPORT') }
|
||||
|
||||
let peer1
|
||||
let peer2
|
||||
let node1
|
||||
let node2
|
||||
let node3
|
||||
|
||||
after((done) => {
|
||||
parallel([
|
||||
(cb) => node1.stop(cb),
|
||||
(cb) => node2.stop(cb),
|
||||
(cb) => node3.stop(cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('create two peerInfo with webrtc-star addrs', (done) => {
|
||||
parallel([
|
||||
@ -264,36 +313,33 @@ describe('transports', () => {
|
||||
function check () {
|
||||
const peers = node1.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(node1._switch.muxedConns)).to.have.length(0)
|
||||
expect(node1._switch.connection.getAll()).to.have.length(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('create a third node and check that discovery works', function (done) {
|
||||
this.timeout(60 * 1000)
|
||||
|
||||
let counter = 0
|
||||
|
||||
function check () {
|
||||
if (++counter === 3) {
|
||||
expect(Object.keys(node1._switch.muxedConns).length).to.equal(1)
|
||||
expect(Object.keys(node2._switch.muxedConns).length).to.equal(1)
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
it('create a third node and check that discovery works', (done) => {
|
||||
PeerId.create({ bits: 512 }, (err, id3) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
const b58Id = id3.toB58String()
|
||||
|
||||
function check () {
|
||||
// Verify both nodes are connected to node 3
|
||||
if (node1._switch.connection.getAllById(b58Id) && node2._switch.connection.getAllById(b58Id)) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
const peer3 = new PeerInfo(id3)
|
||||
const ma3 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + id3.toB58String()
|
||||
const ma3 = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star/ipfs/' + b58Id
|
||||
peer3.multiaddrs.add(ma3)
|
||||
|
||||
node1.on('peer:discovery', (peerInfo) => node1.dial(peerInfo, check))
|
||||
node2.on('peer:discovery', (peerInfo) => node2.dial(peerInfo, check))
|
||||
|
||||
const node3 = new Node({
|
||||
node3 = new Node({
|
||||
peerInfo: peer3
|
||||
})
|
||||
node3.start(check)
|
||||
@ -372,7 +418,7 @@ describe('transports', () => {
|
||||
function check () {
|
||||
const peers = node1.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(node1._switch.muxedConns)).to.have.length(0)
|
||||
expect(node1._switch.connection.getAll()).to.have.length(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
@ -385,8 +431,8 @@ describe('transports', () => {
|
||||
|
||||
function check () {
|
||||
if (++counter === 3) {
|
||||
expect(Object.keys(node1._switch.muxedConns).length).to.equal(1)
|
||||
expect(Object.keys(node2._switch.muxedConns).length).to.equal(1)
|
||||
expect(node1._switch.connection.getAll()).to.have.length(1)
|
||||
expect(node2._switch.connection.getAll()).to.have.length(1)
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
@ -91,14 +91,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeB.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
|
||||
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeB._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -117,15 +116,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeB.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
}
|
||||
], () => tryEcho(conn, done))
|
||||
@ -143,15 +140,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeB.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
|
||||
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeB._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -170,13 +165,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeB.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
}
|
||||
], () => tryEcho(conn, done))
|
||||
@ -185,7 +180,7 @@ describe('transports', () => {
|
||||
})
|
||||
|
||||
it('nodeA.hangUp nodeB using PeerId (third)', (done) => {
|
||||
nodeA.hangUp(nodeB.peerInfo.multiaddrs.toArray()[0], (err) => {
|
||||
nodeA.hangUp(nodeB.peerInfo.id, (err) => {
|
||||
expect(err).to.not.exist()
|
||||
setTimeout(check, 500)
|
||||
|
||||
@ -194,19 +189,64 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeB.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeB._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeB._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('.dialFSM check conn and close', (done) => {
|
||||
nodeA.dialFSM(nodeB.peerInfo, (err, connFSM) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
connFSM.once('muxed', () => {
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(nodeB.peerInfo.id.toB58String())
|
||||
).to.have.length(1)
|
||||
|
||||
connFSM.once('error', done)
|
||||
connFSM.once('close', () => {
|
||||
// ensure the connection is closed
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(nodeB.peerInfo.id.toB58String())
|
||||
).to.have.length(0)
|
||||
done()
|
||||
})
|
||||
|
||||
connFSM.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('.dialFSM with a protocol, do an echo and close', (done) => {
|
||||
nodeA.dialFSM(nodeB.peerInfo, '/echo/1.0.0', (err, connFSM) => {
|
||||
expect(err).to.not.exist()
|
||||
connFSM.once('connection', (conn) => {
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(nodeB.peerInfo.id.toB58String())
|
||||
).to.have.length(1)
|
||||
tryEcho(conn, () => {
|
||||
connFSM.close()
|
||||
})
|
||||
})
|
||||
connFSM.once('error', done)
|
||||
connFSM.once('close', () => {
|
||||
// ensure the connection is closed
|
||||
expect(
|
||||
nodeA._switch.connection.getAllById(nodeB.peerInfo.id.toB58String())
|
||||
).to.have.length(0)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('TCP + WebSockets', () => {
|
||||
@ -264,13 +304,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeTCP.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeTCP._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeTCP._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeTCPnWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeTCPnWS._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -288,14 +328,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeTCP.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeTCP._switch.muxedConns)).to.have.length(0)
|
||||
|
||||
expect(nodeTCP._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeTCPnWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeTCPnWS._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -315,13 +354,13 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeTCPnWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(2)
|
||||
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeTCPnWS._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeWS._switch.muxedConns)).to.have.length(1)
|
||||
expect(nodeWS._switch.connection.getAll()).to.have.length(1)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -339,14 +378,14 @@ describe('transports', () => {
|
||||
(cb) => {
|
||||
const peers = nodeTCPnWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(2)
|
||||
expect(Object.keys(nodeTCPnWS._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeTCPnWS._switch.connection.getAll()).to.have.length(0)
|
||||
|
||||
cb()
|
||||
},
|
||||
(cb) => {
|
||||
const peers = nodeWS.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeWS._switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeWS._switch.connection.getAll()).to.have.length(0)
|
||||
cb()
|
||||
}
|
||||
], done)
|
||||
@ -471,7 +510,7 @@ describe('transports', () => {
|
||||
let i = 1;
|
||||
[nodeAll, otherNode].forEach((node) => {
|
||||
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
||||
expect(Object.keys(node._switch.muxedConns)).to.have.length(muxed)
|
||||
expect(node._switch.connection.getAll()).to.have.length(muxed)
|
||||
})
|
||||
callback()
|
||||
}
|
||||
@ -633,7 +672,7 @@ describe('transports', () => {
|
||||
let i = 1;
|
||||
[nodeAll, otherNode].forEach((node) => {
|
||||
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
||||
expect(Object.keys(node._switch.muxedConns)).to.have.length(muxed)
|
||||
expect(node._switch.connection.getAll()).to.have.length(muxed)
|
||||
})
|
||||
done()
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ describe('Turbolence tests', () => {
|
||||
}
|
||||
})
|
||||
|
||||
/* eslint-disable-next-line no-console */
|
||||
nodeSpawn.stderr.on('data', (data) => console.log(data.toString()))
|
||||
})
|
||||
|
||||
@ -76,7 +77,7 @@ describe('Turbolence tests', () => {
|
||||
function check () {
|
||||
const peers = nodeA.peerBook.getAll()
|
||||
expect(Object.keys(peers)).to.have.length(1)
|
||||
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||
expect(nodeA._switch.connection.getAll()).to.have.length(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
|
@ -3,7 +3,7 @@
|
||||
const WS = require('libp2p-websockets')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
@ -78,6 +78,10 @@ class Node extends libp2p {
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
enabledDiscovery: true
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: false,
|
||||
pubsub: false
|
||||
|
@ -3,7 +3,7 @@
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-railing')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
@ -72,7 +72,8 @@ class Node extends libp2p {
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
kBucketSize: 20,
|
||||
enabledDiscovery: true
|
||||
},
|
||||
EXPERIMENTAL: {
|
||||
dht: false,
|
||||
|
@ -21,8 +21,7 @@ function createNode (multiaddrs, options, callback) {
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 512 }, cb),
|
||||
(peerId, cb) => PeerInfo.create(peerId, cb),
|
||||
(cb) => createPeerInfo(cb),
|
||||
(peerInfo, cb) => {
|
||||
multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma))
|
||||
options.peerInfo = peerInfo
|
||||
@ -31,4 +30,12 @@ function createNode (multiaddrs, options, callback) {
|
||||
], callback)
|
||||
}
|
||||
|
||||
function createPeerInfo (callback) {
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 512 }, cb),
|
||||
(peerId, cb) => PeerInfo.create(peerId, cb)
|
||||
], callback)
|
||||
}
|
||||
|
||||
module.exports = createNode
|
||||
module.exports.createPeerInfo = createPeerInfo
|
||||
|
Reference in New Issue
Block a user