From 6ab84ba064d186c9236ed1272685f2f6a982774e Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Sun, 20 Dec 2015 21:10:48 +0100 Subject: [PATCH] bugfixing and better limited mode --- lib/boards-api.js | 89 +++++++++++++++++++-------------------- webapp/pages/getipfs.jsx | 5 +-- webapp/pages/settings.jsx | 30 +++++++++---- 3 files changed, 67 insertions(+), 57 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index 055a60f..2e3284e 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -748,59 +748,58 @@ BoardsAPI.prototype.isNode = function () { // Initialize API BoardsAPI.prototype.init = function (done) { if (this.isInit) return - this.ipfs.id((err, res) => { + this.ipfs.version((err, res) => { if (err) { - console.log('Error while getting OWN ID:', err) - if (this.isRunningFromGateway()) { - console.log('Running in limited mode') - this.limited = true - } else console.log('Limited mode not available') + this.limited = this.isRunningFromGateway() ? 2 : false this.ee.emit('error', err) this.ee.emit('init', err, this.limited) - if (done && done.apply) { - done(err, this.limited) - } - } else if (res.ID) { - console.log('I am', res.ID) - this.id = res.ID - this.resolveIPNS(res.ID) - console.log('Version is', this.version) - this.ipfs.add(new Buffer(this.version), (err2, r) => { - if (err2) { - this.ee.emit('error', err2) - console.log('Error while calculating version hash:', err2) - this.ee.emit('init', err2) - if (done && done.apply) done(err2) - } else { - if (r && r.Hash) this.version_hash = r.Hash - if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash - console.log('Version hash is', this.version_hash) - this.ipfs.version((err, res) => { - if (err) { - this.ee.emit('error', err) - this.ee.emit('init', err) - console.log('Error while getting ipfs version:', err) - if (done && done.apply) done(err) - } else { - this.ipfs_version = res.Version.split('-')[0] - console.log('IPFS Version is', res.Version) - if (semver.satisfies(this.ipfs_version, '~0.4.0')) { - console.log('IPFS version is supported') + console.log('Error while getting ipfs version:', err) + if (done && done.apply) done(err, this.limited) + } else { + this.ipfs_version = res.Version.split('-')[0] + console.log('IPFS Version is', res.Version) + if (semver.satisfies(this.ipfs_version, '~0.4.0')) { + console.log('IPFS version is supported') + this.ipfs.id((err, res) => { + if (err) { + console.log('Error while getting OWN ID:', err) + this.limited = this.isRunningFromGateway() ? 1 : false + this.ee.emit('error', err) + this.ee.emit('init', err, this.limited) + if (done && done.apply) { + done(err, this.limited) + } + } else if (res.ID) { + console.log('I am', res.ID) + this.id = res.ID + this.resolveIPNS(res.ID) + console.log('Version is', this.version) + this.ipfs.add(new Buffer(this.version), (err2, r) => { + if (err2) { + this.ee.emit('error', err2) + console.log('Error while calculating version hash:', err2) + this.ee.emit('init', err2, this.limited) + if (done && done.apply) done(err2) + } else { + if (r && r.Hash) this.version_hash = r.Hash + if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash + console.log('Version hash is', this.version_hash) + // DONE! this.ee.emit('init', undefined) this.isInit = true delete this.init_error if (done && done.apply) done(null) - } else { - var e = { Message: 'IPFS Version not supported. This app supports go-ipfs 0.4.x' } - if (done && done.apply) done(e) - console.log('Error:', e.Message) - this.ee.emit('error', e) - this.ee.emit('init', e) } - } - }) - } - }) + }) + } + }) + } else { + var e = { Message: 'IPFS Version not supported. This app supports go-ipfs 0.4.x' } + if (done && done.apply) done(e) + console.log('Error:', e.Message) + this.ee.emit('error', e) + this.ee.emit('init', e) + } } }) } diff --git a/webapp/pages/getipfs.jsx b/webapp/pages/getipfs.jsx index 1365e04..cf2c260 100644 --- a/webapp/pages/getipfs.jsx +++ b/webapp/pages/getipfs.jsx @@ -40,21 +40,20 @@ module.exports = React.createClass({ console.log('Connection to go-ipfs has timed out (probably due to CORS)') if (this.isMounted() && !this.state.connected && !this.state.limited) { this.setState({ long: true }) - this.init(this.props) } }, 5000) }, getContent () { if (this.state.limited) { return
-

You're running in limited mode

+

You're running in limited mode

Sorry, but at the moment an external application is needed to fully take advantage of the app

Only a few features are available in limited mode.

Why am I running in limited mode?
} else { return
-

Connection to IPFS not available

+

Connection to IPFS not available

Sorry, but at the moment an external application is needed to try the Prototype

Tip: you can also run in limited mode by loading the app from an IPFS Gateway.

diff --git a/webapp/pages/settings.jsx b/webapp/pages/settings.jsx index f7f7fd3..13d164a 100644 --- a/webapp/pages/settings.jsx +++ b/webapp/pages/settings.jsx @@ -7,14 +7,6 @@ module.exports = function (boardsAPI) { return { addr: 'localhost', port: 5001, api: false } }, getInitialState: function () { - boardsAPI.use(boards => { - if (boards.isInit && this.isMounted()) this.setState({ api: true }) - boards.getEventEmitter().on('init', (err, limited) => { - if ((!err || limited) && this.isMounted()) { - this.setState({ api: true, limited }) - } - }) - }) var s = window.localStorage.getItem('ipfs-boards-settings') var obj = this.getDefaults() try { @@ -24,6 +16,20 @@ module.exports = function (boardsAPI) { } return obj || this.getDefaults() }, + componentDidMount () { + boardsAPI.use(boards => { + if (boards.isInit || boards.limited) { + this.setState({ api: true, limited: boards.limited }) + } else { + this.setState({ error: boards.init_error || 'neither limited mode nor the API is available.' }) + } + boards.getEventEmitter().on('init', (err, limited) => { + if (this.isMounted()) { + this.setState({ error: err, api: (!err || limited), limited }) + } + }) + }) + }, save: function () { if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) { window.alert('Port number invalid') @@ -46,7 +52,13 @@ module.exports = function (boardsAPI) { } }, isOK: function () { - if (this.state.limited) { + if (this.state.error) { + console.log('Error', this.state.error) + return
+
Error
+

{this.state.error}

+
+ } else if (this.state.limited) { return
Limited Mode

Some features may not be available.