Package Exports
- ipfsd-ctl
- ipfsd-ctl/src/endpoint/routes.js
- ipfsd-ctl/src/endpoint/server.js
- ipfsd-ctl/src/factory-daemon.js
- ipfsd-ctl/src/ipfsd-daemon.js
- ipfsd-ctl/src/utils/exec.js
- ipfsd-ctl/src/utils/find-ipfs-executable.js
- ipfsd-ctl/src/utils/repo/create-browser.js
- ipfsd-ctl/src/utils/repo/create-nodejs.js
- ipfsd-ctl/src/utils/run.js
- ipfsd-ctl/src/utils/tmp-dir-browser.js
- ipfsd-ctl/src/utils/tmp-dir.js
- ipfsd-ctl/test/utils/df-config-browser.js
- ipfsd-ctl/test/utils/df-config-nodejs.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ipfsd-ctl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ipfsd-ctl, the IPFS Factory.
Spawn IPFS daemons using JavaScript!
Table of Contents
Install
npm install --save ipfsd-ctlUsage
Spawn an IPFS daemon from Node.js
// Start a disposable node, and get access to the api
// print the node id, and stop the temporary daemon
const IPFSFactory = require('ipfsd-ctl')
const f = IPFSFactory.create()
f.spawn(function (err, ipfsd) {
if (err) { throw err }
ipfsd.api.id(function (err, id) {
if (err) { throw err }
console.log(id)
ipfsd.stop()
})
})Spawn an IPFS daemon from the Browser using the provided remote endpoint
// Start a remote disposable node, and get access to the api
// print the node id, and stop the temporary daemon
const IPFSFactory = require('ipfsd-ctl')
const port = 9090
const server = IPFSFactory.createServer(port)
const f = IPFSFactory.create({ remote: true, port: port })
server.start((err) => {
if (err) { throw err }
f.spawn((err, ipfsd) => {
if (err) { throw err }
ipfsd.api.id(function (err, id) {
if (err) { throw err }
console.log(id)
ipfsd.stop(server.stop)
})
})
})Disposable vs non Disposable nodes
ipfsd-ctl can spawn disposable and non-disposable daemons.
disposable- Creates on a temporary repo which will be optionally initialized and started (the default), as well cleaned up on process exit. Great for tests.non-disposable- Requires the user to initialize and start the node, as well as stop and cleanup after wards. Additionally, a non-disposable will allow you to pass a custom repo using therepoPathoption, if therepoPathis not defined, it will use the default repo for the node type ($HOME/.ipfsor$HOME/.jsipfs). TherepoPathparameter is ignored for disposable nodes, as there is a risk of deleting a live repo.
Batteries not included. Bring your own IPFS executable.
Install one or both of the following modules:
ipfs-> npm i ipfs- If you want to spawn js-ipfs nodes and/or daemons.go-ipfs-dep-> npm i go-ipfs-dep- If you want to spwan go-ipfs daemons.
API
IPFSFactory - const f = IPFSFactory.create([options])
IPFSFactory.create([options]) returns an object that will expose the df.spawn method
options- optional object with:remotebool - use remote endpoint to spawn the nodes.portnumber - remote endpoint point. Defaults to 43134.exec- IPFS executable path.ipfsd-ctlwill attempt to locate it by default. If you desire to spawn js-ipfs instances in the same process, pass the ref to the module instead (e.gexec: require('ipfs'))type- the daemon type, see below the optionsgo- spawn go-ipfs daemonjs- spawn js-ipfs daemonproc- spawn in-process js-ipfs instance. Needs to be called also with exec. Example:DaemonFactory.create({type: 'proc', exec: require('ipfs') }).
example: See Usage
Spawn a daemon with f.spawn([options], callback)
Spawn the daemon
optionsis an optional object the following properties:initbool (default true) or Object - should the node be initializedinitOptionsobject - should be of the form{bits: <size>}, which sets the desired key sizestartbool (default true) - should the node be startedrepoPathstring - the repository path to use for this node, ignored if node is disposabledisposablebool (default true) - a new repo is created and initialized for each invocation, as well as cleaned up automatically once the process exitsargs- array of cmd line arguments to be passed to ipfs daemonconfig- ipfs configuration options
callback- is a function with the signaturefunction (err, ipfsd)where:err- is the error set if spawning the node is unsuccessfulipfsd- is the daemon controller instance:api- a property ofipfsd, an instance of ipfs-api attached to the newly created ipfs node
example: See Usage
Get daemon version with f.version(callback)
Get the version without spawning a daemon
callback- is a function with the signaturefunction(err, version), where version might be one of the following:- if
typeis 'go' a version string likeipfs version <version number> - if
typeis 'js' a version string likejs-ipfs version: <version number> - if
typeis 'proc' an object with the following properties:- version - the ipfs version
- repo - the repo version
- commit - the commit hash for this version
- if
Remote endpoint - const server = IPFSFactory.createServer([options]) `
IPFSFactory.createServer starts a IPFSFactory endpoint.
example:
const IPFSFactory = require('ipfsd-ctl')
const server = IPFSFactory.createServer({ port: 12345 })
server.start((err) => {
if (err) { throw err }
console.log('endpoint is running')
server.stop((err) => {
if (err) { throw err }
console.log('endpoint has stopped')
})
})IPFS Daemon Controller - ipfsd
The IPFS daemon controller (ipfsd) allows you to interact with the spawned IPFS daemon.
ipfsd.apiAddr (getter)
Get the address (multiaddr) of connected IPFS API. Returns a multiaddr
ipfsd.gatewayAddr (getter)
Get the address (multiaddr) of connected IPFS HTTP Gateway. Returns a multiaddr.
ipfsd.repoPath (getter)
Get the current repo path. Returns string.
ipfsd.started (getter)
Is the node started. Returns a boolean.
init([initOpts], callback)
Initialize a repo.
initOpts (optional) is an object with the following properties:
keysize(default 2048) - The bit size of the identity key.directory(default IPFS_PATH if defined, or ~/.ipfs for go-ipfs and ~/.jsipfs for js-ipfs) - The location of the repo.pass(optional) - The passphrase of the key chain.
callback is a function with the signature function (Error, ipfsd) where err is an Error in case something goes wrong and ipfsd is the daemon controller instance.
ipfsd.cleanup(callback)
Delete the repo that was being used. If the node was marked as disposable this will be called automatically when the process is exited.
callback is a function with the signature function(err).
ipfsd.start(flags, callback)
Start the daemon.
flags - Flags array to be passed to the ipfs daemon command.
callback is a function with the signature function(err, ipfsApi) that receives an instance of ipfs-api on success or an instance of Error on failure
ipfsd.stop([callback])
Stop the daemon.
callback is a function with the signature function(err) callback - function that receives an instance of Error on failure
ipfsd.killProcess([callback])
Kill the ipfs daemon process.
First a SIGTERM is sent, after 10.5 seconds SIGKILL is sent if the process hasn't exited yet.
callback is a function with the signature function() called once the process is killed
ipfsd.pid(callback)
Get the pid of the ipfs daemon process. Returns the pid number
callback is a function with the signature function(err, pid) that receives the pid of the running daemon or an Error instance on failure
ipfsd.getConfig([key], callback)
Returns the output of an ipfs config command. If no key is passed, the whole config is returned as an object.
key (optional) - A specific config to retrieve.
callback is a function with the signature function(err, (Object|string)) that receives an object or string on success or an Error instance on failure
ipfsd.setConfig(key, value, callback)
Set a config value.
key - the key of the config entry to change/set
value - the config value to change/set
callback is a function with the signature function(err) callback - function that receives an Error instance on failure
ipfsd.version(callback)
Get the version of ipfs
callback is a function with the signature function(err, version)
IPFS HTTP Client - ipfsd.api
An instance of ipfs-api that is used to interact with the daemon.
This instance is returned for each successfully started IPFS daemon, when either df.spawn({start: true}) (the default) is called, or ipfsd.start() is invoked in the case of nodes that were spawned with df.spawn({start: false}).
Packaging
ipfsd-ctl can be packaged in Electron applications, but the ipfs binary has to be excluded from asar (Electron Archives).
read more about unpack files from asar.
ipfsd-ctl will try to detect if used from within an app.asar archive and tries to resolve ipfs from app.asar.unpacked. The ipfs binary is part of the go-ipfs-dep module.
electron-packager ./ --asar.unpackDir=node_modules/go-ipfs-depDevelopment
Project structure:
src
├── defaults
│ ├── config.json
│ └── options.json
├── endpoint # endpoint to support remote spawning
│ ├── routes.js
│ └── server.js
├── factory-client.js # IPFS Factories: client (remote), daemon (go or js) and in-proc (js)
├── factory-daemon.js
├── factory-in-proc.js
├── index.js
├── ipfsd-client.js # ipfsd (Daemon Controller): client (remote), daemon (go or js), in-proc (js)
├── ipfsd-daemon.js
├── ipfsd-in-proc.js
└── utils # Utils used by the Factories and Daemon Controllers
├── configure-node.js
├── exec.js
├── find-ipfs-executable.js
├── flatten.js
├── parse-config.js
├── repo
│ ├── create-browser.js
│ └── create-nodejs.js
├── run.js
├── set-config-value.js
└── tmp-dir.js
4 directories, 21 filesContribute
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.
