diff --git a/lib/index.js b/lib/index.js index 1b2d9fb..74ed33e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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, 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} */ 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(() => { diff --git a/lib/msg-v3/util.js b/lib/msg-v3/util.js index 78e8a48..9455185 100644 --- a/lib/msg-v3/util.js +++ b/lib/msg-v3/util.js @@ -1,4 +1,3 @@ - /** * @param {any} obj */ @@ -11,4 +10,4 @@ function isEmptyObject(obj) { module.exports = { isEmptyObject, -} \ No newline at end of file +} diff --git a/test/account-add.test.js b/test/account-add.test.js index 0f6bfd9..7869e5d 100644 --- a/test/account-add.test.js +++ b/test/account-add.test.js @@ -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() diff --git a/test/account-create.test.js b/test/account-create.test.js index 7b06ae8..fa314a9 100644 --- a/test/account-create.test.js +++ b/test/account-create.test.js @@ -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') diff --git a/test/add.test.js b/test/add.test.js index 5b7805b..4422905 100644 --- a/test/add.test.js +++ b/test/add.test.js @@ -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() diff --git a/test/del.test.js b/test/del.test.js index ef788bc..0d1cdd3 100644 --- a/test/del.test.js +++ b/test/del.test.js @@ -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: { diff --git a/test/erase.test.js b/test/erase.test.js index be97a5b..9cb75b9 100644 --- a/test/erase.test.js +++ b/test/erase.test.js @@ -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: { diff --git a/test/feed-get-id.test.js b/test/feed-get-id.test.js index eb56540..fc25b85 100644 --- a/test/feed-get-id.test.js +++ b/test/feed-get-id.test.js @@ -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() diff --git a/test/feed-publish.test.js b/test/feed-publish.test.js index bcdefd2..25af71d 100644 --- a/test/feed-publish.test.js +++ b/test/feed-publish.test.js @@ -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) }) diff --git a/test/get.test.js b/test/get.test.js index b199339..389f2db 100644 --- a/test/get.test.js +++ b/test/get.test.js @@ -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() diff --git a/test/getTangle.test.js b/test/getTangle.test.js index 6ac983e..c524ff8 100644 --- a/test/getTangle.test.js +++ b/test/getTangle.test.js @@ -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() diff --git a/test/ghosts.tests.js b/test/ghosts.tests.js index bea9624..ff1cc4c 100644 --- a/test/ghosts.tests.js +++ b/test/ghosts.tests.js @@ -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() diff --git a/test/msg-v3/lipmaa.test.js b/test/msg-v3/lipmaa.test.js index bcf8815..a5e2967 100644 --- a/test/msg-v3/lipmaa.test.js +++ b/test/msg-v3/lipmaa.test.js @@ -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, diff --git a/test/msgs-iterator.test.js b/test/msgs-iterator.test.js index a673c60..c6bd2d3 100644 --- a/test/msgs-iterator.test.js +++ b/test/msgs-iterator.test.js @@ -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)({ diff --git a/test/on-record-added.test.js b/test/on-record-added.test.js index 949e4d3..0bca52f 100644 --- a/test/on-record-added.test.js +++ b/test/on-record-added.test.js @@ -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) => { diff --git a/test/re-open.test.js b/test/re-open.test.js index 89f8f80..31c5861 100644 --- a/test/re-open.test.js +++ b/test/re-open.test.js @@ -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() diff --git a/test/records-iterator.test.js b/test/records-iterator.test.js index 349021b..6b20fe7 100644 --- a/test/records-iterator.test.js +++ b/test/records-iterator.test.js @@ -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)({