Lazy load node:net for improved edge runtime compatibility

This commit is contained in:
2025-10-12 13:52:50 -04:00
parent 94a751d9b0
commit db96e8d933
7 changed files with 53 additions and 10 deletions
+15
View File
@@ -0,0 +1,15 @@
// src/whois/edge-mock.test.ts
import { describe, expect, it, vi } from "vitest";
vi.mock("net", () => {
throw new Error("node:net unavailable (edge)");
});
describe("edge runtime behavior", () => {
it("throws a clear error when WHOIS hits node:net on edge", async () => {
const { whoisQuery } = await import("../whois/client");
await expect(whoisQuery("whois.iana.org", "com")).rejects.toThrow(
/WHOIS client is only available in Node.js runtimes/,
);
});
});
+16 -3
View File
@@ -1,4 +1,3 @@
import { createConnection } from "node:net";
import { withTimeout } from "../lib/async";
import { DEFAULT_TIMEOUT_MS } from "../lib/constants";
import type { LookupOptions } from "../types";
@@ -29,14 +28,28 @@ export async function whoisQuery(
}
// Low-level WHOIS TCP client. Some registries require CRLF after the domain query.
function queryTcp(
async function queryTcp(
host: string,
port: number,
query: string,
options?: LookupOptions,
): Promise<string> {
let net: typeof import("net") | null;
try {
// biome-ignore lint/style/useNodejsImportProtocol: compatibility
net = await import("net");
} catch {
net = null;
}
if (!net?.createConnection) {
throw new Error(
"WHOIS client is only available in Node.js runtimes; try setting `rdapOnly: true`.",
);
}
return new Promise((resolve, reject) => {
const socket = createConnection({ host, port });
const socket = net.createConnection({ host, port });
let data = "";
let done = false;
const cleanup = () => {