Compare commits

..

104 Commits

Author SHA1 Message Date
babb90fe17 chore: release version v0.8.0 2017-03-31 16:19:24 +01:00
2f9070a725 chore: update contributors 2017-03-31 16:19:24 +01:00
a48ec65961 Merge pull request #85 from libp2p/feat/advanced-peer-book
fix: correct method on peer-book
2017-03-31 16:18:37 +01:00
031ecb3fe7 fix: correct method on peer-book 2017-03-31 16:17:59 +01:00
c6bdd869be Merge pull request #76 from libp2p/feat/advanced-peer-book
fix: avoid deleting nodes from peerBook
2017-03-31 16:17:18 +01:00
dee0340806 chore: update to latest peer-info, peer-book and libp2p-swarm 2017-03-31 15:52:20 +01:00
a4b41b0f9a feat: not remove peer from peerBook on disconnect 2017-03-30 07:20:45 +01:00
291e79fc99 fix: addition of ipfs id appendix must come before transport filtering 2017-03-29 21:02:37 +01:00
59ea9c388f feat: append peer id to multiaddr if not there 2017-03-29 20:02:54 +01:00
300936f2f3 fix: avoid deleting nodes from peerBook 2017-03-29 18:03:00 +01:00
9f4ec2a915 chore: release version v0.7.0 2017-03-29 08:03:58 +01:00
c7f4368007 chore: update contributors 2017-03-29 08:03:58 +01:00
9ff04779f5 Merge pull request #75 from libp2p/feat/update-peer-events
feat: Feat/update peer events
2017-03-29 08:03:13 +01:00
f73c045767 feat: update events to conform with #74 2017-03-29 07:32:46 +01:00
8840c9b250 docs: document new events 2017-03-29 07:32:29 +01:00
df4c99df4a chore: release version v0.6.2 2017-03-28 12:44:52 +01:00
6e05a0b239 chore: update contributors 2017-03-28 12:44:52 +01:00
c781abc30c chore: deps 2017-03-28 12:43:00 +01:00
6a66931ba4 chore: release version v0.6.1 2017-03-27 16:48:53 +01:00
426fc27b26 chore: update contributors 2017-03-27 16:48:52 +01:00
d6325dee90 chore: update deps 2017-03-27 16:48:41 +01:00
f74bc0596d chore: release version v0.6.0 2017-03-27 15:48:46 +01:00
8f9e82b0fb chore: update contributors 2017-03-27 15:48:46 +01:00
9724fa190e chore: update deps 2017-03-27 15:46:10 +01:00
721da9ab41 Merge pull request #64 from libp2p/feat/new-api
feat: Formalising the API
2017-03-27 15:45:41 +01:00
d27bd2b912 cr 2017-03-27 15:43:08 +01:00
2c23d9a718 fix: last touches 2017-03-27 14:42:05 +01:00
a6623c1ba2 feat: new super simplified API 2017-03-27 12:26:34 +01:00
5d4d94e75f docs: finish API documentation 2017-03-24 18:20:14 +00:00
a4b97b9627 docs: libp2p API 2017-03-24 17:07:08 +00:00
3584209947 docs: update readme, add base class extending example and start listing API calls 2017-03-24 16:02:57 +00:00
b5209fc456 chore: release version v0.5.5 2017-03-21 17:06:34 +01:00
932d8772da chore: update contributors 2017-03-21 17:06:33 +01:00
baee2b7945 chore(deps): update 2017-03-21 17:04:56 +01:00
153f8884fa Merge pull request #54 from libp2p/greenkeeper-aegir-10.0.0
Update aegir to version 10.0.0 🚀
2017-02-13 18:05:01 -08:00
b71f77dcf7 chore: release version v0.5.4 2017-02-10 23:19:27 -08:00
8dc8e0530b chore: update contributors 2017-02-10 23:19:27 -08:00
4ed0ce16af chore: update deps 2017-02-10 23:17:30 -08:00
cbb7290a97 chore: release version v0.5.3 2017-02-10 19:28:41 -08:00
63b10c01e3 chore: update contributors 2017-02-10 19:28:41 -08:00
11c21b6999 fix: use async.setImmediate 2017-02-10 19:28:34 -08:00
109ee51d7b chore: release version v0.5.2 2017-02-09 10:18:38 -08:00
09182b43bf chore: update contributors 2017-02-09 10:18:38 -08:00
a130ab2dc1 chore: ^ to ~ 2017-02-09 10:16:51 -08:00
13355bf179 chore(package): update aegir to version 10.0.0
https://greenkeeper.io/
2017-02-07 18:45:33 +01:00
e24a1e43dc chore: release version v0.5.1 2017-01-28 21:20:21 +00:00
cb7d60ef65 chore: update contributors 2017-01-28 21:20:20 +00:00
a01b9479eb fix: remove unnecessary log 2017-01-28 21:20:16 +00:00
d01876a3d1 chore: release version v0.5.0 2017-01-28 21:14:26 +00:00
eb5eed8b21 chore: update contributors 2017-01-28 21:14:26 +00:00
3c58613b69 chore: release version v0.4.0 2017-01-28 21:14:04 +00:00
5dd5d37f6a feat: new discovery interface 2017-01-28 21:14:04 +00:00
6c3dd4b780 chore: update deps 2017-01-28 21:14:04 +00:00
06913271c7 docs(readme): fix typos 2017-01-16 12:01:09 +01:00
2cdd7033f0 add options 2016-12-11 12:03:21 -08:00
c2cf1da2ce Merge pull request #50 from libp2p/greenkeeper-aegir-9.2.1 2016-12-10 12:00:26 -08:00
8d0124ee5c chore(package): update aegir to version 9.2.1
https://greenkeeper.io/
2016-12-09 13:25:07 +01:00
078956b310 chore: release version v0.3.1 2016-12-01 12:17:33 +00:00
4f44158889 chore: update contributors 2016-12-01 12:17:32 +00:00
3a8d51c142 Merge pull request #48 from libp2p/feat/add-ping
feat: add ping to the mix
2016-12-01 12:17:10 +00:00
f8c09db20f feat: add ping to the mix 2016-12-01 11:53:27 +00:00
ee801c9be2 chore: release version v0.3.0 2016-11-28 10:40:29 +00:00
03349d7caf chore: update contributors 2016-11-28 10:40:29 +00:00
0c87dbc8f3 feat: export Node class at the top level 2016-11-28 10:38:40 +00:00
c4abbaa2f9 chore: release version v0.2.1 2016-11-27 18:56:25 +00:00
21759f1204 chore: update contributors 2016-11-27 18:56:25 +00:00
0830827c06 docs: add links to readme 2016-11-27 18:54:30 +00:00
8f31d1cb17 docs: update package list 2016-11-27 18:48:01 +00:00
fc43b99836 chore: release version v0.2.0 2016-11-27 18:41:13 +00:00
4027c3cca4 chore: update contributors 2016-11-27 18:41:13 +00:00
049c7ce347 docs: readme badges 2016-11-27 18:30:37 +00:00
54b65b3cbc Merge pull request #43 from libp2p/feat/revamp
WIP: create the libp2p base class
2016-11-27 19:29:06 +01:00
3db3dca98b docs: update examples folder 2016-11-27 18:23:50 +00:00
40fdc4147c feat: support websockets, even if no listener is added 2016-11-27 18:20:25 +00:00
e10ec13c33 feat: add the CI stuffs 2016-11-26 03:12:30 +01:00
5d0b25cb00 feat: create base libp2p.Node class 2016-11-26 03:07:52 +01:00
57c85fe346 chore: update oldie package.json 2016-11-25 20:08:05 +00:00
d7eb5b7abc docs: overall to the README, lots of more updated information 2016-11-25 20:07:45 +00:00
b5608f0ef8 Merge pull request #39 from libp2p/greenkeeper-libp2p-swarm-0.26.0
Update libp2p-swarm to version 0.26.0 🚀
2016-11-07 15:53:16 +00:00
b423a1c583 chore(package): update libp2p-swarm to version 0.26.0
https://greenkeeper.io/
2016-11-03 12:42:11 +01:00
0f7bde7bc3 Merge pull request #29 from libp2p/feat/remove-below
Remove word below
2016-10-14 10:55:39 -04:00
fecb41b275 readme 2016-09-14 01:34:15 -04:00
14d14ff0b6 Merge pull request #30 from libp2p/readme
update the roadmap
2016-09-14 01:33:14 -04:00
af493363aa add images 2016-09-14 01:31:47 -04:00
fe2d097bfa update the roadmap 2016-09-14 01:29:30 -04:00
3cf54db8d5 Remove word below
It doesnt belong
2016-09-13 17:23:47 -04:00
ed9d0e5a42 Merge pull request #27 from libp2p/greenkeeper-multiaddr-2.0.3
Update multiaddr to version 2.0.3 🚀
2016-09-07 20:24:13 -04:00
8486f04c55 Merge pull request #28 from libp2p/greenkeeper-peer-info-0.7.1
Update peer-info to version 0.7.1 🚀
2016-09-07 20:24:06 -04:00
012433b5d5 chore(package): update peer-info to version 0.7.1
https://greenkeeper.io/
2016-09-08 01:37:08 +02:00
48fcf7ee90 chore(package): update multiaddr to version 2.0.3
https://greenkeeper.io/
2016-09-08 01:20:14 +02:00
517723a093 Merge pull request #22 from libp2p/feature/standardize-readme
Standardized Readme
2016-09-07 14:19:40 -04:00
55223d528c Merge pull request #23 from libp2p/greenkeeper-libp2p-tcp-0.8.0
Update libp2p-tcp to version 0.8.0 🚀
2016-09-07 14:19:15 -04:00
f6f96d3b5f chore(package): update libp2p-tcp to version 0.8.0
https://greenkeeper.io/
2016-09-06 14:56:54 +02:00
3c93b7cd4e Standardized Readme
Thoughts?
2016-09-02 15:22:28 -04:00
c72ce8a7fb Merge pull request #14 from libp2p/module-list
docs(readme): add package listing
2016-08-29 16:33:45 +01:00
a06dc87670 Merge pull request #12 from dignifiedquire/dignified
Use dignified.js
2016-08-29 16:33:17 +01:00
93a0103fb8 docs(readme): add package listing
See https://github.com/ipfs/js-ipfs/pull/385 for details
2016-08-01 15:40:47 +02:00
a6652f295e Use dignified.js 1.0.0 2016-04-07 21:20:56 +02:00
f08dbbc8a9 Use dignified.js 2016-03-23 15:30:14 +01:00
815a85d51f Merge pull request #3 from diasdavid/example/using-swarm
using swarm example
2015-12-15 03:45:22 +00:00
c4b6de1db6 notes
License: MIT
Signed-off-by: David Dias <daviddias.p@gmail.com>
2015-12-11 21:21:07 -08:00
2c48724db7 using swarm example
License: MIT
Signed-off-by: David Dias <daviddias.p@gmail.com>
2015-12-10 15:40:26 -08:00
d79abccb85 update readme with roadmap and links to discussion
License: MIT
Signed-off-by: David Dias <daviddias.p@gmail.com>
2015-11-18 18:58:30 +00:00
0293aee549 update the readme with roadmap 2015-11-18 16:28:20 +00:00
24 changed files with 739 additions and 58 deletions

