Files: b3dbb252f189d256c891324515d83ad97f2ffee6 / lib / parse.js
2638 bytesRaw
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 | } |
87 |
Built with git-ssb-web