Browse Source

closest -> update

v4
Mathias Buus 8 years ago
parent
commit
963510c3e9
  1. 18
      README.md
  2. 32
      index.js
  3. 18
      test.js

18
README.md

@ -39,7 +39,7 @@ function createNode () {
var values = {}
// When we are the closest node and someone is sending us a "store" command
node.on('closest:store', function (query, cb) {
node.on('update:values', function (query, cb) {
if (!query.value) return cb()
// Use the hash of the value as the key
@ -50,7 +50,7 @@ function createNode () {
})
// When someone is querying for a "lookup" command
node.on('query:lookup', function (query, cb) {
node.on('query:values', function (query, cb) {
var value = values[query.target.toString('hex')]
cb(null, value)
})
@ -67,7 +67,7 @@ To insert a value into this dht make another script that does this following
// Set ephemeral: true as we are not part of the network.
var node = dht({ephemeral: true})
node.closest({command: 'store', target: sha256(val), value: val}, function (err, res) {
node.update({command: 'values', target: sha256(val), value: val}, function (err, res) {
if (err) throw err
console.log('Inserted', sha256(val).toString('hex'))
})
@ -76,7 +76,7 @@ node.closest({command: 'store', target: sha256(val), value: val}, function (err,
Then after inserting run this script to query for a value
``` js
node.query({command: 'lookup', target: new Buffer(hexFromAbove, 'hex')})
node.query({command: 'values', target: new Buffer(hexFromAbove, 'hex')})
.on('data', function (data) {
if (data.value && sha256(data.value).toString('hex') === hexFromAbove) {
// We found the value! Destroy the query stream as there is no need to continue.
@ -128,10 +128,10 @@ And options include
The stream will emit query results as they arrive. If you backpressure the query it will backpressure the query as well.
Call `.destroy()` on the stream to cancel the query. If you pass the callback the streams payload will be buffered and passed to that.
#### `var stream = node.closest(query, [options], [callback])`
#### `var stream = node.update(query, [options], [callback])`
Same as a query but will trigger a closest query on the 20 closest nodes (distance between node ids and target) after the query finishes.
Per default the stream will only contain results from the closest query. To include the query results also pass the `verbose: true` option.
Same as a query but will trigger an update query on the 20 closest nodes (distance between node ids and target) after the query finishes.
Per default the stream will only contain results from the closest query. To include the query results also pass the `query: true` option.
#### `node.on('query:{command}', data, callback)`
@ -139,9 +139,9 @@ Called when a specific query is invoked on a node. `data` contains the same valu
Call the callback with `(err, value)` to respond.
#### `node.on('closest:{command}', data, callback)`
#### `node.on('update:{command}', data, callback)`
Called when a closest query is invoked. The `data.node` is also guaranteed to have roundtripped to this dht before, meaning that you can trust that the host, port was not spoofed.
Called when an update query is invoked. The `data.node` is also guaranteed to have roundtripped to this dht before, meaning that you can trust that the host, port was not spoofed.
#### `node.ready(callback)`

32
index.js

@ -99,9 +99,10 @@ DHT.prototype.query = function (query, opts, cb) {
return collect(queryStream(this, query, opts), cb)
}
DHT.prototype.closest = function (query, opts, cb) {
if (typeof opts === 'function') return this.closest(query, null, opts)
DHT.prototype.update = function (query, opts, cb) {
if (typeof opts === 'function') return this.update(query, null, opts)
if (!opts) opts = {}
if (opts.query) opts.verbose = true
opts.token = true
return collect(queryStream(this, query, opts), cb)
}
@ -120,31 +121,6 @@ DHT.prototype._pingSome = function () {
}
}
DHT.prototype._closestNodes = function (target, opts, cb) {
var nodes = opts.nodes || opts.node
if (nodes) {
if (!Array.isArray(nodes)) nodes = [nodes]
process.nextTick(function () {
cb(null, nodes)
})
return null
}
var qs = this.get({
command: '_find_node',
target: target
})
qs.resume()
qs.on('error', noop)
qs.on('end', function () {
cb(null, qs.closest)
})
return qs
}
DHT.prototype.holepunch = function (peer, referrer, cb) {
this._holepunch(parseAddr(peer), parseAddr(referrer), cb)
}
@ -297,7 +273,7 @@ DHT.prototype._onquery = function (request, peer) {
roundtripToken: request.roundtripToken
}
var method = request.roundtripToken ? 'closest' : 'query'
var method = request.roundtripToken ? 'update' : 'query'
if (!this.emit(method + ':' + request.command, query, callback) && !this.emit(method, query, callback)) callback()

18
test.js

@ -2,12 +2,12 @@ var tape = require('tape')
var dht = require('./')
var crypto = require('crypto')
tape('simple closest', function (t) {
tape('simple update', function (t) {
bootstrap(function (port, node) {
var a = dht({bootstrap: port})
var b = dht({bootstrap: port})
a.on('closest:echo', function (data, callback) {
a.on('update:echo', function (data, callback) {
t.ok(data.roundtripToken, 'has roundtrip token')
t.same(data.value, new Buffer('Hello, World!'), 'expected data')
callback(null, data.value)
@ -20,7 +20,7 @@ tape('simple closest', function (t) {
value: new Buffer('Hello, World!')
}
b.closest(data, function (err, responses) {
b.update(data, function (err, responses) {
a.destroy()
b.destroy()
node.destroy()
@ -109,18 +109,18 @@ tape('targeted query', function (t) {
})
})
tape('targeted closest', function (t) {
tape('targeted update', function (t) {
bootstrap(function (port, node) {
var a = dht({bootstrap: port})
a.on('closest:echo', function (data, cb) {
a.on('update:echo', function (data, cb) {
t.pass('in echo')
cb(null, data.value)
})
var b = dht({bootstrap: port})
b.on('closest:echo', function (data, cb) {
b.on('update:echo', function (data, cb) {
t.fail('should not hit me')
cb()
})
@ -129,7 +129,7 @@ tape('targeted closest', function (t) {
b.ready(function () {
var client = dht({bootstrap: port})
client.closest({
client.update({
command: 'echo',
value: new Buffer('hi'),
target: client.id
@ -166,7 +166,7 @@ tape('swarm query', function (t) {
var key = crypto.createHash('sha256').update('hello').digest()
var me = dht({bootstrap: port})
me.closest({command: 'kv', target: key, value: new Buffer('hello')}, function (err, responses) {
me.update({command: 'kv', target: key, value: new Buffer('hello')}, function (err, responses) {
t.error(err, 'no error')
t.same(closest, 20, '20 closest nodes')
t.same(responses.length, 20, '20 responses')
@ -195,7 +195,7 @@ tape('swarm query', function (t) {
var value = null
node.on('closest:kv', function (data, cb) {
node.on('update:kv', function (data, cb) {
closest++
value = data.value
cb()

Loading…
Cancel
Save