Files: 012e32d5ba7c8cc7ea4d15234b2715ffc1f0e154 / constants.js
4590 bytesRaw
1 | // Copyright 2012 Iris Couch, all rights reserved. |
2 | // |
3 | // Looking up and converting between various constants |
4 | |
5 | var util = require('util') |
6 | |
7 | module.exports = { 'type' : swap_type |
8 | , 'class' : swap_class |
9 | , 'type_to_label' : type_to_label |
10 | , 'type_to_number' : type_to_number |
11 | , 'class_to_label' : class_to_label |
12 | , 'class_to_number': class_to_number |
13 | |
14 | // For testing |
15 | , 'transpose' : transpose |
16 | , 'mk_type_labels': mk_type_labels |
17 | } |
18 | |
19 | var TYPE_LABELS = mk_type_labels() |
20 | , CLASS_LABELS = mk_class_labels() |
21 | , TYPE_NUMBERS = transpose(TYPE_LABELS) |
22 | , CLASS_NUMBERS = transpose(CLASS_LABELS) |
23 | |
24 | function swap_type(obj) { |
25 | return (typeof obj == 'string') ? type_to_number(obj) : type_to_label(obj) |
26 | } |
27 | |
28 | function swap_class(obj) { |
29 | return (typeof obj == 'string') ? class_to_number(obj) : class_to_label(obj) |
30 | } |
31 | |
32 | function type_to_label(type) { |
33 | if(isNaN(type) || typeof type != 'number' || type < 1 || type > 65535) |
34 | throw new Error('Invalid record type: ' + type) |
35 | return TYPE_LABELS[type] |
36 | } |
37 | |
38 | function type_to_number(type) { |
39 | if(typeof type != 'string') |
40 | throw new Error('Type must be string: ' + type) |
41 | |
42 | var num = TYPE_NUMBERS[type] |
43 | if(!num) |
44 | throw new Error('No such type label: ' + type) |
45 | else |
46 | return num |
47 | } |
48 | |
49 | function class_to_label(clas) { |
50 | if(isNaN(clas) || typeof clas != 'number' || clas < 1 || clas > 65535) |
51 | throw new Error('Invalid record class: ' + clas) |
52 | return CLASS_LABELS[clas] |
53 | } |
54 | |
55 | function class_to_number(clas) { |
56 | if(typeof clas != 'string') |
57 | throw new Error('Type must be string: ' + clas) |
58 | |
59 | var num = CLASS_NUMBERS[clas] |
60 | if(!num) |
61 | throw new Error('No such clas label: ' + clas) |
62 | else |
63 | return num |
64 | } |
65 | |
66 | // |
67 | // Utilities |
68 | // |
69 | |
70 | function transpose(obj) { |
71 | var result = {} |
72 | Object.keys(obj).forEach(function(key) { |
73 | var val = obj[key] |
74 | if(typeof val == 'string') |
75 | result[val] = +key |
76 | }) |
77 | |
78 | return result |
79 | } |
80 | |
81 | function mk_class_labels() { |
82 | var classes = |
83 | { 0: 'reserved' |
84 | , 1: 'IN' |
85 | , 2: null |
86 | , 3: 'CH' |
87 | , 4: 'HS' |
88 | // 5 - 127 unassigned classes |
89 | // 128 - 253 unassigned qclasses |
90 | , 254: 'NONE' |
91 | , 255: '*' |
92 | // 256 - 32767 unassigned |
93 | // 32768 - 57343 unassigned |
94 | // 57344 - 65279 unassigned qclasses and metaclasses |
95 | // 65280 - 65534 Private use |
96 | , 65535: 'reserved' |
97 | } |
98 | |
99 | var unassigned = [ [5,253], [256,65279] ] |
100 | unassigned.forEach(function(pair) { |
101 | var start = pair[0], stop = pair[1] |
102 | for(var i = start; i <= stop; i++) |
103 | classes[i] = null |
104 | }) |
105 | |
106 | for(var i = 65280; i <= 65534; i++) |
107 | classes[i] = 'Private use' |
108 | |
109 | return classes |
110 | } |
111 | |
112 | function mk_type_labels() { |
113 | var types = |
114 | { 0: null |
115 | , 1: 'A' |
116 | , 2: 'NS' |
117 | , 3: 'MD' |
118 | , 4: 'MF' |
119 | , 5: 'CNAME' |
120 | , 6: 'SOA' |
121 | , 7: 'MB' |
122 | , 8: 'MG' |
123 | , 9: 'MR' |
124 | , 10: 'NULL' |
125 | , 11: 'WKS' |
126 | , 12: 'PTR' |
127 | , 13: 'HINFO' |
128 | , 14: 'MINFO' |
129 | , 15: 'MX' |
130 | , 16: 'TXT' |
131 | , 17: 'RP' |
132 | , 18: 'AFSDB' |
133 | , 19: 'X25' |
134 | , 20: 'ISDN' |
135 | , 21: 'RT' |
136 | , 22: 'NSAP' |
137 | , 23: 'NSAP-PTR' |
138 | , 24: 'SIG' |
139 | , 25: 'KEY' |
140 | , 26: 'PX' |
141 | , 27: 'GPOS' |
142 | , 28: 'AAAA' |
143 | , 29: 'LOC' |
144 | , 30: 'NXT' |
145 | , 31: 'EID' |
146 | , 32: 'NIMLOC' |
147 | , 33: 'SRV' |
148 | , 34: 'ATMA' |
149 | , 35: 'NAPTR' |
150 | , 36: 'KX' |
151 | , 37: 'CERT' |
152 | , 38: 'A6' |
153 | , 39: 'DNAME' |
154 | , 40: 'SINK' |
155 | , 41: 'OPT' |
156 | , 42: 'APL' |
157 | , 43: 'DS' |
158 | , 44: 'SSHFP' |
159 | , 45: 'IPSECKEY' |
160 | , 46: 'RRSIG' |
161 | , 47: 'NSEC' |
162 | , 48: 'DNSKEY' |
163 | , 49: 'DHCID' |
164 | , 50: 'NSEC3' |
165 | , 51: 'NSEC3PARAM' |
166 | , 52: 'TLSA' |
167 | // 53 - 54 unassigned |
168 | , 55: 'HIP' |
169 | , 56: 'NINFO' |
170 | , 57: 'RKEY' |
171 | , 58: 'TALINK' |
172 | , 59: 'CDS' |
173 | // 60 - 98 unassigned |
174 | , 99: 'SPF' |
175 | , 100: 'UINFO' |
176 | , 101: 'UID' |
177 | , 102: 'GID' |
178 | , 103: 'UNSPEC' |
179 | , 104: 'NID' |
180 | , 105: 'L32' |
181 | , 106: 'L64' |
182 | , 107: 'LP' |
183 | // 108 - 248 unassigned |
184 | , 249: 'TKEY' |
185 | , 250: 'TSIG' |
186 | , 251: 'IXFR' |
187 | , 252: 'AXFR' |
188 | , 253: 'MAILB' |
189 | , 254: 'MAILA' |
190 | , 255: '*' |
191 | , 256: 'URI' |
192 | , 257: 'CAA' |
193 | // 258 - 32767 unassigned |
194 | , 32768: 'TA' |
195 | , 32769: 'DLV' |
196 | // 32770 - 65279 unassigned |
197 | // 65280 - 65534 Private use |
198 | , 65535: 'Reserved' |
199 | } |
200 | |
201 | var unassigned = [ [53,54], [60,98], [108,248], [258,32767], [32770,65279] ] |
202 | unassigned.forEach(function(pair) { |
203 | var start = pair[0], stop = pair[1] |
204 | for(var i = start; i <= stop; i++) |
205 | types[i] = null |
206 | }) |
207 | |
208 | for(var i = 65280; i <= 65534; i++) |
209 | types[i] = 'Private use' |
210 | |
211 | return types |
212 | } |
213 |
Built with git-ssb-web