-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathfactory-daemon.js
147 lines (129 loc) · 4.42 KB
/
factory-daemon.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const defaultsDeep = require('lodash.defaultsdeep')
const clone = require('lodash.clone')
const series = require('async/series')
const path = require('path')
const tmpDir = require('./utils/tmp-dir')
const Daemon = require('./ipfsd-daemon')
const defaultConfig = require('./defaults/config')
const defaultOptions = require('./defaults/options')
// TODO extract common functionality into base class
/**
* Spawn IPFS Daemons (either JS or Go)
*
* @namespace FactoryDaemon
*/
class FactoryDaemon {
/**
*
* @param {Object} options
* - `type` string - 'go' or 'js'
* - `exec` string (optional) - the path of the daemon executable
* @return {*}
*/
constructor (options) {
if (options && options.type === 'proc') {
throw new Error('This Factory does not know how to spawn in proc nodes')
}
options = Object.assign({}, { type: 'go' }, options)
this.type = options.type
this.exec = options.exec
}
/**
* Utility method to get a temporary directory
* useful in browsers to be able to generate temp
* repos manually
*
* *Here for completeness*
*
* @param {String} type - the type of the node
* @param {function(Error, string)} callback
* @returns {undefined}
*/
tmpDir (type, callback) {
callback(null, tmpDir(type === 'js'))
}
/**
* Get the version of the IPFS Daemon.
*
* @memberof FactoryDaemon
* @param {Object} [options={}]
* @param {function(Error, string)} callback
* @returns {undefined}
*/
version (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = Object.assign({}, options, { type: this.type, exec: this.exec })
// TODO: (1) this should check to see if it is looking for Go or JS
// TODO: (2) This spawns a whole daemon just to get his version? There is
// a way to get the version while the daemon is offline...
const d = new Daemon(options)
d.version(callback)
}
/**
* Spawn an IPFS node, either js-ipfs or go-ipfs
*
* Options are:
* - `init` bool - should the node be initialized
* - `initOptions` Object, it is expected to be of the form `{bits: <size>}`, which sets the desired key size
* - `start` bool - should the node be started
* - `repoPath` string - the repository path to use for this node, ignored if node is disposable
* - `disposable` bool - a new repo is created and initialized for each invocation
* - `config` - ipfs configuration options
* - `args` - array of cmd line arguments to be passed to ipfs daemon
* - `exec` string (optional) - path to the desired IPFS executable to spawn,
* this will override the `exec` set when creating the daemon controller factory instance
*
* @param {Object} [options={}] - various config options and ipfs config parameters
* @param {Function} callback(err, [`ipfs-api instance`, `Node (ctrl) instance`]) - a callback that receives an array with an `ipfs-instance` attached to the node and a `Node`
* @return {undefined}
*/
spawn (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
// TODO this options parsing is daunting. Refactor and move to a separate
// func documenting what it is trying to do.
options = defaultsDeep({}, options, defaultOptions)
options.init = typeof options.init !== 'undefined'
? options.init
: true
if (!options.disposable) {
const nonDisposableConfig = clone(defaultConfig)
// TODO Why delete these?
// delete nonDisposableConfig.Addresses
options.init = false
options.start = false
const defaultRepo = path.join(
process.env.HOME || process.env.USERPROFILE,
options.isJs
? '.jsipfs'
: '.ipfs'
)
options.repoPath = options.repoPath ||
(process.env.IPFS_PATH || defaultRepo)
options.config = defaultsDeep({}, options.config, nonDisposableConfig)
} else {
options.config = defaultsDeep({}, options.config, defaultConfig)
}
options.type = this.type
options.exec = options.exec || this.exec
const node = new Daemon(options)
series([
(cb) => options.init
? node.init(cb)
: cb(null, node),
(cb) => options.start
? node.start(options.args, cb)
: cb()
], (err) => {
if (err) { return callback(err) }
callback(null, node)
})
}
}
module.exports = FactoryDaemon