git ssb

15+

ansuz / dnssb



Tree: 5166584127078cfd0d1f1b37acff5e540c24ddce

Files: 5166584127078cfd0d1f1b37acff5e540c24ddce / lib / publish.js

2000 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', 'SRV', 'SSHFP', 'DS', 'SPF'];
11
12var isValidType = Publish.isValidType = function (t) {
13 return TYPES.indexOf(t) !== -1;
14};
15
16var CLASSES = Publish.CLASSES = ['IN', 'CH', 'NONE'];
17
18var isValidClass = Publish.isValidClass = function (c) {
19 return CLASSES.indexOf(c) !== -1;
20};
21
22var validateRecord = Publish.validateRecord = function (record) {
23 if (!isValidType(record.type)) { return "[Record TypeError] " + record.type + " is not a valid dns type"; }
24 if (!isValidClass(record.class)) { return "[Record ClassError] class must be one of [" + CLASSES.join(', ') + "]"; }
25
26 // TODO perform stricter validation on data
27 if (!record.data) { return "[Record DataError] expected data to publish"; }
28};
29
30Publish.record = function (branches, record, cb) {
31 var complaint = validateRecord(record);
32 if (complaint) { return void cb(new Error(complaint)); }
33
34 if (!branches.every(SsbRef.isMsgId)) {
35 return void cb(new Error("invalid branches"));
36 }
37
38 // our schema does not use the trailing dot in record names
39 record.name = record.name.replace(/\.$/, '')
40
41 require("ssb-client")(function (err, sbot) {
42 if (err) { return void cb(err); }
43
44 var val = {
45 type: "ssb-dns",
46 record: record,
47 path: record.name.split(/\./g).reverse()
48 .concat(record.class, record.type)
49 }
50 if (branches.length > 1) val.branch = branches;
51 else if (branches.length == 1) val.branch = branches[0];
52
53 // publish a message
54 sbot.publish(val, function (err, msg) {
55 if (err) { return void cb(err); }
56 sbot.close();
57 return void cb(err, msg);
58 })
59 });
60};
61
62

Built with git-ssb-web