Skip to content

Commit

Permalink
chore: fix interface
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Mar 11, 2024
1 parent 747df0b commit 5ce06b9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ export interface AbortOptions {
*/
export const resolvers = new Map<string, Resolver>()

export type { Resolver }

export { MultiaddrFilter } from './filter/multiaddr-filter.js'

export interface ResolveOptions extends AbortOptions {
Expand Down
6 changes: 4 additions & 2 deletions src/multiaddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { bytesToMultiaddrParts, stringToMultiaddrParts, type MultiaddrParts, tuplesToBytes } from './codec.js'
import { getProtocol, names } from './protocols-table.js'
import { isMultiaddr, resolvers } from './index.js'
import { isMultiaddr, multiaddr, resolvers } from './index.js'
import type { MultiaddrInput, Multiaddr as MultiaddrInterface, MultiaddrObject, Protocol, StringTuple, Tuple, NodeAddress, ResolveOptions } from './index.js'

const inspect = Symbol.for('nodejs.util.inspect.custom')
Expand Down Expand Up @@ -235,7 +235,9 @@ export class Multiaddr implements MultiaddrInterface {
throw new CodeError(`no available resolver for ${resolvableProto.name}`, 'ERR_NO_AVAILABLE_RESOLVER')
}

return resolver(this, options)
const result = await resolver(this, options)

return result.map(str => multiaddr(str))
}

nodeAddress (): NodeAddress {
Expand Down
8 changes: 4 additions & 4 deletions src/resolvers/dnsaddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface DNSADDROptions extends AbortOptions {
maxRecursiveDepth?: number
}

export const dnsaddr: Resolver<DNSADDROptions> = async function dnsaddr (ma: Multiaddr, options: DNSADDROptions = {}): Promise<Multiaddr[]> {
export const dnsaddrResolver: Resolver<DNSADDROptions> = async function dnsaddr (ma: Multiaddr, options: DNSADDROptions = {}): Promise<string[]> {
const recursionLimit = options.maxRecursiveDepth ?? MAX_RECURSIVE_DEPTH

if (recursionLimit === 0) {
Expand All @@ -43,7 +43,7 @@ export const dnsaddr: Resolver<DNSADDROptions> = async function dnsaddr (ma: Mul
}), options.signal)

const peerId = ma.getPeerId()
const output: Multiaddr[] = []
const output: string[] = []

for (const answer of result.Answer) {
const addr = answer.data.split('=')[1]
Expand All @@ -64,9 +64,9 @@ export const dnsaddr: Resolver<DNSADDROptions> = async function dnsaddr (ma: Mul
maxRecursiveDepth: recursionLimit - 1
})

output.push(...resolved)
output.push(...resolved.map(ma => ma.toString()))
} else {
output.push(ma)
output.push(ma.toString())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AbortOptions, Multiaddr } from '../index.js'

export interface Resolver<ResolveOptions extends AbortOptions = AbortOptions> {
(ma: Multiaddr, options?: ResolveOptions): Promise<Multiaddr[]>
(ma: Multiaddr, options?: ResolveOptions): Promise<string[]>
}

export { dnsaddr } from './dnsaddr.js'
export { dnsaddrResolver } from './dnsaddr.js'
4 changes: 2 additions & 2 deletions test/resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'aegir/chai'
import sinon from 'sinon'
import { stubInterface, type StubbedInstance } from 'sinon-ts'
import { multiaddr, resolvers } from '../src/index.js'
import { dnsaddr } from '../src/resolvers/index.js'
import { dnsaddrResolver } from '../src/resolvers/index.js'
import type { DNS } from '@multiformats/dns'

const stubs: Record<string, string[]> = {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('multiaddr resolve', () => {
})
})

resolvers.set('dnsaddr', dnsaddr)
resolvers.set('dnsaddr', dnsaddrResolver)
})

it('should throw if no resolver is available', async () => {
Expand Down

0 comments on commit 5ce06b9

Please sign in to comment.