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

Commit 0a0aa4a

Browse files
committed
config API + tests
1 parent 666bc94 commit 0a0aa4a

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed

API/config/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
config API
2+
==========
3+
4+
#### `config.get`
5+
6+
> Returns the currently being used config. If the daemon is off, it returns the stored config.
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.config.get([key, callback])
11+
12+
`key` is the key of the value that should be fetched from the config file. If no key is passed, then the whole config should be returned. `key` should be of type String.
13+
14+
`callback` must follow `function (err, config) {}` signature, where `err` is an error if the operation was not successful and `config` is a JSON object containing the configuration of the IPFS node.
15+
16+
If no callback is passed, a [promise][] is returned
17+
18+
#### `config.set`
19+
20+
> Adds or replaces a config value.
21+
22+
##### `Go` **WIP**
23+
24+
##### `JavaScript` - ipfs.config.set(key, value, [callback])
25+
26+
`key` is the key value that will be added or replaced (in case of the value already). `key` should be of type String.
27+
28+
`value` value to be set.
29+
30+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
31+
32+
If no callback is passed, a [promise][] is returned
33+
34+
Note that this operation will spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.
35+
36+
#### `config.replace`
37+
38+
> Adds or replaces a config value.
39+
40+
##### `Go` **WIP**
41+
42+
##### `JavaScript` - ipfs.config.replace(config, [callback])
43+
44+
`config` is a JSON object that contains the new config.
45+
46+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
47+
48+
If no callback is passed, a [promise][] is returned
49+
50+
Note that this operation will spark the restart of any service, i.e: if a config.replace changes the multiaddrs of the Swarm, Swarm will have to be restarted manually for the changes to take difference.
51+
52+
[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

src/config.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
6+
module.exports = (common) => {
7+
describe('.config', () => {
8+
let ipfs
9+
10+
before((done) => {
11+
common.setup((err, _ipfs) => {
12+
expect(err).to.not.exist
13+
ipfs = _ipfs
14+
done()
15+
})
16+
})
17+
18+
after((done) => {
19+
common.teardown(done)
20+
})
21+
22+
describe('callback API', () => {
23+
describe('.get', () => {
24+
it('retrieve the whole config', (done) => {
25+
ipfs.config.get((err, config) => {
26+
expect(err).to.not.exist
27+
expect(config).to.exist
28+
done()
29+
})
30+
})
31+
32+
it('retrieve a value through a key', (done) => {
33+
ipfs.config.get('Identity', (err, identity) => {
34+
expect(err).to.not.exist
35+
expect(identity).to.exist
36+
done()
37+
})
38+
})
39+
40+
it('retrieve a value through a nested key', (done) => {
41+
ipfs.config.get('Addresses.Swarm', (err, swarmAddrs) => {
42+
expect(err).to.not.exist
43+
expect(swarmAddrs).to.exist
44+
done()
45+
})
46+
})
47+
48+
it('fail on non valid key', (done) => {
49+
ipfs.config.get(1234, (err, peerId) => {
50+
expect(err).to.exist
51+
done()
52+
})
53+
})
54+
55+
it('fail on non existent key', (done) => {
56+
ipfs.config.get('Bananas', (err, peerId) => {
57+
expect(err).to.exist
58+
done()
59+
})
60+
})
61+
})
62+
describe('.set', () => {
63+
it('set a new key', (done) => {
64+
ipfs.config.set('Fruit', 'banana', (err) => {
65+
expect(err).to.not.exist
66+
ipfs.config.get('Fruit', (err, fruit) => {
67+
expect(err).to.not.exist
68+
expect(fruit).to.equal('banana')
69+
done()
70+
})
71+
})
72+
})
73+
74+
it('set an already existing key', (done) => {
75+
ipfs.config.set('Fruit', 'morango', (err) => {
76+
expect(err).to.not.exist
77+
ipfs.config.get('Fruit', (err, fruit) => {
78+
expect(err).to.not.exist
79+
expect(fruit).to.equal('morango')
80+
done()
81+
})
82+
})
83+
})
84+
85+
it('set a json object', (done) => {
86+
const key = 'API.HTTPHeaders.Access-Control-Allow-Origin'
87+
const val = ['http://example.io']
88+
ipfs.config.set(key, val, function (err) {
89+
expect(err).to.not.exist
90+
ipfs.config.get(key, function (err, result) {
91+
expect(err).to.not.exist
92+
expect(result).to.deep.equal(val)
93+
done()
94+
})
95+
})
96+
})
97+
98+
it('fail on non valid key', (done) => {
99+
ipfs.config.set(new Buffer('heeey'), '', (err) => {
100+
expect(err).to.exist
101+
done()
102+
})
103+
})
104+
105+
it('fail on non valid value', (done) => {
106+
ipfs.config.set('Fruit', new Buffer('abc'), (err) => {
107+
expect(err).to.exist
108+
done()
109+
})
110+
})
111+
})
112+
113+
// Waiting for fix on go-ipfs
114+
// - https://github.com/ipfs/js-ipfs-api/pull/307#discussion_r69281789
115+
// - https://github.com/ipfs/go-ipfs/issues/2927
116+
describe.skip('.replace', () => {
117+
const config = {
118+
Fruit: 'Bananas'
119+
}
120+
121+
it('replace the whole config', (done) => {
122+
ipfs.config.replace(config, (err) => {
123+
expect(err).to.not.exist
124+
ipfs.config.get((err, _config) => {
125+
expect(err).to.not.exist
126+
expect(_config).to.deep.equal(config)
127+
})
128+
})
129+
})
130+
131+
it('replace to empty config', (done) => {
132+
ipfs.config.replace({}, (err) => {
133+
expect(err).to.not.exist
134+
ipfs.config.get((err, _config) => {
135+
expect(err).to.not.exist
136+
expect(_config).to.deep.equal(config)
137+
})
138+
})
139+
})
140+
})
141+
})
142+
143+
describe('promise API', () => {
144+
describe('.get', () => {
145+
it('retrieve the whole config', (done) => {
146+
ipfs.config.get()
147+
.then((config) => {
148+
expect(config).to.exist
149+
done()
150+
})
151+
.catch((err) => {
152+
expect(err).to.not.exist
153+
})
154+
})
155+
})
156+
157+
describe('.set', () => {
158+
it('set a new key', (done) => {
159+
ipfs.config.set('Fruit', 'banana')
160+
.then(() => {
161+
ipfs.config.get('Fruit', (err, fruit) => {
162+
expect(err).to.not.exist
163+
expect(fruit).to.equal('banana')
164+
done()
165+
})
166+
})
167+
.catch((err) => {
168+
expect(err).to.not.exist
169+
})
170+
})
171+
})
172+
173+
// Waiting for fix on go-ipfs
174+
// - https://github.com/ipfs/js-ipfs-api/pull/307#discussion_r69281789
175+
// - https://github.com/ipfs/go-ipfs/issues/2927
176+
describe.skip('.replace', () => {})
177+
})
178+
})
179+
}

src/index.js

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

3-
exports.all = () => {}
43
exports.object = require('./object')
54
exports.files = require('./files')
5+
exports.config = require('./config')

0 commit comments

Comments
 (0)