Skip to content

Commit

Permalink
fix(put): don't flush if an error happened
Browse files Browse the repository at this point in the history
What happens now is that the index is written even if there was an error
in the content. We don't want this.
  • Loading branch information
wraithgar committed Apr 27, 2022
1 parent 4279989 commit e870016
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function putStream (cache, key, opts = {}) {
opts = putOpts(opts)
let integrity
let size
let error

let memoData
const pipeline = new Pipeline()
Expand All @@ -58,28 +59,33 @@ function putStream (cache, key, opts = {}) {
.on('size', (s) => {
size = s
})
.on('error', (err) => {
error = err
})

pipeline.push(contentStream)

// last but not least, we write the index and emit hash and size,
// and memoize if we're doing that
pipeline.push(new Flush({
flush () {
return index
.insert(cache, key, integrity, { ...opts, size })
.then((entry) => {
if (memoize && memoData) {
memo.put(cache, entry, memoData, opts)
}
if (!error) {
return index
.insert(cache, key, integrity, { ...opts, size })
.then((entry) => {
if (memoize && memoData) {
memo.put(cache, entry, memoData, opts)
}

if (integrity) {
pipeline.emit('integrity', integrity)
}
if (integrity) {
pipeline.emit('integrity', integrity)
}

if (size) {
pipeline.emit('size', size)
}
})
if (size) {
pipeline.emit('size', size)
}
})
}
},
}))

Expand Down

0 comments on commit e870016

Please sign in to comment.