*Abort if module is already registered

This commit is contained in:
Derrick Hammer 2022-08-19 09:25:53 -04:00
parent 92241c3748
commit a7b6948312
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 22 additions and 9 deletions

View File

@ -8,17 +8,18 @@ import type { RpcNetwork } from "@lumeweb/kernel-rpc-client";
import { callModule } from "libkmodule/dist"; import { callModule } from "libkmodule/dist";
export class ResolverRegistry { export class ResolverRegistry {
private _resolvers: Set<ResolverModule> = new Set();
private _rpcNetwork: RpcNetwork;
constructor(network: RpcNetwork) { constructor(network: RpcNetwork) {
this._rpcNetwork = network; this._rpcNetwork = network;
} }
private _resolvers: Set<ResolverModule> = new Set<ResolverModule>();
get resolvers(): Set<ResolverModule> { get resolvers(): Set<ResolverModule> {
return this._resolvers; return this._resolvers;
} }
private _rpcNetwork: RpcNetwork;
get rpcNetwork(): RpcNetwork { get rpcNetwork(): RpcNetwork {
return this._rpcNetwork; return this._rpcNetwork;
} }
@ -28,7 +29,7 @@ export class ResolverRegistry {
options: ResolverOptions = { type: DNS_RECORD_TYPE.DEFAULT }, options: ResolverOptions = { type: DNS_RECORD_TYPE.DEFAULT },
bypassCache: boolean = false bypassCache: boolean = false
): Promise<DNSResult> { ): Promise<DNSResult> {
for (const resolver of this._resolvers) { for (const resolver: ResolverModule of this._resolvers) {
const result = await resolver.resolve(domain, options, bypassCache); const result = await resolver.resolve(domain, options, bypassCache);
if (!result.error && result.records.length) { if (!result.error && result.records.length) {
return result; return result;
@ -39,6 +40,13 @@ export class ResolverRegistry {
} }
public register(resolver: ResolverModule): void { public register(resolver: ResolverModule): void {
if (
[...this._resolvers.values()].filter(
(item) => item.domain === resolver.domain
).length > 0
) {
return;
}
this._resolvers.add(resolver); this._resolvers.add(resolver);
} }
@ -48,12 +56,17 @@ export class ResolverRegistry {
} }
export class ResolverModule { export class ResolverModule {
private resolver: ResolverRegistry; private _resolver: ResolverRegistry;
private domain: string;
constructor(resolver: ResolverRegistry, domain: string) { constructor(resolver: ResolverRegistry, domain: string) {
this.resolver = resolver; this._resolver = resolver;
this.domain = domain; this._domain = domain;
}
private _domain: string;
get domain(): string {
return this._domain;
} }
async resolve( async resolve(
@ -61,7 +74,7 @@ export class ResolverModule {
options: ResolverOptions, options: ResolverOptions,
bypassCache: boolean bypassCache: boolean
): Promise<DNSResult> { ): Promise<DNSResult> {
const [ret, err] = await callModule(this.domain, "resolve", { const [ret, err] = await callModule(this._domain, "resolve", {
domain, domain,
options, options,
bypassCache, bypassCache,