Skip to content

Commit

Permalink
Fix invalid date causing WHOIS summary to fail completely ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
wotschofsky committed Sep 13, 2024
1 parent 4274aec commit 720570f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/whois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { getBaseDomain, isValidDomain } from './utils';

export const formatDate = (date: Date) => date.toISOString().split('T')[0];

const parseDateSafe = (date: string): Date | null => {
const parsed = new Date(date);
if (!isNaN(parsed.getTime())) return parsed;
return null
};

export const lookupWhois = async (domain: string) => {
const result = await whoiser(domain, {
raw: true,
Expand Down Expand Up @@ -82,13 +88,14 @@ export const getWhoisSummary = async (
};
}

const createdAt =
firstResult && 'Created Date' in firstResult
? parseDateSafe(firstResult['Created Date'].toString())
: null;
return {
registered: true,
registrar: firstResult['Registrar']?.toString(),
createdAt:
firstResult && 'Created Date' in firstResult
? formatDate(new Date(firstResult['Created Date'].toString()))
: null,
createdAt: createdAt && formatDate(createdAt),
dnssec: firstResult['DNSSEC']?.toString(),
};
} catch (error) {
Expand Down

0 comments on commit 720570f

Please sign in to comment.