mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-08 05:11:33 +00:00
Compare commits
47 Commits
Author | SHA1 | Date | |
---|---|---|---|
078956b310 | |||
4f44158889 | |||
3a8d51c142 | |||
f8c09db20f | |||
ee801c9be2 | |||
03349d7caf | |||
0c87dbc8f3 | |||
c4abbaa2f9 | |||
21759f1204 | |||
0830827c06 | |||
8f31d1cb17 | |||
fc43b99836 | |||
4027c3cca4 | |||
049c7ce347 | |||
54b65b3cbc | |||
3db3dca98b | |||
40fdc4147c | |||
e10ec13c33 | |||
5d0b25cb00 | |||
57c85fe346 | |||
d7eb5b7abc | |||
b5608f0ef8 | |||
b423a1c583 | |||
0f7bde7bc3 | |||
fecb41b275 | |||
14d14ff0b6 | |||
af493363aa | |||
fe2d097bfa | |||
3cf54db8d5 | |||
ed9d0e5a42 | |||
8486f04c55 | |||
012433b5d5 | |||
48fcf7ee90 | |||
517723a093 | |||
55223d528c | |||
f6f96d3b5f | |||
3c93b7cd4e | |||
c72ce8a7fb | |||
a06dc87670 | |||
93a0103fb8 | |||
a6652f295e | |||
f08dbbc8a9 | |||
815a85d51f | |||
c4b6de1db6 | |||
2c48724db7 | |||
d79abccb85 | |||
0293aee549 |
10
.gitignore
vendored
10
.gitignore
vendored
@ -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
28
.npmignore
Normal 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
|
39
.travis.yml
39
.travis.yml
@ -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
|
134
README.md
134
README.md
@ -1,48 +1,126 @@
|
||||
js-libp2p
|
||||
=========
|
||||

