From cac6ba9622a2d3e9ade308e1428f9dd9e3a56176 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:53:44 -0800 Subject: [PATCH] feat: allow passing custom default resolvers --- packages/ipns/src/index.ts | 12 ++++---- packages/ipns/test/resolveDns.spec.ts | 41 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 packages/ipns/test/resolveDns.spec.ts diff --git a/packages/ipns/src/index.ts b/packages/ipns/src/index.ts index 8793fc4..37eeb9d 100644 --- a/packages/ipns/src/index.ts +++ b/packages/ipns/src/index.ts @@ -229,12 +229,12 @@ class DefaultIPNS implements IPNS { private readonly routers: IPNSRouting[] private readonly localStore: LocalStore private timeout?: ReturnType - private readonly defaultResolver: DNSResolver + private readonly defaultResolvers: DNSResolver[] - constructor (components: IPNSComponents, routers: IPNSRouting[] = []) { + constructor (components: IPNSComponents, routers: IPNSRouting[] = [], resolvers: DNSResolver[] = []) { this.routers = routers this.localStore = localStore(components.datastore) - this.defaultResolver = defaultResolver() + this.defaultResolvers = resolvers.length > 0 ? resolvers : [defaultResolver()] } async publish (key: PeerId, value: CID | PeerId, options: PublishOptions = {}): Promise { @@ -275,7 +275,7 @@ class DefaultIPNS implements IPNS { } async resolveDns (domain: string, options: ResolveDNSOptions = {}): Promise { - const resolvers = options.resolvers ?? [this.defaultResolver] + const resolvers = options.resolvers ?? this.defaultResolvers const dnslink = await Promise.any( resolvers.map(async resolver => resolver(domain, options)) @@ -376,8 +376,8 @@ class DefaultIPNS implements IPNS { } } -export function ipns (components: IPNSComponents, routers: IPNSRouting[] = []): IPNS { - return new DefaultIPNS(components, routers) +export function ipns (components: IPNSComponents, routers: IPNSRouting[] = [], resolvers: DNSResolver[] = []): IPNS { + return new DefaultIPNS(components, routers, resolvers) } export { ipnsValidator } diff --git a/packages/ipns/test/resolveDns.spec.ts b/packages/ipns/test/resolveDns.spec.ts new file mode 100644 index 0000000..ac0af6c --- /dev/null +++ b/packages/ipns/test/resolveDns.spec.ts @@ -0,0 +1,41 @@ +/* eslint-env mocha */ + +import { expect } from 'aegir/chai' +import { MemoryDatastore } from 'datastore-core' +import { type Datastore } from 'interface-datastore' +import { stub } from 'sinon' +import { type StubbedInstance, stubInterface } from 'sinon-ts' +import { type IPNSRouting, ipns } from '../src/index.js' + +describe('resolveDns', () => { + let routing: StubbedInstance + let datastore: Datastore + + beforeEach(async () => { + datastore = new MemoryDatastore() + routing = stubInterface() + routing.get.throws(new Error('Not found')) + }) + + it('should use resolvers passed in constructor', async () => { + const stubbedResolver1 = stub().returns('dnslink=/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + + const name = ipns({ datastore }, [routing], [stubbedResolver1]) + const result = await name.resolveDns('foobar.baz', { nocache: true, offline: true }) + expect(stubbedResolver1.called).to.be.true() + expect(stubbedResolver1.calledWith('foobar.baz')).to.be.true() + expect(result.toString()).to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + }) + + it('should allow overriding of resolvers passed in constructor', async () => { + const stubbedResolver1 = stub().returns('dnslink=/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + const stubbedResolver2 = stub().returns('dnslink=/ipfs/bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq') + + const name = ipns({ datastore }, [routing], [stubbedResolver1]) + const result = await name.resolveDns('foobar.baz', { nocache: true, offline: true, resolvers: [stubbedResolver2] }) + expect(stubbedResolver1.called).to.be.false() + expect(stubbedResolver2.called).to.be.true() + expect(stubbedResolver2.calledWith('foobar.baz')).to.be.true() + expect(result.toString()).to.equal('bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq') + }) +})