From 05e16a70375171bb0c7e4e61655886968e05c8a0 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Thu, 19 Oct 2023 13:46:16 +0300 Subject: [PATCH] reading empty ghosts should not raise error --- lib/utils.js | 8 ++++++-- test/ghosts.tests.js | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 2419b67..6617f9c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -123,9 +123,13 @@ class GhostDB { this.#path(tangleID), GhostDB.encodingOpts, (/** @type {any} */ err, /** @type {any} */ str) => { + // Load Map + /** @type {Map} */ + let map; + if (err && err.code === 'ENOENT') map = new Map() // prettier-ignore - if (err) return cb(new Error('GhostDB.read() failed to read ghost file', { cause: err })) - const map = this.#deserialize(str) + else if (err) return cb(new Error('GhostDB.read() failed to read ghost file', { cause: err })) + else map = this.#deserialize(str) cb(null, map) } diff --git a/test/ghosts.tests.js b/test/ghosts.tests.js index 506acd9..5044350 100644 --- a/test/ghosts.tests.js +++ b/test/ghosts.tests.js @@ -34,6 +34,9 @@ test('ghosts.add, ghosts.get, ghosts.getMinDepth', async (t) => { } const feedID = peer.db.feed.getID(account, 'post') + const ghosts0 = await p(peer.db.ghosts.get)(feedID) + assert.deepEqual(ghosts0, [], 'no ghosts so far') + await p(peer.db.ghosts.add)({ msg: msgIDs[0], tangle: feedID, max: MAX }) await p(peer.db.ghosts.add)({ msg: msgIDs[1], tangle: feedID, max: MAX }) await p(peer.db.ghosts.add)({ msg: msgIDs[2], tangle: feedID, max: MAX })