Skip to content

Commit 53fb15f

Browse files
authored
fix: support dns and dnsaddr (#253)
Not sure why but we currently can't parse `/dns/foo.com/...` or `/dnsaddr/foo.com/...` so this PR fixes that.
1 parent 25d22f9 commit 53fb15f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/index.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
99

1010
const inspect = Symbol.for('nodejs.util.inspect.custom')
1111

12+
const IP_CODES = [
13+
getProtocol('ip4').code,
14+
getProtocol('ip6').code
15+
]
16+
const DNS_CODES = [
17+
getProtocol('dns').code,
18+
getProtocol('dns4').code,
19+
getProtocol('dns6').code,
20+
getProtocol('dnsaddr').code
21+
]
22+
1223
export interface Protocol {
1324
code: number
1425
size: number
@@ -447,8 +458,8 @@ export class Multiaddr {
447458

448459
if (parts.length < 4) {
449460
throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6}/{address}/{tcp, udp}/{port}".')
450-
} else if (codes[0] !== 4 && codes[0] !== 41 && codes[0] !== 54 && codes[0] !== 55) {
451-
throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns4, dns6}".`)
461+
} else if (!IP_CODES.includes(codes[0]) && !DNS_CODES.includes(codes[0])) {
462+
throw new Error(`no protocol with name: "'${names[0]}'". Must have a valid family name: "{ip4, ip6, dns, dns4, dns6, dnsaddr}".`)
452463
} else if (parts[2] !== 'tcp' && parts[2] !== 'udp') {
453464
throw new Error(`no protocol with name: "'${names[1]}'". Must have a valid transport protocol: "{tcp, udp}".`)
454465
}

test/index.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,16 @@ describe('helpers', () => {
686686
})
687687

688688
it('returns a node friendly address with dns', () => {
689+
expect(
690+
new Multiaddr('/dns/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
691+
).to.be.eql({
692+
address: 'wss0.bootstrap.libp2p.io',
693+
family: 4,
694+
port: 443
695+
})
696+
})
697+
698+
it('returns a node friendly address with dns4', () => {
689699
expect(
690700
new Multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
691701
).to.be.eql({
@@ -695,6 +705,26 @@ describe('helpers', () => {
695705
})
696706
})
697707

708+
it('returns a node friendly address with dns6', () => {
709+
expect(
710+
new Multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
711+
).to.be.eql({
712+
address: 'wss0.bootstrap.libp2p.io',
713+
family: 6,
714+
port: 443
715+
})
716+
})
717+
718+
it('returns a node friendly address with dnsaddr', () => {
719+
expect(
720+
new Multiaddr('/dnsaddr/wss0.bootstrap.libp2p.io/tcp/443').nodeAddress()
721+
).to.be.eql({
722+
address: 'wss0.bootstrap.libp2p.io',
723+
family: 4,
724+
port: 443
725+
})
726+
})
727+
698728
it('throws on an invalid format address when the addr is not prefixed with a /', () => {
699729
expect(
700730
() => new Multiaddr('ip4/192.168.0.1/udp').nodeAddress()

0 commit comments

Comments
 (0)