Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: npm/cacache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v16.0.6
Choose a base ref
...
head repository: npm/cacache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v16.0.7
Choose a head ref
  • 7 commits
  • 33 files changed
  • 3 contributors

Commits on Apr 27, 2022

  1. fix(put): don't flush if an error happened

    What happens now is that the index is written even if there was an error
    in the content. We don't want this.
    wraithgar committed Apr 27, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    wmouchere William Mouchère
    Copy the full SHA
    e870016 View commit details
  2. fix: remove fs.copyFile checks

    This exists in all of the node versions this module supports
    wraithgar committed Apr 27, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    wmouchere William Mouchère
    Copy the full SHA
    90776fd View commit details
  3. chore: move to tap.testdir, remove benchmarks

    Removes these devDependencies
    
    * benchmark
    * chalk
    * tacks
    wraithgar committed Apr 27, 2022
    Copy the full SHA
    a359d25 View commit details
  4. Copy the full SHA
    bd2b7a3 View commit details
  5. Copy the full SHA
    e77658c View commit details
  6. fix: remove disposer

    promises have finally. actually
    wraithgar committed Apr 27, 2022
    Copy the full SHA
    76ab648 View commit details
  7. chore(main): release 16.0.7 (#105)

    Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
    github-actions[bot] authored Apr 27, 2022
    Copy the full SHA
    283e815 View commit details
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

### [16.0.7](https://github.com/npm/cacache/compare/v16.0.6...v16.0.7) (2022-04-27)


### Bug Fixes

* **put:** don't flush if an error happened ([e870016](https://github.com/npm/cacache/commit/e8700167e036f392e5554af2d582caa17e4e7237))
* remove disposer ([76ab648](https://github.com/npm/cacache/commit/76ab64857b6874bc54d542ddd483c526434c0b9b))
* remove fs.copyFile checks ([90776fd](https://github.com/npm/cacache/commit/90776fd4a6c5362ea56a979b9611bdf4391e1fd8))

### [16.0.6](https://github.com/npm/cacache/compare/v16.0.5...v16.0.6) (2022-04-21)


9 changes: 3 additions & 6 deletions lib/content/read.js
Original file line number Diff line number Diff line change
@@ -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) => {
65 changes: 30 additions & 35 deletions lib/content/write.js
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 6 additions & 2 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
@@ -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
49 changes: 14 additions & 35 deletions lib/get.js
Original file line number Diff line number Diff line change
@@ -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

26 changes: 13 additions & 13 deletions lib/put.js
Original file line number Diff line number Diff line change
@@ -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,28 +59,27 @@ function putStream (cache, key, opts = {}) {
.on('size', (s) => {
size = s
})
.on('error', (err) => {
error = err
})

pipeline.push(contentStream)

// last but not least, we write the index and emit hash and size,
// 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)
}
})
})
}
},
}))

31 changes: 0 additions & 31 deletions lib/util/disposer.js

This file was deleted.

7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Loading