dnssd-advertise 0.1.0

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.
@@ -0,0 +1,2552 @@
1
+ Object.defineProperty(exports, "__esModule", {
2
+ value: !0
3
+ });
4
+
5
+ var e = require("node:os");
6
+
7
+ var t = require("node:dgram");
8
+
9
+ let n = function(e) {
10
+ e[e.QUERY = 0] = "QUERY";
11
+ e[e.RESPONSE = 32768] = "RESPONSE";
12
+ return e;
13
+ }({});
14
+
15
+ let r = function(e) {
16
+ e[e.A = 1] = "A";
17
+ e[e.NS = 2] = "NS";
18
+ e[e.CNAME = 5] = "CNAME";
19
+ e[e.SOA = 6] = "SOA";
20
+ e[e.NULL = 10] = "NULL";
21
+ e[e.PTR = 12] = "PTR";
22
+ e[e.HINFO = 13] = "HINFO";
23
+ e[e.MX = 15] = "MX";
24
+ e[e.TXT = 16] = "TXT";
25
+ e[e.RP = 17] = "RP";
26
+ e[e.AFSDB = 18] = "AFSDB";
27
+ e[e.SIG = 24] = "SIG";
28
+ e[e.KEY = 25] = "KEY";
29
+ e[e.AAAA = 28] = "AAAA";
30
+ e[e.LOC = 29] = "LOC";
31
+ e[e.SRV = 33] = "SRV";
32
+ e[e.NAPTR = 35] = "NAPTR";
33
+ e[e.KX = 36] = "KX";
34
+ e[e.CERT = 37] = "CERT";
35
+ e[e.DNAME = 39] = "DNAME";
36
+ e[e.OPT = 41] = "OPT";
37
+ e[e.APL = 42] = "APL";
38
+ e[e.DS = 43] = "DS";
39
+ e[e.SSHFP = 44] = "SSHFP";
40
+ e[e.IPSECKEY = 45] = "IPSECKEY";
41
+ e[e.RRSIG = 46] = "RRSIG";
42
+ e[e.NSEC = 47] = "NSEC";
43
+ e[e.DNSKEY = 48] = "DNSKEY";
44
+ e[e.DHCID = 49] = "DHCID";
45
+ e[e.NSEC3 = 50] = "NSEC3";
46
+ e[e.NSEC3PARAM = 51] = "NSEC3PARAM";
47
+ e[e.TLSA = 52] = "TLSA";
48
+ e[e.HIP = 55] = "HIP";
49
+ e[e.CDS = 59] = "CDS";
50
+ e[e.CDNSKEY = 60] = "CDNSKEY";
51
+ e[e.SVCB = 64] = "SVCB";
52
+ e[e.HTTPS = 65] = "HTTPS";
53
+ e[e.SPF = 99] = "SPF";
54
+ e[e.TKEY = 249] = "TKEY";
55
+ e[e.TSIG = 250] = "TSIG";
56
+ e[e.IXFR = 251] = "IXFR";
57
+ e[e.AXFR = 252] = "AXFR";
58
+ e[e.ANY = 255] = "ANY";
59
+ e[e.CAA = 257] = "CAA";
60
+ e[e.TA = 32768] = "TA";
61
+ e[e.DLV = 32769] = "DLV";
62
+ return e;
63
+ }({});
64
+
65
+ let s = function(e) {
66
+ e[e.IN = 1] = "IN";
67
+ e[e.CS = 2] = "CS";
68
+ e[e.CH = 3] = "CH";
69
+ e[e.HS = 4] = "HS";
70
+ e[e.ANY = 255] = "ANY";
71
+ return e;
72
+ }({});
73
+
74
+ let a = function(e) {
75
+ e[e.NOERR = 0] = "NOERR";
76
+ e[e.FORMERR = 1] = "FORMERR";
77
+ e[e.SERVFAIL = 2] = "SERVFAIL";
78
+ e[e.NXDOMAIN = 3] = "NXDOMAIN";
79
+ e[e.NOTIMP = 4] = "NOTIMP";
80
+ e[e.REFUSED = 5] = "REFUSED";
81
+ e[e.YXDOMAIN = 6] = "YXDOMAIN";
82
+ e[e.YXRRSET = 7] = "YXRRSET";
83
+ e[e.NXRRSET = 8] = "NXRRSET";
84
+ e[e.NOTAUTH = 9] = "NOTAUTH";
85
+ e[e.NOTZONE = 10] = "NOTZONE";
86
+ e[e.CHECKING_DISABLED = 16] = "CHECKING_DISABLED";
87
+ e[e.AUTHENTIC_DATA = 32] = "AUTHENTIC_DATA";
88
+ e[e.RECURSION_AVAILABLE = 128] = "RECURSION_AVAILABLE";
89
+ e[e.RECURSION_DESIRED = 256] = "RECURSION_DESIRED";
90
+ e[e.TRUNCATED_RESPONSE = 512] = "TRUNCATED_RESPONSE";
91
+ e[e.AUTHORITATIVE_ANSWER = 1024] = "AUTHORITATIVE_ANSWER";
92
+ return e;
93
+ }({});
94
+
95
+ let o = function(e) {
96
+ e[e.OPTION_0 = 0] = "OPTION_0";
97
+ e[e.LLQ = 1] = "LLQ";
98
+ e[e.UL = 2] = "UL";
99
+ e[e.NSID = 3] = "NSID";
100
+ e[e.OPTION_4 = 4] = "OPTION_4";
101
+ e[e.DAU = 5] = "DAU";
102
+ e[e.DHU = 6] = "DHU";
103
+ e[e.N3U = 7] = "N3U";
104
+ e[e.CLIENT_SUBNET = 8] = "CLIENT_SUBNET";
105
+ e[e.EXPIRE = 9] = "EXPIRE";
106
+ e[e.COOKIE = 10] = "COOKIE";
107
+ e[e.TCP_KEEPALIVE = 11] = "TCP_KEEPALIVE";
108
+ e[e.PADDING = 12] = "PADDING";
109
+ e[e.CHAIN = 13] = "CHAIN";
110
+ e[e.KEY_TAG = 14] = "KEY_TAG";
111
+ e[e.DEVICEID = 26946] = "DEVICEID";
112
+ e[e.OPTION_65535 = 65535] = "OPTION_65535";
113
+ return e;
114
+ }({});
115
+
116
+ const i = new TextEncoder;
117
+
118
+ const c = new TextDecoder;
119
+
120
+ const f = "undefined" != typeof Buffer ? e => Buffer.byteLength(e) : e => {
121
+ let t = e.length;
122
+ for (let n = t - 1; n >= 0; n--) {
123
+ const r = e.charCodeAt(n);
124
+ if (r > 127 && r <= 2047) {
125
+ t++;
126
+ } else if (r > 2047 && r <= 65535) {
127
+ t += 2;
128
+ }
129
+ if (r >= 56320 && r <= 57343) {
130
+ n--;
131
+ }
132
+ }
133
+ return t;
134
+ };
135
+
136
+ const l = {
137
+ bytes(e) {
138
+ let t = 2;
139
+ switch (e) {
140
+ case "":
141
+ case ".":
142
+ case "..":
143
+ return 1;
144
+
145
+ default:
146
+ if ("." === e[0]) {
147
+ t--;
148
+ }
149
+ if ("." === e[e.length - 1]) {
150
+ t--;
151
+ }
152
+ t += e.replace(/\\\./g, ".").length;
153
+ if (t > 255) {
154
+ throw new RangeError(`Name "${e}" is above 255 byte limit.`);
155
+ }
156
+ return t;
157
+ }
158
+ },
159
+ write(e, t, n) {
160
+ const r = i.encode(n);
161
+ for (let s = 46 === r[0] ? 1 : 0, a = 0; s < r.byteLength; s = a + 1) {
162
+ a = r.indexOf(46, s);
163
+ while (a > -1 && 92 === r[a - 1]) {
164
+ a = r.indexOf(46, a + 1);
165
+ }
166
+ if (-1 === a) {
167
+ a = r.byteLength;
168
+ }
169
+ if (a === s) {
170
+ continue;
171
+ } else if (a - s > 63) {
172
+ throw new RangeError(`Label in "${n}" is above 63 byte limit.`);
173
+ }
174
+ let o = t + 1;
175
+ for (let t = s; t < a; t++) {
176
+ if (92 === r[t] && 46 === r[t + 1]) {
177
+ t++;
178
+ }
179
+ e.setUint8(o++, r[t]);
180
+ }
181
+ e.setUint8(t, o - t - 1);
182
+ t = o;
183
+ s = a + 1;
184
+ }
185
+ return t + 1;
186
+ },
187
+ read(e, t) {
188
+ const n = [];
189
+ let r = t.offset;
190
+ let s = t;
191
+ while (1) {
192
+ const t = e.getUint8(s.offset);
193
+ if (0 === t) {
194
+ advance(s, 1);
195
+ break;
196
+ } else if (!(192 & t)) {
197
+ advance(s, 1);
198
+ const r = sliceView(e, s, t);
199
+ n.push(c.decode(r).replace(/\./g, "\\."));
200
+ } else {
201
+ const t = e.getUint16(s.offset) - 49152;
202
+ advance(s, 2);
203
+ if (t < r) {
204
+ s = {
205
+ offset: r = t,
206
+ length: 0
207
+ };
208
+ } else {
209
+ break;
210
+ }
211
+ }
212
+ }
213
+ return n.join(".") || ".";
214
+ }
215
+ };
216
+
217
+ const u = {
218
+ bytes: e => "string" == typeof e ? f(e) : e.byteLength,
219
+ write(e, t, n) {
220
+ const r = "string" == typeof n ? i.encode(n) : n;
221
+ new Uint8Array(e.buffer, e.byteOffset + t, r.byteLength).set(r);
222
+ return t + r.byteLength;
223
+ },
224
+ read: (e, t) => sliceView(e, t)
225
+ };
226
+
227
+ const d = {
228
+ bytes: e => e.byteLength + 1,
229
+ write(e, t, n) {
230
+ e.setUint8(t++, n.byteLength);
231
+ new Uint8Array(e.buffer, e.byteOffset + t, n.byteLength).set(n);
232
+ return t + n.byteLength;
233
+ },
234
+ read(e, t) {
235
+ const n = e.getUint8(t.offset);
236
+ advance(t, 1);
237
+ return sliceView(e, t, n);
238
+ }
239
+ };
240
+
241
+ const h = {
242
+ bytes: e => f(e) + 1,
243
+ write(e, t, n) {
244
+ const r = i.encode(n);
245
+ e.setUint8(t++, r.byteLength);
246
+ return u.write(e, t, r);
247
+ },
248
+ read(e, t) {
249
+ const n = e.getUint8(t.offset);
250
+ advance(t, 1);
251
+ return c.decode(sliceView(e, t, n));
252
+ }
253
+ };
254
+
255
+ const y = {
256
+ bytes(e) {
257
+ const t = [];
258
+ for (let n = 0; n < e.length; n++) {
259
+ t[e[n] >> 8] = Math.max(t[e[n] >> 8] || 0, 255 & e[n]);
260
+ }
261
+ let n = 0;
262
+ for (let e = 0; e < t.length; e++) {
263
+ if (null != t[e]) {
264
+ n += 2 + Math.ceil((t[e] + 1) / 8);
265
+ }
266
+ }
267
+ return n;
268
+ },
269
+ write(e, t, n) {
270
+ const r = [];
271
+ for (let e = 0; e < n.length; e++) {
272
+ (r[n[e] >> 8] || (r[n[e] >> 8] = []))[n[e] >> 3 & 31] |= 1 << 7 - (7 & n[e]);
273
+ }
274
+ for (let n = 0; n < r.length; n++) {
275
+ const s = r[n];
276
+ if (null != s) {
277
+ e.setUint8(t++, n);
278
+ e.setUint8(t++, s.length);
279
+ for (let n = 0; n < s.length; n++) {
280
+ e.setUint8(t++, s[n]);
281
+ }
282
+ }
283
+ }
284
+ return t;
285
+ },
286
+ read(e, t) {
287
+ const {offset: n, length: r} = t;
288
+ const s = [];
289
+ while (t.offset - n < r) {
290
+ const n = e.getUint8(t.offset);
291
+ const r = e.getUint8(t.offset + 1);
292
+ for (let a = 0; a < r; a++) {
293
+ const r = e.getUint8(t.offset + 2 + a);
294
+ for (let e = 0; e < 8; e++) {
295
+ if (r & 1 << 7 - e) {
296
+ s.push(n << 8 | a << 3 | e);
297
+ }
298
+ }
299
+ }
300
+ advance(t, 2 + r);
301
+ }
302
+ return s;
303
+ }
304
+ };
305
+
306
+ const p = {
307
+ bytes: () => 4,
308
+ write(e, t, n) {
309
+ const r = n.split(".", 4);
310
+ for (let n = 0; n < 4; n++) {
311
+ e.setUint8(t++, parseInt(r[n], 10));
312
+ }
313
+ return t;
314
+ },
315
+ read(e, t) {
316
+ const n = Math.min(t.length, 4);
317
+ const r = new Array(4).fill(0).map((r, s) => s < n ? e.getUint8(t.offset + s) : 0).join(".");
318
+ advance(t, n);
319
+ return r;
320
+ }
321
+ };
322
+
323
+ const g = {
324
+ bytes: () => 2,
325
+ write(e, t, n) {
326
+ e.setUint16(t, n);
327
+ return t + 2;
328
+ },
329
+ read(e, t) {
330
+ const n = e.getUint16(t.offset);
331
+ advance(t, 2);
332
+ return n;
333
+ }
334
+ };
335
+
336
+ const E = {
337
+ bytes: () => 16,
338
+ write(e, t, n) {
339
+ const r = n.indexOf("::");
340
+ const s = (r > -1 ? n.slice(0, r) : n).split(":");
341
+ const a = r > -1 ? n.slice(r + 2).split(":") : [];
342
+ const o = a.length > 0 && a[a.length - 1].includes(".") ? a.pop() : void 0;
343
+ for (let n = 0; n < s.length; n++) {
344
+ e.setUint16(t, parseInt(s[n], 16));
345
+ t += 2;
346
+ }
347
+ for (let n = 8 - (s.length + a.length + (o ? 2 : 0)); n > 0; n--) {
348
+ e.setUint16(t, 0);
349
+ t += 2;
350
+ }
351
+ for (let n = 0; n < a.length; n++) {
352
+ e.setUint16(t, parseInt(a[n], 16));
353
+ t += 2;
354
+ }
355
+ if (o) {
356
+ const n = o.split(".", 4).map(e => parseInt(e, 10));
357
+ e.setUint16(t, n[0] << 8 | n[1]);
358
+ e.setUint16(t + 2, n[2] << 8 | n[3]);
359
+ t += 4;
360
+ }
361
+ return t;
362
+ },
363
+ read(e, t) {
364
+ let n = "";
365
+ const r = Math.min(t.length, 16);
366
+ for (let s = 0; s < r; s += 2) {
367
+ if (0 !== s) {
368
+ n += ":";
369
+ }
370
+ n += e.getUint16(t.offset + s).toString(16);
371
+ }
372
+ advance(t, r);
373
+ return n.replace(/(^|:)0(:0)*:0(:|$)/, "$1::$3").replace(/:{3,4}/, "::");
374
+ }
375
+ };
376
+
377
+ const withRDLength = e => ({
378
+ bytes: t => e.bytes(t) + 2,
379
+ write(t, n, r) {
380
+ const s = n;
381
+ n = e.write(t, n + 2, r);
382
+ t.setUint16(s, n - s - 2);
383
+ return n;
384
+ },
385
+ read(t, n) {
386
+ const {offset: r, length: s} = n;
387
+ const a = n.length = t.getUint16(n.offset);
388
+ n.offset += 2;
389
+ const o = e.read(t, n);
390
+ n.offset = r + 2 + a;
391
+ n.length = s;
392
+ return o;
393
+ }
394
+ });
395
+
396
+ const array = e => ({
397
+ bytes(t) {
398
+ let n = 0;
399
+ for (let r = 0; null != t && r < t.length; r++) {
400
+ n += e.bytes(t[r]);
401
+ }
402
+ return n;
403
+ },
404
+ write(t, n, r) {
405
+ for (let s = 0; null != r && s < r.length; s++) {
406
+ n = e.write(t, n, r[s]);
407
+ }
408
+ return n;
409
+ },
410
+ read(t, n) {
411
+ const {offset: r, length: s} = n;
412
+ const a = [];
413
+ while (n.offset - r < s) {
414
+ a.push(e.read(t, n));
415
+ }
416
+ return a;
417
+ }
418
+ });
419
+
420
+ const advance = (e, t) => {
421
+ e.offset += 0 | t;
422
+ e.length -= 0 | t;
423
+ e.length &= ~(e.length >> 31);
424
+ };
425
+
426
+ const sliceView = (e, t, n = t.length) => {
427
+ const r = new Uint8Array(e.buffer, e.byteOffset + t.offset, n);
428
+ advance(t, n);
429
+ return r;
430
+ };
431
+
432
+ const A = {
433
+ bytes: e => l.bytes(e.name) + 4,
434
+ write(e, t, n) {
435
+ let r = n.class || s.IN;
436
+ if (n.qu) {
437
+ r |= 32768;
438
+ }
439
+ t = l.write(e, t, n.name);
440
+ e.setUint16(t, n.type);
441
+ e.setUint16(t + 2, r);
442
+ return t + 4;
443
+ },
444
+ read(e, t) {
445
+ const n = l.read(e, t);
446
+ const r = e.getUint16(t.offset);
447
+ let a = e.getUint16(t.offset + 2) || s.ANY;
448
+ let o = !1;
449
+ if (a !== s.ANY && 32768 & a) {
450
+ a &= -32769;
451
+ o = !0;
452
+ }
453
+ advance(t, 4);
454
+ return {
455
+ name: n,
456
+ type: r,
457
+ class: a,
458
+ qu: o
459
+ };
460
+ }
461
+ };
462
+
463
+ let b = function(e) {
464
+ e[e.v4 = 1] = "v4";
465
+ e[e.v6 = 2] = "v6";
466
+ return e;
467
+ }({});
468
+
469
+ const N = withRDLength({
470
+ bytes: e => u.bytes(e.data),
471
+ write: (e, t, n) => u.write(e, t, n.data),
472
+ read: (e, t) => ({
473
+ code: o.OPTION_0,
474
+ data: u.read(e, t)
475
+ })
476
+ });
477
+
478
+ const S = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])";
479
+
480
+ const w = new RegExp(`^(?:${S}\\.){3}${S}$`);
481
+
482
+ const m = withRDLength({
483
+ bytes: e => Math.ceil((e.sourcePrefixLength || 0) / 8) + 4,
484
+ write(e, t, n) {
485
+ const r = n.sourcePrefixLength || 0;
486
+ const s = n.scopePrefixLength || 0;
487
+ const a = n.family || (w.test(n.ip) ? b.v4 : b.v6);
488
+ const o = Math.ceil(r / 8);
489
+ e.setUint16(t, a);
490
+ e.setUint8(t + 2, r);
491
+ e.setUint8(t + 3, s);
492
+ t += 4;
493
+ const i = ((e, t) => {
494
+ const n = new ArrayBuffer(e.bytes(t));
495
+ const r = e.write(new DataView(n), 0, t);
496
+ return new Uint8Array(n, 0, r);
497
+ })(a === b.v4 ? p : E, n.ip);
498
+ for (let n = 0; n < o; n++) {
499
+ e.setUint8(t++, i[n]);
500
+ }
501
+ return t;
502
+ },
503
+ read(e, t) {
504
+ const n = e.getUint16(t.offset);
505
+ const r = e.getUint8(t.offset + 2);
506
+ const s = e.getUint8(t.offset + 3);
507
+ advance(t, 4);
508
+ return {
509
+ code: o.CLIENT_SUBNET,
510
+ family: n,
511
+ sourcePrefixLength: r,
512
+ scopePrefixLength: s,
513
+ ip: n === b.v4 ? p.read(e, t) : E.read(e, t)
514
+ };
515
+ }
516
+ });
517
+
518
+ const U = withRDLength({
519
+ bytes: e => e.timeout ? 2 : 0,
520
+ write(e, t, n) {
521
+ if (n.timeout) {
522
+ e.setUint16(t, n.timeout);
523
+ t += 2;
524
+ }
525
+ return t;
526
+ },
527
+ read(e, t) {
528
+ if (t.length) {
529
+ const n = e.getUint16(t.offset);
530
+ advance(t, 2);
531
+ return {
532
+ code: o.TCP_KEEPALIVE,
533
+ timeout: n
534
+ };
535
+ } else {
536
+ return {
537
+ code: o.TCP_KEEPALIVE,
538
+ timeout: void 0
539
+ };
540
+ }
541
+ }
542
+ });
543
+
544
+ const T = withRDLength({
545
+ bytes: e => e.length || 0,
546
+ write: (e, t, n) => t + (n.length || 0),
547
+ read(e, t) {
548
+ const {length: n} = t;
549
+ advance(t, n);
550
+ return {
551
+ code: o.PADDING,
552
+ length: n
553
+ };
554
+ }
555
+ });
556
+
557
+ const I = withRDLength({
558
+ bytes: e => 2 * e.tags.length,
559
+ write(e, t, n) {
560
+ for (let r = 0; r < n.tags.length; r++) {
561
+ e.setUint16(t, n.tags[r]);
562
+ t += 2;
563
+ }
564
+ return t;
565
+ },
566
+ read(e, t) {
567
+ const {offset: n, length: r} = t;
568
+ const s = [];
569
+ while (t.offset - n < r) {
570
+ s.push(e.getUint16(t.offset));
571
+ advance(t, 2);
572
+ }
573
+ return {
574
+ code: o.KEY_TAG,
575
+ tags: s
576
+ };
577
+ }
578
+ });
579
+
580
+ const isUnknownOpt = e => !!e.data;
581
+
582
+ const O = {
583
+ bytes(e) {
584
+ if (isUnknownOpt(e)) {
585
+ return N.bytes(e) + 2;
586
+ }
587
+ switch (e.code) {
588
+ case o.CLIENT_SUBNET:
589
+ return m.bytes(e) + 2;
590
+
591
+ case o.TCP_KEEPALIVE:
592
+ return U.bytes(e) + 2;
593
+
594
+ case o.PADDING:
595
+ return T.bytes(e) + 2;
596
+
597
+ case o.KEY_TAG:
598
+ return I.bytes(e) + 2;
599
+ }
600
+ },
601
+ write(e, t, n) {
602
+ e.setUint16(t, n.code);
603
+ t += 2;
604
+ if (isUnknownOpt(n)) {
605
+ return N.write(e, t, n);
606
+ }
607
+ switch (n.code) {
608
+ case o.CLIENT_SUBNET:
609
+ return m.write(e, t, n);
610
+
611
+ case o.TCP_KEEPALIVE:
612
+ return U.write(e, t, n);
613
+
614
+ case o.PADDING:
615
+ return T.write(e, t, n);
616
+
617
+ case o.KEY_TAG:
618
+ return I.write(e, t, n);
619
+ }
620
+ },
621
+ read(e, t) {
622
+ const n = e.getUint16(t.offset);
623
+ advance(t, 2);
624
+ switch (n) {
625
+ case o.CLIENT_SUBNET:
626
+ return m.read(e, t);
627
+
628
+ case o.TCP_KEEPALIVE:
629
+ return U.read(e, t);
630
+
631
+ case o.PADDING:
632
+ return T.read(e, t);
633
+
634
+ case o.KEY_TAG:
635
+ return I.read(e, t);
636
+
637
+ default:
638
+ const r = N.read(e, t);
639
+ r.code = n;
640
+ return r;
641
+ }
642
+ }
643
+ };
644
+
645
+ let R = function(e) {
646
+ e[e.Mandatory = 0] = "Mandatory";
647
+ e[e.Alpn = 1] = "Alpn";
648
+ e[e.NoDefaultAlpn = 2] = "NoDefaultAlpn";
649
+ e[e.Port = 3] = "Port";
650
+ e[e.Ipv4Hint = 4] = "Ipv4Hint";
651
+ e[e.EchConfig = 5] = "EchConfig";
652
+ e[e.Ipv6Hint = 6] = "Ipv6Hint";
653
+ e[e.DohPath = 7] = "DohPath";
654
+ e[e.Odoh = 32769] = "Odoh";
655
+ return e;
656
+ }({});
657
+
658
+ const P = withRDLength(array(g));
659
+
660
+ const C = withRDLength(array(h));
661
+
662
+ const D = withRDLength(g);
663
+
664
+ const v = withRDLength(array(p));
665
+
666
+ const L = withRDLength(array(E));
667
+
668
+ const k = withRDLength(u);
669
+
670
+ const M = {
671
+ bytes(e) {
672
+ let t = 0;
673
+ if (null != e.mandatory) {
674
+ t += P.bytes(e.mandatory) + 2;
675
+ }
676
+ if (null != e.alpn) {
677
+ t += C.bytes(e.alpn) + 2;
678
+ }
679
+ if (e["no-default-alpn"]) {
680
+ t += 4;
681
+ }
682
+ if (null != e.port) {
683
+ t += D.bytes(e.port) + 2;
684
+ }
685
+ if (e.ipv4hint) {
686
+ t += v.bytes(e.ipv4hint) + 2;
687
+ }
688
+ if (e.ipv6hint) {
689
+ t += L.bytes(e.ipv6hint) + 2;
690
+ }
691
+ if (e.echconfig) {
692
+ t += k.bytes(e.echconfig) + 2;
693
+ }
694
+ if (e.dohpath) {
695
+ t += k.bytes(e.dohpath) + 2;
696
+ }
697
+ if (e.odoh) {
698
+ t += k.bytes(e.odoh) + 2;
699
+ }
700
+ return t;
701
+ },
702
+ write(e, t, n) {
703
+ if (null != n.mandatory) {
704
+ e.setUint16(t, R.Mandatory);
705
+ t = P.write(e, t + 2, n.mandatory);
706
+ }
707
+ if (null != n.alpn) {
708
+ e.setUint16(t, R.Alpn);
709
+ t = C.write(e, t + 2, n.alpn);
710
+ }
711
+ if (n["no-default-alpn"]) {
712
+ e.setUint16(t, R.NoDefaultAlpn);
713
+ e.setUint16(t + 2, 0);
714
+ t += 4;
715
+ }
716
+ if (null != n.port) {
717
+ e.setUint16(t, R.Port);
718
+ t = D.write(e, t + 2, n.port);
719
+ }
720
+ if (n.ipv4hint) {
721
+ e.setUint16(t, R.Ipv4Hint);
722
+ t = v.write(e, t + 2, n.ipv4hint);
723
+ }
724
+ if (n.ipv6hint) {
725
+ e.setUint16(t, R.Ipv6Hint);
726
+ t = L.write(e, t + 2, n.ipv6hint);
727
+ }
728
+ if (n.echconfig) {
729
+ e.setUint16(t, R.EchConfig);
730
+ t = k.write(e, t + 2, n.echconfig);
731
+ }
732
+ if (n.dohpath) {
733
+ e.setUint16(t, R.DohPath);
734
+ t = k.write(e, t + 2, n.dohpath);
735
+ }
736
+ if (n.odoh) {
737
+ e.setUint16(t, R.Odoh);
738
+ t = k.write(e, t + 2, n.odoh);
739
+ }
740
+ return t;
741
+ },
742
+ read(e, t) {
743
+ const {length: n, offset: r} = t;
744
+ const s = {
745
+ mandatory: void 0,
746
+ alpn: void 0,
747
+ "no-default-alpn": !1,
748
+ port: void 0,
749
+ ipv4hint: void 0,
750
+ ipv6hint: void 0,
751
+ echconfig: void 0,
752
+ dohpath: void 0,
753
+ odoh: void 0
754
+ };
755
+ while (t.offset - r < n) {
756
+ const n = e.getUint16(t.offset);
757
+ advance(t, 2);
758
+ switch (n) {
759
+ case R.Mandatory:
760
+ s.mandatory = P.read(e, t);
761
+ break;
762
+
763
+ case R.Alpn:
764
+ s.alpn = C.read(e, t);
765
+ break;
766
+
767
+ case R.NoDefaultAlpn:
768
+ s["no-default-alpn"] = !0;
769
+ advance(t, 2);
770
+ break;
771
+
772
+ case R.Port:
773
+ s.port = D.read(e, t);
774
+ break;
775
+
776
+ case R.Ipv4Hint:
777
+ s.ipv4hint = v.read(e, t);
778
+ break;
779
+
780
+ case R.Ipv6Hint:
781
+ s.ipv6hint = L.read(e, t);
782
+ break;
783
+
784
+ case R.EchConfig:
785
+ s.echconfig = k.read(e, t);
786
+ break;
787
+
788
+ case R.DohPath:
789
+ s.dohpath = c.decode(k.read(e, t));
790
+ break;
791
+
792
+ case R.Odoh:
793
+ s.odoh = k.read(e, t);
794
+ break;
795
+
796
+ default:
797
+ k.read(e, t);
798
+ }
799
+ }
800
+ return s;
801
+ }
802
+ };
803
+
804
+ const _ = withRDLength(u);
805
+
806
+ const H = withRDLength(l);
807
+
808
+ const x = withRDLength(p);
809
+
810
+ const V = withRDLength(E);
811
+
812
+ const Y = withRDLength(array(h));
813
+
814
+ const q = withRDLength({
815
+ bytes: e => l.bytes(e.target) + 6,
816
+ write(e, t, n) {
817
+ e.setUint16(t, n.priority || 0);
818
+ e.setUint16(t + 2, n.weight || 0);
819
+ e.setUint16(t + 4, n.port || 0);
820
+ return l.write(e, t + 6, n.target);
821
+ },
822
+ read(e, t) {
823
+ const n = {
824
+ priority: 0,
825
+ weight: 0,
826
+ port: 0,
827
+ target: ""
828
+ };
829
+ n.priority = e.getUint16(t.offset);
830
+ n.weight = e.getUint16(t.offset + 2);
831
+ n.port = e.getUint16(t.offset + 4);
832
+ advance(t, 6);
833
+ n.target = l.read(e, t);
834
+ return n;
835
+ }
836
+ });
837
+
838
+ const B = withRDLength({
839
+ bytes: e => h.bytes(e.cpu) + h.bytes(e.os),
840
+ write(e, t, n) {
841
+ t = h.write(e, t, n.cpu);
842
+ return h.write(e, t, n.os);
843
+ },
844
+ read: (e, t) => ({
845
+ cpu: h.read(e, t),
846
+ os: h.read(e, t)
847
+ })
848
+ });
849
+
850
+ const toCaaTag = e => {
851
+ switch (e) {
852
+ case "issue":
853
+ case "issuewild":
854
+ case "iodef":
855
+ return e;
856
+
857
+ default:
858
+ return "issue";
859
+ }
860
+ };
861
+
862
+ const G = withRDLength({
863
+ bytes: e => h.bytes(e.tag) + u.bytes(e.value) + 1,
864
+ write(e, t, n) {
865
+ let r = n.flags || 0;
866
+ if (n.issuerCritical) {
867
+ r |= 128;
868
+ }
869
+ e.setUint8(t, r);
870
+ t = h.write(e, t + 1, n.tag);
871
+ return u.write(e, t, n.value);
872
+ },
873
+ read(e, t) {
874
+ const n = e.getUint8(t.offset);
875
+ advance(t, 1);
876
+ return {
877
+ flags: n,
878
+ tag: toCaaTag(h.read(e, t)),
879
+ value: u.read(e, t),
880
+ issuerCritical: !!(128 & n)
881
+ };
882
+ }
883
+ });
884
+
885
+ const K = withRDLength({
886
+ bytes: e => l.bytes(e.mname) + l.bytes(e.rname) + 20,
887
+ write(e, t, n) {
888
+ t = l.write(e, t, n.mname);
889
+ t = l.write(e, t, n.rname);
890
+ e.setUint32(t, n.serial || 0);
891
+ e.setUint32(t + 4, n.refresh || 0);
892
+ e.setUint32(t + 8, n.retry || 0);
893
+ e.setUint32(t + 12, n.expire || 0);
894
+ e.setUint32(t + 16, n.minimum || 0);
895
+ return t + 20;
896
+ },
897
+ read(e, t) {
898
+ const n = {
899
+ mname: l.read(e, t),
900
+ rname: l.read(e, t),
901
+ serial: e.getUint32(t.offset),
902
+ refresh: e.getUint32(t.offset + 4),
903
+ retry: e.getUint32(t.offset + 8),
904
+ expire: e.getUint32(t.offset + 12),
905
+ minimum: e.getUint32(t.offset + 16)
906
+ };
907
+ t.offset += 20;
908
+ t.length -= 20;
909
+ return n;
910
+ }
911
+ });
912
+
913
+ const X = withRDLength({
914
+ bytes: e => l.bytes(e.exchange) + 2,
915
+ write(e, t, n) {
916
+ e.setUint16(t, n.preference || 0);
917
+ return l.write(e, t + 2, n.exchange);
918
+ },
919
+ read(e, t) {
920
+ const n = {
921
+ preference: e.getUint16(t.offset),
922
+ exchange: ""
923
+ };
924
+ advance(t, 2);
925
+ n.exchange = l.read(e, t);
926
+ return n;
927
+ }
928
+ });
929
+
930
+ const $ = withRDLength({
931
+ bytes: e => u.bytes(e.key) + 4,
932
+ write(e, t, n) {
933
+ e.setUint16(t, n.flags);
934
+ e.setUint8(t + 2, 3);
935
+ e.setUint8(t + 3, n.algorithm);
936
+ return u.write(e, t + 4, n.key);
937
+ },
938
+ read(e, t) {
939
+ const n = e.getUint16(t.offset);
940
+ const r = e.getUint8(t.offset + 3);
941
+ advance(t, 4);
942
+ return {
943
+ flags: n,
944
+ algorithm: r,
945
+ key: u.read(e, t)
946
+ };
947
+ }
948
+ });
949
+
950
+ const F = withRDLength({
951
+ bytes: e => 18 + l.bytes(e.signersName) + u.bytes(e.signature),
952
+ write(e, t, n) {
953
+ e.setUint16(t, n.typeCovered);
954
+ e.setUint8(t + 2, n.algorithm);
955
+ e.setUint8(t + 3, n.labels);
956
+ e.setUint32(t + 4, n.originalTTL);
957
+ e.setUint32(t + 8, n.expiration);
958
+ e.setUint32(t + 12, n.inception);
959
+ e.setUint16(t + 16, n.keyTag);
960
+ t = l.write(e, t + 18, n.signersName);
961
+ return u.write(e, t, n.signature);
962
+ },
963
+ read(e, t) {
964
+ const n = e.getUint16(t.offset);
965
+ const r = e.getUint8(t.offset + 2);
966
+ const s = e.getUint8(t.offset + 3);
967
+ const a = e.getUint32(t.offset + 4);
968
+ const o = e.getUint32(t.offset + 8);
969
+ const i = e.getUint32(t.offset + 12);
970
+ const c = e.getUint16(t.offset + 16);
971
+ advance(t, 18);
972
+ return {
973
+ typeCovered: n,
974
+ algorithm: r,
975
+ labels: s,
976
+ originalTTL: a,
977
+ expiration: o,
978
+ inception: i,
979
+ keyTag: c,
980
+ signersName: l.read(e, t),
981
+ signature: u.read(e, t)
982
+ };
983
+ }
984
+ });
985
+
986
+ const Q = withRDLength({
987
+ bytes: e => l.bytes(e.mbox) + l.bytes(e.txt),
988
+ write(e, t, n) {
989
+ t = l.write(e, t, n.mbox);
990
+ return l.write(e, t, n.txt);
991
+ },
992
+ read: (e, t) => ({
993
+ mbox: l.read(e, t),
994
+ txt: l.read(e, t)
995
+ })
996
+ });
997
+
998
+ const j = withRDLength({
999
+ bytes: e => l.bytes(e.nextDomain) + y.bytes(e.rrtypes),
1000
+ write(e, t, n) {
1001
+ t = l.write(e, t, n.nextDomain);
1002
+ return y.write(e, t, n.rrtypes);
1003
+ },
1004
+ read: (e, t) => ({
1005
+ nextDomain: l.read(e, t),
1006
+ rrtypes: y.read(e, t)
1007
+ })
1008
+ });
1009
+
1010
+ const z = withRDLength({
1011
+ bytes: e => d.bytes(e.salt) + d.bytes(e.nextDomain) + y.bytes(e.rrtypes) + 4,
1012
+ write(e, t, n) {
1013
+ e.setUint8(t, n.algorithm);
1014
+ e.setUint8(t + 1, n.flags);
1015
+ e.setUint16(t + 2, n.iterations);
1016
+ t = d.write(e, t + 4, n.salt);
1017
+ t = d.write(e, t, n.nextDomain);
1018
+ return y.write(e, t, n.rrtypes);
1019
+ },
1020
+ read(e, t) {
1021
+ const n = e.getUint8(t.offset);
1022
+ const r = e.getUint8(t.offset + 1);
1023
+ const s = e.getUint16(t.offset + 2);
1024
+ advance(t, 4);
1025
+ return {
1026
+ algorithm: n,
1027
+ flags: r,
1028
+ iterations: s,
1029
+ salt: d.read(e, t),
1030
+ nextDomain: d.read(e, t),
1031
+ rrtypes: y.read(e, t)
1032
+ };
1033
+ }
1034
+ });
1035
+
1036
+ const W = withRDLength({
1037
+ bytes: e => u.bytes(e.fingerprint) + 2,
1038
+ write(e, t, n) {
1039
+ e.setUint8(t, n.algorithm);
1040
+ e.setUint8(t + 1, n.hash);
1041
+ return u.write(e, t + 2, n.fingerprint);
1042
+ },
1043
+ read(e, t) {
1044
+ const n = e.getUint8(t.offset);
1045
+ const r = e.getUint8(t.offset + 1);
1046
+ advance(t, 2);
1047
+ return {
1048
+ algorithm: n,
1049
+ hash: r,
1050
+ fingerprint: u.read(e, t)
1051
+ };
1052
+ }
1053
+ });
1054
+
1055
+ const Z = withRDLength({
1056
+ bytes: e => u.bytes(e.digest) + 4,
1057
+ write(e, t, n) {
1058
+ e.setUint16(t, n.keyTag);
1059
+ e.setUint8(t + 2, n.algorithm);
1060
+ e.setUint8(t + 3, n.digestType);
1061
+ return u.write(e, t + 4, n.digest);
1062
+ },
1063
+ read(e, t) {
1064
+ const n = e.getUint16(t.offset);
1065
+ const r = e.getUint8(t.offset + 2);
1066
+ const s = e.getUint8(t.offset + 3);
1067
+ advance(t, 4);
1068
+ return {
1069
+ keyTag: n,
1070
+ algorithm: r,
1071
+ digestType: s,
1072
+ digest: u.read(e, t)
1073
+ };
1074
+ }
1075
+ });
1076
+
1077
+ const J = withRDLength({
1078
+ bytes: e => h.bytes(e.flags) + h.bytes(e.services) + h.bytes(e.regexp) + l.bytes(e.replacement) + 4,
1079
+ write(e, t, n) {
1080
+ e.setUint16(t, n.order);
1081
+ e.setUint16(t + 2, n.preference);
1082
+ t = h.write(e, t + 4, n.flags);
1083
+ t = h.write(e, t, n.services);
1084
+ t = h.write(e, t, n.regexp);
1085
+ return l.write(e, t, n.replacement);
1086
+ },
1087
+ read(e, t) {
1088
+ const n = e.getUint16(t.offset);
1089
+ const r = e.getUint16(t.offset + 2);
1090
+ advance(t, 4);
1091
+ return {
1092
+ order: n,
1093
+ preference: r,
1094
+ flags: h.read(e, t),
1095
+ services: h.read(e, t),
1096
+ regexp: h.read(e, t),
1097
+ replacement: l.read(e, t)
1098
+ };
1099
+ }
1100
+ });
1101
+
1102
+ const ee = withRDLength({
1103
+ bytes: e => u.bytes(e.certificate) + 3,
1104
+ write(e, t, n) {
1105
+ e.setUint8(t, n.usage);
1106
+ e.setUint8(t + 1, n.selector);
1107
+ e.setUint8(t + 2, n.matchingType);
1108
+ return u.write(e, t + 3, n.certificate);
1109
+ },
1110
+ read(e, t) {
1111
+ const n = e.getUint8(t.offset);
1112
+ const r = e.getUint8(t.offset + 1);
1113
+ const s = e.getUint8(t.offset + 2);
1114
+ advance(t, 3);
1115
+ return {
1116
+ usage: n,
1117
+ selector: r,
1118
+ matchingType: s,
1119
+ certificate: u.read(e, t)
1120
+ };
1121
+ }
1122
+ });
1123
+
1124
+ const te = withRDLength({
1125
+ bytes: e => l.bytes(e.name) + M.bytes(e.params) + 2,
1126
+ write(e, t, n) {
1127
+ e.setUint16(t, n.priority || 0);
1128
+ t = l.write(e, t + 2, n.name);
1129
+ return M.write(e, t, n.params);
1130
+ },
1131
+ read(e, t) {
1132
+ const n = e.getUint16(t.offset);
1133
+ advance(t, 2);
1134
+ return {
1135
+ name: l.read(e, t),
1136
+ priority: n,
1137
+ params: M.read(e, t)
1138
+ };
1139
+ }
1140
+ });
1141
+
1142
+ const ne = withRDLength(array(O));
1143
+
1144
+ const re = {
1145
+ bytes(e) {
1146
+ const t = 8 + l.bytes(e.type === r.OPT ? "." : e.name);
1147
+ switch (e.type) {
1148
+ case r.A:
1149
+ return t + x.bytes(e.data);
1150
+
1151
+ case r.NS:
1152
+ return t + H.bytes(e.data);
1153
+
1154
+ case r.SOA:
1155
+ return t + K.bytes(e.data);
1156
+
1157
+ case r.HINFO:
1158
+ return t + B.bytes(e.data);
1159
+
1160
+ case r.MX:
1161
+ return t + X.bytes(e.data);
1162
+
1163
+ case r.TXT:
1164
+ return t + Y.bytes(e.data);
1165
+
1166
+ case r.RP:
1167
+ return t + Q.bytes(e.data);
1168
+
1169
+ case r.AAAA:
1170
+ return t + V.bytes(e.data);
1171
+
1172
+ case r.SRV:
1173
+ return t + q.bytes(e.data);
1174
+
1175
+ case r.NAPTR:
1176
+ return t + J.bytes(e.data);
1177
+
1178
+ case r.OPT:
1179
+ return t + ne.bytes(e.data);
1180
+
1181
+ case r.DS:
1182
+ return t + Z.bytes(e.data);
1183
+
1184
+ case r.SSHFP:
1185
+ return t + W.bytes(e.data);
1186
+
1187
+ case r.RRSIG:
1188
+ return t + F.bytes(e.data);
1189
+
1190
+ case r.NSEC:
1191
+ return t + j.bytes(e.data);
1192
+
1193
+ case r.DNSKEY:
1194
+ return t + $.bytes(e.data);
1195
+
1196
+ case r.NSEC3:
1197
+ return t + z.bytes(e.data);
1198
+
1199
+ case r.TLSA:
1200
+ return t + ee.bytes(e.data);
1201
+
1202
+ case r.SVCB:
1203
+ case r.HTTPS:
1204
+ return t + te.bytes(e.data);
1205
+
1206
+ case r.CAA:
1207
+ return t + G.bytes(e.data);
1208
+
1209
+ case r.PTR:
1210
+ case r.CNAME:
1211
+ case r.DNAME:
1212
+ return t + H.bytes(e.data);
1213
+
1214
+ default:
1215
+ return t + _.bytes(e.data);
1216
+ }
1217
+ },
1218
+ write(e, t, n) {
1219
+ if (n.type === r.OPT) {
1220
+ t = l.write(e, t, ".");
1221
+ e.setUint16(t, n.type);
1222
+ e.setUint16(t + 2, n.udpPayloadSize || 4096);
1223
+ e.setUint8(t + 4, n.extendedRcode || 0);
1224
+ e.setUint8(t + 5, n.ednsVersion || 0);
1225
+ e.setUint16(t + 6, n.flags || 0);
1226
+ return ne.write(e, t += 8, n.data);
1227
+ }
1228
+ t = l.write(e, t, n.name);
1229
+ e.setUint16(t, n.type);
1230
+ e.setUint16(t + 2, (n.class || 0) | (n.flush ? 32768 : 0));
1231
+ e.setUint32(t + 4, n.ttl || 0);
1232
+ t += 8;
1233
+ switch (n.type) {
1234
+ case r.A:
1235
+ return x.write(e, t, n.data);
1236
+
1237
+ case r.NS:
1238
+ return H.write(e, t, n.data);
1239
+
1240
+ case r.SOA:
1241
+ return K.write(e, t, n.data);
1242
+
1243
+ case r.HINFO:
1244
+ return B.write(e, t, n.data);
1245
+
1246
+ case r.MX:
1247
+ return X.write(e, t, n.data);
1248
+
1249
+ case r.TXT:
1250
+ return Y.write(e, t, n.data);
1251
+
1252
+ case r.RP:
1253
+ return Q.write(e, t, n.data);
1254
+
1255
+ case r.AAAA:
1256
+ return V.write(e, t, n.data);
1257
+
1258
+ case r.SRV:
1259
+ return q.write(e, t, n.data);
1260
+
1261
+ case r.NAPTR:
1262
+ return J.write(e, t, n.data);
1263
+
1264
+ case r.DS:
1265
+ return Z.write(e, t, n.data);
1266
+
1267
+ case r.SSHFP:
1268
+ return W.write(e, t, n.data);
1269
+
1270
+ case r.RRSIG:
1271
+ return F.write(e, t, n.data);
1272
+
1273
+ case r.NSEC:
1274
+ return j.write(e, t, n.data);
1275
+
1276
+ case r.DNSKEY:
1277
+ return $.write(e, t, n.data);
1278
+
1279
+ case r.NSEC3:
1280
+ return z.write(e, t, n.data);
1281
+
1282
+ case r.TLSA:
1283
+ return ee.write(e, t, n.data);
1284
+
1285
+ case r.SVCB:
1286
+ case r.HTTPS:
1287
+ return te.write(e, t, n.data);
1288
+
1289
+ case r.CAA:
1290
+ return G.write(e, t, n.data);
1291
+
1292
+ case r.PTR:
1293
+ case r.CNAME:
1294
+ case r.DNAME:
1295
+ return H.write(e, t, n.data);
1296
+
1297
+ default:
1298
+ return _.write(e, t, n.data);
1299
+ }
1300
+ },
1301
+ read(e, t) {
1302
+ const n = l.read(e, t);
1303
+ const s = e.getUint16(t.offset);
1304
+ if (s === r.OPT) {
1305
+ const n = e.getUint16(t.offset + 2) || 4096;
1306
+ const r = e.getUint8(t.offset + 4);
1307
+ const a = e.getUint8(t.offset + 5);
1308
+ const o = e.getUint16(t.offset + 6);
1309
+ advance(t, 8);
1310
+ return {
1311
+ type: s,
1312
+ udpPayloadSize: n,
1313
+ extendedRcode: r,
1314
+ ednsVersion: a,
1315
+ flags: o,
1316
+ data: ne.read(e, t)
1317
+ };
1318
+ }
1319
+ const a = e.getUint16(t.offset + 2);
1320
+ const o = e.getUint32(t.offset + 4);
1321
+ advance(t, 8);
1322
+ const i = {
1323
+ name: n,
1324
+ type: s,
1325
+ class: -32769 & a,
1326
+ flush: !!(32768 & a),
1327
+ ttl: o,
1328
+ data: null
1329
+ };
1330
+ switch (i.type) {
1331
+ case r.A:
1332
+ i.data = x.read(e, t);
1333
+ return i;
1334
+
1335
+ case r.NS:
1336
+ i.data = H.read(e, t);
1337
+ return i;
1338
+
1339
+ case r.SOA:
1340
+ i.data = K.read(e, t);
1341
+ return i;
1342
+
1343
+ case r.HINFO:
1344
+ i.data = B.read(e, t);
1345
+ return i;
1346
+
1347
+ case r.MX:
1348
+ i.data = X.read(e, t);
1349
+ return i;
1350
+
1351
+ case r.TXT:
1352
+ i.data = Y.read(e, t);
1353
+ return i;
1354
+
1355
+ case r.RP:
1356
+ i.data = Q.read(e, t);
1357
+ return i;
1358
+
1359
+ case r.AAAA:
1360
+ i.data = V.read(e, t);
1361
+ return i;
1362
+
1363
+ case r.SRV:
1364
+ i.data = q.read(e, t);
1365
+ return i;
1366
+
1367
+ case r.NAPTR:
1368
+ i.data = J.read(e, t);
1369
+ return i;
1370
+
1371
+ case r.DS:
1372
+ i.data = Z.read(e, t);
1373
+ return i;
1374
+
1375
+ case r.SSHFP:
1376
+ i.data = W.read(e, t);
1377
+ return i;
1378
+
1379
+ case r.RRSIG:
1380
+ i.data = F.read(e, t);
1381
+ return i;
1382
+
1383
+ case r.NSEC:
1384
+ i.data = j.read(e, t);
1385
+ return i;
1386
+
1387
+ case r.DNSKEY:
1388
+ i.data = $.read(e, t);
1389
+ return i;
1390
+
1391
+ case r.NSEC3:
1392
+ i.data = z.read(e, t);
1393
+ return i;
1394
+
1395
+ case r.TLSA:
1396
+ i.data = ee.read(e, t);
1397
+ return i;
1398
+
1399
+ case r.SVCB:
1400
+ case r.HTTPS:
1401
+ i.data = te.read(e, t);
1402
+ return i;
1403
+
1404
+ case r.CAA:
1405
+ i.data = G.read(e, t);
1406
+ return i;
1407
+
1408
+ case r.PTR:
1409
+ case r.CNAME:
1410
+ case r.DNAME:
1411
+ i.data = H.read(e, t);
1412
+ return i;
1413
+
1414
+ default:
1415
+ i.data = _.read(e, t);
1416
+ return i;
1417
+ }
1418
+ }
1419
+ };
1420
+
1421
+ const readList = (e, t, n, r) => {
1422
+ if (!r) {
1423
+ return;
1424
+ }
1425
+ const {offset: s, length: a} = n;
1426
+ const o = [];
1427
+ for (let i = 0; i < r && n.offset - s < a; i++) {
1428
+ o.push(e.read(t, n));
1429
+ }
1430
+ return o;
1431
+ };
1432
+
1433
+ const se = {
1434
+ bytes(e) {
1435
+ const {questions: t, answers: n, authorities: r, additionals: s} = e;
1436
+ let a = 12;
1437
+ let o = 0;
1438
+ for (o = 0; t && o < t.length; o++) {
1439
+ a += A.bytes(t[o]);
1440
+ }
1441
+ for (o = 0; n && o < n.length; o++) {
1442
+ a += re.bytes(n[o]);
1443
+ }
1444
+ for (o = 0; r && o < r.length; o++) {
1445
+ a += re.bytes(r[o]);
1446
+ }
1447
+ for (o = 0; s && o < s.length; o++) {
1448
+ a += re.bytes(s[o]);
1449
+ }
1450
+ return a;
1451
+ },
1452
+ write(e, t, r) {
1453
+ const {questions: s, answers: a, authorities: o, additionals: i} = r;
1454
+ let c = 32767 & (r.flags || 0) | (r.type || n.QUERY) | (r.rtype || 0);
1455
+ e.setUint16(t, r.id || 0);
1456
+ e.setUint16(t + 2, c);
1457
+ e.setUint16(t + 4, r.questions?.length || 0);
1458
+ e.setUint16(t + 6, r.answers?.length || 0);
1459
+ e.setUint16(t + 8, r.authorities?.length || 0);
1460
+ e.setUint16(t + 10, r.additionals?.length || 0);
1461
+ t += 12;
1462
+ let f = 0;
1463
+ for (f = 0; s && f < s.length; f++) {
1464
+ t = A.write(e, t, s[f]);
1465
+ }
1466
+ for (f = 0; a && f < a.length; f++) {
1467
+ t = re.write(e, t, a[f]);
1468
+ }
1469
+ for (f = 0; o && f < o.length; f++) {
1470
+ t = re.write(e, t, o[f]);
1471
+ }
1472
+ for (f = 0; i && f < i.length; f++) {
1473
+ t = re.write(e, t, i[f]);
1474
+ }
1475
+ return t;
1476
+ },
1477
+ read(e, t) {
1478
+ const r = e.getUint16(t.offset);
1479
+ const s = e.getUint16(t.offset + 2);
1480
+ const a = e.getUint16(t.offset + 4);
1481
+ const o = e.getUint16(t.offset + 6);
1482
+ const i = e.getUint16(t.offset + 8);
1483
+ const c = e.getUint16(t.offset + 10);
1484
+ advance(t, 12);
1485
+ return {
1486
+ id: r,
1487
+ flags: s,
1488
+ rtype: 15 & s,
1489
+ type: s & n.RESPONSE ? n.RESPONSE : n.QUERY,
1490
+ questions: readList(A, e, t, a),
1491
+ answers: readList(re, e, t, o),
1492
+ authorities: readList(re, e, t, i),
1493
+ additionals: readList(re, e, t, c)
1494
+ };
1495
+ }
1496
+ };
1497
+
1498
+ function encode(e) {
1499
+ const t = new ArrayBuffer(se.bytes(e));
1500
+ const n = se.write(new DataView(t), 0, e);
1501
+ return new Uint8Array(t, 0, n);
1502
+ }
1503
+
1504
+ const ae = (() => {
1505
+ let t = null;
1506
+ let n = null;
1507
+ return () => {
1508
+ if (!t) {
1509
+ t = e.networkInterfaces();
1510
+ }
1511
+ if (!n) {
1512
+ n = Promise.resolve().then(() => t = n = null);
1513
+ }
1514
+ return t;
1515
+ };
1516
+ })();
1517
+
1518
+ const hasScopeid = e => null != e.scopeid && e.scopeid > 0;
1519
+
1520
+ const hostname = () => e.hostname();
1521
+
1522
+ const sanitizeLabel = e => e.trim().replace(/\.local\.?$/i, "").replace(/[^a-zA-Z0-9-]/g, "-").replace(/^-+|-+$/g, "").toLowerCase().slice(0, fe);
1523
+
1524
+ const createServiceName = (e, t, n) => {
1525
+ const r = (e => e.trim().replace(/\.+/, "_").slice(0, fe))(e);
1526
+ if (n) {
1527
+ const e = ` (${((e, t) => {
1528
+ const n = `${hostname()}:${e}`;
1529
+ let r = 5381;
1530
+ if (0 !== t) {
1531
+ r = (r << 5) + r + (255 & t);
1532
+ }
1533
+ for (let e = 0, t = n.length; e < t; e++) {
1534
+ r = (r << 5) + r + n.charCodeAt(e);
1535
+ }
1536
+ return (65535 & r).toString(16).toUpperCase().padStart(4, "0");
1537
+ })(t, n - 1)})`;
1538
+ return r.slice(0, fe - e.length) + e;
1539
+ } else {
1540
+ return r;
1541
+ }
1542
+ };
1543
+
1544
+ const srvAnswer = (e, t) => ({
1545
+ type: r.SRV,
1546
+ class: s.IN,
1547
+ name: e.fqdnOut,
1548
+ ttl: t,
1549
+ flush: !0,
1550
+ data: {
1551
+ priority: 0,
1552
+ weight: 0,
1553
+ port: e.port,
1554
+ target: e.host
1555
+ }
1556
+ });
1557
+
1558
+ const txtAnswer = (e, t) => ({
1559
+ type: r.TXT,
1560
+ class: s.IN,
1561
+ name: e.fqdnOut,
1562
+ ttl: t,
1563
+ flush: !0,
1564
+ data: e.txt
1565
+ });
1566
+
1567
+ const ptrAnswer = (e, t, n) => ({
1568
+ type: r.PTR,
1569
+ class: s.IN,
1570
+ name: e,
1571
+ ttl: n,
1572
+ flush: !1,
1573
+ data: t
1574
+ });
1575
+
1576
+ const ptrAnswers = (e, t) => [ ptrAnswer(e.domain, e.fqdnOut, t), ptrAnswer(ue, e.domain, t), ...Object.keys(e.subtypes).map(n => ptrAnswer(n, e.fqdnOut, t)) ];
1577
+
1578
+ const aAnswers = (e, t, n) => t.filter(e => e.family === ie.v4).map(t => ({
1579
+ type: r.A,
1580
+ class: s.IN,
1581
+ name: e.host,
1582
+ ttl: n,
1583
+ flush: !0,
1584
+ data: t.address
1585
+ }));
1586
+
1587
+ const aaaaAnswers = (e, t, n) => t.filter(e => e.family === ie.v6).map(t => ({
1588
+ type: r.AAAA,
1589
+ class: s.IN,
1590
+ name: e.host,
1591
+ ttl: n,
1592
+ flush: !0,
1593
+ data: t.address
1594
+ }));
1595
+
1596
+ const answers = (e, t, n) => [ ...ptrAnswers(e, n), srvAnswer(e, n), txtAnswer(e, n), ...aAnswers(e, t, n), ...aaaaAnswers(e, t, n) ];
1597
+
1598
+ let oe = function(e) {
1599
+ e[e.NONE = 0] = "NONE";
1600
+ e[e.NAME = 1] = "NAME";
1601
+ e[e.HOSTNAME_A = 2] = "HOSTNAME_A";
1602
+ e[e.HOSTNAME_AAAA = 4] = "HOSTNAME_AAAA";
1603
+ e[e.HOSTNAME = e.HOSTNAME_A | e.HOSTNAME_AAAA] = "HOSTNAME";
1604
+ return e;
1605
+ }({});
1606
+
1607
+ const checkAnswerConflicts = (e, t, n) => {
1608
+ let s = oe.NONE;
1609
+ const a = new Set(n.filter(e => e.family === ie.v4).map(e => e.address));
1610
+ const o = new Set(n.filter(e => e.family === ie.v6).map(e => e.address));
1611
+ for (const n of e) {
1612
+ const e = n.name?.toLowerCase();
1613
+ if (n.type === r.SRV && e === t.fqdnIn && (n.data.port !== t.port || n.data.target.toLowerCase() !== t.host)) {
1614
+ s |= oe.NAME;
1615
+ } else if (n.type === r.A && e === t.host && !a.has(n.data)) {
1616
+ s |= oe.HOSTNAME_A;
1617
+ } else if (n.type === r.AAAA && e === t.host && !o.has(n.data.toLowerCase())) {
1618
+ s |= oe.HOSTNAME_AAAA;
1619
+ }
1620
+ }
1621
+ return s;
1622
+ };
1623
+
1624
+ const checkResponseConflicts = (e, t, r) => {
1625
+ let s = oe.NONE;
1626
+ if (e.type === n.RESPONSE) {
1627
+ if (e.answers?.length) {
1628
+ s |= checkAnswerConflicts(e.answers, t, r);
1629
+ }
1630
+ if (e.additionals?.length) {
1631
+ s |= checkAnswerConflicts(e.additionals, t, r);
1632
+ }
1633
+ }
1634
+ return s;
1635
+ };
1636
+
1637
+ const checkQuestionConflicts = (e, t, s) => {
1638
+ if (e.type !== n.QUERY || !e.questions?.length || !e.authorities?.length) {
1639
+ return oe.NONE;
1640
+ }
1641
+ return e.questions.some(e => {
1642
+ const n = e.name.toLowerCase();
1643
+ return (e => e.type === r.ANY || e.type === r.A || e.type === r.AAAA || e.type === r.SRV || e.type === r.TXT || e.type === r.PTR)(e) && (n === t.host || n === t.fqdnIn);
1644
+ }) ? checkAnswerConflicts(e.authorities, t, s) : oe.NONE;
1645
+ };
1646
+
1647
+ const responseMessage = (e, t, s, o) => {
1648
+ if (e.type !== n.QUERY || !e.questions?.length) {
1649
+ return null;
1650
+ }
1651
+ const i = Object.create(null);
1652
+ let c = !1;
1653
+ let f = !1;
1654
+ let l = !1;
1655
+ let u = !1;
1656
+ if (e.answers) {
1657
+ for (const n of e.answers) {
1658
+ const e = n.name?.toLowerCase();
1659
+ switch (n.type) {
1660
+ case r.SRV:
1661
+ c ||= e === t.fqdnIn;
1662
+ break;
1663
+
1664
+ case r.TXT:
1665
+ f ||= e === t.fqdnIn;
1666
+ break;
1667
+
1668
+ case r.A:
1669
+ l ||= e === t.host;
1670
+ break;
1671
+
1672
+ case r.AAAA:
1673
+ u ||= e === t.host;
1674
+ break;
1675
+
1676
+ case r.PTR:
1677
+ switch (e) {
1678
+ case t.domain:
1679
+ if (n.data.toLowerCase() === t.fqdnIn) {
1680
+ i[t.domain] = !0;
1681
+ }
1682
+ break;
1683
+
1684
+ case ue:
1685
+ if (n.data.toLowerCase() === t.domain) {
1686
+ i[ue] = !0;
1687
+ }
1688
+ break;
1689
+
1690
+ default:
1691
+ if (e && t.subtypes[e] && n.data.toLowerCase() === t.fqdnIn) {
1692
+ i[e] = !0;
1693
+ }
1694
+ }
1695
+ }
1696
+ }
1697
+ }
1698
+ const d = [];
1699
+ const h = [];
1700
+ for (const n of e.questions) {
1701
+ if (!!n.qu !== o) {
1702
+ continue;
1703
+ }
1704
+ const e = n.name.toLowerCase();
1705
+ switch (n.type) {
1706
+ case r.SRV:
1707
+ if (!c && e === t.fqdnIn) {
1708
+ d.push(srvAnswer(t, t.ttl));
1709
+ c = !0;
1710
+ }
1711
+ break;
1712
+
1713
+ case r.TXT:
1714
+ if (!f && e === t.fqdnIn) {
1715
+ d.push(txtAnswer(t, t.ttl));
1716
+ f = !0;
1717
+ }
1718
+ break;
1719
+
1720
+ case r.A:
1721
+ if (!l && e === t.host) {
1722
+ d.push(...aAnswers(t, s, t.ttl));
1723
+ l = !0;
1724
+ }
1725
+ break;
1726
+
1727
+ case r.AAAA:
1728
+ if (!u && e === t.host) {
1729
+ d.push(...aaaaAnswers(t, s, t.ttl));
1730
+ u = !0;
1731
+ }
1732
+ break;
1733
+
1734
+ case r.PTR:
1735
+ switch (e) {
1736
+ case t.domain:
1737
+ if (!i[t.domain]) {
1738
+ d.push(ptrAnswer(t.domain, t.fqdnOut, t.ttl));
1739
+ i[t.domain] = !0;
1740
+ }
1741
+ break;
1742
+
1743
+ case ue:
1744
+ if (!i[ue]) {
1745
+ d.push(ptrAnswer(ue, t.domain, t.ttl));
1746
+ i[ue] = !0;
1747
+ }
1748
+ break;
1749
+
1750
+ default:
1751
+ if (t.subtypes[e]) {
1752
+ d.push(ptrAnswer(e, t.fqdnOut, t.ttl));
1753
+ i[e] = !0;
1754
+ }
1755
+ }
1756
+ break;
1757
+
1758
+ case r.ANY:
1759
+ switch (e) {
1760
+ case t.fqdnIn:
1761
+ if (!c) {
1762
+ d.push(srvAnswer(t, t.ttl));
1763
+ c = !0;
1764
+ }
1765
+ if (!f) {
1766
+ d.push(txtAnswer(t, t.ttl));
1767
+ f = !0;
1768
+ }
1769
+ break;
1770
+
1771
+ case t.host:
1772
+ if (!l) {
1773
+ d.push(...aAnswers(t, s, t.ttl));
1774
+ l = !0;
1775
+ }
1776
+ if (!u) {
1777
+ d.push(...aaaaAnswers(t, s, t.ttl));
1778
+ u = !0;
1779
+ }
1780
+ break;
1781
+
1782
+ case t.domain:
1783
+ if (!i[t.domain]) {
1784
+ d.push(ptrAnswer(t.domain, t.fqdnOut, t.ttl));
1785
+ i[t.domain] = !0;
1786
+ }
1787
+ break;
1788
+
1789
+ case ue:
1790
+ if (!i[ue]) {
1791
+ d.push(ptrAnswer(ue, t.domain, t.ttl));
1792
+ i[ue] = !0;
1793
+ }
1794
+ break;
1795
+
1796
+ default:
1797
+ if (t.subtypes[e]) {
1798
+ d.push(ptrAnswer(e, t.fqdnOut, t.ttl));
1799
+ i[e] = !0;
1800
+ }
1801
+ }
1802
+ }
1803
+ }
1804
+ if (i[t.domain] && !c) {
1805
+ h.push(srvAnswer(t, t.ttl));
1806
+ c = !0;
1807
+ }
1808
+ if (i[t.domain] && !f) {
1809
+ h.push(txtAnswer(t, t.ttl));
1810
+ f = !0;
1811
+ }
1812
+ if (c && !l) {
1813
+ h.push(...aAnswers(t, s, t.ttl));
1814
+ l = !0;
1815
+ }
1816
+ if (c && !u) {
1817
+ h.push(...aaaaAnswers(t, s, t.ttl));
1818
+ u = !0;
1819
+ }
1820
+ if (d.length > 0) {
1821
+ return encode({
1822
+ type: n.RESPONSE,
1823
+ flags: a.AUTHORITATIVE_ANSWER,
1824
+ answers: d,
1825
+ additionals: h
1826
+ });
1827
+ } else {
1828
+ return null;
1829
+ }
1830
+ };
1831
+
1832
+ const createSocketSettings = (e, t) => {
1833
+ const n = t === ie.v4 ? ce.v4 : ce.v6;
1834
+ const r = ((e, t) => {
1835
+ const n = ae()[e]?.filter(e => e.family === t).map(n => ({
1836
+ ...n,
1837
+ family: t,
1838
+ iname: e
1839
+ }));
1840
+ return n?.length ? n : void 0;
1841
+ })(e, t);
1842
+ if (!r?.length) {
1843
+ return null;
1844
+ }
1845
+ const s = new Set;
1846
+ let a;
1847
+ if (t === ie.v4) {
1848
+ a = r[0].address;
1849
+ for (const e of r) {
1850
+ s.add(e.address);
1851
+ }
1852
+ } else {
1853
+ const t = "win32" === process.platform ? r.find(hasScopeid)?.scopeid.toString() : e;
1854
+ if (!t) {
1855
+ return null;
1856
+ }
1857
+ s.add(a = `::%${t}`);
1858
+ }
1859
+ return {
1860
+ family: t,
1861
+ bindings: r,
1862
+ memberships: s,
1863
+ multicastAddress: n,
1864
+ multicastInterface: a
1865
+ };
1866
+ };
1867
+
1868
+ const addMembership = (e, t, n) => {
1869
+ try {
1870
+ e.addMembership(t.multicastAddress, n);
1871
+ return !0;
1872
+ } catch (e) {
1873
+ return !1;
1874
+ }
1875
+ };
1876
+
1877
+ const dropMembership = (e, t, n) => {
1878
+ try {
1879
+ e.dropMembership(t.multicastAddress, n);
1880
+ return !0;
1881
+ } catch {
1882
+ return !1;
1883
+ }
1884
+ };
1885
+
1886
+ const createInterfaceSocket = (e, n, r) => {
1887
+ const s = [];
1888
+ let a = null;
1889
+ let o = initSocket();
1890
+ function initSocket() {
1891
+ const s = createSocketSettings(e, n);
1892
+ if (!s) {
1893
+ return null;
1894
+ }
1895
+ const a = t.createSocket(n === ie.v4 ? {
1896
+ type: "udp4",
1897
+ reuseAddr: !0
1898
+ } : {
1899
+ type: "udp6",
1900
+ reuseAddr: !0,
1901
+ ipv6Only: !0
1902
+ });
1903
+ a.unref();
1904
+ a.on("message", async (e, t) => {
1905
+ try {
1906
+ await r.onMessage(e, {
1907
+ socket: i,
1908
+ family: "IPv6" === t.family ? ie.v6 : ie.v4,
1909
+ address: t.address,
1910
+ port: t.port,
1911
+ reply: e => send(e, t.address, t.port)
1912
+ });
1913
+ } catch {}
1914
+ });
1915
+ scheduleTimer(new Promise((e, t) => {
1916
+ a.prependOnceListener("error", t);
1917
+ a.prependOnceListener("close", closeSocket);
1918
+ a.bind(le, () => {
1919
+ try {
1920
+ setupSocket(a, s);
1921
+ e();
1922
+ } catch (e) {
1923
+ closeSocket();
1924
+ t(e);
1925
+ } finally {
1926
+ a.removeListener("error", t);
1927
+ a.on("error", closeSocket);
1928
+ }
1929
+ });
1930
+ }));
1931
+ return {
1932
+ settings: s,
1933
+ socket: a
1934
+ };
1935
+ }
1936
+ function setupSocket(e, t) {
1937
+ const n = t !== o?.settings ? o?.settings : null;
1938
+ let r = !!n && n.multicastInterface !== t.multicastInterface;
1939
+ try {
1940
+ e.setMulticastTTL(255);
1941
+ e.setMulticastLoopback(!0);
1942
+ e.setMulticastInterface(t.multicastInterface);
1943
+ } catch {
1944
+ closeSocket();
1945
+ return !1;
1946
+ }
1947
+ if (n) {
1948
+ for (const s of n.memberships) {
1949
+ if (!t.memberships.has(s)) {
1950
+ dropMembership(e, n, s);
1951
+ r = !0;
1952
+ }
1953
+ }
1954
+ }
1955
+ for (const s of t.memberships) {
1956
+ if (!n?.memberships.has(s)) {
1957
+ if (!addMembership(e, t, s)) {
1958
+ t.memberships.delete(s);
1959
+ } else if (n) {
1960
+ r = !0;
1961
+ }
1962
+ }
1963
+ }
1964
+ if (!t.memberships.size) {
1965
+ closeSocket();
1966
+ return !1;
1967
+ } else if (o) {
1968
+ o.settings = t;
1969
+ }
1970
+ return r;
1971
+ }
1972
+ function closeSocket() {
1973
+ if (o) {
1974
+ const {socket: e} = o;
1975
+ o = null;
1976
+ try {
1977
+ e.close();
1978
+ } catch {}
1979
+ }
1980
+ }
1981
+ function sendImmediate(e) {
1982
+ try {
1983
+ if (o) {
1984
+ o.socket.send(e.message, 0, e.message.byteLength, e.port, e.address, e.onSent);
1985
+ } else {
1986
+ e.onSent(null);
1987
+ }
1988
+ } catch (t) {
1989
+ e.onSent(t);
1990
+ }
1991
+ }
1992
+ function flushQueue(e) {
1993
+ if (null === e || a === e) {
1994
+ a = null;
1995
+ }
1996
+ let t;
1997
+ while (!a && null != (t = s.shift())) {
1998
+ sendImmediate(t);
1999
+ }
2000
+ }
2001
+ function scheduleTimer(e = function waitTick() {
2002
+ return new Promise(e => setTimeout(e, 0).unref());
2003
+ }()) {
2004
+ if (!a) {
2005
+ const t = a = e.then(() => flushQueue(t), () => flushQueue(null));
2006
+ }
2007
+ }
2008
+ async function send(e, t, n) {
2009
+ return new Promise((r, a) => {
2010
+ scheduleTimer();
2011
+ s.push({
2012
+ message: e,
2013
+ address: t,
2014
+ port: n,
2015
+ onSent(e) {
2016
+ if (null != e) {
2017
+ a(e);
2018
+ } else {
2019
+ r();
2020
+ }
2021
+ }
2022
+ });
2023
+ });
2024
+ }
2025
+ const i = {
2026
+ get closed() {
2027
+ return !o;
2028
+ },
2029
+ get bindings() {
2030
+ return o?.settings.bindings ?? [];
2031
+ },
2032
+ async send(e) {
2033
+ if (o) {
2034
+ await send(e, o.settings.multicastAddress, le);
2035
+ }
2036
+ },
2037
+ refresh() {
2038
+ if (o) {
2039
+ const t = createSocketSettings(e, n);
2040
+ if (t) {
2041
+ return setupSocket(o.socket, t);
2042
+ } else {
2043
+ closeSocket();
2044
+ return !1;
2045
+ }
2046
+ } else {
2047
+ o = initSocket();
2048
+ return !!o;
2049
+ }
2050
+ },
2051
+ close() {
2052
+ closeSocket();
2053
+ flushQueue(null);
2054
+ }
2055
+ };
2056
+ return i;
2057
+ };
2058
+
2059
+ let ie = function(e) {
2060
+ e.v4 = "IPv4";
2061
+ e.v6 = "IPv6";
2062
+ return e;
2063
+ }({});
2064
+
2065
+ let ce = function(e) {
2066
+ e.v4 = "224.0.0.251";
2067
+ e.v6 = "ff02::fb";
2068
+ return e;
2069
+ }({});
2070
+
2071
+ const fe = 63;
2072
+
2073
+ const le = 5353;
2074
+
2075
+ const ue = "_services._dns-sd._udp.local";
2076
+
2077
+ const de = {
2078
+ onError() {},
2079
+ createSocket: (e, t) => {
2080
+ const n = createInterfaceSocket(e, ie.v4, t);
2081
+ const r = createInterfaceSocket(e, ie.v6, t);
2082
+ return {
2083
+ get closed() {
2084
+ return n.closed && r.closed;
2085
+ },
2086
+ get bindings() {
2087
+ return [ ...n.bindings, ...r.bindings ];
2088
+ },
2089
+ async send(e) {
2090
+ await Promise.all([ n.send(e), r.send(e) ]);
2091
+ },
2092
+ refresh() {
2093
+ const e = n.refresh();
2094
+ const t = r.refresh();
2095
+ return e || t;
2096
+ },
2097
+ close() {
2098
+ n.close();
2099
+ r.close();
2100
+ }
2101
+ };
2102
+ },
2103
+ createScheduler: function createScheduler() {
2104
+ const e = new Set;
2105
+ return {
2106
+ schedule: async function schedule(t, n) {
2107
+ return await async function schedule(r, s = getDelay(t, r)) {
2108
+ return new Promise(async (a, o) => {
2109
+ const i = Math.max(s, 20);
2110
+ const c = runTimer(t, i, async () => {
2111
+ e.delete(onCancel);
2112
+ let i = !1;
2113
+ try {
2114
+ const e = await n({
2115
+ attempt: r,
2116
+ async retry(e) {
2117
+ i = !0;
2118
+ return schedule(r + 1, e);
2119
+ }
2120
+ });
2121
+ a(e);
2122
+ } catch (e) {
2123
+ if (!i && shouldRetry(t, r)) {
2124
+ schedule(r + 1, s).then(a, o);
2125
+ } else {
2126
+ o(e);
2127
+ }
2128
+ }
2129
+ });
2130
+ function onCancel() {
2131
+ o(new AbortError);
2132
+ c();
2133
+ }
2134
+ e.add(onCancel);
2135
+ });
2136
+ }(0);
2137
+ },
2138
+ cancel() {
2139
+ for (const t of e) {
2140
+ t();
2141
+ }
2142
+ e.clear();
2143
+ }
2144
+ };
2145
+ },
2146
+ createServiceInput: e => ({
2147
+ ...e,
2148
+ nameSeed: 0,
2149
+ hostnameSeed: 0
2150
+ }),
2151
+ createServiceRecord: e => {
2152
+ const t = ((e, t) => {
2153
+ let n = sanitizeLabel(e);
2154
+ if (t) {
2155
+ const e = /[-_](\d+)$/.exec(n);
2156
+ if (e) {
2157
+ const r = n.slice(0, -e[0].length);
2158
+ const s = `-${(parseInt(e[1], 10) || t) + 1}`;
2159
+ n = r.slice(0, fe - s.length) + s;
2160
+ } else {
2161
+ const e = `-${t + 1}`;
2162
+ n = n.slice(0, fe - e.length) + e;
2163
+ }
2164
+ }
2165
+ return n;
2166
+ })(e.hostname, e.hostnameSeed);
2167
+ const n = createServiceName(e.name, e.port, e.nameSeed);
2168
+ const r = `_${sanitizeLabel(e.type)}._${sanitizeLabel(e.protocol)}.local`;
2169
+ const s = `${n}.${r}`;
2170
+ const a = [];
2171
+ for (const t in e.txt) {
2172
+ const n = e.txt[t];
2173
+ if ("string" == typeof n || "number" == typeof n) {
2174
+ a.push(`${t}=${n}`);
2175
+ } else if ("boolean" == typeof n && n) {
2176
+ a.push(t);
2177
+ }
2178
+ }
2179
+ return {
2180
+ domain: r,
2181
+ fqdnOut: s,
2182
+ fqdnIn: s.toLowerCase(),
2183
+ host: `${t}.local`,
2184
+ port: e.port >>> 0,
2185
+ subtypes: (e.subtypes || []).reduce((e, t) => {
2186
+ e[`${(e => e.trim().replace(/[^a-zA-Z0-9-_.]/g, "-").replace(/^-+|-+$/g, "").toLowerCase())(t)}._sub.${r}`] = !0;
2187
+ return e;
2188
+ }, Object.create(null)),
2189
+ txt: a,
2190
+ ttl: e.ttl || 250
2191
+ };
2192
+ },
2193
+ networkInterfaceNames: () => {
2194
+ const e = [];
2195
+ const t = ae();
2196
+ for (const n in t) {
2197
+ const r = t[n];
2198
+ if (r?.some(e => !e.internal)) {
2199
+ e.push(n);
2200
+ }
2201
+ }
2202
+ return e.length ? e : Object.keys(t);
2203
+ },
2204
+ hostname
2205
+ };
2206
+
2207
+ let he = function(e) {
2208
+ e[e.SEND = 0] = "SEND";
2209
+ e[e.PROBE = 1] = "PROBE";
2210
+ e[e.ANNOUNCE = 2] = "ANNOUNCE";
2211
+ e[e.REOPEN = 3] = "REOPEN";
2212
+ return e;
2213
+ }({});
2214
+
2215
+ const ye = new Map;
2216
+
2217
+ const randomDelay = (e, t) => e + Math.floor(Math.random() * (t - e + 1));
2218
+
2219
+ const shouldRetry = (e, t) => {
2220
+ switch (e) {
2221
+ case he.SEND:
2222
+ case he.PROBE:
2223
+ return t < 4;
2224
+
2225
+ case he.ANNOUNCE:
2226
+ return t < 3;
2227
+
2228
+ case he.REOPEN:
2229
+ return !0;
2230
+ }
2231
+ };
2232
+
2233
+ const getDelay = (e, t) => {
2234
+ switch (e) {
2235
+ case he.SEND:
2236
+ return randomDelay(20, 120);
2237
+
2238
+ case he.PROBE:
2239
+ return t ? 250 : randomDelay(0, 250);
2240
+
2241
+ case he.ANNOUNCE:
2242
+ return t ? 1e3 * 2 ** (Math.min(t, 3) - 1) : 0;
2243
+
2244
+ case he.REOPEN:
2245
+ return 6e3;
2246
+ }
2247
+ };
2248
+
2249
+ const runTimer = (e, t, n) => {
2250
+ const r = Date.now() + t;
2251
+ let s = ye.get(e);
2252
+ let a;
2253
+ if (s) {
2254
+ for (const t of s) {
2255
+ if (e === he.REOPEN || Math.abs(t.time - r) <= 100) {
2256
+ a = t;
2257
+ break;
2258
+ }
2259
+ }
2260
+ } else {
2261
+ s = new Set;
2262
+ ye.set(e, s);
2263
+ }
2264
+ if (!a) {
2265
+ const e = setTimeout(() => {
2266
+ s.delete(a);
2267
+ for (const e of a.tasks) {
2268
+ e();
2269
+ }
2270
+ }, t);
2271
+ if ("unref" in e) {
2272
+ e.unref();
2273
+ }
2274
+ s.add(a = {
2275
+ time: r,
2276
+ tasks: new Set,
2277
+ timeout: e
2278
+ });
2279
+ }
2280
+ a.tasks.add(n);
2281
+ return () => {
2282
+ a.tasks.delete(n);
2283
+ if (!a.tasks.size) {
2284
+ clearTimeout(a.timeout);
2285
+ s.delete(a);
2286
+ }
2287
+ };
2288
+ };
2289
+
2290
+ class AbortError extends Error {
2291
+ static isAbortError(e) {
2292
+ return e && "object" == typeof e && "AbortError" === e.name;
2293
+ }
2294
+ constructor() {
2295
+ super("Operation cancelled");
2296
+ this.name = "AbortError";
2297
+ }
2298
+ }
2299
+
2300
+ var pe = function(e) {
2301
+ e[e.PROBING = 0] = "PROBING";
2302
+ e[e.ADVERTISE = 2] = "ADVERTISE";
2303
+ e[e.CLOSED = 3] = "CLOSED";
2304
+ return e;
2305
+ }(pe || {});
2306
+
2307
+ function createInterfaceAdvertiser(e, t, o) {
2308
+ const i = o.createServiceInput(t);
2309
+ const c = o.createScheduler();
2310
+ let f = 0;
2311
+ let l = 0;
2312
+ let u = o.createServiceRecord(i);
2313
+ let d = pe.PROBING;
2314
+ let h = oe.NONE;
2315
+ const y = o.createSocket(e, {
2316
+ async onMessage(e, t) {
2317
+ if (d === pe.CLOSED) {
2318
+ return;
2319
+ }
2320
+ const r = function decode(e) {
2321
+ const t = "buffer" in e ? new DataView(e.buffer, e.byteOffset, e.byteLength) : new DataView(e);
2322
+ return se.read(t, {
2323
+ offset: 0,
2324
+ length: t.byteLength
2325
+ });
2326
+ }(e);
2327
+ if (d === pe.PROBING) {
2328
+ l++;
2329
+ h |= ((e, t, r) => e.type === n.RESPONSE ? checkResponseConflicts(e, t, r) : checkQuestionConflicts(e, t, r))(r, u, y.bindings);
2330
+ return;
2331
+ } else if (d === pe.ADVERTISE) {
2332
+ h = checkResponseConflicts(r, u, y.bindings);
2333
+ if (resolveConflicts()) {
2334
+ d = pe.PROBING;
2335
+ return;
2336
+ }
2337
+ try {
2338
+ await async function sendReply(e, t) {
2339
+ const n = responseMessage(e, u, y.bindings, !1);
2340
+ const r = responseMessage(e, u, y.bindings, !0);
2341
+ await Promise.all([ n && c.schedule(he.SEND, () => t.reply(n)), r && c.schedule(he.SEND, () => y.send(r)) ]);
2342
+ }(r, t);
2343
+ } catch (e) {
2344
+ if (!AbortError.isAbortError(e)) {
2345
+ o.onError(e);
2346
+ }
2347
+ }
2348
+ }
2349
+ }
2350
+ });
2351
+ async function sendProbe() {
2352
+ const e = ((e, t) => encode({
2353
+ type: n.QUERY,
2354
+ questions: [ {
2355
+ name: e.fqdnOut,
2356
+ type: r.ANY,
2357
+ class: s.IN,
2358
+ qu: !0
2359
+ }, {
2360
+ name: e.host,
2361
+ type: r.ANY,
2362
+ class: s.IN,
2363
+ qu: !0
2364
+ } ],
2365
+ authorities: [ srvAnswer(e, e.ttl), ...aAnswers(e, t, e.ttl), ...aaaaAnswers(e, t, e.ttl) ]
2366
+ }))(u, y.bindings);
2367
+ await c.schedule(he.SEND, () => y.send(e));
2368
+ }
2369
+ async function sendAnnouncement() {
2370
+ const e = ((e, t) => encode({
2371
+ type: n.RESPONSE,
2372
+ flags: a.AUTHORITATIVE_ANSWER,
2373
+ answers: answers(e, t, e.ttl)
2374
+ }))(u, y.bindings);
2375
+ await c.schedule(he.SEND, () => y.send(e));
2376
+ }
2377
+ async function sendGoodbye() {
2378
+ const e = ((e, t) => encode({
2379
+ type: n.RESPONSE,
2380
+ flags: a.AUTHORITATIVE_ANSWER,
2381
+ answers: answers(e, t, 0)
2382
+ }))(u, y.bindings);
2383
+ await c.schedule(he.SEND, () => y.send(e));
2384
+ }
2385
+ function resolveConflicts() {
2386
+ if (h !== oe.NONE) {
2387
+ if (h & oe.NAME) {
2388
+ i.nameSeed++;
2389
+ }
2390
+ if (h & oe.HOSTNAME) {
2391
+ i.hostnameSeed++;
2392
+ }
2393
+ u = o.createServiceRecord(i);
2394
+ h = oe.NONE;
2395
+ return !0;
2396
+ } else {
2397
+ return !1;
2398
+ }
2399
+ }
2400
+ function next() {
2401
+ switch (d) {
2402
+ case pe.PROBING:
2403
+ return async function probe() {
2404
+ l = 0;
2405
+ h = oe.NONE;
2406
+ await c.schedule(he.PROBE, async e => {
2407
+ if (y.closed) {
2408
+ d = pe.CLOSED;
2409
+ return;
2410
+ } else if (d !== pe.PROBING) {
2411
+ return;
2412
+ } else if (e.attempt >= 3) {
2413
+ if (l) {
2414
+ d = h === oe.NONE ? pe.ADVERTISE : pe.CLOSED;
2415
+ } else {
2416
+ d = pe.CLOSED;
2417
+ }
2418
+ return;
2419
+ } else {
2420
+ resolveConflicts();
2421
+ await sendProbe();
2422
+ return e.retry();
2423
+ }
2424
+ });
2425
+ return next();
2426
+ }();
2427
+
2428
+ case pe.ADVERTISE:
2429
+ return async function announce() {
2430
+ while (d === pe.ADVERTISE && !y.closed) {
2431
+ if (!await c.schedule(he.ANNOUNCE, async e => {
2432
+ if (d !== pe.ADVERTISE) {
2433
+ return !1;
2434
+ } else if (e.attempt > 2 && y.refresh()) {
2435
+ return !0;
2436
+ }
2437
+ if (!y.closed) {
2438
+ await sendAnnouncement();
2439
+ if (e.attempt < 3) {
2440
+ return e.retry();
2441
+ }
2442
+ }
2443
+ return !1;
2444
+ })) {
2445
+ await c.schedule(he.REOPEN, async e => {
2446
+ if (d !== pe.ADVERTISE) {
2447
+ return;
2448
+ } else if (!y.refresh() && !y.closed) {
2449
+ return e.retry();
2450
+ }
2451
+ });
2452
+ }
2453
+ }
2454
+ return next();
2455
+ }();
2456
+
2457
+ case pe.CLOSED:
2458
+ return async function reopen() {
2459
+ c.cancel();
2460
+ if (f++ > 15) {
2461
+ return;
2462
+ }
2463
+ await c.schedule(he.REOPEN, async e => {
2464
+ if (d === pe.CLOSED && y.refresh() && !y.closed) {
2465
+ return e.retry();
2466
+ } else if (!y.closed) {
2467
+ d = pe.PROBING;
2468
+ return;
2469
+ }
2470
+ });
2471
+ return next();
2472
+ }();
2473
+ }
2474
+ }
2475
+ return {
2476
+ promise: (async () => {
2477
+ try {
2478
+ d = pe.PROBING;
2479
+ await next();
2480
+ c.cancel();
2481
+ } catch (e) {
2482
+ if (!AbortError.isAbortError(e)) {
2483
+ o.onError(e);
2484
+ }
2485
+ } finally {
2486
+ y.close();
2487
+ }
2488
+ })(),
2489
+ async close() {
2490
+ try {
2491
+ c.cancel();
2492
+ if (d !== pe.CLOSED) {
2493
+ d = pe.CLOSED;
2494
+ await sendGoodbye();
2495
+ }
2496
+ c.cancel();
2497
+ } catch (e) {
2498
+ if (!AbortError.isAbortError(e)) {
2499
+ o.onError(e);
2500
+ }
2501
+ } finally {
2502
+ y.close();
2503
+ }
2504
+ }
2505
+ };
2506
+ }
2507
+
2508
+ exports.advertise = function advertise(e) {
2509
+ return function advertiseInternal(e, t) {
2510
+ const n = new Set(t.networkInterfaceNames());
2511
+ const r = new Map;
2512
+ const s = t.createScheduler();
2513
+ for (const s of n) {
2514
+ r.set(s, createInterfaceAdvertiser(s, e, t));
2515
+ }
2516
+ s.schedule(he.REOPEN, n => {
2517
+ try {
2518
+ const s = new Set(t.networkInterfaceNames());
2519
+ for (const n of s) {
2520
+ if (!r.has(n)) {
2521
+ r.set(n, createInterfaceAdvertiser(n, e, t));
2522
+ }
2523
+ }
2524
+ for (const [e, t] of r) {
2525
+ if (!s.has(e)) {
2526
+ r.delete(e);
2527
+ t.close();
2528
+ }
2529
+ }
2530
+ return n.retry();
2531
+ } catch (e) {
2532
+ if (!AbortError.isAbortError(e)) {
2533
+ t.onError(e);
2534
+ }
2535
+ }
2536
+ });
2537
+ return async () => {
2538
+ s.cancel();
2539
+ await Promise.all([ ...r.values() ].map(e => e.close()));
2540
+ };
2541
+ }({
2542
+ name: e.name,
2543
+ type: e.type,
2544
+ protocol: e.protocol,
2545
+ hostname: e.hostname || de.hostname(),
2546
+ port: e.port,
2547
+ subtypes: e.subtypes || [],
2548
+ txt: e.txt || {},
2549
+ ttl: e.ttl || 250
2550
+ }, de);
2551
+ };
2552
+ //# sourceMappingURL=dnssd-advertise.js.map