This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from ipfs/feat/use-base-libp2p
Use the base class
- Loading branch information
Showing
7 changed files
with
46 additions
and
29,766 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,10 +31,10 @@ | |
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-browser#readme", | ||
"devDependencies": { | ||
"aegir": "^9.1.2", | ||
"async": "^2.1.2", | ||
"async": "^2.1.4", | ||
"chai": "^3.5.0", | ||
"gulp": "^3.9.1", | ||
"libp2p-ipfs": "^0.15.0", | ||
"libp2p-ipfs-nodejs": "^0.16.0", | ||
"peer-id": "^0.8.0", | ||
"pre-commit": "^1.1.3", | ||
"pull-goodbye": "0.0.1", | ||
|
@@ -45,19 +45,19 @@ | |
"dependencies": { | ||
"libp2p-secio": "^0.6.3", | ||
"libp2p-spdy": "^0.10.0", | ||
"libp2p-swarm": "^0.26.1", | ||
"libp2p-swarm": "^0.26.3", | ||
"libp2p-webrtc-star": "^0.6.0", | ||
"libp2p-websockets": "^0.9.1", | ||
"mafmt": "^2.1.2", | ||
"multiaddr": "^2.0.3", | ||
"multiaddr": "^2.1.1", | ||
"peer-book": "^0.3.0", | ||
"peer-id": "^0.8.0", | ||
"peer-info": "^0.8.0" | ||
"peer-info": "^0.8.1" | ||
}, | ||
"contributors": [ | ||
"David Dias <[email protected]>", | ||
"Friedel Ziegelmayer <[email protected]>", | ||
"Richard Littauer <[email protected]>", | ||
"greenkeeperio-bot <[email protected]>" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,206 +1,34 @@ | ||
'use strict' | ||
|
||
const Swarm = require('libp2p-swarm') | ||
const WS = require('libp2p-websockets') | ||
const WebRTCStar = require('libp2p-webrtc-star') | ||
const spdy = require('libp2p-spdy') | ||
const secio = require('libp2p-secio') | ||
const PeerInfo = require('peer-info') | ||
const PeerId = require('peer-id') | ||
const EE = require('events').EventEmitter | ||
const multiaddr = require('multiaddr') | ||
const PeerBook = require('peer-book') | ||
const mafmt = require('mafmt') | ||
|
||
exports = module.exports | ||
|
||
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet' | ||
const IPFS_CODE = 421 | ||
|
||
exports.Node = function Node (pInfo, pBook) { | ||
if (!(this instanceof Node)) { | ||
return new Node(pInfo, pBook) | ||
} | ||
|
||
if (!pInfo) { | ||
throw new Error('missing peer info') | ||
} | ||
|
||
if (!pBook) { | ||
pBook = new PeerBook() | ||
} | ||
|
||
this.peerInfo = pInfo | ||
this.peerBook = pBook | ||
|
||
// Swarm | ||
this.swarm = new Swarm(pInfo) | ||
this.swarm.connection.addStreamMuxer(spdy) | ||
this.swarm.connection.reuse() | ||
this.swarm.connection.crypto(secio.tag, secio.encrypt) | ||
|
||
this.swarm.on('peer-mux-established', (peerInfo) => { | ||
this.peerBook.put(peerInfo) | ||
}) | ||
|
||
this.swarm.on('peer-mux-closed', (peerInfo) => { | ||
this.peerBook.removeByB58String(peerInfo.id.toB58String()) | ||
}) | ||
|
||
let isOnline = false | ||
|
||
this.start = (callback) => { | ||
// if we have `webrtc-star` addrs, then add | ||
// the WebRTCStar transport | ||
const wstar = new WebRTCStar() | ||
|
||
if (wstar.filter(this.peerInfo.multiaddrs).length > 0) { | ||
this.swarm.transport.add('wstar', wstar) | ||
wstar.discovery.on('peer', (peerInfo) => { | ||
this.discovery.emit('peer', peerInfo) | ||
}) | ||
this.swarm.listen((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
// WebSockets needs to be added after because | ||
// it can't have a listener on the browser | ||
this.swarm.transport.add('ws', new WS()) | ||
isOnline = true | ||
callback() | ||
}) | ||
} else { | ||
// if just WebSockets, no thing to listen | ||
this.swarm.transport.add('ws', new WS()) | ||
isOnline = true | ||
callback() | ||
} | ||
const libp2p = require('libp2p') | ||
|
||
class Node extends libp2p { | ||
constructor (peerInfo, peerBook) { | ||
const webRTCStar = new WebRTCStar() | ||
|
||
const modules = { | ||
transport: [ | ||
new WS(), | ||
webRTCStar | ||
], | ||
connection: { | ||
muxer: [ | ||
spdy | ||
], | ||
crypto: [ | ||
secio | ||
] | ||
}, | ||
discovery: [ | ||
webRTCStar.discovery | ||
] | ||
} | ||
super(modules, peerInfo, peerBook) | ||
} | ||
|
||
this.stop = (callback) => { | ||
isOnline = false | ||
this.swarm.close(callback) | ||
} | ||
|
||
this.dialById = (id, protocol, callback) => { | ||
if (typeof protocol === 'function') { | ||
callback = protocol | ||
protocol = undefined | ||
} | ||
|
||
if (!isOnline) { | ||
return callback(new Error(OFFLINE_ERROR_MESSAGE)) | ||
} | ||
// NOTE, these dialById only works if a previous dial | ||
// was made until we have PeerRouting | ||
// TODO support PeerRouting when it is Ready | ||
callback(new Error('not implemented yet')) | ||
} | ||
|
||
this.dialByMultiaddr = (maddr, protocol, callback) => { | ||
if (typeof protocol === 'function') { | ||
callback = protocol | ||
protocol = undefined | ||
} | ||
|
||
if (!isOnline) { | ||
return callback(new Error(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] | ||
|
||
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) | ||
} | ||
|
||
this.dialByPeerInfo = (peer, protocol, callback) => { | ||
if (typeof protocol === 'function') { | ||
callback = protocol | ||
protocol = undefined | ||
} | ||
if (!isOnline) { | ||
return callback(new Error(OFFLINE_ERROR_MESSAGE)) | ||
} | ||
|
||
this.swarm.dial(peer, protocol, (err, conn) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
this.peerBook.put(peer) | ||
callback(null, conn) | ||
}) | ||
} | ||
|
||
this.hangUpById = (id, callback) => { | ||
callback(new Error('not implemented yet')) | ||
// TODO | ||
} | ||
|
||
this.hangUpByMultiaddr = (maddr, callback) => { | ||
if (!isOnline) { | ||
return callback(new Error(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() | ||
} | ||
} | ||
|
||
this.hangUpByPeerInfo = (peer, callback) => { | ||
if (!isOnline) { | ||
return callback(new Error(OFFLINE_ERROR_MESSAGE)) | ||
} | ||
|
||
this.peerBook.removeByB58String(peer.id.toB58String()) | ||
this.swarm.hangUp(peer, callback) | ||
} | ||
|
||
this.handle = (protocol, handlerFunc, matchFunc) => { | ||
return this.swarm.handle(protocol, handlerFunc, matchFunc) | ||
} | ||
|
||
this.unhandle = (protocol) => { | ||
return this.swarm.unhandle(protocol) | ||
} | ||
|
||
this.discovery = new EE() | ||
this.routing = null | ||
this.records = null | ||
} | ||
|
||
module.exports = Node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.