From a1d84c2c1f2f14de70c61fba43abf61c7a50703b Mon Sep 17 00:00:00 2001 From: Dima Pulkinen Date: Tue, 6 Oct 2026 12:26:13 +0300 Subject: [PATCH] add encoding option --- README.md | 3 +++ src/types.ts | 3 ++- src/whoiser.ts | 14 ++++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 37e9a32db..1b58bc116 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Get WHOIS info for domains. - `options` - Object of options to use, all optional: - `host` - WHOIS server to query. Default: WHOIS server from IANA - `timeout` - WHOIS server request timeout in ms. Default: 1500 + - `encoding` - Encoding used to decode the WHOIS response - `follow` - How many WHOIS server to query. 1 = registry server (faster), 2 = registry + registrar (more domain details). Default: 2 - `raw` - Return the raw WHOIS result in response. Added to `__raw` - `ignorePrivacy` - Show or hide the WHOIS protected data from response, accepts boolean. Default: true @@ -118,6 +119,7 @@ Get WHOIS info for IPs - `options` - Object of options to use, all optional: - `host` - WHOIS server to query. Default: WHOIS server from IANA - `timeout` - WHOIS server request timeout in ms. Default: 1500 + - `encoding` - Encoding used to decode the WHOIS response - `raw` - Return the raw WHOIS result in response. Added to `__raw` ```js @@ -160,6 +162,7 @@ Get WHOIS info for an AS number - `options` - Object of options to use, all optional: - `host` - WHOIS server to query. Default: WHOIS server from IANA - `timeout` - WHOIS server request timeout in ms. Default: 1500 + - `encoding` - Encoding used to decode the WHOIS response - `raw` - Return the raw WHOIS result in response. Added to `__raw` ```js diff --git a/src/types.ts b/src/types.ts index 1f8661787..ecbce3145 100644 --- a/src/types.ts +++ b/src/types.ts @@ -46,8 +46,9 @@ export interface TldWhoisResponse { export interface DomainWhoisOptions { host?: string timeout?: number + encoding?: BufferEncoding follow?: 1 | 2 raw?: boolean ignorePrivacy?: boolean - whoisQuery?: (host: string, query: string, timeout?: number) => Promise + whoisQuery?: (host: string, query: string, timeout?: number, encoding?: BufferEncoding) => Promise } diff --git a/src/whoiser.ts b/src/whoiser.ts index 96a239a78..190d95c7d 100644 --- a/src/whoiser.ts +++ b/src/whoiser.ts @@ -70,13 +70,15 @@ const misspelledWhoisServer = { * @param host WHOIS server hostname * @param query Query string * @param timeout Timeout duration in milliseconds + * @param encoding Encoding used to decode the response, for servers that don't reply in UTF-8. Example: 'latin1'. Default: UTF-8 * @returns Result of the WHOIS query */ -export function whoisQuery(host: string, query: string, timeout: number = 5000): Promise { +export function whoisQuery(host: string, query: string, timeout: number = 5000, encoding?: BufferEncoding): Promise { return new Promise((resolve, reject) => { let data = '' const socket = net.connect({ host, port: 43, family: 4 }, () => socket.write(query + '\r\n')) socket.setTimeout(timeout) + if (encoding) socket.setEncoding(encoding) socket.on('data', (chunk) => (data += chunk)) socket.on('close', () => resolve(data)) socket.on('timeout', () => socket.destroy(new Error('Timeout'))) @@ -197,7 +199,7 @@ export async function whoisDomain(domain: string, options?: DomainWhoisOptions): } try { - resultRaw = await queryFn(host, query, options?.timeout) + resultRaw = await queryFn(host, query, options?.timeout, options?.encoding) result = parseDomainWhois(domain, resultRaw, options?.ignorePrivacy ?? true) } catch (err) { result = { error: err.message } @@ -263,7 +265,7 @@ async function findWhoisServerInIana(query: string) { * @returns Normalized WHOIS data * @throws Error if IP is invalid or not found */ -export async function whoisIp(ip: string, options: { host?: string; timeout?: number } = {}): Promise { +export async function whoisIp(ip: string, options: { host?: string; timeout?: number; encoding?: BufferEncoding } = {}): Promise { if (!net.isIP(ip)) { throw new Error(`Invalid IP address "${ip}"`) } @@ -281,7 +283,7 @@ export async function whoisIp(ip: string, options: { host?: string; timeout?: nu modifiedQuery = `+ n ${ip}` } - const ipWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000) + const ipWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000, options.encoding) return parseSimpleWhois(ipWhoisResult) } @@ -293,7 +295,7 @@ export async function whoisIp(ip: string, options: { host?: string; timeout?: nu * @returns Normalized WHOIS data * @throws Error if ASN is invalid or not found */ -export async function whoisAsn(asn: number, options: { host?: string; timeout?: number } = {}): Promise { +export async function whoisAsn(asn: number, options: { host?: string; timeout?: number; encoding?: BufferEncoding } = {}): Promise { if (asn < 0 || asn > 4294967295) { throw new Error(`Invalid ASN number "${asn}"`) } @@ -312,7 +314,7 @@ export async function whoisAsn(asn: number, options: { host?: string; timeout?: modifiedQuery = `+ a ${asn}` } - const asnWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000) + const asnWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000, options.encoding) return parseSimpleWhois(asnWhoisResult) }