From e069033f9a67ccbb60cc64068995d3a7bcfea8f7 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Thu, 21 Dec 2023 12:44:53 +0200 Subject: [PATCH] setGhostSpan --- lib/index.js | 12 +++++++++++- test/index.test.js | 5 +++-- test/util.js | 11 ++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index f5f6072..ad5cdce 100644 --- a/lib/index.js +++ b/lib/index.js @@ -77,7 +77,7 @@ function assertDBPlugin(peer) { function initDict(peer, config) { assertDBPlugin(peer) - const ghostSpan = config.dict?.ghostSpan ?? 32 + let ghostSpan = config.dict?.ghostSpan ?? 32 if (ghostSpan < 1) throw new Error('config.dict.ghostSpan must be >= 0') //#region state @@ -458,6 +458,15 @@ function initDict(peer, config) { return ghostSpan } + /** + * @param {number} span + * @returns {void} + */ + function setGhostSpan(span) { + if (span < 1) throw new Error('ghostSpan must be >= 0') + ghostSpan = span + } + /** * @public * @param {string} subdomain @@ -516,6 +525,7 @@ function initDict(peer, config) { getFeedID, isGhostable, getGhostSpan, + setGhostSpan, minGhostDepth, minRequiredDepth, squeeze, diff --git a/test/index.test.js b/test/index.test.js index 8688856..d401626 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -29,8 +29,8 @@ let aliceID test('setup', async (t) => { peer = createPeer({ keypair: aliceKeypair, - path: DIR, - dict: { ghostSpan: 4 }, + db: {path: DIR}, + dict: { ghostSpan: 40 }, }) await peer.db.loaded() @@ -41,6 +41,7 @@ test('setup', async (t) => { }) await p(peer.dict.load)(aliceID) + peer.dict.setGhostSpan(4) assert.equal(peer.dict.getGhostSpan(), 4, 'getGhostSpan') }) diff --git a/test/util.js b/test/util.js index 0d8e070..5f7cb3d 100644 --- a/test/util.js +++ b/test/util.js @@ -1,19 +1,20 @@ -const os = require('node:os') -const path = require('node:path') +const OS = require('node:os') +const Path = require('node:path') const rimraf = require('rimraf') const caps = require('ppppp-caps') const Keypair = require('ppppp-keypair') function createPeer(opts) { if (opts.name) { - opts.path ??= path.join(os.tmpdir(), 'tanglesync-' + opts.name) + const tmp = OS.tmpdir() + opts.db ??= {path: Path.join(tmp, `ppppp-dict-${opts.name}-${Date.now()}`)} opts.keypair ??= Keypair.generate('ed25519', opts.name) opts.name = undefined } - if (!opts.path) throw new Error('need opts.path in createPeer()') + if (!opts.db.path) throw new Error('need opts.path in createPeer()') if (!opts.keypair) throw new Error('need opts.keypair in createPeer()') - rimraf.sync(opts.path) + rimraf.sync(opts.db.path) return require('secret-stack/bare')() .use(require('secret-stack/plugins/net')) .use(require('secret-handshake-ext/secret-stack'))