-
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
New API + use bin-wrapper (bundle in all arch without any concern) + global install #89
Changes from all commits
0523e82
a83f657
6977aa0
503aa15
258fbcb
cf54866
0de6a35
c7cadba
e6b4f77
331deab
67bee6a
a837058
e738f0d
e92eaf4
97a305d
ce0ef0e
88054c8
3253762
a4a3d2b
0b1b625
289d65b
e95741c
05fb3ef
c03c3ed
fe852d4
1214340
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 |
---|---|---|
|
@@ -33,3 +33,6 @@ node_modules | |
|
||
lib | ||
dist | ||
|
||
# Bin default dir | ||
vendor |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ Install: | |
npm install --save ipfsd-ctl | ||
``` | ||
|
||
__The current go ipfs version used is v0.4.3-rc3__ | ||
|
||
## Usage | ||
|
||
IPFS daemons are already easy to start and stop, but this module is here to do it from javascript itself. | ||
|
@@ -36,17 +38,58 @@ IPFS daemons are already easy to start and stop, but this module is here to do i | |
// IPFS_PATH will point to /tmp/ipfs_***** and will be | ||
// cleaned up when the process exits. | ||
|
||
var ipfsd = require('ipfsd-ctl') | ||
|
||
ipfsd.disposableApi(function (err, ipfs) { | ||
ipfs.id(function (err, id) { | ||
console.log(id) | ||
process.kill() | ||
const ipfsd = require('ipfsd-ctl') | ||
|
||
ipfsd.create((err, node) => { | ||
if (err) throw err | ||
node.startDaemon((err) => { | ||
if (err) throw err | ||
const ipfs = node.apiCtl() | ||
ipfs.id((err, id) => { | ||
console.log(id) | ||
process.kill() | ||
}) | ||
}) | ||
}) | ||
``` | ||
|
||
If you need want to use an existing ipfs installation you can set `$IPFS_EXEC=/path/to/ipfs` to ensure it uses that. | ||
The daemon controller safely spawns the node for you and exposes you an ipfs API client through `node.apiCtl()`. __If the parent process exits, the daemon will also be killed__ ensuring that the daemon isn't left hanging. | ||
|
||
This module works by downloading the binary once, on first use, if it detects that no current binary is available to use. So keep in mind that the first command executed might throw in some overhead. | ||
|
||
If you want to use an existing ipfs installation you can set `$IPFS_EXEC=/path/to/ipfs` to ensure it uses that. | ||
|
||
## API | ||
|
||
## ipfsd | ||
|
||
#### ipfsd.local(path, done) | ||
|
||
#### ipfsd.create(opts, done) | ||
|
||
## IPFSNode(path, opts, disposable) | ||
|
||
#### node.init(initOpts, done) | ||
|
||
#### node.shutdown(done) | ||
|
||
#### node.startDaemon(done) | ||
|
||
#### node.stopDaemon(done) | ||
|
||
#### node.apiCtl(done) | ||
|
||
#### node.daemonPid() | ||
|
||
#### node.getConfig(key, done) | ||
|
||
#### node.setConfig(key, value, done) | ||
|
||
#### node.replaceConf(file, done) | ||
|
||
#### node.version(done) | ||
|
||
#### node.subprocess | ||
|
||
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. @dignifiedquire @haadcode please review the above, for methods you would like to see on ipfsd-ctl :) 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. To be honest, I would like to use: and that's it. start() would automatically init a repo if one doesn't exist at the given path. However, I reckon that this would require a bigger re-think of the library and as I don't want to bikeshed it too much, I'm happy to use the current API if that's an easier way to bring this to a working state. 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. @haadcode now it is the time to bikeshed, since we have the opportunity to make a new version of ipfsd-ctl. Changing API is always harder as adoption increases, because that means breaking the interface for more people and harder to migrate. You are one of the primary users of this module and so, your input is super valuable when it comes to its API. |
||
## Contribute | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env node | ||
'use strict' | ||
|
||
console.log('Checking IPFS binary...') | ||
const binary = require('../src/binary')() | ||
const version = require('../src/config').version | ||
|
||
binary.check((err) => { | ||
if (err) throw err | ||
console.log(`IPFS binary version ${version} working`) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict' | ||
|
||
const BinWrapper = require('bin-wrapper') | ||
const config = require('./config') | ||
|
||
const binWrapper = new BinWrapper() | ||
.src(config.baseUrl + 'darwin-386.tar.gz', 'darwin', 'ia32') | ||
.src(config.baseUrl + 'darwin-amd64.tar.gz', 'darwin', 'x64') | ||
.src(config.baseUrl + 'freebsd-amd64.tar.gz', 'freebsd', 'x64') | ||
.src(config.baseUrl + 'linux-386.tar.gz', 'linux', 'ia32') | ||
.src(config.baseUrl + 'linux-amd64.tar.gz', 'linux', 'x64') | ||
.src(config.baseUrl + 'linux-arm.tar.gz', 'linux', 'arm') | ||
.src(config.baseUrl + 'windows-386.tar.gz', 'win32', 'ia32') | ||
.src(config.baseUrl + 'windows-amd64.tar.gz', 'win32', 'x64') | ||
.use(process.platform === 'win32' ? 'ipfs.exe' : 'ipfs') | ||
|
||
function Binary (execPath) { | ||
// Set dest path for the exec file | ||
binWrapper.dest(process.env.IPFS_EXEC || execPath || config.defaultExecPath) | ||
return { | ||
path: () => binWrapper.path(), | ||
// Has the binary been checked? | ||
checked: false, | ||
// Check that the binary exists and works | ||
check (cb) { | ||
if (this.checked) return cb() | ||
binWrapper.run(['version'], (err) => { | ||
// The binary is ok if no error poped up | ||
this.checked = !err | ||
return cb(err) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
module.exports = Binary |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
|
||
const config = { | ||
version: 'v0.4.3-rc4', // ipfs dist version | ||
defaultExecPath: path.join(__dirname, '..', 'vendor'), // default path for the downloaded exec | ||
gracePeriod: 7500 // amount of ms to wait before sigkill | ||
} | ||
|
||
const version = config.version | ||
config.baseUrl = `https://dist.ipfs.io/go-ipfs/${version}/go-ipfs_${version}_` | ||
|
||
module.exports = config |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const execa = require('execa') | ||
|
||
function exec (cmd, args, opts, handlers) { | ||
opts = opts || {} | ||
let err = '' | ||
let result = '' | ||
let callback | ||
// Handy method if we just want the result and err returned in a callback | ||
if (typeof handlers === 'function') { | ||
callback = handlers | ||
handlers = { | ||
error: callback, | ||
data: (data) => { | ||
result += data | ||
}, | ||
done: () => { | ||
if (err) return callback(new Error(err)) | ||
callback(null, result) | ||
} | ||
} | ||
} | ||
|
||
// The listeners that will actually be set on the process | ||
const listeners = { | ||
data: handlers.data, | ||
error: (data) => { | ||
err += data | ||
}, | ||
done: (code) => { | ||
if (code !== 0) { | ||
return handlers.error( | ||
new Error(`non-zero exit code ${code}\n | ||
while running: ${cmd} ${args.join(' ')}\n\n | ||
${err}`) | ||
) | ||
} | ||
if (handlers.done) handlers.done() | ||
} | ||
} | ||
|
||
const command = execa(cmd, args, opts) | ||
|
||
if (listeners.data) command.stdout.on('data', listeners.data) | ||
|
||
command.stderr.on('data', listeners.error) | ||
|
||
// If command fails to execute return directly to the handler | ||
command.on('error', handlers.error) | ||
|
||
command.on('close', listeners.done) | ||
|
||
return command | ||
} | ||
|
||
module.exports = exec |
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.
These should be methods of the
node
objectThere 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.
The version and local methods? @diasdavid