reading empty ghosts should not raise error

This commit is contained in:
Andre Staltz 2023-10-19 13:46:16 +03:00
parent 748125b6e6
commit 05e16a7037
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 9 additions and 2 deletions

View File

@ -123,9 +123,13 @@ class GhostDB {
this.#path(tangleID),
GhostDB.encodingOpts,
(/** @type {any} */ err, /** @type {any} */ str) => {
// Load Map
/** @type {Map<string, number>} */
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)
}

View File

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