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() {
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;
}

View File

@ -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();
}));

View File

@ -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