Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit b14e22f

Browse files
committed
make the config API spec compliant
1 parent 76bf64d commit b14e22f

File tree

10 files changed

+112
-152
lines changed

10 files changed

+112
-152
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"form-data": "^1.0.0-rc3",
4848
"gulp": "^3.9.1",
4949
"idb-plus-blob-store": "^1.1.2",
50-
"interface-ipfs-core": "^0.3.0",
50+
"interface-ipfs-core": "^0.4.3",
5151
"left-pad": "^1.1.0",
5252
"lodash": "^4.11.2",
5353
"mocha": "^2.5.1",
@@ -69,7 +69,7 @@
6969
"glob": "^7.0.3",
7070
"hapi": "^13.4.1",
7171
"ipfs-bitswap": "^0.6.0",
72-
"ipfs-api": "^6.0.1",
72+
"ipfs-api": "ipfs/js-ipfs-api#ebb7996",
7373
"ipfs-block": "^0.3.0",
7474
"ipfs-block-service": "^0.4.0",
7575
"ipfs-merkle-dag": "^0.6.0",
@@ -118,4 +118,4 @@
118118
"kumavis <[email protected]>",
119119
"nginnever <[email protected]>"
120120
]
121-
}
121+
}

src/cli/commands/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = Command.extend({
4646
})
4747
}
4848

49-
ipfs.config.show((err, config) => {
49+
ipfs.config.get((err, config) => {
5050
if (err) {
5151
log.error(err)
5252
throw new Error('failed to read the config')
@@ -78,7 +78,7 @@ module.exports = Command.extend({
7878
})
7979
}
8080

81-
ipfs.config.show((err, originalConfig) => {
81+
ipfs.config.get((err, originalConfig) => {
8282
if (err) {
8383
log.error(err)
8484
throw new Error('failed to read the config')

src/cli/commands/config/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = Command.extend({
2525
}
2626

2727
function getConfig (next) {
28-
ipfs.config.show((err, config) => {
28+
ipfs.config.get((err, config) => {
2929
if (err) {
3030
log.error(err)
3131
next(new Error('failed to get the config'))

src/cli/commands/config/show.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = Command.extend({
1616
if (err) {
1717
throw err
1818
}
19-
ipfs.config.show((err, config) => {
19+
ipfs.config.get((err, config) => {
2020
if (err) {
2121
throw err
2222
}

src/core/ipfs/config.js

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,86 @@
11
'use strict'
22

3+
const promisify = require('promisify-es6')
4+
35
module.exports = function config (self) {
46
return {
5-
// cli only feature built with show and replace
6-
// edit: (callback) => {},
7-
replace: (config, callback) => {
7+
get: promisify((key, callback) => {
8+
if (typeof key === 'function') {
9+
callback = key
10+
key = undefined
11+
}
12+
13+
if (!key) {
14+
return self._repo.config.get(callback)
15+
}
16+
17+
if (typeof key !== 'string') {
18+
return callback(new Error('Invalid key type'))
19+
}
20+
21+
self._repo.config.get((err, config) => {
22+
if (err) {
23+
return callback(err)
24+
}
25+
const keys = key.split('.')
26+
let finished = false
27+
keys.forEach((key) => {
28+
if (finished) {
29+
return
30+
}
31+
if (config[key]) {
32+
config = config[key]
33+
} else {
34+
finished = true
35+
callback(new Error(('Key does not exist in config')))
36+
}
37+
})
38+
if (!finished) {
39+
callback(null, config)
40+
}
41+
})
42+
}),
43+
set: promisify((key, value, callback) => {
44+
if (!key || typeof key !== 'string') {
45+
return callback(new Error('Invalid key type'))
46+
}
47+
48+
if (!value || Buffer.isBuffer(value)) {
49+
return callback(new Error('Invalid value type'))
50+
}
51+
52+
self._repo.config.get((err, config) => {
53+
const configBak = config
54+
if (err) {
55+
return callback(err)
56+
}
57+
const keys = key.split('.')
58+
let finished = false
59+
keys.forEach((key, index) => {
60+
if (finished) {
61+
return
62+
}
63+
if (config[key]) {
64+
if (index === keys.length - 1) {
65+
finished = true
66+
config[key] = value
67+
}
68+
config = config[key]
69+
} else {
70+
if (index === keys.length - 1) {
71+
finished = true
72+
config[key] = value
73+
} else {
74+
config = config[key] = {}
75+
}
76+
}
77+
})
78+
79+
self.config.replace(configBak, callback)
80+
})
81+
}),
82+
replace: promisify((config, callback) => {
883
self._repo.config.set(config, callback)
9-
},
10-
show: (callback) => {
11-
self._repo.config.get(callback)
12-
}
84+
})
1385
}
1486
}

src/http-api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ exports = module.exports = function HttpApi (repo) {
3838
fs.writeFileSync(apiPath, 'api is on by js-ipfs', {flag: 'w+'})
3939
}
4040

41-
this.ipfs.config.show((err, config) => {
41+
this.ipfs.config.get((err, config) => {
4242
if (err) {
4343
return callback(err)
4444
}

src/http-api/resources/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ exports.getOrSet = {
6060

6161
if (value === undefined) {
6262
// Get the value of a given key
63-
return request.server.app.ipfs.config.show((err, config) => {
63+
return request.server.app.ipfs.config.get((err, config) => {
6464
if (err) {
6565
log.error(err)
6666
return reply({
@@ -84,7 +84,7 @@ exports.getOrSet = {
8484
})
8585
} else {
8686
// Set the new value of a given key
87-
request.server.app.ipfs.config.show((err, originalConfig) => {
87+
request.server.app.ipfs.config.get((err, originalConfig) => {
8888
if (err) {
8989
log.error(err)
9090
return reply({
@@ -114,7 +114,7 @@ exports.getOrSet = {
114114
}
115115

116116
exports.show = (request, reply) => {
117-
return request.server.app.ipfs.config.show((err, config) => {
117+
return request.server.app.ipfs.config.get((err, config) => {
118118
if (err) {
119119
log.error(err)
120120
return reply({

test/core/both/test-bitswap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('bitswap', () => {
2626
beforeEach((done) => {
2727
ipfs = new IPFS(require('../../utils/repo-path'))
2828
if (!isNode) {
29-
ipfs.config.show((err, config) => {
29+
ipfs.config.get((err, config) => {
3030
configBak = JSON.parse(JSON.stringify(config))
3131
expect(err).to.not.exist
3232
config.Addresses.Swarm = []

test/core/both/test-config.js

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,19 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const expect = require('chai').expect
4+
const test = require('interface-ipfs-core')
55
const IPFS = require('../../../src/core')
66

7-
describe('config', () => {
8-
var defaultConfig = {
9-
Identity: {
10-
PeerID: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',
11-
PrivKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrPHvXvfIqjDScLJ4NcRO2TddhXAfZoOPVH5k4PJHKLBPKuXZpWlookCAyENY7+Pd55S8r+a+MusrMagYNljb5WbVTgN8cgdpim9lbbIFlpN6SZaVjLQL3J8TWH6wKBgQDSChzItkqWX11CNstJ9zJyUE20I7LrpyBJNgG1gtvz3ZMUQCn3PxxHtQzN9n1P0mSSYs+jBKPuoSyYLt1wwe10/lpgL4rkKWU3/m1Myt0tveJ9WcqHh6tzcAbb/fXpUFT/o4SWDimWkPkuCb+8j//2yiXk0a/T2f36zKMuZvujqQKBgC6B7BAQDG2H2B/ijofp12ejJU36nL98gAZyqOfpLJ+FeMz4TlBDQ+phIMhnHXA5UkdDapQ+zA3SrFk+6yGk9Vw4Hf46B+82SvOrSbmnMa+PYqKYIvUzR4gg34rL/7AhwnbEyD5hXq4dHwMNsIDq+l2elPjwm/U9V0gdAl2+r50HAoGALtsKqMvhv8HucAMBPrLikhXP/8um8mMKFMrzfqZ+otxfHzlhI0L08Bo3jQrb0Z7ByNY6M8epOmbCKADsbWcVre/AAY0ZkuSZK/CaOXNX/AhMKmKJh8qAOPRY02LIJRBCpfS4czEdnfUhYV/TYiFNnKRj57PPYZdTzUsxa/yVTmECgYBr7slQEjb5Onn5mZnGDh+72BxLNdgwBkhO0OCdpdISqk0F0Pxby22DFOKXZEpiyI9XYP1C8wPiJsShGm2yEwBPWXnrrZNWczaVuCbXHrZkWQogBDG3HGXNdU4MAWCyiYlyinIBpPpoAJZSzpGLmWbMWh28+RJS6AQX6KHrK1o2uw==' },
12-
Datastore: { Type: '',
13-
Path: '',
14-
StorageMax: '',
15-
StorageGCWatermark: 0,
16-
GCPeriod: '',
17-
Params: null,
18-
NoSync: false },
19-
Addresses: { Swarm: [ '/ip4/127.0.0.1/tcp/9999', '/ip4/127.0.0.1/tcp/9990/ws' ],
20-
API: '/ip4/127.0.0.1/tcp/6001',
21-
Gateway: '/ip4/127.0.0.1/tcp/0' },
22-
Mounts: { IPFS: '/ipfs', IPNS: '/ipns', FuseAllowOther: false },
23-
Version: { Current: '0.4.0-dev',
24-
Check: 'error',
25-
CheckDate: '0001-01-01T00:00:00Z',
26-
CheckPeriod: '172800000000000',
27-
AutoUpdate: 'minor' },
28-
Discovery: { MDNS: { Enabled: true, Interval: 10 } },
29-
Ipns: { RepublishPeriod: '',
30-
RecordLifetime: '',
31-
ResolveCacheSize: 128 },
32-
Bootstrap: [ '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
33-
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
34-
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
35-
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
36-
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
37-
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
38-
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
39-
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
40-
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx' ],
41-
Tour: { Last: '' },
42-
Gateway: { HTTPHeaders: null, RootRedirect: '', Writable: false },
43-
SupernodeRouting: {
44-
Servers: [
45-
'/ip4/104.236.176.52/tcp/4002/ipfs/QmXdb7tWTxdFEQEFgWBqkuYSrZd3mXrC7HxkD4krGNYx2U',
46-
'/ip4/104.236.179.241/tcp/4002/ipfs/QmVRqViDByUxjUMoPnjurjKvZhaEMFDtK35FJXHAM4Lkj6',
47-
'/ip4/104.236.151.122/tcp/4002/ipfs/QmSZwGx8Tn8tmcM4PtDJaMeUQNRhNFdBLVGPzRiNaRJtFH',
48-
'/ip4/162.243.248.213/tcp/4002/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP',
49-
'/ip4/128.199.219.111/tcp/4002/ipfs/Qmb3brdCYmKG1ycwqCbo6LUwWxTuo3FisnJV2yir7oN92R',
50-
'/ip4/104.236.76.40/tcp/4002/ipfs/QmdRBCV8Cz2dGhoKLkD3YjPwVFECmqADQkx5ZteF2c6Fy4',
51-
'/ip4/178.62.158.247/tcp/4002/ipfs/QmUdiMPci7YoEUBkyFZAh2pAbjqcPr7LezyiPD2artLw3v',
52-
'/ip4/178.62.61.185/tcp/4002/ipfs/QmVw6fGNqBixZE4bewRLT2VXX7fAHUHs8JyidDiJ1P7RUN'
53-
]
54-
},
55-
API: {
56-
HTTPHeaders: null
57-
},
58-
Swarm: {
59-
AddrFilters: null
60-
},
61-
Log: {
62-
MaxSizeMB: 250,
63-
MaxBackups: 1,
64-
MaxAgeDays: 0
65-
}
66-
}
67-
68-
var ipfs
69-
70-
before((done) => {
71-
ipfs = new IPFS(require('../../utils/repo-path'))
72-
ipfs.load(done)
73-
})
74-
75-
it('show', (done) => {
76-
ipfs.config.show((err, config) => {
77-
expect(err).to.not.exist
78-
expect(config).to.deep.equal(defaultConfig)
79-
done()
7+
const common = {
8+
setup: function (cb) {
9+
const ipfs = new IPFS(require('../../utils/repo-path'))
10+
ipfs.load(() => {
11+
cb(null, ipfs)
8012
})
81-
})
13+
},
14+
teardown: function (cb) {
15+
cb()
16+
}
17+
}
8218

83-
it('replace', (done) => {
84-
ipfs = new IPFS(require('../../utils/repo-path'))
85-
ipfs.config.replace({}, (err) => {
86-
expect(err).to.not.exist
87-
ipfs.config.show((err, config) => {
88-
expect(err).to.not.exist
89-
expect(config).to.deep.equal({})
90-
ipfs.config.replace(defaultConfig, (err) => {
91-
expect(err).to.not.exist
92-
done()
93-
})
94-
})
95-
})
96-
})
97-
})
19+
test.config(common)

0 commit comments

Comments
 (0)