Skip to content

Commit b81e92c

Browse files
authored
fix: strip trailing slashes from COREPACK_NPM_REGISTRY (#871)
A `COREPACK_NPM_REGISTRY` value with a trailing slash produced request URLs with a double slash (e.g. `https://registry.npmjs.org//pnpm`). registry.npmjs.org tolerates this, but some registries such as registry.npmmirror.com reject it with a 404. Normalize the registry URL by removing trailing slashes before building request URLs. Fixes: #711 Signed-off-by: Paul Bouchon <mail@bitpshr.net>
1 parent f0f4ee1 commit b81e92c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎sources/npmRegistryUtils.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export const DEFAULT_HEADERS: Record<string, string> = {
1414
export const DEFAULT_NPM_REGISTRY_URL = `https://registry.npmjs.org`;
1515

1616
export async function fetchAsJson(packageName: string, version?: string) {
17-
const npmRegistryUrl = process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL;
17+
// Strip any trailing slashes so a `COREPACK_NPM_REGISTRY` with a trailing
18+
// slash does not produce a double slash in the request URL (some registries,
19+
// e.g. registry.npmmirror.com, reject `//package` with a 404).
20+
const npmRegistryUrl = (process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL).replace(/\/+$/, ``);
1821

1922
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
2023
throw new UsageError(`Network access disabled by the environment; can't reach npm repository ${npmRegistryUrl}`);

‎tests/npmRegistryUtils.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ describe(`npm registry utils fetchAsJson`, () => {
3535
expect(httpFetchAsJson).lastCalledWith(`${process.env.COREPACK_NPM_REGISTRY}/package-name`, {headers: DEFAULT_HEADERS});
3636
});
3737

38+
it(`does not produce a double slash when COREPACK_NPM_REGISTRY has a trailing slash`, async () => {
39+
// `process.env` is reset after each tests in setupTests.js.
40+
process.env.COREPACK_NPM_REGISTRY = `https://registry.example.org/`;
41+
await fetchAsJson(`package-name`);
42+
43+
expect(httpFetchAsJson).toBeCalled();
44+
expect(httpFetchAsJson).lastCalledWith(`https://registry.example.org/package-name`, {headers: DEFAULT_HEADERS});
45+
});
46+
3847
it(`adds authorization header with bearer token if COREPACK_NPM_TOKEN is set`, async () => {
3948
// `process.env` is reset after each tests in setupTests.js.
4049
process.env.COREPACK_NPM_TOKEN = `foo`;

0 commit comments

Comments
 (0)