ipaddress 0.2.3 → 0.2.6

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.
Files changed (113) hide show
  1. package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +484 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -5
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +18 -16
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1228 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +176 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +35 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +25 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy.d.ts +2 -0
  44. package/dist/test/test_crunchy.d.ts.map +1 -0
  45. package/dist/test/test_crunchy.js +451 -0
  46. package/dist/test/test_ipaddress.d.ts +2 -0
  47. package/dist/test/test_ipaddress.d.ts.map +1 -0
  48. package/dist/test/test_ipaddress.js +182 -0
  49. package/dist/test/test_ipv4.d.ts +2 -0
  50. package/dist/test/test_ipv4.d.ts.map +1 -0
  51. package/dist/test/test_ipv4.js +561 -0
  52. package/dist/test/test_ipv6.d.ts +2 -0
  53. package/dist/test/test_ipv6.d.ts.map +1 -0
  54. package/dist/test/test_ipv6.js +313 -0
  55. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  56. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  57. package/dist/test/test_ipv6_loopback.js +40 -0
  58. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  59. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_mapped.js +89 -0
  61. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  62. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_unspec.js +39 -0
  64. package/dist/test/test_prefix128.d.ts +2 -0
  65. package/dist/test/test_prefix128.d.ts.map +1 -0
  66. package/dist/test/test_prefix128.js +35 -0
  67. package/dist/test/test_prefix32.d.ts +2 -0
  68. package/dist/test/test_prefix32.d.ts.map +1 -0
  69. package/dist/test/test_prefix32.js +126 -0
  70. package/dist/test/test_rle.d.ts +2 -0
  71. package/dist/test/test_rle.d.ts.map +1 -0
  72. package/dist/test/test_rle.js +141 -0
  73. package/dist/vitest.config.d.ts +3 -0
  74. package/dist/vitest.config.d.ts.map +1 -0
  75. package/dist/vitest.config.js +11 -0
  76. package/package.json +40 -37
  77. package/src/crunchy.ts +546 -0
  78. package/src/index.ts +23 -0
  79. package/src/ip_bits.ts +148 -0
  80. package/src/ip_version.ts +6 -0
  81. package/src/ipaddress.ts +1331 -0
  82. package/src/ipv4.ts +190 -0
  83. package/src/ipv6.ts +202 -0
  84. package/src/ipv6_loopback.ts +55 -0
  85. package/src/ipv6_mapped.ts +163 -0
  86. package/src/ipv6_unspec.ts +59 -0
  87. package/src/prefix.ts +170 -0
  88. package/src/prefix128.ts +38 -0
  89. package/src/prefix32.ts +28 -0
  90. package/src/rle.ts +85 -0
  91. package/test/test_crunchy.ts +614 -0
  92. package/test/test_ipaddress.ts +208 -0
  93. package/test/test_ipv4.ts +596 -0
  94. package/test/test_ipv6.ts +325 -0
  95. package/test/test_ipv6_loopback.ts +52 -0
  96. package/test/test_ipv6_mapped.ts +101 -0
  97. package/test/test_ipv6_unspec.ts +51 -0
  98. package/test/test_prefix128.ts +39 -0
  99. package/test/test_prefix32.ts +130 -0
  100. package/test/test_rle.ts +144 -0
  101. package/LICENSE +0 -20
  102. package/README.md +0 -956
  103. package/index.d.ts +0 -12
  104. package/ipaddress.js +0 -2854
  105. package/ipaddress.js.map +0 -1
  106. package/ipv4.d.ts +0 -14
  107. package/ipv6.d.ts +0 -12
  108. package/ipv6_loopback.d.ts +0 -5
  109. package/ipv6_mapped.d.ts +0 -5
  110. package/ipv6_unspec.d.ts +0 -5
  111. package/prefix128.d.ts +0 -6
  112. package/prefix32.d.ts +0 -6
  113. package/rle.d.ts +0 -14
