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 pathindex.js
188 lines (163 loc) · 5.48 KB
/
index.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
'use strict'
const series = require('async/series')
const Hapi = require('hapi')
const debug = require('debug')
const multiaddr = require('multiaddr')
const setHeader = require('hapi-set-header')
const once = require('once')
const IPFS = require('../core')
const WStar = require('libp2p-webrtc-star')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-bootstrap')
const errorHandler = require('./error-handler')
function uriToMultiaddr (uri) {
const ipPort = uri.split('/')[2].split(':')
return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}`
}
function HttpApi (repo, config, cliArgs) {
cliArgs = cliArgs || {}
this.node = undefined
this.server = undefined
this.log = debug('jsipfs:http-api')
this.log.error = debug('jsipfs:http-api:error')
if (process.env.IPFS_MONITORING) {
// Setup debug metrics collection
const prometheusClient = require('prom-client')
const prometheusGcStats = require('prometheus-gc-stats')
const collectDefaultMetrics = prometheusClient.collectDefaultMetrics
collectDefaultMetrics({ timeout: 5000 })
prometheusGcStats(prometheusClient.register)()
}
this.start = (init, callback) => {
if (typeof init === 'function') {
callback = init
init = false
}
this.log('starting')
series([
(cb) => {
cb = once(cb)
const libp2p = { modules: {} }
// Attempt to use any of the WebRTC versions available globally
let electronWebRTC
let wrtc
try { electronWebRTC = require('electron-webrtc')() } catch (err) {}
try { wrtc = require('wrtc') } catch (err) {}
if (wrtc || electronWebRTC) {
const using = wrtc ? 'wrtc' : 'electron-webrtc'
console.log(`Using ${using} for webrtc support`)
const wstar = new WStar({ wrtc: (wrtc || electronWebRTC) })
libp2p.modules.transport = [TCP, WS, wstar]
libp2p.modules.peerDiscovery = [MulticastDNS, Bootstrap, wstar.discovery]
}
// try-catch so that programmer errors are not swallowed during testing
try {
// start the daemon
this.node = new IPFS({
repo: repo,
init: init,
start: true,
config: config,
local: cliArgs.local,
pass: cliArgs.pass,
EXPERIMENTAL: {
pubsub: cliArgs.enablePubsubExperiment,
ipnsPubsub: cliArgs.enableNamesysPubsub,
dht: cliArgs.enableDhtExperiment,
sharding: cliArgs.enableShardingExperiment
},
libp2p: libp2p
})
} catch (err) {
return cb(err)
}
this.node.once('error', (err) => {
this.log('error starting core', err)
err.code = 'ENOENT'
cb(err)
})
this.node.once('start', cb)
},
(cb) => {
this.log('fetching config')
this.node._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
// CORS is enabled by default
// TODO: shouldn't, fix this
this.server = new Hapi.Server({
connections: {
routes: {
cors: true
}
},
debug: process.env.DEBUG ? {
request: ['*'],
log: ['*']
} : undefined
})
this.server.app.ipfs = this.node
const api = config.Addresses.API.split('/')
const gateway = config.Addresses.Gateway.split('/')
// select which connection with server.select(<label>) to add routes
this.server.connection({
host: api[2],
port: api[4],
labels: 'API'
})
this.server.connection({
host: gateway[2],
port: gateway[4],
labels: 'Gateway'
})
// Nicer errors
errorHandler(this, this.server)
// load routes
require('./api/routes')(this.server)
// load gateway routes
require('./gateway/routes')(this.server)
// Set default headers
setHeader(this.server,
'Access-Control-Allow-Headers',
'X-Stream-Output, X-Chunked-Output, X-Content-Length')
setHeader(this.server,
'Access-Control-Expose-Headers',
'X-Stream-Output, X-Chunked-Output, X-Content-Length')
this.server.start(cb)
})
},
(cb) => {
const api = this.server.select('API')
const gateway = this.server.select('Gateway')
this.apiMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/' + api.info.port)
api.info.ma = uriToMultiaddr(api.info.uri)
gateway.info.ma = uriToMultiaddr(gateway.info.uri)
console.log('API listening on %s', api.info.ma)
console.log('Gateway (read only) listening on %s', gateway.info.ma)
console.log('Web UI available at %s', api.info.uri + '/webui')
// for the CLI to know the where abouts of the API
this.node._repo.apiAddr.set(api.info.ma, cb)
}
], (err) => {
this.log('done', err)
callback(err)
})
}
this.stop = (callback) => {
this.log('stopping')
series([
(cb) => this.server.stop(cb),
(cb) => this.node.stop(cb)
], (err) => {
if (err) {
this.log.error(err)
console.log('There were errors stopping')
}
callback()
})
}
}
module.exports = HttpApi