forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple HttpApi from cli/commands/daemon
In the past API was exposed via HTTP Server only when jsipfs daemon was run from the commandline, so src/http/index.js was also responsible for orchestration that is not related to HTTP itself. This refactor moves code that is not related to HTTP Servers into standalone-daemon.js, which is easier to reason about, and unlocks use of HttpApi in contexts other than commandline jsipfs daemon, such as Firefox with libdweb or Chromium-based web browser with chrome.sockets APIs. Refs. ipfs/ipfs-companion#664 License: MIT Signed-off-by: Marcin Rataj <[email protected]>
- Loading branch information
Showing
4 changed files
with
99 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ docs | |
# Logs | ||
logs | ||
*.log | ||
# npm pack | ||
*.tgz | ||
|
||
coverage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
|
||
const IPFS = require('../core') | ||
const HttpApi = require('../http') | ||
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') | ||
|
||
class StandaloneDaemon { | ||
constructor (options) { | ||
this._options = options || {} | ||
this._log = debug('ipfs:daemon') | ||
this._log.error = debug('ipfs:daemon: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)() | ||
} | ||
} | ||
|
||
async start () { | ||
this._log('starting') | ||
|
||
const libp2p = { modules: {} } | ||
|
||
// Attempt to use any of the WebRTC versions available globally | ||
let electronWebRTC | ||
let wrtc | ||
try { | ||
electronWebRTC = require('electron-webrtc')() | ||
} catch (err) { | ||
this._log('failed to load optional electron-webrtc dependency') | ||
} | ||
try { | ||
wrtc = require('wrtc') | ||
} catch (err) { | ||
this._log('failed to load optional webrtc dependency') | ||
} | ||
|
||
if (wrtc || electronWebRTC) { | ||
const using = wrtc ? 'wrtc' : 'electron-webrtc' | ||
this._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] | ||
} | ||
|
||
// start the daemon | ||
const ipfsOpts = Object.assign({ init: false }, this._options, { start: true, libp2p }) | ||
const ipfs = new IPFS(ipfsOpts) | ||
|
||
await new Promise((resolve, reject) => { | ||
ipfs.once('error', err => { | ||
this._log('error starting core', err) | ||
err.code = 'ENOENT' | ||
reject(err) | ||
}) | ||
ipfs.once('start', resolve) | ||
}) | ||
|
||
this._ipfs = ipfs | ||
|
||
// start HTTP servers (if API or Gateway is enabled in options) | ||
this._httpApi = new HttpApi(ipfs, ipfsOpts) | ||
await this._httpApi.start() | ||
|
||
this._log('started') | ||
return this | ||
} | ||
|
||
async stop () { | ||
this._log('stopping') | ||
await Promise.all([ | ||
this._httpApi && this._httpApi, | ||
this._ipfs && this._ipfs.stop() | ||
]) | ||
this._log('stopped') | ||
return this | ||
} | ||
} | ||
|
||
module.exports = StandaloneDaemon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters