This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathdaemons.js
117 lines (97 loc) · 2.41 KB
/
daemons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict'
/* eslint max-nested-callbacks: ["error", 8] */ // TODO reduce nesteness
const gulp = require('gulp')
const fs = require('fs')
const path = require('path')
let daemons
function startDisposableDaemons (callback) {
const ipfsd = require('ipfsd-ctl')
// a, b, c
const ipfsNodes = {}
// a, b, c
const apiAddrs = {}
let counter = 0
function finish () {
counter++
if (counter === 3) {
const targetPath = path.join(__dirname, '/../test/tmp-disposable-nodes-addrs.json')
fs.writeFileSync(targetPath, JSON.stringify(apiAddrs))
callback(ipfsNodes)
}
}
function startIndependentNode (nodes, addrs, key, cb) {
ipfsd.disposable((err, node) => {
if (err) {
throw err
}
nodes[key] = node
console.log(' ipfs init done - (bootstrap and mdns off) - ' + key)
nodes[key].setConfig('Bootstrap', null, (err) => {
if (err) {
throw err
}
nodes[key].setConfig('Discovery', '{}', (err) => {
if (err) {
throw err
}
const headers = {
HTTPHeaders: {
'Access-Control-Allow-Origin': ['*']
}
}
nodes[key].setConfig('API', JSON.stringify(headers), (err) => {
if (err) {
throw err
}
nodes[key].startDaemon((err, ignore) => {
if (err) {
throw err
}
addrs[key] = nodes[key].apiAddr
cb()
})
})
})
})
})
}
startIndependentNode(ipfsNodes, apiAddrs, 'a', finish)
startIndependentNode(ipfsNodes, apiAddrs, 'b', finish)
startIndependentNode(ipfsNodes, apiAddrs, 'c', finish)
}
function stopDisposableDaemons (ds, callback) {
let counter = 0
function finish () {
counter++
if (counter === 3) {
callback()
}
}
function stopIPFSNode (list, key, cb) {
let nodeStopped
list[key].stopDaemon((err) => {
if (err) {
throw err
}
if (!nodeStopped) {
nodeStopped = true
cb()
}
})
}
stopIPFSNode(ds, 'a', finish)
stopIPFSNode(ds, 'b', finish)
stopIPFSNode(ds, 'c', finish)
}
gulp.task('daemons:start', (done) => {
startDisposableDaemons((d) => {
daemons = d
done()
})
})
gulp.task('daemons:stop', (done) => {
stopDisposableDaemons(daemons, () => {
daemons = null
done()
})
})