This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from ipfs/feat/ipfs-factory
feat(factory): use the factory mode
- Loading branch information
Showing
53 changed files
with
321 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
node_modules | ||
*.log | ||
test/tmp-disposable-nodes-addrs.json | ||
test/setup/tmp-disposable-nodes-addrs.json | ||
dist | ||
lib | ||
coverage | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
'use strict' | ||
|
||
require('./setup') | ||
require('./setup/setup-ipfs-api-clients') |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict' | ||
|
||
// const defaultConfig = require('./default-config.json') | ||
const ipfsd = require('ipfsd-ctl') | ||
const series = require('run-series') | ||
|
||
module.exports = Factory | ||
|
||
function Factory () { | ||
if (!(this instanceof Factory)) { | ||
return new Factory() | ||
} | ||
|
||
const nodes = [] | ||
|
||
this.spawnNode = (repoPath, config, callback) => { | ||
if (typeof repoPath === 'function') { | ||
callback = repoPath | ||
repoPath = undefined | ||
} | ||
if (typeof config === 'function') { | ||
callback = config | ||
config = undefined | ||
} | ||
|
||
// if (!repoPath) { | ||
// repoPath = '/tmp/.ipfs-' + Math.random() | ||
// .toString() | ||
// .substring(2, 8) | ||
// } | ||
|
||
// TODO | ||
// - [ ] Support custom repoPath | ||
// - [ ] Support custom config | ||
// This will come once the new ipfsd-ctl is | ||
// complete: https://github.com/ipfs/js-ipfsd-ctl/pull/89 | ||
|
||
spawnEphemeralNode((err, node) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
nodes.push(node) | ||
callback(null, node.apiAddr) | ||
}) | ||
} | ||
|
||
this.dismantle = function (callback) { | ||
series( | ||
nodes.map((node) => { | ||
return node.stopDaemon | ||
}), callback) | ||
} | ||
} | ||
|
||
function spawnEphemeralNode (callback) { | ||
ipfsd.disposable((err, node) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
// Note: we have to set each config value | ||
// independently since the config/replace endpoint | ||
// doesn't work as expected | ||
series([ | ||
(cb) => { | ||
node.setConfig('Bootstrap', null, cb) | ||
}, | ||
(cb) => { | ||
node.setConfig('Discovery', '{}', cb) | ||
}, | ||
(cb) => { | ||
const headers = { | ||
HTTPHeaders: { | ||
'Access-Control-Allow-Origin': ['*'] | ||
} | ||
} | ||
node.setConfig('API', JSON.stringify(headers), cb) | ||
}, | ||
(cb) => { | ||
node.startDaemon(cb) | ||
} | ||
], (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
callback(null, node) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const io = require('socket.io-client') | ||
const ipfsAPI = require('../../src') | ||
|
||
module.exports = Factory | ||
|
||
function Factory () { | ||
if (!(this instanceof Factory)) { | ||
return new Factory() | ||
} | ||
const sioOptions = { | ||
transports: ['websocket'], | ||
'force new connection': true | ||
} | ||
const sioUrl = 'http://localhost:55155' | ||
let sioConnected = false | ||
let ioC | ||
|
||
this.spawnNode = (repoPath, config, callback) => { | ||
if (typeof repoPath === 'function') { | ||
callback = repoPath | ||
repoPath = undefined | ||
} | ||
if (typeof config === 'function') { | ||
callback = config | ||
config = undefined | ||
} | ||
|
||
if (sioConnected) { | ||
spawnNode() | ||
} else { | ||
ioC = io.connect(sioUrl, sioOptions) | ||
ioC.on('connect_error', callback) | ||
ioC.on('connect', () => { | ||
sioConnected = true | ||
spawnNode() | ||
}) | ||
} | ||
|
||
function spawnNode () { | ||
ioC.once('fc-node', (apiAddr) => { | ||
const ipfs = ipfsAPI(apiAddr) | ||
callback(null, ipfs) | ||
}) | ||
ioC.emit('fs-spawn-node', repoPath, config) | ||
} | ||
} | ||
|
||
this.dismantle = function (callback) { | ||
ioC.once('fc-nodes-shutdown', callback) | ||
ioC.emit('fs-dismantle') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
const SocketIO = require('socket.io') | ||
const DaemonSpawner = require('./daemon-spawner') | ||
|
||
module.exports = (http) => { | ||
const io = new SocketIO(http.listener) | ||
io.on('connection', handle) | ||
|
||
const ds = new DaemonSpawner() | ||
|
||
function handle (socket) { | ||
socket.on('fs-spawn-node', spawnNode.bind(socket)) | ||
socket.on('fs-dismantle', dismantle.bind(socket)) | ||
} | ||
|
||
function spawnNode (repoPath, config) { | ||
ds.spawnNode(repoPath, config, (err, apiAddr) => { | ||
if (err) { | ||
throw err | ||
} | ||
this.emit('fc-node', apiAddr) | ||
}) | ||
} | ||
|
||
function dismantle () { | ||
ds.dismantle((err) => { | ||
if (err) { | ||
throw err | ||
} | ||
this.emit('fc-nodes-shutdown') | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict' | ||
|
||
const Hapi = require('hapi') | ||
|
||
const port = Number(process.env.PORT) || 55155 | ||
const options = { | ||
connections: { | ||
routes: { | ||
cors: true | ||
} | ||
} | ||
} | ||
|
||
module.exports = (callback) => { | ||
const http = new Hapi.Server(options) | ||
|
||
http.connection({ port: port }) | ||
|
||
http.start((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
require('./factory-server-routes')(http) | ||
|
||
callback(null, http) | ||
// note: http.stop(callback) to stop the server :) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
const gulp = require('gulp') | ||
const factoryServer = require('./factory-server') | ||
|
||
let factory | ||
|
||
gulp.task('factory:start', (done) => { | ||
factoryServer((err, http) => { | ||
if (err) { | ||
throw err | ||
} | ||
factory = http | ||
done() | ||
}) | ||
}) | ||
|
||
gulp.task('factory:stop', (done) => { | ||
factory.stop(done) | ||
}) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.