cacache
Advanced tools
Comparing version 16.0.6 to 16.0.7
@@ -13,2 +13,3 @@ 'use strict' | ||
const readFile = util.promisify(fs.readFile) | ||
const copyFile = util.promisify(fs.copyFile) | ||
@@ -94,8 +95,4 @@ module.exports = read | ||
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 | ||
@@ -102,0 +99,0 @@ function copy (cache, integrity, dest) { |
@@ -16,3 +16,2 @@ 'use strict' | ||
const uniqueFilename = require('unique-filename') | ||
const { disposer } = require('./../util/disposer') | ||
const fsm = require('fs-minipass') | ||
@@ -24,3 +23,3 @@ | ||
function write (cache, data, opts = {}) { | ||
async function write (cache, data, opts = {}) { | ||
const { algorithms, size, integrity } = opts | ||
@@ -32,3 +31,3 @@ if (algorithms && algorithms.length > 1) { | ||
if (typeof size === 'number' && data.length !== size) { | ||
return Promise.reject(sizeError(size, data.length)) | ||
throw sizeError(size, data.length) | ||
} | ||
@@ -38,11 +37,15 @@ | ||
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) | ||
} | ||
} | ||
} | ||
@@ -101,14 +104,18 @@ | ||
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) | ||
} | ||
} | ||
} | ||
@@ -144,7 +151,3 @@ | ||
return pipeline.promise() | ||
.then(() => ({ integrity, size })) | ||
.catch(er => rimraf(tmpTarget).then(() => { | ||
throw er | ||
})) | ||
return pipeline.promise().then(() => ({ integrity, size })) | ||
} | ||
@@ -160,10 +163,2 @@ | ||
function makeTmpDisposer (tmp) { | ||
if (tmp.moved) { | ||
return Promise.resolve() | ||
} | ||
return rimraf(tmp.target) | ||
} | ||
function moveToDestination (tmp, cache, sri, opts) { | ||
@@ -170,0 +165,0 @@ const destination = contentPath(cache, sri) |
@@ -11,3 +11,2 @@ 'use strict' | ||
const { disposer } = require('./util/disposer') | ||
const contentPath = require('./content/path') | ||
@@ -106,3 +105,8 @@ const fixOwner = require('./util/fix-owner') | ||
// write the file atomically | ||
await disposer(setup(), teardown, write) | ||
const tmp = await setup() | ||
try { | ||
await write(tmp) | ||
} finally { | ||
await teardown(tmp) | ||
} | ||
@@ -109,0 +113,0 @@ // we reverse the list we generated such that the newest |
@@ -6,4 +6,2 @@ 'use strict' | ||
const Pipeline = require('minipass-pipeline') | ||
const fs = require('fs') | ||
const util = require('util') | ||
@@ -14,4 +12,2 @@ const index = require('./entry-index') | ||
const writeFile = util.promisify(fs.writeFile) | ||
function getData (cache, key, opts = {}) { | ||
@@ -214,38 +210,21 @@ const { integrity, memoize, size } = opts | ||
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) | ||
} | ||
@@ -252,0 +231,0 @@ module.exports.copy.byDigest = copyByDigest |
@@ -40,2 +40,3 @@ 'use strict' | ||
let size | ||
let error | ||
@@ -62,2 +63,5 @@ let memoData | ||
}) | ||
.on('error', (err) => { | ||
error = err | ||
}) | ||
@@ -70,17 +74,13 @@ pipeline.push(contentStream) | ||
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) | ||
} | ||
}) | ||
}) | ||
} | ||
}, | ||
@@ -87,0 +87,0 @@ })) |
{ | ||
"name": "cacache", | ||
"version": "16.0.6", | ||
"version": "16.0.7", | ||
"cache-version": { | ||
@@ -15,3 +15,2 @@ "content": "2", | ||
"scripts": { | ||
"benchmarks": "node test/benchmarks", | ||
"preversion": "npm test", | ||
@@ -75,6 +74,2 @@ "postversion": "npm publish", | ||
"@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" | ||
@@ -81,0 +76,0 @@ }, |
3
8
75549
18
1670