Skip to content

Commit

Permalink
fix(ls): deleted entries could cause a premature stream EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jan 7, 2018
1 parent 32dc59a commit 347dc36
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ function lsStream (cache) {
}, new Map())

return getKeyToEntry.then(reduced => {
return Array.from(reduced.values()).map(
entry => stream.push(formatEntry(cache, entry))
)
for (let entry of reduced.values()) {
const formatted = formatEntry(cache, entry)
formatted && stream.push(formatted)
}
}).catch({code: 'ENOENT'}, nop)
})
})
Expand Down
58 changes: 57 additions & 1 deletion test/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
const BB = require('bluebird')

const CacheIndex = require('./util/cache-index')
const contentPath = require('../lib/content/path')
const finished = BB.promisify(require('mississippi').finished)
const index = require('../lib/entry-index.js')
const path = require('path')
const Tacks = require('tacks')
const test = require('tap').test
const testDir = require('./util/test-dir')(__filename)

const CACHE = path.join(testDir, 'cache')
const contentPath = require('../lib/content/path')
const File = Tacks.File

const ls = require('..').ls
Expand Down Expand Up @@ -111,3 +112,58 @@ test('ignores non-dir files', function (t) {
t.equal(listing.whatever.key, 'whatever', 'only the correct entry listed')
})
})

test('correctly ignores deleted entries', t => {
const contents = {
'whatever': {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 234234
},
'whatnot': {
key: 'whatnot',
integrity: 'sha512-bada55',
time: 54321,
metadata: null,
size: 425345345
},
'whatwhere': {
key: 'whatwhere',
integrity: 'sha512-bada55e5',
time: 54321,
metadata: null,
size: 425345345
}
}
const fixture = new Tacks(CacheIndex(contents))
contents.whatever.path =
contentPath(
CACHE, contents.whatever.integrity)
contents.whatnot.path =
contentPath(
CACHE, contents.whatnot.integrity)
contents.whatwhere.path =
contentPath(
CACHE, contents.whatwhere.integrity)
fixture.create(CACHE)
return index.delete(CACHE, 'whatnot')
.then(() => ls(CACHE))
.then(listing => t.deepEqual(listing, {
whatever: contents.whatever,
whatwhere: contents.whatwhere
}, 'index contents correct'))
.then(() => {
const listing = []
const stream = ls.stream(CACHE)
stream.on('data', entry => {
listing[entry.key] = entry
})
return finished(stream)
.then(() => t.deepEqual(listing, {
whatever: contents.whatever,
whatwhere: contents.whatwhere
}, 'ls is streamable'))
})
})

0 comments on commit 347dc36

Please sign in to comment.