Skip to content

Commit

Permalink
feat(promise): removed .using/.disposer
Browse files Browse the repository at this point in the history
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent 9c457a0 commit 5d832f3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
57 changes: 40 additions & 17 deletions lib/content/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Y = require('../util/y.js')
const writeFileAsync = BB.promisify(fs.writeFile)

module.exports = write

function write (cache, data, opts) {
opts = opts || {}
if (opts.algorithms && opts.algorithms.length > 1) {
Expand All @@ -34,20 +35,23 @@ function write (cache, data, opts) {
if (opts.integrity && !ssri.checkData(data, opts.integrity, opts)) {
return BB.reject(checksumError(opts.integrity, sri))
}
return BB.using(makeTmp(cache, opts), tmp => (
writeFileAsync(
tmp.target, data, { flag: 'wx' }
).then(() => (
moveToDestination(tmp, cache, sri, opts)
))
)).then(() => ({ integrity: sri, size: data.length }))
return makeTmp(cache, opts)
.then((tmp) => {
return writeFileAsync(
tmp.target, data, { flag: 'wx' }
).then(() => moveToDestination(tmp, cache, sri, opts))
.then((result) => makeTmpDisposer(tmp, result))
.catch((err) => makeTmpDisposer(tmp, err, true))
}).then(() => ({ integrity: sri, size: data.length }))
}

module.exports.stream = writeStream

function writeStream (cache, opts) {
opts = opts || {}
const inputStream = new PassThrough()
let inputErr = false

function errCheck () {
if (inputErr) { throw inputErr }
}
Expand Down Expand Up @@ -81,16 +85,19 @@ function writeStream (cache, opts) {
}

function handleContent (inputStream, cache, opts, errCheck) {
return BB.using(makeTmp(cache, opts), tmp => {
errCheck()
return pipeToTmp(
inputStream, cache, tmp.target, opts, errCheck
).then(res => {
return moveToDestination(
tmp, cache, res.integrity, opts, errCheck
).then(() => res)
return makeTmp(cache, opts)
.then((tmp) => {
errCheck()
return pipeToTmp(
inputStream, cache, tmp.target, opts, errCheck
).then(res => {
return moveToDestination(
tmp, cache, res.integrity, opts, errCheck
).then(() => res)
})
.then((result) => makeTmpDisposer(tmp, result))
.catch((err) => makeTmpDisposer(tmp, err, true))
})
})
}

function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
Expand Down Expand Up @@ -125,7 +132,23 @@ function makeTmp (cache, opts) {
).then(() => ({
target: tmpTarget,
moved: false
})).disposer(tmp => (!tmp.moved && rimraf(tmp.target)))
}))
}

function makeTmpDisposer (tmp, result, shouldThrow = false) {
const returnResult = () => {
if (shouldThrow) {
throw result
}
return result
}

if (tmp.moved) {
return returnResult()
}
return rimraf(tmp.target)
.then(() => returnResult())
.catch(() => returnResult())
}

function moveToDestination (tmp, cache, sri, opts, errCheck) {
Expand Down
18 changes: 17 additions & 1 deletion lib/util/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ function withTmp (cache, opts, cb) {
opts = null
}
opts = TmpOpts(opts)
return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb)

const disposer = (target, result, shouldThrow = false) => {
const returnResult = () => {
if (shouldThrow) {
throw result
}
return result
}
return rimraf(target)
.then(() => returnResult())
.catch(() => returnResult())
}

return mktmpdir(cache, opts)
.then((target) => cb(target)
.then((result) => disposer(target, result))
.catch((err) => disposer(target, err, true)))
}

module.exports.fix = fixtmpdir
Expand Down

0 comments on commit 5d832f3

Please sign in to comment.