@tachybase/plugin-password-policy 1.0.18 → 1.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,1056 @@
1
+ (function (root) {
2
+ 'use strict';
3
+ // A list of regular expressions that match arbitrary IPv4 addresses,
4
+ // for which a number of weird notations exist.
5
+ // Note that an address like 0010.0xa5.1.1 is considered legal.
6
+ const ipv4Part = '(0?\\d+|0x[a-f0-9]+)';
7
+ const ipv4Regexes = {
8
+ fourOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`, 'i'),
9
+ threeOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`, 'i'),
10
+ twoOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}$`, 'i'),
11
+ longValue: new RegExp(`^${ipv4Part}$`, 'i')
12
+ };
13
+
14
+ // Regular Expression for checking Octal numbers
15
+ const octalRegex = new RegExp(`^0[0-7]+$`, 'i');
16
+ const hexRegex = new RegExp(`^0x[a-f0-9]+$`, 'i');
17
+
18
+ const zoneIndex = '%[0-9a-z]{1,}';
19
+
20
+ // IPv6-matching regular expressions.
21
+ // For IPv6, the task is simpler: it is enough to match the colon-delimited
22
+ // hexadecimal IPv6 and a transitional variant with dotted-decimal IPv4 at
23
+ // the end.
24
+ const ipv6Part = '(?:[0-9a-f]+::?)+';
25
+ const ipv6Regexes = {
26
+ zoneIndex: new RegExp(zoneIndex, 'i'),
27
+ 'native': new RegExp(`^(::)?(${ipv6Part})?([0-9a-f]+)?(::)?(${zoneIndex})?$`, 'i'),
28
+ deprecatedTransitional: new RegExp(`^(?:::)(${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?)$`, 'i'),
29
+ transitional: new RegExp(`^((?:${ipv6Part})|(?:::)(?:${ipv6Part})?)${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?$`, 'i')
30
+ };
31
+
32
+ // Expand :: in an IPv6 address or address part consisting of `parts` groups.
33
+ function expandIPv6 (string, parts) {
34
+ // More than one '::' means invalid adddress
35
+ if (string.indexOf('::') !== string.lastIndexOf('::')) {
36
+ return null;
37
+ }
38
+
39
+ let colonCount = 0;
40
+ let lastColon = -1;
41
+ let zoneId = (string.match(ipv6Regexes.zoneIndex) || [])[0];
42
+ let replacement, replacementCount;
43
+
44
+ // Remove zone index and save it for later
45
+ if (zoneId) {
46
+ zoneId = zoneId.substring(1);
47
+ string = string.replace(/%.+$/, '');
48
+ }
49
+
50
+ // How many parts do we already have?
51
+ while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
52
+ colonCount++;
53
+ }
54
+
55
+ // 0::0 is two parts more than ::
56
+ if (string.substr(0, 2) === '::') {
57
+ colonCount--;
58
+ }
59
+
60
+ if (string.substr(-2, 2) === '::') {
61
+ colonCount--;
62
+ }
63
+
64
+ // The following loop would hang if colonCount > parts
65
+ if (colonCount > parts) {
66
+ return null;
67
+ }
68
+
69
+ // replacement = ':' + '0:' * (parts - colonCount)
70
+ replacementCount = parts - colonCount;
71
+ replacement = ':';
72
+ while (replacementCount--) {
73
+ replacement += '0:';
74
+ }
75
+
76
+ // Insert the missing zeroes
77
+ string = string.replace('::', replacement);
78
+
79
+ // Trim any garbage which may be hanging around if :: was at the edge in
80
+ // the source strin
81
+ if (string[0] === ':') {
82
+ string = string.slice(1);
83
+ }
84
+
85
+ if (string[string.length - 1] === ':') {
86
+ string = string.slice(0, -1);
87
+ }
88
+
89
+ parts = (function () {
90
+ const ref = string.split(':');
91
+ const results = [];
92
+
93
+ for (let i = 0; i < ref.length; i++) {
94
+ results.push(parseInt(ref[i], 16));
95
+ }
96
+
97
+ return results;
98
+ })();
99
+
100
+ return {
101
+ parts: parts,
102
+ zoneId: zoneId
103
+ };
104
+ }
105
+
106
+ // A generic CIDR (Classless Inter-Domain Routing) RFC1518 range matcher.
107
+ function matchCIDR (first, second, partSize, cidrBits) {
108
+ if (first.length !== second.length) {
109
+ throw new Error('ipaddr: cannot match CIDR for objects with different lengths');
110
+ }
111
+
112
+ let part = 0;
113
+ let shift;
114
+
115
+ while (cidrBits > 0) {
116
+ shift = partSize - cidrBits;
117
+ if (shift < 0) {
118
+ shift = 0;
119
+ }
120
+
121
+ if (first[part] >> shift !== second[part] >> shift) {
122
+ return false;
123
+ }
124
+
125
+ cidrBits -= partSize;
126
+ part += 1;
127
+ }
128
+
129
+ return true;
130
+ }
131
+
132
+ function parseIntAuto (string) {
133
+ // Hexadedimal base 16 (0x#)
134
+ if (hexRegex.test(string)) {
135
+ return parseInt(string, 16);
136
+ }
137
+ // While octal representation is discouraged by ECMAScript 3
138
+ // and forbidden by ECMAScript 5, we silently allow it to
139
+ // work only if the rest of the string has numbers less than 8.
140
+ if (string[0] === '0' && !isNaN(parseInt(string[1], 10))) {
141
+ if (octalRegex.test(string)) {
142
+ return parseInt(string, 8);
143
+ }
144
+ throw new Error(`ipaddr: cannot parse ${string} as octal`);
145
+ }
146
+ // Always include the base 10 radix!
147
+ return parseInt(string, 10);
148
+ }
149
+
150
+ function padPart (part, length) {
151
+ while (part.length < length) {
152
+ part = `0${part}`;
153
+ }
154
+
155
+ return part;
156
+ }
157
+
158
+ const ipaddr = {};
159
+
160
+ // An IPv4 address (RFC791).
161
+ ipaddr.IPv4 = (function () {
162
+ // Constructs a new IPv4 address from an array of four octets
163
+ // in network order (MSB first)
164
+ // Verifies the input.
165
+ function IPv4 (octets) {
166
+ if (octets.length !== 4) {
167
+ throw new Error('ipaddr: ipv4 octet count should be 4');
168
+ }
169
+
170
+ let i, octet;
171
+
172
+ for (i = 0; i < octets.length; i++) {
173
+ octet = octets[i];
174
+ if (!((0 <= octet && octet <= 255))) {
175
+ throw new Error('ipaddr: ipv4 octet should fit in 8 bits');
176
+ }
177
+ }
178
+
179
+ this.octets = octets;
180
+ }
181
+
182
+ // Special IPv4 address ranges.
183
+ // See also https://en.wikipedia.org/wiki/Reserved_IP_addresses
184
+ IPv4.prototype.SpecialRanges = {
185
+ unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
186
+ broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
187
+ // RFC3171
188
+ multicast: [[new IPv4([224, 0, 0, 0]), 4]],
189
+ // RFC3927
190
+ linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
191
+ // RFC5735
192
+ loopback: [[new IPv4([127, 0, 0, 0]), 8]],
193
+ // RFC6598
194
+ carrierGradeNat: [[new IPv4([100, 64, 0, 0]), 10]],
195
+ // RFC1918
196
+ 'private': [
197
+ [new IPv4([10, 0, 0, 0]), 8],
198
+ [new IPv4([172, 16, 0, 0]), 12],
199
+ [new IPv4([192, 168, 0, 0]), 16]
200
+ ],
201
+ // Reserved and testing-only ranges; RFCs 5735, 5737, 2544, 1700
202
+ reserved: [
203
+ [new IPv4([192, 0, 0, 0]), 24],
204
+ [new IPv4([192, 0, 2, 0]), 24],
205
+ [new IPv4([192, 88, 99, 0]), 24],
206
+ [new IPv4([198, 18, 0, 0]), 15],
207
+ [new IPv4([198, 51, 100, 0]), 24],
208
+ [new IPv4([203, 0, 113, 0]), 24],
209
+ [new IPv4([240, 0, 0, 0]), 4]
210
+ ],
211
+ // RFC7534, RFC7535
212
+ as112: [
213
+ [new IPv4([192, 175, 48, 0]), 24],
214
+ [new IPv4([192, 31, 196, 0]), 24],
215
+ ],
216
+ // RFC7450
217
+ amt: [
218
+ [new IPv4([192, 52, 193, 0]), 24],
219
+ ],
220
+ };
221
+
222
+ // The 'kind' method exists on both IPv4 and IPv6 classes.
223
+ IPv4.prototype.kind = function () {
224
+ return 'ipv4';
225
+ };
226
+
227
+ // Checks if this address matches other one within given CIDR range.
228
+ IPv4.prototype.match = function (other, cidrRange) {
229
+ let ref;
230
+ if (cidrRange === undefined) {
231
+ ref = other;
232
+ other = ref[0];
233
+ cidrRange = ref[1];
234
+ }
235
+
236
+ if (other.kind() !== 'ipv4') {
237
+ throw new Error('ipaddr: cannot match ipv4 address with non-ipv4 one');
238
+ }
239
+
240
+ return matchCIDR(this.octets, other.octets, 8, cidrRange);
241
+ };
242
+
243
+ // returns a number of leading ones in IPv4 address, making sure that
244
+ // the rest is a solid sequence of 0's (valid netmask)
245
+ // returns either the CIDR length or null if mask is not valid
246
+ IPv4.prototype.prefixLengthFromSubnetMask = function () {
247
+ let cidr = 0;
248
+ // non-zero encountered stop scanning for zeroes
249
+ let stop = false;
250
+ // number of zeroes in octet
251
+ const zerotable = {
252
+ 0: 8,
253
+ 128: 7,
254
+ 192: 6,
255
+ 224: 5,
256
+ 240: 4,
257
+ 248: 3,
258
+ 252: 2,
259
+ 254: 1,
260
+ 255: 0
261
+ };
262
+ let i, octet, zeros;
263
+
264
+ for (i = 3; i >= 0; i -= 1) {
265
+ octet = this.octets[i];
266
+ if (octet in zerotable) {
267
+ zeros = zerotable[octet];
268
+ if (stop && zeros !== 0) {
269
+ return null;
270
+ }
271
+
272
+ if (zeros !== 8) {
273
+ stop = true;
274
+ }
275
+
276
+ cidr += zeros;
277
+ } else {
278
+ return null;
279
+ }
280
+ }
281
+
282
+ return 32 - cidr;
283
+ };
284
+
285
+ // Checks if the address corresponds to one of the special ranges.
286
+ IPv4.prototype.range = function () {
287
+ return ipaddr.subnetMatch(this, this.SpecialRanges);
288
+ };
289
+
290
+ // Returns an array of byte-sized values in network order (MSB first)
291
+ IPv4.prototype.toByteArray = function () {
292
+ return this.octets.slice(0);
293
+ };
294
+
295
+ // Converts this IPv4 address to an IPv4-mapped IPv6 address.
296
+ IPv4.prototype.toIPv4MappedAddress = function () {
297
+ return ipaddr.IPv6.parse(`::ffff:${this.toString()}`);
298
+ };
299
+
300
+ // Symmetrical method strictly for aligning with the IPv6 methods.
301
+ IPv4.prototype.toNormalizedString = function () {
302
+ return this.toString();
303
+ };
304
+
305
+ // Returns the address in convenient, decimal-dotted format.
306
+ IPv4.prototype.toString = function () {
307
+ return this.octets.join('.');
308
+ };
309
+
310
+ return IPv4;
311
+ })();
312
+
313
+ // A utility function to return broadcast address given the IPv4 interface and prefix length in CIDR notation
314
+ ipaddr.IPv4.broadcastAddressFromCIDR = function (string) {
315
+
316
+ try {
317
+ const cidr = this.parseCIDR(string);
318
+ const ipInterfaceOctets = cidr[0].toByteArray();
319
+ const subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
320
+ const octets = [];
321
+ let i = 0;
322
+ while (i < 4) {
323
+ // Broadcast address is bitwise OR between ip interface and inverted mask
324
+ octets.push(parseInt(ipInterfaceOctets[i], 10) | parseInt(subnetMaskOctets[i], 10) ^ 255);
325
+ i++;
326
+ }
327
+
328
+ return new this(octets);
329
+ } catch (e) {
330
+ throw new Error('ipaddr: the address does not have IPv4 CIDR format');
331
+ }
332
+ };
333
+
334
+ // Checks if a given string is formatted like IPv4 address.
335
+ ipaddr.IPv4.isIPv4 = function (string) {
336
+ return this.parser(string) !== null;
337
+ };
338
+
339
+ // Checks if a given string is a valid IPv4 address.
340
+ ipaddr.IPv4.isValid = function (string) {
341
+ try {
342
+ new this(this.parser(string));
343
+ return true;
344
+ } catch (e) {
345
+ return false;
346
+ }
347
+ };
348
+
349
+ // Checks if a given string is a valid IPv4 address in CIDR notation.
350
+ ipaddr.IPv4.isValidCIDR = function (string) {
351
+ try {
352
+ this.parseCIDR(string);
353
+ return true;
354
+ } catch (e) {
355
+ return false;
356
+ }
357
+ };
358
+
359
+ // Checks if a given string is a full four-part IPv4 Address.
360
+ ipaddr.IPv4.isValidFourPartDecimal = function (string) {
361
+ if (ipaddr.IPv4.isValid(string) && string.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/)) {
362
+ return true;
363
+ } else {
364
+ return false;
365
+ }
366
+ };
367
+
368
+ // A utility function to return network address given the IPv4 interface and prefix length in CIDR notation
369
+ ipaddr.IPv4.networkAddressFromCIDR = function (string) {
370
+ let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;
371
+
372
+ try {
373
+ cidr = this.parseCIDR(string);
374
+ ipInterfaceOctets = cidr[0].toByteArray();
375
+ subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
376
+ octets = [];
377
+ i = 0;
378
+ while (i < 4) {
379
+ // Network address is bitwise AND between ip interface and mask
380
+ octets.push(parseInt(ipInterfaceOctets[i], 10) & parseInt(subnetMaskOctets[i], 10));
381
+ i++;
382
+ }
383
+
384
+ return new this(octets);
385
+ } catch (e) {
386
+ throw new Error('ipaddr: the address does not have IPv4 CIDR format');
387
+ }
388
+ };
389
+
390
+ // Tries to parse and validate a string with IPv4 address.
391
+ // Throws an error if it fails.
392
+ ipaddr.IPv4.parse = function (string) {
393
+ const parts = this.parser(string);
394
+
395
+ if (parts === null) {
396
+ throw new Error('ipaddr: string is not formatted like an IPv4 Address');
397
+ }
398
+
399
+ return new this(parts);
400
+ };
401
+
402
+ // Parses the string as an IPv4 Address with CIDR Notation.
403
+ ipaddr.IPv4.parseCIDR = function (string) {
404
+ let match;
405
+
406
+ if ((match = string.match(/^(.+)\/(\d+)$/))) {
407
+ const maskLength = parseInt(match[2]);
408
+ if (maskLength >= 0 && maskLength <= 32) {
409
+ const parsed = [this.parse(match[1]), maskLength];
410
+ Object.defineProperty(parsed, 'toString', {
411
+ value: function () {
412
+ return this.join('/');
413
+ }
414
+ });
415
+ return parsed;
416
+ }
417
+ }
418
+
419
+ throw new Error('ipaddr: string is not formatted like an IPv4 CIDR range');
420
+ };
421
+
422
+ // Classful variants (like a.b, where a is an octet, and b is a 24-bit
423
+ // value representing last three octets; this corresponds to a class C
424
+ // address) are omitted due to classless nature of modern Internet.
425
+ ipaddr.IPv4.parser = function (string) {
426
+ let match, part, value;
427
+
428
+ // parseInt recognizes all that octal & hexadecimal weirdness for us
429
+ if ((match = string.match(ipv4Regexes.fourOctet))) {
430
+ return (function () {
431
+ const ref = match.slice(1, 6);
432
+ const results = [];
433
+
434
+ for (let i = 0; i < ref.length; i++) {
435
+ part = ref[i];
436
+ results.push(parseIntAuto(part));
437
+ }
438
+
439
+ return results;
440
+ })();
441
+ } else if ((match = string.match(ipv4Regexes.longValue))) {
442
+ value = parseIntAuto(match[1]);
443
+ if (value > 0xffffffff || value < 0) {
444
+ throw new Error('ipaddr: address outside defined range');
445
+ }
446
+
447
+ return ((function () {
448
+ const results = [];
449
+ let shift;
450
+
451
+ for (shift = 0; shift <= 24; shift += 8) {
452
+ results.push((value >> shift) & 0xff);
453
+ }
454
+
455
+ return results;
456
+ })()).reverse();
457
+ } else if ((match = string.match(ipv4Regexes.twoOctet))) {
458
+ return (function () {
459
+ const ref = match.slice(1, 4);
460
+ const results = [];
461
+
462
+ value = parseIntAuto(ref[1]);
463
+ if (value > 0xffffff || value < 0) {
464
+ throw new Error('ipaddr: address outside defined range');
465
+ }
466
+
467
+ results.push(parseIntAuto(ref[0]));
468
+ results.push((value >> 16) & 0xff);
469
+ results.push((value >> 8) & 0xff);
470
+ results.push( value & 0xff);
471
+
472
+ return results;
473
+ })();
474
+ } else if ((match = string.match(ipv4Regexes.threeOctet))) {
475
+ return (function () {
476
+ const ref = match.slice(1, 5);
477
+ const results = [];
478
+
479
+ value = parseIntAuto(ref[2]);
480
+ if (value > 0xffff || value < 0) {
481
+ throw new Error('ipaddr: address outside defined range');
482
+ }
483
+
484
+ results.push(parseIntAuto(ref[0]));
485
+ results.push(parseIntAuto(ref[1]));
486
+ results.push((value >> 8) & 0xff);
487
+ results.push( value & 0xff);
488
+
489
+ return results;
490
+ })();
491
+ } else {
492
+ return null;
493
+ }
494
+ };
495
+
496
+ // A utility function to return subnet mask in IPv4 format given the prefix length
497
+ ipaddr.IPv4.subnetMaskFromPrefixLength = function (prefix) {
498
+ prefix = parseInt(prefix);
499
+ if (prefix < 0 || prefix > 32) {
500
+ throw new Error('ipaddr: invalid IPv4 prefix length');
501
+ }
502
+
503
+ const octets = [0, 0, 0, 0];
504
+ let j = 0;
505
+ const filledOctetCount = Math.floor(prefix / 8);
506
+
507
+ while (j < filledOctetCount) {
508
+ octets[j] = 255;
509
+ j++;
510
+ }
511
+
512
+ if (filledOctetCount < 4) {
513
+ octets[filledOctetCount] = Math.pow(2, prefix % 8) - 1 << 8 - (prefix % 8);
514
+ }
515
+
516
+ return new this(octets);
517
+ };
518
+
519
+ // An IPv6 address (RFC2460)
520
+ ipaddr.IPv6 = (function () {
521
+ // Constructs an IPv6 address from an array of eight 16 - bit parts
522
+ // or sixteen 8 - bit parts in network order(MSB first).
523
+ // Throws an error if the input is invalid.
524
+ function IPv6 (parts, zoneId) {
525
+ let i, part;
526
+
527
+ if (parts.length === 16) {
528
+ this.parts = [];
529
+ for (i = 0; i <= 14; i += 2) {
530
+ this.parts.push((parts[i] << 8) | parts[i + 1]);
531
+ }
532
+ } else if (parts.length === 8) {
533
+ this.parts = parts;
534
+ } else {
535
+ throw new Error('ipaddr: ipv6 part count should be 8 or 16');
536
+ }
537
+
538
+ for (i = 0; i < this.parts.length; i++) {
539
+ part = this.parts[i];
540
+ if (!((0 <= part && part <= 0xffff))) {
541
+ throw new Error('ipaddr: ipv6 part should fit in 16 bits');
542
+ }
543
+ }
544
+
545
+ if (zoneId) {
546
+ this.zoneId = zoneId;
547
+ }
548
+ }
549
+
550
+ // Special IPv6 ranges
551
+ IPv6.prototype.SpecialRanges = {
552
+ // RFC4291, here and after
553
+ unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
554
+ linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
555
+ multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
556
+ loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
557
+ uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
558
+ ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
559
+ // RFC6666
560
+ discard: [new IPv6([0x100, 0, 0, 0, 0, 0, 0, 0]), 64],
561
+ // RFC6145
562
+ rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
563
+ // RFC6052
564
+ rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
565
+ // RFC3056
566
+ '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
567
+ // RFC6052, RFC6146
568
+ teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
569
+ // RFC5180
570
+ benchmarking: [new IPv6([0x2001, 0x2, 0, 0, 0, 0, 0, 0]), 48],
571
+ // RFC7450
572
+ amt: [new IPv6([0x2001, 0x3, 0, 0, 0, 0, 0, 0]), 32],
573
+ as112v6: [
574
+ [new IPv6([0x2001, 0x4, 0x112, 0, 0, 0, 0, 0]), 48],
575
+ [new IPv6([0x2620, 0x4f, 0x8000, 0, 0, 0, 0, 0]), 48],
576
+ ],
577
+ deprecated: [new IPv6([0x2001, 0x10, 0, 0, 0, 0, 0, 0]), 28],
578
+ orchid2: [new IPv6([0x2001, 0x20, 0, 0, 0, 0, 0, 0]), 28],
579
+ droneRemoteIdProtocolEntityTags: [new IPv6([0x2001, 0x30, 0, 0, 0, 0, 0, 0]), 28],
580
+ reserved: [
581
+ // RFC3849
582
+ [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 23],
583
+ // RFC2928
584
+ [new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32],
585
+ ],
586
+ };
587
+
588
+ // Checks if this address is an IPv4-mapped IPv6 address.
589
+ IPv6.prototype.isIPv4MappedAddress = function () {
590
+ return this.range() === 'ipv4Mapped';
591
+ };
592
+
593
+ // The 'kind' method exists on both IPv4 and IPv6 classes.
594
+ IPv6.prototype.kind = function () {
595
+ return 'ipv6';
596
+ };
597
+
598
+ // Checks if this address matches other one within given CIDR range.
599
+ IPv6.prototype.match = function (other, cidrRange) {
600
+ let ref;
601
+
602
+ if (cidrRange === undefined) {
603
+ ref = other;
604
+ other = ref[0];
605
+ cidrRange = ref[1];
606
+ }
607
+
608
+ if (other.kind() !== 'ipv6') {
609
+ throw new Error('ipaddr: cannot match ipv6 address with non-ipv6 one');
610
+ }
611
+
612
+ return matchCIDR(this.parts, other.parts, 16, cidrRange);
613
+ };
614
+
615
+ // returns a number of leading ones in IPv6 address, making sure that
616
+ // the rest is a solid sequence of 0's (valid netmask)
617
+ // returns either the CIDR length or null if mask is not valid
618
+ IPv6.prototype.prefixLengthFromSubnetMask = function () {
619
+ let cidr = 0;
620
+ // non-zero encountered stop scanning for zeroes
621
+ let stop = false;
622
+ // number of zeroes in octet
623
+ const zerotable = {
624
+ 0: 16,
625
+ 32768: 15,
626
+ 49152: 14,
627
+ 57344: 13,
628
+ 61440: 12,
629
+ 63488: 11,
630
+ 64512: 10,
631
+ 65024: 9,
632
+ 65280: 8,
633
+ 65408: 7,
634
+ 65472: 6,
635
+ 65504: 5,
636
+ 65520: 4,
637
+ 65528: 3,
638
+ 65532: 2,
639
+ 65534: 1,
640
+ 65535: 0
641
+ };
642
+ let part, zeros;
643
+
644
+ for (let i = 7; i >= 0; i -= 1) {
645
+ part = this.parts[i];
646
+ if (part in zerotable) {
647
+ zeros = zerotable[part];
648
+ if (stop && zeros !== 0) {
649
+ return null;
650
+ }
651
+
652
+ if (zeros !== 16) {
653
+ stop = true;
654
+ }
655
+
656
+ cidr += zeros;
657
+ } else {
658
+ return null;
659
+ }
660
+ }
661
+
662
+ return 128 - cidr;
663
+ };
664
+
665
+
666
+ // Checks if the address corresponds to one of the special ranges.
667
+ IPv6.prototype.range = function () {
668
+ return ipaddr.subnetMatch(this, this.SpecialRanges);
669
+ };
670
+
671
+ // Returns an array of byte-sized values in network order (MSB first)
672
+ IPv6.prototype.toByteArray = function () {
673
+ let part;
674
+ const bytes = [];
675
+ const ref = this.parts;
676
+ for (let i = 0; i < ref.length; i++) {
677
+ part = ref[i];
678
+ bytes.push(part >> 8);
679
+ bytes.push(part & 0xff);
680
+ }
681
+
682
+ return bytes;
683
+ };
684
+
685
+ // Returns the address in expanded format with all zeroes included, like
686
+ // 2001:0db8:0008:0066:0000:0000:0000:0001
687
+ IPv6.prototype.toFixedLengthString = function () {
688
+ const addr = ((function () {
689
+ const results = [];
690
+ for (let i = 0; i < this.parts.length; i++) {
691
+ results.push(padPart(this.parts[i].toString(16), 4));
692
+ }
693
+
694
+ return results;
695
+ }).call(this)).join(':');
696
+
697
+ let suffix = '';
698
+
699
+ if (this.zoneId) {
700
+ suffix = `%${this.zoneId}`;
701
+ }
702
+
703
+ return addr + suffix;
704
+ };
705
+
706
+ // Converts this address to IPv4 address if it is an IPv4-mapped IPv6 address.
707
+ // Throws an error otherwise.
708
+ IPv6.prototype.toIPv4Address = function () {
709
+ if (!this.isIPv4MappedAddress()) {
710
+ throw new Error('ipaddr: trying to convert a generic ipv6 address to ipv4');
711
+ }
712
+
713
+ const ref = this.parts.slice(-2);
714
+ const high = ref[0];
715
+ const low = ref[1];
716
+
717
+ return new ipaddr.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
718
+ };
719
+
720
+ // Returns the address in expanded format with all zeroes included, like
721
+ // 2001:db8:8:66:0:0:0:1
722
+ //
723
+ // Deprecated: use toFixedLengthString() instead.
724
+ IPv6.prototype.toNormalizedString = function () {
725
+ const addr = ((function () {
726
+ const results = [];
727
+
728
+ for (let i = 0; i < this.parts.length; i++) {
729
+ results.push(this.parts[i].toString(16));
730
+ }
731
+
732
+ return results;
733
+ }).call(this)).join(':');
734
+
735
+ let suffix = '';
736
+
737
+ if (this.zoneId) {
738
+ suffix = `%${this.zoneId}`;
739
+ }
740
+
741
+ return addr + suffix;
742
+ };
743
+
744
+ // Returns the address in compact, human-readable format like
745
+ // 2001:db8:8:66::1
746
+ // in line with RFC 5952 (see https://tools.ietf.org/html/rfc5952#section-4)
747
+ IPv6.prototype.toRFC5952String = function () {
748
+ const regex = /((^|:)(0(:|$)){2,})/g;
749
+ const string = this.toNormalizedString();
750
+ let bestMatchIndex = 0;
751
+ let bestMatchLength = -1;
752
+ let match;
753
+
754
+ while ((match = regex.exec(string))) {
755
+ if (match[0].length > bestMatchLength) {
756
+ bestMatchIndex = match.index;
757
+ bestMatchLength = match[0].length;
758
+ }
759
+ }
760
+
761
+ if (bestMatchLength < 0) {
762
+ return string;
763
+ }
764
+
765
+ return `${string.substring(0, bestMatchIndex)}::${string.substring(bestMatchIndex + bestMatchLength)}`;
766
+ };
767
+
768
+ // Returns the address in compact, human-readable format like
769
+ // 2001:db8:8:66::1
770
+ // Calls toRFC5952String under the hood.
771
+ IPv6.prototype.toString = function () {
772
+ return this.toRFC5952String();
773
+ };
774
+
775
+ return IPv6;
776
+
777
+ })();
778
+
779
+ // A utility function to return broadcast address given the IPv6 interface and prefix length in CIDR notation
780
+ ipaddr.IPv6.broadcastAddressFromCIDR = function (string) {
781
+ try {
782
+ const cidr = this.parseCIDR(string);
783
+ const ipInterfaceOctets = cidr[0].toByteArray();
784
+ const subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
785
+ const octets = [];
786
+ let i = 0;
787
+ while (i < 16) {
788
+ // Broadcast address is bitwise OR between ip interface and inverted mask
789
+ octets.push(parseInt(ipInterfaceOctets[i], 10) | parseInt(subnetMaskOctets[i], 10) ^ 255);
790
+ i++;
791
+ }
792
+
793
+ return new this(octets);
794
+ } catch (e) {
795
+ throw new Error(`ipaddr: the address does not have IPv6 CIDR format (${e})`);
796
+ }
797
+ };
798
+
799
+ // Checks if a given string is formatted like IPv6 address.
800
+ ipaddr.IPv6.isIPv6 = function (string) {
801
+ return this.parser(string) !== null;
802
+ };
803
+
804
+ // Checks to see if string is a valid IPv6 Address
805
+ ipaddr.IPv6.isValid = function (string) {
806
+
807
+ // Since IPv6.isValid is always called first, this shortcut
808
+ // provides a substantial performance gain.
809
+ if (typeof string === 'string' && string.indexOf(':') === -1) {
810
+ return false;
811
+ }
812
+
813
+ try {
814
+ const addr = this.parser(string);
815
+ new this(addr.parts, addr.zoneId);
816
+ return true;
817
+ } catch (e) {
818
+ return false;
819
+ }
820
+ };
821
+
822
+ // Checks if a given string is a valid IPv6 address in CIDR notation.
823
+ ipaddr.IPv6.isValidCIDR = function (string) {
824
+
825
+ // See note in IPv6.isValid
826
+ if (typeof string === 'string' && string.indexOf(':') === -1) {
827
+ return false;
828
+ }
829
+
830
+ try {
831
+ this.parseCIDR(string);
832
+ return true;
833
+ } catch (e) {
834
+ return false;
835
+ }
836
+ };
837
+
838
+ // A utility function to return network address given the IPv6 interface and prefix length in CIDR notation
839
+ ipaddr.IPv6.networkAddressFromCIDR = function (string) {
840
+ let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;
841
+
842
+ try {
843
+ cidr = this.parseCIDR(string);
844
+ ipInterfaceOctets = cidr[0].toByteArray();
845
+ subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
846
+ octets = [];
847
+ i = 0;
848
+ while (i < 16) {
849
+ // Network address is bitwise AND between ip interface and mask
850
+ octets.push(parseInt(ipInterfaceOctets[i], 10) & parseInt(subnetMaskOctets[i], 10));
851
+ i++;
852
+ }
853
+
854
+ return new this(octets);
855
+ } catch (e) {
856
+ throw new Error(`ipaddr: the address does not have IPv6 CIDR format (${e})`);
857
+ }
858
+ };
859
+
860
+ // Tries to parse and validate a string with IPv6 address.
861
+ // Throws an error if it fails.
862
+ ipaddr.IPv6.parse = function (string) {
863
+ const addr = this.parser(string);
864
+
865
+ if (addr.parts === null) {
866
+ throw new Error('ipaddr: string is not formatted like an IPv6 Address');
867
+ }
868
+
869
+ return new this(addr.parts, addr.zoneId);
870
+ };
871
+
872
+ ipaddr.IPv6.parseCIDR = function (string) {
873
+ let maskLength, match, parsed;
874
+
875
+ if ((match = string.match(/^(.+)\/(\d+)$/))) {
876
+ maskLength = parseInt(match[2]);
877
+ if (maskLength >= 0 && maskLength <= 128) {
878
+ parsed = [this.parse(match[1]), maskLength];
879
+ Object.defineProperty(parsed, 'toString', {
880
+ value: function () {
881
+ return this.join('/');
882
+ }
883
+ });
884
+ return parsed;
885
+ }
886
+ }
887
+
888
+ throw new Error('ipaddr: string is not formatted like an IPv6 CIDR range');
889
+ };
890
+
891
+ // Parse an IPv6 address.
892
+ ipaddr.IPv6.parser = function (string) {
893
+ let addr, i, match, octet, octets, zoneId;
894
+
895
+ if ((match = string.match(ipv6Regexes.deprecatedTransitional))) {
896
+ return this.parser(`::ffff:${match[1]}`);
897
+ }
898
+ if (ipv6Regexes.native.test(string)) {
899
+ return expandIPv6(string, 8);
900
+ }
901
+ if ((match = string.match(ipv6Regexes.transitional))) {
902
+ zoneId = match[6] || '';
903
+ addr = match[1]
904
+ if (!match[1].endsWith('::')) {
905
+ addr = addr.slice(0, -1)
906
+ }
907
+ addr = expandIPv6(addr + zoneId, 6);
908
+ if (addr.parts) {
909
+ octets = [
910
+ parseInt(match[2]),
911
+ parseInt(match[3]),
912
+ parseInt(match[4]),
913
+ parseInt(match[5])
914
+ ];
915
+ for (i = 0; i < octets.length; i++) {
916
+ octet = octets[i];
917
+ if (!((0 <= octet && octet <= 255))) {
918
+ return null;
919
+ }
920
+ }
921
+
922
+ addr.parts.push(octets[0] << 8 | octets[1]);
923
+ addr.parts.push(octets[2] << 8 | octets[3]);
924
+ return {
925
+ parts: addr.parts,
926
+ zoneId: addr.zoneId
927
+ };
928
+ }
929
+ }
930
+
931
+ return null;
932
+ };
933
+
934
+ // A utility function to return subnet mask in IPv6 format given the prefix length
935
+ ipaddr.IPv6.subnetMaskFromPrefixLength = function (prefix) {
936
+ prefix = parseInt(prefix);
937
+ if (prefix < 0 || prefix > 128) {
938
+ throw new Error('ipaddr: invalid IPv6 prefix length');
939
+ }
940
+
941
+ const octets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
942
+ let j = 0;
943
+ const filledOctetCount = Math.floor(prefix / 8);
944
+
945
+ while (j < filledOctetCount) {
946
+ octets[j] = 255;
947
+ j++;
948
+ }
949
+
950
+ if (filledOctetCount < 16) {
951
+ octets[filledOctetCount] = Math.pow(2, prefix % 8) - 1 << 8 - (prefix % 8);
952
+ }
953
+
954
+ return new this(octets);
955
+ };
956
+
957
+ // Try to parse an array in network order (MSB first) for IPv4 and IPv6
958
+ ipaddr.fromByteArray = function (bytes) {
959
+ const length = bytes.length;
960
+
961
+ if (length === 4) {
962
+ return new ipaddr.IPv4(bytes);
963
+ } else if (length === 16) {
964
+ return new ipaddr.IPv6(bytes);
965
+ } else {
966
+ throw new Error('ipaddr: the binary input is neither an IPv6 nor IPv4 address');
967
+ }
968
+ };
969
+
970
+ // Checks if the address is valid IP address
971
+ ipaddr.isValid = function (string) {
972
+ return ipaddr.IPv6.isValid(string) || ipaddr.IPv4.isValid(string);
973
+ };
974
+
975
+ // Checks if the address is valid IP address in CIDR notation
976
+ ipaddr.isValidCIDR = function (string) {
977
+ return ipaddr.IPv6.isValidCIDR(string) || ipaddr.IPv4.isValidCIDR(string);
978
+ };
979
+
980
+
981
+ // Attempts to parse an IP Address, first through IPv6 then IPv4.
982
+ // Throws an error if it could not be parsed.
983
+ ipaddr.parse = function (string) {
984
+ if (ipaddr.IPv6.isValid(string)) {
985
+ return ipaddr.IPv6.parse(string);
986
+ } else if (ipaddr.IPv4.isValid(string)) {
987
+ return ipaddr.IPv4.parse(string);
988
+ } else {
989
+ throw new Error('ipaddr: the address has neither IPv6 nor IPv4 format');
990
+ }
991
+ };
992
+
993
+ // Attempt to parse CIDR notation, first through IPv6 then IPv4.
994
+ // Throws an error if it could not be parsed.
995
+ ipaddr.parseCIDR = function (string) {
996
+ try {
997
+ return ipaddr.IPv6.parseCIDR(string);
998
+ } catch (e) {
999
+ try {
1000
+ return ipaddr.IPv4.parseCIDR(string);
1001
+ } catch (e2) {
1002
+ throw new Error('ipaddr: the address has neither IPv6 nor IPv4 CIDR format');
1003
+ }
1004
+ }
1005
+ };
1006
+
1007
+ // Parse an address and return plain IPv4 address if it is an IPv4-mapped address
1008
+ ipaddr.process = function (string) {
1009
+ const addr = this.parse(string);
1010
+
1011
+ if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
1012
+ return addr.toIPv4Address();
1013
+ } else {
1014
+ return addr;
1015
+ }
1016
+ };
1017
+
1018
+ // An utility function to ease named range matching. See examples below.
1019
+ // rangeList can contain both IPv4 and IPv6 subnet entries and will not throw errors
1020
+ // on matching IPv4 addresses to IPv6 ranges or vice versa.
1021
+ ipaddr.subnetMatch = function (address, rangeList, defaultName) {
1022
+ let i, rangeName, rangeSubnets, subnet;
1023
+
1024
+ if (defaultName === undefined || defaultName === null) {
1025
+ defaultName = 'unicast';
1026
+ }
1027
+
1028
+ for (rangeName in rangeList) {
1029
+ if (Object.prototype.hasOwnProperty.call(rangeList, rangeName)) {
1030
+ rangeSubnets = rangeList[rangeName];
1031
+ // ECMA5 Array.isArray isn't available everywhere
1032
+ if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
1033
+ rangeSubnets = [rangeSubnets];
1034
+ }
1035
+
1036
+ for (i = 0; i < rangeSubnets.length; i++) {
1037
+ subnet = rangeSubnets[i];
1038
+ if (address.kind() === subnet[0].kind() && address.match.apply(address, subnet)) {
1039
+ return rangeName;
1040
+ }
1041
+ }
1042
+ }
1043
+ }
1044
+
1045
+ return defaultName;
1046
+ };
1047
+
1048
+ // Export for both the CommonJS and browser-like environment
1049
+ if (typeof module !== 'undefined' && module.exports) {
1050
+ module.exports = ipaddr;
1051
+
1052
+ } else {
1053
+ root.ipaddr = ipaddr;
1054
+ }
1055
+
1056
+ }(this));