-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ChainSafe/cayman/init-implementaiton
feat: initial implementation
- Loading branch information
Showing
8 changed files
with
558 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -75,3 +75,5 @@ lib | |
|
||
# others | ||
package-lock.json | ||
|
||
benchmark_data |
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,90 @@ | ||
import { itBench } from "@dapplion/benchmark"; | ||
import * as ours from "../src/index.js"; | ||
import * as net from "node:net"; | ||
import * as isIpLib from "is-ip"; | ||
|
||
const ipv4s = [ | ||
"0.0.0.0", | ||
"192.168.0.1", | ||
"255.255.255.255", | ||
"0.0f.0.0", | ||
"hahahahah", | ||
"999.0.0.0", | ||
"0000.0000.0000.0000", | ||
]; | ||
|
||
const ipv6s = [ | ||
"1:2:3:4:5:6:7:8", | ||
"::1", | ||
"0000:0000:0000:0000:0000:0000:0000:0000", | ||
"0000:0000:0000:0000:0000:ffff:192.168.100.228", | ||
"2001:0dc5:72a3:0000:0000:802e:3370:73E4", | ||
"hahahahahahaha", | ||
"0000.0000:0000.0000", | ||
]; | ||
|
||
describe("isIPv4", async function () { | ||
this.timeout(5000); | ||
for (const { name, fn } of [ | ||
{ | ||
name: "ours.isIPv4", | ||
fn: ours.isIPv4, | ||
}, | ||
{ | ||
name: "net.isIPv4", | ||
fn: net.isIPv4, | ||
}, | ||
{ | ||
name: "isIpLib.isIPv4", | ||
fn: isIpLib.isIPv4, | ||
}, | ||
]) { | ||
for (const ipStr of ipv4s) { | ||
itBench(`${name}("${ipStr}")`, () => fn(ipStr)); | ||
} | ||
} | ||
}); | ||
|
||
describe("isIPv6", async function () { | ||
this.timeout(10000); | ||
for (const { name, fn } of [ | ||
{ | ||
name: "ours.isIPv6", | ||
fn: ours.isIPv6, | ||
}, | ||
{ | ||
name: "net.isIPv6", | ||
fn: net.isIPv6, | ||
}, | ||
{ | ||
name: "isIpLib.isIPv6", | ||
fn: isIpLib.isIPv6, | ||
}, | ||
]) { | ||
for (const ipStr of ipv6s) { | ||
itBench(`${name}("${ipStr}")`, () => fn(ipStr)); | ||
} | ||
} | ||
}); | ||
|
||
describe("isIP", async function () { | ||
this.timeout(10000); | ||
for (const { name, fn } of [ | ||
{ | ||
name: "ours.isIP", | ||
fn: ours.isIP, | ||
}, | ||
{ | ||
name: "net.isIP", | ||
fn: net.isIP, | ||
}, | ||
{ | ||
name: "isIpLib.isIP", | ||
fn: isIpLib.isIP, | ||
}, | ||
]) { | ||
for (const ipStr of [...ipv4s, ...ipv6s]) { | ||
itBench(`${name}("${ipStr}")`, () => fn(ipStr)); | ||
} | ||
} | ||
}); |
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 @@ | ||
export * from "./is-ip.js"; |
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,74 @@ | ||
import { Parser, err } from "./parser.js"; | ||
|
||
// See https://stackoverflow.com/questions/166132/maximum-length-of-the-textual-representation-of-an-ipv6-address | ||
const MAX_IPV6_LENGTH = 45; | ||
const MAX_IPV4_LENGTH = 15; | ||
|
||
const parser = new Parser(); | ||
|
||
/** Parse `input` into IPv4 bytes. */ | ||
export function parseIPv4(input: string): Uint8Array { | ||
if (input.length > MAX_IPV4_LENGTH) { | ||
throw err; | ||
} | ||
return parser.new(input).parseWith(() => parser.readIPv4Addr()); | ||
} | ||
|
||
/** Parse `input` into IPv6 bytes. */ | ||
export function parseIPv6(input: string): Uint8Array { | ||
if (input.length > MAX_IPV6_LENGTH) { | ||
throw err; | ||
} | ||
return parser.new(input).parseWith(() => parser.readIPv6Addr()); | ||
} | ||
|
||
/** Parse `input` into IPv4 or IPv6 bytes. */ | ||
export function parseIP(input: string): Uint8Array { | ||
if (input.length > MAX_IPV6_LENGTH) { | ||
throw err; | ||
} | ||
return parser.new(input).parseWith(() => parser.readIPAddr()); | ||
} | ||
|
||
/** Check if `input` is IPv4. */ | ||
export function isIPv4(input: string): boolean { | ||
try { | ||
parseIPv4(input); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** Check if `input` is IPv6. */ | ||
export function isIPv6(input: string): boolean { | ||
try { | ||
parseIPv6(input); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** Check if `input` is IPv4 or IPv6. */ | ||
export function isIP(input: string): boolean { | ||
try { | ||
parseIP(input); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @returns `6` if `input` is IPv6, `4` if `input` is IPv4, or `undefined` if `input` is neither. | ||
*/ | ||
export function ipVersion(input: string): 4 | 6 | undefined { | ||
if (isIPv4(input)) { | ||
return 4; | ||
} else if (isIPv6(input)) { | ||
return 6; | ||
} else { | ||
return undefined; | ||
} | ||
} |
Oops, something went wrong.