mirror of
https://github.com/jakejarvis/rdapper.git
synced 2026-09-11 04:15:32 -04:00
Add query transformation for non-English WHOIS servers, specifically for whois.jprs.jp (fixes #15)
This commit is contained in:
@@ -13,3 +13,40 @@ describe("edge runtime behavior", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("query transformation for non-English WHOIS servers", () => {
|
||||
it("should append /e to queries for whois.jprs.jp", async () => {
|
||||
// Mock net module to capture the query being sent
|
||||
let capturedQuery = "";
|
||||
|
||||
vi.doMock("node:net", () => ({
|
||||
createConnection: () => {
|
||||
return {
|
||||
on: (event: string, callback: (arg?: unknown) => void) => {
|
||||
if (event === "connect") {
|
||||
// Capture the query that will be written
|
||||
setTimeout(() => callback(), 0);
|
||||
} else if (event === "end") {
|
||||
setTimeout(() => callback(), 10);
|
||||
}
|
||||
},
|
||||
write: (data: string) => {
|
||||
capturedQuery = data.replace(/\r\n$/, ""); // Strip CRLF
|
||||
},
|
||||
destroy: () => {},
|
||||
setTimeout: () => {},
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
const { whoisQuery } = await import("../whois/client");
|
||||
|
||||
try {
|
||||
await whoisQuery("whois.jprs.jp", "hairtect.jp");
|
||||
} catch {
|
||||
// Query may fail since we're mocking, but we just want to check the query format
|
||||
}
|
||||
|
||||
expect(capturedQuery).toBe("hairtect.jp/e");
|
||||
});
|
||||
});
|
||||
|
||||
+17
-1
@@ -7,6 +7,17 @@ export interface WhoisQueryResult {
|
||||
text: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some WHOIS servers default to non-English responses. This mapping allows automatic
|
||||
* query transformation to request English-only output for easier parsing.
|
||||
*
|
||||
* To add new servers: Add an entry with the hostname and transformation function:
|
||||
* "whois.example.org": (query) => `${query}/english`,
|
||||
*/
|
||||
const WHOIS_QUERY_TRANSFORMERS: Record<string, (query: string) => string> = {
|
||||
"whois.jprs.jp": (query) => `${query}/e`, // Append /e for English-only response
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform a WHOIS query against an RFC 3912 server over TCP 43.
|
||||
* Returns the raw text and the server used.
|
||||
@@ -19,8 +30,13 @@ export async function whoisQuery(
|
||||
const timeoutMs = options?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
||||
const port = 43;
|
||||
const host = server.replace(/^whois:\/\//i, "");
|
||||
|
||||
// Transform query if server requires special formatting
|
||||
const transformer = WHOIS_QUERY_TRANSFORMERS[host];
|
||||
const transformedQuery = transformer ? transformer(query) : query;
|
||||
|
||||
const text = await withTimeout(
|
||||
queryTcp(host, port, query, options),
|
||||
queryTcp(host, port, transformedQuery, options),
|
||||
timeoutMs,
|
||||
"WHOIS timeout",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user