This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdaemon.js
146 lines (131 loc) · 4.36 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
'use strict'
const os = require('os')
const fs = require('fs')
// @ts-ignore no types
const toUri = require('multiaddr-to-uri')
const { ipfsPathHelp } = require('../utils')
const { isTest } = require('ipfs-utils/src/env')
const debug = require('debug')('ipfs:cli:daemon')
module.exports = {
command: 'daemon',
describe: 'Start a long-running daemon process',
/**
* @param {import('yargs').Argv} yargs
*/
builder (yargs) {
return yargs
.epilog(ipfsPathHelp)
.option('init-config', {
type: 'string',
desc: 'Path to existing configuration file to be loaded during --init.'
})
.option('init-profile', {
type: 'string',
desc: 'Configuration profiles to apply for --init. See ipfs init --help for more.',
coerce: (value) => {
return (value || '').split(',')
}
})
.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
})
},
/**
* @param {object} argv
* @param {import('../types').Context} argv.ctx
* @param {string} [argv.initConfig]
* @param {string[]} [argv.initProfile]
* @param {boolean} argv.enableShardingExperiment
* @param {boolean} argv.offline
* @param {boolean} argv.enableNamesysPubsub
* @param {boolean} argv.enablePreload
* @param {boolean} argv.silent
* @param {boolean} argv.migrate
* @param {string} argv.pass
*/
async handler (argv) {
const { print, repoPath } = argv.ctx
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}`)
let config = {}
// read and parse config file
if (argv.initConfig) {
try {
const raw = fs.readFileSync(argv.initConfig, { encoding: 'utf8' })
config = JSON.parse(raw)
} catch (error) {
debug(error)
throw new Error('Default config couldn\'t be found or content isn\'t valid JSON.')
}
}
// Required inline to reduce startup time
const Daemon = require('ipfs-daemon')
const daemon = new Daemon({
config,
silent: argv.silent,
repo: process.env.IPFS_PATH,
repoAutoMigrate: argv.migrate,
offline: argv.offline,
pass: argv.pass,
preload: { enabled: argv.enablePreload },
EXPERIMENTAL: {
ipnsPubsub: argv.enableNamesysPubsub,
sharding: argv.enableShardingExperiment
},
init: argv.initProfile ? { profiles: argv.initProfile } : undefined
})
try {
await daemon.start()
if (daemon._httpApi && daemon._httpApi._apiServers) {
daemon._httpApi._apiServers.forEach(apiServer => {
print(`HTTP API listening on ${apiServer.info.ma}`)
})
}
// @ts-ignore - _httpGateway is possibly undefined
if (daemon._grpcServer && daemon._grpcServer) {
print(`gRPC listening on ${daemon._grpcServer.info.ma}`)
}
if (daemon._httpGateway && daemon._httpGateway._gatewayServers) {
daemon._httpGateway._gatewayServers.forEach(gatewayServer => {
print(`Gateway (read only) listening on ${gatewayServer.info.ma}`)
})
}
if (daemon._httpApi && daemon._httpApi._apiServers) {
daemon._httpApi._apiServers.forEach(apiServer => {
print(`Web UI available at ${toUri(apiServer.info.ma)}/webui`)
})
}
} catch (err) {
if (err.code === 'ERR_REPO_NOT_INITIALIZED' || err.message.match(/uninitialized/i)) {
err.message = 'no initialized ipfs repo found in ' + repoPath + '\nplease run: jsipfs init'
}
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)
}
}