10
.gitignore vendored
View File

@ -1,6 +1,7 @@
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
@ -23,5 +24,12 @@ coverage
build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
dist

28
.npmignore Normal file
View File

@ -0,0 +1,28 @@
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
test

View File

@ -1,13 +1,36 @@
sudo: false
language: node_js
node_js:
- "iojs"
branches:
only:
- master
matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8
# Make sure we have new NPM.
before_install:
- npm i -g npm
# Workaround for a permissions issue with Travis virtual machine images
- npm install -g npm
script:
- npm run lint
- npm test
- npm run coverage
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish
addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

58
CHANGELOG.md Normal file
View File

@ -0,0 +1,58 @@
<a name="0.8.0"></a>
# [0.8.0](https://github.com/libp2p/js-libp2p/compare/v0.7.0...v0.8.0) (2017-03-31)
### Bug Fixes
* addition of ipfs id appendix must come before transport filtering ([291e79f](https://github.com/libp2p/js-libp2p/commit/291e79f))
* avoid deleting nodes from peerBook ([300936f](https://github.com/libp2p/js-libp2p/commit/300936f))
* correct method on peer-book ([031ecb3](https://github.com/libp2p/js-libp2p/commit/031ecb3))
### Features
* append peer id to multiaddr if not there ([59ea9c3](https://github.com/libp2p/js-libp2p/commit/59ea9c3))
* not remove peer from peerBook on disconnect ([a4b41b0](https://github.com/libp2p/js-libp2p/commit/a4b41b0))
<a name="0.7.0"></a>
# [0.7.0](https://github.com/libp2p/js-libp2p/compare/v0.6.2...v0.7.0) (2017-03-29)
### Features
* update events to conform with [#74](https://github.com/libp2p/js-libp2p/issues/74) ([f73c045](https://github.com/libp2p/js-libp2p/commit/f73c045))
<a name="0.6.2"></a>
## [0.6.2](https://github.com/libp2p/js-libp2p/compare/v0.6.1...v0.6.2) (2017-03-28)
<a name="0.6.1"></a>
## [0.6.1](https://github.com/libp2p/js-libp2p/compare/v0.6.0...v0.6.1) (2017-03-27)
<a name="0.6.0"></a>
# [0.6.0](https://github.com/libp2p/js-libp2p/compare/v0.5.5...v0.6.0) (2017-03-27)
### Bug Fixes
* last touches ([2c23d9a](https://github.com/libp2p/js-libp2p/commit/2c23d9a))
### Features
* new super simplified API ([a6623c1](https://github.com/libp2p/js-libp2p/commit/a6623c1))
<a name="0.5.5"></a>
## [0.5.5](https://github.com/libp2p/js-libp2p/compare/v0.5.4...v0.5.5) (2017-03-21)

265
README.md
View File

@ -1,48 +1,263 @@
js-libp2p
=========
![](https://github.com/libp2p/js-libp2p/blob/fe2d097bfa072ef6733f2defb568bdc526052ee4/img/js-libp2p.png?raw=true)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [[![](https://img.shields.io/badge/freejs-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) ![Build Status](https://travis-ci.org/diasdavid/js-libp2p.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-libp2p.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/libp2p/js-libp2p.svg?style=flat-square)](https://travis-ci.org/libp2p/js-libp2p)
![coverage](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
> Node.js implementation of libp2p
> libp2p is the networking stack of IPFS, a modular networking library to solve P2P application needs.
## Interface
## Table of Contents
> **This is a work in progress, interface might change at anytime**
- [Background](#background)
- [Bundles](#bundles)
- [Usage](#usage)
- [Install](#install)
- [API](#api)
- [Development](#development)
- [Tests](#tests)
- [Packages](#packages)
- [Contribute](#contribute)
- [License](#license)
libp2p expects a [Record Store interface](https://github.com/diasdavid/abstract-record-store), a swarm and one or more Peer Routers that implement the [Peer Routing](https://github.com/diasdavid/abstract-peer-routing), the goal is to keep simplicity and plugability while the remaining modules execute the heavy lifting.
## Background
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)
libp2p is the product of a long and arduous quest to understand the evolution of the Internet networking stack. In order to build P2P applications, dev have long had to made custom ad-hoc solutions to fit their needs, sometimes making some hard assumptions about their runtimes and the state of the network at the time of their development. Today, looking back more than 20 years, we see a clear pattern in the types of mechanisms built around the Internet Protocol, IP, which can be found throughout many layers of the OSI layer system, libp2p distils these mechanisms into flat categories and defines clear interfaces that once exposed, enable other protocols and applications to use and swap them, enabling upgradability and adaptability for the runtime, without breaking the API.
### Setting everything up
We are in the process of writting better documentation, blog posts, tutorials and a formal specification. Today you can find:
```
var Libp2p = require('libp2p')
- [libp2p.io - The libp2p Website (WIP)](https://github.com/libp2p/website)
- [Specification (WIP)](https://github.com/libp2p/specs)
- Talks
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
- Articles
- [The overview of libp2p](https://github.com/libp2p/libp2p#description)
// set up a Swarm, Peer Routing and Record Store instances, the last two are optional
To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.
var p2p = new Libp2p(swarm, [peerRouting, recordStore])
## Bundles
With its modular nature, libp2p can be found being used in different projects with different sets of features, while preserving the same top level API. `js-libp2p` is only a skeleton and should not be installed directly, if you are looking for a prebundled libp2p stack, please check:
- [libp2p-ipfs-nodejs](https://github.com/ipfs/js-libp2p-ipfs-nodejs) - The libp2p build used by js-ipfs when run in Node.js
- [libp2p-ipfs-browser](https://github.com/ipfs/js-libp2p-ipfs-browser) - The libp2p build used by js-ipfs when run in a Browser (that supports WebRTC)
If you have developed a libp2p bundle, please consider submitting it to this list so that it can be found easily by the users of libp2p.
## Install
Again, as noted above, this module is only a skeleton and should not be used directly other than libp2p bundle implementors that want to extend its code.
```sh
npm install --save libp2p
```
### Dialing and listening
## Usage
p2p.swarm.dial(peerInfo, options, protocol, function (err, stream) {})
p2p.swarm.handleProtocol(protocol, options, handlerFunction)
### Extending libp2p skeleton
### Using Peer Routing
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).
p2p.routing.findPeers(key, function (err, peerInfos) {})
**Example:**
### Using Records
```JavaScript
// Creating a bundle that adds:
// transport: websockets + tcp
// stream-muxing: SPDY
// crypto-channel: secio
// discovery: multicast-dns
p2p.record.get(key, function (err, records) {})
p2p.record.store(key, record)
const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const secio = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
### Stats
class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
options = options || {}
const modules = {
transport: [
new TCP(),
new WS()
],
connection: {
muxer: [
spdy
],
crypto: [
secio
]
},
discovery: [
new MulticastDNS(peerInfo, 'your-identifier')
]
}
super(modules, peerInfo, peerBook, options)
}
}
// Now all the nodes you create, will have TCP, WebSockets, SPDY, SECIO and MulticastDNS support.
```
### API
#### Create a Node - `new libp2p.Node([peerInfo, peerBook, 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.
#### `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.
#### `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.
#### `libp2p.dial(peer [, protocol, callback])`
> Dials to another peer in the network.
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
`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.
#### `libp2p.hangUp(peer, callback)
> Closes an open connection with a peer, graciously.
- `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.
#### `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) {}`
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.
#### `libp2p.unhandle(protocol)
> Stop handling protocol
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
#### `libp2p.on('peer:discovery', (peer) => {})`
> Peer has been discovered.
- `peer`: instance of [PeerInfo][]
#### `libp2p.on('peer:connect', (peer) => {})`
> We connected to a new peer
- `peer`: instance of [PeerInfo][]
#### `libp2p.on('peer:disconnect', (peer) => {})`
> We disconnected from Peer
- `peer`: instance of [PeerInfo][]
#### `libp2p.isOn()`
> Check if libp2p is started
#### `libp2p.ping(peer [, options], callback)`
> Ping a node in the network
#### `libp2p.peerBook`
> PeerBook instance of the node
#### `libp2p.peerInfo`
> PeerInfo instance of the node
---------------------
`SOON™`
#### `libp2p.findPeers`
#### `libp2p.findProviders`
#### `libp2p.record.put`
#### `libp2p.record.get`
[PeerInfo]: https://github.com/libp2p/js-peer-info
[PeerId]: https://github.com/libp2p/js-peer-id
[PeerBook]: https://github.com/libp2p/js-peer-book
[multiaddr]: https://github.com/multiformats/js-multiaddr
[Connection]: https://github.com/libp2p/interface-connection
### Packages
## Notes
List of packages currently in existence for libp2p
Img for ref (till we get a better graph)
| Package | Version | Dependencies | DevDependencies |
|---------|---------|--------------|-----------------|
| **Bundles** |
| [`libp2p-ipfs-nodejs`](//github.com/ipfs/js-libp2p-ipfs-nodejs) | [![npm](https://img.shields.io/npm/v/libp2p-ipfs-nodejs.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/js-libp2p-ipfs-nodejs/releases) | [![Dependency Status](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs) | [![devDependency Status](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs/dev-status.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs?type=dev) |
| [`libp2p-ipfs-browser`](//github.com/ipfs/js-libp2p-ipfs-browser) | [![npm](https://img.shields.io/npm/v/libp2p-ipfs-browser.svg?maxAge=86400&style=flat-square)](//github.com/ipfs/js-libp2p-ipfs-browser/releases) | [![Dependency Status](https://david-dm.org/ipfs/js-libp2p-ipfs-browser.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-ipfs-browser) | [![devDependency Status](https://david-dm.org/ipfs/js-libp2p-ipfs-browser/dev-status.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-ipfs-browser?type=dev) |
| **Transports** |
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [![npm](https://img.shields.io/npm/v/libp2p-utp.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-utp/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-utp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utp) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-utp/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utp?type=dev) |
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [![npm](https://img.shields.io/npm/v/libp2p-websockets.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-websockets/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-websockets/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets?type=dev) |
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [![npm](https://img.shields.io/npm/v/libp2p-webrtc-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-webrtc-star/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star?type=dev) |
| **Connection Upgrades** |
| [`interface-connection`](//github.com/libp2p/interface-connection) | [![npm](https://img.shields.io/npm/v/interface-connection.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-connection/releases) | [![Dependency Status](https://david-dm.org/libp2p/interface-connection.svg?style=flat-square)](https://david-dm.org/libp2p/interface-connection) | [![devDependency Status](https://david-dm.org/libp2p/interface-connection/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/interface-connection?type=dev) |
| **Stream Muxers** |
| [`interface-stream-muxer`](//github.com/libp2p/interface-stream-muxer) | [![npm](https://img.shields.io/npm/v/interface-stream-muxer.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-stream-muxer/releases) | [![Dependency Status](https://david-dm.org/libp2p/interface-stream-muxer.svg?style=flat-square)](https://david-dm.org/libp2p/interface-stream-muxer) | [![devDependency Status](https://david-dm.org/libp2p/interface-stream-muxer/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/interface-stream-muxer?type=dev) |
| [`libp2p-spdy`](//github.com/libp2p/js-libp2p-spdy) | [![npm](https://img.shields.io/npm/v/libp2p-spdy.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-spdy/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-spdy.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-spdy) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-spdy/dev-status.svg?style=flat-square)](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) | [![npm](https://img.shields.io/npm/v/libp2p-mdns-discovery.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-mdns-discovery/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-mdns-discovery.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns-discovery) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-mdns-discovery/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns-discovery?type=dev) |
| [`libp2p-railing`](//github.com/libp2p/js-libp2p-railing) | [![npm](https://img.shields.io/npm/v/libp2p-railing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-railing/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-railing.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-railing) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-railing/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-railing?type=dev) |
| **Crypto Channels** |
| [`libp2p-secio`](//github.com/libp2p/js-libp2p-secio) | [![npm](https://img.shields.io/npm/v/libp2p-secio.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-secio/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-secio.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-secio) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-secio/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-secio?type=dev) |
| **Peer Routing** |
| [`libp2p-kad-routing`](//github.com/libp2p/js-libp2p-kad-routing) | [![npm](https://img.shields.io/npm/v/libp2p-kad-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-routing/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-kad-routing.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-routing) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-kad-routing/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-routing?type=dev) |
| **Content Routing** |
| [`interface-record-store`](//github.com/libp2p/interface-record-store) | [![npm](https://img.shields.io/npm/v/interface-record-store.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/interface-record-store/releases) | [![Dependency Status](https://david-dm.org/libp2p/interface-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/interface-record-store) | [![devDependency Status](https://david-dm.org/libp2p/interface-record-store/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/interface-record-store?type=dev) |
| [`libp2p-record`](//github.com/libp2p/js-libp2p-record) | [![npm](https://img.shields.io/npm/v/libp2p-record.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-record/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-record.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-record) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-record/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-record?type=dev) |
| [`libp2p-distributed-record-store`](//github.com/libp2p/js-libp2p-distributed-record-store) | [![npm](https://img.shields.io/npm/v/undefined.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-distributed-record-store/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-distributed-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-distributed-record-store) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-distributed-record-store/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-distributed-record-store?type=dev) |
| [`libp2p-kad-record-store`](//github.com/libp2p/js-libp2p-kad-record-store) | [![npm](https://img.shields.io/npm/v/libp2p-kad-record-store.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-record-store/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-kad-record-store.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-record-store) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-kad-record-store/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-record-store?type=dev) |
| **Generics** |
| [`libp2p-swarm`](//github.com/libp2p/js-libp2p-swarm) | [![npm](https://img.shields.io/npm/v/libp2p-swarm.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-swarm/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-swarm) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-swarm/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-swarm?type=dev) |
| [`libp2p-ping`](//github.com/libp2p/js-libp2p-ping) | [![npm](https://img.shields.io/npm/v/libp2p-ping.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-ping/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-ping.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping) | [![devDependency Status](https://david-dm.org/libp2p/js-libp2p-ping/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping?type=dev) |
| [`multistream-select`](//github.com/libp2p/js-multistream) | [![npm](https://img.shields.io/npm/v/multistream-select.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-multistream/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-multistream.svg?style=flat-square)](https://david-dm.org/libp2p/js-multistream) | [![devDependency Status](https://david-dm.org/libp2p/js-multistream/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-multistream?type=dev) |
| **Data Types** |
| [`peer-book`](//github.com/libp2p/js-peer-book) | [![npm](https://img.shields.io/npm/v/peer-book.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-peer-book/releases) | [![Dependency Status](https://david-dm.org/libp2p/js-peer-book.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-book) | [![devDependency Status](https://david-dm.org/libp2p/js-peer-book/dev-status.svg?style=flat-square)](https://david-dm.org/libp2p/js-peer-book?type=dev) |
| [`peer-id`](https://github.com/libp2p/js-peer-id)
![](https://cloud.githubusercontent.com/assets/1211152/9450620/a02e3a9c-4aa1-11e5-83fd-cd996a0a4b6f.png)
## Contribute
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:
- Go through the modules and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrasture behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- **Perform code reviews**. Most of this has been developed by @diasdavid, which means that more eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
- **Add tests**. There can never be enough tests.
## License
[MIT](LICENSE) © David Dias

12
circle.yml Normal file
View File

@ -0,0 +1,12 @@
machine:
node:
version: stable
dependencies:
pre:
- google-chrome --version
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- sudo apt-get update
- sudo apt-get --only-upgrade install google-chrome-stable
- google-chrome --version

6
examples/README.md Normal file
View File

@ -0,0 +1,6 @@
### Examples list
Here are some examples built with libp2p bundles.
- https://github.com/ipfs/js-libp2p-ipfs/tree/master/examples/echo
- https://github.com/ipfs/js-libp2p-ipfs/tree/master/examples/chat

View File

@ -0,0 +1,4 @@
Using libp2p-swarm
==================

View File

@ -0,0 +1,39 @@
'use strict'
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')
// set up
var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
// create a libp2p node
var node = new Libp2p(sw)
node.swarm.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8010}, function () {
// Ready to receive incoming connections
sw.addStreamMuxer('spdy', Spdy, {})
// dial to another node
var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p2 = new Peer(Id.create(), [mh2])
node.swarm.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
if (err) {
return console.error(err)
}
console.log('-> connection is ready')
process.stdin.pipe(conn).pipe(process.stdout)
})
})

View File

@ -0,0 +1,31 @@
'use strict'
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')
// set up
var mh = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)
sw.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8020}, function () {
// Ready to receive incoming connections
sw.addStreamMuxer('spdy', Spdy, {})
// create a libp2p node
var node = new Libp2p(sw)
// handle/mount a protocol
node.swarm.handleProtocol('/sparkles/1.0.0', function (conn) {
process.stdin.pipe(conn).pipe(process.stdout)
})
})

View File

View File

View File

View File

View File

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
img/js-libp2p-ipfs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
img/js-libp2p.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
img/libp2p-arch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 MiB

BIN
img/libp2p-logos.sketch Normal file

Binary file not shown.

View File

@ -1,32 +1,58 @@
{
"name": "libp2p",
"version": "0.1.1",
"description": "Node.js implementation of libp2p",
"version": "0.8.0",
"description": "JavaScript Skeleton for libp2p bundles",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "aegir-test node",
"lint": "aegir-lint",
"release": "aegir-release node",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-libp2p.git"
"url": "https://github.com/libp2p/js-libp2p.git"
},
"keywords": [
"IPFS"
],
"engines": {
"node": "^4.0.0"
"node": ">=4.0.0",
"npm": ">=3.0.0"
},
"pre-commit": [],
"pre-commit": [
"lint",
"test"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-libp2p/issues"
"url": "https://github.com/libp2p/js-libp2p/issues"
},
"homepage": "https://github.com/diasdavid/js-libp2p",
"homepage": "https://github.com/libp2p/js-libp2p",
"devDependencies": {
"code": "^1.5.0",
"lab": "^5.17.0",
"pre-commit": "^1.1.1",
"standard": "^5.3.1"
}
}
"aegir": "^11.0.1",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"pre-commit": "^1.2.2"
},
"dependencies": {
"libp2p-ping": "~0.3.2",
"libp2p-swarm": "~0.29.0",
"mafmt": "^2.1.8",
"multiaddr": "^2.3.0",
"peer-book": "~0.4.0",
"peer-id": "~0.8.6",
"peer-info": "~0.9.2"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>"
]
}

View File

@ -1,18 +1,235 @@
'use strict'
exports = module.exports = Libp2p
const Swarm = require('libp2p-swarm')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const mafmt = require('mafmt')
const PeerBook = require('peer-book')
const multiaddr = require('multiaddr')
const EventEmitter = require('events').EventEmitter
const assert = require('assert')
const Ping = require('libp2p-ping')
const setImmediate = require('async/setImmediate')
function Libp2p (swarm, peerRouting, recordStore) {
var self = this
exports = module.exports
if (!(self instanceof Libp2p)) {
throw new Error('Must be called with new')
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
class Node extends EventEmitter {
constructor (_modules, _peerInfo, _peerBook, _options) {
super()
assert(_modules, 'requires modules to equip libp2p with features')
assert(_peerInfo, 'requires a PeerInfo instance')
this.modules = _modules
this.peerInfo = _peerInfo
this.peerBook = _peerBook || new PeerBook()
this.isOnline = false
this.swarm = new Swarm(this.peerInfo, this.peerBook)
// Attach stream multiplexers
if (this.modules.connection.muxer) {
let muxers = this.modules.connection.muxer
muxers = Array.isArray(muxers) ? muxers : [muxers]
muxers.forEach((muxer) => {
this.swarm.connection.addStreamMuxer(muxer)
})
// If muxer exists, we can use Identify
this.swarm.connection.reuse()
// Received incommind dial and muxer upgrade happened,
// reuse this muxed connection
this.swarm.on('peer-mux-established', (peerInfo) => {
this.emit('peer:connect', peerInfo)
this.peerBook.put(peerInfo)
})
this.swarm.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
})
}
// Attach crypto channels
if (this.modules.connection.crypto) {
let cryptos = this.modules.connection.crypto
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
cryptos.forEach((crypto) => {
this.swarm.connection.crypto(crypto.tag, crypto.encrypt)
})
}
// Attach discovery mechanisms
if (this.modules.discovery) {
let discoveries = this.modules.discovery
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
discoveries.forEach((discovery) => {
discovery.on('peer', (peerInfo) => this.emit('peer:discovery', peerInfo))
})
}
// Mount default protocols
Ping.mount(this.swarm)
// Not fully implemented in js-libp2p yet
this.routing = undefined
this.records = undefined
}
if (!swarm) {
throw new Error('a swarm must be passed')
/*
* Start the libp2p node
* - create listeners on the multiaddrs the Peer wants to listen
*/
start (callback) {
if (!this.modules.transport) {
return callback(new Error('no transports were present'))
}
let ws
let transports = this.modules.transport
transports = Array.isArray(transports) ? transports : [transports]
// so that we can have webrtc-star addrs without adding manually the id
const maOld = []
const maNew = []
this.peerInfo.multiaddrs.forEach((ma) => {
if (!mafmt.IPFS.matches(ma)) {
maOld.push(ma)
maNew.push(ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String()))
}
})
this.peerInfo.multiaddrs.replace(maOld, maNew)
const multiaddrs = this.peerInfo.multiaddrs.toArray()
transports.forEach((transport) => {
if (transport.filter(multiaddrs).length > 0) {
this.swarm.transport.add(
transport.tag || transport.constructor.name, transport)
} else if (transport.constructor &&
transport.constructor.name === 'WebSockets') {
// TODO find a cleaner way to signal that a transport is always
// used for dialing, even if no listener
ws = transport
}
})
this.swarm.listen((err) => {
if (err) {
return callback(err)
}
if (ws) {
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
}
this.isOnline = true
if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
setImmediate(() => discovery.start(() => {}))
})
}
callback()
})
}
self.swarm = swarm
self.routing = peerRouting
self.record = recordStore
/*
* Stop the libp2p node by closing its listeners and open connections
*/
stop (callback) {
this.isOnline = false
if (this.modules.discovery) {
this.modules.discovery.forEach((discovery) => {
setImmediate(() => discovery.stop(() => {}))
})
}
this.swarm.close(callback)
}
isOn () {
return this.isOnline
}
ping (peer, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)
callback(null, new Ping(this.swarm, peerInfo))
}
dial (peer, protocol, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}
let peerInfo
try {
peerInfo = this._getPeerInfo(peer)
} catch (err) {
return callback(err)
}
this.swarm.dial(peerInfo, protocol, (err, conn) => {
if (err) {
return callback(err)
}
this.peerBook.put(peerInfo)
callback(null, conn)
})
}
hangUp (peer, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)
this.swarm.hangUp(peerInfo, callback)
}
handle (protocol, handlerFunc, matchFunc) {
this.swarm.handle(protocol, handlerFunc, matchFunc)
}
unhandle (protocol) {
this.swarm.unhandle(protocol)
}
/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
_getPeerInfo (peer) {
let p
if (PeerInfo.isPeerInfo(peer)) {
p = peer
} else if (multiaddr.isMultiaddr(peer)) {
const peerIdB58Str = peer.getPeerId()
try {
p = this.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = this.peerBook.get(peerIdB58Str)
} catch (err) {
// TODO this is where PeerRouting comes into place
throw new Error('No knowledge about: ' + peerIdB58Str)
}
} else {
throw new Error('peer type not recognized')
}
return p
}
}
module.exports = Node

14
test/index.spec.js Normal file
View File

@ -0,0 +1,14 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const libp2p = require('../src')
describe('libp2p', () => {
it('the skeleton is fine, now go build your own libp2p bundle', () => {
expect(libp2p).to.exist()
})
})