Skip to content

Commit 347dc36

Browse files
committed
fix(ls): deleted entries could cause a premature stream EOF
1 parent 32dc59a commit 347dc36

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

lib/entry-index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ function lsStream (cache) {
116116
}, new Map())
117117

118118
return getKeyToEntry.then(reduced => {
119-
return Array.from(reduced.values()).map(
120-
entry => stream.push(formatEntry(cache, entry))
121-
)
119+
for (let entry of reduced.values()) {
120+
const formatted = formatEntry(cache, entry)
121+
formatted && stream.push(formatted)
122+
}
122123
}).catch({code: 'ENOENT'}, nop)
123124
})
124125
})

test/ls.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
const BB = require('bluebird')
44

55
const CacheIndex = require('./util/cache-index')
6+
const contentPath = require('../lib/content/path')
67
const finished = BB.promisify(require('mississippi').finished)
8+
const index = require('../lib/entry-index.js')
79
const path = require('path')
810
const Tacks = require('tacks')
911
const test = require('tap').test
1012
const testDir = require('./util/test-dir')(__filename)
1113

1214
const CACHE = path.join(testDir, 'cache')
13-
const contentPath = require('../lib/content/path')
1415
const File = Tacks.File
1516

1617
const ls = require('..').ls
@@ -111,3 +112,58 @@ test('ignores non-dir files', function (t) {
111112
t.equal(listing.whatever.key, 'whatever', 'only the correct entry listed')
112113
})
113114
})
115+
116+
test('correctly ignores deleted entries', t => {
117+
const contents = {
118+
'whatever': {
119+
key: 'whatever',
120+
integrity: 'sha512-deadbeef',
121+
time: 12345,
122+
metadata: 'omgsometa',
123+
size: 234234
124+
},
125+
'whatnot': {
126+
key: 'whatnot',
127+
integrity: 'sha512-bada55',
128+
time: 54321,
129+
metadata: null,
130+
size: 425345345
131+
},
132+
'whatwhere': {
133+
key: 'whatwhere',
134+
integrity: 'sha512-bada55e5',
135+
time: 54321,
136+
metadata: null,
137+
size: 425345345
138+
}
139+
}
140+
const fixture = new Tacks(CacheIndex(contents))
141+
contents.whatever.path =
142+
contentPath(
143+
CACHE, contents.whatever.integrity)
144+
contents.whatnot.path =
145+
contentPath(
146+
CACHE, contents.whatnot.integrity)
147+
contents.whatwhere.path =
148+
contentPath(
149+
CACHE, contents.whatwhere.integrity)
150+
fixture.create(CACHE)
151+
return index.delete(CACHE, 'whatnot')
152+
.then(() => ls(CACHE))
153+
.then(listing => t.deepEqual(listing, {
154+
whatever: contents.whatever,
155+
whatwhere: contents.whatwhere
156+
}, 'index contents correct'))
157+
.then(() => {
158+
const listing = []
159+
const stream = ls.stream(CACHE)
160+
stream.on('data', entry => {
161+
listing[entry.key] = entry
162+
})
163+
return finished(stream)
164+
.then(() => t.deepEqual(listing, {
165+
whatever: contents.whatever,
166+
whatwhere: contents.whatwhere
167+
}, 'ls is streamable'))
168+
})
169+
})

0 commit comments

Comments
 (0)