-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-kernel): ip address utils (#4195)
- Loading branch information
1 parent
9f9178d
commit a776999
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import "jest-extended"; | ||
|
||
import { isValidAddress, isIPv6Address, normalizeAddress } from "@packages/core-kernel/src/utils/ip-address"; | ||
|
||
describe("isValidAddress", () => { | ||
it("should return true for valid IPv6 address", () => { | ||
expect(isValidAddress("2001:3984:3989::104")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for localhost IPv6 address", () => { | ||
expect(isValidAddress("::1")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for :: IPv6 address", () => { | ||
expect(isValidAddress("::")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for valid IPv6 address in brackets", () => { | ||
expect(isValidAddress("[2001:3984:3989::104]")).toBeTrue(); | ||
}); | ||
|
||
it("should return false for invalid IPv6 address", () => { | ||
expect(isValidAddress("2001:3984:3989:104:1:2001:3984:3989:10")).toBeFalse(); // Too long address | ||
}); | ||
|
||
it("should return true for valid IPv4 address", () => { | ||
expect(isValidAddress("127.0.0.1")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for invalid IPv4 address", () => { | ||
expect(isValidAddress("127.0.0.300")).toBeFalse(); | ||
}); | ||
|
||
|
||
it("should return false for random string", () => { | ||
expect(isValidAddress("random")).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe("isIPv6Address", () => { | ||
it("should return true for valid IPv6 address", () => { | ||
expect(isIPv6Address("2001:3984:3989::104")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for localhost IPv6 address", () => { | ||
expect(isIPv6Address("::1")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for :: IPv6 address", () => { | ||
expect(isIPv6Address("::")).toBeTrue(); | ||
}); | ||
|
||
it("should return true for valid IPv6 address in brackets", () => { | ||
expect(isIPv6Address("[2001:3984:3989::104]")).toBeTrue(); | ||
}); | ||
|
||
it("should return false for invalid IPv6 address", () => { | ||
expect(isIPv6Address("2001:3984:3989:104:1:2001:3984:3989:10")).toBeFalse(); // Too long address | ||
}); | ||
|
||
it("should return false for valid IPv4 address", () => { | ||
expect(isIPv6Address("127.0.0.1")).toBeFalse(); | ||
}); | ||
|
||
it("should return false for random string", () => { | ||
expect(isIPv6Address("random")).toBeFalse(); | ||
}); | ||
}); | ||
|
||
describe("normalizeAddress", () => { | ||
it("should return normalized IPv6 address", () => { | ||
expect(normalizeAddress("2001:3984:3989::104")).toEqual("[2001:3984:3989::104]"); | ||
}); | ||
|
||
it("should return normalized localhost IPv6 address", () => { | ||
expect(normalizeAddress("::1")).toEqual("[::1]"); | ||
}); | ||
|
||
it("should return normalized :: IPv6 address", () => { | ||
expect(normalizeAddress("::")).toEqual("[::]"); | ||
}); | ||
|
||
it("should keep normalized IPv6 address in brackets", () => { | ||
expect(normalizeAddress("[2001:3984:3989::104]")).toEqual("[2001:3984:3989::104]"); | ||
}); | ||
|
||
it("should return same IPv4 address", () => { | ||
expect(normalizeAddress("127.0.0.1")).toEqual("127.0.0.1"); | ||
}); | ||
|
||
it("should return same random string", () => { | ||
expect(normalizeAddress("random")).toEqual("random"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import ipaddr from "ipaddr.js"; | ||
|
||
export const isValidAddress = (ip: string) => { | ||
return ipaddr.isValid(clean(ip)); | ||
}; | ||
|
||
export const isIPv6Address = (ip: string) => { | ||
try { | ||
return ipaddr.parse(clean(ip)).kind() === "ipv6"; | ||
} catch {} | ||
|
||
return false; | ||
}; | ||
|
||
export const normalizeAddress = (ip: string) => { | ||
ip = clean(ip); | ||
|
||
if (isIPv6Address(ip)) { | ||
return `[${ip}]`; | ||
} | ||
|
||
return ip; | ||
}; | ||
|
||
const clean = (ip: string) => { | ||
return ip.replace("[", "").replace("]", ""); | ||
}; |