Comparing version 1.0.0 to 1.0.1
@@ -182,2 +182,3 @@ var udp = require('udp-request') | ||
DHT.prototype._bootstrap = function () { | ||
// TODO: i'm guessing we need to rebootstrap after some timeout | ||
// TODO: check stats, to determine wheather to rerun? | ||
@@ -195,7 +196,3 @@ var self = this | ||
qs.on('data', update) | ||
qs.on('error', function (err) { | ||
self.emit('error', err) | ||
}) | ||
qs.on('error', noop) // noop this out as it'll bootstrap on subsequent runs | ||
qs.on('end', done) | ||
@@ -202,0 +199,0 @@ |
{ | ||
"name": "dht-rpc", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Make RPC calls over a Kademlia based DHT.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,7 @@ var stream = require('readable-stream') | ||
var BLANK = new Buffer([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
]) | ||
module.exports = QueryStream | ||
@@ -279,8 +284,8 @@ | ||
return { | ||
id: node.id, | ||
id: node.id || BLANK, | ||
port: node.port, | ||
host: node.host, | ||
roundtripToken: node.roundtripToken, | ||
referrer: node.referrer | ||
referrer: node.referrer || node.referer | ||
} | ||
} |
@@ -92,4 +92,70 @@ # dht-rpc | ||
## API | ||
#### `var node = dht([options])` | ||
Create a new DHT node. Options include | ||
``` js | ||
{ | ||
id: nodeId, // id of the node | ||
ephemeral: false, // will this node answer queries? | ||
bootstrap: ['host:port'], // bootstrap nodes | ||
socket: udpSocket // optional udp socket | ||
} | ||
``` | ||
#### `var stream = node.query(query, [options], [callback])` | ||
Create a new query. Query should look like this | ||
``` js | ||
{ | ||
command: 'command-to-run', | ||
target: new Buffer('32 byte target'), | ||
value: new Buffer('some payload') | ||
} | ||
``` | ||
And options include | ||
``` js | ||
{ | ||
nodes: [{host: 'example.com', port: 4224}], // only contact these nodes | ||
holepunching: true // set to false to disable hole punching | ||
} | ||
``` | ||
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])` | ||
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. | ||
#### `node.on('query:{command}', data, callback)` | ||
Called when a specific query is invoked on a node. `data` contains the same values as in the query above and also a `.node` property with info about the node invoking the query. | ||
Call the callback with `(err, value)` to respond. | ||
#### `node.on('closest:{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. | ||
#### `node.ready(callback)` | ||
Makes sure the initial bootstrap table has been built. You do not need to wait for this before querying. | ||
#### `node.holepunch(peer, referrer, callback)` | ||
UDP hole punch to another peer using the `referrer` as a STUN server. | ||
#### `node.destroy()` | ||
Destroy the dht node. Releases all resources. | ||
## License | ||
MIT |
92
test.js
@@ -67,2 +67,92 @@ var tape = require('tape') | ||
tape('targeted query', function (t) { | ||
bootstrap(function (port, node) { | ||
var a = dht({bootstrap: port}) | ||
a.on('query:echo', function (data, cb) { | ||
t.pass('in echo') | ||
cb(null, data.value) | ||
}) | ||
var b = dht({bootstrap: port}) | ||
b.on('query:echo', function (data, cb) { | ||
t.fail('should not hit me') | ||
cb() | ||
}) | ||
a.ready(function () { | ||
b.ready(function () { | ||
var client = dht({bootstrap: port}) | ||
client.query({ | ||
command: 'echo', | ||
value: new Buffer('hi'), | ||
target: client.id | ||
}, { | ||
node: { | ||
port: a.address().port, | ||
host: '127.0.0.1' | ||
} | ||
}, function (err, responses) { | ||
client.destroy() | ||
a.destroy() | ||
b.destroy() | ||
node.destroy() | ||
t.error(err, 'no error') | ||
t.same(responses.length, 1, 'one response') | ||
t.same(responses[0].value, new Buffer('hi'), 'echoed') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
tape('targeted closest', function (t) { | ||
bootstrap(function (port, node) { | ||
var a = dht({bootstrap: port}) | ||
a.on('closest:echo', function (data, cb) { | ||
t.pass('in echo') | ||
cb(null, data.value) | ||
}) | ||
var b = dht({bootstrap: port}) | ||
b.on('closest:echo', function (data, cb) { | ||
t.fail('should not hit me') | ||
cb() | ||
}) | ||
a.ready(function () { | ||
b.ready(function () { | ||
var client = dht({bootstrap: port}) | ||
client.closest({ | ||
command: 'echo', | ||
value: new Buffer('hi'), | ||
target: client.id | ||
}, { | ||
node: { | ||
port: a.address().port, | ||
host: '127.0.0.1' | ||
} | ||
}, function (err, responses) { | ||
client.destroy() | ||
a.destroy() | ||
b.destroy() | ||
node.destroy() | ||
t.error(err, 'no error') | ||
t.same(responses.length, 1, 'one response') | ||
t.same(responses[0].value, new Buffer('hi'), 'echoed') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
tape('swarm query', function (t) { | ||
@@ -128,5 +218,5 @@ bootstrap(function (port, node) { | ||
node.listen(10000, function () { | ||
node.listen(function () { | ||
done(node.address().port, node) | ||
}) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31676
849
161