Skip to content

Commit 5d832f3

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): removed .using/.disposer
1 parent 9c457a0 commit 5d832f3

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

Diff for: lib/content/write.js

+40-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const Y = require('../util/y.js')
1818
const writeFileAsync = BB.promisify(fs.writeFile)
1919

2020
module.exports = write
21+
2122
function write (cache, data, opts) {
2223
opts = opts || {}
2324
if (opts.algorithms && opts.algorithms.length > 1) {
@@ -34,20 +35,23 @@ function write (cache, data, opts) {
3435
if (opts.integrity && !ssri.checkData(data, opts.integrity, opts)) {
3536
return BB.reject(checksumError(opts.integrity, sri))
3637
}
37-
return BB.using(makeTmp(cache, opts), tmp => (
38-
writeFileAsync(
39-
tmp.target, data, { flag: 'wx' }
40-
).then(() => (
41-
moveToDestination(tmp, cache, sri, opts)
42-
))
43-
)).then(() => ({ integrity: sri, size: data.length }))
38+
return makeTmp(cache, opts)
39+
.then((tmp) => {
40+
return writeFileAsync(
41+
tmp.target, data, { flag: 'wx' }
42+
).then(() => moveToDestination(tmp, cache, sri, opts))
43+
.then((result) => makeTmpDisposer(tmp, result))
44+
.catch((err) => makeTmpDisposer(tmp, err, true))
45+
}).then(() => ({ integrity: sri, size: data.length }))
4446
}
4547

4648
module.exports.stream = writeStream
49+
4750
function writeStream (cache, opts) {
4851
opts = opts || {}
4952
const inputStream = new PassThrough()
5053
let inputErr = false
54+
5155
function errCheck () {
5256
if (inputErr) { throw inputErr }
5357
}
@@ -81,16 +85,19 @@ function writeStream (cache, opts) {
8185
}
8286

8387
function handleContent (inputStream, cache, opts, errCheck) {
84-
return BB.using(makeTmp(cache, opts), tmp => {
85-
errCheck()
86-
return pipeToTmp(
87-
inputStream, cache, tmp.target, opts, errCheck
88-
).then(res => {
89-
return moveToDestination(
90-
tmp, cache, res.integrity, opts, errCheck
91-
).then(() => res)
88+
return makeTmp(cache, opts)
89+
.then((tmp) => {
90+
errCheck()
91+
return pipeToTmp(
92+
inputStream, cache, tmp.target, opts, errCheck
93+
).then(res => {
94+
return moveToDestination(
95+
tmp, cache, res.integrity, opts, errCheck
96+
).then(() => res)
97+
})
98+
.then((result) => makeTmpDisposer(tmp, result))
99+
.catch((err) => makeTmpDisposer(tmp, err, true))
92100
})
93-
})
94101
}
95102

96103
function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
@@ -125,7 +132,23 @@ function makeTmp (cache, opts) {
125132
).then(() => ({
126133
target: tmpTarget,
127134
moved: false
128-
})).disposer(tmp => (!tmp.moved && rimraf(tmp.target)))
135+
}))
136+
}
137+
138+
function makeTmpDisposer (tmp, result, shouldThrow = false) {
139+
const returnResult = () => {
140+
if (shouldThrow) {
141+
throw result
142+
}
143+
return result
144+
}
145+
146+
if (tmp.moved) {
147+
return returnResult()
148+
}
149+
return rimraf(tmp.target)
150+
.then(() => returnResult())
151+
.catch(() => returnResult())
129152
}
130153

131154
function moveToDestination (tmp, cache, sri, opts, errCheck) {

Diff for: lib/util/tmp.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,23 @@ function withTmp (cache, opts, cb) {
2828
opts = null
2929
}
3030
opts = TmpOpts(opts)
31-
return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb)
31+
32+
const disposer = (target, result, shouldThrow = false) => {
33+
const returnResult = () => {
34+
if (shouldThrow) {
35+
throw result
36+
}
37+
return result
38+
}
39+
return rimraf(target)
40+
.then(() => returnResult())
41+
.catch(() => returnResult())
42+
}
43+
44+
return mktmpdir(cache, opts)
45+
.then((target) => cb(target)
46+
.then((result) => disposer(target, result))
47+
.catch((err) => disposer(target, err, true)))
3248
}
3349

3450
module.exports.fix = fixtmpdir

0 commit comments

Comments
 (0)