diff --git a/node_modules/cacache/lib/content/read.js b/node_modules/cacache/lib/content/read.js index 8bffb2af83cab..0aeb454725bd0 100644 --- a/node_modules/cacache/lib/content/read.js +++ b/node_modules/cacache/lib/content/read.js @@ -10,6 +10,7 @@ const Pipeline = require('minipass-pipeline') const lstat = util.promisify(fs.lstat) const readFile = util.promisify(fs.readFile) +const copyFile = util.promisify(fs.copyFile) module.exports = read @@ -90,12 +91,8 @@ function readStream (cache, integrity, opts = {}) { return stream } -let copyFile -if (fs.copyFile) { - module.exports.copy = copy - module.exports.copy.sync = copySync - copyFile = util.promisify(fs.copyFile) -} +module.exports.copy = copy +module.exports.copy.sync = copySync function copy (cache, integrity, dest) { return withContentSri(cache, integrity, (cpath, sri) => { diff --git a/node_modules/cacache/lib/content/write.js b/node_modules/cacache/lib/content/write.js index a71e81ad5e150..b0aa18c12ba78 100644 --- a/node_modules/cacache/lib/content/write.js +++ b/node_modules/cacache/lib/content/write.js @@ -13,34 +13,37 @@ const path = require('path') const rimraf = util.promisify(require('rimraf')) const ssri = require('ssri') const uniqueFilename = require('unique-filename') -const { disposer } = require('./../util/disposer') const fsm = require('fs-minipass') const writeFile = util.promisify(fs.writeFile) module.exports = write -function write (cache, data, opts = {}) { +async function write (cache, data, opts = {}) { const { algorithms, size, integrity } = opts if (algorithms && algorithms.length > 1) { throw new Error('opts.algorithms only supports a single algorithm for now') } if (typeof size === 'number' && data.length !== size) { - return Promise.reject(sizeError(size, data.length)) + throw sizeError(size, data.length) } const sri = ssri.fromData(data, algorithms ? { algorithms } : {}) if (integrity && !ssri.checkData(data, integrity, opts)) { - return Promise.reject(checksumError(integrity, sri)) + throw checksumError(integrity, sri) } - return disposer(makeTmp(cache, opts), makeTmpDisposer, - (tmp) => { - return writeFile(tmp.target, data, { flag: 'wx' }) - .then(() => moveToDestination(tmp, cache, sri, opts)) - }) - .then(() => ({ integrity: sri, size: data.length })) + const tmp = await makeTmp(cache, opts) + try { + await writeFile(tmp.target, data, { flag: 'wx' }) + await moveToDestination(tmp, cache, sri, opts) + return { integrity: sri, size: data.length } + } finally { + if (!tmp.moved) { + await rimraf(tmp.target) + } + } } module.exports.stream = writeStream @@ -94,18 +97,22 @@ function writeStream (cache, opts = {}) { return new CacacheWriteStream(cache, opts) } -function handleContent (inputStream, cache, opts) { - return disposer(makeTmp(cache, opts), makeTmpDisposer, (tmp) => { - return pipeToTmp(inputStream, cache, tmp.target, opts) - .then((res) => { - return moveToDestination( - tmp, - cache, - res.integrity, - opts - ).then(() => res) - }) - }) +async function handleContent (inputStream, cache, opts) { + const tmp = await makeTmp(cache, opts) + try { + const res = await pipeToTmp(inputStream, cache, tmp.target, opts) + await moveToDestination( + tmp, + cache, + res.integrity, + opts + ) + return res + } finally { + if (!tmp.moved) { + await rimraf(tmp.target) + } + } } function pipeToTmp (inputStream, cache, tmpTarget, opts) { @@ -136,11 +143,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts) { outStream ) - return pipeline.promise() - .then(() => ({ integrity, size })) - .catch(er => rimraf(tmpTarget).then(() => { - throw er - })) + return pipeline.promise().then(() => ({ integrity, size })) } function makeTmp (cache, opts) { @@ -151,14 +154,6 @@ function makeTmp (cache, opts) { })) } -function makeTmpDisposer (tmp) { - if (tmp.moved) { - return Promise.resolve() - } - - return rimraf(tmp.target) -} - function moveToDestination (tmp, cache, sri, opts) { const destination = contentPath(cache, sri) const destDir = path.dirname(destination) diff --git a/node_modules/cacache/lib/entry-index.js b/node_modules/cacache/lib/entry-index.js index 426778b850963..9d4485624acbc 100644 --- a/node_modules/cacache/lib/entry-index.js +++ b/node_modules/cacache/lib/entry-index.js @@ -8,7 +8,6 @@ const path = require('path') const ssri = require('ssri') const uniqueFilename = require('unique-filename') -const { disposer } = require('./util/disposer') const contentPath = require('./content/path') const fixOwner = require('./util/fix-owner') const hashToSegments = require('./util/hash-to-segments') @@ -102,7 +101,12 @@ async function compact (cache, key, matchFn, opts = {}) { } // write the file atomically - await disposer(setup(), teardown, write) + const tmp = await setup() + try { + await write(tmp) + } finally { + await teardown(tmp) + } // we reverse the list we generated such that the newest // entries come first in order to make looping through them easier diff --git a/node_modules/cacache/lib/get.js b/node_modules/cacache/lib/get.js index d9d4bf4c6416f..58f357b1da3d1 100644 --- a/node_modules/cacache/lib/get.js +++ b/node_modules/cacache/lib/get.js @@ -3,15 +3,11 @@ const Collect = require('minipass-collect') const Minipass = require('minipass') const Pipeline = require('minipass-pipeline') -const fs = require('fs') -const util = require('util') const index = require('./entry-index') const memo = require('./memoization') const read = require('./content/read') -const writeFile = util.promisify(fs.writeFile) - function getData (cache, key, opts = {}) { const { integrity, memoize, size } = opts const memoized = memo.get(cache, key, opts) @@ -209,42 +205,25 @@ function info (cache, key, opts = {}) { module.exports.info = info function copy (cache, key, dest, opts = {}) { - if (read.copy) { - return index.find(cache, key, opts).then((entry) => { - if (!entry) { - throw new index.NotFoundError(cache, key) - } - return read.copy(cache, entry.integrity, dest, opts) - .then(() => { - return { - metadata: entry.metadata, - size: entry.size, - integrity: entry.integrity, - } - }) - }) - } - - return getData(cache, key, opts).then((res) => { - return writeFile(dest, res.data).then(() => { - return { - metadata: res.metadata, - size: res.size, - integrity: res.integrity, - } - }) + return index.find(cache, key, opts).then((entry) => { + if (!entry) { + throw new index.NotFoundError(cache, key) + } + return read.copy(cache, entry.integrity, dest, opts) + .then(() => { + return { + metadata: entry.metadata, + size: entry.size, + integrity: entry.integrity, + } + }) }) } + module.exports.copy = copy function copyByDigest (cache, key, dest, opts = {}) { - if (read.copy) { - return read.copy(cache, key, dest, opts).then(() => key) - } - - return getDataByDigest(cache, key, opts).then((res) => { - return writeFile(dest, res).then(() => key) - }) + return read.copy(cache, key, dest, opts).then(() => key) } module.exports.copy.byDigest = copyByDigest diff --git a/node_modules/cacache/lib/put.js b/node_modules/cacache/lib/put.js index d6904fa301272..eed51874f9f64 100644 --- a/node_modules/cacache/lib/put.js +++ b/node_modules/cacache/lib/put.js @@ -37,6 +37,7 @@ function putStream (cache, key, opts = {}) { opts = putOpts(opts) let integrity let size + let error let memoData const pipeline = new Pipeline() @@ -58,6 +59,9 @@ function putStream (cache, key, opts = {}) { .on('size', (s) => { size = s }) + .on('error', (err) => { + error = err + }) pipeline.push(contentStream) @@ -65,21 +69,17 @@ function putStream (cache, key, opts = {}) { // 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 (integrity) { + if (!error) { + return index + .insert(cache, key, integrity, { ...opts, size }) + .then((entry) => { + if (memoize && memoData) { + memo.put(cache, entry, memoData, opts) + } pipeline.emit('integrity', integrity) - } - - if (size) { pipeline.emit('size', size) - } - }) + }) + } }, })) diff --git a/node_modules/cacache/lib/util/disposer.js b/node_modules/cacache/lib/util/disposer.js deleted file mode 100644 index 52d7d3edda7d5..0000000000000 --- a/node_modules/cacache/lib/util/disposer.js +++ /dev/null @@ -1,31 +0,0 @@ -'use strict' - -module.exports.disposer = disposer - -function disposer (creatorFn, disposerFn, fn) { - const runDisposer = (resource, result, shouldThrow = false) => { - return disposerFn(resource) - .then( - // disposer resolved, do something with original fn's promise - () => { - if (shouldThrow) { - throw result - } - - return result - }, - // Disposer fn failed, crash process - (err) => { - throw err - // Or process.exit? - }) - } - - return creatorFn - .then((resource) => { - // fn(resource) can throw, so wrap in a promise here - return Promise.resolve().then(() => fn(resource)) - .then((result) => runDisposer(resource, result)) - .catch((err) => runDisposer(resource, err, true)) - }) -} diff --git a/node_modules/cacache/package.json b/node_modules/cacache/package.json index 9eb646df76b40..cd7e4de5e0cba 100644 --- a/node_modules/cacache/package.json +++ b/node_modules/cacache/package.json @@ -1,6 +1,6 @@ { "name": "cacache", - "version": "16.0.6", + "version": "16.0.7", "cache-version": { "content": "2", "index": "5" @@ -12,7 +12,6 @@ "lib/" ], "scripts": { - "benchmarks": "node test/benchmarks", "preversion": "npm test", "postversion": "npm publish", "prepublishOnly": "git push origin --follow-tags", @@ -71,10 +70,6 @@ "devDependencies": { "@npmcli/eslint-config": "^3.0.1", "@npmcli/template-oss": "3.4.1", - "benchmark": "^2.1.4", - "chalk": "^4.1.2", - "require-inject": "^1.4.4", - "tacks": "^1.3.0", "tap": "^16.0.0" }, "tap": { diff --git a/package-lock.json b/package-lock.json index 89f3515d7b3bb..f184e3a58a22d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -96,7 +96,7 @@ "@npmcli/run-script": "^3.0.1", "abbrev": "~1.1.1", "archy": "~1.0.0", - "cacache": "^16.0.6", + "cacache": "^16.0.7", "chalk": "^4.1.2", "chownr": "^2.0.0", "cli-columns": "^4.0.0", @@ -1597,9 +1597,9 @@ } }, "node_modules/cacache": { - "version": "16.0.6", - "resolved": "https://registry.npmjs.org/cacache/-/cacache-16.0.6.tgz", - "integrity": "sha512-9a/MLxGaw3LEGes0HaPez2RgZWDV6X0jrgChsuxfEh8xoDoYGxaGrkMe7Dlyjrb655tA/b8fX0qlUg6Ii5MBvw==", + "version": "16.0.7", + "resolved": "https://registry.npmjs.org/cacache/-/cacache-16.0.7.tgz", + "integrity": "sha512-a4zfQpp5vm4Ipdvbj+ZrPonikRhm6WBEd4zT1Yc1DXsmAxrPgDwWBLF/u/wTVXSFPIgOJ1U3ghSa2Xm4s3h28w==", "inBundle": true, "dependencies": { "@npmcli/fs": "^2.1.0", @@ -11253,9 +11253,9 @@ } }, "cacache": { - "version": "16.0.6", - "resolved": "https://registry.npmjs.org/cacache/-/cacache-16.0.6.tgz", - "integrity": "sha512-9a/MLxGaw3LEGes0HaPez2RgZWDV6X0jrgChsuxfEh8xoDoYGxaGrkMe7Dlyjrb655tA/b8fX0qlUg6Ii5MBvw==", + "version": "16.0.7", + "resolved": "https://registry.npmjs.org/cacache/-/cacache-16.0.7.tgz", + "integrity": "sha512-a4zfQpp5vm4Ipdvbj+ZrPonikRhm6WBEd4zT1Yc1DXsmAxrPgDwWBLF/u/wTVXSFPIgOJ1U3ghSa2Xm4s3h28w==", "requires": { "@npmcli/fs": "^2.1.0", "@npmcli/move-file": "^2.0.0", diff --git a/package.json b/package.json index 14c1ceb6963dc..90090e758cf23 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "@npmcli/run-script": "^3.0.1", "abbrev": "~1.1.1", "archy": "~1.0.0", - "cacache": "^16.0.6", + "cacache": "^16.0.7", "chalk": "^4.1.2", "chownr": "^2.0.0", "cli-columns": "^4.0.0",