mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Join a network based on ipfs hash, not host+port
This commit is contained in:
parent
9580102266
commit
7d44f6c421
@ -88,51 +88,64 @@ class OrbitDB {
|
||||
this.events.emit('closed', dbname);
|
||||
}
|
||||
|
||||
_connect(host, port, username, password, allowOffline) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(allowOffline === undefined) allowOffline = false;
|
||||
_connect(hash, username, password, allowOffline) {
|
||||
if(allowOffline === undefined) allowOffline = false;
|
||||
|
||||
this._pubsub = new PubSub(this._ipfs);
|
||||
this._pubsub.connect(host, port, username, password).then(() => {
|
||||
const readNetworkInfo = (hash) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._ipfs.cat(hash).then((res) => {
|
||||
let buf = '';
|
||||
res
|
||||
.on('error', (err) => reject(err))
|
||||
.on('data', (data) => buf += data)
|
||||
.on('end', () => resolve(buf))
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
let host, port, name;
|
||||
return readNetworkInfo(hash)
|
||||
.then((network) => JSON.parse(network))
|
||||
.then((network) => {
|
||||
this.network = network;
|
||||
name = network.name;
|
||||
host = network.publishers[0].split(":")[0];
|
||||
port = network.publishers[0].split(":")[1];
|
||||
})
|
||||
.then(() => {
|
||||
this._pubsub = new PubSub();
|
||||
return this._pubsub.connect(host, port, username, password)
|
||||
})
|
||||
.then(() => {
|
||||
logger.debug(`Connected to Pubsub at '${host}:${port}'`);
|
||||
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
||||
this.network = { host: host, port: port, name: 'TODO: network name' }
|
||||
resolve();
|
||||
}).catch((e) => {
|
||||
logger.warn("Couldn't connect to Pubsub:", e.message);
|
||||
return;
|
||||
})
|
||||
.catch((e) => {
|
||||
logger.warn("Couldn't connect to Pubsub: " + e.message);
|
||||
if(!allowOffline) {
|
||||
logger.debug("'allowOffline' set to false, terminating");
|
||||
this._pubsub.disconnect();
|
||||
reject(e);
|
||||
return;
|
||||
throw e;
|
||||
}
|
||||
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
||||
this.network = { host: host, port: port, name: 'TODO: network name' }
|
||||
resolve();
|
||||
return;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class OrbitClientFactory {
|
||||
static connect(host, port, username, password, ipfs, options) {
|
||||
const createClient =(ipfs) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const client = new OrbitDB(ipfs, options);
|
||||
client._connect(host, port, username, password, options.allowOffline)
|
||||
.then(() => resolve(client))
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
options = options ? options : {};
|
||||
static connect(network, username, password, ipfs, options) {
|
||||
if(!options) options = { allowOffline: false };
|
||||
|
||||
if(!ipfs) {
|
||||
logger.error("IPFS instance not provided");
|
||||
throw new Error("IPFS instance not provided");
|
||||
}
|
||||
|
||||
return createClient(ipfs);
|
||||
const client = new OrbitDB(ipfs, options);
|
||||
return client._connect(network, username, password, options.allowOffline)
|
||||
.then(() => client)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,17 +7,19 @@ const async = require('asyncawait/async');
|
||||
const await = require('asyncawait/await');
|
||||
const ipfsd = require('ipfsd-ctl');
|
||||
const OrbitDB = require('../src/OrbitDB');
|
||||
const OrbitServer = require('orbit-server/src/server');
|
||||
|
||||
// Mute logging
|
||||
require('logplease').setLogLevel('ERROR');
|
||||
|
||||
// Orbit
|
||||
const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // network.json
|
||||
const username = 'testrunner';
|
||||
const password = '';
|
||||
const ipfsPath = '/tmp/orbittests';
|
||||
|
||||
const startIpfs = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// OrbitServer.start();
|
||||
ipfsd.disposableApi((err, ipfs) => {
|
||||
if(err) console.error(err);
|
||||
resolve(ipfs);
|
||||
@ -36,7 +38,7 @@ describe('Orbit Client', function() {
|
||||
this.timeout(30000);
|
||||
|
||||
let ipfs, client, client2, db;
|
||||
let channel = 'abcdefgh';
|
||||
let channel = 'abcdefghijklmn';
|
||||
const cacheFile = path.join(process.cwd(), '/test', 'orbit-db-test-cache.json');
|
||||
|
||||
before(async(function (done) {
|
||||
@ -44,10 +46,12 @@ describe('Orbit Client', function() {
|
||||
|
||||
try {
|
||||
ipfs = await(startIpfs());
|
||||
client = await(OrbitDB.connect('localhost', 3333, username, password, ipfs, { allowOffline: true }));
|
||||
client2 = await(OrbitDB.connect('localhost', 3333, username + "2", password, ipfs, { allowOffline: true }));
|
||||
const networkFile = await(ipfs.add('./test/network.json'))
|
||||
assert.equal(networkFile[0].Hash, network);
|
||||
client = await(OrbitDB.connect(network, username, password, ipfs, { allowOffline: true }));
|
||||
client2 = await(OrbitDB.connect(network, username + "2", password, ipfs, { allowOffline: true }));
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
console.log(e.stack);
|
||||
assert.equal(e, null);
|
||||
}
|
||||
|
||||
@ -622,12 +626,14 @@ describe('Orbit Client', function() {
|
||||
it('syncs databases', async((done) => {
|
||||
const db2 = await(client2.kvstore(channel, { subscribe: false }));
|
||||
db2.delete();
|
||||
db2.events.on('data', async((dbname, hash) => {
|
||||
await(db.sync(hash))
|
||||
const value = db.get('key1');
|
||||
assert.equal(value, 'hello2');
|
||||
done();
|
||||
}));
|
||||
await(db.put('key1', 'hello1'));
|
||||
await(db2.put('key1', 'hello2'));
|
||||
await(db.sync('QmNtELU2N3heY9cFgRuLWavgov7NTXibNyZCxcTCYjw1TM'))
|
||||
const value = db.get('key1');
|
||||
assert.equal(value, 'hello2');
|
||||
done();
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
@ -10,6 +10,7 @@ const OrbitServer = require('orbit-server/src/server');
|
||||
// Mute logging
|
||||
require('logplease').setLogLevel('ERROR');
|
||||
|
||||
const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // network.json
|
||||
const username = 'testrunner';
|
||||
const username2 = 'rennurtset';
|
||||
|
||||
@ -18,6 +19,10 @@ const ipfsPath = '/tmp/orbittests';
|
||||
const startIpfs = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
OrbitServer.start();
|
||||
ipfsd.disposableApi((err, ipfs) => {
|
||||
if(err) reject(err);
|
||||
resolve(ipfs);
|
||||
});
|
||||
// ipfsd.local(ipfsPath, (err, node) => {
|
||||
// if(err) reject(err);
|
||||
// node.startDaemon((err, ipfs) => {
|
||||
@ -25,10 +30,6 @@ const startIpfs = () => {
|
||||
// resolve(ipfs);
|
||||
// });
|
||||
// });
|
||||
ipfsd.disposableApi((err, ipfs) => {
|
||||
if(err) reject(err);
|
||||
resolve(ipfs);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -39,19 +40,26 @@ describe('CounterStore', function() {
|
||||
|
||||
before((done) => {
|
||||
rimraf.sync('./orbit-db-cache.json')
|
||||
startIpfs().then((res) => {
|
||||
ipfs = res;
|
||||
Promise.map([username, username2], (login) => {
|
||||
return OrbitDB.connect('localhost', 3333, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
|
||||
}).then((clients) => {
|
||||
client1 = clients[0];
|
||||
client2 = clients[1];
|
||||
done();
|
||||
}).catch((e) => {
|
||||
console.log(e.stack);
|
||||
assert.equal(e, null);
|
||||
});
|
||||
});
|
||||
startIpfs()
|
||||
.then((res) => {
|
||||
ipfs = res;
|
||||
return Promise.map([username, username2], (login) => {
|
||||
return OrbitDB.connect(network, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
|
||||
}).then((clients) => {
|
||||
client1 = clients[0];
|
||||
client2 = clients[1];
|
||||
return;
|
||||
}).catch((e) => {
|
||||
console.log(e.stack);
|
||||
assert.equal(e, null);
|
||||
});
|
||||
})
|
||||
.then(() => ipfs.add('./test/network.json'))
|
||||
.then((networkFile)=> {
|
||||
assert.equal(networkFile[0].Hash, network);
|
||||
return;
|
||||
})
|
||||
.then(done)
|
||||
});
|
||||
|
||||
after((done) => {
|
||||
|
6
test/network.json
Normal file
6
test/network.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "localhost dev network",
|
||||
"publishers": [
|
||||
"localhost:3333"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user