mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-13 15:51:34 +00:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
7068aae527 | |||
d632451f92 | |||
8047e3fe64 | |||
35af31023c |
30
CHANGELOG.md
30
CHANGELOG.md
@ -1,30 +0,0 @@
|
|||||||
<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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
151
README.md
151
README.md
@ -39,7 +39,7 @@ To sum up, libp2p is a "network stack" -- a protocol suite -- that cleanly separ
|
|||||||
|
|
||||||
## Bundles
|
## 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:
|
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-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)
|
- [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)
|
||||||
@ -48,7 +48,7 @@ If you have developed a libp2p bundle, please consider submitting it to this lis
|
|||||||
|
|
||||||
## Install
|
## 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.
|
> 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
|
```sh
|
||||||
npm install --save libp2p
|
npm install --save libp2p
|
||||||
@ -56,152 +56,27 @@ npm install --save libp2p
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
> **Disclamer - We haven't solidified [libp2p interface](https://github.com/libp2p/interface-libp2p) yet, it might change at anytime.**
|
||||||
|
|
||||||
### Extending libp2p skeleton
|
### 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).
|
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)
|
||||||
|
|
||||||
**Example:**
|
### libp2p API
|
||||||
|
|
||||||
```JavaScript
|
Defined by [interface-libp2p](https://github.com/libp2p/interface-libp2p)
|
||||||
// Creating a bundle that adds:
|
|
||||||
// transport: websockets + tcp
|
|
||||||
// stream-muxing: SPDY
|
|
||||||
// crypto-channel: secio
|
|
||||||
// discovery: multicast-dns
|
|
||||||
|
|
||||||
const libp2p = require('libp2p')
|
## Development
|
||||||
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')
|
|
||||||
|
|
||||||
class Node extends libp2p {
|
## Linting
|
||||||
constructor (peerInfo, peerBook, options) {
|
|
||||||
options = options || {}
|
|
||||||
|
|
||||||
const modules = {
|
```sh
|
||||||
transport: [
|
> npm run lint
|
||||||
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', (peer) => {})`
|
|
||||||
|
|
||||||
> Peer has been discovered.
|
|
||||||
|
|
||||||
- `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
|
### Packages
|
||||||
|
|
||||||
List of packages currently in existence for libp2p
|
> List of packages currently in existance for libp2p
|
||||||
|
|
||||||
| Package | Version | Dependencies | DevDependencies |
|
| Package | Version | Dependencies | DevDependencies |
|
||||||
|---------|---------|--------------|-----------------|
|
|---------|---------|--------------|-----------------|
|
||||||
@ -240,7 +115,7 @@ List of packages currently in existence for libp2p
|
|||||||
|
|
||||||
## Contribute
|
## 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:
|
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:
|
||||||
|
|
||||||
- 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.
|
- 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.
|
- **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.
|
||||||
|
23
package.json
23
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p",
|
"name": "libp2p",
|
||||||
"version": "0.6.2",
|
"version": "0.4.0",
|
||||||
"description": "JavaScript Skeleton for libp2p bundles",
|
"description": "JavaScript Skeleton for libp2p bundles",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -32,26 +32,25 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/libp2p/js-libp2p/issues"
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-libp2p",
|
"homepage": "https://github.com/diasdavid/js-libp2p",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^11.0.1",
|
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"dirty-chai": "^1.2.2",
|
"aegir": "^9.4.0",
|
||||||
"pre-commit": "^1.2.2"
|
"pre-commit": "^1.2.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"libp2p-ping": "~0.3.2",
|
"libp2p-ping": "^0.3.0",
|
||||||
"libp2p-swarm": "~0.28.0",
|
"libp2p-swarm": "^0.26.13",
|
||||||
"multiaddr": "^2.3.0",
|
"mafmt": "^2.1.6",
|
||||||
"peer-book": "~0.3.2",
|
"multiaddr": "^2.2.0",
|
||||||
"peer-id": "~0.8.5",
|
"peer-book": "^0.3.0",
|
||||||
"peer-info": "~0.8.5"
|
"peer-id": "^0.8.1",
|
||||||
|
"peer-info": "^0.8.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
"greenkeeperio-bot <support@greenkeeper.io>"
|
||||||
"mayerwin <mayerwin@users.noreply.github.com>"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
177
src/index.js
177
src/index.js
@ -5,18 +5,18 @@ const PeerId = require('peer-id')
|
|||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
const PeerBook = require('peer-book')
|
const PeerBook = require('peer-book')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const mafmt = require('mafmt')
|
||||||
|
const EE = require('events').EventEmitter
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const Ping = require('libp2p-ping')
|
const Ping = require('libp2p-ping')
|
||||||
const setImmediate = require('async/setImmediate')
|
|
||||||
|
|
||||||
exports = module.exports
|
exports = module.exports
|
||||||
|
|
||||||
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
|
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
|
||||||
|
const IPFS_CODE = 421
|
||||||
|
|
||||||
class Node extends EventEmitter {
|
class Node {
|
||||||
constructor (_modules, _peerInfo, _peerBook, _options) {
|
constructor (_modules, _peerInfo, _peerBook) {
|
||||||
super()
|
|
||||||
assert(_modules, 'requires modules to equip libp2p with features')
|
assert(_modules, 'requires modules to equip libp2p with features')
|
||||||
assert(_peerInfo, 'requires a PeerInfo instance')
|
assert(_peerInfo, 'requires a PeerInfo instance')
|
||||||
|
|
||||||
@ -25,6 +25,8 @@ class Node extends EventEmitter {
|
|||||||
this.peerBook = _peerBook || new PeerBook()
|
this.peerBook = _peerBook || new PeerBook()
|
||||||
this.isOnline = false
|
this.isOnline = false
|
||||||
|
|
||||||
|
this.discovery = new EE()
|
||||||
|
|
||||||
this.swarm = new Swarm(this.peerInfo)
|
this.swarm = new Swarm(this.peerInfo)
|
||||||
|
|
||||||
// Attach stream multiplexers
|
// Attach stream multiplexers
|
||||||
@ -63,7 +65,9 @@ class Node extends EventEmitter {
|
|||||||
let discoveries = this.modules.discovery
|
let discoveries = this.modules.discovery
|
||||||
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
|
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
|
||||||
discoveries.forEach((discovery) => {
|
discoveries.forEach((discovery) => {
|
||||||
discovery.on('peer', (peerInfo) => this.emit('peer', peerInfo))
|
discovery.on('peer', (peerInfo) => {
|
||||||
|
this.discovery.emit('peer', peerInfo)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,6 +134,7 @@ class Node extends EventEmitter {
|
|||||||
|
|
||||||
if (this.modules.discovery) {
|
if (this.modules.discovery) {
|
||||||
this.modules.discovery.forEach((discovery) => {
|
this.modules.discovery.forEach((discovery) => {
|
||||||
|
console.log(discovery)
|
||||||
setImmediate(() => discovery.stop(() => {}))
|
setImmediate(() => discovery.stop(() => {}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -137,42 +142,142 @@ class Node extends EventEmitter {
|
|||||||
this.swarm.close(callback)
|
this.swarm.close(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
isOn () {
|
//
|
||||||
return this.isOnline
|
// Ping
|
||||||
|
//
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
pingById (id, callback) {
|
||||||
|
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
|
||||||
|
callback(new Error('not implemented yet'))
|
||||||
}
|
}
|
||||||
|
|
||||||
ping (peer, callback) {
|
// TODO
|
||||||
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
pingByMultiaddr (maddr, callback) {
|
||||||
const peerInfo = this._getPeerInfo(peer)
|
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))
|
callback(null, new Ping(this.swarm, peerInfo))
|
||||||
}
|
}
|
||||||
|
|
||||||
dial (peer, protocol, callback) {
|
//
|
||||||
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
// Dialing methods
|
||||||
const peerInfo = this._getPeerInfo(peer)
|
//
|
||||||
|
|
||||||
|
// 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') {
|
if (typeof protocol === 'function') {
|
||||||
callback = protocol
|
callback = protocol
|
||||||
protocol = undefined
|
protocol = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
this.swarm.dial(peerInfo, protocol, (err, conn) => {
|
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) {
|
if (err) {
|
||||||
return callback(err)
|
return callback(err)
|
||||||
}
|
}
|
||||||
this.peerBook.put(peerInfo)
|
this.peerBook.put(peer)
|
||||||
callback(null, conn)
|
callback(null, conn)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
hangUp (peer, callback) {
|
//
|
||||||
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
|
// Disconnecting (hangUp) methods
|
||||||
const peerInfo = this._getPeerInfo(peer)
|
//
|
||||||
|
|
||||||
this.peerBook.removeByB58String(peerInfo.id.toB58String())
|
hangUpById (id, callback) {
|
||||||
this.swarm.hangUp(peerInfo, 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) {
|
handle (protocol, handlerFunc, matchFunc) {
|
||||||
this.swarm.handle(protocol, handlerFunc, matchFunc)
|
this.swarm.handle(protocol, handlerFunc, matchFunc)
|
||||||
}
|
}
|
||||||
@ -180,36 +285,6 @@ class Node extends EventEmitter {
|
|||||||
unhandle (protocol) {
|
unhandle (protocol) {
|
||||||
this.swarm.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.getByB58String(peerIdB58Str)
|
|
||||||
} catch (err) {
|
|
||||||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
|
||||||
}
|
|
||||||
p.multiaddr.add(peer)
|
|
||||||
} else if (PeerId.isPeerId(peer)) {
|
|
||||||
const peerIdB58Str = peer.toB58String()
|
|
||||||
try {
|
|
||||||
p = this.peerBook.getByB58String(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
|
module.exports = Node
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const chai = require('chai')
|
const expect = require('chai').expect
|
||||||
chai.use(require('dirty-chai'))
|
|
||||||
const expect = chai.expect
|
|
||||||
|
|
||||||
const libp2p = require('../src')
|
const libp2p = require('../src')
|
||||||
|
|
||||||
describe('libp2p', () => {
|
describe('libp2p', () => {
|
||||||
it('the skeleton is fine, now go build your own libp2p bundle', () => {
|
it('the skeleton is fine, now go build your own libp2p bundle', () => {
|
||||||
expect(libp2p).to.exist()
|
expect(libp2p).to.exist
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user