Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(read): skip reading the filesystem if our integrity hash is singl…
Browse files Browse the repository at this point in the history
…e-entry
  • Loading branch information
zkat committed Apr 3, 2017
1 parent 4f2bcf2 commit 0adfb82
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function readStream (cache, integrity, opts) {
module.exports.hasContent = hasContent
function hasContent (cache, integrity) {
if (!integrity) { return BB.resolve(false) }
return pickContentSri(cache, integrity)
return pickContentSri(cache, integrity, true)
.catch({code: 'ENOENT'}, () => false)
.catch({code: 'EPERM'}, err => {
if (process.platform !== 'win32') {
Expand All @@ -64,18 +64,22 @@ function hasContent (cache, integrity) {
}

module.exports._pickContentSri = pickContentSri
function pickContentSri (cache, integrity) {
function pickContentSri (cache, integrity, checkFs) {
const sri = ssri.parse(integrity)
// If `integrity` has multiple entries, pick the first digest
// with available local data.
const algo = sri.pickAlgorithm()
const digests = sri[algo]
if (digests.length <= 1) {
const cpath = contentPath(cache, digests[0])
return fs.lstatAsync(cpath).then(() => digests[0])
if (checkFs) {
return fs.lstatAsync(cpath).then(() => digests[0])
} else {
return BB.resolve(digests[0])
}
} else {
return BB.any(sri[sri.pickAlgorithm()].map(meta => {
return pickContentSri(cache, meta)
return pickContentSri(cache, meta, true)
}))
}
}
Expand Down

0 comments on commit 0adfb82

Please sign in to comment.