git ssb

15+

ansuz / dnssb



Tree: 8b277e861c3c9b30cb3e985a8b7fd3bdad237398

Files: 8b277e861c3c9b30cb3e985a8b7fd3bdad237398 / lib / publish.js

2296 bytesRaw
1var SsbRef = require("ssb-ref");
2
3var Publish = module.exports = {};
4
5var help = Publish.help = function () {
6 console.log("Try: ");
7 console.log("./publish.js name type data (class)");
8};
9
10var TYPES = Publish.TYPES = ['A', 'AAAA', 'CNAME', 'HINFO', 'ISDN', 'MX', 'NS', 'PTR', 'SOA', 'TXT'];
11
12var isValidType = Publish.isValidType = function (t) {
13 return TYPES.indexOf(t) !== -1;
14};
15
16var CLASSES = Publish.CLASSES = ['IN', 'CH'];
17
18var isValidClass = Publish.isValidClass = function (c) {
19 return CLASSES.indexOf(c) !== -1;
20};
21
22var endsInSSB = Publish.endsInSSB = function (s) {
23 return /\.ssb$/i.test(s);
24};
25
26var validateRecord = Publish.validateRecord = function (record) {
27 if (!endsInSSB(record.name)) { return "[Record NameError] records must end in .ssb"; }
28 if (!isValidType(record.type)) { return "[Record TypeError] " + record.type + " is not a valid dns type"; }
29 if (!isValidClass(record.class)) { return "[Record ClassError] class must be one of [" + CLASSES.join(', ') + "]"; }
30
31 // TODO perform stricter validation on data
32 if (!(record.data && typeof(record.data) === 'string')) { return "[Record DataError] expected data to publish"; }
33};
34
35var makeRecord = Publish.makeRecord = function (name, type, data, _class) {
36 return {
37 name: typeof(name) === 'string' && name.toLowerCase(), // domain names must be lowercase
38 type: type,
39 data: data,
40 class: _class || 'IN',
41 };
42};
43
44Publish.record = function (branches, name, type, data, _class, cb) {
45 var record = makeRecord(name, type, data, _class);
46
47 var complaint = validateRecord(record);
48 if (complaint) { return void cb(new Error(complaint)); }
49
50 if (!branches.every(SsbRef.isMsgId)) {
51 return void cb(new Error("invalid branches"));
52 }
53
54 require("ssb-client")(function (err, sbot) {
55 if (err) { return void cb(err); }
56
57 var val = {
58 type: "ssb-dns",
59 record: record
60 }
61 if (branches.length > 1) val.branch = branches;
62 else if (branches.length == 1) val.branch = branches[0];
63
64 // publish a message
65 sbot.publish(val, function (err, msg) {
66 if (err) { return void cb(err); }
67 sbot.close();
68 return void cb(err, msg);
69 })
70 });
71};
72
73

Built with git-ssb-web