From 08f211d8fd2daf0aefe876b02526b3b65caff760 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Feb 2023 17:16:24 -0500 Subject: [PATCH] *refactor to new system *remove rpc code for now --- package.json | 15 +++++---- src/client.ts | 3 ++ src/index.ts | 18 +++++----- src/resolverRegistry.ts | 73 +++++++++++++++++------------------------ 4 files changed, 52 insertions(+), 57 deletions(-) create mode 100644 src/client.ts diff --git a/package.json b/package.json index 954129e..19f6c3a 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,16 @@ "version": "0.1.0", "main": "dist/index.js", "dependencies": { - "@lumeweb/kernel-dns-client": "https://github.com/LumeWeb/kernel-dns-client.git", - "@lumeweb/kernel-rpc-client": "https://github.com/LumeWeb/kernel-rpc-client.git", - "@lumeweb/libresolver": "https://github.com/LumeWeb/libresolver.git", - "libkmodule": "^0.2.51" + "@lumeweb/kernel-dns-client": "git+https://git.lumeweb.com/LumeWeb/kernel-dns-client.git", + "@lumeweb/kernel-libresolver": "git+https://git.lumeweb.com/LumeWeb/kernel-libresolver.git", + "@lumeweb/kernel-rpc-client": "git+https://git.lumeweb.com/LumeWeb/kernel-rpc-client.git", + "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", + "@lumeweb/libresolver": "git+https://git.lumeweb.com/LumeWeb/libresolver.git", + "libkmodule": "^0.2.53" }, "devDependencies": { - "@lumeweb/relay-types": "https://github.com/LumeWeb/relay-types.git", - "@types/node": "^18.7.14" + "@lumeweb/relay-types": "git+https://git.lumeweb.com/LumeWeb/relay-types.git", + "@types/node": "^18.14.0", + "prettier": "^2.8.4" } } diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..6a905f6 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,3 @@ +import { createClient } from "@lumeweb/kernel-dns-client"; + +export const dnsClient = createClient(); diff --git a/src/index.ts b/src/index.ts index dffccc7..92b8b3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,13 @@ import { addHandler, ActiveQuery, handleMessage } from "libkmodule"; -import { register } from "@lumeweb/kernel-dns-client"; -import type { - DNSResult, - ResolverModuleConstructor, + +import type { DNSResult } from "@lumeweb/libresolver"; +import { ResolverModule, -} from "@lumeweb/resolver-common"; -import { RpcNetwork } from "@lumeweb/kernel-rpc-client"; -import { ResolverRegistry } from "./resolverRegistry.js"; + ResolverModuleConstructor, + ResolverRegistry, +} from "./resolverRegistry.js"; import { DNS_RECORD_TYPE } from "@lumeweb/libresolver"; +import { dnsClient } from "./client.js"; let resolver: ResolverModule; @@ -16,11 +16,11 @@ export function setup(rm: ResolverModuleConstructor) { addHandler("register", handleRegister); addHandler("getSupportedTlds", handleGetSupportedTlds); onmessage = handleMessage; - resolver = new rm(new ResolverRegistry(new RpcNetwork()) as any); + resolver = new rm(new ResolverRegistry()); } async function handleRegister(aq: ActiveQuery) { - await register(); + await dnsClient.register(); aq.respond(); } diff --git a/src/resolverRegistry.ts b/src/resolverRegistry.ts index ec269f5..1322e68 100644 --- a/src/resolverRegistry.ts +++ b/src/resolverRegistry.ts @@ -3,31 +3,29 @@ import { ResolverOptions, DNS_RECORD_TYPE, resolverError, -} from "@lumeweb/resolver-common"; -import { getResolvers, resolve } from "@lumeweb/kernel-dns-client"; -import { RpcNetwork } from "@lumeweb/kernel-rpc-client"; -import { callModule } from "libkmodule"; + 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 { - private _rpcNetwork: RpcNetwork; - - constructor(network: RpcNetwork) { - this._rpcNetwork = network; - } - - get rpcNetwork(): RpcNetwork { - return this._rpcNetwork; - } - get resolvers(): Promise> { - return getResolvers() - .catch(() => { - return new Set(); - }) + return dnsClient + .getResolvers() .then((resolvers: string[]) => { return new Set( - resolvers.map((resolver) => new ResolverModule(this, resolver)) + resolvers.map((resolver) => + factory(ResolverModule, resolver)(this, resolver) + ) ); + }) + .catch(() => { + return new Set(); }); } @@ -36,23 +34,22 @@ export class ResolverRegistry { options: ResolverOptions = { type: DNS_RECORD_TYPE.CONTENT }, bypassCache: boolean = false ): Promise { - let result: DNSResult; - try { - result = await resolve(domain, options, bypassCache); + return dnsClient.resolve(domain, options, bypassCache); } catch (e: any) { return resolverError(e); } - - return result; } + public register(resolver: ResolverModule): void {} + public clear(): void {} } -export class ResolverModule { +export class ResolverModule extends Client { private resolver: ResolverRegistry; private domain: string; constructor(resolver: ResolverRegistry, domain: string) { + super(); this.resolver = resolver; this.domain = domain; } @@ -62,25 +59,17 @@ export class ResolverModule { options: ResolverOptions, bypassCache: boolean ): Promise { - const [ret, err] = await callModule(this.domain, "resolve", { - domain, - options, - bypassCache, - }); - if (err) { - return resolverError(err); + try { + return this.callModuleReturn("resolve", { + domain, + options, + bypassCache, + }); + } catch (e) { + return resolverError(e as Error); } - - return ret; } async getSupportedTlds(): Promise { - const [ret, err] = await callModule(this.domain, "getSupportedTlds"); - if (err) { - throw new Error(err); - } - - return ret; + return this.callModuleReturn("getSupportedTlds"); } } - -export { RpcNetwork };