Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sources/npmRegistryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const DEFAULT_HEADERS: Record<string, string> = {
export const DEFAULT_NPM_REGISTRY_URL = `https://registry.npmjs.org`;

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

if (process.env.COREPACK_ENABLE_NETWORK === `0`)
throw new UsageError(`Network access disabled by the environment; can't reach npm repository ${npmRegistryUrl}`);
Expand Down
9 changes: 9 additions & 0 deletions tests/npmRegistryUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ describe(`npm registry utils fetchAsJson`, () => {
expect(httpFetchAsJson).lastCalledWith(`${process.env.COREPACK_NPM_REGISTRY}/package-name`, {headers: DEFAULT_HEADERS});
});

it(`does not produce a double slash when COREPACK_NPM_REGISTRY has a trailing slash`, async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_NPM_REGISTRY = `https://registry.example.org/`;
await fetchAsJson(`package-name`);

expect(httpFetchAsJson).toBeCalled();
expect(httpFetchAsJson).lastCalledWith(`https://registry.example.org/package-name`, {headers: DEFAULT_HEADERS});
});

it(`adds authorization header with bearer token if COREPACK_NPM_TOKEN is set`, async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_NPM_TOKEN = `foo`;
Expand Down