Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const version = semver.clean(selector);
if (version === null)
throw new ReportError(MessageName.RESOLVER_NOT_FOUND, `The npm semver resolver got selected, but the version isn't semver`);
const registryData = await npmHttpUtils.get(npmHttpUtils.getIdentUrl(locator), {
configuration: opts.project.configuration,
ident: locator,
json: true,
});
if (!Object.prototype.hasOwnProperty.call(registryData, `versions`))
throw new ReportError(MessageName.REMOTE_INVALID, `Registry returned invalid data for - missing "versions" field`);
if (!Object.prototype.hasOwnProperty.call(registryData.versions, version))
throw new ReportError(MessageName.REMOTE_NOT_FOUND, `Registry failed to return reference "${version}"`);
const manifest = new Manifest();
manifest.load(registryData.versions[version]);
// Manually add node-gyp dependency if there is a script using it and not already set
// This is because the npm registry will automatically add a `node-gyp rebuild` install script
// in the metadata if there is not already an install script and a binding.gyp file exists.
// Also, node-gyp is not always set as a dependency in packages, so it will also be added if used in scripts.
if (!manifest.dependencies.has(NODE_GYP_IDENT.identHash) && !manifest.peerDependencies.has(NODE_GYP_IDENT.identHash)) {
for (const value of manifest.scripts.values()) {
if (value.match(NODE_GYP_MATCH)) {
manifest.dependencies.set(NODE_GYP_IDENT.identHash, structUtils.makeDescriptor(NODE_GYP_IDENT, `latest`));
opts.report.reportWarning(MessageName.NODE_GYP_INJECTED, `${structUtils.prettyLocator(opts.project.configuration, locator)}: Implicit dependencies on node-gyp are discouraged`);
break;
}
}
}, async report => {
// Not an error if --tolerate-republish is set
if (this.tolerateRepublish) {
try {
const registryData = await npmHttpUtils.get(npmHttpUtils.getIdentUrl(ident), {
configuration,
registry,
ident,
json: true,
});
if (!Object.prototype.hasOwnProperty.call(registryData, `versions`))
throw new ReportError(MessageName.REMOTE_INVALID, `Registry returned invalid data for - missing "versions" field`);
if (Object.prototype.hasOwnProperty.call(registryData.versions, version)) {
report.reportWarning(MessageName.UNNAMED, `Registry already knows about version ${version}; skipping.`);
return;
}
} catch (error) {
if (error.name !== `HTTPError`) {
throw error;
} else if (error.response.statusCode !== 404) {
throw new ReportError(MessageName.NETWORK_ERROR, `The remote server answered with HTTP ${error.response.statusCode} ${error.response.statusMessage}`);
}
}
}
await packUtils.prepareForPack(workspace, {report}, async () => {
const files = await packUtils.genPackList(workspace);
function extractError(val: any) {
let err;
try {
err = extractErrorImpl(val);
} catch (caught) {
if (typeof caught === `string`) {
throw new ReportError(MessageName.PROLOG_UNKNOWN_ERROR, `Unknown error: ${val} (note: ${caught})`);
} else {
throw caught;
}
}
if (typeof err.line !== `undefined` && typeof err.column !== `undefined`)
err.message += ` at line ${err.line}, column ${err.column}`;
return err;
}
function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration}: {authType?: AuthType, configuration: Configuration}) {
const registryConfiguration = npmConfigUtils.getRegistryConfiguration(registry, {configuration});
const effectiveConfiguration = registryConfiguration || configuration;
const mustAuthenticate = shouldAuthenticate(effectiveConfiguration, authType);
if (!mustAuthenticate)
return null;
if (effectiveConfiguration.get(`npmAuthToken`))
return `Bearer ${effectiveConfiguration.get(`npmAuthToken`)}`;
if (effectiveConfiguration.get(`npmAuthIdent`))
return `Basic ${effectiveConfiguration.get(`npmAuthIdent`)}`;
if (mustAuthenticate && authType !== AuthType.BEST_EFFORT) {
throw new ReportError(MessageName.AUTHENTICATION_NOT_FOUND ,`No authentication configured for request`);
} else {
return null;
}
}
if (value instanceof pl.type.Term) {
if (value.args.length === 0)
return value.id;
switch (value.indicator) {
case `throw/1`:
return extractErrorImpl(value.args[0]);
case `error/1`:
return extractErrorImpl(value.args[0]);
case `error/2`:
return Object.assign(extractErrorImpl(value.args[0]), ...extractErrorImpl(value.args[1]));
case `syntax_error/1`:
return new ReportError(MessageName.PROLOG_SYNTAX_ERROR, `Syntax error: ${extractErrorImpl(value.args[0])}`);
case `existence_error/2`:
return new ReportError(MessageName.PROLOG_EXISTENCE_ERROR, `Existence error: ${extractErrorImpl(value.args[0])} ${extractErrorImpl(value.args[1])} not found`);
case `line/1`:
return {line: extractErrorImpl(value.args[0])};
case `column/1`:
return {column: extractErrorImpl(value.args[0])};
case `found/1`:
return {found: extractErrorImpl(value.args[0])};
case `./2`:
return [extractErrorImpl(value.args[0])].concat(extractErrorImpl(value.args[1]));
case `//2`:
return `${extractErrorImpl(value.args[0])}/${extractErrorImpl(value.args[1])}`;
}
}
throw `couldn't pretty print because of unsupported node ${value}`;
}
if (!file.includes(MERGE_CONFLICT_START))
return false;
if (immutable)
throw new ReportError(MessageName.AUTOMERGE_IMMUTABLE, `Cannot autofix a lockfile when running an immutable install`);
const [left, right] = getVariants(file);
let parsedLeft;
let parsedRight;
try {
parsedLeft = parseSyml(left);
parsedRight = parseSyml(right);
} catch (error) {
throw new ReportError(MessageName.AUTOMERGE_FAILED_TO_PARSE, `The individual variants of the lockfile failed to parse`);
}
const merged = Object.assign({}, parsedLeft, parsedRight);
const serialized = stringifySyml(merged);
await xfs.changeFilePromise(lockfilePath, serialized);
return true;
}
static getLocatorUrl(locator: Locator) {
const version = semver.clean(locator.reference.slice(PROTOCOL.length));
if (version === null)
throw new ReportError(MessageName.RESOLVER_NOT_FOUND, `The npm semver resolver got selected, but the version isn't semver`);
return `${npmHttpUtils.getIdentUrl(locator)}/-/${locator.name}-${version}.tgz`;
}
}
if (value instanceof pl.type.Num)
return value.value;
if (value instanceof pl.type.Term) {
if (value.args.length === 0)
return value.id;
switch (value.indicator) {
case `throw/1`:
return extractErrorImpl(value.args[0]);
case `error/1`:
return extractErrorImpl(value.args[0]);
case `error/2`:
return Object.assign(extractErrorImpl(value.args[0]), ...extractErrorImpl(value.args[1]));
case `syntax_error/1`:
return new ReportError(MessageName.PROLOG_SYNTAX_ERROR, `Syntax error: ${extractErrorImpl(value.args[0])}`);
case `existence_error/2`:
return new ReportError(MessageName.PROLOG_EXISTENCE_ERROR, `Existence error: ${extractErrorImpl(value.args[0])} ${extractErrorImpl(value.args[1])} not found`);
case `line/1`:
return {line: extractErrorImpl(value.args[0])};
case `column/1`:
return {column: extractErrorImpl(value.args[0])};
case `found/1`:
return {found: extractErrorImpl(value.args[0])};
case `./2`:
return [extractErrorImpl(value.args[0])].concat(extractErrorImpl(value.args[1]));
case `//2`:
return `${extractErrorImpl(value.args[0])}/${extractErrorImpl(value.args[1])}`;
}
}
throw `couldn't pretty print because of unsupported node ${value}`;
async getCandidates(descriptor: Descriptor, dependencies: unknown, opts: ResolveOptions) {
const tag = descriptor.range.slice(PROTOCOL.length);
const registryData = await npmHttpUtils.get(npmHttpUtils.getIdentUrl(descriptor), {
configuration: opts.project.configuration,
ident: descriptor,
json: true,
});
if (!Object.prototype.hasOwnProperty.call(registryData, `dist-tags`))
throw new ReportError(MessageName.REMOTE_INVALID, `Registry returned invalid data - missing "dist-tags" field`);
const distTags = registryData[`dist-tags`];
if (!Object.prototype.hasOwnProperty.call(distTags, tag))
throw new ReportError(MessageName.REMOTE_NOT_FOUND, `Registry failed to return tag "${tag}"`);
const version = distTags[tag];
const versionLocator = structUtils.makeLocator(descriptor, `${PROTOCOL}${version}`);
const archiveUrl = registryData.versions[version].dist.tarball;
if (NpmSemverFetcher.isConventionalTarballUrl(versionLocator, archiveUrl, {configuration: opts.project.configuration})) {
return [versionLocator];
} else {
return [structUtils.bindLocator(versionLocator, {__archiveUrl: archiveUrl})];
}