Skip to content

Commit 24460e7

Browse files
authored
Merge pull request #2 from tableflip/default-to-dns4
fix: assume dns4 for domain names
2 parents 96776ec + 4f82773 commit 24460e7

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/tableflip/uri-to-multiaddr.svg?branch=master)](https://travis-ci.org/tableflip/uri-to-multiaddr) [![dependencies Status](https://david-dm.org/tableflip/uri-to-multiaddr/status.svg)](https://david-dm.org/tableflip/uri-to-multiaddr) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
44

5-
> Convert a URI to a [Multiaddr](https://multiformats.io/multiaddr/): https://protocol.ai -> /dnsaddr/protocol.ai/https
5+
> Convert a URI to a [Multiaddr](https://multiformats.io/multiaddr/): https://protocol.ai -> /dns4/protocol.ai/https
66
77
## Install
88

@@ -16,14 +16,31 @@ npm install uri-to-multiaddr
1616
const toMultiaddr = require('uri-to-multiaddr')
1717

1818
console.log(toMultiaddr('https://protocol.ai'))
19+
// -> /dns4/protocol.ai/https
20+
```
21+
22+
Domain names can represent one of
23+
24+
- `/dns4` - domain resolves to an ipv4 address (**default**)
25+
- `/dns6` - domain resolves to an ipv6 address
26+
- `/dnsaddr` - domain has a [DNSLink](https://docs.ipfs.io/guides/concepts/dnslink/) TXT record pointing to an IPFS CID
27+
28+
This library assumes `/dns4` when it finds a domain name in the input string.
29+
It makes no attempt query DNS. To override the default assumption, you can pass
30+
in an options object as the second parameter to override it:
31+
32+
```js
33+
const toMultiaddr = require('uri-to-multiaddr')
34+
35+
console.log(toMultiaddr('https://protocol.ai'), {defaultDnsType: 'dnsaddr'})
1936
// -> /dnsaddr/protocol.ai/https
2037
```
2138

22-
* See [test.js](./test.js) for the currently supported conversions.
23-
* Might be lossy - e.g. a DNSv6 multiaddr
24-
* Can throw if the passed URI:
25-
* is not a valid
26-
* is not supported yet e.g. quic
39+
See [test.js](./test.js) for the currently supported conversions.
40+
41+
**Note**: `uri-to-multiaddr` will throw if the passed URI:
42+
- is not a valid, according the WHATWG URL spec implementation used.
43+
- is not supported yet e.g. quic
2744

2845
## Related
2946

@@ -35,4 +52,4 @@ Feel free to dive in! [Open an issue](https://github.com/tableflip/uri-to-multia
3552

3653
## License
3754

38-
[MIT](LICENSE) © TABLEFLIP
55+
[MIT](LICENSE) © TABLEFLIP

index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ const url = require('url')
22
const isIp = require('is-ip')
33
const Multiaddr = require('multiaddr')
44

5-
// Convert a URI to a multiaddr
6-
//
7-
// [https, foobar.com] => /dnsaddr/foobar.com/https
8-
// [https, foobar.com, 8080] => /dnsaddr/foobar.com/tcp/8080/https
9-
// [ws, foobar.com] => /dnsaddr/foobar.com/ws
10-
// [https, 127.0.0.1, 8080] => /ip4/127.0.0.1/tcp/8080/https
11-
// [tcp, foobar.com, 8080] => /dnsaddr/foobar.com/tcp/8080
12-
// [udp, foobar.com, 8080] => /dnsaddr/foobar.com/udp/8080
5+
/**
6+
* Convert a URI to a multiaddr
7+
*
8+
* http://foobar.com => /dns4/foobar.com/https
9+
* https://foobar.com:8080 => /dns4/foobar.com/tcp/8080/https
10+
* ws://foobar.com => /dns4/foobar.com/ws
11+
* https://127.0.0.1:8080 => /ip4/127.0.0.1/tcp/8080/https
12+
* http://[::1]:8080 => /ip6/::1/tcp/8080/http
13+
* tcp://foobar.com:8080 => /dns4/foobar.com/tcp/8080
14+
* udp://foobar.com:8080 => /dns4/foobar.com/udp/8080
15+
*/
1316

14-
function multiaddrFromUri (uriStr) {
17+
function multiaddrFromUri (uriStr, opts) {
18+
opts = opts || {}
19+
const defaultDnsType = opts.defaultDnsType || 'dns4'
1520
const { scheme, hostname, port } = parseUri(uriStr)
1621
const parts = [
17-
tupleForHostname(hostname),
22+
tupleForHostname(hostname, defaultDnsType),
1823
tupleForPort(port, scheme),
1924
tupleForScheme(scheme)
2025
]
@@ -35,7 +40,7 @@ function parseUri (uriStr) {
3540
return { scheme, hostname, port }
3641
}
3742

38-
function tupleForHostname (hostname) {
43+
function tupleForHostname (hostname, defaultDnsType) {
3944
if (!hostname) throw new Error('hostname is requried')
4045
if (isIp.v4(hostname)) {
4146
return ['ip4', hostname]
@@ -51,8 +56,8 @@ function tupleForHostname (hostname) {
5156
return ['ip6', trimmed]
5257
}
5358
}
54-
// assumes that any non-ip hostname is a dns address.
55-
return ['dnsaddr', hostname]
59+
// assumes that any non-ip hostname is a dns4 address.
60+
return [defaultDnsType, hostname]
5661
}
5762

5863
function tupleForPort (port, scheme) {

test.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,32 @@ test('should convert URIs to multiaddrs', (t) => {
1111
['/ip6/::/tcp/0', 'tcp://[::]:0'],
1212
['/ip4/0.0.7.6/udp/1234', 'udp://0.0.7.6:1234'],
1313
['/ip6/::/udp/0', 'udp://[::]:0'],
14-
['/dnsaddr/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
15-
['/dnsaddr/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
16-
['/dnsaddr/protocol.ai/tcp/80/https', 'https://protocol.ai:80'],
17-
['/dnsaddr/ipfs.io/ws', 'ws://ipfs.io'],
18-
['/dnsaddr/ipfs.io/http', 'http://ipfs.io'],
19-
['/dnsaddr/ipfs.io/https', 'https://ipfs.io'],
14+
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
15+
['/dns4/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
16+
['/dns4/protocol.ai/tcp/80/https', 'https://protocol.ai:80'],
17+
['/dns4/ipfs.io/ws', 'ws://ipfs.io'],
18+
['/dns4/ipfs.io/http', 'http://ipfs.io'],
19+
['/dns4/ipfs.io/https', 'https://ipfs.io'],
2020
['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'],
2121
['/ip6/::/tcp/0/ws', 'ws://[::]:0'],
22-
['/dnsaddr/ipfs.io/wss', 'wss://ipfs.io'],
22+
['/dns4/ipfs.io/wss', 'wss://ipfs.io'],
2323
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
2424
['/ip6/::/tcp/0/wss', 'wss://[::]:0']
2525
]
2626

2727
data.forEach(d => t.is(toMultiaddr(d[1]).toString(), d[0], `Converts ${d[1]} to ${d[0]}`))
2828
})
2929

30+
test('should use the defaultDnsType where provided', (t) => {
31+
const data = [
32+
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80', { defaultDnsType: 'dns4' }],
33+
['/dns6/protocol.ai/tcp/80/http', 'http://protocol.ai:80', { defaultDnsType: 'dns6' }],
34+
['/dnsaddr/protocol.ai/tcp/80/https', 'https://protocol.ai:80', { defaultDnsType: 'dnsaddr' }]
35+
]
36+
37+
data.forEach(d => t.is(toMultiaddr(d[1], d[2]).toString(), d[0], `Converts ${d[1]} to ${d[0]} with opts ${d[2]}`))
38+
})
39+
3040
test('should throw for unsupported protocol', (t) => {
3141
t.throws(() => toMultiaddr('quic://'))
3242
})

0 commit comments

Comments
 (0)