Files: 970c91583716ce49e23e1a89eb78103d6ecfdfa9 / lib / publish.js
2153 bytesRaw
1 | var SsbRef = require("ssb-ref"); |
2 | |
3 | var Publish = module.exports = {}; |
4 | |
5 | var help = Publish.help = function () { |
6 | console.log("Try: "); |
7 | console.log("./publish.js name type data (class)"); |
8 | }; |
9 | |
10 | var TYPES = Publish.TYPES = ['A', 'AAAA', 'CNAME', 'HINFO', 'ISDN', 'MX', 'NS', 'PTR', 'SOA', 'TXT', 'SRV', 'SSHFP', 'DS', 'SPF']; |
11 | |
12 | var isValidType = Publish.isValidType = function (t) { |
13 | return TYPES.indexOf(t) !== -1; |
14 | }; |
15 | |
16 | var CLASSES = Publish.CLASSES = ['IN', 'CH', 'NONE']; |
17 | |
18 | var isValidClass = Publish.isValidClass = function (c) { |
19 | return CLASSES.indexOf(c) !== -1; |
20 | }; |
21 | |
22 | var 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 | if (record.type === 'SOA' && record.data.serial !== 0) { |
30 | return "[Record DataError] set SOA serial to 0 so it can be auto-generated"; |
31 | } |
32 | }; |
33 | |
34 | Publish.record = function (branches, record, cb) { |
35 | var complaint = validateRecord(record); |
36 | if (complaint) { return void cb(new Error(complaint)); } |
37 | |
38 | if (!branches.every(SsbRef.isMsgId)) { |
39 | return void cb(new Error("invalid branches")); |
40 | } |
41 | |
42 | // our schema does not use the trailing dot in record names |
43 | record.name = record.name.replace(/\.$/, '') |
44 | |
45 | require("ssb-client")(function (err, sbot) { |
46 | if (err) { return void cb(err); } |
47 | |
48 | var val = { |
49 | type: "ssb-dns", |
50 | record: record, |
51 | path: record.name.split(/\./g).reverse() |
52 | .concat(record.class, record.type) |
53 | } |
54 | if (branches.length > 1) val.branch = branches; |
55 | else if (branches.length == 1) val.branch = branches[0]; |
56 | |
57 | // publish a message |
58 | sbot.publish(val, function (err, msg) { |
59 | if (err) { return void cb(err); } |
60 | sbot.close(); |
61 | return void cb(err, msg); |
62 | }) |
63 | }); |
64 | }; |
65 | |
66 |
Built with git-ssb-web