Skip to content

Commit 113e1a3

Browse files
committed
inflate-shrinkwrap: Infer versions from tarballs to self heal
Credit: @iarna
1 parent 36f9984 commit 113e1a3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/install/inflate-shrinkwrap.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,39 @@ function normalizePackageDataNoErrors (pkg) {
6969
}
7070
}
7171

72+
function quotemeta (str) {
73+
return str.replace(/([^A-Za-z_0-9/])/g, '\\$1')
74+
}
75+
76+
function tarballToVersion (name, tb) {
77+
const registry = quotemeta(npm.config.get('registry'))
78+
.replace(/https?:/, 'https?:')
79+
.replace(/([^/])$/, '$1/')
80+
let matchRegTarball
81+
if (name) {
82+
const nameMatch = quotemeta(name)
83+
matchRegTarball = new RegExp(`^${registry}${nameMatch}/-/${nameMatch}-(.*)[.]tgz$`)
84+
} else {
85+
matchRegTarball = new RegExp(`^${registry}(.*)?/-/\\1-(.*)[.]tgz$`)
86+
}
87+
const match = tb.match(matchRegTarball)
88+
if (!match) return
89+
return match[2] || match[1]
90+
}
91+
7292
function inflatableChild (onDiskChild, name, topPath, tree, sw, requested, opts) {
7393
validate('OSSOOOO|ZSSOOOO', arguments)
7494
const usesIntegrity = (
7595
requested.registry ||
7696
requested.type === 'remote' ||
7797
requested.type === 'file'
7898
)
99+
const regTarball = tarballToVersion(name, sw.version)
100+
if (regTarball) {
101+
sw.resolved = sw.version
102+
sw.version = regTarball
103+
}
104+
if (sw.requires) Object.keys(sw.requires).map((_) => { sw.requires[_] = tarballToVersion(_, sw.requires[_]) || sw.requires[_] })
79105
const modernLink = requested.type === 'directory' && !sw.from
80106
if (hasModernMeta(onDiskChild) && childIsEquivalent(sw, requested, onDiskChild)) {
81107
// The version on disk matches the shrinkwrap entry.

0 commit comments

Comments
 (0)