mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Read network info from ipfs object (not from a file)
This commit is contained in:
parent
6e4be8903e
commit
6cc2afab80
@ -1,7 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const logger = require('logplease').create("orbit-db.Client");
|
const Logger = require('logplease');
|
||||||
|
const logger = Logger.create("orbit-db", { color: Logger.Colors.Magenta });
|
||||||
const EventStore = require('orbit-db-eventstore');
|
const EventStore = require('orbit-db-eventstore');
|
||||||
const FeedStore = require('orbit-db-feedstore');
|
const FeedStore = require('orbit-db-feedstore');
|
||||||
const KeyValueStore = require('orbit-db-kvstore');
|
const KeyValueStore = require('orbit-db-kvstore');
|
||||||
@ -96,22 +97,9 @@ class OrbitDB {
|
|||||||
|
|
||||||
_connect(hash, username, password, allowOffline) {
|
_connect(hash, username, password, allowOffline) {
|
||||||
if(allowOffline === undefined) allowOffline = false;
|
if(allowOffline === undefined) allowOffline = false;
|
||||||
|
|
||||||
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;
|
let host, port, name;
|
||||||
return readNetworkInfo(hash)
|
return this._ipfs.object.get(hash)
|
||||||
.then((network) => JSON.parse(network))
|
.then((object) => JSON.parse(object.Data))
|
||||||
.then((network) => {
|
.then((network) => {
|
||||||
this.network = network;
|
this.network = network;
|
||||||
name = network.name;
|
name = network.name;
|
||||||
@ -120,6 +108,7 @@ class OrbitDB {
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this._pubsub = new PubSub();
|
this._pubsub = new PubSub();
|
||||||
|
logger.warn(`Connecting to Pubsub at '${host}:${port}'`);
|
||||||
return this._pubsub.connect(host, port, username, password)
|
return this._pubsub.connect(host, port, username, password)
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const async = require('asyncawait/async');
|
const async = require('asyncawait/async');
|
||||||
@ -13,7 +14,7 @@ const OrbitServer = require('orbit-server/src/server');
|
|||||||
require('logplease').setLogLevel('ERROR');
|
require('logplease').setLogLevel('ERROR');
|
||||||
|
|
||||||
// Orbit
|
// Orbit
|
||||||
const network = 'QmYPobvobKsyoCKTw476yTui611XABf927KxUPCf4gRLRr'; // network.json
|
const network = 'QmaAHGFm78eupEaDFzBfhUL5xn32dbeqn8oU2XCZJTQGBj';
|
||||||
const username = 'testrunner';
|
const username = 'testrunner';
|
||||||
const password = '';
|
const password = '';
|
||||||
|
|
||||||
@ -46,8 +47,10 @@ describe('Orbit Client', function() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
ipfs = await(startIpfs());
|
ipfs = await(startIpfs());
|
||||||
const networkFile = await(ipfs.add('./test/network.json'))
|
const str = fs.readFileSync('./test/network.json', 'utf-8');
|
||||||
assert.equal(networkFile[0].Hash, network);
|
const networkData = new Buffer(JSON.stringify({ Data: str }));
|
||||||
|
const networkFile = await(ipfs.object.put(networkData))
|
||||||
|
assert.equal(networkFile.Hash, network);
|
||||||
client = await(OrbitDB.connect(network, username, password, ipfs, { allowOffline: true }));
|
client = await(OrbitDB.connect(network, username, password, ipfs, { allowOffline: true }));
|
||||||
client2 = await(OrbitDB.connect(network, username + "2", password, ipfs, { allowOffline: true }));
|
client2 = await(OrbitDB.connect(network, username + "2", password, ipfs, { allowOffline: true }));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const fs = require('fs');
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
const rimraf = require('rimraf')
|
const rimraf = require('rimraf')
|
||||||
const ipfsd = require('ipfsd-ctl');
|
const ipfsd = require('ipfsd-ctl');
|
||||||
@ -10,7 +11,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 network = 'QmaAHGFm78eupEaDFzBfhUL5xn32dbeqn8oU2XCZJTQGBj';
|
||||||
const username = 'testrunner';
|
const username = 'testrunner';
|
||||||
const username2 = 'rennurtset';
|
const username2 = 'rennurtset';
|
||||||
|
|
||||||
@ -41,6 +42,11 @@ describe('CounterStore', function() {
|
|||||||
before((done) => {
|
before((done) => {
|
||||||
rimraf.sync('./orbit-db-cache.json')
|
rimraf.sync('./orbit-db-cache.json')
|
||||||
startIpfs()
|
startIpfs()
|
||||||
|
.then((ipfs) => {
|
||||||
|
const str = fs.readFileSync('./test/network.json', 'utf-8');
|
||||||
|
const networkData = new Buffer(JSON.stringify({ Data: str }));
|
||||||
|
return ipfs.object.put(networkData).then(() => ipfs)
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
ipfs = res;
|
ipfs = res;
|
||||||
return Promise.map([username, username2], (login) => {
|
return Promise.map([username, username2], (login) => {
|
||||||
@ -54,11 +60,6 @@ describe('CounterStore', function() {
|
|||||||
assert.equal(e, null);
|
assert.equal(e, null);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(() => ipfs.add('./test/network.json'))
|
|
||||||
.then((networkFile)=> {
|
|
||||||
assert.equal(networkFile[0].Hash, network);
|
|
||||||
return;
|
|
||||||
})
|
|
||||||
.then(done)
|
.then(done)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user