node-rdap
Version:
Node RDAP client for Whois lookup in Node JS.
64 lines • 2.44 kB
JavaScript
import { autnumCache, dnsCache, ipv4Cache, ipv6Cache } from "./cache/index.js";
import { getTopLevelDomain, isFullyQualifiedDomainName, } from "./utils/domain.js";
const resolveRdapServerByDomain = async (domain) => {
const topLevelDomain = getTopLevelDomain(domain);
if (!topLevelDomain) {
throw new Error("Could not parse the top level domain.");
}
const rdapServer = await dnsCache.get(topLevelDomain);
if (!rdapServer) {
throw new Error("RDAP Server for the given top level domain could not be found.");
}
return rdapServer;
};
export const domain = async (fqdn) => {
const normalizedFqdn = fqdn.trim().toLowerCase();
if (!isFullyQualifiedDomainName(normalizedFqdn)) {
throw new Error("The given domain could not be validated.");
}
try {
const rdapServer = await resolveRdapServerByDomain(normalizedFqdn);
const requestUrl = `${rdapServer}/domain/${normalizedFqdn}`;
const response = await fetch(requestUrl, {
headers: {
accept: "application/json,application/rdap+json",
},
});
if (!response.ok) {
throw new Error(`RDAP server responded with status ${response.status}: ${response.statusText}`);
}
const data = (await response.json());
return data;
}
catch (error) {
throw new Error(`Failed to fetch RDAP domain data: ${error.message}`);
}
};
export const ip = async (ip) => {
//Todo: Validate IP String.
const isIpv6 = ip.includes(":");
const cache = isIpv6 ? ipv6Cache : ipv4Cache;
const rdapServer = await cache.get(ip);
if (!rdapServer) {
throw new Error("RDAP Server for the given IP Address could not be found.");
}
const requestUrl = `${rdapServer}/ip/${ip}`;
return (await (await fetch(requestUrl, {
headers: {
accept: "application/json,application/rdap+json",
},
})).json());
};
export const autnum = async (autnum) => {
const rdapServer = await autnumCache.get(autnum);
if (!rdapServer) {
throw new Error("RDAP Server for the given Autonomous System Number could not be found.");
}
const requestUrl = `${rdapServer}/autnum/${autnum}`;
return (await (await fetch(requestUrl, {
headers: {
accept: "application/json,application/rdap+json",
},
})).json());
};
//# sourceMappingURL=client.js.map