@nictool/dns-resource-record 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@ Notable changes to this project are documented in this file.
4
4
 
5
5
  #### Unreleased
6
6
 
7
+ ### [1.8.2] - 2026-07-27
8
+
9
+ - lib/nictool: imported getMap,applyMap,unApplyMap
10
+ - rr/bind: quote character strings in presentation format
11
+
7
12
  ### [1.8.1] - 2026-07-25
8
13
 
9
14
  - rr/bind: merge charstrs for opaque rdata types
@@ -437,3 +442,4 @@ Notable changes to this project are documented in this file.
437
442
  [1.7.0]: https://github.com/NicTool/dns-resource-record/releases/tag/v1.7.0
438
443
  [1.8.0]: https://github.com/NicTool/dns-resource-record/releases/tag/v1.8.0
439
444
  [1.8.1]: https://github.com/NicTool/dns-resource-record/releases/tag/v1.8.1
445
+ [1.8.2]: https://github.com/NicTool/dns-resource-record/releases/tag/v1.8.2
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import RR from './rr.js'
2
+ import { registerRdataFormats } from './lib/nictool.js'
2
3
  import A from './rr/a.js'
3
4
  import AAAA from './rr/aaaa.js'
4
5
  import APL from './rr/apl.js'
@@ -90,6 +91,8 @@ for (const c of classes) {
90
91
  typeMap[c.typeName] = id
91
92
  }
92
93
 
