pmcf 1.35.1 → 1.35.2

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils.mjs +27 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.35.1",
3
+ "version": "1.35.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/utils.mjs CHANGED
@@ -57,6 +57,30 @@ export function normalizeIPAddress(address) {
57
57
  return parts.map(s => s.padStart(4, "0")).join(":");
58
58
  }
59
59
 
60
+ function encodeIPv4(address) {
61
+ const octets = [0, 0, 0, 0];
62
+
63
+ let i = 0;
64
+ for (const a of address.split(/\./)) {
65
+ octets[i++] = parseInt(a);
66
+ }
67
+
68
+ return (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3];
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(".");
82
+ }
83
+
60
84
  export function normalizeCIDR(address) {
61
85
  let [prefix, prefixLength] = address.split(/\//);
62
86
 
@@ -66,8 +90,9 @@ export function normalizeCIDR(address) {
66
90
  } else {
67
91
  if (prefixLength) {
68
92
  if (isIPv4Address(prefix)) {
69
- const parts = prefix.split(/\./);
70
- prefix = parts.slice(0, prefixLength / 8).join(".");
93
+ let n = encodeIPv4(prefix);
94
+ n = n & (0xffffffff << (32 - prefixLength));
95
+ prefix = decodeIPv4(n, prefixLength);
71
96
  } else {
72
97
  prefix = normalizeIPAddress(prefix);
73
98
  const parts = prefix.split(/\:/);