move config.path to config.db.path

This commit is contained in:
Andre Staltz 2023-11-24 15:44:25 +02:00
parent dd492553be
commit 5b81c6ea82
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
17 changed files with 62 additions and 62 deletions

View File

@ -28,6 +28,12 @@ const { decrypt } = require('./encryption')
* @typedef {import('./encryption').EncryptionFormat} EncryptionFormat
* @typedef {import('./msg-v3/tangle')} Tangle
* @typedef {Buffer | Uint8Array} B4A
* @typedef {{
* db: {
* path: string
* }
* }} ExpectedConfig
* @typedef {{db?: Partial<ExpectedConfig['db']>, keypair: Keypair;}} Config
*/
/**
@ -69,6 +75,16 @@ const { decrypt } = require('./encryption')
* } CB
*/
/**
* @param {Config} config
* @returns {asserts config is ExpectedConfig}
*/
function assertValidConfig(config) {
if (typeof config.db?.path !== 'string') {
throw new Error('db requires config.db.path')
}
}
class DBTangle extends MsgV3.Tangle {
/**
* @param {MsgID} rootID
@ -121,9 +137,11 @@ class DBTangle extends MsgV3.Tangle {
/**
* @param {any} peer
* @param {{ path: string; keypair: Keypair; }} config
* @param {Config} config
*/
function initDB(peer, config) {
assertValidConfig(config)
/** @type {Array<Rec | null>} */
const recs = []
@ -152,7 +170,7 @@ function initDB(peer, config) {
},
}
const log = Log(Path.join(config.path, 'db.bin'), {
const log = Log(Path.join(config.db.path, 'log'), {
blockSize: 64 * 1024,
codec,
/**
@ -168,7 +186,7 @@ function initDB(peer, config) {
},
})
const ghosts = new Ghosts(Path.join(config.path, 'ghosts'))
const ghosts = new Ghosts(Path.join(config.db.path, 'ghosts'))
peer.close.hook(function (/** @type {any} */ fn, /** @type {any} */ args) {
log.close(() => {

View File

@ -1,4 +1,3 @@
/**
* @param {any} obj
*/
@ -11,4 +10,4 @@ function isEmptyObject(obj) {
module.exports = {
isEmptyObject,
}
}

View File

@ -19,7 +19,7 @@ test('account.add()', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair1, path: DIR })
.call(null, { keypair: keypair1, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({
@ -78,7 +78,7 @@ test('account.add()', async (t) => {
const peer1 = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair1, path: DIR })
.call(null, { keypair: keypair1, db: { path: DIR } })
await peer1.db.loaded()
const id = await p(peer1.db.account.create)({
@ -102,7 +102,7 @@ test('account.add()', async (t) => {
const peer2 = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair2, path: DIR })
.call(null, { keypair: keypair2, db: { path: DIR } })
await peer2.db.loaded()
await p(peer2.db.add)(msg1, id)
@ -134,7 +134,7 @@ test('account.add()', async (t) => {
const peer1again = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair1, path: DIR })
.call(null, { keypair: keypair1, db: { path: DIR } })
await peer1again.db.loaded()
await p(peer1again.db.add)(msg1, id) // re-add because lost during rimraf
@ -158,7 +158,7 @@ test('account.add()', async (t) => {
let peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair1, path: DIR })
.call(null, { keypair: keypair1, db: { path: DIR } })
await peer.db.loaded()
@ -213,7 +213,7 @@ test('account.add()', async (t) => {
const carol = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypair3, path: DIR })
.call(null, { keypair: keypair3, db: { path: DIR } })
await carol.db.loaded()

View File

@ -17,7 +17,7 @@ test('account.create() ', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({
@ -59,7 +59,7 @@ test('account.create() ', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({
@ -89,7 +89,7 @@ test('account.create() ', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ keypair, subdomain })
@ -109,7 +109,7 @@ test('account.create() ', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ keypair, subdomain })
@ -129,7 +129,7 @@ test('account.create() ', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
@ -140,7 +140,10 @@ test('account.create() ', async (t) => {
})
assert.ok(gotError, 'account not found')
const account = await p(peer.db.account.findOrCreate)({ keypair, subdomain })
const account = await p(peer.db.account.findOrCreate)({
keypair,
subdomain,
})
assert.ok(account, 'account created')
const msg = peer.db.get(account)
assert.equal(msg.data.key.bytes, keypair.public, 'msg.data')

View File

@ -17,7 +17,7 @@ test('add()', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()

View File

@ -16,7 +16,7 @@ test('del()', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
@ -71,7 +71,7 @@ test('del()', async (t) => {
await p(peer.close)(true)
const log = Log(path.join(DIR, 'db.bin'), {
const log = Log(path.join(DIR, 'log'), {
cacheSize: 1,
blockSize: 64 * 1024,
codec: {

View File

@ -16,7 +16,7 @@ test('erase()', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
@ -82,7 +82,7 @@ test('erase()', async (t) => {
await p(peer.close)(true)
const log = Log(path.join(DIR, 'db.bin'), {
const log = Log(path.join(DIR, 'log'), {
cacheSize: 1,
blockSize: 64 * 1024,
codec: {

View File

@ -17,7 +17,7 @@ test('feed.getID()', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()

View File

@ -25,7 +25,7 @@ test('feed.publish()', async (t) => {
peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
@ -158,5 +158,5 @@ test('feed.publish()', async (t) => {
)
})
await p(peer.close)(true)
await p(peer.close)(true)
})

View File

@ -17,7 +17,7 @@ test('get()', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()

View File

@ -25,7 +25,7 @@ test('getTangle()', async (t) => {
peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair: keypairA, path: DIR })
.call(null, { keypair: keypairA, db: { path: DIR } })
await peer.db.loaded()

View File

@ -16,7 +16,7 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
@ -62,7 +62,7 @@ test('ghosts.add queues very-concurrent calls', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()

View File

@ -28,11 +28,7 @@ test('MsgV3 lipmaa prevs', (t) => {
const msgID1 = MsgV3.getMsgID(msg1)
tangle.add(msgID1, msg1)
assert.equal(msg1.metadata.tangles[mootID].depth, 1, 'msg1 depth')
assert.deepEqual(
msg1.metadata.tangles[mootID].prev,
[mootID],
'msg1 prev'
)
assert.deepEqual(msg1.metadata.tangles[mootID].prev, [mootID], 'msg1 prev')
const msg2 = MsgV3.create({
account,
@ -47,11 +43,7 @@ test('MsgV3 lipmaa prevs', (t) => {
const msgID2 = MsgV3.getMsgID(msg2)
tangle.add(msgID2, msg2)
assert.equal(msg2.metadata.tangles[mootID].depth, 2, 'msg2 depth')
assert.deepEqual(
msg2.metadata.tangles[mootID].prev,
[msgID1],
'msg2 prev'
)
assert.deepEqual(msg2.metadata.tangles[mootID].prev, [msgID1], 'msg2 prev')
const msg3 = MsgV3.create({
account,
@ -85,11 +77,7 @@ test('MsgV3 lipmaa prevs', (t) => {
const msgID4 = MsgV3.getMsgID(msg4)
tangle.add(msgID4, msg4)
assert.equal(msg4.metadata.tangles[mootID].depth, 4, 'msg4 depth')
assert.deepEqual(
msg4.metadata.tangles[mootID].prev,
[msgID3],
'msg4 prev'
)
assert.deepEqual(msg4.metadata.tangles[mootID].prev, [msgID3], 'msg4 prev')
const msg5 = MsgV3.create({
account,
@ -104,11 +92,7 @@ test('MsgV3 lipmaa prevs', (t) => {
const msgID5 = MsgV3.getMsgID(msg5)
tangle.add(msgID5, msg5)
assert.equal(msg5.metadata.tangles[mootID].depth, 5, 'msg5 depth')
assert.deepEqual(
msg5.metadata.tangles[mootID].prev,
[msgID4],
'msg5 prev'
)
assert.deepEqual(msg5.metadata.tangles[mootID].prev, [msgID4], 'msg5 prev')
const msg6 = MsgV3.create({
account,
@ -123,11 +107,7 @@ test('MsgV3 lipmaa prevs', (t) => {
const msgID6 = MsgV3.getMsgID(msg6)
tangle.add(msgID6, msg6)
assert.equal(msg6.metadata.tangles[mootID].depth, 6, 'msg6 depth')
assert.deepEqual(
msg6.metadata.tangles[mootID].prev,
[msgID5],
'msg6 prev'
)
assert.deepEqual(msg6.metadata.tangles[mootID].prev, [msgID5], 'msg6 prev')
const msg7 = MsgV3.create({
account,

View File

@ -15,11 +15,11 @@ test('msgs() iterator', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = (await p(peer.db.account.create)({subdomain: 'person'}))
const account = await p(peer.db.account.create)({ subdomain: 'person' })
for (let i = 0; i < 6; i++) {
await p(peer.db.feed.publish)({

View File

@ -15,11 +15,11 @@ test('onRecordAdded', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = (await p(peer.db.account.create)({subdomain: 'person'}))
const account = await p(peer.db.account.create)({ subdomain: 'person' })
const listened = []
var remove = peer.db.onRecordAdded((ev) => {

View File

@ -16,7 +16,7 @@ test('publish some msgs, close, re-open', async (t) => {
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = await p(peer.db.account.create)({ subdomain: 'person' })
@ -42,7 +42,7 @@ test('publish some msgs, close, re-open', async (t) => {
const peer2 = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.use(require('ssb-box'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
// t.pass('re-opened')
await peer2.db.loaded()

View File

@ -15,10 +15,10 @@ test('records() iterator', async (t) => {
const keypair = Keypair.generate('ed25519', 'alice')
const peer = SecretStack({ appKey: caps.shse })
.use(require('../lib'))
.call(null, { keypair, path: DIR })
.call(null, { keypair, db: { path: DIR } })
await peer.db.loaded()
const account = (await p(peer.db.account.create)({ subdomain: 'person' }))
const account = await p(peer.db.account.create)({ subdomain: 'person' })
for (let i = 0; i < 6; i++) {
await p(peer.db.feed.publish)({