From 1b508659394a59a9742d7a539648715ee1c58a85 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Feb 2023 20:29:44 -0500 Subject: [PATCH] *Refactor to set the registry via getter vs passing as a constructor --- src/index.ts | 22 +++++++----- src/resolverRegistry.ts | 75 ----------------------------------------- 2 files changed, 13 insertions(+), 84 deletions(-) delete mode 100644 src/resolverRegistry.ts diff --git a/src/index.ts b/src/index.ts index e81a174..bad382b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,27 @@ import { addHandler, ActiveQuery, handleMessage } from "libkmodule"; -import type { DNSResult } from "@lumeweb/libresolver"; -import { - ResolverModule, - ResolverModuleConstructor, - ResolverRegistry, -} from "./resolverRegistry.js"; +import type { + DNSResult, + ResolverModule as ResolverModuleBase, +} from "@lumeweb/libresolver"; import { DNS_RECORD_TYPE } from "@lumeweb/libresolver"; import { dnsClient } from "./client.js"; +import { DnsClient } from "@lumeweb/kernel-dns-client"; let resolver: ResolverModule; -export function setup(rm: ResolverModuleConstructor) { +interface ResolverModule extends ResolverModuleBase { + get resolver(): DnsClient; + set resolver(value: DnsClient); +} + +export function setup(rm: ResolverModule) { addHandler("resolve", handleResolve); addHandler("register", handleRegister); addHandler("getSupportedTlds", handleGetSupportedTlds); onmessage = handleMessage; - resolver = new rm(new ResolverRegistry()); + resolver = rm; + resolver.resolver = dnsClient; } async function handleRegister(aq: ActiveQuery) { @@ -54,7 +59,6 @@ function handleGetSupportedTlds(aq: ActiveQuery) { aq.respond(resolver.getSupportedTlds()); } -export * from "./resolverRegistry.js"; export * from "@lumeweb/libresolver/dist/util.js"; export * from "@lumeweb/libresolver/dist/types.js"; export { AbstractResolverModule } from "@lumeweb/libresolver/dist/resolverModule.js"; diff --git a/src/resolverRegistry.ts b/src/resolverRegistry.ts deleted file mode 100644 index 1322e68..0000000 --- a/src/resolverRegistry.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { - DNSResult, - ResolverOptions, - DNS_RECORD_TYPE, - resolverError, - ResolverModuleConstructor as ResolverModuleConstructorBase, -} from "@lumeweb/libresolver"; -import { Client, factory } from "@lumeweb/libkernel-universal"; -import { dnsClient } from "./client.js"; - -export interface ResolverModuleConstructor - extends ResolverModuleConstructorBase { - new (resolver: ResolverRegistry): ResolverModule; -} - -export class ResolverRegistry { - get resolvers(): Promise> { - return dnsClient - .getResolvers() - .then((resolvers: string[]) => { - return new Set( - resolvers.map((resolver) => - factory(ResolverModule, resolver)(this, resolver) - ) - ); - }) - .catch(() => { - return new Set(); - }); - } - - public async resolve( - domain: string, - options: ResolverOptions = { type: DNS_RECORD_TYPE.CONTENT }, - bypassCache: boolean = false - ): Promise { - try { - return dnsClient.resolve(domain, options, bypassCache); - } catch (e: any) { - return resolverError(e); - } - } - public register(resolver: ResolverModule): void {} - public clear(): void {} -} - -export class ResolverModule extends Client { - private resolver: ResolverRegistry; - private domain: string; - - constructor(resolver: ResolverRegistry, domain: string) { - super(); - this.resolver = resolver; - this.domain = domain; - } - - async resolve( - domain: string, - options: ResolverOptions, - bypassCache: boolean - ): Promise { - try { - return this.callModuleReturn("resolve", { - domain, - options, - bypassCache, - }); - } catch (e) { - return resolverError(e as Error); - } - } - async getSupportedTlds(): Promise { - return this.callModuleReturn("getSupportedTlds"); - } -}