feat: expose OrbitDBAddress isValid & parse
This commit is contained in:
parent
4772c596cc
commit
1b54f4daa8
@ -31,8 +31,8 @@ class OrbitDB {
|
||||
constructor(ipfs, directory, options = {}) {
|
||||
this._ipfs = ipfs
|
||||
this.id = options.peerId || (this._ipfs._peerInfo ? this._ipfs._peerInfo.id._idB58String : 'default')
|
||||
this._pubsub = options && options.broker
|
||||
? new options.broker(this._ipfs)
|
||||
this._pubsub = options && options.broker
|
||||
? new options.broker(this._ipfs)
|
||||
: new Pubsub(this._ipfs, this.id)
|
||||
this.stores = {}
|
||||
this.directory = directory || './orbitdb'
|
||||
@ -97,7 +97,7 @@ class OrbitDB {
|
||||
Object.keys(this._directConnections).forEach(removeDirectConnect)
|
||||
|
||||
// Disconnect from pubsub
|
||||
if (this._pubsub)
|
||||
if (this._pubsub)
|
||||
this._pubsub.disconnect()
|
||||
|
||||
// Remove all databases from the state
|
||||
@ -125,8 +125,8 @@ class OrbitDB {
|
||||
|
||||
const cache = await this._loadCache(this.directory, address)
|
||||
|
||||
const opts = Object.assign({ replicate: true }, options, {
|
||||
accessController: accessController,
|
||||
const opts = Object.assign({ replicate: true }, options, {
|
||||
accessController: accessController,
|
||||
keystore: this.keystore,
|
||||
cache: cache,
|
||||
onClose: this._onClose.bind(this),
|
||||
@ -185,7 +185,7 @@ class OrbitDB {
|
||||
onMessage,
|
||||
onChannelCreated
|
||||
)
|
||||
|
||||
|
||||
if (getStore(address))
|
||||
getStore(address).events.emit('peer', peer)
|
||||
}
|
||||
@ -374,6 +374,14 @@ class OrbitDB {
|
||||
static getDatabaseTypes () {
|
||||
return databaseTypes
|
||||
}
|
||||
|
||||
static isValidAddress (address) {
|
||||
return OrbitDBAddress.isValid(address)
|
||||
}
|
||||
|
||||
static parseAddress (address) {
|
||||
return OrbitDBAddress.parse(address)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OrbitDB
|
||||
|
104
test/orbit-db-address.test.js
Normal file
104
test/orbit-db-address.test.js
Normal file
@ -0,0 +1,104 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const rmrf = require('rimraf')
|
||||
const OrbitDB = require('../src/OrbitDB')
|
||||
const OrbitDBAddress = require('../src/orbit-db-address')
|
||||
|
||||
const dbPath = './orbitdb/tests/orbit-db-address'
|
||||
const ipfsPath = './orbitdb/tests/orbit-db-address/ipfs'
|
||||
|
||||
const {
|
||||
config,
|
||||
startIpfs,
|
||||
stopIpfs,
|
||||
testAPIs
|
||||
} = require('./utils')
|
||||
|
||||
Object.keys(testAPIs).forEach(API => {
|
||||
describe(`orbit-db - OrbitDB Address (${API})`, function() {
|
||||
this.timeout(config.timeout)
|
||||
|
||||
let ipfsd, ipfs, orbitdb
|
||||
|
||||
before(async () => {
|
||||
config.daemon1.repo = ipfsPath
|
||||
rmrf.sync(config.daemon1.repo)
|
||||
rmrf.sync(dbPath)
|
||||
ipfsd = await startIpfs(API, config.daemon1)
|
||||
ipfs = ipfsd.api
|
||||
orbitdb = new OrbitDB(ipfs, dbPath)
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
if(orbitdb)
|
||||
await orbitdb.stop()
|
||||
|
||||
if (ipfsd)
|
||||
await stopIpfs(ipfsd)
|
||||
})
|
||||
|
||||
describe('Parse Address', () => {
|
||||
it('throws an error if address is empty', () => {
|
||||
let err
|
||||
try {
|
||||
const result = OrbitDB.parseAddress('')
|
||||
} catch (e) {
|
||||
err = e.toString()
|
||||
}
|
||||
assert.equal(err, 'Error: Not a valid OrbitDB address: ')
|
||||
})
|
||||
|
||||
it('parse address successfully', () => {
|
||||
const address = '/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC/first-database'
|
||||
const result = OrbitDB.parseAddress(address)
|
||||
|
||||
const isInstanceOf = result instanceof OrbitDBAddress
|
||||
assert.equal(isInstanceOf, true)
|
||||
|
||||
assert.equal(result.root, 'Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC')
|
||||
assert.equal(result.path, 'first-database')
|
||||
|
||||
assert.equal(result.toString().indexOf('/orbitdb'), 0)
|
||||
assert.equal(result.toString().indexOf('Qm'), 9)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isValid Address', () => {
|
||||
it('throws an error if address is empty', () => {
|
||||
assert.throws(() => {
|
||||
const result = OrbitDB.isValidAddress('')
|
||||
})
|
||||
})
|
||||
|
||||
it('validate address successfully', () => {
|
||||
const address = '/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC/first-database'
|
||||
const result = OrbitDB.isValidAddress(address)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
|
||||
it('handle missing orbitdb prefix', () => {
|
||||
const address = 'Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC/first-database'
|
||||
const result = OrbitDB.isValidAddress(address)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
|
||||
it('handle missing db address name', () => {
|
||||
const address = '/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzcJC'
|
||||
const result = OrbitDB.isValidAddress(address)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
|
||||
it('handle invalid multihash', () => {
|
||||
const address = '/orbitdb/Qmdgwt7w4uBsw8LXduzCd18zfGXeTmBsiR8edQ1hSfzc/first-database'
|
||||
const result = OrbitDB.isValidAddress(address)
|
||||
|
||||
assert.equal(result, false)
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user