This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
daemon-spawner.js
90 lines (75 loc) · 2 KB
/
daemon-spawner.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
'use strict'
const ipfsd = require('ipfsd-ctl')
const series = require('async/series')
const eachSeries = require('async/eachSeries')
const once = require('once')
module.exports = Factory
function Factory () {
if (!(this instanceof Factory)) {
return new Factory()
}
let nodes = []
this.spawnNode = (repoPath, config, callback) => {
if (typeof repoPath === 'function') {
callback = repoPath
repoPath = undefined
}
if (typeof config === 'function') {
callback = config
config = undefined
}
// TODO
// support custom repoPath
// support custom configs being passed
spawnEphemeralNode((err, node) => {
if (err) {
return callback(err)
}
nodes.push(node)
callback(null, node.apiAddr)
})
}
this.dismantle = (callback) => {
eachSeries(nodes, (node, cb) => {
cb = once(cb)
node.stopDaemon(cb)
}, (err) => {
if (err) {
return callback(err)
}
nodes = []
callback()
})
}
}
function spawnEphemeralNode (callback) {
ipfsd.disposable((err, node) => {
if (err) {
return callback(err)
}
// Note: we have to set each config value
// independently since the config/replace endpoint
// doesn't work as expected
series([
(cb) => {
const configValues = {
Bootstrap: [],
Discovery: {},
'API.HTTPHeaders.Access-Control-Allow-Origin': ['*'],
// This is not needed after all
// 'API.HTTPHeaders.Access-Control-Allow-Credentials': true,
'API.HTTPHeaders.Access-Control-Allow-Methods': [
'PUT',
'POST',
'GET'
]
}
eachSeries(Object.keys(configValues), (configKey, cb) => {
const configVal = JSON.stringify(configValues[configKey])
node.setConfig(configKey, configVal, cb)
}, cb)
},
(cb) => node.startDaemon(['--enable-pubsub-experiment'], cb)
], (err) => callback(err, node))
})
}