Skip to content

Commit adbef1b

Browse files
authored
feat: the big refactor! (#200)
1 parent 34a3f59 commit adbef1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2024
-1569
lines changed

Diff for: .aegir.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const createServer = require('./src').createServer
44

5-
const server = createServer()
5+
const server = createServer() // using defaults
6+
67
module.exports = {
78
karma: {
89
files: [{

Diff for: README.md

+115-72
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,45 @@
1-
# ipfsd-ctl
1+
# ipfsd-ctl, the IPFS Factory.
22

3-
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
3+
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
44
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
55
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6-
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
6+
[![standard-readme](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
77
[![Coverage Status](https://coveralls.io/repos/github/ipfs/js-ipfsd-ctl/badge.svg?branch=master)](https://coveralls.io/github/ipfs/js-ipfsd-ctl?branch=master)
88
[![Travis CI](https://travis-ci.org/ipfs/js-ipfsd-ctl.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfsd-ctl)
99
[![Circle CI](https://circleci.com/gh/ipfs/js-ipfsd-ctl.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfsd-ctl)
1010
[![Appveyor CI](https://ci.appveyor.com/api/projects/status/4p9r12ch0jtthnha?svg=true)](https://ci.appveyor.com/project/wubalubadubdub/js-ipfsd-ctl-a9ywu)
11-
[![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)
11+
[![Dependency Status](https://david-dm.org/ipfs/js-ipfsd-ctl.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfsd-ctl)
12+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
1213

13-
> Control an IPFS daemon using JavaScript in Node.js or in the Browser.
14-
15-
```
16-
+-----+
17-
| H |
18-
| T |
19-
+-----------------------------+ | T |
20-
| Node.js | +-----------------------+ | P | +-----------------------------+
21-
| | | | | | | BROWSER |
22-
| +-----------------------+ | | IPFS Daemon | | S | | |
23-
| | Local Daemon Ctrl | | | | | E | | +----------------------+ |
24-
| | +------- -------- R -----|---- Remote Daemon Ctrl | |
25-
| +-----------------------+ | +-----|-----------|-----+ | V | | | | |
26-
| | | | | E | | +----------------------+ |
27-
| +-----------------------+ | | | | R | | |
28-
| | IPFS API | | | | +-----+ | +----------------------+ |
29-
| | -------------+ | | | IPFS API | |
30-
| +-----------------------+ | +-----------------------|---- | |
31-
| | | +----------------------+ |
32-
+-----------------------------+ +-----------------------------+
33-
```
14+
> Spawn IPFS daemons using JavaScript!
3415
3516
## Table of Contents
3617

3718
- [Install](#install)
3819
- [Usage](#usage)
3920
- [API](#api)
21+
- [Packaging](#packaging)
4022
- [Contribute](#contribute)
4123
- [License](#license)
4224

4325
## Install
4426

45-
Install:
4627
```sh
4728
npm install --save ipfsd-ctl
4829
```
4930

5031
## Usage
5132

52-
IPFS daemons are already easy to start and stop, but this module is here to do it from JavaScript itself.
53-
54-
### Spawn an IPFS daemon from Node.js
33+
**Spawn an IPFS daemon from Node.js**
5534

5635
```js
5736
// Start a disposable node, and get access to the api
5837
// print the node id, and stop the temporary daemon
5938

60-
const DaemonFactory = require('ipfsd-ctl')
61-
const df = DaemonFactory.create()
39+
const IPFSFactory = require('ipfsd-ctl')
40+
const f = IPFSFactory.create()
6241

63-
df.spawn(function (err, ipfsd) {
42+
f.spawn(function (err, ipfsd) {
6443
if (err) { throw err }
6544

6645
ipfsd.api.id(function (err, id) {
@@ -72,22 +51,22 @@ df.spawn(function (err, ipfsd) {
7251
})
7352
```
7453

75-
### Spawn an IPFS daemon from the Browser using the provided remote endpoint
54+
**Spawn an IPFS daemon from the Browser using the provided remote endpoint**
7655

7756
```js
7857
// Start a remote disposable node, and get access to the api
7958
// print the node id, and stop the temporary daemon
8059

81-
const DaemonFactory = require('ipfsd-ctl')
60+
const IPFSFactory = require('ipfsd-ctl')
8261

8362
const port = 9999
84-
const server = DaemonFactory.createServer(port)
85-
const df = DaemonFactory.create({ remote: true, port: port })
63+
const server = IPFSFactory.createServer(port)
64+
const f = IPFSFactory.create({ remote: true, port: port })
8665

8766
server.start((err) => {
8867
if (err) { throw err }
8968

90-
df.spawn((err, ipfsd) => {
69+
f.spawn((err, ipfsd) => {
9170
if (err) { throw err }
9271

9372
ipfsd.api.id(function (err, id) {
@@ -102,61 +81,90 @@ server.start((err) => {
10281

10382
## Disposable vs non Disposable nodes
10483

105-
`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.
84+
`ipfsd-ctl` can spawn `disposable` and `non-disposable` daemons.
10685

107-
## IPFS executables
86+
- `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.
87+
- `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 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.
10888

109-
`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.
89+
## Batteries not included. Bring your own IPFS executable.
11090

111-
- `ipfs` - the js-ipfs implementation
112-
- `go-ipfs-dep` - the packaged go-ipfs implementation
91+
Install one or both of the following modules:
11392

114-
## API
93+
- `ipfs` - `> npm i ipfs` - If you want to spawn js-ipfs nodes and/or daemons.
94+
- `go-ipfs-dep` - `> npm i go-ipfs-dep` - If you want to spwan go-ipfs daemons.
11595

116-
### Daemon Factory Class
117-
118-
#### `DaemonFactory` - `const df = DaemonFactory.create([options])`
119-
120-
`DaemonFactory.create([options])` returns an object that will expose the `df.spawn` method
121-
122-
- `options` - an optional object with the following properties
123-
- `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.
124-
- `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.
125-
- `type` - the daemon type to create with this factory. See the section bellow for the supported types
126-
- `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.
127-
128-
`ipfsd-ctl` allows spawning different IPFS implementations, such as:
96+
## API
12997

130-
- **`go`** - calling `DaemonFactory.create({type: 'go'})` will spawn a `go-ipfs` daemon.
131-
- **`js`** - calling `DaemonFactory.create({type: 'js'})` will spawn a `js-ipfs` daemon.
132-
- **`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.
133-
134-
#### DaemonFactory endpoint for remote spawning - `const server = `DaemonFactory.createServer([options]) `
98+
### `IPFSFactory` - `const f = IPFSFactory.create([options])`
13599

136-
`DaemonFactory.createServer` create an instance of the bundled REST API used by the remote controller.
100+
`IPFSFactory.create([options])` returns an object that will expose the `df.spawn` method
137101

138-
- exposes `start` and `stop` methods to start and stop the http server endpoint.
102+
- `options` - optional object with:
103+
- `remote` bool - use remote endpoint to spawn the nodes.
104+
- `port` number - remote endpoint point. Defaults to 9999.
105+
- `exec` - IPFS executable path. `ipfsd-ctl` will 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.g `exec: require('ipfs')`)
106+
- `type` - the daemon type, see below the options
107+
- `go` - spawn go-ipfs daemon
108+
- `js` - spawn js-ipfs daemon
109+
- `proc` - spawn in-process js-ipfs instance. Needs to be called also with exec. Example: `DaemonFactory.create({type: 'proc', exec: require('ipfs') })`.
139110

140-
#### Spawn a new daemon with `df.spawn`
111+
**example:** See [Usage](#usage)
141112

142-
Spawn either a js-ipfs or go-ipfs daemon
113+
#### Spawn a daemon with `f.spawn([options], callback)`
143114

144-
`df.spawn([options], callback)`
115+
Spawn the daemon
145116

146-
`options` is an optional object the following properties:
117+
- `options` is an optional object the following properties:
147118
- `init` bool (default true) - should the node be initialized
148119
- `start` bool (default true) - should the node be started
149120
- `repoPath` string - the repository path to use for this node, ignored if node is disposable
150121
- `disposable` bool (default true) - a new repo is created and initialized for each invocation, as well as cleaned up automatically once the process exits
151122
- `args` - array of cmd line arguments to be passed to ipfs daemon
152123
- `config` - ipfs configuration options
153124

154-
`callback` - is a function with the signature `function (err, ipfsd)` where:
125+
- `callback` - is a function with the signature `function (err, ipfsd)` where:
155126
- `err` - is the error set if spawning the node is unsuccessful
156127
- `ipfsd` - is the daemon controller instance:
157128
- `api` - a property of `ipfsd`, an instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api) attached to the newly created ipfs node
158129

159-
### IPFS Daemon Controller (`ipfsd`)
130+
**example:** See [Usage](#usage)
131+
132+
#### Get daemon version with `f.version(callback)`
133+
134+
Get the version without spawning a daemon
135+
136+
- `callback` - is a function with the signature `function(err, version)`, where version might be one of the following:
137+
- if `type` is 'go' a version string like `ipfs version <version number>`
138+
- if `type` is 'js' a version string like `js-ipfs version: <version number>`
139+
- if `type` is 'proc' an object with the following properties:
140+
- version - the ipfs version
141+
- repo - the repo version
142+
- commit - the commit hash for this version
143+
144+
### Remote endpoint - `const server = `IPFSFactory.createServer([options]) `
145+
146+
`IPFSFactory.createServer` starts a IPFSFactory endpoint.
147+
148+
**example:**
149+
```
150+
const IPFSFactory = require('ipfsd-ctl')
151+
152+
const server = IPFSFactory.createServer({ port: 12345 })
153+
154+
server.start((err) => {
155+
if (err) { throw err }
156+
157+
console.log('endpoint is running')
158+
159+
server.stop((err) => {
160+
if (err) { throw err }
161+
162+
console.log('endpoint has stopped')
163+
})
164+
})
165+
```
166+
167+
### IPFS Daemon Controller - `ipfsd`
160168

161169
The IPFS daemon controller (`ipfsd`) allows you to interact with the spawned IPFS daemon.
162170

@@ -246,13 +254,13 @@ Get the version of ipfs
246254

247255
`callback` is a function with the signature `function(err, version)`
248256

249-
### IPFS Client (`ipfsd.api`)
257+
### IPFS HTTP Client - `ipfsd.api`
250258

251259
An instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api#api) that is used to interact with the daemon.
252260

253-
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})`.
261+
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})`.
254262

255-
### Packaging
263+
## Packaging
256264

257265
`ipfsd-ctl` can be packaged in Electron applications, but the ipfs binary has to be excluded from asar (Electron Archives).
258266
[read more about unpack files from asar](https://electron.atom.io/docs/tutorial/application-packaging/#adding-unpacked-files-in-asar-archive).
@@ -265,6 +273,41 @@ electron-packager ./ --asar.unpackDir=node_modules/go-ipfs-dep
265273

266274
See [electron asar example](https://github.com/ipfs/js-ipfsd-ctl/tree/master/examples/electron-asar/)
267275

276+
## Development
277+
278+
Project structure:
279+
280+
```
281+
src
282+
├── defaults
283+
│   ├── config.json
284+
│   └── options.json
285+
├── endpoint # endpoint to support remote spawning
286+
│   ├── routes.js
287+
│   └── server.js
288+
├── factory-client.js # IPFS Factories: client (remote), daemon (go or js) and in-proc (js)
289+
├── factory-daemon.js
290+
├── factory-in-proc.js
291+
├── index.js
292+
├── ipfsd-client.js # ipfsd (Daemon Controller): client (remote), daemon (go or js), in-proc (js)
293+
├── ipfsd-daemon.js
294+
├── ipfsd-in-proc.js
295+
└── utils # Utils used by the Factories and Daemon Controllers
296+
├── configure-node.js
297+
├── exec.js
298+
├── find-ipfs-executable.js
299+
├── flatten.js
300+
├── parse-config.js
301+
├── repo
302+
│   ├── create-browser.js
303+
│   └── create-nodejs.js
304+
├── run.js
305+
├── set-config-value.js
306+
└── tmp-dir.js
307+
308+
4 directories, 21 files
309+
```
310+
268311
## Contribute
269312

270313
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfsd-ctl/issues)!

Diff for: package.json

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
{
22
"name": "ipfsd-ctl",
33
"version": "0.27.3",
4-
"description": "simple controls for an ipfs node",
4+
"description": "Spawn IPFS Daemons, JS or Go",
55
"main": "src/index.js",
66
"scripts": {
77
"lint": "aegir lint",
8-
"coverage": "COVERAGE=true aegir coverage --timeout 50000",
8+
"docs": "aegir docs",
9+
"build": "aegir build",
910
"test": "aegir test -t node -t browser --no-cors",
1011
"test:node": "aegir test -t node",
1112
"test:browser": "aegir test -t browser --no-cors",
12-
"docs": "aegir docs",
13-
"release": "aegir release -t node",
14-
"release-minor": "aegir release --type minor -t node",
15-
"release-major": "aegir release --type major -t node",
13+
"release": "aegir release",
14+
"release-minor": "aegir release --type minor",
15+
"release-major": "aegir release --type major",
16+
"coverage": "COVERAGE=true aegir coverage --timeout 50000",
1617
"coverage-publish": "aegir coverage -u"
1718
},
1819
"browser": {
19-
"./src/utils/create-repo-nodejs.js": "./src/utils/create-repo-browser.js",
20-
"./src/daemon-node.js": false,
21-
"./src/remote-node/routes.js": false,
22-
"./src/exec.js": false,
2320
"hapi": false,
2421
"glob": false,
2522
"fs": false,
2623
"joi": false,
2724
"stream": "readable-stream",
28-
"http": "stream-http"
25+
"http": "stream-http",
26+
"./src/utils/repo/create-nodejs.js": "./src/utils/repo/create-browser.js",
27+
"./src/utils/exec.js": false,
28+
"./src/utils/find-ipfs-executable.js": false,
29+
"./src/utils/tmp-dir.js": "./src/utils/tmp-dir-browser.js",
30+
"./src/utils/run.js": false,
31+
"./src/factory-daemon.js": false,
32+
"./src/ipfsd-daemon.js": false,
33+
"./src/endpoint/server.js": false,
34+
"./src/endpoint/routes.js": false,
35+
"./test/utils/df-config-nodejs.js": "./test/utils/df-config-browser.js"
2936
},
3037
"engines": {
3138
"node": ">=6.0.0",

0 commit comments

Comments
 (0)