-
Notifications
You must be signed in to change notification settings - Fork 882
/
Copy pathdaemon.js
194 lines (160 loc) · 5.11 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const Ctl = require('ipfsd-ctl')
const i18n = require('i18next')
const { showDialog } = require('../dialogs')
const logger = require('../common/logger')
const { getCustomBinary } = require('../custom-ipfs-binary')
const { applyDefaults, migrateConfig, checkCorsConfig, checkPorts, configExists, rmApiFile, apiFileExists } = require('./config')
const showMigrationPrompt = require('./migration-prompt')
function cannotConnectDialog (addr) {
showDialog({
title: i18n.t('cannotConnectToApiDialog.title'),
message: i18n.t('cannotConnectToApiDialog.message', { addr }),
type: 'error',
buttons: [
i18n.t('close')
]
})
}
function getIpfsBinPath () {
return process.env.IPFS_GO_EXEC ||
getCustomBinary() ||
require('go-ipfs')
.path()
.replace('app.asar', 'app.asar.unpacked')
}
async function spawn ({ flags, path }) {
const ipfsBin = getIpfsBinPath()
const ipfsd = await Ctl.createController({
ipfsHttpModule: require('ipfs-http-client'),
ipfsBin,
ipfsOptions: {
repo: path
},
remote: false,
disposable: false,
test: false,
args: flags
})
if (configExists(ipfsd)) {
migrateConfig(ipfsd)
checkCorsConfig(ipfsd)
return { ipfsd, isRemote: false }
}
// If config does not exist, but $IPFS_PATH/api exists, then
// it is a remote repository.
if (apiFileExists(ipfsd)) {
return { ipfsd, isRemote: true }
}
await ipfsd.init()
applyDefaults(ipfsd)
return { ipfsd, isRemote: false }
}
function listenToIpfsLogs (ipfsd, callback) {
let stdout, stderr
const listener = data => {
callback(data.toString())
}
const interval = setInterval(() => {
if (!ipfsd.subprocess) {
return
}
stdout = ipfsd.subprocess.stdout
stderr = ipfsd.subprocess.stderr
stdout.on('data', listener)
stderr.on('data', listener)
clearInterval(interval)
}, 20)
const stop = () => {
clearInterval(interval)
if (stdout) stdout.removeListener('data', listener)
if (stderr) stderr.removeListener('data', listener)
}
return stop
}
async function startIpfsWithLogs (ipfsd) {
let err, id, migrationPrompt
let isMigrating, isErrored, isFinished
let logs = ''
const isSpawnedDaemonDead = (ipfsd) => {
if (typeof ipfsd.subprocess === 'undefined') throw new Error('undefined ipfsd.subprocess, unable to reason about startup errors')
if (ipfsd.subprocess === null) return false // not spawned yet or remote
if (ipfsd.subprocess?.failed) return true // explicit failure
// detect when spawned ipfsd process is gone/dead
// by inspecting its pid - it should be alive
const { pid } = ipfsd.subprocess
try {
// signal 0 throws if process is missing, noop otherwise
process.kill(pid, 0)
return false
} catch (e) {
return true
}
}
const stopListening = listenToIpfsLogs(ipfsd, data => {
logs += data.toString()
const line = data.toLowerCase()
isMigrating = isMigrating || line.includes('migration')
isErrored = isErrored || isSpawnedDaemonDead(ipfsd)
isFinished = isFinished || line.includes('daemon is ready')
if (!isMigrating && !isErrored) {
return
}
if (!migrationPrompt) {
logger.info('[daemon] ipfs data store is migrating')
migrationPrompt = showMigrationPrompt(logs, isErrored, isFinished)
return
}
if (isErrored || isFinished) {
// forced show on error or when finished,
// because user could close it to run in background
migrationPrompt.loadWindow(logs, isErrored, isFinished)
} else { // update progress if the window is still around
migrationPrompt.update(logs)
}
})
try {
await ipfsd.start()
const idRes = await ipfsd.api.id()
id = idRes.id
} catch (e) {
err = e
} finally {
// stop monitoring daemon output - we only care about startup phase
stopListening()
// Show startup error using the same UI as migrations.
// This is catch-all that will show stdout/stderr of ipfs daemon
// that failed to start, allowing user to self-diagnose or report issue.
isErrored = isErrored || isSpawnedDaemonDead(ipfsd)
if (isErrored) { // save daemon output to error.log
logger.error(logs)
if (migrationPrompt) {
migrationPrompt.loadWindow(logs, isErrored, isFinished)
} else {
showMigrationPrompt(logs, isErrored, isFinished)
}
}
}
return {
err, id, logs
}
}
module.exports = async function (opts) {
const { ipfsd, isRemote } = await spawn(opts)
if (!isRemote) {
await checkPorts(ipfsd)
}
let errLogs = await startIpfsWithLogs(ipfsd)
if (errLogs.err) {
if (!errLogs.err.message.includes('ECONNREFUSED') && !errLogs.err.message.includes('ERR_CONNECTION_REFUSED')) {
return { ipfsd, err: errLogs.err, logs: errLogs.logs }
}
if (!configExists(ipfsd)) {
cannotConnectDialog(ipfsd.apiAddr.toString())
return { ipfsd, err: errLogs.err, logs: errLogs.logs }
}
logger.info('[daemon] removing api file')
rmApiFile(ipfsd)
errLogs = await startIpfsWithLogs(ipfsd)
}
return { ipfsd, err: errLogs.err, logs: errLogs.logs, id: errLogs.id }
}