This repository has been archived on 2023-12-17. You can view files and clone it, but cannot push or open issues or pull requests.
extension/src/dns.ts

35 lines
788 B
TypeScript
Raw Normal View History

2022-08-15 08:29:13 +00:00
import NodeCache from "node-cache";
import { resolve as resolveDns } from "@lumeweb/kernel-dns-client";
2022-08-22 01:48:46 +00:00
import { DNSResult, ResolverOptions } from "@lumeweb/libresolver";
import { blake2b, Err } from "libskynet/dist";
2022-08-15 08:29:13 +00:00
const cache = new NodeCache({ stdTTL: 60 });
2022-08-22 01:48:46 +00:00
export async function resolve(
domain: string,
options?: ResolverOptions,
bypassCache = false
): Promise<DNSResult | Error> {
let cacheId = `${domain}:{${blake2b(
new TextEncoder().encode(JSON.stringify(options))
)}`;
if (cache.has(cacheId)) {
cache.ttl(cacheId);
return cache.get(cacheId) as DNSResult;
2022-08-15 08:29:13 +00:00
}
2022-08-22 01:48:46 +00:00
let res;
try {
res = await resolveDns(domain, options, bypassCache);
} catch (e: any) {
return e as Error;
}
2022-08-15 08:29:13 +00:00
if (res) {
2022-08-22 01:48:46 +00:00
cache.set(cacheId, res);
2022-08-15 08:29:13 +00:00
}
return res;
}