From c226e38f599c1fd0abd30ae98b2139b8e628e739 Mon Sep 17 00:00:00 2001 From: Svante Bengtson Date: Mon, 5 Aug 2024 14:53:52 +0200 Subject: [PATCH] Allow custom registries without trailing slash (#1440) --- src/package-managers/npm.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/package-managers/npm.ts b/src/package-managers/npm.ts index 54e2c78a..f42d64b0 100644 --- a/src/package-managers/npm.ts +++ b/src/package-managers/npm.ts @@ -59,7 +59,24 @@ const fetchPartialPackument = async ( accept: opts.fullMetadata ? fullDoc : corgiDoc, ...opts.headers, } - const url = new URL(encodeURIComponent(name), registry) + const url = new URL( + // since the registry API expects /package or /package/version encoding + // scoped packages is needed as to not treat the package scope as the full + // package name and the actual package name as the version/dist-tag + encodeURIComponent(name), + // the WhatWG URL standard, when given a base URL to place the first + // parameter relative to, will find the dirname of the base, treating the + // last segment as a file name and not a directory name if it isn't + // terminated by a / and thus remove it before adding the first argument + // to the URL. + // this is undesirable for registries configured without a trailing slash + // in the npm config since, for example looking up the package @foo/bar + // will give the following results given these configured registry URL:s + // https://example.com/npm => https://example.com/%40foo%2fbar + // https://example.com/npm/ => https://example.com/npm/%40foo%2fbar + // however, like npm itself does there should be leniency allowed in this. + registry.endsWith('/') ? registry : `${registry}/`, + ) if (version) { url.pathname += `/${version}` }