From 41d32e302372bb5a1b2bd36a439bdada0404fa12 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 3 Apr 2018 16:12:07 +0100 Subject: [PATCH] feat: public-readonly-method-for-getting-host-and-port Closes #580 --- README.md | 6 ++++++ src/index.js | 2 +- src/util/get-endpoint-config.js | 8 ++++++++ src/utils/load-commands.js | 9 +++++---- test/util.spec.js | 9 +++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/util/get-endpoint-config.js diff --git a/README.md b/README.md index e7b7b05f1..392423ced 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,12 @@ ipfs.util.addFromStream(, (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: diff --git a/src/index.js b/src/index.js index b56a521a9..70ed59097 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/src/util/get-endpoint-config.js b/src/util/get-endpoint-config.js new file mode 100644 index 000000000..7598239ee --- /dev/null +++ b/src/util/get-endpoint-config.js @@ -0,0 +1,8 @@ +'use strict' + +module.exports = (config) => { + return () => ({ + host: config.host, + port: config.port + }) +} diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index e86982e18..e9494055e 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -55,11 +55,12 @@ 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 } @@ -67,12 +68,12 @@ function requireCommands () { 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 diff --git a/test/util.spec.js b/test/util.spec.js index 766fd3a65..0c15f3347 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -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') + }) + }) })