Fix js-ipfs-api compatibility

This commit is contained in:
haad 2016-05-22 15:07:21 +02:00
parent 027ceb747f
commit fc0326217a
3 changed files with 74 additions and 62 deletions

View File

@ -37,7 +37,7 @@ class OrbitDB {
} }
disconnect() { disconnect() {
this._pubsub.disconnect(); if(this._pubsub) this._pubsub.disconnect();
this.stores = {}; this.stores = {};
this.user = null; this.user = null;
this.network = null; this.network = null;
@ -76,7 +76,7 @@ class OrbitDB {
_onWrite(dbname, hash) { _onWrite(dbname, hash) {
// console.log(".WRITE", dbname); // console.log(".WRITE", dbname);
if(!hash) throw new Error("Hash can't be null!"); 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); this.events.emit('data', dbname, hash);
} }
@ -101,7 +101,7 @@ class OrbitDB {
} }
_onClose(dbname) { _onClose(dbname) {
this._pubsub.unsubscribe(dbname); if(this._pubsub) this._pubsub.unsubscribe(dbname);
delete this.stores[dbname]; delete this.stores[dbname];
this.events.emit('closed', dbname); this.events.emit('closed', dbname);
} }
@ -111,16 +111,27 @@ class OrbitDB {
const readNetworkInfo = (hash) => { const readNetworkInfo = (hash) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._ipfs.files.cat(hash, (err, res) => { // js-ipfs-api
if(err) return reject(e) this._ipfs.cat(hash, (err, res) => {
if(err) return reject(err)
let buf = ''; let buf = '';
res.on('data', (res) => { res
res.stream .on('error', (err) => reject(err))
.on('error', (err) => reject(err)) .on('data', (data) => buf += data)
.on('data', (data) => buf += data) .on('end', () => resolve(buf.toString()))
.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({ // resolve(JSON.stringify({
// name: 'localhost dev network', // name: 'localhost dev network',
// publishers: ['localhost:3333'] // publishers: ['localhost:3333']
@ -138,7 +149,7 @@ class OrbitDB {
}) })
.then(() => { .then(() => {
this._pubsub = new PubSub(); 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) return this._pubsub.connect(host, port, username, password)
}) })
.then(() => { .then(() => {
@ -149,7 +160,7 @@ class OrbitDB {
.catch((e) => { .catch((e) => {
logger.warn("Couldn't connect to Pubsub: " + e.message); logger.warn("Couldn't connect to Pubsub: " + e.message);
if(!allowOffline) { if(!allowOffline) {
logger.debug("'allowOffline' set to false, terminating"); logger.warn("'allowOffline' set to false, terminating");
if(this._pubsub) this._pubsub.disconnect(); if(this._pubsub) this._pubsub.disconnect();
throw e; throw e;
} }

View File

@ -36,27 +36,27 @@ const IpfsApis = [
// stop: () => Promise.resolve() // stop: () => Promise.resolve()
stop: () => new Promise((resolve, reject) => ipfs.goOffline(resolve)) stop: () => new Promise((resolve, reject) => ipfs.goOffline(resolve))
}, },
// { {
// // js-ipfs-api via local daemon // js-ipfs-api via local daemon
// start: () => { start: () => {
// return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// ipfsd.disposableApi((err, ipfs) => { ipfsd.disposableApi((err, ipfs) => {
// if(err) console.error(err); if(err) console.error(err);
// resolve(ipfs); resolve(ipfs);
// }); });
// // ipfsd.local((err, node) => { // ipfsd.local((err, node) => {
// // if(err) reject(err); // if(err) reject(err);
// // ipfsDaemon = node; // ipfsDaemon = node;
// // ipfsDaemon.startDaemon((err, ipfs) => { // ipfsDaemon.startDaemon((err, ipfs) => {
// // if(err) reject(err); // if(err) reject(err);
// // resolve(ipfs); // resolve(ipfs);
// // }); // });
// // }); // });
// }); });
// }, },
// stop: () => Promise.resolve() stop: () => Promise.resolve()
// // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve)) // stop: () => new Promise((resolve, reject) => ipfsDaemon.stopDaemon(resolve))
// } }
]; ];
OrbitServer.start(); OrbitServer.start();
@ -92,6 +92,7 @@ IpfsApis.forEach(function(ipfsApi) {
after(async((done) => { after(async((done) => {
if(db) db.delete(); if(db) db.delete();
if(client) client.disconnect(); if(client) client.disconnect();
if(client2) client2.disconnect();
await(ipfsApi.stop()); await(ipfsApi.stop());
done(); done();
})); }));

View File

@ -20,39 +20,39 @@ const ipfsPath = '/tmp/orbittests';
let ipfs, ipfsDaemon; let ipfs, ipfsDaemon;
const IpfsApis = [ 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: () => { // start: () => {
// return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
// ipfsd.disposableApi((err, ipfs) => { // const IPFS = require('ipfs')
// if(err) console.error(err); // const ipfs = new IPFS();
// resolve(ipfs); // ipfs.goOnline(() => resolve(ipfs));
// }); // // 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) => ipfs.goOffline(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(); // uncomment if running this test suite stand-alone // OrbitServer.start(); // uncomment if running this test suite stand-alone