forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.js
94 lines (83 loc) · 2.76 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
'use strict'
const os = require('os')
const toUri = require('multiaddr-to-uri')
const { ipfsPathHelp } = require('../utils')
const { isTest } = require('ipfs-utils/src/env')
module.exports = {
command: 'daemon',
describe: 'Start a long-running daemon process',
builder (yargs) {
return yargs
.epilog(ipfsPathHelp)
.option('enable-sharding-experiment', {
type: 'boolean',
default: false
})
.option('offline', {
type: 'boolean',
desc: 'Run offline. Do not connect to the rest of the network but provide local API.',
default: false
})
.option('enable-namesys-pubsub', {
type: 'boolean',
default: false
})
.option('enable-preload', {
type: 'boolean',
default: !isTest // preload by default, unless in test env
})
},
handler (argv) {
argv.resolve((async () => {
const { print } = argv
print('Initializing IPFS daemon...')
print(`js-ipfs version: ${require('../../../package.json').version}`)
print(`System version: ${os.arch()}/${os.platform()}`)
print(`Node.js version: ${process.versions.node}`)
const repoPath = argv.getRepoPath()
// Required inline to reduce startup time
const Daemon = require('../../cli/daemon')
const daemon = new Daemon({
silent: argv.silent,
repo: process.env.IPFS_PATH,
offline: argv.offline,
pass: argv.pass,
preload: { enabled: argv.enablePreload },
EXPERIMENTAL: {
ipnsPubsub: argv.enableNamesysPubsub,
dht: argv.enableDhtExperiment,
sharding: argv.enableShardingExperiment
}
})
try {
await daemon.start()
daemon._httpApi._apiServers.forEach(apiServer => {
print(`API listening on ${apiServer.info.ma.toString()}`)
})
daemon._httpApi._gatewayServers.forEach(gatewayServer => {
print(`Gateway (read only) listening on ${gatewayServer.info.ma.toString()}`)
})
daemon._httpApi._apiServers.forEach(apiServer => {
print(`Web UI available at ${toUri(apiServer.info.ma)}/webui`)
})
} catch (err) {
if (err.code === 'ENOENT' && err.message.match(/uninitialized/i)) {
print('Error: no initialized ipfs repo found in ' + repoPath)
print('please run: jsipfs init')
process.exit(1)
}
throw err
}
print('Daemon is ready')
const cleanup = async () => {
print(`Received interrupt signal, shutting down...`)
await daemon.stop()
process.exit(0)
}
// listen for graceful termination
process.on('SIGTERM', cleanup)
process.on('SIGINT', cleanup)
process.on('SIGHUP', cleanup)
})())
}
}