94
+ registerRdataFormats(classes)
95
+
93
96
  export {
94
97
  A,
95
98
  AAAA,
@@ -134,4 +137,6 @@ export {
134
137
  typeMap,
135
138
  }
136
139
 
140
+ export { getMap, applyMap, unApplyMap } from './lib/nictool.js'
141
+
137
142
  export default RR
package/lib/nictool.js ADDED
@@ -0,0 +1,286 @@
1
+ /**
2
+ * NicTool 2.x storage columns <-> RFC/IETF resource-record field names.
3
+ *
4
+ * NicTool stores every record type in the same handful of columns — `address`
5
+ * holds the rdata, with `weight`, `priority`, `other` and `description` reused
6
+ * per type — while this library names fields as the RFCs do. These maps
7
+ * translate between the two, so a record read straight from the NicTool schema
8
+ * can be handed to the matching RR class and exported with toBind() or
9
+ * toTinydns().
10
+ *
11
+ * Types whose rdata packs several fields into `address` (NAPTR, NSEC3, SOA...)
12
+ * are unpacked explicitly in unApplyMap.
13
+ */
14
+
15
+ /**
16
+ * Values are arrays for the packed types, so freeze those too. Freezing makes
17
+ * the caches safe to share: mutating a map is a TypeError under ESM's strict mode
18
+ */
19
+ function freezeMap(map) {
20
+ for (const value of Object.values(map)) {
21
+ if (Array.isArray(value)) Object.freeze(value)
22
+ }
23
+ return Object.freeze(map)
24
+ }
25
+
26
+ // A zone export maps once per record — millions of them for a large install —
27
+ // so build each type's map, and its entries, once.
28
+ const mapCache = new Map()
29
+ const entriesCache = new WeakMap()
30
+
31
+ function mapEntries(map) {
32
+ let entries = entriesCache.get(map)
33
+ if (entries === undefined) {
34
+ entries = Object.entries(map)
35
+ entriesCache.set(map, entries)
36
+ }
37
+ return entries
38
+ }
39
+
40
+ export function getMap(rrType) {
41
+ let map = mapCache.get(rrType)
42
+ if (map === undefined) {
43
+ map = freezeMap(mapFor(rrType))
44
+ mapCache.set(rrType, map)
45
+ }
46
+ return map
47
+ }
48
+
49
+ function mapFor(rrType) {
50
+ switch (rrType) {
51
+ case 'CAA':
52
+ return {
53
+ weight: 'flags',
54
+ other: 'tag',
55
+ address: 'value',
56
+ }
57
+ case 'CERT':
58
+ return {
59
+ other: 'cert type',
60
+ priority: 'key tag',
61
+ weight: 'algorithm',
62
+ address: 'certificate',
63
+ }
64
+ case 'CNAME':
65
+ return { address: 'cname' }
66
+ case 'DNAME':
67
+ return { address: 'target' }
68
+ case 'DNSKEY':
69
+ return {
70
+ address: 'publickey',
71
+ weight: 'flags',
72
+ priority: 'protocol',
73
+ other: 'algorithm',
74
+ }
75
+ case 'DS':
76
+ return {
77
+ address: 'digest',
78
+ weight: 'digest type',
79
+ priority: 'algorithm',
80
+ other: 'key tag',
81
+ }
82
+ case 'HINFO':
83
+ return { address: 'os', other: 'cpu' }
84
+ case 'HTTPS':
85
+ return {
86
+ address: 'target name',
87
+ other: 'params',
88
+ }
89
+ case 'IPSECKEY':
90
+ return {
91
+ address: 'gateway',
92
+ description: 'publickey',
93
+ weight: 'precedence',
94
+ priority: 'gateway type',
95
+ other: 'algorithm',
96
+ }
97
+ case 'KEY':
98
+ return {
99
+ address: 'publickey',
100
+ weight: 'protocol',
101
+ priority: 'algorithm',
102
+ other: 'flags',
103
+ }
104
+ case 'MX':
105
+ return { weight: 'preference', address: 'exchange' }
106
+ case 'NAPTR':
107
+ return {
108
+ weight: 'order',
109
+ priority: 'preference',
110
+ address: ['flags', 'service', 'regexp'],
111
+ description: 'replacement',
112
+ }
113
+ case 'NS':
114
+ return { address: 'dname' }
115
+ case 'NSEC':
116
+ return {
117
+ address: 'next domain',
118
+ description: 'type bit maps',
119
+ }
120
+ case 'NSEC3':
121
+ return {
122
+ address: ['hash algorithm', 'flags', 'iterations', 'salt', 'type bit maps', 'next hashed owner name'],
123
+ }
124
+ case 'NSEC3PARAM':
125
+ return {
126
+ address: ['hash algorithm', 'flags', 'iterations', 'salt'],
127
+ }
128
+ case 'NXT':
129
+ return {
130
+ address: 'next domain',
131
+ description: 'type bit map',
132
+ }
133
+ case 'OPENPGPKEY':
134
+ return { address: 'public key' }
135
+ case 'PTR':
136
+ return { address: 'dname' }
137
+ case 'SMIMEA':
138
+ return {
139
+ address: 'certificate association data',
140
+ weight: 'matching type',
141
+ priority: 'selector',
142
+ other: 'certificate usage',
143
+ }
144
+ case 'SOA':
145
+ return {
146
+ address: ['mname', 'rname', 'serial', 'refresh', 'retry', 'expire', 'minimum'],
147
+ }
148
+ case 'SPF':
149
+ return { address: 'data' }
150
+ case 'SSHFP':
151
+ return {
152
+ address: 'fingerprint',
153
+ weight: 'algorithm',
154
+ priority: 'fptype',
155
+ }
156
+ case 'SRV':
157
+ return { address: 'target', other: 'port' }
158
+ case 'SVCB':
159
+ return {
160
+ address: 'target name',
161
+ other: 'params',
162
+ }
163
+ case 'TLSA':
164
+ return {
165
+ weight: 'certificate usage',
166
+ priority: 'selector',
167
+ address: 'certificate association data',
168
+ other: 'matching type',
169
+ }
170
+ case 'TXT':
171
+ return { address: 'data' }
172
+ case 'URI':
173
+ return { address: 'target' }
174
+ default:
175
+ // Types NicTool stores without column overloading need no translation.
176
+ return {}
177
+ }
178
+ }
179
+
180
+ export function applyMap(obj, map) {
181
+ // map dns-r-r (RFC/IETF) field names to NicTool 2.0 DB fields
182
+
183
+ for (const [key, value] of mapEntries(map)) {
184
+ if (Array.isArray(value)) {
185
+ obj[key] = `'${value.map((a) => obj[a]).join("','")}'`
186
+ for (const f of value) {
187
+ delete obj[f]
188
+ }
189
+ // No `delete obj[value]` here: an array key stringifies to
190
+ // "flags,service,regexp" and would delete an unrelated property.
191
+ continue
192
+ }
193
+
194
+ obj[key] = obj[value]
195
+ delete obj[value]
196
+ }
197
+ }
198
+
199
+ // Integers on the wire. `certtype` is numeric but also accepts mnemonics
200
+ // ("PKIX"), so a value is converted only when it actually looks numeric —
201
+ // the declared format alone is not enough.
202
+ const NUMERIC_FORMATS = new Set(['u8', 'u16', 'u32', 'certtype'])
203
+
204
+ /**
205
+ * type -> the fields that class declares as integers, from its static
206
+ * rdataFields. NicTool's `other` column is VARCHAR, so MySQL hands back "1"
207
+ * where the RR setter demands an integer; `weight` and `priority` are SMALLINT
208
+ * and only arrive as strings from a file store or hand-edited data.
209
+ *
210
+ * Keyed on the type because a bare field name is ambiguous: `flags` is u8 in
211
+ * CAA, u16 in DNSKEY and a character string in NAPTR.
212
+ *
213
+ * Populated by index.js, which is where the classes are already enumerated;
214
+ * importing them here would close an import cycle.
215
+ */
216
+ const numericFields = new Map()
217
+
218
+ export function registerRdataFormats(classes) {
219
+ for (const rrClass of classes) {
220
+ const numeric = new Set()
221
+ for (const entry of rrClass.rdataFields ?? []) {
222
+ if (Array.isArray(entry) && NUMERIC_FORMATS.has(entry[1])) numeric.add(entry[0])
223
+ }
224
+ numericFields.set(rrClass.typeName, numeric)
225
+ }
226
+ }
227
+
228
+ export function unApplyMap(obj, map) {
229
+ // map NicTool 2.0 DB fields to dns-r-r (RFC/IETF) field names
230
+ let packed = false
231
+
232
+ if (obj.type === 'NAPTR') {
233
+ const [flags, service, regexp] = obj.address.slice(1, -1).split("','")
234
+ obj.flags = flags ?? ''
235
+ obj.service = service ?? ''
236
+ obj.regexp = regexp ?? ''
237
+ delete obj.address
238
+ packed = true
239
+ }
240
+ if (obj.type === 'NSEC3') {
241
+ const [algo, flags, iters, salt, bitmaps, next] = obj.address.slice(1, -1).split("','")
242
+ obj['hash algorithm'] = /^\d+$/.test(algo) ? parseInt(algo, 10) : (algo ?? '')
243
+ obj.flags = /^\d+$/.test(flags) ? parseInt(flags, 10) : (flags ?? '')
244
+ obj.iterations = /^\d+$/.test(iters) ? parseInt(iters, 10) : (iters ?? '')
245
+ obj.salt = salt
246
+ obj['type bit maps'] = bitmaps
247
+ obj['next hashed owner name'] = next
248
+ delete obj.address
249
+ packed = true
250
+ }
251
+ if (obj.type === 'NSEC3PARAM') {
252
+ const [algo, flags, iters, salt] = obj.address.slice(1, -1).split("','")
253
+ obj['hash algorithm'] = /^\d+$/.test(algo) ? parseInt(algo, 10) : (algo ?? '')
254
+ obj.flags = /^\d+$/.test(flags) ? parseInt(flags, 10) : (flags ?? '')
255
+ obj.iterations = /^\d+$/.test(iters) ? parseInt(iters, 10) : (iters ?? '')
256
+ obj.salt = salt
257
+ delete obj.address
258
+ packed = true
259
+ }
260
+ if (obj.type === 'SOA') {
261
+ const [one, two, three, four, five, six, seven] = obj.address.slice(1, -1).split("','")
262
+ obj.mname = one
263
+ obj.rname = two
264
+ obj.serial = parseInt(three, 10)
265
+ obj.refresh = parseInt(four, 10)
266
+ obj.retry = parseInt(five, 10)
267
+ obj.expire = parseInt(six, 10)
268
+ obj.minimum = parseInt(seven, 10)
269
+ delete obj.address
270
+ packed = true
271
+ }
272
+
273
+ const numeric = numericFields.get(obj.type)
274
+
275
+ for (const [key, value] of mapEntries(map)) {
276
+ if (packed && key === 'address') continue
277
+ const stored = obj[key]
278
+ // Only a string can need converting, and skipping the rest keeps the
279
+ // numeric columns MySQL already types correctly off this path entirely.
280
+ obj[value] =
281
+ typeof stored === 'string' && numeric?.has(value) && /^\d+$/.test(stored)
282
+ ? parseInt(stored, 10)
283
+ : stored
284
+ delete obj[key]
285
+ }
286
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nictool/dns-resource-record",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "DNS Resource Records",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -72,7 +72,7 @@
72
72
  "devDependencies": {
73
73
  "@eslint/js": "^10.0.1",
74
74
  "eslint": "^10.8.0",
75
- "globals": "^17.7.0"
75
+ "globals": "^17.8.0"
76
76
  },
77
77
  "prettier": {
78
78
  "printWidth": 110,
package/rr/ds.js CHANGED
@@ -7,7 +7,12 @@ export default class DS extends RR {
7
7
  static typeName = 'DS'
8
8
  static typeId = 43
9
9
  static RFCs = [4034, 4509, 9619]
10
- static rdataFields = [['key tag', 'u16'], 'algorithm', 'digest type', ['digest', 'str']]
10
+ static rdataFields = [
11
+ ['key tag', 'u16'],
12
+ ['algorithm', 'u8'],
13
+ ['digest type', 'u8'],
14
+ ['digest', 'str'],
15
+ ]
11
16
  static tags = ['dnssec']
12
17
 
13
18
  constructor(opts) {
package/rr/ipseckey.js CHANGED
@@ -8,7 +8,13 @@ export default class IPSECKEY extends RR {
8
8
  static typeName = 'IPSECKEY'
9
9
  static typeId = 45
10
10
  static RFCs = [4025]
11
- static rdataFields = ['precedence', 'gateway type', 'algorithm', 'gateway', 'publickey']
11
+ static rdataFields = [
12
+ ['precedence', 'u8'],
13
+ ['gateway type', 'u8'],
14
+ ['algorithm', 'u8'],
15
+ 'gateway',
16
+ 'publickey',
17
+ ]
12
18
  static tags = ['security']
13
19
 
14
20
  constructor(opts) {
package/rr/txt.js CHANGED
@@ -84,7 +84,11 @@ export default class TXT extends RR {
84
84
  }
85
85
 
86
86
  toMaraDNS() {
87
- const data = asQuotedStrings(this.get('data')).replace(/"/g, "'")
87
+ // csv2 quotes with ' and has its own escaping, so the RFC 1035 rules below
88
+ // are presentation-format only and must not reach the payload. The chunk
89
+ // delimiter differs too; joining with it beats rewriting quotes after the
90
+ // fact, which also hit quotes belonging to the payload.
91
+ const data = asQuotedStrings(this.get('data'), { escape: false, delimiter: "' '" })
88
92
  return `${this.get('owner')}\t+${this.get('ttl')}\t${this.get('type')}\t'${data}' ~\n`
89
93
  }
90
94
 
@@ -125,19 +129,25 @@ export default class TXT extends RR {
125
129
  }
126
130
  }
127
131
 
128
- function asQuotedStrings(data) {
132
+ // RFC 1035 §5.1: inside a quoted character-string, \ and " are escaped. The
133
+ // 255-byte limit counts wire bytes, so chunk on the unescaped value and escape
134
+ // each chunk afterwards.
135
+ const escapeCharString = (s) => String(s).replace(/\\/g, '\\\\').replace(/"/g, '\\"')
136
+
137
+ function asQuotedStrings(data, { escape = true, delimiter = '" "' } = {}) {
129
138
  // RFC 1035 character-strings are 255 bytes max; chunk by UTF-8 bytes,
130
139
  // not JS chars, so non-ASCII TXT data doesn't overflow the 255-byte limit.
131
140
  const enc = new TextEncoder()
141
+ const esc = escape ? escapeCharString : (s) => s
132
142
 
133
143
  if (Array.isArray(data)) {
134
144
  const anyTooLong = data.some((s) => enc.encode(s).length > 255)
135
- if (!anyTooLong) return data.join('" "')
136
- return chunkByBytes(data.join(''), 255).join('" "')
145
+ if (!anyTooLong) return data.map(esc).join(delimiter)
146
+ return chunkByBytes(data.join(''), 255).map(esc).join(delimiter)
137
147
  }
138
148
 
139
- if (enc.encode(data).length <= 255) return data
140
- return chunkByBytes(data, 255).join('" "')
149
+ if (enc.encode(data).length <= 255) return esc(data)
150
+ return chunkByBytes(data, 255).map(esc).join(delimiter)
141
151
  }
142
152
 
143
153
  function chunkByBytes(str, maxBytes) {