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
feat(factory): use the factory mode #340
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to bring in hapi? just using core-http should be enough for serving socket.io
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.
I like
hapi
because then I can just set CORS to true without having to write headers by hand. Alsohapi
is pretty modular, a lot of features (like template engine) are buy in modules, which makes it a 'smaller' framework