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 28
WIP: Use the base class #41
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3299d71
feat: use base class
daviddias c438ad4
fix: mad tests missing with the `this value`
daviddias 7517d58
docs: update deps and docs
daviddias d0dd760
fix: update due to websockets dialer
daviddias 81af959
feat: expose Node class directly
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,202 +1,31 @@ | ||
'use strict' | ||
|
||
const Swarm = require('libp2p-swarm') | ||
const TCP = require('libp2p-tcp') | ||
// const UTP = require('libp2p-utp') | ||
const WS = require('libp2p-websockets') | ||
const spdy = require('libp2p-spdy') | ||
const secio = require('libp2p-secio') | ||
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 | ||
|
||
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) => { | ||
const ws = new WS() | ||
const tcp = new TCP() | ||
|
||
// Do not activate the dialer if no listener is going to be present | ||
if (ws.filter(this.peerInfo.multiaddrs).length > 0) { | ||
this.swarm.transport.add('ws', new WS()) | ||
} | ||
if (tcp.filter(this.peerInfo.multiaddrs).length > 0) { | ||
this.swarm.transport.add('tcp', new TCP()) | ||
} | ||
|
||
this.swarm.listen((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
isOnline = true | ||
callback() | ||
}) | ||
} | ||
|
||
this.stop = (callback) => { | ||
isOnline = false | ||
this.swarm.close(callback) | ||
const libp2p = require('libp2p') | ||
|
||
class Node extends libp2p.Node { | ||
constructor (peerInfo, peerBook) { | ||
const modules = { | ||
transport: [ | ||
new TCP(), | ||
new WS() | ||
], | ||
connection: { | ||
muxer: [ | ||
spdy | ||
], | ||
crypto: [ | ||
secio | ||
] | ||
}, | ||
discovery: [] | ||
} | ||
super(modules, peerInfo, peerBook) | ||
} | ||
|
||
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
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes inheritance is just awesome :)