cacache 16.0.4 → 16.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ const Pipeline = require('minipass-pipeline')
10
10
 
11
11
  const lstat = util.promisify(fs.lstat)
12
12
  const readFile = util.promisify(fs.readFile)
13
+ const copyFile = util.promisify(fs.copyFile)
13
14
 
14
15
  module.exports = read
15
16
 
@@ -90,12 +91,8 @@ function readStream (cache, integrity, opts = {}) {
90
91
  return stream
91
92
  }
92
93
 
93
- let copyFile
94
- if (fs.copyFile) {
95
- module.exports.copy = copy
96
- module.exports.copy.sync = copySync
97
- copyFile = util.promisify(fs.copyFile)
98
- }
94
+ module.exports.copy = copy
95
+ module.exports.copy.sync = copySync
99
96
 
100
97
  function copy (cache, integrity, dest) {
101
98
  return withContentSri(cache, integrity, (cpath, sri) => {
@@ -13,34 +13,37 @@ const path = require('path')
13
13
  const rimraf = util.promisify(require('rimraf'))
14
14
  const ssri = require('ssri')
15
15
  const uniqueFilename = require('unique-filename')
16
- const { disposer } = require('./../util/disposer')
17
16
  const fsm = require('fs-minipass')
18
17
 
19
18
  const writeFile = util.promisify(fs.writeFile)
20
19
 
21
20
  module.exports = write
22
21
 
23
- function write (cache, data, opts = {}) {
22
+ async function write (cache, data, opts = {}) {
24
23
  const { algorithms, size, integrity } = opts
25
24
  if (algorithms && algorithms.length > 1) {
26
25
  throw new Error('opts.algorithms only supports a single algorithm for now')
27
26
  }
28
27
 
29
28
  if (typeof size === 'number' && data.length !== size) {
30
- return Promise.reject(sizeError(size, data.length))
29
+ throw sizeError(size, data.length)
31
30
  }
32
31
 
33
32
  const sri = ssri.fromData(data, algorithms ? { algorithms } : {})
34
33
  if (integrity && !ssri.checkData(data, integrity, opts)) {
35
- return Promise.reject(checksumError(integrity, sri))
34
+ throw checksumError(integrity, sri)
36
35
  }
37
36
 
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
+ }
44
47
  }
45
48
 
46
49
  module.exports.stream = writeStream
@@ -94,18 +97,22 @@ function writeStream (cache, opts = {}) {
94
97
  return new CacacheWriteStream(cache, opts)
95
98
  }
96
99
 
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
+ }
109
116
  }
110
117
 
111
118
  function pipeToTmp (inputStream, cache, tmpTarget, opts) {
@@ -136,11 +143,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts) {
136
143
  outStream
137
144
  )
138
145
 
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 }))
144
147
  }
145
148
 
146
149
  function makeTmp (cache, opts) {
@@ -151,14 +154,6 @@ function makeTmp (cache, opts) {
151
154
  }))
152
155
  }
153
156
 
154
- function makeTmpDisposer (tmp) {
155
- if (tmp.moved) {
156
- return Promise.resolve()
157
- }
158
-
159
- return rimraf(tmp.target)
160
- }
161
-
162
157
  function moveToDestination (tmp, cache, sri, opts) {
163
158
  const destination = contentPath(cache, sri)
164
159
  const destDir = path.dirname(destination)
@@ -8,7 +8,6 @@ const path = require('path')
8
8
  const ssri = require('ssri')
9
9
  const uniqueFilename = require('unique-filename')
10
10
 
11
- const { disposer } = require('./util/disposer')
12
11
  const contentPath = require('./content/path')
13
12
  const fixOwner = require('./util/fix-owner')
14
13
  const hashToSegments = require('./util/hash-to-segments')
@@ -102,7 +101,12 @@ async function compact (cache, key, matchFn, opts = {}) {
102
101
  }
103
102
 
104
103
  // 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
+ }
106
110
 
