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('./encryption').EncryptionFormat} EncryptionFormat
* @typedef {import('./msg-v3/tangle')} Tangle * @typedef {import('./msg-v3/tangle')} Tangle
* @typedef {Buffer | Uint8Array} B4A * @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 * } 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 { class DBTangle extends MsgV3.Tangle {
/** /**
* @param {MsgID} rootID * @param {MsgID} rootID
@ -121,9 +137,11 @@ class DBTangle extends MsgV3.Tangle {
/** /**
* @param {any} peer * @param {any} peer
* @param {{ path: string; keypair: Keypair; }} config * @param {Config} config
*/ */
function initDB(peer, config) { function initDB(peer, config) {
assertValidConfig(config)
/** @type {Array<Rec | null>} */ /** @type {Array<Rec | null>} */
const recs = [] 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, blockSize: 64 * 1024,
codec, 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) { peer.close.hook(function (/** @type {any} */ fn, /** @type {any} */ args) {
log.close(() => { log.close(() => {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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