|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const promisify = require('promisify-es6') |
| 4 | +const every = require('async/every') |
| 5 | +const PeerId = require('peer-id') |
| 6 | +const CID = require('cids') |
| 7 | +const each = require('async/each') |
| 8 | +// const bsplit = require('buffer-split') |
| 9 | + |
| 10 | +module.exports = (self) => { |
| 11 | + return { |
| 12 | + /** |
| 13 | + * Given a key, query the DHT for its best value. |
| 14 | + * |
| 15 | + * @param {Buffer} key |
| 16 | + * @param {function(Error)} [callback] |
| 17 | + * @returns {Promise|void} |
| 18 | + */ |
| 19 | + get: promisify((key, options, callback) => { |
| 20 | + if (!Buffer.isBuffer(key)) { |
| 21 | + return callback(new Error('Not valid key')) |
| 22 | + } |
| 23 | + |
| 24 | + if (typeof options === 'function') { |
| 25 | + callback = options |
| 26 | + options = {} |
| 27 | + } |
| 28 | + |
| 29 | + self._libp2pNode.dht.get(key, options.timeout, callback) |
| 30 | + }), |
| 31 | + |
| 32 | + /** |
| 33 | + * Write a key/value pair to the DHT. |
| 34 | + * |
| 35 | + * Given a key of the form /foo/bar and a value of any |
| 36 | + * form, this will write that value to the DHT with |
| 37 | + * that key. |
| 38 | + * |
| 39 | + * @param {Buffer} key |
| 40 | + * @param {Buffer} value |
| 41 | + * @param {function(Error)} [callback] |
| 42 | + * @returns {Promise|void} |
| 43 | + */ |
| 44 | + put: promisify((key, value, callback) => { |
| 45 | + if (!Buffer.isBuffer(key)) { |
| 46 | + return callback(new Error('Not valid key')) |
| 47 | + } |
| 48 | + |
| 49 | + self._libp2pNode.dht.put(key, value, callback) |
| 50 | + }), |
| 51 | + |
| 52 | + /** |
| 53 | + * Find peers in the DHT that can provide a specific value, given a key. |
| 54 | + * |
| 55 | + * @param {CID} key - They key to find providers for. |
| 56 | + * @param {function(Error, Array<PeerInfo>)} [callback] |
| 57 | + * @returns {Promise<PeerInfo>|void} |
| 58 | + */ |
| 59 | + findprovs: promisify((key, callback) => { |
| 60 | + if (typeof key === 'string') { |
| 61 | + key = new CID(key) |
| 62 | + } |
| 63 | + |
| 64 | + self._libp2pNode.contentRouting.findProviders(key, callback) |
| 65 | + }), |
| 66 | + |
| 67 | + /** |
| 68 | + * Query the DHT for all multiaddresses associated with a `PeerId`. |
| 69 | + * |
| 70 | + * @param {PeerId} peer - The id of the peer to search for. |
| 71 | + * @param {function(Error, Array<Multiaddr>)} [callback] |
| 72 | + * @returns {Promise<Array<Multiaddr>>|void} |
| 73 | + */ |
| 74 | + findpeer: promisify((peer, callback) => { |
| 75 | + if (typeof peer === 'string') { |
| 76 | + peer = PeerId.createFromB58String(peer) |
| 77 | + } |
| 78 | + |
| 79 | + self._libp2pNode.peerRouting.findPeer(peer, (err, info) => { |
| 80 | + if (err) { |
| 81 | + return callback(err) |
| 82 | + } |
| 83 | + |
| 84 | + // convert to go-ipfs return value, we need to revisit |
| 85 | + // this. For now will just conform. |
| 86 | + const goResult = [ |
| 87 | + { |
| 88 | + Responses: [{ |
| 89 | + ID: info.id.toB58String(), |
| 90 | + Addresses: info.multiaddrs.toArray().map((a) => a.toString()) |
| 91 | + }] |
| 92 | + } |
| 93 | + ] |
| 94 | + |
| 95 | + callback(null, goResult) |
| 96 | + }) |
| 97 | + }), |
| 98 | + |
| 99 | + /** |
| 100 | + * Announce to the network that we are providing given values. |
| 101 | + * |
| 102 | + * @param {CID|Array<CID>} keys - The keys that should be announced. |
| 103 | + * @param {Object} [options={}] |
| 104 | + * @param {bool} [options.recursive=false] - Provide not only the given object but also all objects linked from it. |
| 105 | + * @param {function(Error)} [callback] |
| 106 | + * @returns {Promise|void} |
| 107 | + */ |
| 108 | + provide: promisify((keys, options, callback) => { |
| 109 | + if (!Array.isArray(keys)) { |
| 110 | + keys = [keys] |
| 111 | + } |
| 112 | + if (typeof options === 'function') { |
| 113 | + callback = options |
| 114 | + options = {} |
| 115 | + } |
| 116 | + |
| 117 | + // ensure blocks are actually local |
| 118 | + every(keys, (key, cb) => { |
| 119 | + self._repo.blockstore.has(key, cb) |
| 120 | + }, (err, has) => { |
| 121 | + if (err) { |
| 122 | + return callback(err) |
| 123 | + } |
| 124 | + /* TODO reconsider this. go-ipfs provides anyway |
| 125 | + if (!has) { |
| 126 | + return callback(new Error('Not all blocks exist locally, can not provide')) |
| 127 | + } |
| 128 | + */ |
| 129 | + |
| 130 | + if (options.recursive) { |
| 131 | + // TODO: Implement recursive providing |
| 132 | + } else { |
| 133 | + each(keys, (cid, cb) => { |
| 134 | + self._libp2pNode.contentRouting.provide(cid, cb) |
| 135 | + }, callback) |
| 136 | + } |
| 137 | + }) |
| 138 | + }), |
| 139 | + |
| 140 | + /** |
| 141 | + * Find the closest peers to a given `PeerId`, by querying the DHT. |
| 142 | + * |
| 143 | + * @param {PeerId} peer - The `PeerId` to run the query agains. |
| 144 | + * @param {function(Error, Array<PeerId>)} [callback] |
| 145 | + * @returns {Promise<Array<PeerId>>|void} |
| 146 | + */ |
| 147 | + query: promisify((peerId, callback) => { |
| 148 | + if (typeof peerId === 'string') { |
| 149 | + peerId = PeerId.createFromB58String(peerId) |
| 150 | + } |
| 151 | + |
| 152 | + // TODO expose this method in peerRouting |
| 153 | + self._libp2pNode._dht.getClosestPeers(peerId.toBytes(), (err, peerIds) => { |
| 154 | + if (err) { |
| 155 | + return callback(err) |
| 156 | + } |
| 157 | + callback(null, peerIds.map((id) => { |
| 158 | + return { ID: id.toB58String() } |
| 159 | + })) |
| 160 | + }) |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
0 commit comments