Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions net/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"exports": {
".": "./mod.ts",
"./get-available-port": "./get_available_port.ts",
"./unstable-ip": "./unstable_ip.ts",
"./get-network-address": "./unstable_get_network_address.ts",
"./unstable-get-network-address": "./unstable_get_network_address.ts"
}
Expand Down
82 changes: 82 additions & 0 deletions net/unstable_ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2018-2025 the Deno authors. MIT license.

/**
* Validates whether a given string is a valid IPv4 address.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param addr IPv4 address in a string format (e.g., "192.168.0.1").
* @returns A boolean indicating if the string is a valid IPv4 address.
*
* @example Check if the address is a IPv4
* ```ts
* import { isIPv4 } from "@std/net/unstable-ip"
* import { assert, assertFalse } from "@std/assert"
*
* const correctIp = "192.168.0.1"
* const incorrectIp = "192.168.0.256"
*
* assert(isIPv4(correctIp))
* assertFalse(isIPv4(incorrectIp))
* ```
*/
export function isIPv4(addr: string): boolean {
const octets = addr.split(".");

return octets.length === 4 && octets.every((octet) => {
const n = Number(octet);
return n >= 0 && n <= 255 && !isNaN(n);
});
}

/**
* Validates whether a given string is a IPv6 address.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param addr IPv6 address in a string format (e.g., "2001:db8::1").
* @returns A boolean indicating if the string is a valid IPv6 address.
*
* @example Check if the address is a IPv6
* ```ts
* import { isIPv6 } from "@std/net/unstable-ip"
* import { assert, assertFalse } from "@std/assert"
*
* const correctIp = "2001::db8:0:1"
* const incorrectIp = "2001::db8::1"
*
* assert(isIPv6(correctIp))
* assertFalse(isIPv6(incorrectIp))
* ```
*/
export function isIPv6(addr: string): boolean {
// more than one use of ::
if (addr.split("::").length > 2) return false;

const hextets = addr.split(":");

// x:x:x:x:x:x:d.d.d.d (https://www.rfc-editor.org/rfc/rfc4291#section-2.2)
// check if has ipv4 on
if (addr.includes(".")) {
// is just an ipv4
if (hextets.length === 1) return false;

const last = hextets.pop();
if (!last || !isIPv4(last)) return false;

// just to maintain the length to 8
hextets.push("");
}

// expand ::
while (hextets.length < 8) {
const idx = hextets.indexOf("");
if (idx === -1) break;
hextets.splice(idx, 0, "");
}

return hextets.length === 8 && hextets.every((hextet) => {
const n = hextet === "" ? 0 : parseInt(hextet, 16);
return n >= 0 && n <= 65535 && !isNaN(n);
});
}
48 changes: 48 additions & 0 deletions net/unstable_ip_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { isIPv4, isIPv6 } from "./unstable_ip.ts";
import { assertEquals } from "@std/assert";

Deno.test("isIPv4()", () => {
const list = [
{ addr: "192.148.122.255", expected: true },
{ addr: "0.128.11.33", expected: true },
{ addr: "0.0.0.0", expected: true },

{ addr: "192.148.122.256", expected: false },
{ addr: "0.128.11", expected: false },
{ addr: "17823366190", expected: false },
{ addr: "192.168.0.-40", expected: false },
{ addr: "1a.1a.1.1", expected: false },
{ addr: "a.b.c.d", expected: false },
{ addr: "192.168.1.2.3", expected: false },
];

for (const { addr, expected } of list) {
assertEquals(isIPv4(addr), expected);
}
});

Deno.test("isIPv6()", () => {
const list = [
{ addr: "2001:db8:3333:4444:5555:6666:7777:8888", expected: true },
{ addr: "2001:db8::1", expected: true },
{ addr: "2001::db8:0:1", expected: true },
{ addr: "::", expected: true },
{ addr: "::1", expected: true },
{ addr: "2003:3333:4444:5555:6666:7777:192.168.0.1", expected: true },
{ addr: "ab::cd:192.168.0.1", expected: true },
{ addr: "::192.168.0.1", expected: true },

{ addr: "2001:db8:3333:4444:5555:6666:7777:gggg", expected: false },
{ addr: "2003:3333:4444:5555:6666:7777:192.168.0.256", expected: false },
{ addr: "2001:db8:3333:4444:5555:6666:7777", expected: false },
{ addr: "2001:db8::4444::8888", expected: false },
{ addr: "2001:db8::4444:0:-30:8888", expected: false },
{ addr: "192.128.0.1", expected: false },
];

for (const { addr, expected } of list) {
assertEquals(isIPv6(addr), expected);
}
});
Loading