Skip to content

Commit dd68ad2

Browse files
committed
fix: prefer /dns to allow resolving A or AAAA records
Default to `/dns` and allow overriding to use `/dns4`, `/dns6`, etc. Fixes #8
1 parent 237abb0 commit dd68ad2

File tree

2 files changed

+127
-20
lines changed

2 files changed

+127
-20
lines changed

src/index.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,31 @@ const portFor: Record<string, string> = {
5151
const BROWSER_SCHEMES = ['http', 'https', 'ws', 'wss']
5252

5353
export interface MultiaddrFromUriOpts {
54-
defaultDnsType?: string
54+
/**
55+
* If a URI contains a domain name, by default the `/dns/` tuple will be used
56+
* to define it. If you wish to use `/dnsaddr` or something more specific like
57+
* `/dns4` or `/dns6`, pass it as an option here.
58+
*
59+
* @default 'dns'
60+
*/
61+
defaultDnsType?: 'dns' | 'dns4' | 'dns6' | 'dnsaddr'
5562
}
5663

5764
/**
5865
* Convert a URI to a multiaddr
5966
*
60-
* http://foobar.com => /dns4/foobar.com/tcp/80/http
61-
* https://foobar.com => /dns4/foobar.com/tcp/443/https
62-
* https://foobar.com:5001 => /dns4/foobar.com/tcp/5001/https
67+
* http://foobar.com => /dns/foobar.com/tcp/80/http
68+
* https://foobar.com => /dns/foobar.com/tcp/443/https
69+
* https://foobar.com:5001 => /dns/foobar.com/tcp/5001/https
6370
* https://127.0.0.1:8080 => /ip4/127.0.0.1/tcp/8080/https
6471
* http://[::1]:8080 => /ip6/::1/tcp/8080/http
65-
* tcp://foobar.com:8080 => /dns4/foobar.com/tcp/8080
66-
* udp://foobar.com:8080 => /dns4/foobar.com/udp/8080
72+
* tcp://foobar.com:8080 => /dns/foobar.com/tcp/8080
73+
* udp://foobar.com:8080 => /dns/foobar.com/udp/8080
6774
*/
6875

6976
export function uriToMultiaddr (uriStr: string, opts?: MultiaddrFromUriOpts): Multiaddr {
7077
opts = opts ?? {}
71-
const defaultDnsType = opts.defaultDnsType ?? 'dns4'
78+
const defaultDnsType = opts.defaultDnsType ?? 'dns'
7279
const { scheme, hostname, port, path } = parseUri(uriStr)
7380
const parts = [
7481
tupleForHostname(hostname, defaultDnsType),

test/test.spec.ts

+113-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { MultiaddrFromUriOpts } from '../src/index.js'
44

55
describe('uri-to-multiaddr', () => {
66
it('should convert URIs to multiaddrs', () => {
7-
const data = [
7+
const data: Array<[string, string, MultiaddrFromUriOpts?]> = [
88
['/ip4/127.0.0.1/tcp/80/http', 'http://127.0.0.1'],
99
['/ip6/fc00::/tcp/80/http', 'http://[fc00::]'],
1010
['/ip4/0.0.7.6/tcp/1234', 'tcp://0.0.7.6:1234'],
@@ -13,28 +13,128 @@ describe('uri-to-multiaddr', () => {
1313
['/ip6/::/tcp/0', 'tcp://[::]:0'],
1414
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
1515
['/ip6/::/udp/0', 'udp://[::]:0'],
16-
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
17-
['/dns4/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
18-
['/dns4/protocol.ai/tcp/80/https', 'https://protocol.ai:80'],
19-
['/dns4/ipfs.io/tcp/80/ws', 'ws://ipfs.io'],
20-
['/dns4/ipfs.io/tcp/443/wss', 'wss://ipfs.io'],
21-
['/dns4/ipfs.io/tcp/80/http', 'http://ipfs.io'],
22-
['/dns4/ipfs.io/tcp/443/https', 'https://ipfs.io'],
23-
['/dns4/ipfs.io/tcp/443/https', 'https://ipfs.io:443'],
24-
['/dns4/ipfs.io/tcp/443', 'tcp://ipfs.io:443'],
25-
['/dns4/ipfs.io/tcp/80', 'tcp://ipfs.io:80'],
16+
['/dns/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
17+
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80', {
18+
defaultDnsType: 'dns4'
19+
}],
20+
['/dns6/protocol.ai/tcp/80', 'tcp://protocol.ai:80', {
21+
defaultDnsType: 'dns6'
22+
}],
23+
['/dnsaddr/protocol.ai/tcp/80', 'tcp://protocol.ai:80', {
24+
defaultDnsType: 'dnsaddr'
25+
}],
26+
['/dns/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
27+
['/dns4/protocol.ai/tcp/80/http', 'http://protocol.ai:80', {
28+
defaultDnsType: 'dns4'
29+
}],
30+
['/dns6/protocol.ai/tcp/80/http', 'http://protocol.ai:80', {
31+
defaultDnsType: 'dns6'
32+
}],
33+
['/dnsaddr/protocol.ai/tcp/80/http', 'http://protocol.ai:80', {
34+
defaultDnsType: 'dnsaddr'
35+
}],
36+
['/dns/protocol.ai/tcp/80/https', 'https://protocol.ai:80'],
37+
['/dns4/protocol.ai/tcp/80/https', 'https://protocol.ai:80', {
38+
defaultDnsType: 'dns4'
39+
}],
40+
['/dns6/protocol.ai/tcp/80/https', 'https://protocol.ai:80', {
41+
defaultDnsType: 'dns6'
42+
}],
43+
['/dnsaddr/protocol.ai/tcp/80/https', 'https://protocol.ai:80', {
44+
defaultDnsType: 'dnsaddr'
45+
}],
46+
['/dns/ipfs.io/tcp/80/ws', 'ws://ipfs.io'],
47+
['/dns4/ipfs.io/tcp/80/ws', 'ws://ipfs.io', {
48+
defaultDnsType: 'dns4'
49+
}],
50+
['/dns6/ipfs.io/tcp/80/ws', 'ws://ipfs.io', {
51+
defaultDnsType: 'dns6'
52+
}],
53+
['/dnsaddr/ipfs.io/tcp/80/ws', 'ws://ipfs.io', {
54+
defaultDnsType: 'dnsaddr'
55+
}],
56+
['/dns/ipfs.io/tcp/443/wss', 'wss://ipfs.io'],
57+
['/dns4/ipfs.io/tcp/443/wss', 'wss://ipfs.io', {
58+
defaultDnsType: 'dns4'
59+
}],
60+
['/dns6/ipfs.io/tcp/443/wss', 'wss://ipfs.io', {
61+
defaultDnsType: 'dns6'
62+
}],
63+
['/dnsaddr/ipfs.io/tcp/443/wss', 'wss://ipfs.io', {
64+
defaultDnsType: 'dnsaddr'
65+
}],
66+
['/dns/ipfs.io/tcp/80/http', 'http://ipfs.io'],
67+
['/dns4/ipfs.io/tcp/80/http', 'http://ipfs.io', {
68+
defaultDnsType: 'dns4'
69+
}],
70+
['/dns6/ipfs.io/tcp/80/http', 'http://ipfs.io', {
71+
defaultDnsType: 'dns6'
72+
}],
73+
['/dnsaddr/ipfs.io/tcp/80/http', 'http://ipfs.io', {
74+
defaultDnsType: 'dnsaddr'
75+
}],
76+
['/dns/ipfs.io/tcp/443/https', 'https://ipfs.io'],
77+
['/dns4/ipfs.io/tcp/443/https', 'https://ipfs.io', {
78+
defaultDnsType: 'dns4'
79+
}],
80+
['/dns6/ipfs.io/tcp/443/https', 'https://ipfs.io', {
81+
defaultDnsType: 'dns6'
82+
}],
83+
['/dnsaddr/ipfs.io/tcp/443/https', 'https://ipfs.io', {
84+
defaultDnsType: 'dnsaddr'
85+
}],
86+
['/dns/ipfs.io/tcp/443/https', 'https://ipfs.io:443'],
87+
['/dns4/ipfs.io/tcp/443/https', 'https://ipfs.io:443', {
88+
defaultDnsType: 'dns4'
89+
}],
90+
['/dns6/ipfs.io/tcp/443/https', 'https://ipfs.io:443', {
91+
defaultDnsType: 'dns6'
92+
}],
93+
['/dnsaddr/ipfs.io/tcp/443/https', 'https://ipfs.io:443', {
94+
defaultDnsType: 'dnsaddr'
95+
}],
96+
['/dns/ipfs.io/tcp/443', 'tcp://ipfs.io:443'],
97+
['/dns4/ipfs.io/tcp/443', 'tcp://ipfs.io:443', {
98+
defaultDnsType: 'dns4'
99+
}],
100+
['/dns6/ipfs.io/tcp/443', 'tcp://ipfs.io:443', {
101+
defaultDnsType: 'dns6'
102+
}],
103+
['/dnsaddr/ipfs.io/tcp/443', 'tcp://ipfs.io:443', {
104+
defaultDnsType: 'dnsaddr'
105+
}],
106+
['/dns/ipfs.io/tcp/80', 'tcp://ipfs.io:80'],
107+
['/dns4/ipfs.io/tcp/80', 'tcp://ipfs.io:80', {
108+
defaultDnsType: 'dns4'
109+
}],
110+
['/dns6/ipfs.io/tcp/80', 'tcp://ipfs.io:80', {
111+
defaultDnsType: 'dns6'
112+
}],
113+
['/dnsaddr/ipfs.io/tcp/80', 'tcp://ipfs.io:80', {
114+
defaultDnsType: 'dnsaddr'
115+
}],
26116
['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'],
27117
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
28118
['/ip6/::/tcp/0/ws', 'ws://[::]:0'],
29119
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
30120
['/ip6/::/tcp/0/wss', 'wss://[::]:0'],
31-
['/dns4/example.com/tcp/443/wss/http-path/foo', 'wss://example.com:443/foo']
121+
['/dns/example.com/tcp/443/wss/http-path/foo', 'wss://example.com:443/foo'],
122+
['/dns4/example.com/tcp/443/wss/http-path/foo', 'wss://example.com:443/foo', {
123+
defaultDnsType: 'dns4'
124+
}],
125+
['/dns6/example.com/tcp/443/wss/http-path/foo', 'wss://example.com:443/foo', {
126+
defaultDnsType: 'dns6'
127+
}],
128+
['/dnsaddr/example.com/tcp/443/wss/http-path/foo', 'wss://example.com:443/foo', {
129+
defaultDnsType: 'dnsaddr'
130+
}]
32131
]
33132

34133
data.forEach(d => {
35134
const input = d[1]
36135
const expected = d[0]
37-
const output = uriToMultiaddr(input).toString()
136+
const opts = d[2]
137+
const output = uriToMultiaddr(input, opts).toString()
38138
expect(output).to.equal(expected, `Converts ${input} to ${expected}`)
39139
})
40140
})

0 commit comments

Comments
 (0)