-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dialProtocol and small refactor
- Loading branch information
Showing
5 changed files
with
143 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
module.exports = (node) => { | ||
return { | ||
findProviders: (key, timeout, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.findProviders(key, timeout, callback) | ||
}, | ||
provide: (key, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.provide(key, callback) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
|
||
module.exports = (node) => { | ||
return { | ||
put: (key, value, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.put(key, value, callback) | ||
}, | ||
get: (key, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.get(key, callback) | ||
}, | ||
getMany (key, nVals, callback) { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.getMany(key, nVals, callback) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
const multiaddr = require('multiaddr') | ||
|
||
module.exports = (node) => { | ||
/* | ||
* Helper method to check the data type of peer and convert it to PeerInfo | ||
*/ | ||
return function (peer, callback) { | ||
let p | ||
// PeerInfo | ||
if (PeerInfo.isPeerInfo(peer)) { | ||
p = peer | ||
// Multiaddr instance or Multiaddr String | ||
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') { | ||
if (typeof peer === 'string') { | ||
peer = multiaddr(peer) | ||
} | ||
|
||
const peerIdB58Str = peer.getPeerId() | ||
if (!peerIdB58Str) { | ||
throw new Error(`peer multiaddr instance or string must include peerId`) | ||
} | ||
|
||
try { | ||
p = node.peerBook.get(peerIdB58Str) | ||
} catch (err) { | ||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str)) | ||
} | ||
p.multiaddrs.add(peer) | ||
|
||
// PeerId | ||
} else if (PeerId.isPeerId(peer)) { | ||
const peerIdB58Str = peer.toB58String() | ||
try { | ||
p = node.peerBook.get(peerIdB58Str) | ||
} catch (err) { | ||
return node.peerRouting.findPeer(peer, callback) | ||
} | ||
} else { | ||
return setImmediate(() => callback(new Error('peer type not recognized'))) | ||
} | ||
|
||
setImmediate(() => callback(null, p)) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
module.exports = (node) => { | ||
return { | ||
findPeer: (id, callback) => { | ||
if (!node._dht) { | ||
return callback(new Error('DHT is not available')) | ||
} | ||
|
||
node._dht.findPeer(id, callback) | ||
} | ||
} | ||
} |