ipfs-bitswap
Advanced tools
Comparing version 0.2.0 to 0.2.1
19
API.md
@@ -28,4 +28,21 @@ # API | ||
Fetch multiple blocks. | ||
Fetch multiple blocks. The `cb` is called with a result object of the form | ||
```js | ||
{ | ||
[key1]: {error: errorOrUndefined, block: blockOrUndefined}, | ||
[key2]: {error: errorOrUndefined, block: blockOrUndefined}, | ||
... | ||
} | ||
``` | ||
Where `key<i>` is the multihash of the block. | ||
### `unwantBlocks(keys)` | ||
- `keys: []Multihash` | ||
Cancel previously requested keys, forcefully. That means they are removed from the | ||
wantlist independent of how many other resources requested these keys. Callbacks | ||
attached to `getBlock` are errored with `Error('manual unwant: key')`. | ||
### `cancelWants(keys)` | ||
@@ -32,0 +49,0 @@ |
@@ -69,3 +69,2 @@ 'use strict'; | ||
_this.datastore.get(nextTask.entry.key, function (err, block) { | ||
log('fetched: %s', block.key.toString('hex'), block.data.toString()); | ||
if (err || !block) { | ||
@@ -72,0 +71,0 @@ nextTask.done(); |
@@ -193,8 +193,7 @@ 'use strict'; | ||
this.getBlocks([key], function (err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
this.getBlocks([key], function (results) { | ||
var err = results[key].error; | ||
var block = results[key].block; | ||
done(null, res[0]); | ||
done(err, block); | ||
}); | ||
@@ -215,14 +214,51 @@ } | ||
var blocks = []; | ||
var finish = function finish(block) { | ||
blocks.push(block); | ||
log('finish: %s/%s', blocks.length, keys.length); | ||
if (blocks.length === keys.length) { | ||
cb(null, blocks); | ||
var results = {}; | ||
var unwantListeners = {}; | ||
var blockListeners = {}; | ||
var unwantEvent = function unwantEvent(key) { | ||
return 'unwant:' + key.toString('hex'); | ||
}; | ||
var blockEvent = function blockEvent(key) { | ||
return 'block:' + key.toString('hex'); | ||
}; | ||
var cleanupListeners = function cleanupListeners() { | ||
keys.forEach(function (key) { | ||
_this4.notifications.removeListener(unwantEvent(key), unwantListeners[key]); | ||
_this4.notifications.removeListener(blockEvent(key), blockListeners[key]); | ||
}); | ||
}; | ||
var addListeners = function addListeners() { | ||
keys.forEach(function (key) { | ||
unwantListeners[key] = function () { | ||
finish(key, new Error('manual unwant: ' + key.toString('hex'))); | ||
}; | ||
blockListeners[key] = function (block) { | ||
finish(key, null, block); | ||
}; | ||
_this4.notifications.once(unwantEvent(key), unwantListeners[key]); | ||
_this4.notifications.once(blockEvent(key), blockListeners[key]); | ||
}); | ||
}; | ||
var finish = function finish(key, err, block) { | ||
results[key] = { | ||
error: err, | ||
block: block | ||
}; | ||
if (Object.keys(results).length === keys.length) { | ||
cleanupListeners(); | ||
cb(results); | ||
} | ||
}; | ||
addListeners(); | ||
keys.forEach(function (key) { | ||
// Sanity check, we don't want to announce looking for blocks | ||
// when we might have them ourselves | ||
// We don't want to announce looking for blocks | ||
// when we might have them ourselves. | ||
_this4.datastore.has(key, function (err, exists) { | ||
@@ -237,4 +273,4 @@ if (err) { | ||
if (!err && res) { | ||
finish(key, null, res); | ||
_this4.wm.cancelWants([key]); | ||
finish(res); | ||
return; | ||
@@ -249,5 +285,2 @@ } | ||
}); | ||
_this4.notifications.once('block:' + key.toString('hex'), function (block) { | ||
finish(block); | ||
}); | ||
}); | ||
@@ -258,2 +291,15 @@ | ||
// removes the given keys from the want list independent of any ref counts | ||
}, { | ||
key: 'unwantBlocks', | ||
value: function unwantBlocks(keys) { | ||
var _this5 = this; | ||
this.wm.unwantBlocks(keys); | ||
keys.forEach(function (key) { | ||
_this5.notifications.emit('unwant:' + key.toString('hex')); | ||
}); | ||
} | ||
// removes the given keys from the want list | ||
@@ -272,3 +318,3 @@ | ||
value: function hasBlock(block, cb) { | ||
var _this5 = this; | ||
var _this6 = this; | ||
@@ -283,4 +329,4 @@ cb = cb || function () {}; | ||
log('put block: %s', block.key.toString('hex')); | ||
_this5.notifications.emit('block:' + block.key.toString('hex'), block); | ||
_this5.engine.receivedBlock(block); | ||
_this6.notifications.emit('block:' + block.key.toString('hex'), block); | ||
_this6.engine.receivedBlock(block); | ||
cb(); | ||
@@ -287,0 +333,0 @@ }); |
@@ -43,2 +43,9 @@ 'use strict'; | ||
}, { | ||
key: 'removeForce', | ||
value: function removeForce(key) { | ||
if (this.set.has(key)) { | ||
this.set.delete(key); | ||
} | ||
} | ||
}, { | ||
key: 'entries', | ||
@@ -45,0 +52,0 @@ value: function entries() { |
@@ -35,3 +35,3 @@ 'use strict'; | ||
key: '_addEntries', | ||
value: function _addEntries(keys, cancel) { | ||
value: function _addEntries(keys, cancel, force) { | ||
var _this = this; | ||
@@ -46,3 +46,7 @@ | ||
if (e.cancel) { | ||
_this.wl.remove(e.key); | ||
if (force) { | ||
_this.wl.removeForce(e.key); | ||
} else { | ||
_this.wl.remove(e.key); | ||
} | ||
} else { | ||
@@ -151,2 +155,11 @@ _this.wl.add(e.key, e.priority); | ||
// remove blocks of all the given keys without respecting refcounts | ||
}, { | ||
key: 'unwantBlocks', | ||
value: function unwantBlocks(keys) { | ||
log('unwant blocks:', keys); | ||
this._addEntries(keys, true, true); | ||
} | ||
// cancel wanting all of the given keys | ||
@@ -153,0 +166,0 @@ |
{ | ||
"name": "ipfs-bitswap", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Node.js implementation of the Bitswap data exchange protocol used by IPFS", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -58,3 +58,2 @@ 'use strict' | ||
this.datastore.get(nextTask.entry.key, (err, block) => { | ||
log('fetched: %s', block.key.toString('hex'), block.data.toString()) | ||
if (err || !block) { | ||
@@ -61,0 +60,0 @@ nextTask.done() |
@@ -141,8 +141,7 @@ 'use strict' | ||
this.getBlocks([key], (err, res) => { | ||
if (err) { | ||
return done(err) | ||
} | ||
this.getBlocks([key], (results) => { | ||
const err = results[key].error | ||
const block = results[key].block | ||
done(null, res[0]) | ||
done(err, block) | ||
}) | ||
@@ -157,14 +156,47 @@ } | ||
getBlocks (keys, cb) { | ||
const blocks = [] | ||
const finish = (block) => { | ||
blocks.push(block) | ||
log('finish: %s/%s', blocks.length, keys.length) | ||
if (blocks.length === keys.length) { | ||
cb(null, blocks) | ||
const results = {} | ||
const unwantListeners = {} | ||
const blockListeners = {} | ||
const unwantEvent = (key) => `unwant:${key.toString('hex')}` | ||
const blockEvent = (key) => `block:${key.toString('hex')}` | ||
const cleanupListeners = () => { | ||
keys.forEach((key) => { | ||
this.notifications.removeListener(unwantEvent(key), unwantListeners[key]) | ||
this.notifications.removeListener(blockEvent(key), blockListeners[key]) | ||
}) | ||
} | ||
const addListeners = () => { | ||
keys.forEach((key) => { | ||
unwantListeners[key] = () => { | ||
finish(key, new Error(`manual unwant: ${key.toString('hex')}`)) | ||
} | ||
blockListeners[key] = (block) => { | ||
finish(key, null, block) | ||
} | ||
this.notifications.once(unwantEvent(key), unwantListeners[key]) | ||
this.notifications.once(blockEvent(key), blockListeners[key]) | ||
}) | ||
} | ||
const finish = (key, err, block) => { | ||
results[key] = { | ||
error: err, | ||
block: block | ||
} | ||
if (Object.keys(results).length === keys.length) { | ||
cleanupListeners() | ||
cb(results) | ||
} | ||
} | ||
addListeners() | ||
keys.forEach((key) => { | ||
// Sanity check, we don't want to announce looking for blocks | ||
// when we might have them ourselves | ||
// We don't want to announce looking for blocks | ||
// when we might have them ourselves. | ||
this.datastore.has(key, (err, exists) => { | ||
@@ -179,4 +211,4 @@ if (err) { | ||
if (!err && res) { | ||
finish(key, null, res) | ||
this.wm.cancelWants([key]) | ||
finish(res) | ||
return | ||
@@ -191,5 +223,2 @@ } | ||
}) | ||
this.notifications.once(`block:${key.toString('hex')}`, (block) => { | ||
finish(block) | ||
}) | ||
}) | ||
@@ -200,2 +229,10 @@ | ||
// removes the given keys from the want list independent of any ref counts | ||
unwantBlocks (keys) { | ||
this.wm.unwantBlocks(keys) | ||
keys.forEach((key) => { | ||
this.notifications.emit(`unwant:${key.toString('hex')}`) | ||
}) | ||
} | ||
// removes the given keys from the want list | ||
@@ -202,0 +239,0 @@ cancelWants (keys) { |
@@ -38,2 +38,8 @@ 'use strict' | ||
removeForce (key) { | ||
if (this.set.has(key)) { | ||
this.set.delete(key) | ||
} | ||
} | ||
entries () { | ||
@@ -40,0 +46,0 @@ return this.set.entries() |
@@ -26,3 +26,3 @@ 'use strict' | ||
_addEntries (keys, cancel) { | ||
_addEntries (keys, cancel, force) { | ||
let i = -1 | ||
@@ -37,3 +37,7 @@ _(keys) | ||
if (e.cancel) { | ||
this.wl.remove(e.key) | ||
if (force) { | ||
this.wl.removeForce(e.key) | ||
} else { | ||
this.wl.remove(e.key) | ||
} | ||
} else { | ||
@@ -95,2 +99,8 @@ this.wl.add(e.key, e.priority) | ||
// remove blocks of all the given keys without respecting refcounts | ||
unwantBlocks (keys) { | ||
log('unwant blocks:', keys) | ||
this._addEntries(keys, true, true) | ||
} | ||
// cancel wanting all of the given keys | ||
@@ -97,0 +107,0 @@ cancelWants (keys) { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
2889135
85567