diff options
author | claudiahdz <cghr1990@gmail.com> | 2020-02-06 23:25:33 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:11:51 +0300 |
commit | 0a45514d8beba749bebbd973ca9750ee0dc13b40 (patch) | |
tree | eec0c989c38fcb7395c9fd565f2a44c79369c59e /node_modules/cacache/put.js | |
parent | 019e6c41a85edf62727b517e1dfe2d5b9e53ba0e (diff) |
cacache@15.0.0
Diffstat (limited to 'node_modules/cacache/put.js')
-rw-r--r-- | node_modules/cacache/put.js | 132 |
1 files changed, 65 insertions, 67 deletions
diff --git a/node_modules/cacache/put.js b/node_modules/cacache/put.js index a40063930..eb21aa867 100644 --- a/node_modules/cacache/put.js +++ b/node_modules/cacache/put.js @@ -1,86 +1,84 @@ 'use strict' -const figgyPudding = require('figgy-pudding') const index = require('./lib/entry-index') const memo = require('./lib/memoization') const write = require('./lib/content/write') -const to = require('mississippi').to +const Flush = require('minipass-flush') +const { PassThrough } = require('minipass-collect') +const Pipeline = require('minipass-pipeline') -const PutOpts = figgyPudding({ - algorithms: { - default: ['sha512'] - }, - integrity: {}, - memoize: {}, - metadata: {}, - pickAlgorithm: {}, - size: {}, - tmpPrefix: {}, - single: {}, - sep: {}, - error: {}, - strict: {} +const putOpts = (opts) => ({ + algorithms: ['sha512'], + ...opts }) module.exports = putData -function putData (cache, key, data, opts) { - opts = PutOpts(opts) - return write(cache, data, opts).then(res => { - return index.insert( - cache, key, res.integrity, opts.concat({ size: res.size }) - ).then(entry => { - if (opts.memoize) { - memo.put(cache, entry, data, opts) - } - return res.integrity - }) + +function putData (cache, key, data, opts = {}) { + const { memoize } = opts + opts = putOpts(opts) + return write(cache, data, opts).then((res) => { + return index + .insert(cache, key, res.integrity, { ...opts, size: res.size }) + .then((entry) => { + if (memoize) { + memo.put(cache, entry, data, opts) + } + return res.integrity + }) }) } module.exports.stream = putStream -function putStream (cache, key, opts) { - opts = PutOpts(opts) + +function putStream (cache, key, opts = {}) { + const { memoize } = opts + opts = putOpts(opts) let integrity let size - const contentStream = write.stream( - cache, opts - ).on('integrity', int => { - integrity = int - }).on('size', s => { - size = s - }) + let memoData - let memoTotal = 0 - const stream = to((chunk, enc, cb) => { - contentStream.write(chunk, enc, () => { - if (opts.memoize) { - if (!memoData) { memoData = [] } - memoData.push(chunk) - memoTotal += chunk.length - } - cb() + const pipeline = new Pipeline() + // first item in the pipeline is the memoizer, because we need + // that to end first and get the collected data. + if (memoize) { + const memoizer = new PassThrough().on('collect', data => { + memoData = data }) - }, cb => { - contentStream.end(() => { - index.insert(cache, key, integrity, opts.concat({ size })).then(entry => { - if (opts.memoize) { - memo.put(cache, entry, Buffer.concat(memoData, memoTotal), opts) - } - stream.emit('integrity', integrity) - cb() - }) + pipeline.push(memoizer) + } + + // contentStream is a write-only, not a passthrough + // no data comes out of it. + const contentStream = write.stream(cache, opts) + .on('integrity', (int) => { + integrity = int }) - }) - let erred = false - stream.once('error', err => { - if (erred) { return } - erred = true - contentStream.emit('error', err) - }) - contentStream.once('error', err => { - if (erred) { return } - erred = true - stream.emit('error', err) - }) - return stream + .on('size', (s) => { + size = s + }) + + 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) { + pipeline.emit('integrity', integrity) + } + if (size) { + pipeline.emit('size', size) + } + }) + } + })) + + return pipeline } |