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);
|
this.events.emit('closed', dbname);
|
||||||
}
|
}
|
||||||
|
|
||||||
_connect(host, port, username, password, allowOffline) {
|
_connect(hash, username, password, allowOffline) {
|
||||||
return new Promise((resolve, reject) => {
|
if(allowOffline === undefined) allowOffline = false;
|
||||||
if(allowOffline === undefined) allowOffline = false;
|
|
||||||
|
|
||||||
this._pubsub = new PubSub(this._ipfs);
|
const readNetworkInfo = (hash) => {
|
||||||
this._pubsub.connect(host, port, username, password).then(() => {
|
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}'`);
|
logger.debug(`Connected to Pubsub at '${host}:${port}'`);
|
||||||
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
||||||
this.network = { host: host, port: port, name: 'TODO: network name' }
|
return;
|
||||||
resolve();
|
})
|
||||||
}).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.debug("'allowOffline' set to false, terminating");
|
||||||
this._pubsub.disconnect();
|
this._pubsub.disconnect();
|
||||||
reject(e);
|
throw e;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
this.user = { username: username, id: username } // TODO: user id from ipfs hash
|
||||||
this.network = { host: host, port: port, name: 'TODO: network name' }
|
return;
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class OrbitClientFactory {
|
class OrbitClientFactory {
|
||||||
static connect(host, port, username, password, ipfs, options) {
|
static connect(network, username, password, ipfs, options) {
|
||||||
const createClient =(ipfs) => {
|
if(!options) options = { allowOffline: false };
|
||||||
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 : {};
|
|
||||||
|
|
||||||
if(!ipfs) {
|
if(!ipfs) {
|
||||||
logger.error("IPFS instance not provided");
|
logger.error("IPFS instance not provided");
|
||||||
throw new 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 await = require('asyncawait/await');
|
||||||
const ipfsd = require('ipfsd-ctl');
|
const ipfsd = require('ipfsd-ctl');
|
||||||
const OrbitDB = require('../src/OrbitDB');
|
const OrbitDB = require('../src/OrbitDB');
|
||||||
|
const OrbitServer = require('orbit-server/src/server');
|
||||||
|
|
||||||
// Mute logging
|
// Mute logging
|
||||||
require('logplease').setLogLevel('ERROR');
|
require('logplease').setLogLevel('ERROR');
|
||||||
|
|
||||||
// Orbit
|
// Orbit
|
||||||
|
const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // network.json
|
||||||
const username = 'testrunner';
|
const username = 'testrunner';
|
||||||
const password = '';
|
const password = '';
|
||||||
const ipfsPath = '/tmp/orbittests';
|
|
||||||
|
|
||||||
const startIpfs = () => {
|
const startIpfs = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
// OrbitServer.start();
|
||||||
ipfsd.disposableApi((err, ipfs) => {
|
ipfsd.disposableApi((err, ipfs) => {
|
||||||
if(err) console.error(err);
|
if(err) console.error(err);
|
||||||
resolve(ipfs);
|
resolve(ipfs);
|
||||||
@ -36,7 +38,7 @@ describe('Orbit Client', function() {
|
|||||||
this.timeout(30000);
|
this.timeout(30000);
|
||||||
|
|
||||||
let ipfs, client, client2, db;
|
let ipfs, client, client2, db;
|
||||||
let channel = 'abcdefgh';
|
let channel = 'abcdefghijklmn';
|
||||||
const cacheFile = path.join(process.cwd(), '/test', 'orbit-db-test-cache.json');
|
const cacheFile = path.join(process.cwd(), '/test', 'orbit-db-test-cache.json');
|
||||||
|
|
||||||
before(async(function (done) {
|
before(async(function (done) {
|
||||||
@ -44,10 +46,12 @@ describe('Orbit Client', function() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ipfs = await(startIpfs());
|
ipfs = await(startIpfs());
|
||||||
client = await(OrbitDB.connect('localhost', 3333, username, password, ipfs, { allowOffline: true }));
|
const networkFile = await(ipfs.add('./test/network.json'))
|
||||||
client2 = await(OrbitDB.connect('localhost', 3333, username + "2", password, ipfs, { allowOffline: true }));
|
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) {
|
} catch(e) {
|
||||||
console.log(e);
|
console.log(e.stack);
|
||||||
assert.equal(e, null);
|
assert.equal(e, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -622,12 +626,14 @@ describe('Orbit Client', function() {
|
|||||||
it('syncs databases', async((done) => {
|
it('syncs databases', async((done) => {
|
||||||
const db2 = await(client2.kvstore(channel, { subscribe: false }));
|
const db2 = await(client2.kvstore(channel, { subscribe: false }));
|
||||||
db2.delete();
|
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(db.put('key1', 'hello1'));
|
||||||
await(db2.put('key1', 'hello2'));
|
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
|
// Mute logging
|
||||||
require('logplease').setLogLevel('ERROR');
|
require('logplease').setLogLevel('ERROR');
|
||||||
|
|
||||||
|
const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // network.json
|
||||||
const username = 'testrunner';
|
const username = 'testrunner';
|
||||||
const username2 = 'rennurtset';
|
const username2 = 'rennurtset';
|
||||||
|
|
||||||
@ -18,6 +19,10 @@ const ipfsPath = '/tmp/orbittests';
|
|||||||
const startIpfs = () => {
|
const startIpfs = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
OrbitServer.start();
|
OrbitServer.start();
|
||||||
|
ipfsd.disposableApi((err, ipfs) => {
|
||||||
|
if(err) reject(err);
|
||||||
|
resolve(ipfs);
|
||||||
|
});
|
||||||
// ipfsd.local(ipfsPath, (err, node) => {
|
// ipfsd.local(ipfsPath, (err, node) => {
|
||||||
// if(err) reject(err);
|
// if(err) reject(err);
|
||||||
// node.startDaemon((err, ipfs) => {
|
// node.startDaemon((err, ipfs) => {
|
||||||
@ -25,10 +30,6 @@ const startIpfs = () => {
|
|||||||
// resolve(ipfs);
|
// resolve(ipfs);
|
||||||
// });
|
// });
|
||||||
// });
|
// });
|
||||||
ipfsd.disposableApi((err, ipfs) => {
|
|
||||||
if(err) reject(err);
|
|
||||||
resolve(ipfs);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -39,19 +40,26 @@ describe('CounterStore', function() {
|
|||||||
|
|
||||||
before((done) => {
|
before((done) => {
|
||||||
rimraf.sync('./orbit-db-cache.json')
|
rimraf.sync('./orbit-db-cache.json')
|
||||||
startIpfs().then((res) => {
|
startIpfs()
|
||||||
ipfs = res;
|
.then((res) => {
|
||||||
Promise.map([username, username2], (login) => {
|
ipfs = res;
|
||||||
return OrbitDB.connect('localhost', 3333, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
|
return Promise.map([username, username2], (login) => {
|
||||||
}).then((clients) => {
|
return OrbitDB.connect(network, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
|
||||||
client1 = clients[0];
|
}).then((clients) => {
|
||||||
client2 = clients[1];
|
client1 = clients[0];
|
||||||
done();
|
client2 = clients[1];
|
||||||
}).catch((e) => {
|
return;
|
||||||
console.log(e.stack);
|
}).catch((e) => {
|
||||||
assert.equal(e, null);
|
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) => {
|
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