ipaddress 0.2.6 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/crunchy.d.ts +107 -33
- package/dist/src/crunchy.d.ts.map +1 -1
- package/dist/src/crunchy.js +274 -142
- package/dist/src/ip_bits.d.ts +2 -2
- package/dist/src/ip_bits.d.ts.map +1 -1
- package/dist/src/ipaddress.d.ts +24 -23
- package/dist/src/ipaddress.d.ts.map +1 -1
- package/dist/src/ipaddress.js +22 -7
- package/dist/src/ipv4.d.ts +3 -3
- package/dist/src/ipv4.d.ts.map +1 -1
- package/dist/src/ipv4.js +26 -15
- package/dist/src/ipv6.d.ts +4 -4
- package/dist/src/ipv6.d.ts.map +1 -1
- package/dist/src/ipv6.js +1 -1
- package/dist/src/ipv6_loopback.d.ts +1 -1
- package/dist/src/ipv6_loopback.d.ts.map +1 -1
- package/dist/src/ipv6_mapped.d.ts +1 -1
- package/dist/src/ipv6_mapped.d.ts.map +1 -1
- package/dist/src/ipv6_unspec.d.ts +1 -1
- package/dist/src/ipv6_unspec.d.ts.map +1 -1
- package/dist/src/prefix128.d.ts +1 -1
- package/dist/src/prefix128.d.ts.map +1 -1
- package/dist/src/prefix128.js +5 -1
- package/dist/src/prefix32.d.ts +1 -1
- package/dist/src/prefix32.d.ts.map +1 -1
- package/dist/src/prefix32.js +5 -1
- package/dist/test/test_crunchy_impls.d.ts +2 -0
- package/dist/test/test_crunchy_impls.d.ts.map +1 -0
- package/dist/test/test_crunchy_impls.js +150 -0
- package/dist/test/test_crunchy_with32bit.d.ts +2 -0
- package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
- package/dist/test/{test_crunchy.js → test_crunchy_with32bit.js} +113 -182
- package/dist/test/test_ipv4.js +1 -1
- package/dist/test/test_ipv6.js +1 -1
- package/package.json +10 -9
- package/src/crunchy.ts +365 -180
- package/src/ip_bits.ts +11 -11
- package/src/ipaddress.ts +49 -33
- package/src/ipv4.ts +32 -19
- package/src/ipv6.ts +6 -6
- package/src/ipv6_loopback.ts +1 -1
- package/src/ipv6_mapped.ts +1 -1
- package/src/ipv6_unspec.ts +1 -1
- package/src/prefix128.ts +6 -2
- package/src/prefix32.ts +6 -2
- package/test/test_crunchy_impls.ts +179 -0
- package/test/{test_crunchy.ts → test_crunchy_with32bit.ts} +131 -235
- package/test/test_ipaddress.ts +9 -9
- package/test/test_ipv4.ts +115 -115
- package/test/test_ipv6.ts +62 -62
- package/test/test_ipv6_loopback.ts +1 -1
- package/test/test_ipv6_mapped.ts +4 -4
- package/test/test_ipv6_unspec.ts +1 -1
- package/test/test_prefix128.ts +2 -2
- package/test/test_prefix32.ts +13 -13
- package/dist/test/test_crunchy.d.ts +0 -2
- package/dist/test/test_crunchy.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ip_bits.d.ts","sourceRoot":"","sources":["../../src/ip_bits.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAInC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAExC,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,MAAM,CAAC;AAGhE,qBAAa,MAAM;IACjB,OAAO,
|
|
1
|
+
{"version":3,"file":"ip_bits.d.ts","sourceRoot":"","sources":["../../src/ip_bits.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAInC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAExC,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,MAAM,CAAC;AAGhE,qBAAa,MAAM;IACjB,OAAO,EAAG,SAAS,CAAC;IACpB,uBAAuB,EAAG,QAAQ,CAAC;IACnC,yBAAyB,EAAG,QAAQ,CAAC;IACrC,IAAI,EAAG,MAAM,CAAC;IACd,SAAS,EAAG,MAAM,CAAC;IACnB,QAAQ,EAAG,MAAM,CAAC;IAClB,UAAU,EAAG,MAAM,CAAC;IACpB,QAAQ,EAAG,MAAM,CAAC;IAClB,QAAQ,EAAG,OAAO,CAAC;IAEZ,KAAK,IAAI,MAAM;IAcf,KAAK,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM,EAAE;IAa5B,oBAAoB,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM;IAIzC,sBAAsB,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM;IAI3C,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IASzC,OAAc,GAAG,EAAE,MAAM,GAAG,IAAI,CAAQ;WAC1B,EAAE,IAAI,MAAM;IAkB1B,OAAc,GAAG,EAAE,MAAM,GAAG,IAAI,CAAQ;WAC1B,EAAE,IAAI,MAAM;WAkBZ,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;WAWlE,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;WAsBlE,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;CAUnF;AAED,eAAe,MAAM,CAAC"}
|
package/dist/src/ipaddress.d.ts
CHANGED
|
@@ -13,16 +13,16 @@ export interface IPAddressParams {
|
|
|
13
13
|
ip_bits: IpBits;
|
|
14
14
|
host_address: Crunchy;
|
|
15
15
|
prefix: Prefix;
|
|
16
|
-
mapped?: IPAddress;
|
|
17
|
-
vt_is_private
|
|
18
|
-
vt_is_loopback
|
|
19
|
-
vt_to_ipv6
|
|
16
|
+
mapped?: IPAddress | null;
|
|
17
|
+
vt_is_private: Is;
|
|
18
|
+
vt_is_loopback: Is;
|
|
19
|
+
vt_to_ipv6: ToIpv4;
|
|
20
20
|
}
|
|
21
21
|
export declare class IPAddress {
|
|
22
22
|
ip_bits: IpBits;
|
|
23
23
|
host_address: Crunchy;
|
|
24
24
|
prefix: Prefix;
|
|
25
|
-
mapped: IPAddress;
|
|
25
|
+
mapped: IPAddress | null;
|
|
26
26
|
vt_is_private: Is;
|
|
27
27
|
vt_is_loopback: Is;
|
|
28
28
|
vt_to_ipv6: ToIpv4;
|
|
@@ -35,19 +35,20 @@ export declare class IPAddress {
|
|
|
35
35
|
cmp(oth: IPAddress): number;
|
|
36
36
|
eq(other: IPAddress): boolean;
|
|
37
37
|
ne(other: IPAddress): boolean;
|
|
38
|
-
static parse(str: string): IPAddress;
|
|
39
|
-
static
|
|
38
|
+
static parse(str: string): IPAddress | null;
|
|
39
|
+
static parse_or_throw(str: string): IPAddress;
|
|
40
|
+
static split_at_slash(str: string): [string, string | null];
|
|
40
41
|
from(addr: Crunchy, prefix: Prefix): IPAddress;
|
|
41
42
|
is_ipv4(): boolean;
|
|
42
43
|
is_ipv6(): boolean;
|
|
43
44
|
static is_valid(addr: string): boolean;
|
|
44
|
-
static parse_dec_str(str: string): number;
|
|
45
|
-
static parse_hex_str(str: string): number;
|
|
46
|
-
static parse_ipv4_part(i: string): number;
|
|
47
|
-
static split_to_u32(addr: string): Crunchy;
|
|
45
|
+
static parse_dec_str(str: string): number | null;
|
|
46
|
+
static parse_hex_str(str: string): number | null;
|
|
47
|
+
static parse_ipv4_part(i: string): number | null;
|
|
48
|
+
static split_to_u32(addr: string): Crunchy | null;
|
|
48
49
|
static is_valid_ipv4(addr: string): boolean;
|
|
49
|
-
static split_on_colon(addr: string): ResultCrunchyParts;
|
|
50
|
-
static split_to_num(addr: string): ResultCrunchyParts;
|
|
50
|
+
static split_on_colon(addr: string): ResultCrunchyParts | null;
|
|
51
|
+
static split_to_num(addr: string): ResultCrunchyParts | null;
|
|
51
52
|
static is_valid_ipv6(addr: string): boolean;
|
|
52
53
|
static pos_to_idx(pos: number, len: number): number;
|
|
53
54
|
static aggregate(networks: IPAddress[]): IPAddress[];
|
|
@@ -64,10 +65,10 @@ export declare class IPAddress {
|
|
|
64
65
|
is_loopback(): boolean;
|
|
65
66
|
is_mapped(): boolean;
|
|
66
67
|
static is_valid_netmask(addr: string): boolean;
|
|
67
|
-
static netmask_to_prefix(nm: Crunchy, bits: number): number;
|
|
68
|
-
static parse_netmask_to_prefix(netmask: string): number;
|
|
69
|
-
change_prefix(num: number): IPAddress;
|
|
70
|
-
change_netmask(str: string): IPAddress;
|
|
68
|
+
static netmask_to_prefix(nm: Crunchy, bits: number): number | null;
|
|
69
|
+
static parse_netmask_to_prefix(netmask: string): number | null;
|
|
70
|
+
change_prefix(num: number): IPAddress | null;
|
|
71
|
+
change_netmask(str: string): IPAddress | null;
|
|
71
72
|
to_string(): string;
|
|
72
73
|
to_s(): string;
|
|
73
74
|
to_string_uncompressed(): string;
|
|
@@ -89,8 +90,8 @@ export declare class IPAddress {
|
|
|
89
90
|
first(): IPAddress;
|
|
90
91
|
last(): IPAddress;
|
|
91
92
|
each_host(func: EachFn): void;
|
|
92
|
-
inc(): IPAddress;
|
|
93
|
-
dec(): IPAddress;
|
|
93
|
+
inc(): IPAddress | null;
|
|
94
|
+
dec(): IPAddress | null;
|
|
94
95
|
each(func: EachFn): void;
|
|
95
96
|
size(): Crunchy;
|
|
96
97
|
is_same_kind(oth: IPAddress): boolean;
|
|
@@ -98,11 +99,11 @@ export declare class IPAddress {
|
|
|
98
99
|
includes_all(oths: IPAddress[]): boolean;
|
|
99
100
|
is_private(): boolean;
|
|
100
101
|
sum_first_found(arr: IPAddress[]): IPAddress[];
|
|
101
|
-
split(subnets: number): IPAddress[];
|
|
102
|
-
supernet(new_prefix: number): IPAddress;
|
|
103
|
-
subnet(subprefix: number): IPAddress[];
|
|
102
|
+
split(subnets: number): IPAddress[] | null;
|
|
103
|
+
supernet(new_prefix: number): IPAddress | null;
|
|
104
|
+
subnet(subprefix: number): IPAddress[] | null;
|
|
104
105
|
to_ipv6(): IPAddress;
|
|
105
|
-
newprefix(num: number): Prefix;
|
|
106
|
+
newprefix(num: number): Prefix | null;
|
|
106
107
|
}
|
|
107
108
|
export default IPAddress;
|
|
108
109
|
//# sourceMappingURL=ipaddress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipaddress.d.ts","sourceRoot":"","sources":["../../src/ipaddress.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,cAAc,CAAC;AAMlC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC;AAEhD,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,SAAS,CAAC;AAEtD,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,CAAC;AAEjD,qBAAa,kBAAkB;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;gBAET,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM;CAK5C;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,OAAO,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"ipaddress.d.ts","sourceRoot":"","sources":["../../src/ipaddress.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,cAAc,CAAC;AAMlC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,OAAO,CAAC;AAEhD,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,SAAS,CAAC;AAEtD,MAAM,MAAM,MAAM,GAAG,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI,CAAC;AAEjD,qBAAa,kBAAkB;IACtB,OAAO,EAAE,OAAO,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QAAQ,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO;IAoBjC,YAAY,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO;IAkBxC,UAAU,IAAI,OAAO;IAmCrB,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE;IAkB9C,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,IAAI;IA2C1C,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;IAgC9C,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,IAAI;IA0B7C,OAAO,IAAI,SAAS;IAIpB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CAS7C;AAED,eAAe,SAAS,CAAC"}
|
package/dist/src/ipaddress.js
CHANGED
|
@@ -24,7 +24,7 @@ export class IPAddress {
|
|
|
24
24
|
this.ip_bits = obj.ip_bits;
|
|
25
25
|
this.host_address = obj.host_address;
|
|
26
26
|
this.prefix = obj.prefix;
|
|
27
|
-
this.mapped = obj.mapped;
|
|
27
|
+
this.mapped = obj.mapped ?? null;
|
|
28
28
|
this.vt_is_private = obj.vt_is_private;
|
|
29
29
|
this.vt_is_loopback = obj.vt_is_loopback;
|
|
30
30
|
this.vt_to_ipv6 = obj.vt_to_ipv6;
|
|
@@ -122,6 +122,17 @@ export class IPAddress {
|
|
|
122
122
|
}
|
|
123
123
|
return null;
|
|
124
124
|
}
|
|
125
|
+
// Like parse(), but for literals that are known to be valid IP
|
|
126
|
+
// addresses (e.g. hardcoded well-known ranges). Throws instead of
|
|
127
|
+
// returning null so call sites don't need to deal with a nullable
|
|
128
|
+
// result they can never actually observe.
|
|
129
|
+
static parse_or_throw(str) {
|
|
130
|
+
const ip = IPAddress.parse(str);
|
|
131
|
+
if (ip === null) {
|
|
132
|
+
throw new Error(`invalid literal ip address: ${str}`);
|
|
133
|
+
}
|
|
134
|
+
return ip;
|
|
135
|
+
}
|
|
125
136
|
static split_at_slash(str) {
|
|
126
137
|
const slash = str.trim().split("/");
|
|
127
138
|
let addr = "";
|
|
@@ -659,9 +670,9 @@ export class IPAddress {
|
|
|
659
670
|
let prefix = 0;
|
|
660
671
|
let addr = nm.clone();
|
|
661
672
|
let in_host_part = true;
|
|
662
|
-
|
|
673
|
+
const two = Crunchy.two();
|
|
663
674
|
for (let _ = 0; _ < bits; _++) {
|
|
664
|
-
const bit = addr.
|
|
675
|
+
const bit = addr.mod(two).eq(Crunchy.one()) ? 1 : 0;
|
|
665
676
|
// console.log(">>>", bits, bit, addr, nm);
|
|
666
677
|
if (in_host_part && bit == 0) {
|
|
667
678
|
prefix = prefix + 1;
|
|
@@ -753,13 +764,13 @@ export class IPAddress {
|
|
|
753
764
|
return this.ip_bits.as_uncompressed_string(this.host_address);
|
|
754
765
|
}
|
|
755
766
|
to_s_mapped() {
|
|
756
|
-
if (this.is_mapped()) {
|
|
767
|
+
if (this.is_mapped() && this.mapped !== null) {
|
|
757
768
|
return `::ffff:${this.mapped.to_s()}`;
|
|
758
769
|
}
|
|
759
770
|
return this.to_s();
|
|
760
771
|
}
|
|
761
772
|
to_string_mapped() {
|
|
762
|
-
if (this.is_mapped()) {
|
|
773
|
+
if (this.is_mapped() && this.mapped !== null) {
|
|
763
774
|
const mapped = this.mapped.clone();
|
|
764
775
|
return `${this.to_s_mapped()}/${mapped.prefix.num}`;
|
|
765
776
|
}
|
|
@@ -856,7 +867,7 @@ export class IPAddress {
|
|
|
856
867
|
for (const ipstr of vec) {
|
|
857
868
|
const ipa = IPAddress.parse(ipstr);
|
|
858
869
|
if (!ipa) {
|
|
859
|
-
return
|
|
870
|
+
return [];
|
|
860
871
|
}
|
|
861
872
|
ret.push(ipa);
|
|
862
873
|
}
|
|
@@ -1122,7 +1133,11 @@ export class IPAddress {
|
|
|
1122
1133
|
if (subnets == 0 || 1 << this.prefix.host_prefix() <= subnets) {
|
|
1123
1134
|
return null;
|
|
1124
1135
|
}
|
|
1125
|
-
const
|
|
1136
|
+
const newprefix = this.newprefix(subnets);
|
|
1137
|
+
if (newprefix === null) {
|
|
1138
|
+
return null;
|
|
1139
|
+
}
|
|
1140
|
+
const networks = this.subnet(newprefix.num);
|
|
1126
1141
|
if (!networks) {
|
|
1127
1142
|
return networks;
|
|
1128
1143
|
}
|
package/dist/src/ipv4.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import Crunchy from "./crunchy.js";
|
|
2
2
|
import IPAddress from "./ipaddress.js";
|
|
3
3
|
export declare const Ipv4: {
|
|
4
|
-
from_number(addr: Crunchy, prefix_num: number): IPAddress;
|
|
5
|
-
create(str: string): IPAddress;
|
|
4
|
+
from_number(addr: Crunchy, prefix_num: number): IPAddress | null;
|
|
5
|
+
create(str: string): IPAddress | null;
|
|
6
6
|
ipv4_is_private(my: IPAddress): boolean;
|
|
7
7
|
ipv4_is_loopback(my: IPAddress): boolean;
|
|
8
8
|
to_ipv6(ia: IPAddress): IPAddress;
|
|
9
9
|
is_class_a(my: IPAddress): boolean;
|
|
10
10
|
is_class_b(my: IPAddress): boolean;
|
|
11
11
|
is_class_c(my: IPAddress): boolean;
|
|
12
|
-
parse_classful(ip_si: string): IPAddress;
|
|
12
|
+
parse_classful(ip_si: string): IPAddress | null;
|
|
13
13
|
};
|
|
14
14
|
export default Ipv4;
|
|
15
15
|
//# sourceMappingURL=ipv4.d.ts.map
|
package/dist/src/ipv4.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipv4.d.ts","sourceRoot":"","sources":["../../src/ipv4.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,SAAS,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"ipv4.d.ts","sourceRoot":"","sources":["../../src/ipv4.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAcvC,eAAO,MAAM,IAAI;sBACG,OAAO,cAAc,MAAM,GAAG,SAAS,GAAG,IAAI;gBAgBpD,MAAM,GAAG,SAAS,GAAG,IAAI;wBA0CjB,SAAS,GAAG,OAAO;yBAWlB,SAAS,GAAG,OAAO;gBAI5B,SAAS,GAAG,SAAS;mBA2BlB,SAAS,GAAG,OAAO;mBAgBnB,SAAS,GAAG,OAAO;mBAmBnB,SAAS,GAAG,OAAO;0BA8BZ,MAAM,GAAG,SAAS,GAAG,IAAI;CAkBhD,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/dist/src/ipv4.js
CHANGED
|
@@ -4,6 +4,13 @@ import IPAddress from "./ipaddress.js";
|
|
|
4
4
|
import IpBits from "./ip_bits.js";
|
|
5
5
|
import Prefix128 from "./prefix128.js";
|
|
6
6
|
import Ipv6 from "./ipv6.js";
|
|
7
|
+
function prefix32_or_throw(num) {
|
|
8
|
+
const ret = Prefix32.create(num);
|
|
9
|
+
if (ret === null) {
|
|
10
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
11
|
+
}
|
|
12
|
+
return ret;
|
|
13
|
+
}
|
|
7
14
|
export const Ipv4 = {
|
|
8
15
|
from_number(addr, prefix_num) {
|
|
9
16
|
const prefix = Prefix32.create(prefix_num);
|
|
@@ -63,20 +70,24 @@ export const Ipv4 = {
|
|
|
63
70
|
},
|
|
64
71
|
ipv4_is_private(my) {
|
|
65
72
|
return ([
|
|
66
|
-
IPAddress.
|
|
67
|
-
IPAddress.
|
|
68
|
-
IPAddress.
|
|
69
|
-
IPAddress.
|
|
73
|
+
IPAddress.parse_or_throw("10.0.0.0/8"),
|
|
74
|
+
IPAddress.parse_or_throw("169.254.0.0/16"),
|
|
75
|
+
IPAddress.parse_or_throw("172.16.0.0/12"),
|
|
76
|
+
IPAddress.parse_or_throw("192.168.0.0/16"),
|
|
70
77
|
].find((i) => i.includes(my)) != null);
|
|
71
78
|
},
|
|
72
79
|
ipv4_is_loopback(my) {
|
|
73
|
-
return IPAddress.
|
|
80
|
+
return IPAddress.parse_or_throw("127.0.0.0/8").includes(my);
|
|
74
81
|
},
|
|
75
82
|
to_ipv6(ia) {
|
|
83
|
+
const prefix = Prefix128.create(ia.prefix.num);
|
|
84
|
+
if (prefix === null) {
|
|
85
|
+
throw new Error(`invalid ipv6 prefix: ${ia.prefix.num}`);
|
|
86
|
+
}
|
|
76
87
|
return new IPAddress({
|
|
77
88
|
ip_bits: IpBits.v6(),
|
|
78
89
|
host_address: ia.host_address.clone(),
|
|
79
|
-
prefix
|
|
90
|
+
prefix,
|
|
80
91
|
mapped: null,
|
|
81
92
|
vt_is_private: Ipv6.ipv6_is_private,
|
|
82
93
|
vt_is_loopback: Ipv6.ipv6_is_loopback,
|
|
@@ -95,8 +106,8 @@ export const Ipv4 = {
|
|
|
95
106
|
// // => true
|
|
96
107
|
//
|
|
97
108
|
is_class_a(my) {
|
|
98
|
-
// console.log("is_class_a:", my.to_string(), Crunchy.
|
|
99
|
-
return my.is_ipv4() && my.host_address.lt(Crunchy.
|
|
109
|
+
// console.log("is_class_a:", my.to_string(), Crunchy.from_string_or_throw("80000000", 16), my.is_ipv4());
|
|
110
|
+
return my.is_ipv4() && my.host_address.lt(Crunchy.from_string_or_throw("80000000", 16));
|
|
100
111
|
},
|
|
101
112
|
// Checks whether the ip address belongs to a
|
|
102
113
|
// RFC 791 CLASS B network, no matter
|
|
@@ -111,8 +122,8 @@ export const Ipv4 = {
|
|
|
111
122
|
//
|
|
112
123
|
is_class_b(my) {
|
|
113
124
|
return (my.is_ipv4() &&
|
|
114
|
-
Crunchy.
|
|
115
|
-
my.host_address.lt(Crunchy.
|
|
125
|
+
Crunchy.from_string_or_throw("80000000", 16).lte(my.host_address) &&
|
|
126
|
+
my.host_address.lt(Crunchy.from_string_or_throw("c0000000", 16)));
|
|
116
127
|
},
|
|
117
128
|
// Checks whether the ip address belongs to a
|
|
118
129
|
// RFC 791 CLASS C network, no matter
|
|
@@ -127,8 +138,8 @@ export const Ipv4 = {
|
|
|
127
138
|
//
|
|
128
139
|
is_class_c(my) {
|
|
129
140
|
return (my.is_ipv4() &&
|
|
130
|
-
Crunchy.
|
|
131
|
-
my.host_address.lt(Crunchy.
|
|
141
|
+
Crunchy.from_string_or_throw("c0000000", 16).lte(my.host_address) &&
|
|
142
|
+
my.host_address.lt(Crunchy.from_string_or_throw("e0000000", 16)));
|
|
132
143
|
},
|
|
133
144
|
// Creates a new IPv4 address object by parsing the
|
|
134
145
|
// address in a classful way.
|
|
@@ -162,13 +173,13 @@ export const Ipv4 = {
|
|
|
162
173
|
}
|
|
163
174
|
const ip = o_ip;
|
|
164
175
|
if (Ipv4.is_class_a(ip)) {
|
|
165
|
-
ip.prefix =
|
|
176
|
+
ip.prefix = prefix32_or_throw(8);
|
|
166
177
|
}
|
|
167
178
|
else if (Ipv4.is_class_b(ip)) {
|
|
168
|
-
ip.prefix =
|
|
179
|
+
ip.prefix = prefix32_or_throw(16);
|
|
169
180
|
}
|
|
170
181
|
else if (Ipv4.is_class_c(ip)) {
|
|
171
|
-
ip.prefix =
|
|
182
|
+
ip.prefix = prefix32_or_throw(24);
|
|
172
183
|
}
|
|
173
184
|
return ip;
|
|
174
185
|
},
|
package/dist/src/ipv6.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import IPAddress from "./ipaddress.js";
|
|
2
2
|
import Crunchy from "./crunchy.js";
|
|
3
3
|
export declare const Ipv6: {
|
|
4
|
-
from_str(str: string, radix: number, prefix: number): IPAddress;
|
|
5
|
-
enhance_if_mapped(ip: IPAddress): IPAddress;
|
|
6
|
-
from_int(adr: Crunchy, prefix_num: number): IPAddress;
|
|
7
|
-
create(str: string): IPAddress;
|
|
4
|
+
from_str(str: string, radix: number, prefix: number): IPAddress | null;
|
|
5
|
+
enhance_if_mapped(ip: IPAddress): IPAddress | null;
|
|
6
|
+
from_int(adr: Crunchy, prefix_num: number): IPAddress | null;
|
|
7
|
+
create(str: string): IPAddress | null;
|
|
8
8
|
to_ipv6(ia: IPAddress): IPAddress;
|
|
9
9
|
ipv6_is_loopback(my: IPAddress): boolean;
|
|
10
10
|
ipv6_is_private(my: IPAddress): boolean;
|
package/dist/src/ipv6.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipv6.d.ts","sourceRoot":"","sources":["../../src/ipv6.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAGvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,eAAO,MAAM,IAAI;kBA0DD,MAAM,SAAS,MAAM,UAAU,MAAM,GAAG,SAAS;
|
|
1
|
+
{"version":3,"file":"ipv6.d.ts","sourceRoot":"","sources":["../../src/ipv6.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAGvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,eAAO,MAAM,IAAI;kBA0DD,MAAM,SAAS,MAAM,UAAU,MAAM,GAAG,SAAS,GAAG,IAAI;0BAQhD,SAAS,GAAG,SAAS,GAAG,IAAI;kBAoCpC,OAAO,cAAc,MAAM,GAAG,SAAS,GAAG,IAAI;gBAmChD,MAAM,GAAG,SAAS,GAAG,IAAI;gBA4CzB,SAAS,GAAG,SAAS;yBAIZ,SAAS,GAAG,OAAO;wBAKpB,SAAS,GAAG,OAAO;CAGxC,CAAC;AAEF,eAAe,IAAI,CAAC"}
|
package/dist/src/ipv6.js
CHANGED
|
@@ -184,7 +184,7 @@ export const Ipv6 = {
|
|
|
184
184
|
return my.host_address.eq(Crunchy.one());
|
|
185
185
|
},
|
|
186
186
|
ipv6_is_private(my) {
|
|
187
|
-
return IPAddress.
|
|
187
|
+
return IPAddress.parse_or_throw("fd00::/8").includes(my);
|
|
188
188
|
},
|
|
189
189
|
};
|
|
190
190
|
export default Ipv6;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipv6_loopback.d.ts","sourceRoot":"","sources":["../../src/ipv6_loopback.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAKvC,eAAO,MAAM,YAAY;cA4Cb,SAAS;
|
|
1
|
+
{"version":3,"file":"ipv6_loopback.d.ts","sourceRoot":"","sources":["../../src/ipv6_loopback.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAKvC,eAAO,MAAM,YAAY;cA4Cb,SAAS,GAAG,IAAI;CAG3B,CAAC;AAEF,eAAe,YAAY,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipv6_mapped.d.ts","sourceRoot":"","sources":["../../src/ipv6_mapped.ts"],"names":[],"mappings":"AAEA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;gBA6ET,MAAM,GAAG,SAAS;CA+
|
|
1
|
+
{"version":3,"file":"ipv6_mapped.d.ts","sourceRoot":"","sources":["../../src/ipv6_mapped.ts"],"names":[],"mappings":"AAEA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;gBA6ET,MAAM,GAAG,SAAS,GAAG,IAAI;CA+EtC,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipv6_unspec.d.ts","sourceRoot":"","sources":["../../src/ipv6_unspec.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;cAkDX,SAAS;
|
|
1
|
+
{"version":3,"file":"ipv6_unspec.d.ts","sourceRoot":"","sources":["../../src/ipv6_unspec.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,eAAO,MAAM,UAAU;cAkDX,SAAS,GAAG,IAAI;CAI3B,CAAC;AAEF,eAAe,UAAU,CAAC"}
|
package/dist/src/prefix128.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefix128.d.ts","sourceRoot":"","sources":["../../src/prefix128.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,SAAS;gBAaD,MAAM,GAAG,MAAM;
|
|
1
|
+
{"version":3,"file":"prefix128.d.ts","sourceRoot":"","sources":["../../src/prefix128.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,SAAS;gBAaD,MAAM,GAAG,MAAM,GAAG,IAAI;aAgBzB,MAAM,OAAO,MAAM,GAAG,MAAM;CAOtC,CAAC;AAEF,eAAe,SAAS,CAAC"}
|
package/dist/src/prefix128.js
CHANGED
|
@@ -29,7 +29,11 @@ const Prefix128 = {
|
|
|
29
29
|
return null;
|
|
30
30
|
},
|
|
31
31
|
from(my, num) {
|
|
32
|
-
|
|
32
|
+
const ret = Prefix128.create(num);
|
|
33
|
+
if (ret === null) {
|
|
34
|
+
throw new Error(`invalid ipv6 prefix: ${num}`);
|
|
35
|
+
}
|
|
36
|
+
return ret;
|
|
33
37
|
},
|
|
34
38
|
};
|
|
35
39
|
export default Prefix128;
|
package/dist/src/prefix32.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefix32.d.ts","sourceRoot":"","sources":["../../src/prefix32.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,QAAQ;aACH,MAAM,OAAO,MAAM,GAAG,MAAM;
|
|
1
|
+
{"version":3,"file":"prefix32.d.ts","sourceRoot":"","sources":["../../src/prefix32.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,QAAQ;aACH,MAAM,OAAO,MAAM,GAAG,MAAM;gBAQzB,MAAM,GAAG,MAAM,GAAG,IAAI;CAiBnC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
package/dist/src/prefix32.js
CHANGED
|
@@ -2,7 +2,11 @@ import Prefix from "./prefix.js";
|
|
|
2
2
|
import IpBits from "./ip_bits.js";
|
|
3
3
|
const Prefix32 = {
|
|
4
4
|
from(my, num) {
|
|
5
|
-
|
|
5
|
+
const ret = Prefix32.create(num);
|
|
6
|
+
if (ret === null) {
|
|
7
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
8
|
+
}
|
|
9
|
+
return ret;
|
|
6
10
|
},
|
|
7
11
|
create(num) {
|
|
8
12
|
if (num >= 0 && num <= 32) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test_crunchy_impls.d.ts","sourceRoot":"","sources":["../../test/test_crunchy_impls.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Exercises the shared CrunchyBase public API against both
|
|
2
|
+
// implementations, so they're guaranteed to behave identically for
|
|
3
|
+
// everything the rest of the codebase actually relies on. Internals
|
|
4
|
+
// specific to one implementation (e.g. CrunchyWith32Bit's 8/14-bit
|
|
5
|
+
// limb representation) are covered separately in
|
|
6
|
+
// test_crunchy_with32bit.ts.
|
|
7
|
+
import { CrunchyWithBigInt, CrunchyWith32Bit } from "../src/crunchy.js";
|
|
8
|
+
const impls = [
|
|
9
|
+
["CrunchyWithBigInt", CrunchyWithBigInt.bootstrap()],
|
|
10
|
+
["CrunchyWith32Bit", new CrunchyWith32Bit()],
|
|
11
|
+
];
|
|
12
|
+
describe.each(impls)("Crunchy impl: %s", (_name, c) => {
|
|
13
|
+
describe("#compare", () => {
|
|
14
|
+
it("Should confirm equality", () => {
|
|
15
|
+
const x = c.parse("12345678901234567890");
|
|
16
|
+
const y = c.parse("12345678901234567890");
|
|
17
|
+
assert.isTrue(x.eq(y));
|
|
18
|
+
});
|
|
19
|
+
it("Should find first is greater than second", () => {
|
|
20
|
+
const x = c.parse("12345678901234567890");
|
|
21
|
+
const y = c.parse("1234567890");
|
|
22
|
+
assert.isTrue(x.gt(y));
|
|
23
|
+
});
|
|
24
|
+
it("Should find first is less than second", () => {
|
|
25
|
+
const x = c.parse("1234567890");
|
|
26
|
+
const y = c.parse("12345678901234567890");
|
|
27
|
+
assert.isTrue(x.lt(y));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe("#add", () => {
|
|
31
|
+
it("Should add numbers", () => {
|
|
32
|
+
const x = c.parse("12345678911234567891");
|
|
33
|
+
const y = c.parse("12345678901234567890");
|
|
34
|
+
assert.equal(x.add(y).toString(), "24691357812469135781");
|
|
35
|
+
});
|
|
36
|
+
it("Should add numbers, first longer than second", () => {
|
|
37
|
+
const x = c.parse("12345678901234567890");
|
|
38
|
+
const y = c.parse("1234567890");
|
|
39
|
+
assert.equal(x.add(y).toString(), "12345678902469135780");
|
|
40
|
+
});
|
|
41
|
+
it("Should add numbers, second longer than first", () => {
|
|
42
|
+
const x = c.parse("1234567890");
|
|
43
|
+
const y = c.parse("12345678901234567890");
|
|
44
|
+
assert.equal(x.add(y).toString(), "12345678902469135780");
|
|
45
|
+
});
|
|
46
|
+
it("Should add zero to number", () => {
|
|
47
|
+
const x = c.parse("12345678901234567890");
|
|
48
|
+
const y = c.zero();
|
|
49
|
+
assert.equal(x.add(y).toString(), "12345678901234567890");
|
|
50
|
+
});
|
|
51
|
+
it("Should add two zeros", () => {
|
|
52
|
+
assert.equal(c.zero().add(c.zero()).toString(), "0");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe("#sub", () => {
|
|
56
|
+
it("Should subtract numbers", () => {
|
|
57
|
+
const x = c.parse("12345678901234567890");
|
|
58
|
+
const y = c.parse("1234567890");
|
|
59
|
+
assert.equal(x.sub(y).toString(), "12345678900000000000");
|
|
60
|
+
});
|
|
61
|
+
it("Should subtract two equal numbers expecting zero", () => {
|
|
62
|
+
const x = c.parse("20");
|
|
63
|
+
const y = c.parse("20");
|
|
64
|
+
assert.equal(x.sub(y).toString(), "0");
|
|
65
|
+
});
|
|
66
|
+
it("Should subtract zero", () => {
|
|
67
|
+
const x = c.parse("244137007161");
|
|
68
|
+
assert.equal(x.sub(c.zero()).toString(), "244137007161");
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe("#leftShift", () => {
|
|
72
|
+
it("Should left shift a number", () => {
|
|
73
|
+
const x = c.from_number(22 * 256 + 11);
|
|
74
|
+
assert.equal(x.shl(5).toString(), "180576");
|
|
75
|
+
});
|
|
76
|
+
it("Should left shift zero", () => {
|
|
77
|
+
assert.equal(c.zero().shl(80).toString(), "0");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe("#rightShift", () => {
|
|
81
|
+
it("Should right shift a number", () => {
|
|
82
|
+
const x = c.from_number(22 * 256 + 11);
|
|
83
|
+
assert.equal(x.shr(5).toString(), "176");
|
|
84
|
+
});
|
|
85
|
+
it("Should right shift a number out of existence", () => {
|
|
86
|
+
const x = c.from_number(22 * 256 + 11);
|
|
87
|
+
assert.equal(x.shr(20).toString(), "0");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe("#mod", () => {
|
|
91
|
+
it("Should calculate modulo", () => {
|
|
92
|
+
const x = c.parse("123456789012345");
|
|
93
|
+
const y = c.from_number(97);
|
|
94
|
+
assert.equal(x.mod(y).toString(), "12");
|
|
95
|
+
});
|
|
96
|
+
it("Should calculate modulo of number smaller than modulus", () => {
|
|
97
|
+
const x = c.from_number(97);
|
|
98
|
+
const y = c.parse("123456789012345");
|
|
99
|
+
assert.equal(x.mod(y).toString(), "97");
|
|
100
|
+
});
|
|
101
|
+
it("Should calculate modulo of modulus", () => {
|
|
102
|
+
const x = c.from_number(241);
|
|
103
|
+
const y = c.from_number(241);
|
|
104
|
+
assert.equal(x.mod(y).toString(), "0");
|
|
105
|
+
});
|
|
106
|
+
it("Should calculate modulo of zero", () => {
|
|
107
|
+
assert.equal(c.zero().mod(c.from_number(241)).toString(), "0");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe("#from_string", () => {
|
|
111
|
+
it("Should parse a decimal string", () => {
|
|
112
|
+
const x = c.from_string("340282366920938463463374607431768211455");
|
|
113
|
+
assert.isOk(x);
|
|
114
|
+
assert.equal(x.toString(), "340282366920938463463374607431768211455");
|
|
115
|
+
});
|
|
116
|
+
it("Should parse a hex string", () => {
|
|
117
|
+
const x = c.from_string("ff", 16);
|
|
118
|
+
assert.isOk(x);
|
|
119
|
+
assert.equal(x.toString(16), "ff");
|
|
120
|
+
});
|
|
121
|
+
it("Should reject invalid characters", () => {
|
|
122
|
+
assert.isNull(c.from_string("12a45"));
|
|
123
|
+
});
|
|
124
|
+
it("Should reject invalid hex digits", () => {
|
|
125
|
+
assert.isNull(c.from_string("fg", 16));
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
describe("#parse", () => {
|
|
129
|
+
it("Should throw on invalid input", () => {
|
|
130
|
+
assert.throws(() => c.parse("not-a-number"));
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe("#create", () => {
|
|
134
|
+
it("Should produce a zero value with no argument", () => {
|
|
135
|
+
assert.isTrue(c.create().eq(c.zero()));
|
|
136
|
+
});
|
|
137
|
+
it("Should act like from_number() given a number", () => {
|
|
138
|
+
assert.isTrue(c.create(42).eq(c.from_number(42)));
|
|
139
|
+
});
|
|
140
|
+
it("Should act like parse() given a string", () => {
|
|
141
|
+
assert.isTrue(c.create("12345678901234567890").eq(c.parse("12345678901234567890")));
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
describe("#toString", () => {
|
|
145
|
+
it("Should round-trip a large number through a non-decimal radix", () => {
|
|
146
|
+
const x = c.parse("340282366920938463463374607431768211455");
|
|
147
|
+
assert.equal(c.from_string(x.toString(16), 16).toString(), x.toString());
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test_crunchy_with32bit.d.ts","sourceRoot":"","sources":["../../test/test_crunchy_with32bit.ts"],"names":[],"mappings":""}
|