107
111
  // we reverse the list we generated such that the newest
108
112
  // entries come first in order to make looping through them easier
package/lib/get.js CHANGED
@@ -3,15 +3,11 @@
3
3
  const Collect = require('minipass-collect')
4
4
  const Minipass = require('minipass')
5
5
  const Pipeline = require('minipass-pipeline')
6
- const fs = require('fs')
7
- const util = require('util')
8
6
 
9
7
  const index = require('./entry-index')
10
8
  const memo = require('./memoization')
11
9
  const read = require('./content/read')
12
10
 
13
- const writeFile = util.promisify(fs.writeFile)
14
-
15
11
  function getData (cache, key, opts = {}) {
16
12
  const { integrity, memoize, size } = opts
17
13
  const memoized = memo.get(cache, key, opts)
@@ -209,42 +205,25 @@ function info (cache, key, opts = {}) {
209
205
  module.exports.info = info
210
206
 
211
207
  function copy (cache, key, dest, opts = {}) {
212
- if (read.copy) {
213
- return index.find(cache, key, opts).then((entry) => {
214
- if (!entry) {
215
- throw new index.NotFoundError(cache, key)
216
- }
217
- return read.copy(cache, entry.integrity, dest, opts)
218
- .then(() => {
219
- return {
220
- metadata: entry.metadata,
221
- size: entry.size,
222
- integrity: entry.integrity,
223
- }
224
- })
225
- })
226
- }
227
-
228
- return getData(cache, key, opts).then((res) => {
229
- return writeFile(dest, res.data).then(() => {
230
- return {
231
- metadata: res.metadata,
232
- size: res.size,
233
- integrity: res.integrity,
234
- }
235
- })
208
+ return index.find(cache, key, opts).then((entry) => {
209
+ if (!entry) {
210
+ throw new index.NotFoundError(cache, key)
211
+ }
212
+ return read.copy(cache, entry.integrity, dest, opts)
213
+ .then(() => {
214
+ return {
215
+ metadata: entry.metadata,
216
+ size: entry.size,
217
+ integrity: entry.integrity,
218
+ }
219
+ })
236
220
  })
237
221
  }
222
+
238
223
  module.exports.copy = copy
239
224
 
240
225
  function copyByDigest (cache, key, dest, opts = {}) {
241
- if (read.copy) {
242
- return read.copy(cache, key, dest, opts).then(() => key)
243
- }
244
-
245
- return getDataByDigest(cache, key, opts).then((res) => {
246
- return writeFile(dest, res).then(() => key)
247
- })
226
+ return read.copy(cache, key, dest, opts).then(() => key)
248
227
  }
249
228
  module.exports.copy.byDigest = copyByDigest
250
229
 
package/lib/put.js CHANGED
@@ -37,6 +37,7 @@ function putStream (cache, key, opts = {}) {
37
37
  opts = putOpts(opts)
38
38
  let integrity
39
39
  let size
40
+ let error
40
41
 
41
42
  let memoData
42
43
  const pipeline = new Pipeline()
@@ -58,6 +59,9 @@ function putStream (cache, key, opts = {}) {
58
59
  .on('size', (s) => {
59
60
  size = s
60
61
  })
62
+ .on('error', (err) => {
63
+ error = err
64
+ })
61
65
 
62
66
  pipeline.push(contentStream)
63
67
 
@@ -65,21 +69,17 @@ function putStream (cache, key, opts = {}) {
65
69
  // and memoize if we're doing that
66
70
  pipeline.push(new Flush({
67
71
  flush () {
68
- return index
69
- .insert(cache, key, integrity, { ...opts, size })
70
- .then((entry) => {
71
- if (memoize && memoData) {
72
- memo.put(cache, entry, memoData, opts)
73
- }
74
-
75
- if (integrity) {
72
+ if (!error) {
73
+ return index
74
+ .insert(cache, key, integrity, { ...opts, size })
75
+ .then((entry) => {
76
+ if (memoize && memoData) {
77
+ memo.put(cache, entry, memoData, opts)
78
+ }
76
79
  pipeline.emit('integrity', integrity)
77
- }
78
-
79
- if (size) {
80
80
  pipeline.emit('size', size)
81
- }
82
- })
81
+ })
82
+ }
83
83
  },
84
84
  }))
85
85
 
package/lib/verify.js CHANGED
@@ -13,6 +13,8 @@ const path = require('path')
13
13
  const rimraf = util.promisify(require('rimraf'))
14
14
  const ssri = require('ssri')
15
15
 
16
+ const globify = pattern => pattern.split('\\').join('/')
17
+
16
18
  const hasOwnProperty = (obj, key) =>
17
19
  Object.prototype.hasOwnProperty.call(obj, key)
18
20
 
@@ -119,7 +121,7 @@ function garbageCollect (cache, opts) {
119
121
  indexStream.on('end', resolve).on('error', reject)
120
122
  }).then(() => {
121
123
  const contentDir = contentPath.contentDir(cache)
122
- return glob(path.join(contentDir, '**'), {
124
+ return glob(globify(path.join(contentDir, '**')), {
123
125
  follow: false,
124
126
  nodir: true,
125
127
  nosort: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cacache",
3
- "version": "16.0.4",
3
+ "version": "16.0.7",
4
4
  "cache-version": {
5
5
  "content": "2",
6
6
  "index": "5"
@@ -12,7 +12,6 @@
12
12
  "lib/"
13
13
  ],
14
14
  "scripts": {
15
- "benchmarks": "node test/benchmarks",
16
15
  "preversion": "npm test",
17
16
  "postversion": "npm publish",
18
17
  "prepublishOnly": "git push origin --follow-tags",
@@ -53,7 +52,7 @@
53
52
  "@npmcli/move-file": "^2.0.0",
54
53
  "chownr": "^2.0.0",
55
54
  "fs-minipass": "^2.1.0",
56
- "glob": "^7.2.0",
55
+ "glob": "^8.0.1",
57
56
  "infer-owner": "^1.0.4",
58
57
  "lru-cache": "^7.7.1",
59
58
  "minipass": "^3.1.6",
@@ -70,11 +69,7 @@
70
69
  },
71
70
  "devDependencies": {
72
71
  "@npmcli/eslint-config": "^3.0.1",
73
- "@npmcli/template-oss": "3.2.2",
74
- "benchmark": "^2.1.4",
75
- "chalk": "^4.1.2",
76
- "require-inject": "^1.4.4",
77
- "tacks": "^1.3.0",
72
+ "@npmcli/template-oss": "3.4.1",
78
73
  "tap": "^16.0.0"
79
74
  },
80
75
  "tap": {
@@ -87,7 +82,7 @@
87
82
  "templateOSS": {
88
83
  "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
89
84
  "windowsCI": false,
90
- "version": "3.2.2"
85
+ "version": "3.4.1"
91
86
  },
92
87
  "author": "GitHub Inc."
93
88
  }
@@ -1,31 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports.disposer = disposer
4
-
5
- function disposer (creatorFn, disposerFn, fn) {
6
- const runDisposer = (resource, result, shouldThrow = false) => {
7
- return disposerFn(resource)
8
- .then(
9
- // disposer resolved, do something with original fn's promise
10
- () => {
11
- if (shouldThrow) {
12
- throw result
13
- }
14
-
15
- return result
16
- },
17
- // Disposer fn failed, crash process
18
- (err) => {
19
- throw err
20
- // Or process.exit?
21
- })
22
- }
23
-
24
- return creatorFn
25
- .then((resource) => {
26
- // fn(resource) can throw, so wrap in a promise here
27
- return Promise.resolve().then(() => fn(resource))
28
- .then((result) => runDisposer(resource, result))
29
- .catch((err) => runDisposer(resource, err, true))
30
- })
31
- }