Skip to content

Commit

Permalink
feat: allow formatEntry to keep entries with no integrity value
Browse files Browse the repository at this point in the history
PR-URL: #53
Credit: @nlf
Close: #53
Reviewed-by: @isaacs
  • Loading branch information
nlf authored and isaacs committed May 19, 2021
1 parent c4efb74 commit 930f531
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function compact (cache, key, matchFn, opts = {}) {
// write the file atomically
await disposer(setup(), teardown, write)

return newEntries.map((entry) => formatEntry(cache, entry))
return newEntries.map((entry) => formatEntry(cache, entry, true))
}

module.exports.insert = insert
Expand Down Expand Up @@ -346,15 +346,15 @@ function hash (str, digest) {
.digest('hex')
}

function formatEntry (cache, entry) {
function formatEntry (cache, entry, keepAll) {
// Treat null digests as deletions. They'll shadow any previous entries.
if (!entry.integrity) {
if (!entry.integrity && !keepAll) {
return null
}
return {
key: entry.key,
integrity: entry.integrity,
path: contentPath(cache, entry.integrity),
path: entry.integrity ? contentPath(cache, entry.integrity) : undefined,
size: entry.size,
time: entry.time,
metadata: entry.metadata
Expand Down
10 changes: 6 additions & 4 deletions test/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,21 @@ test('compact', async (t) => {
index.insert(CACHE, KEY, INTEGRITY, { metadata: { rev: 1 } }),
index.insert(CACHE, KEY, INTEGRITY, { metadata: { rev: 2 } }),
index.insert(CACHE, KEY, INTEGRITY, { metadata: { rev: 2 } }),
index.insert(CACHE, KEY, INTEGRITY, { metadata: { rev: 1 } })
index.insert(CACHE, KEY, INTEGRITY, { metadata: { rev: 1 } }),
// compact will return entries with a null integrity
index.insert(CACHE, KEY, null, { metadata: { rev: 3 } })
])

const bucket = index.bucketPath(CACHE, KEY)
const entries = await index.bucketEntries(bucket)
t.equal(entries.length, 4, 'started with 4 entries')
t.equal(entries.length, 5, 'started with 5 entries')

const filter = (entryA, entryB) => entryA.metadata.rev === entryB.metadata.rev
const compacted = await index.compact(CACHE, KEY, filter)
t.equal(compacted.length, 2, 'should return only two entries')
t.equal(compacted.length, 3, 'should return only three entries')

const newEntries = await index.bucketEntries(bucket)
t.equal(newEntries.length, 2, 'bucket was deduplicated')
t.equal(newEntries.length, 3, 'bucket was deduplicated')
})

test('compact: ENOENT in chownr does not cause failure', async (t) => {
Expand Down

0 comments on commit 930f531

Please sign in to comment.