package/src/prefix.ts ADDED
@@ -0,0 +1,170 @@
1
+ import IpBits from "./ip_bits.js";
2
+ import Crunchy from "./crunchy.js";
3
+
4
+ export type From = (source: Prefix, num: number) => Prefix;
5
+
6
+ interface PrefixParams {
7
+ num: number;
8
+ ip_bits: IpBits;
9
+ net_mask: Crunchy;
10
+ vt_from: From;
11
+ }
12
+
13
+ export class Prefix {
14
+ num: number;
15
+ ip_bits: IpBits;
16
+ net_mask: Crunchy;
17
+ vt_from: From;
18
+
19
+ constructor(obj: PrefixParams) {
20
+ this.num = obj.num;
21
+ this.ip_bits = obj.ip_bits;
22
+ this.net_mask = obj.net_mask;
23
+ this.vt_from = obj.vt_from;
24
+ }
25
+
26
+ public clone(): Prefix {
27
+ return new Prefix({
28
+ num: this.num,
29
+ ip_bits: this.ip_bits,
30
+ net_mask: this.net_mask,
31
+ vt_from: this.vt_from,
32
+ });
33
+ }
34
+
35
+ public eq(other: Prefix): boolean {
36
+ return this.ip_bits.version == other.ip_bits.version && this.num == other.num;
37
+ }
38
+
39
+ public ne(other: Prefix): boolean {
40
+ return !this.eq(other);
41
+ }
42
+
43
+ public cmp(oth: Prefix): number {
44
+ if (this.ip_bits.version < oth.ip_bits.version) {
45
+ return -1;
46
+ } else if (this.ip_bits.version > oth.ip_bits.version) {
47
+ return 1;
48
+ } else {
49
+ if (this.num < oth.num) {
50
+ return -1;
51
+ } else if (this.num > oth.num) {
52
+ return 1;
53
+ } else {
54
+ return 0;
55
+ }
56
+ }
57
+ }
58
+
59
+ // #[allow(dead_code)]
60
+ public from(num: number): Prefix {
61
+ return this.vt_from(this, num);
62
+ }
63
+
64
+ public to_ip_str(): string {
65
+ return this.ip_bits.vt_as_compressed_string(this.ip_bits, this.net_mask);
66
+ }
67
+
68
+ public size(): Crunchy {
69
+ return Crunchy.one().shl(this.ip_bits.bits - this.num);
70
+ }
71
+
72
+ public static new_netmask(prefix: number, bits: number): Crunchy {
73
+ let mask = Crunchy.zero();
74
+ const host_prefix = bits - prefix;
75
+ for (let i = 0; i < prefix; ++i) {
76
+ // console.log(">>>", i, host_prefix, mask);
77
+ mask = mask.add(Crunchy.one().shl(host_prefix + i));
78
+ }
79
+ return mask;
80
+ }
81
+
82
+ public netmask(): Crunchy {
83
+ return this.net_mask;
84
+ }
85
+
86
+ public get_prefix(): number {
87
+ return this.num;
88
+ }
89
+
90
+ // The hostmask is the contrary of the subnet mask,
91
+ // as it shows the bits that can change within the
92
+ // hosts
93
+ //
94
+ // prefix = IPAddress::Prefix32.new 24
95
+ //
96
+ // prefix.hostmask
97
+ // // => "0.0.0.255"
98
+ //
99
+ public host_mask(): Crunchy {
100
+ let ret = Crunchy.zero();
101
+ for (let _ = 0; _ < this.ip_bits.bits - this.num; _++) {
102
+ ret = ret.shl(1).add(Crunchy.one());
103
+ }
104
+ return ret;
105
+ }
106
+
107
+ //
108
+ // Returns the length of the host portion
109
+ // of a netmask.
110
+ //
111
+ // prefix = Prefix128.new 96
112
+ //
113
+ // prefix.host_prefix
114
+ // // => 128
115
+ //
116
+ public host_prefix(): number {
117
+ return this.ip_bits.bits - this.num;
118
+ }
119
+
120
+ //
121
+ // Transforms the prefix into a string of bits
122
+ // representing the netmask
123
+ //
124
+ // prefix = IPAddress::Prefix128.new 64
125
+ //
126
+ // prefix.bits
127
+ // // => "1111111111111111111111111111111111111111111111111111111111111111"
128
+ // "0000000000000000000000000000000000000000000000000000000000000000"
129
+ //
130
+ public bits(): string {
131
+ return this.netmask().toString(2);
132
+ }
133
+ // #[allow(dead_code)]
134
+ // public net_mask(&self) -> BigUint {
135
+ // return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize);
136
+ // }
137
+
138
+ public to_s(): string {
139
+ return this.get_prefix().toString();
140
+ }
141
+
142
+ // #[allow(dead_code)]
143
+ // public inspect(&self) -> String {
144
+ // return self.to_s();
145
+ // }
146
+ public to_i(): number {
147
+ return this.get_prefix();
148
+ }
149
+
150
+ public add_prefix(other: Prefix): Prefix {
151
+ return this.from(this.get_prefix() + other.get_prefix());
152
+ }
153
+
154
+ public add(other: number): Prefix {
155
+ return this.from(this.get_prefix() + other);
156
+ }
157
+
158
+ public sub_prefix(other: Prefix): Prefix {
159
+ return this.sub(other.get_prefix());
160
+ }
161
+
162
+ public sub(other: number): Prefix {
163
+ if (other > this.get_prefix()) {
164
+ return this.from(other - this.get_prefix());
165
+ }
166
+ return this.from(this.get_prefix() - other);
167
+ }
168
+ }
169
+
170
+ export default Prefix;
@@ -0,0 +1,38 @@
1
+ import Prefix from "./prefix.js";
2
+ import IpBits from "./ip_bits.js";
3
+
4
+ const Prefix128 = {
5
+ // #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
6
+ // pub struct Prefix128 {
7
+ // }
8
+ //
9
+ // impl Prefix128 {
10
+ //
11
+ // Creates a new prefix object for 128 bits IPv6 addresses
12
+ //
13
+ // prefix = IPAddressPrefix128.new 64
14
+ // // => 64
15
+ //
16
+ // #[allow(unused_comparisons)]
17
+ create(num: number): Prefix {
18
+ if (num <= 128) {
19
+ // static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
20
+ // static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
21
+ const ip_bits = IpBits.v6();
22
+ const bits = ip_bits.bits;
23
+ return new Prefix({
24
+ num,
25
+ ip_bits,
26
+ net_mask: Prefix.new_netmask(num, bits),
27
+ vt_from: Prefix128.from, // vt_to_ip_str: _TO_IP_STR
28
+ });
29
+ }
30
+ return null;
31
+ },
32
+
33
+ from(my: Prefix, num: number): Prefix {
34
+ return Prefix128.create(num);
35
+ },
36
+ };
37
+
38
+ export default Prefix128;
@@ -0,0 +1,28 @@
1
+ import Prefix from "./prefix.js";
2
+ import IpBits from "./ip_bits.js";
3
+
4
+ const Prefix32 = {
5
+ from(my: Prefix, num: number): Prefix {
6
+ return Prefix32.create(num);
7
+ },
8
+
9
+ create(num: number): Prefix {
10
+ if (num >= 0 && num <= 32) {
11
+ // static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
12
+ // &from;
13
+ // static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
14
+ const ip_bits = IpBits.v4();
15
+ const bits = ip_bits.bits;
16
+ return new Prefix({
17
+ num,
18
+ ip_bits,
19
+ net_mask: Prefix.new_netmask(num, bits),
20
+ vt_from: Prefix32.from,
21
+ // vt_to_ip_str: _TO_IP_STR,
22
+ });
23
+ }
24
+ return null;
25
+ },
26
+ };
27
+
28
+ export default Prefix32;
package/src/rle.ts ADDED
@@ -0,0 +1,85 @@
1
+ class Last {
2
+ val?: Rle;
3
+ max_poses: number[][] = [];
4
+ ret: Rle[] = [];
5
+
6
+ public handle_last() {
7
+ if (this.val == null) {
8
+ return;
9
+ }
10
+ const _last = this.val;
11
+ let max_rles = this.max_poses[_last.part];
12
+ if (max_rles == null) {
13
+ max_rles = this.max_poses[_last.part] = [];
14
+ }
15
+ // console.log(_last.part, this.max_poses);
16
+ for (const idx in max_rles) {
17
+ const prev = this.ret[max_rles[idx]];
18
+ if (prev.cnt > _last.cnt) {
19
+ // console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
20
+ _last.max = false;
21
+ } else if (prev.cnt == _last.cnt) {
22
+ // nothing
23
+ } else if (prev.cnt < _last.cnt) {
24
+ // console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
25
+ prev.max = false;
26
+ }
27
+ }
28
+ // println!("push:{}:{:?}", self.ret.len(), _last);
29
+ max_rles.push(this.ret.length);
30
+ _last.pos = this.ret.length;
31
+ this.ret.push(_last);
32
+ }
33
+ }
34
+
35
+ interface RleParams {
36
+ part: number;
37
+ pos: number;
38
+ cnt: number;
39
+ max: boolean;
40
+ }
41
+
42
+ class Rle {
43
+ public part: number;
44
+ public pos: number;
45
+ public cnt: number;
46
+ public max: boolean;
47
+
48
+ constructor(obj: RleParams) {
49
+ this.part = obj.part;
50
+ this.pos = obj.pos;
51
+ this.cnt = obj.cnt;
52
+ this.max = obj.max;
53
+ }
54
+
55
+ public toString() {
56
+ return `<Rle@part:${this.part},pos:${this.pos},cnt:${this.cnt},max:${this.max}>`;
57
+ }
58
+
59
+ public eq(other: Rle): boolean {
60
+ return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
61
+ }
62
+
63
+ public ne(other: Rle): boolean {
64
+ return !this.eq(other);
65
+ }
66
+
67
+ public static code(parts: number[]): Rle[] {
68
+ const last = new Last();
69
+ // println!("code");
70
+ for (const part of parts) {
71
+ // const part = parts[i]
72
+ // console.log(`part:${part}`);
73
+ if (last.val && last.val.part == part) {
74
+ last.val.cnt += 1;
75
+ } else {
76
+ last.handle_last();
77
+ last.val = new Rle({ part, pos: 0, cnt: 1, max: true });
78
+ }
79
+ }
80
+ last.handle_last();
81
+ return last.ret;
82
+ }
83
+ }
84
+
85
+ export default Rle;