This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92f8ef9
commit 860165c
Showing
17 changed files
with
428 additions
and
96 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 |
---|---|---|
@@ -0,0 +1,163 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const every = require('async/every') | ||
const PeerId = require('peer-id') | ||
const CID = require('cids') | ||
const each = require('async/each') | ||
// const bsplit = require('buffer-split') | ||
|
||
module.exports = (self) => { | ||
return { | ||
/** | ||
* Given a key, query the DHT for its best value. | ||
* | ||
* @param {Buffer} key | ||
* @param {function(Error)} [callback] | ||
* @returns {Promise|void} | ||
*/ | ||
get: promisify((key, options, callback) => { | ||
if (!Buffer.isBuffer(key)) { | ||
return callback(new Error('Not valid key')) | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
self._libp2pNode.dht.get(key, options.timeout, callback) | ||
}), | ||
|
||
/** | ||
* Write a key/value pair to the DHT. | ||
* | ||
* Given a key of the form /foo/bar and a value of any | ||
* form, this will write that value to the DHT with | ||
* that key. | ||
* | ||
* @param {Buffer} key | ||
* @param {Buffer} value | ||
* @param {function(Error)} [callback] | ||
* @returns {Promise|void} | ||
*/ | ||
put: promisify((key, value, callback) => { | ||
if (!Buffer.isBuffer(key)) { | ||
return callback(new Error('Not valid key')) | ||
} | ||
|
||
self._libp2pNode.dht.put(key, value, callback) | ||
}), | ||
|
||
/** | ||
* Find peers in the DHT that can provide a specific value, given a key. | ||
* | ||
* @param {CID} key - They key to find providers for. | ||
* @param {function(Error, Array<PeerInfo>)} [callback] | ||
* @returns {Promise<PeerInfo>|void} | ||
*/ | ||
findprovs: promisify((key, callback) => { | ||
if (typeof key === 'string') { | ||
key = new CID(key) | ||
} | ||
|
||
self._libp2pNode.contentRouting.findProviders(key, callback) | ||
}), | ||
|
||
/** | ||
* Query the DHT for all multiaddresses associated with a `PeerId`. | ||
* | ||
* @param {PeerId} peer - The id of the peer to search for. | ||
* @param {function(Error, Array<Multiaddr>)} [callback] | ||
* @returns {Promise<Array<Multiaddr>>|void} | ||
*/ | ||
findpeer: promisify((peer, callback) => { | ||
if (typeof peer === 'string') { | ||
peer = PeerId.createFromB58String(peer) | ||
} | ||
|
||
self._libp2pNode.peerRouting.findPeer(peer, (err, info) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
// convert to go-ipfs return value, we need to revisit | ||
// this. For now will just conform. | ||
const goResult = [ | ||
{ | ||
Responses: [{ | ||
ID: info.id.toB58String(), | ||
Addresses: info.multiaddrs.toArray().map((a) => a.toString()) | ||
}] | ||
} | ||
] | ||
|
||
callback(null, goResult) | ||
}) | ||
}), | ||
|
||
/** | ||
* Announce to the network that we are providing given values. | ||
* | ||
* @param {CID|Array<CID>} keys - The keys that should be announced. | ||
* @param {Object} [options={}] | ||
* @param {bool} [options.recursive=false] - Provide not only the given object but also all objects linked from it. | ||
* @param {function(Error)} [callback] | ||
* @returns {Promise|void} | ||
*/ | ||
provide: promisify((keys, options, callback) => { | ||
if (!Array.isArray(keys)) { | ||
keys = [keys] | ||
} | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
// ensure blocks are actually local | ||
every(keys, (key, cb) => { | ||
self._repo.blockstore.has(key, cb) | ||
}, (err, has) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
/* TODO reconsider this. go-ipfs provides anyway | ||
if (!has) { | ||
return callback(new Error('Not all blocks exist locally, can not provide')) | ||
} | ||
*/ | ||
|
||
if (options.recursive) { | ||
// TODO: Implement recursive providing | ||
} else { | ||
each(keys, (cid, cb) => { | ||
self._libp2pNode.contentRouting.provide(cid, cb) | ||
}, callback) | ||
} | ||
}) | ||
}), | ||
|
||
/** | ||
* Find the closest peers to a given `PeerId`, by querying the DHT. | ||
* | ||
* @param {PeerId} peer - The `PeerId` to run the query agains. | ||
* @param {function(Error, Array<PeerId>)} [callback] | ||
* @returns {Promise<Array<PeerId>>|void} | ||
*/ | ||
query: promisify((peerId, callback) => { | ||
if (typeof peerId === 'string') { | ||
peerId = PeerId.createFromB58String(peerId) | ||
} | ||
|
||
// TODO expose this method in peerRouting | ||
self._libp2pNode._dht.getClosestPeers(peerId.toBytes(), (err, peerIds) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, peerIds.map((id) => { | ||
return { ID: id.toB58String() } | ||
})) | ||
}) | ||
}) | ||
} | ||
} |
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
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,22 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
/* | ||
const test = require('interface-ipfs-core') | ||
const IPFSFactory = require('../../utils/ipfs-factory-instance') | ||
let factory | ||
const common = { | ||
setup: function (callback) { | ||
factory = new IPFSFactory() | ||
callback(null, factory) | ||
}, | ||
teardown: function (callback) { | ||
factory.dismantle(callback) | ||
} | ||
} | ||
test.dht(common) | ||
*/ |
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.