git ssb

15+

ansuz / dnssb



Commit 07c65eb6badf7eed11d56d8791156f8e161528ec

Refactor querying

- Factor lib/ssb-dns.js into lib/query.js
- Remove code dupliation
cel committed on 11/5/2016, 5:03:30 PM
Parent: c6437ca992f64f9237f1517743bd9713e165a004

Files changed

lib/dump.jschanged
lib/query.jschanged
lib/server.jschanged
lib/ssb-dns.jsdeleted
lib/dump.jsView
@@ -1,20 +1,17 @@
11 var Pull = require("pull-stream");
2-var Ansuz = require("ansuz");
2 +var Query = require("./query");
33 var Dump = module.exports = {};
4-var SsbDns = require("./ssb-dns");
54
65 Dump.records = function (each, done) {
76 var Client = require("ssb-client");
87
98 Client(function (err, sbot) {
109 if (err) { return void done(err); }
1110
12- Pull(sbot.messagesByType({
13- type: 'ssb-dns',
14- }),
15- SsbDns(function (err, records) {
16- records.forEach(each);
17- done(sbot);
11 + Pull(Query.all(),
12 + Pull.drain(each, function (err) {
13 + if (err) throw err;
14 + done(sbot);
1815 }));
1916 });
2017 };
lib/query.jsView
@@ -1,32 +1,71 @@
11 var Pull = require("pull-stream");
22 var KVSet = require("kvset");
3 +var Pad = require("pad-ipv6");
34 var Query = module.exports = {};
45
56 Query.branches = function (name, type, _class, cb) {
67 var Client = require("ssb-client");
78
89 Client(function (err, sbot) {
910 if (err) return cb(err);
1011
11- var set = new KVSet()
12- Pull(sbot.messagesByType({
13- type: 'ssb-dns',
14- }),
15- Pull.filter(function (msg) {
16- var c = msg.value.content;
17- var record = c && c.record;
18- return record
19- && record.name === name
20- && record.type === type
21- && (!_class || _class === (record.class || 'IN'));
22- }),
23- Pull.drain(function (msg) {
24- var c = msg.value.content;
25- if (c.branch) set.remove(c.branch);
26- set.add(msg.key);
27- }, function (err) {
12 + var question = {name: name, type: type, class: _class || "IN"};
13 + Pull(Query.query(sbot, question, function (err, recs) {
2814 if (err) return cb(err);
29- cb(null, Object.keys(set.heads));
15 + cb(null, recs.map(function (record) {
16 + return record.key;
17 + }))
3018 }));
3119 });
3220 };
21 +
22 +function fixRecord(r) {
23 + if (!r.ttl) r.ttl = 500;
24 + if (!r.class) r.class = "IN";
25 + if (r.value) r.data = r.value, delete r.value
26 + if (r.type === 'AAAA') r.data = Pad(r.data);
27 +}
28 +
29 +Query.all = function (sbot) {
30 + return sbot.messagesByType({
31 + type: 'ssb-dns',
32 + });
33 +};
34 +
35 +Query.matches = function (question) {
36 + return Pull.filter(function (msg) {
37 + var c = msg.value.content;
38 + var record = c && c.record;
39 + return record
40 + && record.name === question.name
41 + && record.type === question.type
42 + && record.class === question.class;
43 + });
44 +}
45 +
46 +Query.records = function (sbot, cb) {
47 + var set = new KVSet();
48 + return Pull.drain(function (msg) {
49 + var c = msg.value.content;
50 + if (c.branch) set.remove(c.branch);
51 + set.add(msg.key, msg.value);
52 + }, function (err) {
53 + var records = [];
54 + for (var key in set.heads) {
55 + var value = set.heads[key];
56 + var c = value.content;
57 + var record = c && c.record;
58 + if (!record) continue;
59 + record.id = key;
60 + fixRecord(record);
61 + records.push(record);
62 + }
63 + cb(err, records);
64 + });
65 +};
66 +
67 +Query.query = function (sbot, question, cb) {
68 + return Pull(Query.all(sbot),
69 + Query.matches(question),
70 + Query.records(sbot, cb));
71 +};
lib/server.jsView
@@ -1,7 +1,7 @@
11 var Pull = require("pull-stream");
22 var Ansuz = require("ansuz");
3-var SsbDns = require("./ssb-dns");
3 +var Query = require("./query");
44
55 var Server = module.exports = {};
66
77 var log = {
@@ -19,8 +19,9 @@
1919
2020 var answer = function (sbot, req, res, opt) {
2121 log.req(req, opt);
2222
23 + // one query per dns message
2324 var q = req.question;
2425
2526 // TODO validate queries more carefully
2627 if (!q.length) {
@@ -28,38 +29,27 @@
2829 res.end();
2930 return;
3031 }
3132
32- var qMap = {}
33- q.forEach(function (q) {
34- qMap[q.name.toLowerCase() + ':' + q.type] = true;
35- });
36-
37- Pull(sbot.messagesByType({
38- type: 'ssb-dns',
39- }),
40- Pull.filter(function (msg) {
41- var c = msg.value.content;
42- var record = c && c.record;
43- return record && qMap[record.name + ':' + record.type];
44- }),
45- SsbDns(function (err, records) {
33 + Query.query(sbot, q[0], function (err, records) {
34 + if (err) console.error(err);
4635 if (opt && opt.verbose) {
47- var names = q.map(function (q) { return q.name }).join(', ');
4836 var types = records.map(function (r) { return r.type }).join(', ');
4937 var vals = records.map(function (r) { return r.value }).join(', ');
50- console.log("%s (%s) => %s", names, types, vals);
38 + console.log("%s (%s) => %s", q[0].name, types, vals);
5139 }
5240 res.answer = records;
5341 res.end();
54- }));
42 + });
5543 };
5644
5745 var createServer = Server.create = function (sbot, port, host, cb, opt) {
5846 var Dnsd = require("dnsd");
59- return Dnsd.createServer(function(req, res) {
47 + var server = Dnsd.createServer(function(req, res) {
6048 answer(sbot, req, res, opt);
61- }).listen(port, host, cb);
49 + });
50 +
51 + return server.listen(port, host, cb);
6252 };
6353
6454 Server.listen = function (sbot, port, host, cb, opt) {
6555 var server = createServer(sbot, port, host, cb, opt);
lib/ssb-dns.jsView
@@ -1,31 +1,0 @@
1-var Pull = require("pull-stream");
2-var KVSet = require("kvset");
3-var Pad = require("pad-ipv6");
4-
5-function fixRecord(r) {
6- if (!r.ttl) r.ttl = 500;
7- if (!r.class) r.class = "IN";
8- if (r.value) r.data = r.value, delete r.value
9- if (r.type === 'AAAA') r.data = Pad(r.data);
10-}
11-
12-module.exports = function SsbDns(cb) {
13- var set = new KVSet()
14- return Pull.drain(function (msg) {
15- var c = msg.value.content;
16- if (c.branch) set.remove(c.branch);
17- set.add(msg.key, msg.value);
18- }, function (err) {
19- var records = [];
20- for (var key in set.heads) {
21- var value = set.heads[key];
22- var c = value.content;
23- var record = c && c.record;
24- if (!record) continue;
25- record.id = key;
26- fixRecord(record);
27- records.push(record);
28- }
29- cb(err, records);
30- });
31-};

Built with git-ssb-web