Skip to content

Commit 744c254

Browse files
committed
feat: add ability to start js-ipfs nodes
1 parent f4c0d19 commit 744c254

File tree

5 files changed

+507
-37
lines changed

5 files changed

+507
-37
lines changed

package.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"description": "simple controls for an ipfs node",
55
"main": "src/index.js",
66
"scripts": {
7-
"lint": "aegir-lint",
8-
"coverage": "aegir-coverage",
9-
"test": "aegir-test --env node",
10-
"docs": "aegir-docs",
11-
"release": "aegir-release --env node --docs",
12-
"release-minor": "aegir-release --type minor --env node --docs",
13-
"release-major": "aegir-release --type major --env node --docs",
14-
"coverage-publish": "aegir-coverage publish"
7+
"lint": "aegir lint",
8+
"coverage": "aegir coverage",
9+
"test": "aegir test --target node",
10+
"docs": "aegir docs",
11+
"release": "aegir release -t node",
12+
"release-minor": "aegir release --type minor -t node",
13+
"release-major": "aegir release --type major -t node",
14+
"coverage-publish": "aegir coverage -u"
1515
},
1616
"engines": {
1717
"node": ">=6.0.0",
@@ -50,6 +50,8 @@
5050
"license": "MIT",
5151
"dependencies": {
5252
"async": "^2.5.0",
53+
"detect-node": "^2.0.3",
54+
"eslint-config-standard-jsx": "^4.0.2",
5355
"go-ipfs-dep": "0.4.11",
5456
"ipfs-api": "^14.3.5",
5557
"multiaddr": "^3.0.1",
@@ -59,9 +61,10 @@
5961
"subcomandante": "^1.0.5"
6062
},
6163
"devDependencies": {
62-
"aegir": "^11.0.2",
64+
"aegir": "^12.1.3",
6365
"chai": "^4.1.2",
6466
"dirty-chai": "^2.0.1",
67+
"ipfs": "^0.26.0",
6568
"is-running": "1.0.5",
6669
"mkdirp": "^0.5.1",
6770
"multihashes": "~0.4.10",
@@ -80,4 +83,4 @@
8083
"example": "examples",
8184
"test": "test"
8285
}
83-
}
86+
}

src/daemon.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ const isWindows = os.platform() === 'win32'
1414

1515
const exec = require('./exec')
1616

17-
const ipfsDefaultPath = findIpfsExecutable()
18-
1917
const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill
2018

