cacache 12.0.3 → 14.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
@@ -4,7 +4,9 @@ const figgyPudding = require('figgy-pudding')
4
4
  const index = require('./lib/entry-index')
5
5
  const memo = require('./lib/memoization')
6
6
  const write = require('./lib/content/write')
7
- const to = require('mississippi').to
7
+ const Flush = require('minipass-flush')
8
+ const { PassThrough } = require('minipass-collect')
9
+ const Pipeline = require('minipass-pipeline')
8
10
 
9
11
  const PutOpts = figgyPudding({
10
12
  algorithms: {
@@ -23,64 +25,70 @@ const PutOpts = figgyPudding({
23
25
  })
24
26
 
25
27
  module.exports = putData
28
+
26
29
  function putData (cache, key, data, opts) {
27
30
  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
- })
31
+ return write(cache, data, opts).then((res) => {
32
+ return index
33
+ .insert(cache, key, res.integrity, opts.concat({ size: res.size }))
34
+ .then((entry) => {
35
+ if (opts.memoize) {
36
+ memo.put(cache, entry, data, opts)
37
+ }
38
+ return res.integrity
39
+ })
37
40
  })
38
41
  }
39
42
 
40
43
  module.exports.stream = putStream
44
+
41
45
  function putStream (cache, key, opts) {
42
46
  opts = PutOpts(opts)
43
47
  let integrity
44
48
  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
- })
49
+
52
50
  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()
51
+ const pipeline = new Pipeline()
52
+ // first item in the pipeline is the memoizer, because we need
53
+ // that to end first and get the collected data.
54
+ if (opts.memoize) {
55
+ const memoizer = new PassThrough().on('collect', data => {
56
+ memoData = data
62
57
  })
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
- })
58
+ pipeline.push(memoizer)
59
+ }
60
+
61
+ // contentStream is a write-only, not a passthrough
62
+ // no data comes out of it.
63
+ const contentStream = write.stream(cache, opts)
64
+ .on('integrity', (int) => {
65
+ integrity = int
72
66
  })
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
67
+ .on('size', (s) => {
68
+ size = s
69
+ })
70
+
71
+ pipeline.push(contentStream)
72
+
73
+ // last but not least, we write the index and emit hash and size,
74
+ // and memoize if we're doing that
75
+ pipeline.push(new Flush({
76
+ flush () {
77
+ return index
78
+ .insert(cache, key, integrity, opts.concat({ size }))
79
+ .then((entry) => {
80
+ if (opts.memoize && memoData) {
81
+ memo.put(cache, entry, memoData, opts)
82
+ }
83
+ if (integrity) {
84
+ pipeline.emit('integrity', integrity)
85
+ }
86
+ if (size) {
87
+ pipeline.emit('size', size)
88
+ }
89
+ })
90
+ }
91
+ }))
92
+
93
+ return pipeline
86
94
  }
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-*)'))