Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const main = async lock => {
for (const [loc, meta] of Object.entries(lock.packages)) {
if (!loc || meta.link || !nm.test(loc))
continue
const name = meta.name || loc.replace(nm, '$1')
console.error('FETCHING', name)
const paku = await pacote.packument(name)
const saveTo = resolve(dir, name.replace(/^@/, '') + '.json')
mkdirp.sync(dirname(saveTo))
writeFileSync(saveTo, JSON.stringify(paku, 0, 2))
// bundled deps sometimes don't have a resolved value
if (!meta.resolved)
continue
const path = url.parse(meta.resolved).pathname.replace(/^\/@?/, '')
const tgzFile = resolve(dir, path)
await pacote.tarball.file(meta.resolved, tgzFile)
}
console.log('OK!')
}
const pacoteRequest = () => {
return pacote
.packument(
pkgName,
this.getPacoteOpts({
"full-metadata": true,
"fetch-retries": 3,
"cache-policy": "ignore",
"cache-key": qItem.cacheKey,
memoize: false
})
)
.tap(x => {
this._metaStat.inTx--;
if (x.readme) delete x.readme; // don't need this
updateItem(x._cached ? "cached" : "200");
})
.catch(err => {
pacote.manifest(`${packageName}@${packageVersion}`, pacoteOptions)
.catch((error) => {
if (error.code === 'ETARGET') {
const { latest } = error.distTags;
return pacote.manifest(`${packageName}@${latest}`, pacoteOptions);
}
if (error.code === 'E404') {
console.error(`\nError: "${packageName}@${packageVersion}" not found`);
return packageVersion !== 'latest' ? pacote.manifest(packageName, pacoteOptions)
: Promise.reject(new Error(`${packageName}@${packageVersion} not found`));
}
return Promise.reject(error);
}),
packageVersion !== 'latest' ? pacote.packument(packageName, pacoteOptions) : Promise.resolve(),
])
.then(([manifest, packument]) => {
if (packument) {
manifest.isLatest = packument['dist-tags'].latest === manifest.version;
} else {
manifest.isLatest = true;
}
return manifest;
});
}
async function getVersion(name: npa.Result): Promise {
if (name.type === 'version') {
return name.fetchSpec!;
}
const packument = await pacote.packument(name.name);
if (name.type === 'tag') {
const version = packument['dist-tags'][name.fetchSpec!];
if (typeof version !== 'string') {
throw new Error(`Could not determine version of ${name.name} for tag ${name.fetchSpec}`);
}
return version;
}
if (name.type === 'range') {
const versions = Object.keys(packument.versions).sort(semver.compare);
const spec = typeof name.fetchSpec === 'string' ? name.fetchSpec : 'latest';
const version = semver.maxSatisfying(versions, spec);
async function getExpectedSemVer(range) {
try {
const { versions, "dist-tags": { latest } } = await pacote.packument(depName, {
registry: REGISTRY_DEFAULT_ADDR,
...token
});
const currVersion = semver.maxSatisfying(Object.keys(versions), range);
return currVersion === null ? latest : currVersion;
}
catch (err) {
return cleanRange(range);
}
}
function viewMany(packageName, fields, currentVersion) {
if (currentVersion && (!semver.validRange(currentVersion) || versionUtil.isWildCard(currentVersion))) {
return Promise.resolve({});
}
npmConfig.fullMetadata = _.includes(fields, 'time');
return pacote.packument(packageName, npmConfig).then(result =>
fields.reduce((accum, field) => Object.assign(
accum,
{
[field]: field.startsWith('dist-tags.') ?
result.versions[_.get(result, field)] :
result[field]
}
), {})
);
}
async function packument(spec) {
return pacote.packument(spec, OPTS);
}
async function syncPackage(token, pkg, progress) {
progress(`${pkg} start`);
const json = await pacote.packument(pkg);
const createPackage = await fetch(
`${REG}/packages/package/legacy@${HOST}/${enc(pkg)}`,
{
method: 'PUT',
body: '{}',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`
}
}
);
if (createPackage.status > 399) {
progress(
`${pkg} saw ${createPackage.status}: ${await createPackage.text()}`