Skip to content

Commit

Permalink
inflate-shrinkwrap: Infer versions from tarballs to self heal
Browse files Browse the repository at this point in the history
Credit: @iarna
  • Loading branch information
iarna committed May 5, 2018
1 parent 36f9984 commit 113e1a3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/install/inflate-shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,39 @@ function normalizePackageDataNoErrors (pkg) {
}
}

function quotemeta (str) {
return str.replace(/([^A-Za-z_0-9/])/g, '\\$1')
}

function tarballToVersion (name, tb) {
const registry = quotemeta(npm.config.get('registry'))
.replace(/https?:/, 'https?:')
.replace(/([^/])$/, '$1/')
let matchRegTarball
if (name) {
const nameMatch = quotemeta(name)
matchRegTarball = new RegExp(`^${registry}${nameMatch}/-/${nameMatch}-(.*)[.]tgz$`)
} else {
matchRegTarball = new RegExp(`^${registry}(.*)?/-/\\1-(.*)[.]tgz$`)
}
const match = tb.match(matchRegTarball)
if (!match) return
return match[2] || match[1]
}

function inflatableChild (onDiskChild, name, topPath, tree, sw, requested, opts) {
validate('OSSOOOO|ZSSOOOO', arguments)
const usesIntegrity = (
requested.registry ||
requested.type === 'remote' ||
requested.type === 'file'
)
const regTarball = tarballToVersion(name, sw.version)
if (regTarball) {
sw.resolved = sw.version
sw.version = regTarball
}
if (sw.requires) Object.keys(sw.requires).map((_) => { sw.requires[_] = tarballToVersion(_, sw.requires[_]) || sw.requires[_] })
const modernLink = requested.type === 'directory' && !sw.from
if (hasModernMeta(onDiskChild) && childIsEquivalent(sw, requested, onDiskChild)) {
// The version on disk matches the shrinkwrap entry.
Expand Down

0 comments on commit 113e1a3

Please sign in to comment.