Skip to content

Commit

Permalink
fix: origin relative api detection
Browse files Browse the repository at this point in the history
- Use the gloabl URL object. We hit webpack/node-libs-browser#69
- This will only work in the browser and node >= 10 now.

License: MIT
Signed-off-by: Oli Evans <[email protected]>
  • Loading branch information
olizilla committed Oct 23, 2018
1 parent 3011e67 commit 82540e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const Multiaddr = require('multiaddr')
// [udp, foobar.com, 8080] => /dnsaddr/foobar.com/udp/8080

function multiaddrFromUri (uriStr) {
const { protocol, hostname } = new url.URL(uriStr)
// WHATWG URL hides port when it's the default for the scheme...
const port = url.parse(uriStr).port
const scheme = protocol.slice(0, -1)
const { scheme, hostname, port } = parseUri(uriStr)
const parts = [
tupleForHostname(hostname),
tupleForPort(port, scheme),
Expand All @@ -29,6 +26,15 @@ function multiaddrFromUri (uriStr) {
return Multiaddr(multiaddrStr)
}

function parseUri (uriStr) {
// Use the WHATWG URL global, in node >= 10 and the browser
const { protocol, hostname } = new URL(uriStr)
// WHATWG URL hides port when it's the default for the scheme...
const port = url.parse(uriStr).port
const scheme = protocol.slice(0, -1)
return { scheme, hostname, port }
}

function tupleForHostname (hostname) {
if (!hostname) throw new Error('hostname is requried')
if (isIp.v4(hostname)) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"devDependencies": {
"standard": "^12.0.1"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "ava test.js"
},
Expand Down

0 comments on commit 82540e7

Please sign in to comment.