From 08a44c9566a747b641ebf4b2a3ffffe3765e6e16 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 28 Feb 2022 10:50:30 +0100
Subject: [PATCH] refactor(npm): npmrc handling (#14443)

---
 .../npm/__snapshots__/index.spec.ts.snap      | 24 +++++++++----------
 lib/datasource/npm/npmrc.ts                   | 10 +++++---
 lib/datasource/types.ts                       | 12 ++++------
 .../lookup/__snapshots__/index.spec.ts.snap   |  2 +-
 4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/lib/datasource/npm/__snapshots__/index.spec.ts.snap b/lib/datasource/npm/__snapshots__/index.spec.ts.snap
index ae5b688b28..f19f219d02 100644
--- a/lib/datasource/npm/__snapshots__/index.spec.ts.snap
+++ b/lib/datasource/npm/__snapshots__/index.spec.ts.snap
@@ -57,7 +57,7 @@ Array [
 exports[`datasource/npm/index should fetch package info from npm 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -95,7 +95,7 @@ Array [
 exports[`datasource/npm/index should handle foobar 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -133,7 +133,7 @@ Array [
 exports[`datasource/npm/index should handle no time 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -170,7 +170,7 @@ Array [
 exports[`datasource/npm/index should not send an authorization header if public package 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -208,7 +208,7 @@ Array [
 exports[`datasource/npm/index should parse repo url (string) 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -241,7 +241,7 @@ Array [
 exports[`datasource/npm/index should parse repo url 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -274,7 +274,7 @@ Array [
 exports[`datasource/npm/index should replace any environment variable in npmrc 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.from-env.com/",
+  "registryUrl": "https://registry.from-env.com",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -311,14 +311,14 @@ Array [
 
 exports[`datasource/npm/index should return deprecated 1`] = `
 Object {
-  "deprecationMessage": "On registry \`https://registry.npmjs.org/\`, the \\"latest\\" version of dependency \`foobar\` has the following deprecation notice:
+  "deprecationMessage": "On registry \`https://registry.npmjs.org\`, the \\"latest\\" version of dependency \`foobar\` has the following deprecation notice:
 
 \`This is deprecated\`
 
 Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.",
   "deprecationSource": "npm",
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -339,7 +339,7 @@ Marking the latest version of an npm package as deprecated results in the entire
 `;
 
 exports[`datasource/npm/index should return deprecated 2`] = `
-"On registry \`https://registry.npmjs.org/\`, the \\"latest\\" version of dependency \`foobar\` has the following deprecation notice:
+"On registry \`https://registry.npmjs.org\`, the \\"latest\\" version of dependency \`foobar\` has the following deprecation notice:
 
 \`This is deprecated\`
 
@@ -409,7 +409,7 @@ Array [
 exports[`datasource/npm/index should send an authorization header if provided 1`] = `
 Object {
   "name": "@foobar/core",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
@@ -523,7 +523,7 @@ Array [
 exports[`datasource/npm/index should use default registry if missing from npmrc 1`] = `
 Object {
   "name": "foobar",
-  "registryUrl": "https://registry.npmjs.org/",
+  "registryUrl": "https://registry.npmjs.org",
   "releases": Array [
     Object {
       "releaseTimestamp": "2018-05-06T05:21:53.000Z",
diff --git a/lib/datasource/npm/npmrc.ts b/lib/datasource/npm/npmrc.ts
index 33d3841b38..3ae20d9d74 100644
--- a/lib/datasource/npm/npmrc.ts
+++ b/lib/datasource/npm/npmrc.ts
@@ -165,7 +165,7 @@ export function setNpmrc(input?: string): void {
   }
 }
 
-export function resolvePackage(packageName: string): PackageResolution {
+export function resolveRegistryUrl(packageName: string): string {
   let registryUrl = defaultRegistryUrls[0];
   for (const rule of packageRules) {
     const { matchPackagePrefixes, registryUrls } = rule;
@@ -176,9 +176,13 @@ export function resolvePackage(packageName: string): PackageResolution {
       registryUrl = registryUrls[0];
     }
   }
-  registryUrl = ensureTrailingSlash(registryUrl);
+  return registryUrl;
+}
+
+export function resolvePackage(packageName: string): PackageResolution {
+  const registryUrl = resolveRegistryUrl(packageName);
   const packageUrl = url.resolve(
-    registryUrl,
+    ensureTrailingSlash(registryUrl),
     encodeURIComponent(packageName).replace(regEx(/^%40/), '@')
   );
   return { packageUrl, registryUrl };
diff --git a/lib/datasource/types.ts b/lib/datasource/types.ts
index fe854e784b..9a9073dc0b 100644
--- a/lib/datasource/types.ts
+++ b/lib/datasource/types.ts
@@ -17,18 +17,16 @@ export interface DigestConfig {
   currentDigest?: string;
 }
 
-export interface ReleasesConfigBase {
+export interface GetReleasesConfig {
   npmrc?: string;
-  defaultRegistryUrls?: string[];
-  registryUrls?: string[];
-}
-
-export interface GetReleasesConfig extends ReleasesConfigBase {
   lookupName: string;
   registryUrl?: string;
 }
 
-export interface GetPkgReleasesConfig extends ReleasesConfigBase {
+export interface GetPkgReleasesConfig {
+  npmrc?: string;
+  defaultRegistryUrls?: string[];
+  registryUrls?: string[];
   datasource: string;
   depName: string;
   lookupName?: string;
diff --git a/lib/workers/repository/process/lookup/__snapshots__/index.spec.ts.snap b/lib/workers/repository/process/lookup/__snapshots__/index.spec.ts.snap
index 21be5395b7..0d0f214b09 100644
--- a/lib/workers/repository/process/lookup/__snapshots__/index.spec.ts.snap
+++ b/lib/workers/repository/process/lookup/__snapshots__/index.spec.ts.snap
@@ -277,7 +277,7 @@ Object {
   "changelogUrl": undefined,
   "currentVersion": "1.3.0",
   "dependencyUrl": undefined,
-  "deprecationMessage": "On registry \`https://registry.npmjs.org/\`, the \\"latest\\" version of dependency \`q2\` has the following deprecation notice:
+  "deprecationMessage": "On registry \`https://registry.npmjs.org\`, the \\"latest\\" version of dependency \`q2\` has the following deprecation notice:
 
 \`true\`
 
-- 
GitLab