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

Commit

Permalink
feat: public-readonly-method-for-getting-host-and-port
Browse files Browse the repository at this point in the history
Closes #580
  • Loading branch information
vasco-santos authored and daviddias committed Apr 3, 2018
1 parent b650e6c commit 41d32e3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ ipfs.util.addFromStream(<readable-stream>, (err, result) => {
})
```
##### Get endpoint configuration (host and port)
> `ipfs.util.getEndpointConfig()`
This returns an object containing the `host` and the `port`
### Callbacks and Promises
If you do not pass in a callback all API functions will return a `Promise`. For example:
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function IpfsAPI (hostOrMultiaddr, port, opts) {
}

const requestAPI = sendRequest(config)
const cmds = loadCommands(requestAPI)
const cmds = loadCommands(requestAPI, config)
cmds.send = requestAPI
cmds.Buffer = Buffer

Expand Down
8 changes: 8 additions & 0 deletions src/util/get-endpoint-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = (config) => {
return () => ({
host: config.host,
port: config.port
})
}
9 changes: 5 additions & 4 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,25 @@ function requireCommands () {
return files
}

cmds.util = function (send) {
cmds.util = function (send, config) {
const util = {
addFromFs: require('../util/fs-add')(send),
addFromStream: require('../files/add')(send),
addFromURL: require('../util/url-add')(send)
addFromURL: require('../util/url-add')(send),
getEndpointConfig: require('../util/get-endpoint-config')(config)
}
return util
}

return cmds
}

function loadCommands (send) {
function loadCommands (send, config) {
const files = requireCommands()
const cmds = {}

Object.keys(files).forEach((file) => {
cmds[file] = files[file](send)
cmds[file] = files[file](send, config)
})

return cmds
Expand Down
9 changes: 9 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,13 @@ describe('.util', () => {
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
})
})

describe('.getEndpointConfig', () => {
it('should return the endpoint configured host and port', function () {
const endpoint = ipfs.util.getEndpointConfig()

expect(endpoint).to.have.property('host')
expect(endpoint).to.have.property('port')
})
})
})

0 comments on commit 41d32e3

Please sign in to comment.