Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 52f440a

Browse files
committed
fix: Support api endpoint with no port defined
1 parent a869ae0 commit 52f440a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"detect-node": "^2.0.3",
3434
"flatmap": "0.0.3",
3535
"glob": "^7.1.2",
36+
"ip-regex": "^3.0.0",
3637
"ipfs-block": "~0.7.1",
3738
"ipfs-unixfs": "~0.1.14",
3839
"ipld-dag-cbor": "~0.12.0",

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const ipRegex = require('ip-regex')
34
const multiaddr = require('multiaddr')
45
const loadCommands = require('./utils/load-commands')
56
const getConfig = require('./utils/default-config')
@@ -15,7 +16,14 @@ function IpfsAPI (hostOrMultiaddr, port, opts) {
1516
} catch (e) {
1617
if (typeof hostOrMultiaddr === 'string') {
1718
config.host = hostOrMultiaddr
18-
config.port = port && typeof port !== 'object' ? port : config.port
19+
const addrParts = hostOrMultiaddr.split(':')
20+
21+
if (addrParts.length === 2 && ipRegex.v4({ exact: true }).test(addrParts[0]) &&
22+
(!port || typeof port === 'object')) {
23+
config.port = undefined
24+
} else {
25+
config.port = port
26+
}
1927
}
2028
}
2129

test/constructor.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ describe('ipfs-api constructor tests', () => {
5050
clientWorks(ipfsAPI(apiAddr, { protocol: 'http' }), done)
5151
})
5252

53+
it('host', (done) => {
54+
const splitted = apiAddr.split('/')
55+
56+
clientWorks(ipfsAPI(splitted[2]), done)
57+
})
58+
5359
it('host, port', (done) => {
5460
const splitted = apiAddr.split('/')
5561

0 commit comments

Comments
 (0)