Skip to content

Commit 7be0e3f

Browse files
authored
Merge pull request #3 from tableflip/always-add-port
fix: add default port for protocol
2 parents 9bee738 + 63dda3a commit 7be0e3f

File tree

5 files changed

+61
-95
lines changed

5 files changed

+61
-95
lines changed

README.md

Lines changed: 5 additions & 5 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 -> /dns4/protocol.ai/https
5+
> Convert a URI to a [Multiaddr](https://multiformats.io/multiaddr/): https://protocol.ai -> /dns4/protocol.ai/tcp/443/https
66
77
## Install
88

@@ -16,7 +16,7 @@ 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
19+
// -> /dns4/protocol.ai/tcp/443/https
2020
```
2121

2222
Domain names can represent one of
@@ -32,15 +32,15 @@ in an options object as the second parameter to override it:
3232
```js
3333
const toMultiaddr = require('uri-to-multiaddr')
3434

35-
console.log(toMultiaddr('https://protocol.ai'), {defaultDnsType: 'dnsaddr'})
36-
// -> /dnsaddr/protocol.ai/https
35+
console.log(toMultiaddr('https://protocol.ai'), { defaultDnsType: 'dns6' })
36+
// -> /dns6/protocol.ai/tcp/443/https
3737
```
3838

3939
See [test.js](./test.js) for the currently supported conversions.
4040

4141
**Note**: `uri-to-multiaddr` will throw if the passed URI:
4242
- is not a valid, according the WHATWG URL spec implementation used.
43-
- is not supported yet e.g. quic
43+
- is not supported yet
4444

4545
## Related
4646

index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
const url = require('url')
21
const isIp = require('is-ip')
32
const Multiaddr = require('multiaddr')
43

54
/**
65
* Convert a URI to a multiaddr
76
*
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
7+
* http://foobar.com => /dns4/foobar.com/tcp/80/http
8+
* https://foobar.com => /dns4/foobar.com/tcp/443/https
9+
* https://foobar.com:5001 => /dns4/foobar.com/tcp/5001/https
1110
* https://127.0.0.1:8080 => /ip4/127.0.0.1/tcp/8080/https
1211
* http://[::1]:8080 => /ip6/::1/tcp/8080/http
1312
* tcp://foobar.com:8080 => /dns4/foobar.com/tcp/8080
@@ -33,15 +32,16 @@ function multiaddrFromUri (uriStr, opts) {
3332

3433
function parseUri (uriStr) {
3534
// Use the WHATWG URL global, in node >= 10 and the browser
36-
const { protocol, hostname } = new URL(uriStr)
37-
// WHATWG URL hides port when it's the default for the scheme...
38-
const port = url.parse(uriStr).port
35+
let { protocol, hostname, port } = new URL(uriStr)
3936
const scheme = protocol.slice(0, -1)
37+
if (!port && port !== 0) {
38+
port = portForProtocol(scheme)
39+
}
4040
return { scheme, hostname, port }
4141
}
4242

4343
function tupleForHostname (hostname, defaultDnsType) {
44-
if (!hostname) throw new Error('hostname is requried')
44+
if (!hostname) return null
4545
if (isIp.v4(hostname)) {
4646
return ['ip4', hostname]
4747
}
@@ -69,10 +69,21 @@ function tupleForPort (port, scheme) {
6969
}
7070

7171
function tupleForScheme (scheme) {
72-
if (scheme.match(/^https?$|^wss?$/)) {
73-
return [scheme]
72+
if (scheme.match(/^tcp$|^udp$/)) {
73+
return null
74+
}
75+
return [scheme]
76+
}
77+
78+
function portForProtocol (protocol) {
79+
if (!protocol) return null
80+
const portFor = {
81+
http: '80',
82+
https: '443',
83+
ws: '80',
84+
wss: '443'
7485
}
75-
return null
86+
return portFor[protocol]
7687
}
7788

7889
module.exports = multiaddrFromUri

package-lock.json

Lines changed: 9 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dependencies": {
77
"ava": "^0.25.0",
88
"is-ip": "^2.0.0",
9-
"multiaddr": "^5.0.0"
9+
"multiaddr": "^6.0.3"
1010
},
1111
"devDependencies": {
1212
"standard": "^12.0.1"

test.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const toMultiaddr = require('./')
33

44
test('should convert URIs to multiaddrs', (t) => {
55
const data = [
6-
['/ip4/127.0.0.1/http', 'http://127.0.0.1'],
7-
['/ip6/fc00::/http', 'http://[fc00::]'],
6+
['/ip4/127.0.0.1/tcp/80/http', 'http://127.0.0.1'],
7+
['/ip6/fc00::/tcp/80/http', 'http://[fc00::]'],
88
['/ip4/0.0.7.6/tcp/1234', 'tcp://0.0.7.6:1234'],
99
['/ip4/0.0.7.6/tcp/1234/http', 'http://0.0.7.6:1234'],
1010
['/ip4/0.0.7.6/tcp/1234/https', 'https://0.0.7.6:1234'],
@@ -14,17 +14,22 @@ test('should convert URIs to multiaddrs', (t) => {
1414
['/dns4/protocol.ai/tcp/80', 'tcp://protocol.ai:80'],
1515
['/dns4/protocol.ai/tcp/80/http', 'http://protocol.ai:80'],
1616
['/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'],
17+
['/dns4/ipfs.io/tcp/80/ws', 'ws://ipfs.io'],
18+
['/dns4/ipfs.io/tcp/443/wss', 'wss://ipfs.io'],
19+
['/dns4/ipfs.io/tcp/80/http', 'http://ipfs.io'],
20+
['/dns4/ipfs.io/tcp/443/https', 'https://ipfs.io'],
2021
['/ip4/1.2.3.4/tcp/3456/ws', 'ws://1.2.3.4:3456'],
22+
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
2123
['/ip6/::/tcp/0/ws', 'ws://[::]:0'],
22-
['/dns4/ipfs.io/wss', 'wss://ipfs.io'],
2324
['/ip4/1.2.3.4/tcp/3456/wss', 'wss://1.2.3.4:3456'],
2425
['/ip6/::/tcp/0/wss', 'wss://[::]:0']
2526
]
26-
27-
data.forEach(d => t.is(toMultiaddr(d[1]).toString(), d[0], `Converts ${d[1]} to ${d[0]}`))
27+
data.forEach(d => {
28+
const input = d[1]
29+
const expected = d[0]
30+
const output = toMultiaddr(input).toString()
31+
t.is(output, expected, `Converts ${input} to ${expected}`)
32+
})
2833
})
2934

3035
test('should use the defaultDnsType where provided', (t) => {
@@ -37,6 +42,15 @@ test('should use the defaultDnsType where provided', (t) => {
3742
data.forEach(d => t.is(toMultiaddr(d[1], d[2]).toString(), d[0], `Converts ${d[1]} to ${d[0]} with opts ${d[2]}`))
3843
})
3944

40-
test('should throw for unsupported protocol', (t) => {
41-
t.throws(() => toMultiaddr('quic://'))
45+
test('should throw for on invalid url', (t) => {
46+
t.throws(() => {
47+
toMultiaddr('whoosh.fast')
48+
}, /Invalid URL/)
49+
})
50+
51+
test('should throw for unknown protocol', (t) => {
52+
t.throws(() => {
53+
// NOTE: `data` is a valid uri protocol but isn't a valid multiaddr protocol yet
54+
toMultiaddr('data:image/svg+xml;base64,test')
55+
}, 'no protocol with name: data')
4256
})

0 commit comments

Comments
 (0)