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

Commit 32f11ac

Browse files
tests passing
1 parent cbe6582 commit 32f11ac

File tree

5 files changed

+81
-88
lines changed

5 files changed

+81
-88
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ before_install:
1818

1919
script:
2020
- npm run lint
21-
- npm run test:node
21+
- npm run test
2222
- npm run coverage
2323

2424
before_script:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ If are using this module in a browser with something like browserify, then you w
9999

100100
```bash
101101
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"
102+
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "\"true\""
103+
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"
102104
```
103105

104106
## Usage

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"browser": {
77
"glob": false,
88
"fs": false,
9-
"http": "stream-http",
10-
"https": "https-browserify"
9+
"stream": "readable-stream"
1110
},
1211
"scripts": {
1312
"test": "gulp test",
@@ -28,19 +27,18 @@
2827
"detect-node": "^2.0.3",
2928
"flatmap": "0.0.3",
3029
"glob": "^7.1.1",
31-
"https-browserify": "0.0.1",
3230
"ipfs-block": "^0.5.0",
3331
"ipld-dag-pb": "^0.8.0",
3432
"is-ipfs": "^0.2.1",
3533
"isstream": "^0.1.2",
3634
"multiaddr": "^2.0.3",
3735
"multipart-stream": "^2.0.1",
3836
"ndjson": "^1.4.3",
37+
"once": "^1.4.0",
3938
"peer-id": "^0.8.0",
4039
"peer-info": "^0.8.0",
4140
"promisify-es6": "^1.0.2",
4241
"qs": "^6.3.0",
43-
"stream-http": "^2.4.1",
4442
"streamifier": "^0.1.1",
4543
"tar-stream": "^1.5.2",
4644
"wreck": "^10.0.0"

test/factory/daemon-spawner.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
// const defaultConfig = require('./default-config.json')
44
const ipfsd = require('ipfsd-ctl')
5-
const series = require('run-series')
5+
const series = require('async/series')
6+
const eachSeries = require('async/eachSeries')
7+
const once = require('once')
68

79
module.exports = Factory
810

@@ -11,7 +13,7 @@ function Factory () {
1113
return new Factory()
1214
}
1315

14-
const nodes = []
16+
let nodes = []
1517

1618
this.spawnNode = (repoPath, config, callback) => {
1719
if (typeof repoPath === 'function') {
@@ -44,11 +46,18 @@ function Factory () {
4446
})
4547
}
4648

47-
this.dismantle = function (callback) {
48-
series(
49-
nodes.map((node) => {
50-
return node.stopDaemon
51-
}), callback)
49+
this.dismantle = (callback) => {
50+
eachSeries(nodes, (node, cb) => {
51+
cb = once(cb)
52+
node.stopDaemon(cb)
53+
}, (err) => {
54+
if (err) {
55+
return callback(err)
56+
}
57+
nodes = []
58+
59+
callback()
60+
})
5261
}
5362
}
5463

@@ -62,23 +71,19 @@ function spawnEphemeralNode (callback) {
6271
// doesn't work as expected
6372
series([
6473
(cb) => {
65-
node.setConfig('Bootstrap', null, cb)
66-
},
67-
(cb) => {
68-
node.setConfig('Discovery', '{}', cb)
69-
},
70-
(cb) => {
71-
const headers = {
72-
HTTPHeaders: {
73-
'Access-Control-Allow-Origin': ['*'],
74-
'Access-Control-Allow-Credentials': 'true'
75-
}
74+
const configValues = {
75+
// Bootstrap: [],
76+
Discovery: {},
77+
'HTTPHeaders.Access-Control-Allow-Origin': ['*'],
78+
'HTTPHeaders.Access-Control-Allow-Credentials': 'true',
79+
'HTTPHeaders.Access-Control-Allow-Methods': ['PUT', 'POST', 'GET']
7680
}
77-
node.setConfig('API', JSON.stringify(headers), cb)
81+
82+
eachSeries(Object.keys(configValues), (configKey, cb) => {
83+
node.setConfig(`API.${configKey}`, JSON.stringify(configValues[configKey]), cb)
84+
}, cb)
7885
},
79-
(cb) => {
80-
node.startDaemon(cb)
81-
}
86+
(cb) => node.startDaemon(cb)
8287
], (err) => {
8388
if (err) {
8489
return callback(err)

test/setup/spawn-daemons.js

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,69 @@ const gulp = require('gulp')
66
const fs = require('fs')
77
const path = require('path')
88
const ipfsd = require('ipfsd-ctl')
9+
const eachSeries = require('async/eachSeries')
10+
const parallel = require('async/parallel')
911

1012
let daemons
1113

1214
function startDisposableDaemons (callback) {
1315
// a, b, c
1416
const ipfsNodes = {}
1517

16-
// a, b, c
17-
const apiAddrs = {}
18-
19-
let counter = 0
20-
21-
function finish () {
22-
counter++
23-
if (counter === 3) {
24-
const targetPath = path.join(__dirname, '/tmp-disposable-nodes-addrs.json')
25-
fs.writeFileSync(targetPath, JSON.stringify(apiAddrs))
26-
callback(ipfsNodes)
27-
}
28-
}
29-
30-
function startIndependentNode (nodes, addrs, key, cb) {
18+
function startIndependentNode (nodes, key, cb) {
3119
ipfsd.disposable((err, node) => {
3220
if (err) {
33-
throw err
21+
return cb(err)
3422
}
3523

3624
nodes[key] = node
3725

3826
console.log(' ipfs init done - (bootstrap and mdns off) - ' + key)
3927

40-
nodes[key].setConfig('Bootstrap', null, (err) => {
28+
const configValues = {
29+
// Bootstrap: [],
30+
Discovery: {},
31+
'HTTPHeaders.Access-Control-Allow-Origin': ['*'],
32+
'HTTPHeaders.Access-Control-Allow-Credentials': 'true',
33+
'HTTPHeaders.Access-Control-Allow-Methods': ['PUT', 'POST', 'GET']
34+
}
35+
36+
eachSeries(Object.keys(configValues), (configKey, cb) => {
37+
nodes[key].setConfig(`API.${configKey}`, JSON.stringify(configValues[configKey]), cb)
38+
}, (err) => {
4139
if (err) {
42-
throw err
40+
return cb(err)
4341
}
44-
nodes[key].setConfig('Discovery', '{}', (err) => {
45-
if (err) {
46-
throw err
47-
}
48-
49-
const headers = {
50-
HTTPHeaders: {
51-
'Access-Control-Allow-Origin': ['*']
52-
}
53-
}
54-
nodes[key].setConfig('API', JSON.stringify(headers), (err) => {
55-
if (err) {
56-
throw err
57-
}
5842

59-
nodes[key].startDaemon((err, ignore) => {
60-
if (err) {
61-
throw err
62-
}
63-
64-
addrs[key] = nodes[key].apiAddr
65-
cb()
66-
})
67-
})
68-
})
43+
nodes[key].startDaemon(cb)
6944
})
7045
})
7146
}
7247

73-
startIndependentNode(ipfsNodes, apiAddrs, 'a', finish)
74-
startIndependentNode(ipfsNodes, apiAddrs, 'b', finish)
75-
startIndependentNode(ipfsNodes, apiAddrs, 'c', finish)
48+
parallel([
49+
(cb) => startIndependentNode(ipfsNodes, 'a', cb),
50+
(cb) => startIndependentNode(ipfsNodes, 'b', cb),
51+
(cb) => startIndependentNode(ipfsNodes, 'c', cb)
52+
], (err) => {
53+
if (err) {
54+
return callback(err)
55+
}
56+
const targetPath = path.join(__dirname, '/tmp-disposable-nodes-addrs.json')
57+
fs.writeFileSync(targetPath, JSON.stringify({
58+
a: ipfsNodes.a.apiAddr,
59+
b: ipfsNodes.b.apiAddr,
60+
c: ipfsNodes.c.apiAddr
61+
}))
62+
callback(null, ipfsNodes)
63+
})
7664
}
7765

7866
function stopDisposableDaemons (ds, callback) {
79-
let counter = 0
80-
function finish () {
81-
counter++
82-
if (counter === 3) {
83-
callback()
84-
}
85-
}
86-
87-
function stopIPFSNode (list, key, cb) {
67+
function stopIPFSNode (node, cb) {
8868
let nodeStopped
89-
list[key].stopDaemon((err) => {
69+
node.stopDaemon((err) => {
9070
if (err) {
91-
throw err
71+
return cb(err)
9272
}
9373
if (!nodeStopped) {
9474
nodeStopped = true
@@ -97,20 +77,28 @@ function stopDisposableDaemons (ds, callback) {
9777
})
9878
}
9979

100-
stopIPFSNode(ds, 'a', finish)
101-
stopIPFSNode(ds, 'b', finish)
102-
stopIPFSNode(ds, 'c', finish)
80+
parallel([
81+
(cb) => stopIPFSNode(ds.a, cb),
82+
(cb) => stopIPFSNode(ds.b, cb),
83+
(cb) => stopIPFSNode(ds.c, cb)
84+
], callback)
10385
}
10486

10587
gulp.task('daemons:start', (done) => {
106-
startDisposableDaemons((d) => {
88+
startDisposableDaemons((err, d) => {
89+
if (err) {
90+
return done(err)
91+
}
10792
daemons = d
10893
done()
10994
})
11095
})
11196

11297
gulp.task('daemons:stop', (done) => {
113-
stopDisposableDaemons(daemons, () => {
98+
stopDisposableDaemons(daemons, (err) => {
99+
if (err) {
100+
return done(err)
101+
}
114102
daemons = null
115103
done()
116104
})

0 commit comments

Comments
 (0)