-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspider.js
More file actions
32 lines (30 loc) · 841 Bytes
/
spider.js
File metadata and controls
32 lines (30 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const DHT = require('.');
class Spider extends DHT {
neighbor(id = this.id, n = 18) {
return Buffer.concat([id.slice(0, n), this.id.slice(n)]);
}
/**
* makeNeighbor
*/
makeNeighbor() {
if (this.table.length < 50)
return this.join();
const target = DHT.randomID();
const nodes = this.table.get(target);
return nodes.map(node => {
const id = this.neighbor(node.id);
return this.query('find_node', { id, target }, [node]).catch(e => e);
});
}
onPing(request, response) {
const { id } = request;
response({ id: this.neighbor(id) });
}
onFindNode(request, response) {
const { id, target } = request;
let nodes = this.get(target);
nodes = Buffer.concat(nodes.map(DHT.Node.encode));
response({ id: this.neighbor(id), nodes });
}
}
module.exports = Spider;