pmcf 1.37.1 → 1.38.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.
- package/package.json +1 -1
- package/src/utils.mjs +60 -48
- package/types/utils.d.mts +6 -1
package/package.json
CHANGED
package/src/utils.mjs
CHANGED
|
@@ -57,60 +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
|
+
let 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
|
|
85
|
-
const words = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
86
|
-
|
|
87
|
-
let i = 0;
|
|
88
|
-
for (const a of normalizeIPAddress(address).split(/\:/)) {
|
|
89
|
-
words[i++] = parseInt(a,16);
|
|
90
|
-
}
|
|
91
|
-
|
|
95
|
+
export function _encode(definition, address) {
|
|
92
96
|
let res = 0n;
|
|
93
|
-
let shift =
|
|
97
|
+
let shift = BigInt(definition.length);
|
|
94
98
|
|
|
95
|
-
for(const word of
|
|
96
|
-
|
|
99
|
+
for (const word of normalizeIPAddress(address)
|
|
100
|
+
.split(definition.separator)
|
|
101
|
+
.map(a => parseInt(a, definition.base))) {
|
|
102
|
+
shift -= BigInt(definition.segmentLength);
|
|
97
103
|
res += BigInt(word) << shift;
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
return res;
|
|
101
107
|
}
|
|
102
108
|
|
|
103
|
-
export function decodeIPv6(address, length
|
|
109
|
+
export function decodeIPv6(address, length) {
|
|
110
|
+
return _decode(ipv6, address, length);
|
|
111
|
+
}
|
|
104
112
|
|
|
105
|
-
|
|
106
|
-
|
|
113
|
+
export function encodeIPv6(address) {
|
|
114
|
+
return _encode(ipv6, address);
|
|
115
|
+
}
|
|
107
116
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
}
|
|
112
128
|
|
|
113
|
-
|
|
129
|
+
export function encodeIP(address) {
|
|
130
|
+
return _encode(isIPv4Address(address) ? ipv4 : ipv6, address);
|
|
114
131
|
}
|
|
115
132
|
|
|
116
133
|
export function normalizeCIDR(address) {
|
|
@@ -121,15 +138,10 @@ export function normalizeCIDR(address) {
|
|
|
121
138
|
prefixLength = 64;
|
|
122
139
|
} else {
|
|
123
140
|
if (prefixLength) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
} else {
|
|
129
|
-
let n = encodeIPv6(prefix);
|
|
130
|
-
n = n & (0xffffffffffffffffffffffffffffffffn << (128n - BigInt(prefixLength)));
|
|
131
|
-
prefix = decodeIPv6(n, prefixLength);
|
|
132
|
-
}
|
|
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);
|
|
133
145
|
} else {
|
|
134
146
|
return {};
|
|
135
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;
|