Merge branch 'master' into update-peer-exchanged

This commit is contained in:
Joel Thorstensson 2020-08-24 18:04:53 +02:00
commit e402fdc457
29 changed files with 7492 additions and 5682 deletions

1
API.md
View File

@ -19,6 +19,7 @@ Read the **[GETTING STARTED](https://github.com/orbitdb/orbit-db/blob/master/GUI
+ [set(key, value)](#setkey-value)
+ [get(key)](#getkey)
+ [del(key)](#delkey)
+ [all](#all)
* [orbitdb.kvstore(name|address)](#orbitdbkvstorenameaddress)
* [orbitdb.log(name|address)](#orbitdblognameaddress)
+ [add(event)](#addevent)

View File

@ -23,7 +23,8 @@ All databases are [implemented](https://github.com/orbitdb/orbit-db-store) on to
#### Project status & support
Status: **in active development**
* Status: **in active development**
* Compatible with **js-ipfs versions <= 0.46**
***NOTE!*** *OrbitDB is **alpha-stage** software. It means OrbitDB hasn't been security audited and programming APIs and data formats can still change. We encourage you to [reach out to the maintainers](https://gitter.im/orbitdb/Lobby) if you plan to use OrbitDB in mission critical systems.*
@ -99,9 +100,10 @@ If you're using `orbit-db` to develop **browser** or **Node.js** applications, u
Install dependencies:
```
npm install orbit-db ipfs
npm install orbit-db ipfs@~0.46.0
```
```javascript
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')

View File

@ -24,7 +24,7 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
})
],

View File

@ -18,7 +18,7 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
})
],

12617
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "orbit-db",
"version": "0.24.1",
"version": "0.25.0",
"description": "Distributed p2p database on IPFS",
"author": "Haad",
"license": "MIT",
@ -13,13 +13,13 @@
},
"main": "src/OrbitDB.js",
"dependencies": {
"cids": "^0.7.1",
"cids": "^1.0.0",
"ipfs-pubsub-1on1": "~0.0.6",
"is-node": "^1.0.2",
"localstorage-down": "^0.6.7",
"logplease": "^1.2.14",
"multihashes": "^0.4.12",
"orbit-db-access-controllers": "~0.2.2",
"multihashes": "~3.0.1",
"orbit-db-access-controllers": "^0.2.2",
"orbit-db-cache": "~0.3.0",
"orbit-db-counterstore": "~1.9.0",
"orbit-db-docstore": "~1.9.0",
@ -31,36 +31,36 @@
"orbit-db-kvstore": "~1.9.0",
"orbit-db-pubsub": "~0.5.5",
"orbit-db-storage-adapter": "~0.5.3",
"orbit-db-store": "~3.3.0"
"orbit-db-store": "~3.3.1"
},
"devDependencies": {
"adm-zip": "^0.4.13",
"adm-zip": "^0.4.16",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-loader": "^8.1.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"cpy-cli": "^2.0.0",
"cross-env": "^6.0.3",
"datastore-level": "~0.14.0",
"fs-extra": "^7.0.1",
"cpy-cli": "^3.1.1",
"cross-env": "^7.0.2",
"datastore-level": "~2.0.0",
"fs-extra": "^9.0.1",
"localstorage-level-migration": "~0.1.0",
"markdown-toc": "^1.2.0",
"mkdirp": "^0.5.1",
"mocha": "^5.2.0",
"orbit-db-test-utils": "^0.9.4",
"p-each-series": "^1.0.0",
"p-map": "^1.2.0",
"p-map-series": "^1.0.0",
"p-whilst": "^1.0.0",
"pify": "^4.0.1",
"puppeteer": "^1.18.1",
"remark-cli": "^5.0.0",
"remark-validate-links": "^7.0.0",
"rimraf": "^2.6.2",
"standard": "^12.0.1",
"validate-maintainers": "^1.1.0",
"mkdirp": "^1.0.4",
"mocha": "^8.1.1",
"orbit-db-test-utils": "^0.10.2",
"p-each-series": "^2.1.0",
"p-map": "^4.0.0",
"p-map-series": "^2.1.0",
"p-whilst": "^2.1.0",
"pify": "^5.0.0",
"puppeteer": "^5.2.1",
"remark-cli": "^8.0.1",
"remark-validate-links": "^10.0.2",
"rimraf": "^3.0.2",
"standard": "^14.3.4",
"validate-maintainers": "^1.2.2",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
@ -73,7 +73,7 @@
"lint:docs": "remark -qf -u validate-links .",
"test:all": "npm run test:browser-multiple-tabs && npm run test",
"test": "cross-env TEST=js mocha && cross-env TEST=go mocha;",
"test:browser-multiple-tabs": "npm run build:dist && cpy dist/orbitdb.min.js ./test/browser --rename=orbitdb.js && cpy node_modules/ipfs/dist/index.js ./test/browser --rename=ipfs.js && cpy node_modules/orbit-db-identity-provider/dist/index-browser.min.js ./test/browser --rename=identities.js && cpy node_modules/ipfs-log/dist/ipfslog.min.js ./test/browser && mocha ./test/browser/concurrent.spec.js",
"test:browser-multiple-tabs": "npm run build:dist && cpy dist/orbitdb.min.js ./test/browser --rename=orbitdb.js && cpy node_modules/ipfs/dist/index.min.js ./test/browser --rename=ipfs.js && cpy node_modules/orbit-db-identity-provider/dist/index-browser.min.js ./test/browser --rename=identities.js && cpy node_modules/ipfs-log/dist/ipfslog.min.js ./test/browser && mocha ./test/browser/concurrent.spec.js",
"build": "npm run build:es5 && npm run build:debug && npm run build:dist && npm run build:examples && npm run build:docs/toc",
"build:examples": "webpack --config conf/webpack.example.config.js --sort-modules-by size",
"build:dist": "webpack --config conf/webpack.config.js --sort-modules-by size && mkdirp examples/browser/lib && cpy dist/orbitdb.min.js examples/browser/lib",

View File

@ -82,7 +82,7 @@ class OrbitDB {
options.offline = false
}
if (options.offline && !options.id ) {
if (options.offline && !options.id) {
throw new Error('Offline mode requires passing an `id` in the options')
}

View File

@ -26,8 +26,18 @@ class OrbitDBAddress {
let accessControllerHash
const validateHash = (hash) => {
const prefixes = ['zd', 'Qm', 'ba', 'k5']
for (const p of prefixes) {
if (hash.indexOf(p) > -1) {
return true
}
}
return false
}
try {
accessControllerHash = (parts[0].indexOf('zd') > -1 || parts[0].indexOf('Qm') > -1 || parts[0].indexOf('ba') > -1)
accessControllerHash = validateHash(parts[0])
? new CID(parts[0]).toBaseEncodedString()
: null
} catch (e) {

View File

@ -4,13 +4,18 @@ const puppeteer = require('puppeteer')
const path = require('path')
const mapSeries = require('p-map-series')
const pMap = require('p-map')
const {
config,
} = require('../utils')
const { config } = require('orbit-db-test-utils')
const clicksPerTab = 20
const numTabs = 3
const wait = async (milliseconds) => {
return new Promise((resolve, reject) => {
console.log("waiting...")
setTimeout(resolve, milliseconds)
})
}
describe(`orbit-db - browser concurrent writes`, function() {
this.timeout(numTabs * config.timeout)
@ -36,6 +41,7 @@ describe(`orbit-db - browser concurrent writes`, function() {
await page.goto(`file://${path.resolve(__dirname, 'index.html')}`)
page.on('dialog', dialog => dialog.dismiss())
page.on('pageerror', err => console.error(err))
await wait(1000)
return page
}

View File

@ -24,15 +24,16 @@ const {
const dbPath = path.join('./orbitdb', 'tests', 'create-open')
const ipfsPath = path.join('./orbitdb', 'tests', 'create-open', 'ipfs')
const migrationFixturePath = path.join('./test', 'fixtures', 'migration', 'cache-schema-test')
const ipfsFixtures = path.join('./test', 'fixtures', 'ipfs.zip')
const ipfsFixturesDir = path.join('./test', 'fixtures', 'ipfs')
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Create & Open (${API})`, function () {
let ipfsFixtures = path.join('./test', 'fixtures', `${API}.zip`)
this.retries(1) // windows...
this.timeout(config.timeout)
let ipfsd, ipfs, orbitdb, db, address
let ipfsd, ipfs, orbitdb, address
let localDataPath
const filterFunc = (src, dest) => {
@ -68,7 +69,7 @@ Object.keys(testAPIs).forEach(API => {
it('throws an error if given an invalid database type', async () => {
let err
try {
db = await orbitdb.create('first', 'invalid-type')
const db = await orbitdb.create('first', 'invalid-type')
} catch (e) {
err = e.toString()
}
@ -78,7 +79,7 @@ Object.keys(testAPIs).forEach(API => {
it('throws an error if given an address instead of name', async () => {
let err
try {
db = await orbitdb.create('/orbitdb/Qmc9PMho3LwTXSaUXJ8WjeBZyXesAwUofdkGeadFXsqMzW/first', 'feed')
const db = await orbitdb.create('/orbitdb/Qmc9PMho3LwTXSaUXJ8WjeBZyXesAwUofdkGeadFXsqMzW/first', 'feed')
} catch (e) {
err = e.toString()
}
@ -86,14 +87,15 @@ Object.keys(testAPIs).forEach(API => {
})
it('throws an error if database already exists', async () => {
let err
let err, db
try {
db = await orbitdb.create('first', 'feed', { replicate: false })
db = await orbitdb.create('first', 'feed', { replicate: false })
const db2 = await orbitdb.create('first', 'feed', { replicate: false })
} catch (e) {
err = e.toString()
}
assert.equal(err, `Error: Database '${db.address}' already exists!`)
await db.close()
})
@ -110,6 +112,8 @@ Object.keys(testAPIs).forEach(API => {
})
describe('Success', function () {
let db
before(async () => {
db = await orbitdb.create('second', 'feed', { replicate: false })
localDataPath = path.join(dbPath, orbitdb.id, 'cache')
@ -150,8 +154,9 @@ Object.keys(testAPIs).forEach(API => {
it('can pass local database directory as an option', async () => {
const dir = './orbitdb/tests/another-feed'
db = await orbitdb.create('third', 'feed', { directory: dir })
const db2 = await orbitdb.create('third', 'feed', { directory: dir })
assert.equal(fs.existsSync(dir), true)
await db2.close()
})
it('loads cache from previous version of orbit-db', async () => {
@ -283,19 +288,16 @@ Object.keys(testAPIs).forEach(API => {
})
it('returns the address that would have been created', async () => {
db = await orbitdb.create('third', 'feed', { replicate: false })
const db = await orbitdb.create('third', 'feed', { replicate: false })
assert.equal(address.toString().indexOf('/orbitdb'), 0)
assert.equal(address.toString().indexOf('zd'), 9)
assert.equal(address.toString(), db.address.toString())
await db.close()
})
})
})
describe('Open', function () {
beforeEach(async () => {
db = await orbitdb.open('abc', { create: true, type: 'feed' })
})
it('throws an error if trying to open a database with name only and \'create\' is not set to \'true\'', async () => {
let err
try {
@ -317,36 +319,45 @@ Object.keys(testAPIs).forEach(API => {
})
it('opens a database - name only', async () => {
db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
assert.equal(db.address.toString().indexOf('/orbitdb'), 0)
assert.equal(db.address.toString().indexOf('zd'), 9)
assert.equal(db.address.toString().indexOf('abc'), 59)
await db.drop()
})
it('opens a database - with a different identity', async () => {
const identity = await Identities.createIdentity({ id: 'test-id', keystore: orbitdb.keystore })
db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true, identity })
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true, identity })
assert.equal(db.address.toString().indexOf('/orbitdb'), 0)
assert.equal(db.address.toString().indexOf('zd'), 9)
assert.equal(db.address.toString().indexOf('abc'), 59)
assert.equal(db.identity, identity)
await db.drop()
})
it('opens the same database - from an address', async () => {
db = await orbitdb.open(db.address)
assert.equal(db.address.toString().indexOf('/orbitdb'), 0)
assert.equal(db.address.toString().indexOf('zd'), 9)
assert.equal(db.address.toString().indexOf('abc'), 59)
const identity = await Identities.createIdentity({ id: 'test-id', keystore: orbitdb.keystore })
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true, identity })
const db2 = await orbitdb.open(db.address)
assert.equal(db2.address.toString().indexOf('/orbitdb'), 0)
assert.equal(db2.address.toString().indexOf('zd'), 9)
assert.equal(db2.address.toString().indexOf('abc'), 59)
await db.drop()
await db2.drop()
})
it('opens a database and adds the creator as the only writer', async () => {
db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
assert.equal(db.access.write.length, 1)
assert.equal(db.access.write[0], db.identity.id)
await db.drop()
})
it('doesn\'t open a database if we don\'t have it locally', async () => {
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
const address = new OrbitDBAddress(db.address.root.slice(0, -1) + 'A', 'non-existent')
await db.drop()
return new Promise((resolve, reject) => {
setTimeout(resolve, 900)
orbitdb.open(address)
@ -355,8 +366,10 @@ Object.keys(testAPIs).forEach(API => {
})
})
it('throws an error if trying to open a database locally and we don\'t have it', () => {
it('throws an error if trying to open a database locally and we don\'t have it', async () => {
const db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true })
const address = new OrbitDBAddress(db.address.root.slice(0, -1) + 'A', 'second')
await db.drop()
return orbitdb.open(address, { localOnly: true })
.then(() => new Error('Shouldn\'t open the database'))
.catch(e => {
@ -365,11 +378,12 @@ Object.keys(testAPIs).forEach(API => {
})
it('open the database and it has the added entries', async () => {
db = await orbitdb.open('ZZZ', { create: true, type: 'feed' })
const db = await orbitdb.open('ZZZ', { create: true, type: 'feed' })
await db.add('hello1')
await db.add('hello2')
await db.close()
db = await orbitdb.open(db.address)
const db2 = await orbitdb.open(db.address)
await db.load()
const res = db.iterator({ limit: -1 }).collect()
@ -377,6 +391,8 @@ Object.keys(testAPIs).forEach(API => {
assert.equal(res.length, 2)
assert.equal(res[0].payload.value, 'hello1')
assert.equal(res[1].payload.value, 'hello2')
await db.drop()
await db2.drop()
})
})
@ -387,14 +403,14 @@ Object.keys(testAPIs).forEach(API => {
})
it('closes a custom store', async () => {
const directory = path.join(dbPath, "custom-store")
db = await orbitdb.open('xyz', { create: true, type: 'feed', directory })
const db = await orbitdb.open('xyz', { create: true, type: 'feed', directory })
await db.close()
assert.strictEqual(db._cache._store._db.status, 'closed')
})
it("close load close sets status to 'closed'", async () => {
const directory = path.join(dbPath, "custom-store")
db = await orbitdb.open('xyz', { create: true, type: 'feed', directory })
const db = await orbitdb.open('xyz', { create: true, type: 'feed', directory })
await db.close()
await db.load()
await db.close()

View File

@ -23,7 +23,7 @@ Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Log Database (${API})`, function() {
this.timeout(config.timeout)
let ipfsd, ipfs, orbitdb1, db
let ipfsd, ipfs, orbitdb1
before(async () => {
config.daemon1.repo = ipfsPath
@ -44,30 +44,34 @@ Object.keys(testAPIs).forEach(API => {
describe('Eventlog', function () {
it('creates and opens a database', async () => {
db = await orbitdb1.eventlog('log database')
const db = await orbitdb1.eventlog('log database')
assert.notEqual(db, null)
assert.equal(db.type, 'eventlog')
assert.equal(db.dbname, 'log database')
await db.drop()
})
it('returns 0 items when it\'s a fresh database', async () => {
db = await orbitdb1.eventlog('log database')
const db = await orbitdb1.eventlog('log database')
const items = db.iterator({ limit: -1 }).collect()
assert.equal(items.length, 0)
await db.drop()
})
it('returns the added entry\'s hash, 1 entry', async () => {
db = await orbitdb1.eventlog('first database')
const db = await orbitdb1.eventlog('first database')
const hash = await db.add('hello1')
const items = db.iterator({ limit: -1 }).collect()
assert.notEqual(hash, null)
assert.equal(hash, last(items).hash)
assert.equal(items.length, 1)
await db.drop()
})
it('returns the added entry\'s hash, 2 entries', async () => {
db = await orbitdb1.eventlog('first database')
const db = await orbitdb1.eventlog('first database')
await db.load()
await db.add('hello1')
const prevHash = db.iterator().collect()[0].hash
const hash = await db.add('hello2')
const items = db.iterator({ limit: -1 }).collect()
@ -75,31 +79,35 @@ Object.keys(testAPIs).forEach(API => {
assert.notEqual(hash, null)
assert.notEqual(hash, prevHash)
assert.equal(hash, last(items).hash)
await db.drop()
})
it('adds five items', async () => {
db = await orbitdb1.eventlog('second database')
const db = await orbitdb1.eventlog('second database')
await mapSeries([1, 2, 3, 4, 5], (i) => db.add('hello' + i))
const items = db.iterator({ limit: -1 }).collect()
assert.equal(items.length, 5)
assert.equal(items[0].payload.value, 'hello1')
assert.equal(last(items.map((f) => f.payload.value)), 'hello5')
await db.drop()
})
it('adds an item that is > 256 bytes', async () => {
db = await orbitdb1.eventlog('third database')
const db = await orbitdb1.eventlog('third database')
let msg = Buffer.alloc(1024)
msg.fill('a')
const hash = await db.add(msg.toString())
assert.notEqual(hash, null)
assert.equal(hash.startsWith('zd'), true)
assert.equal(hash.length, 49)
await db.drop()
})
})
describe('Iterator', function() {
let hashes = []
const itemCount = 5
let db
before(async () => {
hashes = []

View File

@ -23,7 +23,7 @@ Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Feed Database (${API})`, function() {
this.timeout(config.timeout)
let ipfsd, ipfs, orbitdb1, db, address
let ipfsd, ipfs, orbitdb1, address
before(async () => {
config.daemon1.repo = ipfsPath
@ -44,31 +44,35 @@ Object.keys(testAPIs).forEach(API => {
describe('Feed', function() {
it('creates and opens a database', async () => {
db = await orbitdb1.feed('feed database')
const db = await orbitdb1.feed('feed database')
assert.notEqual(db, null)
assert.equal(db.type, 'feed')
assert.equal(db.dbname, 'feed database')
await db.drop()
})
it('returns 0 items when it\'s a fresh database', async () => {
db = await orbitdb1.feed('feed database')
const db = await orbitdb1.feed('feed database')
const items = db.iterator({ limit: -1 }).collect()
assert.equal(items.length, 0)
await db.drop()
})
it('returns the added entry\'s hash, 1 entry', async () => {
db = await orbitdb1.feed('first')
const db = await orbitdb1.feed('first')
address = db.address.toString()
const hash = await db.add('hello1')
const items = db.iterator({ limit: -1 }).collect()
assert.notEqual(hash, null)
assert.equal(hash, last(items).hash)
assert.equal(items.length, 1)
await db.drop()
})
it('returns the added entry\'s hash, 2 entries', async () => {
db = await orbitdb1.feed(address)
const db = await orbitdb1.feed(address)
await db.load()
await db.add('hello1')
const prevHash = db.iterator().collect()[0].hash
const hash = await db.add('hello2')
const items = db.iterator({ limit: -1 }).collect()
@ -76,49 +80,53 @@ Object.keys(testAPIs).forEach(API => {
assert.notEqual(hash, null)
assert.notEqual(hash, prevHash)
assert.equal(hash, last(items).hash)
await db.drop()
})
it('adds five items', async () => {
db = await orbitdb1.feed('second')
const db = await orbitdb1.feed('second')
await mapSeries([1, 2, 3, 4, 5], (i) => db.add('hello' + i))
const items = db.iterator({ limit: -1 }).collect()
assert.equal(items.length, 5)
assert.equal(items[0].payload.value, 'hello1')
assert.equal(items[items.length - 1].payload.value, 'hello5')
await db.drop()
})
it('adds an item that is > 256 bytes', async () => {
db = await orbitdb1.feed('third')
const db = await orbitdb1.feed('third')
let msg = Buffer.alloc(1024)
msg.fill('a')
const hash = await db.add(msg.toString())
assert.notEqual(hash, null)
assert.equal(hash.startsWith('zd'), true)
assert.equal(hash.length, 49)
await db.drop()
})
it('deletes an item when only one item in the database', async () => {
db = await orbitdb1.feed('fourth')
const db = await orbitdb1.feed('fourth')
const hash = await db.add('hello3')
const delopHash = await db.remove(hash)
const items = db.iterator().collect()
assert.equal(delopHash.startsWith('zd'), true)
assert.equal(items.length, 0)
await db.drop()
})
it('deletes an item when two items in the database', async () => {
db = await orbitdb1.feed('fifth')
const db = await orbitdb1.feed('fifth')
await db.add('hello1')
const hash = await db.add('hello2')
await db.remove(hash)
const items = db.iterator({ limit: -1 }).collect()
assert.equal(items.length, 1)
assert.equal(items[0].payload.value, 'hello1')
await db.drop()
})
it('deletes an item between adds', async () => {
db = await orbitdb1.feed('sixth')
const db = await orbitdb1.feed('sixth')
const hash = await db.add('hello1')
await db.add('hello2')
@ -134,10 +142,13 @@ Object.keys(testAPIs).forEach(API => {
assert.equal(firstItem.payload.key, null)
assert.equal(firstItem.payload.value, 'hello2')
assert.equal(secondItem.payload.value, 'hello3')
await db.drop()
})
})
describe('Iterator', function() {
let db
let hashes = []
const itemCount = 5

BIN
test/fixtures/js-ipfs.zip vendored Normal file

Binary file not shown.

View File

@ -20,7 +20,7 @@ Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Key-Value Database (${API})`, function() {
this.timeout(config.timeout)
let ipfsd, ipfs, orbitdb1, db
let ipfsd, ipfs, orbitdb1
before(async () => {
config.daemon1.repo = ipfsPath
@ -38,47 +38,49 @@ Object.keys(testAPIs).forEach(API => {
}, 0)
})
beforeEach(async () => {
db = await orbitdb1.kvstore('orbit-db-tests', { path: dbPath })
})
afterEach(async () => {
await db.drop()
})
it('creates and opens a database', async () => {
db = await orbitdb1.keyvalue('first kv database')
const db = await orbitdb1.keyvalue('first kv database')
assert.notEqual(db, null)
assert.equal(db.type, 'keyvalue')
assert.equal(db.dbname, 'first kv database')
await db.drop()
})
it('put', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello1')
const value = db.get('key1')
assert.equal(value, 'hello1')
await db.drop()
})
it('get', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello2')
const value = db.get('key1')
assert.equal(value, 'hello2')
await db.drop()
})
it('put updates a value', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello3')
await db.put('key1', 'hello4')
const value = db.get('key1')
assert.equal(value, 'hello4')
await db.drop()
})
it('set is an alias for put', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.set('key1', 'hello5')
const value = db.get('key1')
assert.equal(value, 'hello5')
await db.drop()
})
it('put/get - multiple keys', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello1')
await db.put('key2', 'hello2')
await db.put('key3', 'hello3')
@ -88,43 +90,54 @@ Object.keys(testAPIs).forEach(API => {
assert.equal(v1, 'hello1')
assert.equal(v2, 'hello2')
assert.equal(v3, 'hello3')
await db.drop()
})
it('deletes a key', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello!')
await db.del('key1')
const value = db.get('key1')
assert.equal(value, null)
await db.drop()
})
it('deletes a key after multiple updates', async () => {
const db = await orbitdb1.keyvalue('first kv database')
await db.put('key1', 'hello1')
await db.put('key1', 'hello2')
await db.put('key1', 'hello3')
await db.del('key1')
const value = db.get('key1')
assert.equal(value, null)
await db.drop()
})
it('get - integer value', async () => {
const db = await orbitdb1.keyvalue('first kv database')
const val = 123
await db.put('key1', val)
const v1 = db.get('key1')
assert.equal(v1, val)
await db.drop()
})
it('get - object value', async () => {
const db = await orbitdb1.keyvalue('first kv database')
const val = { one: 'first', two: 2 }
await db.put('key1', val)
const v1 = db.get('key1')
assert.deepEqual(v1, val)
await db.drop()
})
it('get - array value', async () => {
const db = await orbitdb1.keyvalue('first kv database')
const val = [1, 2, 3, 4, 5]
await db.put('key1', val)
const v1 = db.get('key1')
assert.deepEqual(v1, val)
await db.drop()
})
})
})

View File

@ -5,7 +5,6 @@ const path = require('path')
const assert = require('assert')
const mapSeries = require('p-map-series')
const rmrf = require('rimraf')
const IPFS = require('ipfs')
const OrbitDB = require('../src/OrbitDB')
const Identities = require('orbit-db-identity-provider')
const Keystore = require('orbit-db-keystore')

View File

@ -25,7 +25,7 @@ Object.keys(testAPIs).forEach(API => {
this.timeout(config.timeout * 2)
let ipfsd1, ipfsd2, ipfs1, ipfs2
let orbitdb1, orbitdb2, db1, db2
let orbitdb1, orbitdb2
before(async () => {
config.daemon1.repo = ipfsPath1
@ -59,20 +59,7 @@ Object.keys(testAPIs).forEach(API => {
})
describe('two peers', function() {
// Opens two databases db1 and db2 and gives write-access to both of the peers
const openDatabases1 = async (options) => {
// Set write access for both clients
options.write = [
orbitdb1.identity.publicKey,
orbitdb2.identity.publicKey
],
options = Object.assign({}, options, { path: dbPath1 })
db1 = await orbitdb1.eventlog('replicate-and-load-tests', options)
// Set 'localOnly' flag on and it'll error if the database doesn't exist locally
options = Object.assign({}, options, { path: dbPath2 })
db2 = await orbitdb2.eventlog(db1.address.toString(), options)
}
let db1, db2
const openDatabases = async (options) => {
// Set write access for both clients
@ -88,7 +75,7 @@ Object.keys(testAPIs).forEach(API => {
db2 = await orbitdb2.eventlog(db1.address.toString(), options)
}
beforeEach(async () => {
before(async () => {
await openDatabases({ sync: true })
assert.equal(db1.address.toString(), db2.address.toString())
@ -99,7 +86,7 @@ Object.keys(testAPIs).forEach(API => {
console.log("Found peers")
})
afterEach(async () => {
after(async () => {
await db1.drop()
await db2.drop()
})
@ -126,8 +113,6 @@ Object.keys(testAPIs).forEach(API => {
assert.equal(items[0].payload.value, 'hello0')
assert.equal(items[items.length - 1].payload.value, 'hello99')
db2 = null
try {
// Set write access for both clients
@ -145,19 +130,22 @@ Object.keys(testAPIs).forEach(API => {
// Open the database again (this time from the disk)
options = Object.assign({}, options, { path: dbPath1, create: false })
db1 = await orbitdb1.eventlog(addr, options)
const db3 = await orbitdb1.eventlog(addr, options)
// Set 'localOnly' flag on and it'll error if the database doesn't exist locally
options = Object.assign({}, options, { path: dbPath2, localOnly: true })
db2 = await orbitdb2.eventlog(addr, options)
const db4 = await orbitdb2.eventlog(addr, options)
await db1.load()
await db2.load()
await db3.load()
await db4.load()
// Make sure we have all the entries in the databases
const result1 = db1.iterator({ limit: -1 }).collect()
const result2 = db2.iterator({ limit: -1 }).collect()
const result1 = db3.iterator({ limit: -1 }).collect()
const result2 = db4.iterator({ limit: -1 }).collect()
assert.equal(result1.length, entryCount)
assert.equal(result2.length, entryCount)
await db3.drop()
await db4.drop()
} catch (e) {
reject(e)
}

View File

@ -522,7 +522,7 @@ Object.keys(testAPIs).forEach(API => {
resolve()
}
}, 100)
}, 500)
} catch (e) {
reject(e)
}

View File

@ -26,7 +26,7 @@ Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Set identities (${API})`, function() {
this.timeout(config.timeout)
let ipfsd, ipfs, orbitdb, db, keystore
let ipfsd, ipfs, orbitdb, keystore, options
let identity1, identity2
let localDataPath
@ -56,7 +56,7 @@ Object.keys(testAPIs).forEach(API => {
})
beforeEach(async () => {
let options = {}
options = {}
options.accessController = {
write : [
orbitdb.identity.id,
@ -64,16 +64,18 @@ Object.keys(testAPIs).forEach(API => {
]
}
options = Object.assign({}, options, { create: true, type: 'eventlog', overwrite: true })
db = await orbitdb.open('abc', options)
})
it('sets identity', async () => {
const db = await orbitdb.open('abc', options)
assert.equal(db.identity, orbitdb.identity)
db.setIdentity(identity1)
assert.equal(db.identity, identity1)
await db.close()
})
it('writes with new identity with access', async () => {
const db = await orbitdb.open('abc', options)
assert.equal(db.identity, orbitdb.identity)
db.setIdentity(identity1)
assert.equal(db.identity, identity1)
@ -84,9 +86,11 @@ Object.keys(testAPIs).forEach(API => {
err = e.message
}
assert.equal(err, null)
await db.drop()
})
it('cannot write with new identity without access', async () => {
const db = await orbitdb.open('abc', options)
assert.equal(db.identity, orbitdb.identity)
db.setIdentity(identity2)
assert.equal(db.identity, identity2)
@ -97,6 +101,7 @@ Object.keys(testAPIs).forEach(API => {
err = e.message
}
assert.equal(err, `Could not append entry, key "${identity2.id}" is not allowed to write to the log`)
await db.drop()
})
})
})

View File

@ -1,71 +0,0 @@
module.exports = {
timeout: 30000,
dbname: 'orbit-db-tests',
defaultIpfsConfig: {
start: true,
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/0',
Swarm: ['/ip4/0.0.0.0/tcp/0'],
Gateway: '/ip4/0.0.0.0/tcp/0'
},
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
},
webRTCStar: {
Enabled: false
}
},
}
},
daemon1: {
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/0',
Swarm: ['/ip4/0.0.0.0/tcp/0'],
Gateway: '/ip4/0.0.0.0/tcp/0'
},
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
},
webRTCStar: {
Enabled: false
}
},
},
},
daemon2: {
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
API: '/ip4/127.0.0.1/tcp/0',
Swarm: ['/ip4/0.0.0.0/tcp/0'],
Gateway: '/ip4/0.0.0.0/tcp/0'
},
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: true,
Interval: 1
},
webRTCStar: {
Enabled: false
}
},
},
}
}

View File

@ -1,10 +0,0 @@
'use strict'
const connectIpfsNodes = async (ipfs1, ipfs2) => {
const id1 = await ipfs1.id()
const id2 = await ipfs2.id()
await ipfs1.swarm.connect(id2.addresses[0])
await ipfs2.swarm.connect(id1.addresses[0])
}
module.exports = connectIpfsNodes

View File

@ -1,7 +0,0 @@
const OrbitDbCache = require('orbit-db-cache')
/**
* A custom cache example. To create a differing custom example, orbitdb cache was
* used with another abstract-leveldown compliant storage, localdown as an example
*/
module.exports = OrbitDbCache

View File

@ -1,10 +1,2 @@
exports.config = require('./config.js')
exports.testAPIs = require('./test-apis')
exports.startIpfs = require('./start-ipfs')
exports.stopIpfs = require('./stop-ipfs')
exports.waitForPeers = require('./wait-for-peers')
exports.connectPeers = require('./connect-peers')
exports.MemStore = require('./mem-store')
exports.CustomTestKeystore = require('./custom-test-keystore')
exports.CustomTestCache = require('./custom-test-cache')
exports.databases = require('./databases')

View File

@ -1,51 +0,0 @@
'use strict'
const multihashing = require('multihashing-async')
const CID = require('cids')
const cidifyString = (str) => {
if (!str) {
return str
}
if (Array.isArray(str)) {
return str.map(cidifyString)
}
return new CID(str)
}
/* Memory store using an LRU cache */
class MemStore {
constructor () {
this._store = new Map()
}
async put (value) {
const buffer = Buffer.from(JSON.stringify(value))
const multihash = await multihashing(buffer, 'sha2-256')
const cid = new CID(1, 'dag-cbor', multihash)
const key = cid.toBaseEncodedString('base58btc')
this._store.set(key, value)
return cid
}
async get (cid) {
if (CID.isCID(cid)) {
cid = cid.toBaseEncodedString('base58btc')
}
const data = this._store.get(cid)
const links = ['next', 'heads']
links.forEach((prop) => {
if(data[prop])
data[prop] = cidifyString(data[prop])
})
return {
value: data
}
}
}
module.exports = MemStore

View File

@ -1,45 +0,0 @@
'use strict'
const IPFSFactory = require('ipfsd-ctl')
const testAPIs = require('./test-apis')
/**
* Start an IPFS instance
* @param {Object} config [IPFS configuration to use]
* @return {[Promise<IPFS>]} [IPFS instance]
*/
const startIpfs = (type, config = {}) => {
return new Promise((resolve, reject) => {
if (!testAPIs[type]) {
reject(new Error(`Wanted API type ${JSON.stringify(type)} is unknown. Available types: ${Object.keys(testAPIs).join(', ')}`))
}
// If we're starting a process, pass command line arguments to it
if (!config.args && type.includes('go')) {
config.args = ['--enable-pubsub-experiment']
}
// Spawn an IPFS daemon (type defined in)
IPFSFactory
.create(testAPIs[type])
.spawn(config, async (err, ipfsd) => {
if (err) {
reject(err)
}
// Monkey patch _peerInfo to the ipfs api/instance
// to make js-ipfs-api compatible with js-ipfs
// TODO: Get IPFS id via coherent API call (without it being asynchronous)
setTimeout(async () => {
if (!ipfsd.api._peerInfo) {
let { id } = await ipfsd.api.id()
ipfsd.api._peerInfo = { id: { _idB58String: id } }
}
resolve(ipfsd)
}, 500)
})
})
}
module.exports = startIpfs

View File

@ -1,17 +0,0 @@
'use strict'
/**
* Stop an IPFS or ipfsd-ctl instance
* @param {Object} config [IPFS ipfsd-ctl to stop]
* @return {None}
*/
const stopIpfs = (ipfs) => {
return new Promise(async (resolve, reject) => {
ipfs.stop((err) => {
if (err) { reject(err) }
resolve()
})
})
}
module.exports = stopIpfs

View File

@ -1,35 +0,0 @@
const IPFS = require('ipfs')
const IPFSHTTPClient = require('ipfs-http-client')
/**
* IPFS daemons to run the tests with.
*/
// Available daemon types are defined in:
// https://github.com/ipfs/js-ipfsd-ctl#ipfsfactory---const-f--ipfsfactorycreateoptions
let jsIpfs = {
'js-ipfs': {
type: 'proc',
exec: IPFS,
}
}
const goIpfs = {
'go-ipfs': {
type: 'go',
IpfsClient: IPFSHTTPClient,
}
}
// By default, we run tests against js-ipfs.
let testAPIs = Object.assign({}, jsIpfs)
// Setting env variable 'TEST=all' will make tests run with js-ipfs and go-ipfs.
// Setting env variable 'TEST=go' will make tests run with go-ipfs.
// Eg. 'TEST=go mocha' runs tests with go-ipfs
if (process.env.TEST === 'all')
testAPIs = Object.assign({}, testAPIs, goIpfs)
else if (process.env.TEST === 'go')
testAPIs = Object.assign({}, goIpfs)
module.exports = testAPIs

View File

@ -1,16 +0,0 @@
'use strict'
const waitForPeers = (ipfs, peersToWait, topic, callback) => {
return new Promise((resolve, reject) => {
const i = setInterval(async () => {
const peers = await ipfs.pubsub.peers(topic)
const hasAllPeers = peersToWait.map((e) => peers.includes(e)).filter((e) => e === false).length === 0
if (hasAllPeers) {
clearInterval(i)
resolve()
}
}, 500)
})
}
module.exports = waitForPeers

View File

@ -11,7 +11,6 @@ const Zip = require('adm-zip')
const OrbitDB = require('../src/OrbitDB')
const OrbitDBAddress = require('../src/orbit-db-address')
const io = require('orbit-db-io')
const IPFS = require('ipfs')
const Identities = require('orbit-db-identity-provider')
const migrate = require('localstorage-level-migration')
const Keystore = require('orbit-db-keystore')
@ -32,10 +31,11 @@ const dbPath = path.join('./orbitdb', 'tests', 'v0')
const dbFixturesDir = path.join('./test', 'fixtures', 'v0', 'QmWDUfC4zcWJGgc9UHn1X3qQ5KZqBv4KCiCtjnpMmBT8JC', 'v0-db')
const keyFixtures = path.join('./test', 'fixtures', 'keys','QmRfPsKJs9YqTot5krRibra4gPwoK4kghhU8iKWxBjGDDX')
const ipfsFixtures = path.join('./test', 'fixtures', 'ipfs.zip')
const ipfsFixturesDir = path.join('./test', 'fixtures', 'ipfs')
Object.keys(testAPIs).forEach(API => {
let ipfsFixtures = path.join('./test', 'fixtures', `${API}.zip`)
describe(`orbit-db - Backward-Compatibility - Open & Load (${API})`, function () {
this.retries(1) // windows...
this.timeout(config.timeout)