*First steps in breaking resolver framework to be modular
This commit is contained in:
parent
cd680ef7d0
commit
508eb692e3
|
@ -52,7 +52,7 @@
|
|||
"@lumeweb/kernel-dht-client": "https://github.com/LumeWeb/kernel-dht-client.git",
|
||||
"@lumeweb/kernel-rpc-client": "https://github.com/LumeWeb/kernel-rpc-client.git",
|
||||
"@lumeweb/kernel-utils": "https://github.com/LumeWeb/kernel-utils.git",
|
||||
"@lumeweb/resolver": "https://github.com/LumeWeb/resolver.git#hypercore",
|
||||
"@lumeweb/resolver-common": "github:LumeWeb/resolver-common",
|
||||
"libkmodule": "^0.2.11",
|
||||
"randombytes": "https://github.com/LumeWeb/randombytes-browser.git",
|
||||
"randomfill": "https://github.com/LumeWeb/randomfill.git"
|
||||
|
@ -63,6 +63,7 @@
|
|||
"os": "os-browserify",
|
||||
"stream": "os-browserify",
|
||||
"libskynetnode": false,
|
||||
"js-sha3": "@lumeweb/js-sha3-browser"
|
||||
"js-sha3": "@lumeweb/js-sha3-browser",
|
||||
"@lumeweb/dht-rpc-client": false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import resolvers from "@lumeweb/resolver"
|
||||
import { RpcNetwork } from "@lumeweb/kernel-rpc-client"
|
||||
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
||||
import { ResolverRegistry } from "./resolverRegistry.js";
|
||||
|
||||
const network = new RpcNetwork()
|
||||
const network: RpcNetwork = new RpcNetwork();
|
||||
|
||||
export const resolver = resolvers.createDefaultResolver(network as any)
|
||||
export const resolver = new ResolverRegistry(network as any);
|
||||
|
|
54
src/index.ts
54
src/index.ts
|
@ -1,24 +1,52 @@
|
|||
import {addHandler, handleMessage} from "libkmodule"
|
||||
import type {ActiveQuery} from "libkmodule"
|
||||
import {resolver} from "./common.js"
|
||||
import { addHandler, handleMessage } from "libkmodule";
|
||||
import type { ActiveQuery } from "libkmodule";
|
||||
import { resolver } from "./common.js";
|
||||
|
||||
import {relayReady, setupRelayListSubscription} from "./relays.js"
|
||||
import { relayReady, setupRelayListSubscription } from "./relays.js";
|
||||
import { validSkylink } from "libskynet/dist/skylinkvalidate.js";
|
||||
import { ResolverModule } from "./resolverRegistry.js";
|
||||
import { b64ToBuf } from "libskynet";
|
||||
|
||||
setupRelayListSubscription()
|
||||
setupRelayListSubscription();
|
||||
|
||||
addHandler("resolve", resolveHandler)
|
||||
onmessage = handleMessage
|
||||
|
||||
async function resolveHandler(aq: ActiveQuery) {
|
||||
const query = aq.callerInput
|
||||
addHandler("resolve", handleResolve);
|
||||
addHandler("register", handleRegister);
|
||||
addHandler("clear", handleClear);
|
||||
addHandler("getResolvers", handleGetResolvers);
|
||||
onmessage = handleMessage;
|
||||
|
||||
async function handleResolve(aq: ActiveQuery) {
|
||||
const query = aq.callerInput;
|
||||
if (!("domain" in query)) {
|
||||
aq.reject("domain required")
|
||||
return
|
||||
aq.reject("domain required");
|
||||
return;
|
||||
}
|
||||
|
||||
await relayReady();
|
||||
aq.respond(await resolver.resolve(aq.callerInput.domain, aq.callerInput.params ?? {}, aq.callerInput.force || false))
|
||||
aq.respond(
|
||||
await resolver.resolve(
|
||||
aq.callerInput.domain,
|
||||
aq.callerInput.options ?? {},
|
||||
aq.callerInput.bypassCache || false
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async function handleRegister(aq: ActiveQuery) {
|
||||
if (!validSkylink(b64ToBuf(aq.domain).shift() as Uint8Array)) {
|
||||
aq.reject("invalid skylink");
|
||||
return;
|
||||
}
|
||||
|
||||
resolver.register(new ResolverModule(resolver, aq.domain));
|
||||
aq.respond();
|
||||
}
|
||||
|
||||
async function handleClear(aq: ActiveQuery) {
|
||||
resolver.clear();
|
||||
aq.respond();
|
||||
}
|
||||
|
||||
async function handleGetResolvers(aq: ActiveQuery) {
|
||||
aq.respond([...resolver.resolvers.values()]);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
import {
|
||||
DNSResult,
|
||||
ResolverOptions,
|
||||
DNS_RECORD_TYPE,
|
||||
resolverError,
|
||||
} from "@lumeweb/resolver-common";
|
||||
import type { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
||||
import { callModule } from "libkmodule/dist";
|
||||
|
||||
export class ResolverRegistry {
|
||||
private _resolvers: Set<ResolverModule> = new Set();
|
||||
private _rpcNetwork: RpcNetwork;
|
||||
|
||||
constructor(network: RpcNetwork) {
|
||||
this._rpcNetwork = network;
|
||||
}
|
||||
|
||||
get resolvers(): Set<ResolverModule> {
|
||||
return this._resolvers;
|
||||
}
|
||||
|
||||
get rpcNetwork(): RpcNetwork {
|
||||
return this._rpcNetwork;
|
||||
}
|
||||
|
||||
public async resolve(
|
||||
domain: string,
|
||||
options: ResolverOptions = { type: DNS_RECORD_TYPE.DEFAULT },
|
||||
bypassCache: boolean = false
|
||||
): Promise<DNSResult> {
|
||||
for (const resolver of this._resolvers) {
|
||||
const result = await resolver.resolve(domain, options, bypassCache);
|
||||
if (!result.error && result.records.length) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return { records: [] };
|
||||
}
|
||||
|
||||
public register(resolver: ResolverModule): void {
|
||||
this._resolvers.add(resolver);
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
this._resolvers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
export class ResolverModule {
|
||||
private resolver: ResolverRegistry;
|
||||
private domain: string;
|
||||
|
||||
constructor(resolver: ResolverRegistry, domain: string) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
async resolve(
|
||||
domain: string,
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean
|
||||
): Promise<DNSResult> {
|
||||
const [ret, err] = await callModule(this.domain, "resolve", {
|
||||
domain,
|
||||
options,
|
||||
bypassCache,
|
||||
});
|
||||
if (err) {
|
||||
return resolverError(err);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue