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

Commit 0270fa0

Browse files
committed
follow interface-ipfs-core config spec
1 parent 153d139 commit 0270fa0

File tree

5 files changed

+66
-148
lines changed

5 files changed

+66
-148
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"ndjson": "^1.4.3",
3333
"promisify-es6": "^1.0.1",
3434
"qs": "^6.1.0",
35+
"streamifier": "^0.1.1",
3536
"wreck": "^7.0.2"
3637
},
3738
"engines": {
@@ -45,7 +46,7 @@
4546
"aegir": "^3.2.0",
4647
"chai": "^3.5.0",
4748
"gulp": "^3.9.1",
48-
"interface-ipfs-core": "^0.3.0",
49+
"interface-ipfs-core": "^0.4.2",
4950
"ipfsd-ctl": "^0.14.0",
5051
"pre-commit": "^1.1.2",
5152
"stream-equal": "^0.1.8",

src/api/config.js

+46-13
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,64 @@
11
'use strict'
22

3-
const argCommand = require('../cmd-helpers').argCommand
3+
const streamifier = require('streamifier')
44

55
module.exports = (send) => {
66
return {
7-
get: argCommand(send, 'config'),
8-
set (key, value, opts, cb) {
9-
if (typeof (opts) === 'function') {
10-
cb = opts
7+
get (key, callback) {
8+
if (typeof key === 'function') {
9+
callback = key
10+
key = undefined
11+
}
12+
13+
if (!key) {
14+
return send('config/show', null, null, null, true, callback)
15+
}
16+
17+
return send('config', key, null, null, (err, result) => {
18+
if (err) {
19+
return callback(err)
20+
}
21+
callback(null, result.Value)
22+
})
23+
},
24+
set (key, value, opts, callback) {
25+
if (typeof opts === 'function') {
26+
callback = opts
1127
opts = {}
1228
}
29+
if (typeof key !== 'string') {
30+
return callback(new Error('Invalid key type'))
31+
}
32+
33+
if (typeof value !== 'object' &&
34+
typeof value !== 'boolean' &&
35+
typeof value !== 'string') {
36+
return callback(new Error('Invalid value type'))
37+
}
1338

14-
if (typeof (value) === 'object') {
39+
if (typeof value === 'object') {
1540
value = JSON.stringify(value)
1641
opts = { json: true }
17-
} else if (typeof (value) === 'boolean') {
42+
}
43+
44+
if (typeof value === 'boolean') {
1845
value = value.toString()
1946
opts = { bool: true }
2047
}
2148

22-
return send('config', [key, value], opts, null, cb)
23-
},
24-
show (cb) {
25-
return send('config/show', null, null, null, true, cb)
49+
return send('config', [key, value], opts, null, callback)
2650
},
27-
replace (file, cb) {
28-
return send('config/replace', null, null, file, cb)
51+
replace (config, callback) {
52+
// Its a path
53+
if (typeof config === 'string') {
54+
return send('config/replace', null, null, config, callback)
55+
}
56+
57+
// Its a config obj
58+
if (typeof config === 'object') {
59+
config = streamifier.createReadStream(new Buffer(JSON.stringify(config)))
60+
return send('config/replace', null, null, config, callback)
61+
}
2962
}
3063
}
3164
}

src/get-files-stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ function loadPaths (opts, file) {
9393
}
9494

9595
function getFilesStream (files, opts) {
96-
if (!files) return null
96+
if (!files) {
97+
return null
98+
}
9799

98100
const mp = new Multipart()
99101

src/request-api.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function onRes (buffer, cb) {
2525

2626
const stream = Boolean(res.headers['x-stream-output'])
2727
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
28-
const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0
28+
const isJson = res.headers['content-type'] &&
29+
res.headers['content-type'].indexOf('application/json') === 0
2930

3031
if (res.statusCode >= 400 || !res.statusCode) {
3132
const error = new Error(`Server responded with ${res.statusCode}`)
@@ -84,7 +85,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
8485
const port = config.port ? `:${config.port}` : ''
8586

8687
const opts = {
87-
method: files ? 'POST' : 'GET',
88+
method: 'POST',
8889
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
8990
headers: {}
9091
}
@@ -100,7 +101,6 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
100101
}
101102

102103
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
103-
opts.downstreamRes = stream
104104
opts.payload = stream
105105
}
106106

test/api/config.spec.js

+12-130
Original file line numberDiff line numberDiff line change
@@ -2,133 +2,15 @@
22
/* globals apiClients */
33
'use strict'
44

5-
const expect = require('chai').expect
6-
const isNode = require('detect-node')
7-
const path = require('path')
8-
9-
describe('.config', () => {
10-
describe('.config.{set, get}', () => {
11-
it('string', (done) => {
12-
const confKey = 'arbitraryKey'
13-
const confVal = 'arbitraryVal'
14-
15-
apiClients.a.config.set(confKey, confVal, (err, res) => {
16-
expect(err).to.not.exist
17-
apiClients.a.config.get(confKey, (err, res) => {
18-
expect(err).to.not.exist
19-
expect(res).to.have.a.property('Value', confVal)
20-
done()
21-
})
22-
})
23-
})
24-
25-
it('bool', (done) => {
26-
const confKey = 'otherKey'
27-
const confVal = true
28-
29-
apiClients.a.config.set(confKey, confVal, (err, res) => {
30-
expect(err).to.not.exist
31-
apiClients.a.config.get(confKey, (err, res) => {
32-
expect(err).to.not.exist
33-
expect(res.Value).to.deep.equal(confVal)
34-
done()
35-
})
36-
})
37-
})
38-
39-
it('json', (done) => {
40-
const confKey = 'API.HTTPHeaders.Access-Control-Allow-Origin'
41-
const confVal = ['http://example.io']
42-
43-
apiClients.a.config.set(confKey, confVal, (err, res) => {
44-
expect(err).to.not.exist
45-
apiClients.a.config.get(confKey, (err, res) => {
46-
expect(err).to.not.exist
47-
expect(res.Value).to.deep.equal(confVal)
48-
done()
49-
})
50-
})
51-
})
52-
})
53-
54-
it('.config.show', (done) => {
55-
apiClients.c.config.show((err, res) => {
56-
expect(err).to.not.exist
57-
expect(res).to.exist
58-
done()
59-
})
60-
})
61-
62-
it('.config.replace', (done) => {
63-
if (!isNode) {
64-
return done()
65-
}
66-
67-
apiClients.c.config.replace(path.join(__dirname, '/../r-config.json'), (err, res) => {
68-
expect(err).to.not.exist
69-
expect(res).to.be.equal(null)
70-
done()
71-
})
72-
})
73-
74-
describe('promise', () => {
75-
describe('.config.{set, get}', () => {
76-
it('string', () => {
77-
const confKey = 'arbitraryKey'
78-
const confVal = 'arbitraryVal'
79-
80-
return apiClients.a.config.set(confKey, confVal)
81-
.then((res) => {
82-
return apiClients.a.config.get(confKey)
83-
})
84-
.then((res) => {
85-
expect(res).to.have.a.property('Value', confVal)
86-
})
87-
})
88-
89-
it('bool', () => {
90-
const confKey = 'otherKey'
91-
const confVal = true
92-
93-
return apiClients.a.config.set(confKey, confVal)
94-
.then((res) => {
95-
return apiClients.a.config.get(confKey)
96-
})
97-
.then((res) => {
98-
expect(res.Value).to.deep.equal(confVal)
99-
})
100-
})
101-
102-
it('json', () => {
103-
const confKey = 'API.HTTPHeaders.Access-Control-Allow-Origin'
104-
const confVal = ['http://example.com']
105-
106-
return apiClients.a.config.set(confKey, confVal)
107-
.then((res) => {
108-
return apiClients.a.config.get(confKey)
109-
})
110-
.then((res) => {
111-
expect(res.Value).to.deep.equal(confVal)
112-
})
113-
})
114-
})
115-
116-
it('.config.show', () => {
117-
return apiClients.c.config.show()
118-
.then((res) => {
119-
expect(res).to.exist
120-
})
121-
})
122-
123-
it('.config.replace', () => {
124-
if (!isNode) {
125-
return
126-
}
127-
128-
return apiClients.c.config.replace(path.join(__dirname, '/../r-config.json'))
129-
.then((res) => {
130-
expect(res).to.be.equal(null)
131-
})
132-
})
133-
})
134-
})
5+
const test = require('interface-ipfs-core')
6+
7+
const common = {
8+
setup: function (cb) {
9+
cb(null, apiClients.a)
10+
},
11+
teardown: function (cb) {
12+
cb()
13+
}
14+
}
15+
16+
test.config(common)

0 commit comments

Comments
 (0)