Skip to content

Commit 76ab648

Browse files
committedApr 27, 2022
fix: remove disposer
promises have finally. actually
1 parent e77658c commit 76ab648

File tree

5 files changed

+58
-214
lines changed

5 files changed

+58
-214
lines changed
 

‎lib/content/write.js

+30-35
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,37 @@ const path = require('path')
1313
const rimraf = util.promisify(require('rimraf'))
1414
const ssri = require('ssri')
1515
const uniqueFilename = require('unique-filename')
16-
const { disposer } = require('./../util/disposer')
1716
const fsm = require('fs-minipass')
1817

1918
const writeFile = util.promisify(fs.writeFile)
2019

2120
module.exports = write
2221

23-
function write (cache, data, opts = {}) {
22+
async function write (cache, data, opts = {}) {
2423
const { algorithms, size, integrity } = opts
2524
if (algorithms && algorithms.length > 1) {
2625
throw new Error('opts.algorithms only supports a single algorithm for now')
2726
}
2827

2928
if (typeof size === 'number' && data.length !== size) {
30-
return Promise.reject(sizeError(size, data.length))
29+
throw sizeError(size, data.length)
3130
}
3231

3332
const sri = ssri.fromData(data, algorithms ? { algorithms } : {})
3433
if (integrity && !ssri.checkData(data, integrity, opts)) {
35-
return Promise.reject(checksumError(integrity, sri))
34+
throw checksumError(integrity, sri)
3635
}
3736

38-
return disposer(makeTmp(cache, opts), makeTmpDisposer,
39-
(tmp) => {
40-
return writeFile(tmp.target, data, { flag: 'wx' })
41-
.then(() => moveToDestination(tmp, cache, sri, opts))
42-
})
43-
.then(() => ({ integrity: sri, size: data.length }))
37+
const tmp = await makeTmp(cache, opts)
38+
try {
39+
await writeFile(tmp.target, data, { flag: 'wx' })
40+
await moveToDestination(tmp, cache, sri, opts)
41+
return { integrity: sri, size: data.length }
42+
} finally {
43+
if (!tmp.moved) {
44+
await rimraf(tmp.target)
45+
}
46+
}
4447
}
4548

4649
module.exports.stream = writeStream
@@ -94,18 +97,22 @@ function writeStream (cache, opts = {}) {
9497
return new CacacheWriteStream(cache, opts)
9598
}
9699

97-
function handleContent (inputStream, cache, opts) {
98-
return disposer(makeTmp(cache, opts), makeTmpDisposer, (tmp) => {
99-
return pipeToTmp(inputStream, cache, tmp.target, opts)
100-
.then((res) => {
101-
return moveToDestination(
102-
tmp,
103-
cache,
104-
res.integrity,
105-
opts
106-
).then(() => res)
107-
})
108-
})
100+
async function handleContent (inputStream, cache, opts) {
101+
const tmp = await makeTmp(cache, opts)
102+
try {
103+
const res = await pipeToTmp(inputStream, cache, tmp.target, opts)
104+
await moveToDestination(
105+
tmp,
106+
cache,
107+
res.integrity,
108+
opts
109+
)
110+
return res
111+
} finally {
112+
if (!tmp.moved) {
113+
await rimraf(tmp.target)
114+
}
115+
}
109116
}
110117

111118
function pipeToTmp (inputStream, cache, tmpTarget, opts) {
@@ -136,11 +143,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts) {
136143
outStream
137144
)
138145

139-
return pipeline.promise()
140-
.then(() => ({ integrity, size }))
141-
.catch(er => rimraf(tmpTarget).then(() => {
142-
throw er
143-
}))
146+
return pipeline.promise().then(() => ({ integrity, size }))
144147
}
145148

146149
function makeTmp (cache, opts) {
@@ -151,14 +154,6 @@ function makeTmp (cache, opts) {
151154
}))
152155
}
153156

154-
function makeTmpDisposer (tmp) {
155-
if (tmp.moved) {
156-
return Promise.resolve()
157-
}
158-
159-
return rimraf(tmp.target)
160-
}
161-
162157
function moveToDestination (tmp, cache, sri, opts) {
163158
const destination = contentPath(cache, sri)
164159
const destDir = path.dirname(destination)

‎lib/entry-index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const path = require('path')
88
const ssri = require('ssri')
99
const uniqueFilename = require('unique-filename')
1010

11-
const { disposer } = require('./util/disposer')
1211
const contentPath = require('./content/path')
1312
const fixOwner = require('./util/fix-owner')
1413
const hashToSegments = require('./util/hash-to-segments')
@@ -102,7 +101,12 @@ async function compact (cache, key, matchFn, opts = {}) {
102101
}
103102

104103
// write the file atomically
105-
await disposer(setup(), teardown, write)
104+
const tmp = await setup()
105+
try {
106+
await write(tmp)
107+
} finally {
108+
await teardown(tmp)
109+
}
106110

107111
// we reverse the list we generated such that the newest
108112
// entries come first in order to make looping through them easier

‎lib/util/disposer.js

-31
This file was deleted.

‎test/content.write.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ t.test('cleans up tmp on successful completion', (t) => {
209209
}))
210210
})
211211

212-
t.test('cleans up tmp on error', (t) => {
212+
t.test('cleans up tmp on streaming error', (t) => {
213213
const CONTENT = 'foobarbaz'
214214
const CACHE = t.testdir()
215215
return t.rejects(
@@ -233,6 +233,25 @@ t.test('cleans up tmp on error', (t) => {
233233
}))
234234
})
235235

236+
t.test('cleans up tmp on non streaming error', (t) => {
237+
// mock writefile and make it reject
238+
const CONTENT = 'foobarbaz'
239+
const CACHE = t.testdir({ 'content-v2': 'oh no a file' })
240+
return t.rejects(write(CACHE, CONTENT))
241+
.then(() => new Promise((resolve, reject) => {
242+
const tmp = path.join(CACHE, 'tmp')
243+
fs.readdir(tmp, function (err, files) {
244+
if (!err || (err && err.code === 'ENOENT')) {
245+
files = files || []
246+
t.same(files, [], 'nothing in the tmp dir!')
247+
resolve()
248+
} else {
249+
reject(err)
250+
}
251+
})
252+
}))
253+
})
254+
236255
t.test('checks the size of stream data if opts.size provided', (t) => {
237256
const CONTENT = 'foobarbaz'
238257
let int1 = null
@@ -270,12 +289,11 @@ t.test('checks the size of stream data if opts.size provided', (t) => {
270289
})
271290
})
272291

273-
t.test('only one algorithm for now', t => {
292+
t.test('only one algorithm for now', async t => {
274293
const CACHE = t.testdir()
275-
t.throws(() => write(CACHE, 'foo', { algorithms: [1, 2] }), {
294+
await t.rejects(() => write(CACHE, 'foo', { algorithms: [1, 2] }), {
276295
message: 'opts.algorithms only supports a single algorithm for now',
277296
})
278-
t.end()
279297
})
280298

281299
t.test('writes to cache with default options', t => {

‎test/disposer.js

-142
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.