@canonical/react-components 4.3.0 → 4.3.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/dist/components/PrefixedIpInput/PrefixedIpInput.js +1 -1
- package/dist/components/PrefixedIpInput/utils.d.ts +10 -1
- package/dist/components/PrefixedIpInput/utils.js +43 -11
- package/dist/esm/components/PrefixedIpInput/PrefixedIpInput.js +1 -1
- package/dist/esm/components/PrefixedIpInput/utils.d.ts +10 -1
- package/dist/esm/components/PrefixedIpInput/utils.js +38 -8
- package/package.json +1 -1
|
@@ -50,7 +50,7 @@ const PrefixedIpInput = _ref => {
|
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
return /*#__PURE__*/_react.default.createElement(_PrefixedInput.default, _extends({
|
|
53
|
-
help: help ? help : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", isIPV4 ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", "The available range in this subnet is", " ", /*#__PURE__*/_react.default.createElement("code", null, immutable, ".", editable, " ")) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", "The
|
|
53
|
+
help: help ? help : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", isIPV4 ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", "The available range in this subnet is", " ", /*#__PURE__*/_react.default.createElement("code", null, immutable, ".", editable, " ")) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ", "The subnet CIDR is ", /*#__PURE__*/_react.default.createElement("code", null, cidr)), "."),
|
|
54
54
|
immutableText: isIPV4 ? "".concat(immutable, ".") : immutable,
|
|
55
55
|
maxLength: maxLength,
|
|
56
56
|
name: name,
|
|
@@ -14,6 +14,7 @@ export declare const isIPv4: (ip: string) => boolean;
|
|
|
14
14
|
export declare const getIpRangeFromCidr: (cidr: string) => string[];
|
|
15
15
|
export declare const getFirstValidIp: (ip: string) => string;
|
|
16
16
|
export declare const convertIpToUint32: (ip: string) => number;
|
|
17
|
+
export declare const convertUint32ToIp: (ipAsUint32: number) => string;
|
|
17
18
|
/**
|
|
18
19
|
* Checks if an IPv4 address is valid for the given subnet.
|
|
19
20
|
*
|
|
@@ -30,10 +31,18 @@ export declare const isIpInSubnet: (ip: string, cidr: string) => boolean;
|
|
|
30
31
|
* @returns The immutable and editable octects as two strings in a list
|
|
31
32
|
*/
|
|
32
33
|
export declare const getImmutableAndEditableOctets: (startIp: string, endIp: string) => string[];
|
|
34
|
+
/**
|
|
35
|
+
* Separates the immutable and editable parts of an IPv6 subnet range.
|
|
36
|
+
* For simplcity, if the prefix is not on a group boundary, the entire last group is considered editable.
|
|
37
|
+
*
|
|
38
|
+
* @param cidr The CIDR notation of the subnet
|
|
39
|
+
* @returns The immutable and editable parts as two strings in a list
|
|
40
|
+
*/
|
|
41
|
+
export declare const getImmutableAndEditableIPv6: (cidr: string) => string[];
|
|
33
42
|
/**
|
|
34
43
|
* Get the immutable and editable parts of an IPv4 or IPv6 subnet.
|
|
35
44
|
*
|
|
36
45
|
* @param cidr The CIDR notation of the subnet
|
|
37
|
-
* @returns The immutable and editable
|
|
46
|
+
* @returns The immutable and editable as two strings in a list
|
|
38
47
|
*/
|
|
39
48
|
export declare const getImmutableAndEditable: (cidr: string) => string[];
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isIpInSubnet = exports.isIPv4 = exports.getIpRangeFromCidr = exports.getImmutableAndEditableOctets = exports.getImmutableAndEditable = exports.getFirstValidIp = exports.convertIpToUint32 = void 0;
|
|
6
|
+
exports.isIpInSubnet = exports.isIPv4 = exports.getIpRangeFromCidr = exports.getImmutableAndEditableOctets = exports.getImmutableAndEditableIPv6 = exports.getImmutableAndEditable = exports.getFirstValidIp = exports.convertUint32ToIp = exports.convertIpToUint32 = void 0;
|
|
7
7
|
/**
|
|
8
8
|
* Checks if a given IP address is a valid IPv4 address.
|
|
9
9
|
* @param ip The IP address to check
|
|
@@ -26,8 +26,11 @@ const getIpRangeFromCidr = cidr => {
|
|
|
26
26
|
// https://gist.github.com/binarymax/6114792
|
|
27
27
|
|
|
28
28
|
// Get start IP and number of valid addresses
|
|
29
|
-
const [
|
|
30
|
-
const
|
|
29
|
+
const [unmaskedStartIp, mask] = cidr.split("/");
|
|
30
|
+
const maskBits = parseInt(mask, 10);
|
|
31
|
+
const subnetMask = maskBits === 0 ? 0 : 0xffffffff << 32 - maskBits >>> 0;
|
|
32
|
+
const startIp = convertUint32ToIp(convertIpToUint32(unmaskedStartIp) & subnetMask);
|
|
33
|
+
const numberOfAddresses = (1 << 32 - maskBits) - 1;
|
|
31
34
|
|
|
32
35
|
// IPv4 can be represented by an unsigned 32-bit integer, so we can use a Uint32Array to store the IP
|
|
33
36
|
const buffer = new ArrayBuffer(4); //4 octets
|
|
@@ -63,6 +66,14 @@ const convertIpToUint32 = ip => {
|
|
|
63
66
|
int32[0] = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3];
|
|
64
67
|
return int32[0];
|
|
65
68
|
};
|
|
69
|
+
exports.convertIpToUint32 = convertIpToUint32;
|
|
70
|
+
const convertUint32ToIp = ipAsUint32 => {
|
|
71
|
+
const buffer = new ArrayBuffer(4); //4 octets
|
|
72
|
+
const int32 = new Uint32Array(buffer);
|
|
73
|
+
int32[0] = ipAsUint32;
|
|
74
|
+
const octets = Array.from(new Uint8Array(buffer));
|
|
75
|
+
return octets.reverse().join(".");
|
|
76
|
+
};
|
|
66
77
|
|
|
67
78
|
/**
|
|
68
79
|
* Checks if an IPv4 address is valid for the given subnet.
|
|
@@ -71,7 +82,7 @@ const convertIpToUint32 = ip => {
|
|
|
71
82
|
* @param cidr The subnet's CIDR notation e.g. 192.168.0.0/24
|
|
72
83
|
* @returns True if the IP is in the subnet, false otherwise
|
|
73
84
|
*/
|
|
74
|
-
exports.
|
|
85
|
+
exports.convertUint32ToIp = convertUint32ToIp;
|
|
75
86
|
const isIpInSubnet = (ip, cidr) => {
|
|
76
87
|
const [startIP, endIP] = getIpRangeFromCidr(cidr);
|
|
77
88
|
const ipUint32 = convertIpToUint32(ip);
|
|
@@ -104,22 +115,43 @@ const getImmutableAndEditableOctets = (startIp, endIp) => {
|
|
|
104
115
|
};
|
|
105
116
|
|
|
106
117
|
/**
|
|
107
|
-
*
|
|
118
|
+
* Separates the immutable and editable parts of an IPv6 subnet range.
|
|
119
|
+
* For simplcity, if the prefix is not on a group boundary, the entire last group is considered editable.
|
|
108
120
|
*
|
|
109
121
|
* @param cidr The CIDR notation of the subnet
|
|
110
|
-
* @returns The immutable and editable
|
|
122
|
+
* @returns The immutable and editable parts as two strings in a list
|
|
111
123
|
*/
|
|
112
124
|
exports.getImmutableAndEditableOctets = getImmutableAndEditableOctets;
|
|
125
|
+
const getImmutableAndEditableIPv6 = cidr => {
|
|
126
|
+
const [address, prefix] = cidr.split("/");
|
|
127
|
+
const prefixLength = parseInt(prefix, 10);
|
|
128
|
+
const [left = "", right = ""] = address.split("::");
|
|
129
|
+
const leftGroups = left ? left.split(":").filter(Boolean) : [];
|
|
130
|
+
const rightGroups = right ? right.split(":").filter(Boolean) : [];
|
|
131
|
+
const missingGroups = Math.max(0, 8 - (leftGroups.length + rightGroups.length));
|
|
132
|
+
const expandedGroups = [...leftGroups, ...Array(missingGroups).fill("0"), ...Array(rightGroups.length).fill("0")];
|
|
133
|
+
const immutableGroupCount = Math.floor(prefixLength / 16);
|
|
134
|
+
let immutableIPV6 = immutableGroupCount > 0 ? "".concat(expandedGroups.slice(0, immutableGroupCount).join(":")) : "";
|
|
135
|
+
if (immutableGroupCount < 8) {
|
|
136
|
+
immutableIPV6 += immutableIPV6 ? ":" : "";
|
|
137
|
+
}
|
|
138
|
+
const editableIPV6 = "".concat(expandedGroups.slice(immutableGroupCount).join(":"));
|
|
139
|
+
return [immutableIPV6, editableIPV6];
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Get the immutable and editable parts of an IPv4 or IPv6 subnet.
|
|
144
|
+
*
|
|
145
|
+
* @param cidr The CIDR notation of the subnet
|
|
146
|
+
* @returns The immutable and editable as two strings in a list
|
|
147
|
+
*/
|
|
148
|
+
exports.getImmutableAndEditableIPv6 = getImmutableAndEditableIPv6;
|
|
113
149
|
const getImmutableAndEditable = cidr => {
|
|
114
150
|
const isIPV4 = isIPv4(cidr.split("/")[0]);
|
|
115
151
|
if (isIPV4) {
|
|
116
152
|
const [startIp, endIp] = getIpRangeFromCidr(cidr);
|
|
117
153
|
return getImmutableAndEditableOctets(startIp, endIp);
|
|
118
154
|
}
|
|
119
|
-
|
|
120
|
-
const immutableIPV6 = networkAddress.substring(0, networkAddress.lastIndexOf(":"));
|
|
121
|
-
const ipv6PlaceholderColons = 7 - (immutableIPV6.match(/:/g) || []).length; // 7 is the maximum number of colons in an IPv6 address
|
|
122
|
-
const editableIPV6 = "".concat("0000:".repeat(ipv6PlaceholderColons), "0000");
|
|
123
|
-
return [immutableIPV6, editableIPV6];
|
|
155
|
+
return getImmutableAndEditableIPv6(cidr);
|
|
124
156
|
};
|
|
125
157
|
exports.getImmutableAndEditable = getImmutableAndEditable;
|
|
@@ -43,7 +43,7 @@ var PrefixedIpInput = _ref => {
|
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
45
|
return /*#__PURE__*/React.createElement(PrefixedInput, _extends({
|
|
46
|
-
help: help ? help : /*#__PURE__*/React.createElement(React.Fragment, null, " ", isIPV4 ? /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The available range in this subnet is", " ", /*#__PURE__*/React.createElement("code", null, immutable, ".", editable, " ")) : /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The
|
|
46
|
+
help: help ? help : /*#__PURE__*/React.createElement(React.Fragment, null, " ", isIPV4 ? /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The available range in this subnet is", " ", /*#__PURE__*/React.createElement("code", null, immutable, ".", editable, " ")) : /*#__PURE__*/React.createElement(React.Fragment, null, " ", "The subnet CIDR is ", /*#__PURE__*/React.createElement("code", null, cidr)), "."),
|
|
47
47
|
immutableText: isIPV4 ? "".concat(immutable, ".") : immutable,
|
|
48
48
|
maxLength: maxLength,
|
|
49
49
|
name: name,
|
|
@@ -14,6 +14,7 @@ export declare const isIPv4: (ip: string) => boolean;
|
|
|
14
14
|
export declare const getIpRangeFromCidr: (cidr: string) => string[];
|
|
15
15
|
export declare const getFirstValidIp: (ip: string) => string;
|
|
16
16
|
export declare const convertIpToUint32: (ip: string) => number;
|
|
17
|
+
export declare const convertUint32ToIp: (ipAsUint32: number) => string;
|
|
17
18
|
/**
|
|
18
19
|
* Checks if an IPv4 address is valid for the given subnet.
|
|
19
20
|
*
|
|
@@ -30,10 +31,18 @@ export declare const isIpInSubnet: (ip: string, cidr: string) => boolean;
|
|
|
30
31
|
* @returns The immutable and editable octects as two strings in a list
|
|
31
32
|
*/
|
|
32
33
|
export declare const getImmutableAndEditableOctets: (startIp: string, endIp: string) => string[];
|
|
34
|
+
/**
|
|
35
|
+
* Separates the immutable and editable parts of an IPv6 subnet range.
|
|
36
|
+
* For simplcity, if the prefix is not on a group boundary, the entire last group is considered editable.
|
|
37
|
+
*
|
|
38
|
+
* @param cidr The CIDR notation of the subnet
|
|
39
|
+
* @returns The immutable and editable parts as two strings in a list
|
|
40
|
+
*/
|
|
41
|
+
export declare const getImmutableAndEditableIPv6: (cidr: string) => string[];
|
|
33
42
|
/**
|
|
34
43
|
* Get the immutable and editable parts of an IPv4 or IPv6 subnet.
|
|
35
44
|
*
|
|
36
45
|
* @param cidr The CIDR notation of the subnet
|
|
37
|
-
* @returns The immutable and editable
|
|
46
|
+
* @returns The immutable and editable as two strings in a list
|
|
38
47
|
*/
|
|
39
48
|
export declare const getImmutableAndEditable: (cidr: string) => string[];
|
|
@@ -19,8 +19,11 @@ export var getIpRangeFromCidr = cidr => {
|
|
|
19
19
|
// https://gist.github.com/binarymax/6114792
|
|
20
20
|
|
|
21
21
|
// Get start IP and number of valid addresses
|
|
22
|
-
var [
|
|
23
|
-
var
|
|
22
|
+
var [unmaskedStartIp, mask] = cidr.split("/");
|
|
23
|
+
var maskBits = parseInt(mask, 10);
|
|
24
|
+
var subnetMask = maskBits === 0 ? 0 : 0xffffffff << 32 - maskBits >>> 0;
|
|
25
|
+
var startIp = convertUint32ToIp(convertIpToUint32(unmaskedStartIp) & subnetMask);
|
|
26
|
+
var numberOfAddresses = (1 << 32 - maskBits) - 1;
|
|
24
27
|
|
|
25
28
|
// IPv4 can be represented by an unsigned 32-bit integer, so we can use a Uint32Array to store the IP
|
|
26
29
|
var buffer = new ArrayBuffer(4); //4 octets
|
|
@@ -54,6 +57,13 @@ export var convertIpToUint32 = ip => {
|
|
|
54
57
|
int32[0] = (octets[0] << 24) + (octets[1] << 16) + (octets[2] << 8) + octets[3];
|
|
55
58
|
return int32[0];
|
|
56
59
|
};
|
|
60
|
+
export var convertUint32ToIp = ipAsUint32 => {
|
|
61
|
+
var buffer = new ArrayBuffer(4); //4 octets
|
|
62
|
+
var int32 = new Uint32Array(buffer);
|
|
63
|
+
int32[0] = ipAsUint32;
|
|
64
|
+
var octets = Array.from(new Uint8Array(buffer));
|
|
65
|
+
return octets.reverse().join(".");
|
|
66
|
+
};
|
|
57
67
|
|
|
58
68
|
/**
|
|
59
69
|
* Checks if an IPv4 address is valid for the given subnet.
|
|
@@ -92,11 +102,35 @@ export var getImmutableAndEditableOctets = (startIp, endIp) => {
|
|
|
92
102
|
return [immutable.join("."), editable.join(".")];
|
|
93
103
|
};
|
|
94
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Separates the immutable and editable parts of an IPv6 subnet range.
|
|
107
|
+
* For simplcity, if the prefix is not on a group boundary, the entire last group is considered editable.
|
|
108
|
+
*
|
|
109
|
+
* @param cidr The CIDR notation of the subnet
|
|
110
|
+
* @returns The immutable and editable parts as two strings in a list
|
|
111
|
+
*/
|
|
112
|
+
export var getImmutableAndEditableIPv6 = cidr => {
|
|
113
|
+
var [address, prefix] = cidr.split("/");
|
|
114
|
+
var prefixLength = parseInt(prefix, 10);
|
|
115
|
+
var [left = "", right = ""] = address.split("::");
|
|
116
|
+
var leftGroups = left ? left.split(":").filter(Boolean) : [];
|
|
117
|
+
var rightGroups = right ? right.split(":").filter(Boolean) : [];
|
|
118
|
+
var missingGroups = Math.max(0, 8 - (leftGroups.length + rightGroups.length));
|
|
119
|
+
var expandedGroups = [...leftGroups, ...Array(missingGroups).fill("0"), ...Array(rightGroups.length).fill("0")];
|
|
120
|
+
var immutableGroupCount = Math.floor(prefixLength / 16);
|
|
121
|
+
var immutableIPV6 = immutableGroupCount > 0 ? "".concat(expandedGroups.slice(0, immutableGroupCount).join(":")) : "";
|
|
122
|
+
if (immutableGroupCount < 8) {
|
|
123
|
+
immutableIPV6 += immutableIPV6 ? ":" : "";
|
|
124
|
+
}
|
|
125
|
+
var editableIPV6 = "".concat(expandedGroups.slice(immutableGroupCount).join(":"));
|
|
126
|
+
return [immutableIPV6, editableIPV6];
|
|
127
|
+
};
|
|
128
|
+
|
|
95
129
|
/**
|
|
96
130
|
* Get the immutable and editable parts of an IPv4 or IPv6 subnet.
|
|
97
131
|
*
|
|
98
132
|
* @param cidr The CIDR notation of the subnet
|
|
99
|
-
* @returns The immutable and editable
|
|
133
|
+
* @returns The immutable and editable as two strings in a list
|
|
100
134
|
*/
|
|
101
135
|
export var getImmutableAndEditable = cidr => {
|
|
102
136
|
var isIPV4 = isIPv4(cidr.split("/")[0]);
|
|
@@ -104,9 +138,5 @@ export var getImmutableAndEditable = cidr => {
|
|
|
104
138
|
var [startIp, endIp] = getIpRangeFromCidr(cidr);
|
|
105
139
|
return getImmutableAndEditableOctets(startIp, endIp);
|
|
106
140
|
}
|
|
107
|
-
|
|
108
|
-
var immutableIPV6 = networkAddress.substring(0, networkAddress.lastIndexOf(":"));
|
|
109
|
-
var ipv6PlaceholderColons = 7 - (immutableIPV6.match(/:/g) || []).length; // 7 is the maximum number of colons in an IPv6 address
|
|
110
|
-
var editableIPV6 = "".concat("0000:".repeat(ipv6PlaceholderColons), "0000");
|
|
111
|
-
return [immutableIPV6, editableIPV6];
|
|
141
|
+
return getImmutableAndEditableIPv6(cidr);
|
|
112
142
|
};
|