Skip to content

Commit

Permalink
Improve WhoisQuickInfo resilience 💪
Browse files Browse the repository at this point in the history
  • Loading branch information
wotschofsky committed Dec 19, 2023
1 parent 3bf4fb0 commit 4699c0c
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions components/WhoisQuickInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isValidDomain from 'is-valid-domain';
import { FC } from 'react';
import whoiser, { type WhoisSearchResult } from 'whoiser';

Expand All @@ -7,42 +8,68 @@ type WhoisQuickInfoProps = {
domain: string;
};

const getSummary = async (
domain: string
): Promise<{ registrar: string; createdAt: string; dnssec: string }> => {
// TODO Allow resolving for TLDs
if (!isValidDomain(domain)) {
return {
registrar: 'Unavailable',
createdAt: 'Unavailable',
dnssec: 'Unavailable',
};
}

try {
const results = await whoiser(domain, {
timeout: 5000,
});

const resultsKey = Object.keys(results).find(
// @ts-expect-error
(key) => !('error' in results[key])
);
if (!resultsKey) {
throw new Error('No valid results found for domain ' + domain);
}
const firstResult = results[resultsKey] as WhoisSearchResult;

return {
registrar: firstResult['Registrar']?.toString() || 'Unavailable',
createdAt:
firstResult && 'Created Date' in firstResult
? new Date(firstResult['Created Date'].toString()).toLocaleDateString(
'en-US'
)
: 'Unavailable',
dnssec: firstResult['DNSSEC']?.toString() || 'Unavailable',
};
} catch (error) {
console.error(error);
return {
registrar: 'Unavailable',
createdAt: 'Unavailable',
dnssec: 'Unavailable',
};
}
};

const WhoisQuickInfo: FC<WhoisQuickInfoProps> = async ({ domain }) => {
const results = await whoiser(domain, {
timeout: 5000,
});
const firstResult = results[
// @ts-expect-error
Object.keys(results).filter((key) => !('error' in results[key]))[0]
] as WhoisSearchResult;
const results = await getSummary(domain);

return (
<div className="my-8 flex gap-8">
<div>
<h3 className="text-xs text-muted-foreground">Registrar</h3>
<p className="text-sm">
{firstResult && 'Registrar' in firstResult
? firstResult['Registrar'].toString()
: 'Unavailable'}
</p>
<p className="text-sm">{results.registrar}</p>
</div>
<div>
<h3 className="text-xs text-muted-foreground">Creation Date</h3>
<p className="text-sm">
{firstResult && 'Created Date' in firstResult
? new Date(
firstResult['Created Date'].toString()
).toLocaleDateString('en-US')
: 'Unavailable'}
</p>
<p className="text-sm">{results.createdAt}</p>
</div>
<div>
<h3 className="text-xs text-muted-foreground">DNSSEC</h3>
<p className="text-sm">
{firstResult && 'DNSSEC' in firstResult
? firstResult['DNSSEC'].toString()
: 'Unavailable'}
</p>
<p className="text-sm">{results.dnssec}</p>
</div>
</div>
);
Expand Down

1 comment on commit 4699c0c

@vercel
Copy link

@vercel vercel bot commented on 4699c0c Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.