-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complete revamp! Spawn js/go daemons locally or remote (from the browser) #176
Changes from all commits
fb0c80e
04476b0
8a306c8
e5103ee
c75389e
aafcae6
8e0b9a5
3848812
a291799
0423702
0ef8413
9c432d2
06c8410
c8866e2
7585650
373d5b8
5d0adc3
aa475d8
c408f57
5e4e69a
3b82182
da2ef07
a10b2c5
e766c22
259ad3d
279f71a
d60f288
50e2b98
067dbc7
74eba98
78f37a1
43ed821
62832e1
3c0845b
14b4f3a
5b8e044
132ce96
e40e482
088eb22
f10693f
38026c6
2f68fd0
d970ee5
28ac033
5d1c5a4
79cfe25
0ceea38
81e8d61
1d81a0a
689ec23
79a7194
6687971
2df79ec
5f89431
591f9d9
9bf6bd5
2724038
67e0108
902b828
9cf21f1
f70ab83
d415a8a
8886cd8
8630ae9
adc8d25
9e543ca
a90c19a
da9e17b
53bfd15
cb7c957
36cf27d
a7728d8
5b1caae
177b1e5
fcd1728
19201d9
1a78f5f
94c966f
ef28e17
b4522b7
85557c9
c60dece
dbe6b37
aa8d72b
c35f6ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const createServer = require('./src').createServer | ||
|
||
const server = createServer() | ||
module.exports = { | ||
karma: { | ||
files: [{ | ||
pattern: 'test/fixtures/**/*', | ||
watched: false, | ||
served: true, | ||
included: false | ||
}], | ||
singleRun: true | ||
}, | ||
hooks: { | ||
browser: { | ||
pre: server.start.bind(server), | ||
post: server.stop.bind(server) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
version: "{build}" | ||
|
||
environment: | ||
matrix: | ||
- nodejs_version: "6" | ||
- nodejs_version: "8" | ||
|
||
# cache: | ||
# - node_modules | ||
|
||
platform: | ||
- x64 | ||
matrix: | ||
fast_finish: true | ||
|
||
install: | ||
- ps: Install-Product node $env:nodejs_version $env:platform | ||
# Install Node.js | ||
- ps: Install-Product node $env:nodejs_version | ||
|
||
# Upgrade npm | ||
- npm install -g npm | ||
|
||
# Output our current versions for debugging | ||
- node --version | ||
- npm --version | ||
|
||
# Install our package dependencies | ||
- npm install | ||
|
||
test_script: | ||
- npm test | ||
- npm run test | ||
|
||
build: off | ||
|
||
version: "{build}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,5 @@ node_modules | |
|
||
dist | ||
docs | ||
|
||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,33 @@ | |
[![Appveyor CI](https://ci.appveyor.com/api/projects/status/4p9r12ch0jtthnha?svg=true)](https://ci.appveyor.com/project/wubalubadubdub/js-ipfsd-ctl-a9ywu) | ||
[![Dependency Status](https://david-dm.org/ipfs/js-ipfsd-ctl.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfsd-ctl) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
|
||
> Control an ipfs node daemon using Node.js | ||
> Control an IPFS daemon using JavaScript in Node.js or in the Browser. | ||
|
||
``` | ||
+-----+ | ||
| H | | ||
| T | | ||
+-----------------------------+ | T | | ||
| Node.js | +-----------------------+ | P | +-----------------------------+ | ||
| | | | | | | BROWSER | | ||
| +-----------------------+ | | IPFS Daemon | | S | | | | ||
| | Local Daemon Ctrl | | | | | E | | +----------------------+ | | ||
| | +------- -------- R -----|---- Remote Daemon Ctrl | | | ||
| +-----------------------+ | +-----|-----------|-----+ | V | | | | | | ||
| | | | | E | | +----------------------+ | | ||
| +-----------------------+ | | | | R | | | | ||
| | IPFS API | | | | +-----+ | +----------------------+ | | ||
| | -------------+ | | | IPFS API | | | ||
| +-----------------------+ | +-----------------------|---- | | | ||
| | | +----------------------+ | | ||
+-----------------------------+ +-----------------------------+ | ||
``` | ||
|
||
## Table of Contents | ||
|
||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [Contribute](#contribute) | ||
- [License](#license) | ||
|
||
|
@@ -30,35 +51,210 @@ npm install --save ipfsd-ctl | |
|
||
IPFS daemons are already easy to start and stop, but this module is here to do it from JavaScript itself. | ||
|
||
### Spawn an IPFS daemon from Node.js | ||
|
||
```js | ||
// Start a disposable node, and get access to the api | ||
// print the node id, and kill the temporary daemon | ||
|
||
// IPFS_PATH will point to /tmp/ipfs_***** and will be | ||
// cleaned up when the process exits. | ||
// print the node id, and stop the temporary daemon | ||
|
||
var ipfsd = require('ipfsd-ctl') | ||
const DaemonFactory = require('ipfsd-ctl') | ||
const df = DaemonFactory.create() | ||
|
||
ipfsd.disposableApi(function (err, ipfs) { | ||
ipfs.id(function (err, id) { | ||
df.spawn(function (err, ipfsd) { | ||
if (err) { throw err } | ||
|
||
ipfsd.api.id(function (err, id) { | ||
if (err) { throw err } | ||
|
||
console.log(id) | ||
process.exit() | ||
ipfsd.stop() | ||
}) | ||
}) | ||
``` | ||
|
||
If you need want to use an existing ipfs installation you can set `$IPFS_EXEC=/path/to/ipfs` to ensure it uses that. | ||
### Spawn an IPFS daemon from the Browser using the provided remote endpoint | ||
|
||
```js | ||
// Start a remote disposable node, and get access to the api | ||
// print the node id, and stop the temporary daemon | ||
|
||
const DaemonFactory = require('ipfsd-ctl') | ||
|
||
const port = 9999 | ||
const server = DaemonFactory.createServer(port) | ||
const df = DaemonFactory.create({ remote: true, port: port }) | ||
|
||
server.start((err) => { | ||
if (err) { throw err } | ||
|
||
df.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 create two types of node controllers, `disposable` and `non-disposable`. A disposable node will be created on a temporary repo which will be optionally initialized and started (the default), as well cleaned up on process exit. A non-disposable node on the other hand, 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 the `repoPath` option, if the `repoPath` is not defined, it will use the default repo for the node type (`$HOME/.ipfs` or `$HOME/.jsipfs`). The `repoPath` parameter is ignored for disposable nodes, as there is a risk of deleting a live repo. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 🌟 for documenting all the things 👍 |
||
## IPFS executables | ||
|
||
`ipfsd-ctl` no longer installs go-ipfs nor js-ipfs dependencies, instead it expects them to be provided by the parent project. In order to be able to use both go and js daemons, please make sure that your project includes these two npm packages as dependencies. | ||
|
||
- `ipfs` - the js-ipfs implementation | ||
- `go-ipfs-dep` - the packaged go-ipfs implementation | ||
|
||
## API | ||
|
||
### Daemon Factory Class | ||
|
||
#### `DaemonFactory` - `const df = DaemonFactory.create([options])` | ||
|
||
`DaemonFactory.create([options])` returns an object that will expose the `df.spawn` method | ||
|
||
- `options` - an optional object with the following properties | ||
- `remote` bool - indicates if the factory should spawn local or remote nodes. By default, local nodes are spawned in Node.js and remote nodes are spawned in Browser environments. | ||
- `port` number - the port number to use for the remote factory. It should match the port on which `DaemonFactory.server` was started. Defaults to 9999. | ||
- `type` - the daemon type to create with this factory. See the section bellow for the supported types | ||
- `exec` - path to the desired IPFS executable to spawn, otherwise `ipfsd-ctl` will try to locate the correct one based on the `type`. In the case of `proc` type, exec is required and expects an IPFS coderef. | ||
|
||
`ipfsd-ctl` allows spawning different IPFS implementations, such as: | ||
|
||
- **`go`** - calling `DaemonFactory.create({type: 'go'})` will spawn a `go-ipfs` daemon. | ||
- **`js`** - calling `DaemonFactory.create({type: 'js'})` will spawn a `js-ipfs` daemon. | ||
- **`proc`** - calling `DaemonFactory.create({type: 'proc', exec: require('ipfs') })` will spawn an `in process js-ipfs node` using the provided code reference that implements the core IPFS API. Note that, `exec` option to `df.spawn()` is required if `type: 'proc'` is used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to pass in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would force users to bring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine either way :) Unless anyone objects, I'll change it. Was trying to find a better name either way |
||
|
||
#### DaemonFactory endpoint for remote spawning - `const server = `DaemonFactory.createServer([options]) ` | ||
|
||
`DaemonFactory.createServer` create an instance of the bundled REST API used by the remote controller. | ||
|
||
- exposes `start` and `stop` methods to start and stop the http server endpoint. | ||
|
||
#### Spawn a new daemon with `df.spawn` | ||
|
||
Spawn either a js-ipfs or go-ipfs daemon | ||
|
||
`df.spawn([options], callback)` | ||
|
||
`options` is an optional object the following properties: | ||
- `init` bool (default true) - should the node be initialized | ||
- `start` bool (default true) - should the node be started | ||
- `repoPath` string - the repository path to use for this node, ignored if node is disposable | ||
- `disposable` bool (default false) - a new repo is created and initialized for each invocation, as well as cleaned up automatically once the process exits | ||
- `args` - array of cmd line arguments to be passed to ipfs daemon | ||
- `config` - ipfs configuration options | ||
|
||
`callback` - is a function with the signature `function (err, ipfsd)` where: | ||
- `err` - is the error set if spawning the node is unsuccessful | ||
- `ipfsd` - is the daemon controller instance: | ||
- `api` - a property of `ipfsd`, an instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api) attached to the newly created ipfs node | ||
|
||
### 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) | ||
|
||
For more details see https://ipfs.github.io/js-ipfsd-ctl/. | ||
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. | ||
|
||
`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()` | ||
|
||
Get the pid of the `ipfs daemon` process. Returns the pid number | ||
|
||
#### `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 Client (`ipfsd.api`) | ||
|
||
An instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api#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), | ||
`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](https://electron.atom.io/docs/tutorial/application-packaging/#adding-unpacked-files-in-asar-archive). | ||
`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. | ||
|
||
`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. | ||
|
||
```bash | ||
electron-packager ./ --asar.unpackDir=node_modules/go-ipfs-dep | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a diagram explaining the difference between a local spawn and a remote spawn and why both exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.