Skip to content

Commit 82540e7

Browse files
committed
fix: origin relative api detection
- 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]>
1 parent 3011e67 commit 82540e7

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ const Multiaddr = require('multiaddr')
1212
// [udp, foobar.com, 8080] => /dnsaddr/foobar.com/udp/8080
1313

1414
function multiaddrFromUri (uriStr) {
15-
const { protocol, hostname } = new url.URL(uriStr)
16-
// WHATWG URL hides port when it's the default for the scheme...
17-
const port = url.parse(uriStr).port
18-
const scheme = protocol.slice(0, -1)
15+
const { scheme, hostname, port } = parseUri(uriStr)
1916
const parts = [
2017
tupleForHostname(hostname),
2118
tupleForPort(port, scheme),
@@ -29,6 +26,15 @@ function multiaddrFromUri (uriStr) {
2926
return Multiaddr(multiaddrStr)
3027
}
3128

29+
function parseUri (uriStr) {
30+
// Use the WHATWG URL global, in node >= 10 and the browser
31+
const { protocol, hostname } = new URL(uriStr)
32+
// WHATWG URL hides port when it's the default for the scheme...
33+
const port = url.parse(uriStr).port
34+
const scheme = protocol.slice(0, -1)
35+
return { scheme, hostname, port }
36+
}
37+
3238
function tupleForHostname (hostname) {
3339
if (!hostname) throw new Error('hostname is requried')
3440
if (isIp.v4(hostname)) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"devDependencies": {
1212
"standard": "^12.0.1"
1313
},
14+
"engines": {
15+
"node": ">=10"
16+
},
1417
"scripts": {
1518
"test": "ava test.js"
1619
},

0 commit comments

Comments
 (0)