Skip to content

Commit dae8350

Browse files
authored
fix!: always return a URI for DNS addresses (#130)
`/dns` and friends should assume a HTTP URI, so always return strings prefixed with `http(s)`. Fixes #8 BREAKING CHANGE: dns addresses previously returned domain names, now they return URLs
1 parent 1f138ab commit dae8350

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/index.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export interface MultiaddrToUriOpts {
3636
assumeHttp?: boolean
3737
}
3838

39+
const ASSUME_HTTP_CODES = [
40+
protocols('tcp').code,
41+
protocols('dns').code,
42+
protocols('dnsaddr').code,
43+
protocols('dns4').code,
44+
protocols('dns6').code
45+
]
46+
3947
interface Interpreter { (value: string, ma: StringTuple[]): string }
4048

4149
function extractSNI (ma: StringTuple[]): string | null {
@@ -219,15 +227,25 @@ export function multiaddrToUri (input: Multiaddr | string | Uint8Array, opts?: M
219227
}
220228

221229
let uri = interpreter(head[1] ?? '', parts)
222-
if (opts?.assumeHttp !== false && head[0] === protocols('tcp').code) {
223-
// If rightmost proto is tcp, we assume http here
224-
uri = uri.replace('tcp://', 'http://')
225-
if (head[1] === '443' || head[1] === '80') {
226-
if (head[1] === '443') {
227-
uri = uri.replace('http://', 'https://')
228-
}
229-
// Drop the port
230-
uri = uri.substring(0, uri.lastIndexOf(':'))
230+
231+
if (opts?.assumeHttp !== false && ASSUME_HTTP_CODES.includes(head[0])) {
232+
// strip any declared protocol
233+
uri = uri.replace(/^.*:\/\//, '')
234+
235+
if (head[1] === '443') {
236+
uri = `https://${uri}`
237+
} else {
238+
uri = `http://${uri}`
239+
}
240+
}
241+
242+
if (uri.startsWith('http://') || uri.startsWith('https://')) {
243+
// this will strip default ports while keeping paths intact
244+
uri = new URL(uri).toString()
245+
246+
// strip trailing slash, e.g. http://127.0.0.1/ -> http://127.0.0.1
247+
if (uri.endsWith('/')) {
248+
uri = uri.substring(0, uri.length - 1)
231249
}
232250
}
233251

test/test.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ describe('multiaddr-to-uri', () => {
1616
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
1717
['/ip6/::/udp/0', 'udp://[::]:0'],
1818
['/dns/a.com/tcp/1234', 'http://a.com:1234'],
19-
['/dns/a.com', 'a.com'],
20-
['/dnsaddr/ipfs.io', 'ipfs.io'],
21-
['/dns4/ipfs.io', 'ipfs.io'],
22-
['/dns4/libp2p.io', 'libp2p.io'],
23-
['/dns6/protocol.ai', 'protocol.ai'],
24-
['/dnsaddr/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
19+
['/dns/a.com/tcp/1234/https', 'https://a.com:1234'],
20+
['/dns/a.com/tcp/1234/tls/http', 'https://a.com:1234'],
21+
['/dns/a.com/tcp/80', 'http://a.com'],
22+
['/dns/a.com/tcp/443', 'https://a.com'],
23+
['/dns/a.com', 'http://a.com'],
24+
['/dnsaddr/ipfs.io', 'http://ipfs.io'],
25+
['/dns4/ipfs.io', 'http://ipfs.io'],
26+
['/dns4/libp2p.io', 'http://libp2p.io'],
27+
['/dns6/protocol.ai', 'http://protocol.ai'],
28+
['/dnsaddr/protocol.ai/tcp/80/http', 'http://protocol.ai'],
2529
['/dnsaddr/protocol.ai/tcp/80/https', 'https://protocol.ai:80'],
2630
['/dnsaddr/ipfs.io/ws', 'ws://ipfs.io'],
2731
['/dnsaddr/ipfs.io/http', 'http://ipfs.io'],

0 commit comments

Comments
 (0)