Skip to content

Commit d29d556

Browse files
committed
feat: COREPACK_NPM_REGISTRY defaults to the regitry of npm
1 parent aefde28 commit d29d556

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ same major line. Should you need to upgrade to a new major, use an explicit
341341
(useful for commands like `yarn init`).
342342

343343
- `COREPACK_NPM_REGISTRY` sets the registry base url used when retrieving
344-
package managers from npm. Default value is `https://registry.npmjs.org`
344+
package managers from npm. Default value is the result of command
345+
`npm config get registry`, which is typically `https://registry.npmjs.org`
346+
unless it has been customized.
345347

346348
- `COREPACK_NPM_TOKEN` sets a Bearer token authorization header when connecting
347349
to a npm type registry.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"esbuild": "^0.25.0",
3333
"eslint": "^9.22.0",
3434
"proxy-from-env": "^1.1.0",
35+
"registry-url": "^7.1.0",
3536
"semver": "^7.6.3",
3637
"supports-color": "^10.0.0",
3738
"tar": "^7.4.0",

sources/corepackUtils.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
224224
let signatures: Array<{keyid: string, sig: string}>;
225225
let integrity: string;
226226
let binPath: string | null = null;
227+
const registryUrl = npmRegistryUtils.getRegistryUrl();
227228
if (locatorIsASupportedPackageManager) {
228229
url = spec.url.replace(`{}`, version);
229230
if (process.env.COREPACK_NPM_REGISTRY) {
@@ -234,17 +235,17 @@ export async function installVersion(installTarget: string, locator: Locator, {s
234235
binPath = registry.bin;
235236
}
236237
}
237-
url = url.replace(
238-
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
239-
() => process.env.COREPACK_NPM_REGISTRY!,
240-
);
241238
}
239+
url = url.replace(
240+
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
241+
() => registryUrl,
242+
);
242243
} else {
243244
url = decodeURIComponent(version);
244-
if (process.env.COREPACK_NPM_REGISTRY && url.startsWith(npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL)) {
245+
if (url.startsWith(npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL)) {
245246
url = url.replace(
246247
npmRegistryUtils.DEFAULT_NPM_REGISTRY_URL,
247-
() => process.env.COREPACK_NPM_REGISTRY!,
248+
() => registryUrl,
248249
);
249250
}
250251
}

sources/httpUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {once} from 'events';
44
import {stderr, stdin} from 'process';
55
import {Readable} from 'stream';
66

7-
import {DEFAULT_NPM_REGISTRY_URL} from './npmRegistryUtils';
7+
import {getRegistryUrl} from './npmRegistryUtils';
88

99
async function fetch(input: string | URL, init?: RequestInit) {
1010
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
@@ -29,7 +29,7 @@ async function fetch(input: string | URL, init?: RequestInit) {
2929
input.username = input.password = ``;
3030
}
3131

32-
if (input.origin === (process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL) && process.env.COREPACK_NPM_TOKEN) {
32+
if (process.env.COREPACK_NPM_TOKEN && input.origin === getRegistryUrl()) {
3333
headers = {
3434
...headers,
3535
authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`,

sources/npmRegistryUtils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {UsageError} from 'clipanion';
22
import {createVerify} from 'crypto';
3+
import registryUrl, {defaultUrl} from 'registry-url';
34

45
import defaultConfig from '../config.json';
56

@@ -11,10 +12,20 @@ import * as httpUtils from './httpUtils';
1112
export const DEFAULT_HEADERS: Record<string, string> = {
1213
[`Accept`]: `application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8`,
1314
};
14-
export const DEFAULT_NPM_REGISTRY_URL = `https://registry.npmjs.org`;
15+
16+
const normalize = (url: string) => url.endsWith(`/`) ? url.slice(0, -1) : url;
17+
18+
export const DEFAULT_NPM_REGISTRY_URL = normalize(defaultUrl);
19+
20+
export function getRegistryUrl() {
21+
if (process.env.COREPACK_NPM_REGISTRY) {
22+
return process.env.COREPACK_NPM_REGISTRY;
23+
}
24+
return normalize(registryUrl());
25+
}
1526

1627
export async function fetchAsJson(packageName: string, version?: string) {
17-
const npmRegistryUrl = process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL;
28+
const npmRegistryUrl = getRegistryUrl();
1829

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

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ __metadata:
13151315
esbuild: "npm:^0.25.0"
13161316
eslint: "npm:^9.22.0"
13171317
proxy-from-env: "npm:^1.1.0"
1318+
registry-url: "npm:^7.1.0"
13181319
semver: "npm:^7.6.3"
13191320
supports-color: "npm:^10.0.0"
13201321
tar: "npm:^7.4.0"
@@ -2003,6 +2004,13 @@ __metadata:
20032004
languageName: node
20042005
linkType: hard
20052006

2007+
"find-up-simple@npm:^1.0.1":
2008+
version: 1.0.1
2009+
resolution: "find-up-simple@npm:1.0.1"
2010+
checksum: 10c0/ad34de157b7db925d50ff78302fefb28e309f3bc947c93ffca0f9b0bccf9cf1a2dc57d805d5c94ec9fc60f4838f5dbdfd2a48ecd77c23015fa44c6dd5f60bc40
2011+
languageName: node
2012+
linkType: hard
2013+
20062014
"find-up@npm:^5.0.0":
20072015
version: 5.0.0
20082016
resolution: "find-up@npm:5.0.0"
@@ -2377,6 +2385,13 @@ __metadata:
23772385
languageName: node
23782386
linkType: hard
23792387

2388+
"ini@npm:^5.0.0":
2389+
version: 5.0.0
2390+
resolution: "ini@npm:5.0.0"
2391+
checksum: 10c0/657491ce766cbb4b335ab221ee8f72b9654d9f0e35c32fe5ff2eb7ab8c5ce72237ff6456555b50cde88e6507a719a70e28e327b450782b4fc20c90326ec8c1a8
2392+
languageName: node
2393+
linkType: hard
2394+
23802395
"ini@npm:~1.3.0":
23812396
version: 1.3.8
23822397
resolution: "ini@npm:1.3.8"
@@ -3443,6 +3458,16 @@ __metadata:
34433458
languageName: node
34443459
linkType: hard
34453460

3461+
"registry-url@npm:^7.1.0":
3462+
version: 7.1.0
3463+
resolution: "registry-url@npm:7.1.0"
3464+
dependencies:
3465+
find-up-simple: "npm:^1.0.1"
3466+
ini: "npm:^5.0.0"
3467+
checksum: 10c0/b9ccddc4d079cc3e6cc9b137aef1a71aaab02d04785890b8f86d1b63eb158e0198e17a444b3bba2986eadc0beff18fae1be058665b4197deec9422650100fb24
3468+
languageName: node
3469+
linkType: hard
3470+
34463471
"resolve-from@npm:^4.0.0":
34473472
version: 4.0.0
34483473
resolution: "resolve-from@npm:4.0.0"

0 commit comments

Comments
 (0)