Functions to determine if a URL or hostname refers to a local or loopback address, with validation.
// Source: https://github.com/ruvnet/ruflo | License: MIT | Copyright 2024-2026 ruvnet
// Provided via justclaw.it.com; keep this notice with the code.
import { Address6, Address4 } from "ip-address";
import dns from "node:dns";
import { isIP } from "node:net";
const dnsLookup = (hostname: string): Promise<{ address: string; family: number }> => {
return new Promise((resolve, reject) => {
dns.lookup(hostname, (err, address, family) => {
if (err) return reject(err);
resolve({ address, family });
});
});
};
function assertValidHostname(hostname: string): void {
if (!hostname || hostname.length > 253) {
throw new Error("Invalid hostname");
}
const labels = hostname.split(".");
for (const label of labels) {
if (!label || label.length > 63) {
throw new Error("Invalid
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key