21-
function findIpfsExecutable () {
22-
const rootPath = process.env.testpath ? process.env.testpath : __dirname
23-
19+
function findIpfsExecutable (isJs, rootPath) {
2420
let appRoot = path.join(rootPath, '..')
2521
// If inside <appname>.asar try to load from .asar.unpacked
2622
// this only works if asar was built with
@@ -31,7 +27,9 @@ function findIpfsExecutable () {
3127
appRoot = appRoot.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`)
3228
}
3329
const appName = isWindows ? 'ipfs.exe' : 'ipfs'
34-
const depPath = path.join('go-ipfs-dep', 'go-ipfs', appName)
30+
const depPath = isJs
31+
? path.join('ipfs', 'src', 'cli', 'bin.js')
32+
: path.join('go-ipfs-dep', 'go-ipfs', appName)
3533
const npm3Path = path.join(appRoot, '../', depPath)
3634
const npm2Path = path.join(appRoot, 'node_modules', depPath)
3735

@@ -49,7 +47,7 @@ function setConfigValue (node, key, value, callback) {
4947
exec(
5048
node.exec,
5149
['config', key, value, '--json'],
52-
{env: node.env},
50+
{ env: node.env },
5351
callback
5452
)
5553
}
@@ -92,13 +90,17 @@ class Node {
9290
* @returns {Node}
9391
*/
9492
constructor (path, opts, disposable) {
95-
this.path = path
96-
this.opts = opts || {}
97-
this.exec = process.env.IPFS_EXEC || ipfsDefaultPath
93+
const rootPath = process.env.testpath ? process.env.testpath : __dirname
94+
const isJS = process.env.IPFS_JS && process.env.IPFS_JS === 'true'
95+
96+
this.path = path || null
97+
this.opts = opts || { isJs: false }
98+
this.isJs = this.opts.isJs || isJS || false
99+
this.exec = process.env.IPFS_EXEC || findIpfsExecutable(this.isJs, rootPath)
98100
this.subprocess = null
99101
this.initialized = fs.existsSync(path)
100102
this.clean = true
101-
this.env = Object.assign({}, process.env, {IPFS_PATH: path})
103+
this.env = path ? Object.assign({}, process.env, { IPFS_PATH: path }) : process.env
102104
this.disposable = disposable
103105
this._apiAddr = null
104106
this._gatewayAddr = null
@@ -152,7 +154,7 @@ class Node {
152154
this.env.IPFS_PATH = this.path
153155
}
154156

155-
this._run(['init', '-b', keySize], {env: this.env}, (err, result) => {
157+
this._run(['init', '-b', keySize], { env: this.env }, (err, result) => {
156158
if (err) {
157159
return callback(err)
158160
}
@@ -226,14 +228,14 @@ class Node {
226228
let output = ''
227229
let returned = false
228230

229-
this.subprocess = this._run(args, {env: this.env}, {
231+
this.subprocess = this._run(args, { env: this.env }, {
230232
error: (err) => {
231233
// Only look at the last error
232234
const input = String(err)
233-
.split('\n')
234-
.map((l) => l.trim())
235-
.filter(Boolean)
236-
.slice(-1)[0] || ''
235+
.split('\n')
236+
.map((l) => l.trim())
237+
.filter(Boolean)
238+
.slice(-1)[0] || ''
237239

238240
if (input.match('daemon is running')) {
239241
// we're good
@@ -248,11 +250,11 @@ class Node {
248250
output += String(data)
249251

250252
const apiMatch = want.api
251-
? output.trim().match(/API server listening on (.*)/)
253+
? output.trim().match(/API (?:server|is) listening on[:]? (.*)/)
252254
: true
253255

254256
const gwMatch = want.gateway
255-
? output.trim().match(/Gateway (.*) listening on (.*)/)
257+
? output.trim().match(/Gateway (?:.*)? 27listening on[:]?(.*)/)
256258
: true
257259

258260
if (apiMatch && gwMatch && !returned) {
@@ -348,7 +350,7 @@ class Node {
348350
async.waterfall([
349351
(cb) => this._run(
350352
['config', key],
351-
{env: this.env},
353+
{ env: this.env },
352354
cb
353355
),
354356
(config, cb) => {
@@ -371,7 +373,7 @@ class Node {
371373
setConfig (key, value, callback) {
372374
this._run(
373375
['config', key, value, '--json'],
374-
{env: this.env},
376+
{ env: this.env },
375377
callback
376378
)
377379
}
@@ -386,18 +388,19 @@ class Node {
386388
replaceConf (file, callback) {
387389
this._run(
388390
['config', 'replace', file],
389-
{env: this.env},
391+
{ env: this.env },
390392
callback
391393
)
392394
}
395+
393396
/**
394397
* Get the version of ipfs
395398
*
396399
* @param {function(Error, string)} callback
397400
* @returns {undefined}
398401
*/
399402
version (callback) {
400-
this._run(['version'], {env: this.env}, callback)
403+
this._run(['version'], { env: this.env }, callback)
401404
}
402405
}
403406

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const IpfsDaemonController = {
5454
callback = path
5555
path = process.env.IPFS_PATH ||
5656
join(process.env.HOME ||
57-
process.env.USERPROFILE, '.ipfs')
57+
process.env.USERPROFILE, '.ipfs')
5858
}
5959

6060
process.nextTick(() => callback(null, new Node(path, opts)))

test/spawning-daemons.spec.js renamed to test/spawning-go-daemons.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe('daemon spawning', () => {
160160

161161
// actually running?
162162
done = once(done)
163-
exec('kill', ['-0', pid], {cleanup: true}, () => done())
163+
exec('kill', ['-0', pid], { cleanup: true }, () => done())
164164
})
165165
})
166166

@@ -180,7 +180,7 @@ describe('daemon spawning', () => {
180180

181181
// make sure it's not still running
182182
const poll = setInterval(() => {
183-
exec('kill', ['-0', pid], {cleanup: true}, {
183+
exec('kill', ['-0', pid], { cleanup: true }, {
184184
error () {
185185
clearInterval(poll)
186186
done()
@@ -335,7 +335,9 @@ describe('daemon spawning', () => {
335335
// skip on windows for now
336336
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326970190
337337
// fails on windows see https://github.com/ipfs/js-ipfs-api/issues/408
338-
if (isWindows) { return it.skip('uses the correct ipfs-api') }
338+
if (isWindows) {
339+
return it.skip('uses the correct ipfs-api')
340+
}
339341

340342
it('uses the correct ipfs-api', (done) => {
341343
ipfs.util.addFromFs(path.join(__dirname, 'fixtures/'), {

0 commit comments

Comments
 (0)