feat: Configurable heads and entry storage. (#42)

* feat: Configurable heads and entry storage.

* fix: Linting.

* test: Re-enable all tests.

* test: Custom entry storage.

* test: Check for heads paths.

* fix: Check for path using fs.
This commit is contained in:
Hayden Young
2023-03-14 05:42:05 +08:00
committed by GitHub
parent a40bc8bdcf
commit aabfd4e2bc
3 changed files with 227 additions and 103 deletions

View File

@@ -1,8 +1,12 @@
import { strictEqual, deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { existsSync } from 'fs'
import { copy } from 'fs-extra'
import * as IPFS from 'ipfs'
import Path from 'path'
import { Log, Entry, Database, KeyStore, Identities } from '../src/index.js'
import LevelStorage from '../src/storage/level.js'
import MemoryStorage from '../src/storage/memory.js'
import config from './config.js'
import testKeysPath from './fixtures/test-keys-path.js'
@@ -49,15 +53,12 @@ describe('Database', function () {
await rmrf('./ipfs1')
})
beforeEach(async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb1' })
})
afterEach(async () => {
await rmrf('./orbitdb1')
await rmrf('./orbitdb')
})
it('adds an operation', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
const expected = 'zdpuAqQ9TJpMhPShuT315m2D9LUBkBPy8YX9zatjEynd2suZv'
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const actual = await db.addOperation(op)
@@ -67,7 +68,75 @@ describe('Database', function () {
await db.close()
})
describe('Options', () => {
it('uses default directory for headsStorage', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
const headsPath = Path.join('./orbitdb/', `${databaseId}/`, '/log/_heads/')
strictEqual(await existsSync(headsPath), true)
await db.close()
const headsStorage = await LevelStorage({ path: headsPath })
deepStrictEqual((await Entry.decode(await headsStorage.get(hash))).payload, op)
await headsStorage.close()
await rmrf(headsPath)
})
it('uses given directory for headsStorage', async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './custom-directory' })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
const headsPath = Path.join('./custom-directory/', `${databaseId}/`, '/log/_heads/')
strictEqual(await existsSync(headsPath), true)
await db.close()
const headsStorage = await LevelStorage({ path: headsPath })
deepStrictEqual((await Entry.decode(await headsStorage.get(hash))).payload, op)
await headsStorage.close()
await rmrf(headsPath)
})
it('uses given MemoryStorage for headsStorage', async () => {
const headsStorage = await MemoryStorage()
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', headsStorage })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
deepStrictEqual((await Entry.decode(await headsStorage.get(hash))).payload, op)
await db.close()
})
it('uses given MemoryStorage for entryStorage', async () => {
const entryStorage = await MemoryStorage()
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb', entryStorage })
const op = { op: 'PUT', key: 1, value: 'record 1 on db 1' }
const hash = await db.addOperation(op)
deepStrictEqual((await Entry.decode(await entryStorage.get(hash))).payload, op)
await db.close()
})
})
describe('Events', () => {
beforeEach(async () => {
db = await Database({ OpLog, ipfs, identity: testIdentity, address: databaseId, accessController, directory: './orbitdb' })
})
it('emits \'close\' when the database is closed', async () => {
let closed = false
const onClose = () => {