Skip to content

Commit

Permalink
perf(finalize-manifest): cache finalized manifests
Browse files Browse the repository at this point in the history
Fixes: #35
  • Loading branch information
zkat committed Mar 6, 2017
1 parent 8ba7249 commit fa3c430
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/finalize-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BB = require('bluebird')

const cache = require('./cache')
const checksumStream = require('checksum-stream')
const dezalgo = require('dezalgo')
const finished = require('mississippi').finished
Expand All @@ -17,12 +18,31 @@ const through = require('mississippi').through

module.exports = finalizeManifest
function finalizeManifest (pkg, spec, opts) {
return BB.fromNode(cb => {
tarballedProps(pkg, spec, opts, (err, props) => {
if (err) { return cb(err) }
// normalize should not add any fields, and once
// makeManifest completes, it should never be modified.
cb(null, new Manifest(pkg, props))
const key = cache.key(`${spec.type}-manifest`, pkg._resolved)
opts = optCheck(opts)
opts.memoize = true
return (opts.cache ? cache.get.info(opts.cache, key, opts).then(res => {
if (!res) { throw new Error('cache miss') }
return new Manifest(res.metadata)
}) : BB.reject()).catch(() => {
return BB.fromNode(cb => {
tarballedProps(pkg, spec, opts, function (err, props) {
if (err) { return cb(err) }
// normalize should not add any fields, and once
// makeManifest completes, it should never be modified.
var result = new Manifest(pkg, props)
if (opts.cache) {
opts.metadata = result
cache.put(
opts.cache,
key,
'.',
opts
).then(() => cb(null, result), cb)
} else {
cb(null, result)
}
})
})
})
}
Expand Down

0 comments on commit fa3c430

Please sign in to comment.