From fc0326217ade45e66b09155d74fce16fde561db0 Mon Sep 17 00:00:00 2001 From: haad Date: Sun, 22 May 2016 15:07:21 +0200 Subject: [PATCH] Fix js-ipfs-api compatibility --- src/OrbitDB.js | 37 ++++++++++++++++++---------- test/client.test.js | 43 ++++++++++++++++---------------- test/counterdb.test.js | 56 +++++++++++++++++++++--------------------- 3 files changed, 74 insertions(+), 62 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 1be44f8..d96ac20 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -37,7 +37,7 @@ class OrbitDB { } disconnect() { - this._pubsub.disconnect(); + if(this._pubsub) this._pubsub.disconnect(); this.stores = {}; this.user = null; this.network = null; @@ -76,7 +76,7 @@ class OrbitDB { _onWrite(dbname, hash) { // console.log(".WRITE", dbname); if(!hash) throw new Error("Hash can't be null!"); - this._pubsub.publish(dbname, hash); + if(this._pubsub) this._pubsub.publish(dbname, hash); this.events.emit('data', dbname, hash); } @@ -101,7 +101,7 @@ class OrbitDB { } _onClose(dbname) { - this._pubsub.unsubscribe(dbname); + if(this._pubsub) this._pubsub.unsubscribe(dbname); delete this.stores[dbname]; this.events.emit('closed', dbname); } @@ -111,16 +111,27 @@ class OrbitDB { const readNetworkInfo = (hash) => { return new Promise((resolve, reject) => { - this._ipfs.files.cat(hash, (err, res) => { - if(err) return reject(e) + // js-ipfs-api + this._ipfs.cat(hash, (err, res) => { + if(err) return reject(err) let buf = ''; - res.on('data', (res) => { - res.stream - .on('error', (err) => reject(err)) - .on('data', (data) => buf += data) - .on('end', () => resolve(buf.toString())) - }) + res + .on('error', (err) => reject(err)) + .on('data', (data) => buf += data) + .on('end', () => resolve(buf.toString())) }); + // js-ipfs + // this._ipfs.files.cat(hash, (err, res) => { + // if(err) return reject(err) + // let buf = ''; + // res.on('data', (res) => { + // res.stream + // .on('error', (err) => reject(err)) + // .on('data', (data) => buf += data) + // .on('end', () => resolve(buf.toString())) + // }) + // }); + // mock // resolve(JSON.stringify({ // name: 'localhost dev network', // publishers: ['localhost:3333'] @@ -138,7 +149,7 @@ class OrbitDB { }) .then(() => { this._pubsub = new PubSub(); - logger.warn(`Connecting to Pubsub at '${host}:${port}'`); + logger.info(`Connecting to Pubsub at '${host}:${port}'`); return this._pubsub.connect(host, port, username, password) }) .then(() => { @@ -149,7 +160,7 @@ class OrbitDB { .catch((e) => { logger.warn("Couldn't connect to Pubsub: " + e.message); if(!allowOffline) { - logger.debug("'allowOffline' set to false, terminating"); + logger.warn("'allowOffline' set to false, terminating"); if(this._pubsub) this._pubsub.disconnect(); throw e; } diff --git a/test/client.test.js b/test/client.test.js index 1a45f65..e296f8c 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -36,27 +36,27 @@ const IpfsApis = [ // stop: () => Promise.resolve() stop: () => new Promise((resolve, reject) => ipfs.goOffline(resolve)) }, -// { -// // js-ipfs-api via local daemon -// start: () => { -// return new Promise((resolve, reject) => { -// ipfsd.disposableApi((err, ipfs) => { -// if(err) console.error(err); -// resolve(ipfs); -// }); -// // ipfsd.local((err, node) => { -// // if(err) reject(err); -// // ipfsDaemon = node; -// // ipfsDaemon.startDaemon((err, ipfs) => { -// // if(err) reject(err); -// // resolve(ipfs); -// // }); -// // }); -// }); -// }, -// stop: () => Promise.resolve() -// // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve)) -// } +{ + // js-ipfs-api via local daemon + start: () => { + return new Promise((resolve, reject) => { + ipfsd.disposableApi((err, ipfs) => { + if(err) console.error(err); + resolve(ipfs); + }); + // ipfsd.local((err, node) => { + // if(err) reject(err); + // ipfsDaemon = node; + // ipfsDaemon.startDaemon((err, ipfs) => { + // if(err) reject(err); + // resolve(ipfs); + // }); + // }); + }); + }, + stop: () => Promise.resolve() + // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve)) +} ]; OrbitServer.start(); @@ -92,6 +92,7 @@ IpfsApis.forEach(function(ipfsApi) { after(async((done) => { if(db) db.delete(); if(client) client.disconnect(); + if(client2) client2.disconnect(); await(ipfsApi.stop()); done(); })); diff --git a/test/counterdb.test.js b/test/counterdb.test.js index 8c6ef98..6555f18 100644 --- a/test/counterdb.test.js +++ b/test/counterdb.test.js @@ -20,39 +20,39 @@ const ipfsPath = '/tmp/orbittests'; let ipfs, ipfsDaemon; const IpfsApis = [ -{ - // js-ipfs - start: () => { - return new Promise((resolve, reject) => { - const IPFS = require('ipfs') - const ipfs = new IPFS(); - ipfs.goOnline(() => resolve(ipfs)); - // resolve(ipfs); - }); - }, - stop: () => new Promise((resolve, reject) => ipfs.goOffline(resolve)) -}, // { -// // js-ipfs-api via local daemon +// // js-ipfs // start: () => { // return new Promise((resolve, reject) => { -// ipfsd.disposableApi((err, ipfs) => { -// if(err) console.error(err); -// resolve(ipfs); -// }); -// // ipfsd.local((err, node) => { -// // if(err) reject(err); -// // ipfsDaemon = node; -// // ipfsDaemon.startDaemon((err, ipfs) => { -// // if(err) reject(err); -// // resolve(ipfs); -// // }); -// // }); +// const IPFS = require('ipfs') +// const ipfs = new IPFS(); +// ipfs.goOnline(() => resolve(ipfs)); +// // resolve(ipfs); // }); // }, -// stop: () => Promise.resolve() -// // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve)) -// } +// stop: () => new Promise((resolve, reject) => ipfs.goOffline(resolve)) +// }, +{ + // js-ipfs-api via local daemon + start: () => { + return new Promise((resolve, reject) => { + ipfsd.disposableApi((err, ipfs) => { + if(err) console.error(err); + resolve(ipfs); + }); + // ipfsd.local((err, node) => { + // if(err) reject(err); + // ipfsDaemon = node; + // ipfsDaemon.startDaemon((err, ipfs) => { + // if(err) reject(err); + // resolve(ipfs); + // }); + // }); + }); + }, + stop: () => Promise.resolve() + // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve)) +} ]; // OrbitServer.start(); // uncomment if running this test suite stand-alone