cacache 15.2.0 → 16.0.1

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.
@@ -20,15 +20,18 @@ function read (cache, integrity, opts = {}) {
20
20
  // get size
21
21
  return lstat(cpath).then(stat => ({ stat, cpath, sri }))
22
22
  }).then(({ stat, cpath, sri }) => {
23
- if (typeof size === 'number' && stat.size !== size)
23
+ if (typeof size === 'number' && stat.size !== size) {
24
24
  throw sizeError(size, stat.size)
25
+ }
25
26
 
26
- if (stat.size > MAX_SINGLE_READ_SIZE)
27
+ if (stat.size > MAX_SINGLE_READ_SIZE) {
27
28
  return readPipeline(cpath, stat.size, sri, new Pipeline()).concat()
29
+ }
28
30
 
29
31
  return readFile(cpath, null).then((data) => {
30
- if (!ssri.checkData(data, sri))
32
+ if (!ssri.checkData(data, sri)) {
31
33
  throw integrityError(sri, cpath)
34
+ }
32
35
 
33
36
  return data
34
37
  })
@@ -55,11 +58,13 @@ function readSync (cache, integrity, opts = {}) {
55
58
  const { size } = opts
56
59
  return withContentSriSync(cache, integrity, (cpath, sri) => {
57
60
  const data = fs.readFileSync(cpath)
58
- if (typeof size === 'number' && size !== data.length)
61
+ if (typeof size === 'number' && size !== data.length) {
59
62
  throw sizeError(size, data.length)
63
+ }
60
64
 
61
- if (ssri.checkData(data, sri))
65
+ if (ssri.checkData(data, sri)) {
62
66
  return data
67
+ }
63
68
 
64
69
  throw integrityError(sri, cpath)
65
70
  })
@@ -75,8 +80,9 @@ function readStream (cache, integrity, opts = {}) {
75
80
  // just lstat to ensure it exists
76
81
  return lstat(cpath).then((stat) => ({ stat, cpath, sri }))
77
82
  }).then(({ stat, cpath, sri }) => {
78
- if (typeof size === 'number' && size !== stat.size)
83
+ if (typeof size === 'number' && size !== stat.size) {
79
84
  return stream.emit('error', sizeError(size, stat.size))
85
+ }
80
86
 
81
87
  readPipeline(cpath, stat.size, sri, stream)
82
88
  }, er => stream.emit('error', er))
@@ -106,21 +112,24 @@ function copySync (cache, integrity, dest) {
106
112
  module.exports.hasContent = hasContent
107
113
 
108
114
  function hasContent (cache, integrity) {
109
- if (!integrity)
115
+ if (!integrity) {
110
116
  return Promise.resolve(false)
117
+ }
111
118
 
112
119
  return withContentSri(cache, integrity, (cpath, sri) => {
113
120
  return lstat(cpath).then((stat) => ({ size: stat.size, sri, stat }))
114
121
  }).catch((err) => {
115
- if (err.code === 'ENOENT')
122
+ if (err.code === 'ENOENT') {
116
123
  return false
124
+ }
117
125
 
118
126
  if (err.code === 'EPERM') {
119
127
  /* istanbul ignore else */
120
- if (process.platform !== 'win32')
128
+ if (process.platform !== 'win32') {
121
129
  throw err
122
- else
130
+ } else {
123
131
  return false
132
+ }
124
133
  }
125
134
  })
126
135
  }
@@ -128,23 +137,26 @@ function hasContent (cache, integrity) {
128
137
  module.exports.hasContent.sync = hasContentSync
129
138
 
130
139
  function hasContentSync (cache, integrity) {
131
- if (!integrity)
140
+ if (!integrity) {
132
141
  return false
142
+ }
133
143
 
134
144
  return withContentSriSync(cache, integrity, (cpath, sri) => {
135
145
  try {
136
146
  const stat = fs.lstatSync(cpath)
137
147
  return { size: stat.size, sri, stat }
138
148
  } catch (err) {
139
- if (err.code === 'ENOENT')
149
+ if (err.code === 'ENOENT') {
140
150
  return false
151
+ }
141
152
 
142
153
  if (err.code === 'EPERM') {
143
154
  /* istanbul ignore else */
144
- if (process.platform !== 'win32')
155
+ if (process.platform !== 'win32') {
145
156
  throw err
146
- else
157
+ } else {
147
158
  return false
159
+ }
148
160
  }
149
161
  }
150
162
  })
@@ -180,13 +192,15 @@ function withContentSri (cache, integrity, fn) {
180
192
  .then((results) => {
181
193
  // Return the first non error if it is found
182
194
  const result = results.find((r) => !(r instanceof Error))
183
- if (result)
195
+ if (result) {
184
196
  return result
197
+ }
185
198
 
186
199
  // Throw the No matching content found error
187
200
  const enoentError = results.find((r) => r.code === 'ENOENT')
188
- if (enoentError)
201
+ if (enoentError) {
189
202
  throw enoentError
203
+ }
190
204
 
191
205
  // Throw generic error
192
206
  throw results.find((r) => r instanceof Error)
@@ -228,6 +242,7 @@ function withContentSriSync (cache, integrity, fn) {
228
242
  }
229
243
 
230
244
  function sizeError (expected, found) {
245
+ /* eslint-disable-next-line max-len */
231
246
  const err = new Error(`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
232
247
  err.expected = expected
233
248
  err.found = found
package/lib/content/rm.js CHANGED
@@ -11,9 +11,10 @@ module.exports = rm
11
11
  function rm (cache, integrity) {
12
12
  return hasContent(cache, integrity).then((content) => {
13
13
  // ~pretty~ sure we can't end up with a content lacking sri, but be safe
14
- if (content && content.sri)
14
+ if (content && content.sri) {
15
15
  return rimraf(contentPath(cache, content.sri)).then(() => true)
16
- else
16
+ } else {
17
17
  return false
18
+ }
18
19
  })
19
20
  }
@@ -22,15 +22,18 @@ module.exports = write
22
22
 
23
23
  function write (cache, data, opts = {}) {
24
24
  const { algorithms, size, integrity } = opts
25
- if (algorithms && algorithms.length > 1)
25
+ if (algorithms && algorithms.length > 1) {
26
26
  throw new Error('opts.algorithms only supports a single algorithm for now')
27
+ }
27
28
 
28
- if (typeof size === 'number' && data.length !== size)
29
+ if (typeof size === 'number' && data.length !== size) {
29
30
  return Promise.reject(sizeError(size, data.length))
31
+ }
30
32
 
31
33
  const sri = ssri.fromData(data, algorithms ? { algorithms } : {})
32
- if (integrity && !ssri.checkData(data, integrity, opts))
34
+ if (integrity && !ssri.checkData(data, integrity, opts)) {
33
35
  return Promise.reject(checksumError(integrity, sri))
36
+ }
34
37
 
35
38
  return disposer(makeTmp(cache, opts), makeTmpDisposer,
36
39
  (tmp) => {
@@ -149,8 +152,9 @@ function makeTmp (cache, opts) {
149
152
  }
150
153
 
151
154
  function makeTmpDisposer (tmp) {
152
- if (tmp.moved)
155
+ if (tmp.moved) {
153
156
  return Promise.resolve()
157
+ }
154
158
 
155
159
  return rimraf(tmp.target)
156
160
  }
@@ -171,6 +175,7 @@ function moveToDestination (tmp, cache, sri, opts) {
171
175
  }
172
176
 
173
177
  function sizeError (expected, found) {
178
+ /* eslint-disable-next-line max-len */
174
179
  const err = new Error(`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
175
180
  err.expected = expected
176
181
  err.found = found
@@ -49,8 +49,9 @@ async function compact (cache, key, matchFn, opts = {}) {
49
49
  // if the integrity is null and no validateEntry is provided, we break
50
50
  // as we consider the null integrity to be a deletion of everything
51
51
  // that came before it.
52
- if (entry.integrity === null && !opts.validateEntry)
52
+ if (entry.integrity === null && !opts.validateEntry) {
53
53
  break
54
+ }
54
55
 
55
56
  // if this entry is valid, and it is either the first entry or
56
57
  // the newEntries array doesn't already include an entry that
@@ -58,8 +59,9 @@ async function compact (cache, key, matchFn, opts = {}) {
58
59
  // it to the beginning of our list
59
60
  if ((!opts.validateEntry || opts.validateEntry(entry) === true) &&
60
61
  (newEntries.length === 0 ||
61
- !newEntries.find((oldEntry) => matchFn(oldEntry, entry))))
62
+ !newEntries.find((oldEntry) => matchFn(oldEntry, entry)))) {
62
63
  newEntries.unshift(entry)
64
+ }
63
65
  }
64
66
 
65
67
  const newIndex = '\n' + newEntries.map((entry) => {
@@ -78,8 +80,9 @@ async function compact (cache, key, matchFn, opts = {}) {
78
80
  }
79
81
 
80
82
  const teardown = async (tmp) => {
81
- if (!tmp.moved)
83
+ if (!tmp.moved) {
82
84
  return rimraf(tmp.target)
85
+ }
83
86
  }
84
87
 
85
88
  const write = async (tmp) => {
@@ -92,8 +95,9 @@ async function compact (cache, key, matchFn, opts = {}) {
92
95
  try {
93
96
  await fixOwner.chownr(cache, bucket)
94
97
  } catch (err) {
95
- if (err.code !== 'ENOENT')
98
+ if (err.code !== 'ENOENT') {
96
99
  throw err
100
+ }
97
101
  }
98
102
  }
99
103
 
@@ -136,8 +140,9 @@ function insert (cache, key, integrity, opts = {}) {
136
140
  })
137
141
  .then(() => fixOwner.chownr(cache, bucket))
138
142
  .catch((err) => {
139
- if (err.code === 'ENOENT')
143
+ if (err.code === 'ENOENT') {
140
144
  return undefined
145
+ }
141
146
 
142
147
  throw err
143
148
  // There's a class of race conditions that happen when things get deleted
@@ -169,8 +174,9 @@ function insertSync (cache, key, integrity, opts = {}) {
169
174
  try {
170
175
  fixOwner.chownr.sync(cache, bucket)
171
176
  } catch (err) {
172
- if (err.code !== 'ENOENT')
177
+ if (err.code !== 'ENOENT') {
173
178
  throw err
179
+ }
174
180
  }
175
181
  return formatEntry(cache, entry)
176
182
  }
@@ -182,17 +188,19 @@ function find (cache, key) {
182
188
  return bucketEntries(bucket)
183
189
  .then((entries) => {
184
190
  return entries.reduce((latest, next) => {
185
- if (next && next.key === key)
191
+ if (next && next.key === key) {
186
192
  return formatEntry(cache, next)
187
- else
193
+ } else {
188
194
  return latest
195
+ }
189
196
  }, null)
190
197
  })
191
198
  .catch((err) => {
192
- if (err.code === 'ENOENT')
199
+ if (err.code === 'ENOENT') {
193
200
  return null
194
- else
201
+ } else {
195
202
  throw err
203
+ }
196
204
  })
197
205
  }
198
206
 
@@ -202,24 +210,27 @@ function findSync (cache, key) {
202
210
  const bucket = bucketPath(cache, key)
203
211
  try {
204
212
  return bucketEntriesSync(bucket).reduce((latest, next) => {
205
- if (next && next.key === key)
213
+ if (next && next.key === key) {
206
214
  return formatEntry(cache, next)
207
- else
215
+ } else {
208
216
  return latest
217
+ }
209
218
  }, null)
210
219
  } catch (err) {
211
- if (err.code === 'ENOENT')
220
+ if (err.code === 'ENOENT') {
212
221
  return null
213
- else
222
+ } else {
214
223
  throw err
224
+ }
215
225
  }
216
226
  }
217
227
 
218
228
  module.exports.delete = del
219
229
 
220
230
  function del (cache, key, opts = {}) {
221
- if (!opts.removeFully)
231
+ if (!opts.removeFully) {
222
232
  return insert(cache, key, null, opts)
233
+ }
223
234
 
224
235
  const bucket = bucketPath(cache, key)
225
236
  return rimraf(bucket)
@@ -228,8 +239,9 @@ function del (cache, key, opts = {}) {
228
239
  module.exports.delete.sync = delSync
229
240
 
230
241
  function delSync (cache, key, opts = {}) {
231
- if (!opts.removeFully)
242
+ if (!opts.removeFully) {
232
243
  return insertSync(cache, key, null, opts)
244
+ }
233
245
 
234
246
  const bucket = bucketPath(cache, key)
235
247
  return rimraf.sync(bucket)
@@ -263,12 +275,14 @@ function lsStream (cache) {
263
275
  // reduced is a map of key => entry
264
276
  for (const entry of reduced.values()) {
265
277
  const formatted = formatEntry(cache, entry)
266
- if (formatted)
278
+ if (formatted) {
267
279
  stream.write(formatted)
280
+ }
268
281
  }
269
282
  }).catch(err => {
270
- if (err.code === 'ENOENT')
283
+ if (err.code === 'ENOENT') {
271
284
  return undefined
285
+ }
272
286
  throw err
273
287
  })
274
288
  })
@@ -312,8 +326,9 @@ function bucketEntriesSync (bucket, filter) {
312
326
  function _bucketEntries (data, filter) {
313
327
  const entries = []
314
328
  data.split('\n').forEach((entry) => {
315
- if (!entry)
329
+ if (!entry) {
316
330
  return
331
+ }
317
332
 
318
333
  const pieces = entry.split('\t')
319
334
  if (!pieces[1] || hashEntry(pieces[1]) !== pieces[0]) {
@@ -328,8 +343,9 @@ function _bucketEntries (data, filter) {
328
343
  // Entry is corrupted!
329
344
  return
330
345
  }
331
- if (obj)
346
+ if (obj) {
332
347
  entries.push(obj)
348
+ }
333
349
  })
334
350
  return entries
335
351
  }
@@ -371,8 +387,9 @@ function hash (str, digest) {
371
387
 
372
388
  function formatEntry (cache, entry, keepAll) {
373
389
  // Treat null digests as deletions. They'll shadow any previous entries.
374
- if (!entry.integrity && !keepAll)
390
+ if (!entry.integrity && !keepAll) {
375
391
  return null
392
+ }
376
393
 
377
394
  return {
378
395
  key: entry.key,
@@ -386,8 +403,9 @@ function formatEntry (cache, entry, keepAll) {
386
403
 
387
404
  function readdirOrEmpty (dir) {
388
405
  return readdir(dir).catch((err) => {
389
- if (err.code === 'ENOENT' || err.code === 'ENOTDIR')
406
+ if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
390
407
  return []
408
+ }
391
409
 
392
410
  throw err
393
411
  })
package/lib/get.js ADDED
@@ -0,0 +1,251 @@
1
+ 'use strict'
2
+
3
+ const Collect = require('minipass-collect')
4
+ const Minipass = require('minipass')
5
+ const Pipeline = require('minipass-pipeline')
6
+ const fs = require('fs')
7
+ const util = require('util')
8
+
9
+ const index = require('./entry-index')
10
+ const memo = require('./memoization')
11
+ const read = require('./content/read')
12
+
13
+ const writeFile = util.promisify(fs.writeFile)
14
+
15
+ function getData (cache, key, opts = {}) {
16
+ const { integrity, memoize, size } = opts
17
+ const memoized = memo.get(cache, key, opts)
18
+ if (memoized && memoize !== false) {
19
+ return Promise.resolve({
20
+ metadata: memoized.entry.metadata,
21
+ data: memoized.data,
22
+ integrity: memoized.entry.integrity,
23
+ size: memoized.entry.size,
24
+ })
25
+ }
26
+
27
+ return index.find(cache, key, opts).then((entry) => {
28
+ if (!entry) {
29
+ throw new index.NotFoundError(cache, key)
30
+ }
31
+
32
+ return read(cache, entry.integrity, { integrity, size }).then((data) => {
33
+ if (memoize) {
34
+ memo.put(cache, entry, data, opts)
35
+ }
36
+
37
+ return {
38
+ data,
39
+ metadata: entry.metadata,
40
+ size: entry.size,
41
+ integrity: entry.integrity,
42
+ }
43
+ })
44
+ })
45
+ }
46
+ module.exports = getData
47
+
48
+ function getDataByDigest (cache, key, opts = {}) {
49
+ const { integrity, memoize, size } = opts
50
+ const memoized = memo.get.byDigest(cache, key, opts)
51
+ if (memoized && memoize !== false) {
52
+ return Promise.resolve(memoized)
53
+ }
54
+
55
+ return read(cache, key, { integrity, size }).then((res) => {
56
+ if (memoize) {
57
+ memo.put.byDigest(cache, key, res, opts)
58
+ }
59
+ return res
60
+ })
61
+ }
62
+ module.exports.byDigest = getDataByDigest
63
+
64
+ function getDataSync (cache, key, opts = {}) {
65
+ const { integrity, memoize, size } = opts
66
+ const memoized = memo.get(cache, key, opts)
67
+
68
+ if (memoized && memoize !== false) {
69
+ return {
70
+ metadata: memoized.entry.metadata,
71
+ data: memoized.data,
72
+ integrity: memoized.entry.integrity,
73
+ size: memoized.entry.size,
74
+ }
75
+ }
76
+ const entry = index.find.sync(cache, key, opts)
77
+ if (!entry) {
78
+ throw new index.NotFoundError(cache, key)
79
+ }
80
+ const data = read.sync(cache, entry.integrity, {
81
+ integrity: integrity,
82
+ size: size,
83
+ })
84
+ const res = {
85
+ metadata: entry.metadata,
86
+ data: data,
87
+ size: entry.size,
88
+ integrity: entry.integrity,
89
+ }
90
+ if (memoize) {
91
+ memo.put(cache, entry, res.data, opts)
92
+ }
93
+
94
+ return res
95
+ }
96
+
97
+ module.exports.sync = getDataSync
98
+
99
+ function getDataByDigestSync (cache, digest, opts = {}) {
100
+ const { integrity, memoize, size } = opts
101
+ const memoized = memo.get.byDigest(cache, digest, opts)
102
+
103
+ if (memoized && memoize !== false) {
104
+ return memoized
105
+ }
106
+
107
+ const res = read.sync(cache, digest, {
108
+ integrity: integrity,
109
+ size: size,
110
+ })
111
+ if (memoize) {
112
+ memo.put.byDigest(cache, digest, res, opts)
113
+ }
114
+
115
+ return res
116
+ }
117
+ module.exports.sync.byDigest = getDataByDigestSync
118
+
119
+ const getMemoizedStream = (memoized) => {
120
+ const stream = new Minipass()
121
+ stream.on('newListener', function (ev, cb) {
122
+ ev === 'metadata' && cb(memoized.entry.metadata)
123
+ ev === 'integrity' && cb(memoized.entry.integrity)
124
+ ev === 'size' && cb(memoized.entry.size)
125
+ })
126
+ stream.end(memoized.data)
127
+ return stream
128
+ }
129
+
130
+ function getStream (cache, key, opts = {}) {
131
+ const { memoize, size } = opts
132
+ const memoized = memo.get(cache, key, opts)
133
+ if (memoized && memoize !== false) {
134
+ return getMemoizedStream(memoized)
135
+ }
136
+
137
+ const stream = new Pipeline()
138
+ index
139
+ .find(cache, key)
140
+ .then((entry) => {
141
+ if (!entry) {
142
+ throw new index.NotFoundError(cache, key)
143
+ }
144
+
145
+ stream.emit('metadata', entry.metadata)
146
+ stream.emit('integrity', entry.integrity)
147
+ stream.emit('size', entry.size)
148
+ stream.on('newListener', function (ev, cb) {
149
+ ev === 'metadata' && cb(entry.metadata)
150
+ ev === 'integrity' && cb(entry.integrity)
151
+ ev === 'size' && cb(entry.size)
152
+ })
153
+
154
+ const src = read.readStream(
155
+ cache,
156
+ entry.integrity,
157
+ { ...opts, size: typeof size !== 'number' ? entry.size : size }
158
+ )
159
+
160
+ if (memoize) {
161
+ const memoStream = new Collect.PassThrough()
162
+ memoStream.on('collect', data => memo.put(cache, entry, data, opts))
163
+ stream.unshift(memoStream)
164
+ }
165
+ stream.unshift(src)
166
+ })
167
+ .catch((err) => stream.emit('error', err))
168
+
169
+ return stream
170
+ }
171
+
172
+ module.exports.stream = getStream
173
+
174
+ function getStreamDigest (cache, integrity, opts = {}) {
175
+ const { memoize } = opts
176
+ const memoized = memo.get.byDigest(cache, integrity, opts)
177
+ if (memoized && memoize !== false) {
178
+ const stream = new Minipass()
179
+ stream.end(memoized)
180
+ return stream
181
+ } else {
182
+ const stream = read.readStream(cache, integrity, opts)
183
+ if (!memoize) {
184
+ return stream
185
+ }
186
+
187
+ const memoStream = new Collect.PassThrough()
188
+ memoStream.on('collect', data => memo.put.byDigest(
189
+ cache,
190
+ integrity,
191
+ data,
192
+ opts
193
+ ))
194
+ return new Pipeline(stream, memoStream)
195
+ }
196
+ }
197
+
198
+ module.exports.stream.byDigest = getStreamDigest
199
+
200
+ function info (cache, key, opts = {}) {
201
+ const { memoize } = opts
202
+ const memoized = memo.get(cache, key, opts)
203
+ if (memoized && memoize !== false) {
204
+ return Promise.resolve(memoized.entry)
205
+ } else {
206
+ return index.find(cache, key)
207
+ }
208
+ }
209
+ module.exports.info = info
210
+
211
+ 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
+ })
236
+ })
237
+ }
238
+ module.exports.copy = copy
239
+
240
+ 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
+ })
248
+ }
249
+ module.exports.copy.byDigest = copyByDigest
250
+
251
+ module.exports.hasContent = read.hasContent
@@ -1,20 +1,19 @@
1
1
  'use strict'
2
2
 
3
- const ls = require('./ls.js')
4
3
  const get = require('./get.js')
5
4
  const put = require('./put.js')
6
5
  const rm = require('./rm.js')
7
6
  const verify = require('./verify.js')
8
- const { clearMemoized } = require('./lib/memoization.js')
9
- const tmp = require('./lib/util/tmp.js')
10
- const index = require('./lib/entry-index.js')
7
+ const { clearMemoized } = require('./memoization.js')
8
+ const tmp = require('./util/tmp.js')
9
+ const index = require('./entry-index.js')
11
10
 
12
11
  module.exports.index = {}
13
12
  module.exports.index.compact = index.compact
14
13
  module.exports.index.insert = index.insert
15
14
 
16
- module.exports.ls = ls
17
- module.exports.ls.stream = ls.stream
15
+ module.exports.ls = index.ls
16
+ module.exports.ls.stream = index.lsStream
18
17
 
19
18
  module.exports.get = get
20
19
  module.exports.get.byDigest = get.byDigest
@@ -2,13 +2,11 @@
2
2
 
3
3
  const LRU = require('lru-cache')
4
4
 
5
- const MAX_SIZE = 50 * 1024 * 1024 // 50MB
6
- const MAX_AGE = 3 * 60 * 1000
7
-
8
5
  const MEMOIZED = new LRU({
9
- max: MAX_SIZE,
10
- maxAge: MAX_AGE,
11
- length: (entry, key) => key.startsWith('key:') ? entry.data.length : entry.length,
6
+ max: 500,
7
+ maxSize: 50 * 1024 * 1024, // 50MB
8
+ ttl: 3 * 60 * 1000, // 3 minutes
9
+ sizeCalculation: (entry, key) => key.startsWith('key:') ? entry.data.length : entry.length,
12
10
  })
13
11
 
14
12
  module.exports.clearMemoized = clearMemoized
@@ -62,12 +60,13 @@ class ObjProxy {
62
60
  }
63
61
 
64
62
  function pickMem (opts) {
65
- if (!opts || !opts.memoize)
63
+ if (!opts || !opts.memoize) {
66
64
  return MEMOIZED
67
- else if (opts.memoize.get && opts.memoize.set)
65
+ } else if (opts.memoize.get && opts.memoize.set) {
68
66
  return opts.memoize
69
- else if (typeof opts.memoize === 'object')
67
+ } else if (typeof opts.memoize === 'object') {
70
68
  return new ObjProxy(opts.memoize)
71
- else
69
+ } else {
72
70
  return MEMOIZED
71
+ }
73
72
  }
@@ -1,8 +1,8 @@
1
1
  'use strict'
2
2
 
3
- const index = require('./lib/entry-index')
4
- const memo = require('./lib/memoization')
5
- const write = require('./lib/content/write')
3
+ const index = require('./entry-index')
4
+ const memo = require('./memoization')
5
+ const write = require('./content/write')
6
6
  const Flush = require('minipass-flush')
7
7
  const { PassThrough } = require('minipass-collect')
8
8
  const Pipeline = require('minipass-pipeline')
@@ -21,8 +21,9 @@ function putData (cache, key, data, opts = {}) {
21
21
  return index
22
22
  .insert(cache, key, res.integrity, { ...opts, size: res.size })
23
23
  .then((entry) => {
24
- if (memoize)
24
+ if (memoize) {
25
25
  memo.put(cache, entry, data, opts)
26
+ }
26
27
 
27
28
  return res.integrity
28
29
  })
@@ -67,14 +68,17 @@ function putStream (cache, key, opts = {}) {
67
68
  return index
68
69
  .insert(cache, key, integrity, { ...opts, size })
69
70
  .then((entry) => {
70
- if (memoize && memoData)
71
+ if (memoize && memoData) {
71
72
  memo.put(cache, entry, memoData, opts)
73
+ }
72
74
 
73
- if (integrity)
75
+ if (integrity) {
74
76
  pipeline.emit('integrity', integrity)
77
+ }
75
78
 
76
- if (size)
79
+ if (size) {
77
80
  pipeline.emit('size', size)
81
+ }
78
82
  })
79
83
  },
80
84
  }))
@@ -2,11 +2,11 @@
2
2
 
3
3
  const util = require('util')
4
4
 
5
- const index = require('./lib/entry-index')
6
- const memo = require('./lib/memoization')
5
+ const index = require('./entry-index')
6
+ const memo = require('./memoization')
7
7
  const path = require('path')
8
8
  const rimraf = util.promisify(require('rimraf'))
9
- const rmContent = require('./lib/content/rm')
9
+ const rmContent = require('./content/rm')
10
10
 
11
11
  module.exports = entry
12
12
  module.exports.entry = entry
@@ -8,8 +8,9 @@ function disposer (creatorFn, disposerFn, fn) {
8
8
  .then(
9
9
  // disposer resolved, do something with original fn's promise
10
10
  () => {
11
- if (shouldThrow)
11
+ if (shouldThrow) {
12
12
  throw result
13
+ }
13
14
 
14
15
  return result
15
16
  },
@@ -49,8 +49,9 @@ function fixOwner (cache, filepath) {
49
49
  const { uid, gid } = owner
50
50
 
51
51
  // No need to override if it's already what we used.
52
- if (self.uid === uid && self.gid === gid)
52
+ if (self.uid === uid && self.gid === gid) {
53
53
  return
54
+ }
54
55
 
55
56
  return inflight('fixOwner: fixing ownership on ' + filepath, () =>
56
57
  chownr(
@@ -58,8 +59,9 @@ function fixOwner (cache, filepath) {
58
59
  typeof uid === 'number' ? uid : self.uid,
59
60
  typeof gid === 'number' ? gid : self.gid
60
61
  ).catch((err) => {
61
- if (err.code === 'ENOENT')
62
+ if (err.code === 'ENOENT') {
62
63
  return null
64
+ }
63
65
 
64
66
  throw err
65
67
  })
@@ -93,8 +95,9 @@ function fixOwnerSync (cache, filepath) {
93
95
  )
94
96
  } catch (err) {
95
97
  // only catch ENOENT, any other error is a problem.
96
- if (err.code === 'ENOENT')
98
+ if (err.code === 'ENOENT') {
97
99
  return null
100
+ }
98
101
 
99
102
  throw err
100
103
  }
@@ -110,12 +113,14 @@ function mkdirfix (cache, p, cb) {
110
113
  return Promise.resolve(inferOwner(cache)).then(() => {
111
114
  return mkdirp(p)
112
115
  .then((made) => {
113
- if (made)
116
+ if (made) {
114
117
  return fixOwner(cache, made).then(() => made)
118
+ }
115
119
  })
116
120
  .catch((err) => {
117
- if (err.code === 'EEXIST')
121
+ if (err.code === 'EEXIST') {
118
122
  return fixOwner(cache, p).then(() => null)
123
+ }
119
124
 
120
125
  throw err
121
126
  })
@@ -136,7 +141,8 @@ function mkdirfixSync (cache, p) {
136
141
  if (err.code === 'EEXIST') {
137
142
  fixOwnerSync(cache, p)
138
143
  return null
139
- } else
144
+ } else {
140
145
  throw err
146
+ }
141
147
  }
142
148
  }
@@ -38,10 +38,12 @@ function moveFile (src, dest) {
38
38
  } else if (err.code === 'EEXIST' || err.code === 'EBUSY') {
39
39
  // file already exists, so whatever
40
40
  return resolve()
41
- } else
41
+ } else {
42
42
  return reject(err)
43
- } else
43
+ }
44
+ } else {
44
45
  return resolve()
46
+ }
45
47
  })
46
48
  })
47
49
  .then(() => {
package/lib/util/tmp.js CHANGED
@@ -1,21 +1,21 @@
1
1
  'use strict'
2
2
 
3
- const util = require('util')
3
+ const fs = require('@npmcli/fs')
4
4
 
5
5
  const fixOwner = require('./fix-owner')
6
6
  const path = require('path')
7
- const rimraf = util.promisify(require('rimraf'))
8
- const uniqueFilename = require('unique-filename')
9
- const { disposer } = require('./disposer')
10
7
 
11
8
  module.exports.mkdir = mktmpdir
12
9
 
13
10
  function mktmpdir (cache, opts = {}) {
14
11
  const { tmpPrefix } = opts
15
- const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), tmpPrefix)
16
- return fixOwner.mkdirfix(cache, tmpTarget).then(() => {
17
- return tmpTarget
18
- })
12
+ const tmpDir = path.join(cache, 'tmp')
13
+ return fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
14
+ .then(() => {
15
+ // do not use path.join(), it drops the trailing / if tmpPrefix is unset
16
+ const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
17
+ return fs.mkdtemp(target, { owner: 'inherit' })
18
+ })
19
19
  }
20
20
 
21
21
  module.exports.withTmp = withTmp
@@ -25,7 +25,7 @@ function withTmp (cache, opts, cb) {
25
25
  cb = opts
26
26
  opts = {}
27
27
  }
28
- return disposer(mktmpdir(cache, opts), rimraf, cb)
28
+ return fs.withTempDir(path.join(cache, 'tmp'), cb, opts)
29
29
  }
30
30
 
31
31
  module.exports.fix = fixtmpdir
package/lib/verify.js CHANGED
@@ -54,8 +54,9 @@ function verify (cache, opts) {
54
54
  stats[k] = s[k]
55
55
  })
56
56
  const end = new Date()
57
- if (!stats.runTime)
57
+ if (!stats.runTime) {
58
58
  stats.runTime = {}
59
+ }
59
60
 
60
61
  stats.runTime[label] = end - start
61
62
  return Promise.resolve(stats)
@@ -108,8 +109,9 @@ function garbageCollect (cache, opts) {
108
109
  const indexStream = index.lsStream(cache)
109
110
  const liveContent = new Set()
110
111
  indexStream.on('data', (entry) => {
111
- if (opts.filter && !opts.filter(entry))
112
+ if (opts.filter && !opts.filter(entry)) {
112
113
  return
114
+ }
113
115
 
114
116
  liveContent.add(entry.integrity.toString())
115
117
  })
@@ -176,8 +178,9 @@ function verifyContent (filepath, sri) {
176
178
  return ssri
177
179
  .checkStream(new fsm.ReadStream(filepath), sri)
178
180
  .catch((err) => {
179
- if (err.code !== 'EINTEGRITY')
181
+ if (err.code !== 'EINTEGRITY') {
180
182
  throw err
183
+ }
181
184
 
182
185
  return rimraf(filepath).then(() => {
183
186
  contentInfo.valid = false
@@ -186,8 +189,9 @@ function verifyContent (filepath, sri) {
186
189
  .then(() => contentInfo)
187
190
  })
188
191
  .catch((err) => {
189
- if (err.code === 'ENOENT')
192
+ if (err.code === 'ENOENT') {
190
193
  return { size: 0, valid: false }
194
+ }
191
195
 
192
196
  throw err
193
197
  })
@@ -209,9 +213,9 @@ function rebuildIndex (cache, opts) {
209
213
  const entry = entries[k]
210
214
  const excluded = opts.filter && !opts.filter(entry)
211
215
  excluded && stats.rejectedEntries++
212
- if (buckets[hashed] && !excluded)
216
+ if (buckets[hashed] && !excluded) {
213
217
  buckets[hashed].push(entry)
214
- else if (buckets[hashed] && excluded) {
218
+ } else if (buckets[hashed] && excluded) {
215
219
  // skip
216
220
  } else if (excluded) {
217
221
  buckets[hashed] = []
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "cacache",
3
- "version": "15.2.0",
3
+ "version": "16.0.1",
4
4
  "cache-version": {
5
5
  "content": "2",
6
6
  "index": "5"
7
7
  },
8
8
  "description": "Fast, fault-tolerant, cross-platform, disk-based, data-agnostic, content-addressable cache.",
9
- "main": "index.js",
9
+ "main": "lib/index.js",
10
10
  "files": [
11
- "*.js",
11
+ "bin",
12
12
  "lib"
13
13
  ],
14
14
  "scripts": {
@@ -20,10 +20,13 @@
20
20
  "snap": "tap",
21
21
  "coverage": "tap",
22
22
  "test-docker": "docker run -it --rm --name pacotest -v \"$PWD\":/tmp -w /tmp node:latest npm test",
23
- "lint": "npm run npmclilint -- \"*.*js\" \"lib/**/*.*js\" \"test/**/*.*js\"",
23
+ "lint": "eslint '**/*.js'",
24
24
  "npmclilint": "npmcli-lint",
25
25
  "lintfix": "npm run lint -- --fix",
26
- "postsnap": "npm run lintfix --"
26
+ "postsnap": "npm run lintfix --",
27
+ "postlint": "npm-template-check",
28
+ "template-copy": "npm-template-copy --force",
29
+ "posttest": "npm run lint"
27
30
  },
28
31
  "repository": "https://github.com/npm/cacache",
29
32
  "keywords": [
@@ -43,26 +46,27 @@
43
46
  ],
44
47
  "license": "ISC",
45
48
  "dependencies": {
46
- "@npmcli/move-file": "^1.0.1",
49
+ "@npmcli/fs": "^1.0.0",
50
+ "@npmcli/move-file": "^1.1.2",
47
51
  "chownr": "^2.0.0",
48
- "fs-minipass": "^2.0.0",
49
- "glob": "^7.1.4",
52
+ "fs-minipass": "^2.1.0",
53
+ "glob": "^7.2.0",
50
54
  "infer-owner": "^1.0.4",
51
- "lru-cache": "^6.0.0",
52
- "minipass": "^3.1.1",
55
+ "lru-cache": "^7.5.1",
56
+ "minipass": "^3.1.6",
53
57
  "minipass-collect": "^1.0.2",
54
58
  "minipass-flush": "^1.0.5",
55
- "minipass-pipeline": "^1.2.2",
56
- "mkdirp": "^1.0.3",
59
+ "minipass-pipeline": "^1.2.4",
60
+ "mkdirp": "^1.0.4",
57
61
  "p-map": "^4.0.0",
58
62
  "promise-inflight": "^1.0.1",
59
63
  "rimraf": "^3.0.2",
60
64
  "ssri": "^8.0.1",
61
- "tar": "^6.0.2",
65
+ "tar": "^6.1.11",
62
66
  "unique-filename": "^1.1.1"
63
67
  },
64
68
  "devDependencies": {
65
- "@npmcli/lint": "^1.0.1",
69
+ "@npmcli/template-oss": "^2.9.2",
66
70
  "benchmark": "^2.1.4",
67
71
  "chalk": "^4.0.0",
68
72
  "require-inject": "^1.4.4",
@@ -74,6 +78,11 @@
74
78
  "test-regex": "test/[^/]*.js"
75
79
  },
76
80
  "engines": {
77
- "node": ">= 10"
78
- }
81
+ "node": "^12.13.0 || ^14.15.0 || >=16"
82
+ },
83
+ "templateOSS": {
84
+ "windowsCI": false,
85
+ "version": "2.9.2"
86
+ },
87
+ "author": "GitHub Inc."
79
88
  }
package/get.js DELETED
@@ -1,253 +0,0 @@
1
- 'use strict'
2
-
3
- const util = require('util')
4
- const fs = require('fs')
5
- const index = require('./lib/entry-index')
6
- const memo = require('./lib/memoization')
7
- const read = require('./lib/content/read')
8
-
9
- const Minipass = require('minipass')
10
- const Collect = require('minipass-collect')
11
- const Pipeline = require('minipass-pipeline')
12
-
13
- const writeFile = util.promisify(fs.writeFile)
14
-
15
- module.exports = function get (cache, key, opts) {
16
- return getData(false, cache, key, opts)
17
- }
18
- module.exports.byDigest = function getByDigest (cache, digest, opts) {
19
- return getData(true, cache, digest, opts)
20
- }
21
-
22
- function getData (byDigest, cache, key, opts = {}) {
23
- const { integrity, memoize, size } = opts
24
- const memoized = byDigest
25
- ? memo.get.byDigest(cache, key, opts)
26
- : memo.get(cache, key, opts)
27
- if (memoized && memoize !== false) {
28
- return Promise.resolve(
29
- byDigest
30
- ? memoized
31
- : {
32
- metadata: memoized.entry.metadata,
33
- data: memoized.data,
34
- integrity: memoized.entry.integrity,
35
- size: memoized.entry.size,
36
- }
37
- )
38
- }
39
- return (byDigest ? Promise.resolve(null) : index.find(cache, key, opts)).then(
40
- (entry) => {
41
- if (!entry && !byDigest)
42
- throw new index.NotFoundError(cache, key)
43
-
44
- return read(cache, byDigest ? key : entry.integrity, {
45
- integrity,
46
- size,
47
- })
48
- .then((data) =>
49
- byDigest
50
- ? data
51
- : {
52
- data,
53
- metadata: entry.metadata,
54
- size: entry.size,
55
- integrity: entry.integrity,
56
- }
57
- )
58
- .then((res) => {
59
- if (memoize && byDigest)
60
- memo.put.byDigest(cache, key, res, opts)
61
- else if (memoize)
62
- memo.put(cache, entry, res.data, opts)
63
-
64
- return res
65
- })
66
- }
67
- )
68
- }
69
-
70
- module.exports.sync = function get (cache, key, opts) {
71
- return getDataSync(false, cache, key, opts)
72
- }
73
- module.exports.sync.byDigest = function getByDigest (cache, digest, opts) {
74
- return getDataSync(true, cache, digest, opts)
75
- }
76
-
77
- function getDataSync (byDigest, cache, key, opts = {}) {
78
- const { integrity, memoize, size } = opts
79
- const memoized = byDigest
80
- ? memo.get.byDigest(cache, key, opts)
81
- : memo.get(cache, key, opts)
82
- if (memoized && memoize !== false) {
83
- return byDigest
84
- ? memoized
85
- : {
86
- metadata: memoized.entry.metadata,
87
- data: memoized.data,
88
- integrity: memoized.entry.integrity,
89
- size: memoized.entry.size,
90
- }
91
- }
92
- const entry = !byDigest && index.find.sync(cache, key, opts)
93
- if (!entry && !byDigest)
94
- throw new index.NotFoundError(cache, key)
95
-
96
- const data = read.sync(cache, byDigest ? key : entry.integrity, {
97
- integrity: integrity,
98
- size: size,
99
- })
100
- const res = byDigest
101
- ? data
102
- : {
103
- metadata: entry.metadata,
104
- data: data,
105
- size: entry.size,
106
- integrity: entry.integrity,
107
- }
108
- if (memoize && byDigest)
109
- memo.put.byDigest(cache, key, res, opts)
110
- else if (memoize)
111
- memo.put(cache, entry, res.data, opts)
112
-
113
- return res
114
- }
115
-
116
- module.exports.stream = getStream
117
-
118
- const getMemoizedStream = (memoized) => {
119
- const stream = new Minipass()
120
- stream.on('newListener', function (ev, cb) {
121
- ev === 'metadata' && cb(memoized.entry.metadata)
122
- ev === 'integrity' && cb(memoized.entry.integrity)
123
- ev === 'size' && cb(memoized.entry.size)
124
- })
125
- stream.end(memoized.data)
126
- return stream
127
- }
128
-
129
- function getStream (cache, key, opts = {}) {
130
- const { memoize, size } = opts
131
- const memoized = memo.get(cache, key, opts)
132
- if (memoized && memoize !== false)
133
- return getMemoizedStream(memoized)
134
-
135
- const stream = new Pipeline()
136
- index
137
- .find(cache, key)
138
- .then((entry) => {
139
- if (!entry)
140
- throw new index.NotFoundError(cache, key)
141
-
142
- stream.emit('metadata', entry.metadata)
143
- stream.emit('integrity', entry.integrity)
144
- stream.emit('size', entry.size)
145
- stream.on('newListener', function (ev, cb) {
146
- ev === 'metadata' && cb(entry.metadata)
147
- ev === 'integrity' && cb(entry.integrity)
148
- ev === 'size' && cb(entry.size)
149
- })
150
-
151
- const src = read.readStream(
152
- cache,
153
- entry.integrity,
154
- { ...opts, size: typeof size !== 'number' ? entry.size : size }
155
- )
156
-
157
- if (memoize) {
158
- const memoStream = new Collect.PassThrough()
159
- memoStream.on('collect', data => memo.put(cache, entry, data, opts))
160
- stream.unshift(memoStream)
161
- }
162
- stream.unshift(src)
163
- })
164
- .catch((err) => stream.emit('error', err))
165
-
166
- return stream
167
- }
168
-
169
- module.exports.stream.byDigest = getStreamDigest
170
-
171
- function getStreamDigest (cache, integrity, opts = {}) {
172
- const { memoize } = opts
173
- const memoized = memo.get.byDigest(cache, integrity, opts)
174
- if (memoized && memoize !== false) {
175
- const stream = new Minipass()
176
- stream.end(memoized)
177
- return stream
178
- } else {
179
- const stream = read.readStream(cache, integrity, opts)
180
- if (!memoize)
181
- return stream
182
-
183
- const memoStream = new Collect.PassThrough()
184
- memoStream.on('collect', data => memo.put.byDigest(
185
- cache,
186
- integrity,
187
- data,
188
- opts
189
- ))
190
- return new Pipeline(stream, memoStream)
191
- }
192
- }
193
-
194
- module.exports.info = info
195
-
196
- function info (cache, key, opts = {}) {
197
- const { memoize } = opts
198
- const memoized = memo.get(cache, key, opts)
199
- if (memoized && memoize !== false)
200
- return Promise.resolve(memoized.entry)
201
- else
202
- return index.find(cache, key)
203
- }
204
-
205
- module.exports.hasContent = read.hasContent
206
-
207
- function cp (cache, key, dest, opts) {
208
- return copy(false, cache, key, dest, opts)
209
- }
210
-
211
- module.exports.copy = cp
212
-
213
- function cpDigest (cache, digest, dest, opts) {
214
- return copy(true, cache, digest, dest, opts)
215
- }
216
-
217
- module.exports.copy.byDigest = cpDigest
218
-
219
- function copy (byDigest, cache, key, dest, opts = {}) {
220
- if (read.copy) {
221
- return (byDigest
222
- ? Promise.resolve(null)
223
- : index.find(cache, key, opts)
224
- ).then((entry) => {
225
- if (!entry && !byDigest)
226
- throw new index.NotFoundError(cache, key)
227
-
228
- return read
229
- .copy(cache, byDigest ? key : entry.integrity, dest, opts)
230
- .then(() => {
231
- return byDigest
232
- ? key
233
- : {
234
- metadata: entry.metadata,
235
- size: entry.size,
236
- integrity: entry.integrity,
237
- }
238
- })
239
- })
240
- }
241
-
242
- return getData(byDigest, cache, key, opts).then((res) => {
243
- return writeFile(dest, byDigest ? res : res.data).then(() => {
244
- return byDigest
245
- ? key
246
- : {
247
- metadata: res.metadata,
248
- size: res.size,
249
- integrity: res.integrity,
250
- }
251
- })
252
- })
253
- }
package/ls.js DELETED
@@ -1,6 +0,0 @@
1
- 'use strict'
2
-
3
- const index = require('./lib/entry-index')
4
-
5
- module.exports = index.ls
6
- module.exports.stream = index.lsStream
package/verify.js DELETED
@@ -1,3 +0,0 @@
1
- 'use strict'
2
-
3
- module.exports = require('./lib/verify')