git ssb

15+

ansuz / dnssb



Commit b3dbb252f189d256c891324515d83ad97f2ffee6

resolve conflicts

ansuz committed on 11/4/2016, 6:21:13 PM
Parent: ed943f98023d05c64d3d1400222a3befe4b23369
Parent: 84d2cc3ff53a172cb5ec228f6d7ee0bd6ddaa75e

Files changed

index.jschanged
lib/index.jschanged
lib/publish.jschanged
lib/parse.jsadded
index.jsView
@@ -7,10 +7,10 @@
77
88 (function () {
99 if (require.main !== module) { return module.exports = Lib; }
1010
11-var publishHelp = "\tdnssb publish (previous record key...) name type value (class)";
12-var updateHelp = "\tdnssb update name type value (class)";
11 +var publishHelp = "\tdnssb publish [previous record key...] name [ttl] [class] type value";
12 +var updateHelp = "\tdnssb update name [ttl] [class] type value";
1313 var branchHelp = "\tdnssb branch name type (class)";
1414 var serverHelp = "\tdnssb server port host";
1515 var dumpHelp = "\tdnssb dump";
1616
@@ -72,14 +72,11 @@
7272 console.error(publishHelp);
7373 return;
7474 }
7575
76- var name = argv[1];
77- var type = argv[2];
78- var value = argv[3];
79- var _class = argv[4];
76 + var record = Lib.parse.argsToRecord(argv.slice(1));
8077
81- Lib.publish.record(branches, name, type, value, _class, function (err, msg) {
78 + Lib.publish.record(branches, record, function (err, msg) {
8279 if (err) {
8380 console.error(err);
8481 process.exit(1);
8582 }
@@ -95,16 +92,13 @@
9592 console.error(updateHelp);
9693 return;
9794 }
9895
99- var name = argv[1];
100- var type = argv[2];
101- var value = argv[3];
102- var _class = argv[4];
96 + var record = Lib.parse.argsToRecord(argv.slice(1));
10397
104- Lib.query.branches(name, type, _class, function (err, branches) {
98 + Lib.query.branches(record.name, record.type, record.class, function (err, branches) {
10599 if (err) throw err;
106- Lib.publish.record(branches, name, type, value, _class, function (err, msg) {
100 + Lib.publish.record(branches, record, function (err, msg) {
107101 if (err) throw err;
108102 console.log(msg);
109103 process.exit(0);
110104 });
lib/index.jsView
@@ -7,8 +7,10 @@
77 Lib.dump = require("./dump");
88
99 Lib.query = require("./query");
1010
11 +Lib.parse = require("./parse");
12 +
1113 // run as a scuttlebot plugin
1214 var plugin = Lib;
1315 var pkg = require("../package");
1416 plugin.name = "dns";
lib/publish.jsView
@@ -6,15 +6,15 @@
66 console.log("Try: ");
77 console.log("./publish.js name type data (class)");
88 };
99
10-var TYPES = Publish.TYPES = ['A', 'AAAA', 'CNAME', 'HINFO', 'ISDN', 'MX', 'NS', 'PTR', 'SOA', 'TXT'];
10 +var TYPES = Publish.TYPES = ['A', 'AAAA', 'CNAME', 'HINFO', 'ISDN', 'MX', 'NS', 'PTR', 'SOA', 'TXT', 'SRV', 'SSHFP', 'DS', 'SPF'];
1111
1212 var isValidType = Publish.isValidType = function (t) {
1313 return TYPES.indexOf(t) !== -1;
1414 };
1515
16-var CLASSES = Publish.CLASSES = ['IN', 'CH'];
16 +var CLASSES = Publish.CLASSES = ['IN', 'CH', 'NONE'];
1717
1818 var isValidClass = Publish.isValidClass = function (c) {
1919 return CLASSES.indexOf(c) !== -1;
2020 };
@@ -28,23 +28,12 @@
2828 if (!isValidType(record.type)) { return "[Record TypeError] " + record.type + " is not a valid dns type"; }
2929 if (!isValidClass(record.class)) { return "[Record ClassError] class must be one of [" + CLASSES.join(', ') + "]"; }
3030
3131 // TODO perform stricter validation on data
32- if (!(record.data && typeof(record.data) === 'string')) { return "[Record DataError] expected data to publish"; }
32 + if (!record.data) { return "[Record DataError] expected data to publish"; }
3333 };
3434
35-var 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-
44-Publish.record = function (branches, name, type, data, _class, cb) {
45- var record = makeRecord(name, type, data, _class);
46-
35 +Publish.record = function (branches, record, cb) {
4736 var complaint = validateRecord(record);
4837 if (complaint) { return void cb(new Error(complaint)); }
4938
5039 if (!branches.every(SsbRef.isMsgId)) {
@@ -54,11 +43,12 @@
5443 require("ssb-client")(function (err, sbot) {
5544 if (err) { return void cb(err); }
5645
5746 var val = {
58- type: "ssb-dns",
59- record: record,
60- path: name.split(/\./g).reverse().concat(record.class, type)
47 + type: "ssb-dns",
48 + record: record,
49 + path: record.name.split(/\./g).reverse()
50 + .concat(record.class, record.type)
6151 }
6252 if (branches.length > 1) val.branch = branches;
6353 else if (branches.length == 1) val.branch = branches[0];
6454
lib/parse.jsView
@@ -1,0 +1,86 @@
1 +var classes = {
2 + IN: true,
3 + NONE: true,
4 +};
5 +
6 +var Parse = module.exports = {};
7 +
8 +Parse.argsToRecord = function (args) {
9 + args = args.slice(0);
10 + var record = {};
11 + record.name = args.shift().toLowerCase();
12 + if (Number.isInteger(+args[0])) {
13 + record.ttl = args.shift();
14 + }
15 + // "type and class mnemonics are disjoint" - RFC 1034
16 + if (args[0] in classes) {
17 + record.class = args.shift();
18 + } else {
19 + record.class = "IN";
20 + }
21 + record.type = args.shift();
22 + record.data = argsToData(record, args);
23 + return record;
24 +}
25 +
26 +function argsToData(record, args) {
27 + switch (record.class + " " + record.type) {
28 + case "IN A":
29 + case "IN AAAA":
30 + case "IN NS":
31 + case "IN PTR":
32 + case "IN CNAME":
33 + if (args.length !== 1) throw new TypeError("Invalid arguments");
34 + return args[0];
35 + case "IN MX":
36 + if (args.length !== 2) throw new TypeError("Invalid arguments");
37 + return args;
38 + case "IN SOA":
39 + if (args.length !== 7) throw new TypeError("Invalid arguments");
40 + return {
41 + mname: args[0],
42 + rname: args[1],
43 + serial: args[2],
44 + refresh: args[3],
45 + retry: args[4],
46 + expire: args[5],
47 + ttl: args[6]
48 + };
49 + case "IN TXT":
50 + case "IN SPF":
51 + if (args.length < 1) throw new TypeError("Invalid arguments");
52 + return args.length === 1 ? args[0] : args;
53 + case "IN SRV":
54 + if (args.length !== 4) throw new TypeError("Invalid arguments");
55 + return {
56 + priority: args[0],
57 + weight: args[1],
58 + port: args[2],
59 + target: args[3]
60 + };
61 + case "IN DS":
62 + if (args.length !== 4) throw new TypeError("Invalid arguments");
63 + return {
64 + key_tag: args[0],
65 + algorithm: args[1],
66 + digest_type: args[2],
67 + digest: args[3]
68 + };
69 + case "IN SSHFP":
70 + if (args.length !== 3) throw new TypeError("Invalid arguments");
71 + return {
72 + algorithm: args[0],
73 + fp_type: args[1],
74 + fingerprint: args[2]
75 + };
76 + case 'NONE A':
77 + if (args.length !== 0) throw new TypeError("Invalid arguments");
78 + return [];
79 + default:
80 + throw new TypeError('Unsupported record type: ' + JSON.stringify(record));
81 + }
82 +}
83 +
84 +if (!module.parent) {
85 + console.log(Parse.argsToRecord(process.argv.slice(2)));
86 +}

Built with git-ssb-web