diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2017-04-29 01:11:18 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2017-05-26 02:13:33 +0300 |
commit | c8b66854adef8d91c4ad2b058db6f1ca48031b58 (patch) | |
tree | 5e34a1bc728c551d5fa1cbf6bd50d53a04c634f3 /node_modules/cacache/put.js | |
parent | cbaf1c914036bc8ac1f9ea965ccba4fd690c8c45 (diff) |
pacote@2.7.4
Diffstat (limited to 'node_modules/cacache/put.js')
-rw-r--r-- | node_modules/cacache/put.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/node_modules/cacache/put.js b/node_modules/cacache/put.js new file mode 100644 index 000000000..fe1293e5e --- /dev/null +++ b/node_modules/cacache/put.js @@ -0,0 +1,71 @@ +'use strict' + +const index = require('./lib/entry-index') +const memo = require('./lib/memoization') +const write = require('./lib/content/write') +const to = require('mississippi').to + +module.exports = putData +function putData (cache, key, data, opts) { + opts = opts || {} + return write(cache, data, opts).then(res => { + // TODO - stop modifying opts + opts.size = res.size + return index.insert(cache, key, res.integrity, opts).then(entry => { + if (opts.memoize) { + memo.put(cache, entry, data, opts) + } + return res.integrity + }) + }) +} + +module.exports.stream = putStream +function putStream (cache, key, opts) { + opts = 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() + }) + }, cb => { + contentStream.end(() => { + // TODO - stop modifying `opts` + opts.size = size + index.insert(cache, key, integrity, opts).then(entry => { + if (opts.memoize) { + memo.put(cache, entry, Buffer.concat(memoData, memoTotal), opts) + } + stream.emit('integrity', integrity) + cb() + }) + }) + }) + 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 +} |