pmcf 1.37.2 → 1.38.1
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/package.json +1 -1
- package/src/utils.mjs +60 -45
- package/types/utils.d.mts +6 -1
package/package.json
CHANGED
package/src/utils.mjs
CHANGED
|
@@ -57,54 +57,77 @@ export function normalizeIPAddress(address) {
|
|
|
57
57
|
return parts.map(s => s.padStart(4, "0")).join(":");
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
const ipv4 = {
|
|
61
|
+
separator: ".",
|
|
62
|
+
length: 32,
|
|
63
|
+
segmentLength: 8,
|
|
64
|
+
segmentMask: 0xffn,
|
|
65
|
+
mask: 0xffffffffn,
|
|
66
|
+
base: 10,
|
|
67
|
+
pad: 0
|
|
68
|
+
};
|
|
69
|
+
const ipv6 = {
|
|
70
|
+
separator: ":",
|
|
71
|
+
length: 128,
|
|
72
|
+
segmentLength: 16,
|
|
73
|
+
segmentMask: 0xffffn,
|
|
74
|
+
mask: 0xffffffffffffffffffffffffffffffffn,
|
|
75
|
+
base: 16,
|
|
76
|
+
pad: 4
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function _decode(definition, address, length = definition.length) {
|
|
80
|
+
const words = [];
|
|
81
|
+
let shift = definition.length;
|
|
82
|
+
|
|
83
|
+
for (let i = 0; i < length / definition.segmentLength; i++) {
|
|
84
|
+
shift -= definition.segmentLength;
|
|
85
|
+
words.push(
|
|
86
|
+
((address >> BigInt(shift)) & definition.segmentMask)
|
|
87
|
+
.toString(definition.base)
|
|
88
|
+
.padStart(definition.pad, "0")
|
|
89
|
+
);
|
|
66
90
|
}
|
|
67
91
|
|
|
68
|
-
return (
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function decodeIPv4(address, length = 32) {
|
|
72
|
-
const octets = [
|
|
73
|
-
(address >> 24) & 0xff,
|
|
74
|
-
(address >> 16) & 0xff,
|
|
75
|
-
(address >> 8) & 0xff,
|
|
76
|
-
address & 0xff
|
|
77
|
-
];
|
|
78
|
-
|
|
79
|
-
octets.length = Math.ceil(length / 8);
|
|
80
|
-
|
|
81
|
-
return octets.join(".");
|
|
92
|
+
return words.join(definition.separator);
|
|
82
93
|
}
|
|
83
94
|
|
|
84
|
-
export function
|
|
95
|
+
export function _encode(definition, address) {
|
|
85
96
|
let res = 0n;
|
|
86
|
-
let shift =
|
|
97
|
+
let shift = BigInt(definition.length);
|
|
87
98
|
|
|
88
99
|
for (const word of normalizeIPAddress(address)
|
|
89
|
-
.split(
|
|
90
|
-
.map(a => parseInt(a,
|
|
91
|
-
shift -=
|
|
100
|
+
.split(definition.separator)
|
|
101
|
+
.map(a => parseInt(a, definition.base))) {
|
|
102
|
+
shift -= BigInt(definition.segmentLength);
|
|
92
103
|
res += BigInt(word) << shift;
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
return res;
|
|
96
107
|
}
|
|
97
108
|
|
|
98
|
-
export function decodeIPv6(address, length
|
|
99
|
-
|
|
100
|
-
|
|
109
|
+
export function decodeIPv6(address, length) {
|
|
110
|
+
return _decode(ipv6, address, length);
|
|
111
|
+
}
|
|
101
112
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
113
|
+
export function encodeIPv6(address) {
|
|
114
|
+
return _encode(ipv6, address);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function decodeIPv4(address, length) {
|
|
118
|
+
return _decode(ipv4, address, length);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function encodeIPv4(address) {
|
|
122
|
+
return _encode(ipv4, address);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function decodeIP(address, length) {
|
|
126
|
+
return _decode(isIPv4Address(address) ? ipv4 : ipv6, address, length);
|
|
127
|
+
}
|
|
106
128
|
|
|
107
|
-
|
|
129
|
+
export function encodeIP(address) {
|
|
130
|
+
return _encode(isIPv4Address(address) ? ipv4 : ipv6, address);
|
|
108
131
|
}
|
|
109
132
|
|
|
110
133
|
export function normalizeCIDR(address) {
|
|
@@ -115,18 +138,10 @@ export function normalizeCIDR(address) {
|
|
|
115
138
|
prefixLength = 64;
|
|
116
139
|
} else {
|
|
117
140
|
if (prefixLength) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
} else {
|
|
123
|
-
let n = encodeIPv6(prefix);
|
|
124
|
-
n =
|
|
125
|
-
n &
|
|
126
|
-
(0xffffffffffffffffffffffffffffffffn <<
|
|
127
|
-
(128n - BigInt(prefixLength)));
|
|
128
|
-
prefix = decodeIPv6(n, prefixLength);
|
|
129
|
-
}
|
|
141
|
+
const definition = isIPv4Address(prefix) ? ipv4 : ipv6;
|
|
142
|
+
let n = _encode(definition, prefix);
|
|
143
|
+
n = n & (definition.mask << BigInt(definition.length - prefixLength));
|
|
144
|
+
prefix = _decode(definition, n, prefixLength);
|
|
130
145
|
} else {
|
|
131
146
|
return {};
|
|
132
147
|
}
|
package/types/utils.d.mts
CHANGED
|
@@ -6,8 +6,13 @@ export function isIPv4Address(address: any): boolean;
|
|
|
6
6
|
export function isIPv6Address(address: any): boolean;
|
|
7
7
|
export function isLinkLocal(address: any): any;
|
|
8
8
|
export function normalizeIPAddress(address: any): any;
|
|
9
|
+
export function _encode(definition: any, address: any): bigint;
|
|
10
|
+
export function decodeIPv6(address: any, length: any): string;
|
|
9
11
|
export function encodeIPv6(address: any): bigint;
|
|
10
|
-
export function
|
|
12
|
+
export function decodeIPv4(address: any, length: any): string;
|
|
13
|
+
export function encodeIPv4(address: any): bigint;
|
|
14
|
+
export function decodeIP(address: any, length: any): string;
|
|
15
|
+
export function encodeIP(address: any): bigint;
|
|
11
16
|
export function normalizeCIDR(address: any): {
|
|
12
17
|
prefix?: undefined;
|
|
13
18
|
prefixLength?: undefined;
|