|
||||
|
||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/js-libp2p)  [](https://david-dm.org/diasdavid/js-libp2p) [](https://github.com/feross/standard)
|
||||
[](http://ipn.io)
|
||||
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||
[](https://travis-ci.org/libp2p/js-libp2p)
|
||||

|
||||
[](https://david-dm.org/libp2p/js-libp2p)
|
||||
[](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 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.
|
||||
|
||||
We are in the process of writting better documentation, blog posts, tutorials and a formal specification. Today you can find:
|
||||
|
||||
- [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)
|
||||
|
||||
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.
|
||||
|
||||
## Bundles
|
||||
|
||||
With its modular nature, libp2p can be found being used in different projects with different sets of features, while perserving the same top level API. `js-libp2p` is only a skelleton 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
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
> **Disclamer - We haven't solidified [libp2p interface](https://github.com/libp2p/interface-libp2p) yet, it might change at anytime.**
|
||||
|
||||
### Extending libp2p skeleton
|
||||
|
||||
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)
|
||||
|
||||
### Setting everything up
|
||||
### libp2p API
|
||||
|
||||
```
|
||||
var Libp2p = require('libp2p')
|
||||
Defined by [interface-libp2p](https://github.com/libp2p/interface-libp2p)
|
||||
|
||||
// set up a Swarm, Peer Routing and Record Store instances, the last two are optional
|
||||
## Development
|
||||
|
||||
var p2p = new Libp2p(swarm, [peerRouting, recordStore])
|
||||
## Linting
|
||||
|
||||
```sh
|
||||
> npm run lint
|
||||
```
|
||||
|
||||
### Dialing and listening
|
||||
### Packages
|
||||
|
||||
p2p.swarm.dial(peerInfo, options, protocol, function (err, stream) {})
|
||||
p2p.swarm.handleProtocol(protocol, options, handlerFunction)
|
||||
> List of packages currently in existance for libp2p
|
||||
|
||||
### Using Peer Routing
|
||||
| Package | Version | Dependencies | DevDependencies |
|
||||
|---------|---------|--------------|-----------------|
|
||||
| **Bundles** |
|
||||
| [`libp2p-ipfs-nodejs`](//github.com/ipfs/js-libp2p-ipfs-nodejs) | [](//github.com/ipfs/js-libp2p-ipfs-nodejs/releases) | [](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs) | [](https://david-dm.org/ipfs/js-libp2p-ipfs-nodejs?type=dev) |
|
||||
| [`libp2p-ipfs-browser`](//github.com/ipfs/js-libp2p-ipfs-browser) | [](//github.com/ipfs/js-libp2p-ipfs-browser/releases) | [](https://david-dm.org/ipfs/js-libp2p-ipfs-browser) | [](https://david-dm.org/ipfs/js-libp2p-ipfs-browser?type=dev) |
|
||||
| **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) |
|
||||
| **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) |
|
||||
| **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-swarm`](//github.com/libp2p/js-libp2p-swarm) | [](//github.com/libp2p/js-libp2p-swarm/releases) | [](https://david-dm.org/libp2p/js-libp2p-swarm) | [](https://david-dm.org/libp2p/js-libp2p-swarm?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) |
|
||||
| **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)
|
||||
|
||||
p2p.routing.findPeers(key, function (err, peerInfos) {})
|
||||
## Contribute
|
||||
|
||||
### Using Records
|
||||
THe libp2p implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:
|
||||
|
||||
p2p.record.get(key, function (err, records) {})
|
||||
p2p.record.store(key, record)
|
||||
- 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.
|
||||
|
||||
### Stats
|
||||
## License
|
||||
|
||||
|
||||
|
||||
## Notes
|
||||
|
||||
Img for ref (till we get a better graph)
|
||||
|
||||

|
||||
[MIT](LICENSE) © David Dias
|
||||
|
12
circle.yml
Normal file
12
circle.yml
Normal 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
6
examples/README.md
Normal 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
|
4
examples/archived/README.md
Normal file
4
examples/archived/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
Using libp2p-swarm
|
||||
==================
|
||||
|
||||
|
39
examples/archived/node-1.js
Normal file
39
examples/archived/node-1.js
Normal 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)
|
||||
})
|
||||
})
|
31
examples/archived/node-2.js
Normal file
31
examples/archived/node-2.js
Normal 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)
|
||||
})
|
||||
})
|
0
examples/browser-chat/.gitkeep
Normal file
0
examples/browser-chat/.gitkeep
Normal file
0
examples/browser-echo/README.md
Normal file
0
examples/browser-echo/README.md
Normal file
0
examples/node-browser-chat/README.md
Normal file
0
examples/node-browser-chat/README.md
Normal file
0
examples/node-browser-echo/README.md
Normal file
0
examples/node-browser-echo/README.md
Normal file
0
examples/node-chat/.gitkeep
Normal file
0
examples/node-chat/.gitkeep
Normal file
0
examples/node-echo/README.md
Normal file
0
examples/node-echo/README.md
Normal file
BIN
img/js-libp2p-ipfs-browser.png
Normal file
BIN
img/js-libp2p-ipfs-browser.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
BIN
img/js-libp2p-ipfs.png
Normal file
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
BIN
img/js-libp2p.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
BIN
img/libp2p-arch.png
Normal file
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
BIN
img/libp2p-logos.sketch
Normal file
Binary file not shown.
48
package.json
48
package.json
@ -1,32 +1,56 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.1.1",
|
||||
"description": "Node.js implementation of libp2p",
|
||||
"version": "0.3.1",
|
||||
"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",
|
||||
"devDependencies": {
|
||||
"code": "^1.5.0",
|
||||
"lab": "^5.17.0",
|
||||
"pre-commit": "^1.1.1",
|
||||
"standard": "^5.3.1"
|
||||
}
|
||||
"chai": "^3.5.0",
|
||||
"aegir": "^9.1.2",
|
||||
"pre-commit": "^1.1.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"libp2p-ping": "^0.3.0",
|
||||
"libp2p-swarm": "^0.26.3",
|
||||
"mafmt": "^2.1.2",
|
||||
"multiaddr": "^2.1.1",
|
||||
"peer-book": "^0.3.0",
|
||||
"peer-id": "^0.8.0",
|
||||
"peer-info": "^0.8.1"
|
||||
},
|
||||
"contributors": [
|
||||
"David Dias <daviddias.p@gmail.com>",
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>"
|
||||
]
|
||||
}
|
277
src/index.js
277
src/index.js
@ -1,18 +1,275 @@
|
||||
'use strict'
|
||||
|
||||
exports = module.exports = Libp2p
|
||||
const Swarm = require('libp2p-swarm')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerBook = require('peer-book')
|
||||
const multiaddr = require('multiaddr')
|
||||
const mafmt = require('mafmt')
|
||||
const EE = require('events').EventEmitter
|
||||
const assert = require('assert')
|
||||
const Ping = require('libp2p-ping')
|
||||
|
||||
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'
|
||||
const IPFS_CODE = 421
|
||||
|
||||
class Node {
|
||||
constructor (_modules, _peerInfo, _peerBook) {
|
||||
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.discovery = new EE()
|
||||
|
||||
this.swarm = new Swarm(this.peerInfo)
|
||||
|
||||
// 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.peerBook.put(peerInfo)
|
||||
})
|
||||
|
||||
this.swarm.on('peer-mux-closed', (peerInfo) => {
|
||||
this.peerBook.removeByB58String(peerInfo.id.toB58String())
|
||||
})
|
||||
}
|
||||
|
||||
if (!swarm) {
|
||||
throw new Error('a swarm must be passed')
|
||||
// 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)
|
||||
})
|
||||
}
|
||||
|
||||
self.swarm = swarm
|
||||
self.routing = peerRouting
|
||||
self.record = recordStore
|
||||
// Attach discovery mechanisms
|
||||
if (this.discovery) {
|
||||
let discoveries = this.modules.discovery
|
||||
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
|
||||
discoveries.forEach((discovery) => {
|
||||
discovery.on('peer', (peerInfo) => {
|
||||
this.discovery.emit('peer', peerInfo)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Mount default protocols
|
||||
Ping.mount(this.swarm)
|
||||
|
||||
// Not fully implemented in js-libp2p yet
|
||||
this.routing = undefined
|
||||
this.records = undefined
|
||||
}
|
||||
|
||||
/*
|
||||
* 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]
|
||||
const multiaddrs = this.peerInfo.multiaddrs
|
||||
|
||||
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
|
||||
callback()
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop the libp2p node by closing its listeners and open connections
|
||||
*/
|
||||
stop (callback) {
|
||||
this.isOnline = false
|
||||
this.swarm.close(callback)
|
||||
}
|
||||
|
||||
//
|
||||
// Ping
|
||||
//
|
||||
|
||||
// TODO
|
||||
pingById (id, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
callback(new Error('not implemented yet'))
|
||||
}
|
||||
|
||||
// TODO
|
||||
pingByMultiaddr (maddr, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
callback(new Error('not implemented yet'))
|
||||
}
|
||||
|
||||
pingByPeerInfo (peerInfo, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
callback(null, new Ping(this.swarm, peerInfo))
|
||||
}
|
||||
|
||||
//
|
||||
// Dialing methods
|
||||
//
|
||||
|
||||
// TODO
|
||||
dialById (id, protocol, callback) {
|
||||
// NOTE: dialById only works if a previous dial was made. This will
|
||||
// change once we have PeerRouting
|
||||
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
callback(new Error('not implemented yet'))
|
||||
}
|
||||
|
||||
dialByMultiaddr (maddr, protocol, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
if (typeof maddr === 'string') {
|
||||
maddr = multiaddr(maddr)
|
||||
}
|
||||
|
||||
if (!mafmt.IPFS.matches(maddr.toString())) {
|
||||
return callback(new Error('multiaddr not valid'))
|
||||
}
|
||||
|
||||
const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
|
||||
if (tuple[0] === IPFS_CODE) {
|
||||
return true
|
||||
}
|
||||
})[0][1]
|
||||
|
||||
let peer
|
||||
try {
|
||||
peer = this.peerBook.getByB58String(ipfsIdB58String)
|
||||
} catch (err) {
|
||||
peer = new PeerInfo(PeerId.createFromB58String(ipfsIdB58String))
|
||||
}
|
||||
|
||||
peer.multiaddr.add(maddr)
|
||||
this.dialByPeerInfo(peer, protocol, callback)
|
||||
}
|
||||
|
||||
dialByPeerInfo (peer, protocol, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
|
||||
if (typeof protocol === 'function') {
|
||||
callback = protocol
|
||||
protocol = undefined
|
||||
}
|
||||
|
||||
this.swarm.dial(peer, protocol, (err, conn) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
this.peerBook.put(peer)
|
||||
callback(null, conn)
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
// Disconnecting (hangUp) methods
|
||||
//
|
||||
|
||||
hangUpById (id, callback) {
|
||||
// TODO
|
||||
callback(new Error('not implemented yet'))
|
||||
}
|
||||
|
||||
hangUpByMultiaddr (maddr, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
|
||||
if (typeof maddr === 'string') {
|
||||
maddr = multiaddr(maddr)
|
||||
}
|
||||
|
||||
if (!mafmt.IPFS.matches(maddr.toString())) {
|
||||
return callback(new Error('multiaddr not valid'))
|
||||
}
|
||||
|
||||
const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
|
||||
if (tuple[0] === IPFS_CODE) {
|
||||
return true
|
||||
}
|
||||
})[0][1]
|
||||
|
||||
try {
|
||||
const pi = this.peerBook.getByB58String(ipfsIdB58String)
|
||||
this.hangUpByPeerInfo(pi, callback)
|
||||
} catch (err) {
|
||||
// already disconnected
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
hangUpByPeerInfo (peer, callback) {
|
||||
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||
|
||||
this.peerBook.removeByB58String(peer.id.toB58String())
|
||||
this.swarm.hangUp(peer, callback)
|
||||
}
|
||||
|
||||
//
|
||||
// Protocol multiplexing handling
|
||||
//
|
||||
|
||||
handle (protocol, handlerFunc, matchFunc) {
|
||||
this.swarm.handle(protocol, handlerFunc, matchFunc)
|
||||
}
|
||||
|
||||
unhandle (protocol) {
|
||||
this.swarm.unhandle(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
||||
|
12
test/index.spec.js
Normal file
12
test/index.spec.js
Normal file
@ -0,0 +1,12 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
const libp2p = require('../src')
|
||||
|
||||
describe('libp2p', () => {
|
||||
it('the skeleton is fine, now go build your own libp2p bundle', () => {
|
||||
expect(libp2p).to.exist
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user