Skip to content

Commit e870016

Browse files
committedApr 27, 2022
fix(put): don't flush if an error happened
What happens now is that the index is written even if there was an error in the content. We don't want this.
1 parent 4279989 commit e870016

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed
 

‎lib/put.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function putStream (cache, key, opts = {}) {
3737
opts = putOpts(opts)
3838
let integrity
3939
let size
40+
let error
4041

4142
let memoData
4243
const pipeline = new Pipeline()
@@ -58,28 +59,33 @@ function putStream (cache, key, opts = {}) {
5859
.on('size', (s) => {
5960
size = s
6061
})
62+
.on('error', (err) => {
63+
error = err
64+
})
6165

6266
pipeline.push(contentStream)
6367

6468
// last but not least, we write the index and emit hash and size,
6569
// and memoize if we're doing that
6670
pipeline.push(new Flush({
6771
flush () {
68-
return index
69-
.insert(cache, key, integrity, { ...opts, size })
70-
.then((entry) => {
71-
if (memoize && memoData) {
72-
memo.put(cache, entry, memoData, opts)
73-
}
72+
if (!error) {
73+
return index
74+
.insert(cache, key, integrity, { ...opts, size })
75+
.then((entry) => {
76+
if (memoize && memoData) {
77+
memo.put(cache, entry, memoData, opts)
78+
}
7479

75-
if (integrity) {
76-
pipeline.emit('integrity', integrity)
77-
}
80+
if (integrity) {
81+
pipeline.emit('integrity', integrity)
82+
}
7883

79-
if (size) {
80-
pipeline.emit('size', size)
81-
}
82-
})
84+
if (size) {
85+
pipeline.emit('size', size)
86+
}
87+
})
88+
}
8389
},
8490
}))
8591

0 commit comments

Comments
 (0)
Please sign in to comment.