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 })