Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const tryFn = () => {
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 fn(cpath, digests[0])
} else {
// Can't use race here because a generic error can happen before a ENOENT error, and can happen before a valid result
return Promise
.all(digests.map((meta) => {
return withContentSri(cache, meta, fn)
.catch((err) => {
if (err.code === 'ENOENT') {
return Object.assign(
function pkgIntegrity (pkg) {
try {
// dist is provided by the registry
var sri = (pkg.dist && pkg.dist.integrity) ||
// _integrity is provided by pacote
pkg._integrity ||
// _shasum is legacy
(pkg._shasum && ssri.fromHex(pkg._shasum, 'sha1').toString())
if (!sri) return
var integrity = ssri.parse(sri)
if (Object.keys(integrity).length === 0) return
return integrity
} catch (ex) {
return
}
}
function withContentSriSync (cache, integrity, fn) {
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 fn(cpath, digests[0])
} else {
let lastErr = null
for (const meta of digests) {
try {
return withContentSriSync(cache, meta, fn)
} catch (err) {
lastErr = err
}
}
test('Lockfile.getLockfile handles integrity field', () => {
const integrity = ssri.parse('sha1-foo sha512-bar');
const patterns = {
foobar: {
name: 'foobar',
version: '0.0.0',
uid: '0.0.0',
dependencies: {},
optionalDependencies: {},
_reference: {
permissions: {},
},
_remote: {
resolved: 'http://example.com/foobar',
registry: 'npm',
integrity,
},
},
export const getIntegrity = async (path: string): Promise => {
try {
const data = await readFile(path);
return fromData(data.toString());
} catch (err) {
bug(`failed to get hash integrity of ${path} from zkat/ssri`, err);
return parse('');
}
};
_analyze(integrity) {
const sri = ssri.parse(integrity, { single: true });
const algorithm = sri.algorithm;
const hex = sri.hexDigest();
const segLen = 2;
const contentPath = Path.join(
...[this._centralDir, algorithm].concat(
hex.substr(0, segLen),
hex.substr(segLen, segLen),
hex.substr(segLen * 2)
)
);
return { algorithm, contentPath, hex };
}
function childIsEquivalent (sw, requested, child) {
if (!child) return false
if (child.fromShrinkwrap) return true
if (
sw.integrity &&
child.package._integrity &&
ssri.parse(sw.integrity).match(child.package._integrity)
) return true
if (child.isLink && requested.type === 'directory') return path.relative(child.realpath, requested.fetchSpec) === ''
if (sw.resolved) return child.package._resolved === sw.resolved
if (!isRegistry(requested) && sw.from) return child.package._from === sw.from
if (!isRegistry(requested) && child.package._resolved) return sw.version === child.package._resolved
return child.package.version === sw.version
}