From 62b2d8d06ae0a762d74a12bac17fc50a42731ee3 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 2 May 2023 11:00:33 -0700 Subject: [PATCH] fix: don't clobber time when verifying cache (#199) --- lib/entry-index.js | 4 ++-- lib/verify.js | 1 + test/verify.js | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/entry-index.js b/lib/entry-index.js index 83b042a..722a37a 100644 --- a/lib/entry-index.js +++ b/lib/entry-index.js @@ -109,12 +109,12 @@ async function compact (cache, key, matchFn, opts = {}) { module.exports.insert = insert async function insert (cache, key, integrity, opts = {}) { - const { metadata, size } = opts + const { metadata, size, time } = opts const bucket = bucketPath(cache, key) const entry = { key, integrity: integrity && ssri.stringify(integrity), - time: Date.now(), + time: time || Date.now(), size, metadata, } diff --git a/lib/verify.js b/lib/verify.js index 729ae98..62e85c9 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -224,6 +224,7 @@ async function rebuildBucket (cache, bucket, stats, opts) { await index.insert(cache, entry.key, entry.integrity, { metadata: entry.metadata, size: entry.size, + time: entry.time, }) stats.totalEntries++ } catch (err) { diff --git a/test/verify.js b/test/verify.js index 37cfb46..a3f8333 100644 --- a/test/verify.js +++ b/test/verify.js @@ -400,3 +400,13 @@ t.test('handles multiple hashes of the same content', async t => { t.match(ls.test.integrity, 'sha512') t.match(ls.test.integrity, 'sha256') }) + +t.test('does not clobber entry time', async t => { + const cache = t.testdir() + const content = Buffer.from('CONTENT!', 'utf8') + await cacache.put(cache, 'test', content) + const entryBefore = await cacache.get.info(cache, 'test') + await cacache.verify(cache) + const entryAfter = await cacache.get.info(cache, 'test') + t.equal(entryBefore.time, entryAfter.time, 'time does not change') +})