Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
feat: allow passing custom default resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Dec 5, 2023
1 parent f8d3153 commit cac6ba9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/ipns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ class DefaultIPNS implements IPNS {
private readonly routers: IPNSRouting[]
private readonly localStore: LocalStore
private timeout?: ReturnType<typeof setTimeout>
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<IPNSRecord> {
Expand Down Expand Up @@ -275,7 +275,7 @@ class DefaultIPNS implements IPNS {
}

async resolveDns (domain: string, options: ResolveDNSOptions = {}): Promise<CID> {
const resolvers = options.resolvers ?? [this.defaultResolver]
const resolvers = options.resolvers ?? this.defaultResolvers

const dnslink = await Promise.any(
resolvers.map(async resolver => resolver(domain, options))
Expand Down Expand Up @@ -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 }
Expand Down
41 changes: 41 additions & 0 deletions packages/ipns/test/resolveDns.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env mocha */

This comment has been minimized.

Copy link
@achingbrain

achingbrain Dec 6, 2023

Member

This is a nit, but please can you follow the conventions of the existing code. All files should be kebab-case.


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<IPNSRouting>
let datastore: Datastore

beforeEach(async () => {
datastore = new MemoryDatastore()
routing = stubInterface<IPNSRouting>()
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')
})
})

0 comments on commit cac6ba9

Please sign in to comment.