Skip to content

Commit

Permalink
refactor: simplify IP address string representation by removing mask …
Browse files Browse the repository at this point in the history
…details
  • Loading branch information
microshine committed Dec 23, 2024
1 parent e189cad commit 2aac251
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 4 additions & 8 deletions packages/x509/src/ip_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ export class IpConverter {

if (uint8.length === 8) {
const addrStr = Array.from(addrBytes).join(".");
const maskStr = Array.from(maskBytes).join(".");
return `${addrStr}/${prefixLen} (mask ${maskStr})`;
return `${addrStr}/${prefixLen}`;
} else {
const addrStr = this.formatIPv6(addrBytes);
return `${addrStr}/${prefixLen}`;
Expand All @@ -182,10 +181,8 @@ export class IpConverter {
}

public static fromString(text: string): ArrayBuffer {
const ipText = text.split(" (mask ")[0];

if (ipText.includes("/")) {
const [addr, prefix] = this.parseCIDR(ipText);
if (text.includes("/")) {
const [addr, prefix] = this.parseCIDR(text);
const maskBytes = new Uint8Array(addr.length);

let bitsLeft = prefix;
Expand All @@ -205,8 +202,7 @@ export class IpConverter {
return out.buffer;
}

// Parse single IP address
const bytes = this.isIPv4(ipText) ? this.parseIPv4(ipText) : this.parseIPv6(ipText);
const bytes = this.isIPv4(text) ? this.parseIPv4(text) : this.parseIPv6(text);
return new Uint8Array(bytes).buffer;
}
}
12 changes: 6 additions & 6 deletions packages/x509/test/ip_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("IpConverter", () => {
255,
0, // Mask
]).buffer;
assert.strictEqual(IpConverter.toString(input), "192.168.0.0/24 (mask 255.255.255.0)");
assert.strictEqual(IpConverter.toString(input), "192.168.0.0/24");
});

it("converts IPv4 with different netmask", () => {
Expand All @@ -41,7 +41,7 @@ describe("IpConverter", () => {
248,
0, // Mask
]).buffer;
assert.strictEqual(IpConverter.toString(input), "10.24.0.0/21 (mask 255.255.248.0)");
assert.strictEqual(IpConverter.toString(input), "10.24.0.0/21");
});

it("converts IPv6 with netmask", () => {
Expand Down Expand Up @@ -103,8 +103,8 @@ describe("IpConverter", () => {
assert.deepStrictEqual(Array.from(new Uint8Array(result)), Array.from(expected));
});

it("parses IPv4 with mask string notation", () => {
const result = IpConverter.fromString("192.168.0.0/24 (mask 255.255.255.0)");
it("parses IPv4 with CIDR notation", () => {
const result = IpConverter.fromString("192.168.0.0/24");
assert.deepStrictEqual(Array.from(new Uint8Array(result)), [
192,
168,
Expand All @@ -117,8 +117,8 @@ describe("IpConverter", () => {
]);
});

it("parses complex IPv4 with mask string notation", () => {
const result = IpConverter.fromString("10.24.0.0/21 (mask 255.255.248.0)");
it("parses IPv4 with non-standard CIDR", () => {
const result = IpConverter.fromString("10.24.0.0/21");
assert.deepStrictEqual(Array.from(new Uint8Array(result)), [
10,
24,
Expand Down

0 comments on commit 2aac251

Please sign in to comment.