cacache 12.0.4 → 15.0.0

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.
package/put.js CHANGED
@@ -1,86 +1,84 @@
1
1
  'use strict'
2
2
 
3
- const figgyPudding = require('figgy-pudding')
4
3
  const index = require('./lib/entry-index')
5
4
  const memo = require('./lib/memoization')
6
5
  const write = require('./lib/content/write')
7
- const to = require('mississippi').to
6
+ const Flush = require('minipass-flush')
7
+ const { PassThrough } = require('minipass-collect')
8
+ const Pipeline = require('minipass-pipeline')
8
9
 
9
- const PutOpts = figgyPudding({
10
- algorithms: {
11
- default: ['sha512']
12
- },
13
- integrity: {},
14
- memoize: {},
15
- metadata: {},
16
- pickAlgorithm: {},
17
- size: {},
18
- tmpPrefix: {},
19
- single: {},
20
- sep: {},
21
- error: {},
22
- strict: {}
10
+ const putOpts = (opts) => ({
11
+ algorithms: ['sha512'],
12
+ ...opts
23
13
  })
24
14
 
25
15
  module.exports = putData
26
- function putData (cache, key, data, opts) {
27
- opts = PutOpts(opts)
28
- return write(cache, data, opts).then(res => {
29
- return index.insert(
30
- cache, key, res.integrity, opts.concat({ size: res.size })
31
- ).then(entry => {
32
- if (opts.memoize) {
33
- memo.put(cache, entry, data, opts)
34
- }
35
- return res.integrity
36
- })
16
+
17
+ function putData (cache, key, data, opts = {}) {
18
+ const { memoize } = opts
19
+ opts = putOpts(opts)
20
+ return write(cache, data, opts).then((res) => {
21
+ return index
22
+ .insert(cache, key, res.integrity, { ...opts, size: res.size })
23
+ .then((entry) => {
24
+ if (memoize) {
25
+ memo.put(cache, entry, data, opts)
26
+ }
27
+ return res.integrity
28
+ })
37
29
  })
38
30
  }
39
31
 
40
32
  module.exports.stream = putStream
41
- function putStream (cache, key, opts) {
42
- opts = PutOpts(opts)
33
+
34
+ function putStream (cache, key, opts = {}) {
35
+ const { memoize } = opts
36
+ opts = putOpts(opts)
43
37
  let integrity
44
38
  let size
45
- const contentStream = write.stream(
46
- cache, opts
47
- ).on('integrity', int => {
48
- integrity = int
49
- }).on('size', s => {
50
- size = s
51
- })
39
+
52
40
  let memoData
53
- let memoTotal = 0
54
- const stream = to((chunk, enc, cb) => {
55
- contentStream.write(chunk, enc, () => {
56
- if (opts.memoize) {
57
- if (!memoData) { memoData = [] }
58
- memoData.push(chunk)
59
- memoTotal += chunk.length
60
- }
61
- cb()
41
+ const pipeline = new Pipeline()
42
+ // first item in the pipeline is the memoizer, because we need
43
+ // that to end first and get the collected data.
44
+ if (memoize) {
45
+ const memoizer = new PassThrough().on('collect', data => {
46
+ memoData = data
62
47
  })
63
- }, cb => {
64
- contentStream.end(() => {
65
- index.insert(cache, key, integrity, opts.concat({ size })).then(entry => {
66
- if (opts.memoize) {
67
- memo.put(cache, entry, Buffer.concat(memoData, memoTotal), opts)
68
- }
69
- stream.emit('integrity', integrity)
70
- cb()
71
- })
48
+ pipeline.push(memoizer)
49
+ }
50
+
51
+ // contentStream is a write-only, not a passthrough
52
+ // no data comes out of it.
53
+ const contentStream = write.stream(cache, opts)
54
+ .on('integrity', (int) => {
55
+ integrity = int
72
56
  })
73
- })
74
- let erred = false
75
- stream.once('error', err => {
76
- if (erred) { return }
77
- erred = true
78
- contentStream.emit('error', err)
79
- })
80
- contentStream.once('error', err => {
81
- if (erred) { return }
82
- erred = true
83
- stream.emit('error', err)
84
- })
85
- return stream
57
+ .on('size', (s) => {
58
+ size = s
59
+ })
60
+
61
+ pipeline.push(contentStream)
62
+
63
+ // last but not least, we write the index and emit hash and size,
64
+ // and memoize if we're doing that
65
+ pipeline.push(new Flush({
66
+ flush () {
67
+ return index
68
+ .insert(cache, key, integrity, { ...opts, size })
69
+ .then((entry) => {
70
+ if (memoize && memoData) {
71
+ memo.put(cache, entry, memoData, opts)
72
+ }
73
+ if (integrity) {
74
+ pipeline.emit('integrity', integrity)
75
+ }
76
+ if (size) {
77
+ pipeline.emit('size', size)
78
+ }
79
+ })
80
+ }
81
+ }))
82
+
83
+ return pipeline
86
84
  }
package/rm.js CHANGED
@@ -1,27 +1,30 @@
1
1
  'use strict'
2
2
 
3
- const BB = require('bluebird')
3
+ const util = require('util')
4
4
 
5
5
  const index = require('./lib/entry-index')
6
6
  const memo = require('./lib/memoization')
7
7
  const path = require('path')
8
- const rimraf = BB.promisify(require('rimraf'))
8
+ const rimraf = util.promisify(require('rimraf'))
9
9
  const rmContent = require('./lib/content/rm')
10
10
 
11
11
  module.exports = entry
12
12
  module.exports.entry = entry
13
+
13
14
  function entry (cache, key) {
14
15
  memo.clearMemoized()
15
16
  return index.delete(cache, key)
16
17
  }
17
18
 
18
19
  module.exports.content = content
20
+
19
21
  function content (cache, integrity) {
20
22
  memo.clearMemoized()
21
23
  return rmContent(cache, integrity)
22
24
  }
23
25
 
24
26
  module.exports.all = all
27
+
25
28
  function all (cache) {
26
29
  memo.clearMemoized()
27
30
  return rimraf(path.join(cache, '*(content-*|index-*)'))