Rename lookupDomain() to lookup() (with backwards compatibility)

This commit is contained in:
2025-10-20 15:05:59 -04:00
parent 5cbefe6a14
commit b3adbf8307
6 changed files with 79 additions and 58 deletions
+16 -7
View File
@@ -20,7 +20,7 @@ import {
* High-level lookup that prefers RDAP and falls back to WHOIS.
* Ensures a standardized DomainRecord, independent of the source.
*/
export async function lookupDomain(
export async function lookup(
domain: string,
opts?: LookupOptions,
): Promise<LookupResult> {
@@ -118,28 +118,37 @@ export async function lookupDomain(
}
}
/** Determine if a domain appears available (not registered).
* Performs a lookup and resolves to a boolean. Rejects on lookup error. */
/**
* Determine if a domain appears available (not registered).
* Performs a lookup and resolves to a boolean. Rejects on lookup error.
*/
export async function isAvailable(
domain: string,
opts?: LookupOptions,
): Promise<boolean> {
const res = await lookupDomain(domain, opts);
const res = await lookup(domain, opts);
if (!res.ok || !res.record) throw new Error(res.error || "Lookup failed");
return res.record.isRegistered === false;
}
/** Determine if a domain appears registered.
* Performs a lookup and resolves to a boolean. Rejects on lookup error. */
/**
* Determine if a domain appears registered.
* Performs a lookup and resolves to a boolean. Rejects on lookup error.
*/
export async function isRegistered(
domain: string,
opts?: LookupOptions,
): Promise<boolean> {
const res = await lookupDomain(domain, opts);
const res = await lookup(domain, opts);
if (!res.ok || !res.record) throw new Error(res.error || "Lookup failed");
return res.record.isRegistered === true;
}
/**
* @deprecated Use `lookup` instead.
*/
export const lookupDomain = lookup;
export {
getDomainParts,
getDomainTld,
+8 -27
View File
@@ -3,7 +3,7 @@ import { parse } from "tldts";
type ParseOptions = Parameters<typeof parse>[1];
/**
* Parse a domain into its parts. Accepts options which are passed to tldts.parse().
* Parse a domain into its parts. Passes options to `tldts.parse()`.
* @see https://github.com/remusao/tldts/blob/master/packages/tldts-core/src/options.ts
*/
export function getDomainParts(
@@ -13,7 +13,10 @@ export function getDomainParts(
return parse(domain, { ...opts });
}
/** Get the TLD (ICANN-only public suffix) of a domain. */
/**
* Get the TLD (ICANN-only public suffix) of a domain. Passes options to `tldts.parse()`.
* @see https://github.com/remusao/tldts/blob/master/packages/tldts-core/src/options.ts
*/
export function getDomainTld(
domain: string,
opts?: ParseOptions,
@@ -47,7 +50,9 @@ export function punyToUnicode(domain: string): string {
/**
* Normalize arbitrary input (domain or URL) to its registrable domain (eTLD+1).
* Returns null when the input is not a valid ICANN domain (e.g., invalid TLD, IPs).
* Passes options to `tldts.parse()`.
* Returns null when the input is not a valid ICANN domain (e.g., invalid TLD, IPs)
* @see https://github.com/remusao/tldts/blob/master/packages/tldts-core/src/options.ts
*/
export function toRegistrableDomain(
input: string,
@@ -69,27 +74,3 @@ export function toRegistrableDomain(
if (domain === "") return null;
return domain.toLowerCase();
}
// Common WHOIS availability phrases seen across registries/registrars
const WHOIS_AVAILABLE_PATTERNS: RegExp[] = [
/\bno match\b/i,
/\bnot found\b/i,
/\bno entries found\b/i,
/\bno data found\b/i,
/\bavailable for registration\b/i,
/\bdomain\s+available\b/i,
/\bdomain status[:\s]+available\b/i,
/\bobject does not exist\b/i,
/\bthe queried object does not exist\b/i,
// Common variants across ccTLDs/registrars
/\bstatus:\s*free\b/i,
/\bstatus:\s*available\b/i,
/\bno object found\b/i,
/\bnicht gefunden\b/i,
/\bpending release\b/i, // often signals not registered/being deleted
];
export function isWhoisAvailable(text: string | undefined): boolean {
if (!text) return false;
return WHOIS_AVAILABLE_PATTERNS.some((re) => re.test(text));
}
+28 -2
View File
@@ -1,5 +1,4 @@
import { toISO } from "../lib/dates";
import { isWhoisAvailable } from "../lib/domain";
import { isPrivacyName } from "../lib/privacy";
import { parseKeyValueLines, uniq } from "../lib/text";
import type {
@@ -9,6 +8,33 @@ import type {
RegistrarInfo,
} from "../types";
// Common WHOIS availability phrases seen across registries/registrars
const WHOIS_AVAILABLE_PATTERNS: RegExp[] = [
/\bno match\b/i,
/\bnot found\b/i,
/\bno entries found\b/i,
/\bno data found\b/i,
/\bavailable for registration\b/i,
/\bdomain\s+available\b/i,
/\bdomain status[:\s]+available\b/i,
/\bobject does not exist\b/i,
/\bthe queried object does not exist\b/i,
// Common variants across ccTLDs/registrars
/\bstatus:\s*free\b/i,
/\bstatus:\s*available\b/i,
/\bno object found\b/i,
/\bnicht gefunden\b/i,
/\bpending release\b/i, // often signals not registered/being deleted
];
/**
* Best-effort heuristic to determine if a WHOIS response indicates the domain is available.
*/
export function isAvailableByWhois(text: string | undefined): boolean {
if (!text) return false;
return WHOIS_AVAILABLE_PATTERNS.some((re) => re.test(text));
}
/**
* Convert raw WHOIS text into our normalized DomainRecord.
* Heuristics cover many gTLD and ccTLD formats; exact fields vary per registry.
@@ -163,7 +189,7 @@ export function normalizeWhois(
const record: DomainRecord = {
domain,
tld,
isRegistered: !isWhoisAvailable(whoisText),
isRegistered: !isAvailableByWhois(whoisText),
isIDN: /(^|\.)xn--/i.test(domain),
unicodeName: undefined,
punycodeName: undefined,
+5 -5
View File
@@ -1,8 +1,8 @@
import { isWhoisAvailable } from "../lib/domain";
import type { LookupOptions } from "../types";
import type { WhoisQueryResult } from "./client";
import { whoisQuery } from "./client";
import { extractWhoisReferral } from "./discovery";
import { isAvailableByWhois } from "./normalize";
/**
* Follow registrar WHOIS referrals up to a configured hop limit.
@@ -30,8 +30,8 @@ export async function followWhoisReferrals(
try {
const res = await whoisQuery(next, domain, opts);
// Prefer authoritative TLD response when registrar contradicts availability
const registeredBefore = !isWhoisAvailable(current.text);
const registeredAfter = !isWhoisAvailable(res.text);
const registeredBefore = !isAvailableByWhois(current.text);
const registeredAfter = !isAvailableByWhois(res.text);
if (registeredBefore && !registeredAfter) {
// Registrar claims availability but TLD shows registered: keep TLD
break;
@@ -74,8 +74,8 @@ export async function collectWhoisReferralChain(
try {
const res = await whoisQuery(next, domain, opts);
// If registrar claims availability while TLD indicated registered, stop.
const registeredBefore = !isWhoisAvailable(current.text);
const registeredAfter = !isWhoisAvailable(res.text);
const registeredBefore = !isAvailableByWhois(current.text);
const registeredAfter = !isAvailableByWhois(res.text);
if (registeredBefore && !registeredAfter) {
// Do not adopt or append contradictory registrar; keep authoritative TLD only.
break;