*refactor to new system

*remove rpc code for now
This commit is contained in:
Derrick Hammer 2023-02-19 17:16:24 -05:00
parent cf3f54f2ee
commit 08f211d8fd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 52 additions and 57 deletions

View File

@ -3,13 +3,16 @@
"version": "0.1.0", "version": "0.1.0",
"main": "dist/index.js", "main": "dist/index.js",
"dependencies": { "dependencies": {
"@lumeweb/kernel-dns-client": "https://github.com/LumeWeb/kernel-dns-client.git", "@lumeweb/kernel-dns-client": "git+https://git.lumeweb.com/LumeWeb/kernel-dns-client.git",
"@lumeweb/kernel-rpc-client": "https://github.com/LumeWeb/kernel-rpc-client.git", "@lumeweb/kernel-libresolver": "git+https://git.lumeweb.com/LumeWeb/kernel-libresolver.git",
"@lumeweb/libresolver": "https://github.com/LumeWeb/libresolver.git", "@lumeweb/kernel-rpc-client": "git+https://git.lumeweb.com/LumeWeb/kernel-rpc-client.git",
"libkmodule": "^0.2.51" "@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": { "devDependencies": {
"@lumeweb/relay-types": "https://github.com/LumeWeb/relay-types.git", "@lumeweb/relay-types": "git+https://git.lumeweb.com/LumeWeb/relay-types.git",
"@types/node": "^18.7.14" "@types/node": "^18.14.0",
"prettier": "^2.8.4"
} }
} }

3
src/client.ts Normal file
View File

@ -0,0 +1,3 @@
import { createClient } from "@lumeweb/kernel-dns-client";
export const dnsClient = createClient();

View File

@ -1,13 +1,13 @@
import { addHandler, ActiveQuery, handleMessage } from "libkmodule"; import { addHandler, ActiveQuery, handleMessage } from "libkmodule";
import { register } from "@lumeweb/kernel-dns-client";
import type { import type { DNSResult } from "@lumeweb/libresolver";
DNSResult, import {
ResolverModuleConstructor,
ResolverModule, ResolverModule,
} from "@lumeweb/resolver-common"; ResolverModuleConstructor,
import { RpcNetwork } from "@lumeweb/kernel-rpc-client"; ResolverRegistry,
import { ResolverRegistry } from "./resolverRegistry.js"; } from "./resolverRegistry.js";
import { DNS_RECORD_TYPE } from "@lumeweb/libresolver"; import { DNS_RECORD_TYPE } from "@lumeweb/libresolver";
import { dnsClient } from "./client.js";
let resolver: ResolverModule; let resolver: ResolverModule;
@ -16,11 +16,11 @@ export function setup(rm: ResolverModuleConstructor) {
addHandler("register", handleRegister); addHandler("register", handleRegister);
addHandler("getSupportedTlds", handleGetSupportedTlds); addHandler("getSupportedTlds", handleGetSupportedTlds);
onmessage = handleMessage; onmessage = handleMessage;
resolver = new rm(new ResolverRegistry(new RpcNetwork()) as any); resolver = new rm(new ResolverRegistry());
} }
async function handleRegister(aq: ActiveQuery) { async function handleRegister(aq: ActiveQuery) {
await register(); await dnsClient.register();
aq.respond(); aq.respond();
} }

View File

@ -3,31 +3,29 @@ import {
ResolverOptions, ResolverOptions,
DNS_RECORD_TYPE, DNS_RECORD_TYPE,
resolverError, resolverError,
} from "@lumeweb/resolver-common"; ResolverModuleConstructor as ResolverModuleConstructorBase,
import { getResolvers, resolve } from "@lumeweb/kernel-dns-client"; } from "@lumeweb/libresolver";
import { RpcNetwork } from "@lumeweb/kernel-rpc-client"; import { Client, factory } from "@lumeweb/libkernel-universal";
import { callModule } from "libkmodule"; import { dnsClient } from "./client.js";
export interface ResolverModuleConstructor
extends ResolverModuleConstructorBase {
new (resolver: ResolverRegistry): ResolverModule;
}
export class ResolverRegistry { export class ResolverRegistry {
private _rpcNetwork: RpcNetwork;
constructor(network: RpcNetwork) {
this._rpcNetwork = network;
}
get rpcNetwork(): RpcNetwork {
return this._rpcNetwork;
}
get resolvers(): Promise<Set<ResolverModule>> { get resolvers(): Promise<Set<ResolverModule>> {
return getResolvers() return dnsClient
.catch(() => { .getResolvers()
return new Set();
})
.then((resolvers: string[]) => { .then((resolvers: string[]) => {
return new Set( return new Set(
resolvers.map((resolver) => new ResolverModule(this, resolver)) resolvers.map((resolver) =>
factory<ResolverModule>(ResolverModule, resolver)(this, resolver)
)
); );
})
.catch(() => {
return new Set();
}); });
} }
@ -36,23 +34,22 @@ export class ResolverRegistry {
options: ResolverOptions = { type: DNS_RECORD_TYPE.CONTENT }, options: ResolverOptions = { type: DNS_RECORD_TYPE.CONTENT },
bypassCache: boolean = false bypassCache: boolean = false
): Promise<DNSResult> { ): Promise<DNSResult> {
let result: DNSResult;
try { try {
result = await resolve(domain, options, bypassCache); return dnsClient.resolve(domain, options, bypassCache);
} catch (e: any) { } catch (e: any) {
return resolverError(e); 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 resolver: ResolverRegistry;
private domain: string; private domain: string;
constructor(resolver: ResolverRegistry, domain: string) { constructor(resolver: ResolverRegistry, domain: string) {
super();
this.resolver = resolver; this.resolver = resolver;
this.domain = domain; this.domain = domain;
} }
@ -62,25 +59,17 @@ export class ResolverModule {
options: ResolverOptions, options: ResolverOptions,
bypassCache: boolean bypassCache: boolean
): Promise<DNSResult> { ): Promise<DNSResult> {
const [ret, err] = await callModule(this.domain, "resolve", { try {
domain, return this.callModuleReturn("resolve", {
options, domain,
bypassCache, options,
}); bypassCache,
if (err) { });
return resolverError(err); } catch (e) {
return resolverError(e as Error);
} }
return ret;
} }
async getSupportedTlds(): Promise<string[]> { async getSupportedTlds(): Promise<string[]> {
const [ret, err] = await callModule(this.domain, "getSupportedTlds"); return this.callModuleReturn("getSupportedTlds");
if (err) {
throw new Error(err);
}
return ret;
} }
} }
export { RpcNetwork };