@@ -44,8 +44,12 @@ export function getRegistryFromContent(content: string, scope: string | null): s
4444 }
4545}
4646
47- function getNpmrcPaths ( cwd : string ) : string [ ] {
48- return [ join ( cwd , '.npmrc' ) , join ( homedir ( ) , '.npmrc' ) ]
47+ /**
48+ * `.npmrc` files to consult, most specific first. Without a `cwd` only the user's
49+ * own file is read, for requests a project should not be able to redirect.
50+ */
51+ function getNpmrcPaths ( cwd : string | undefined ) : string [ ] {
52+ return cwd ? [ join ( cwd , '.npmrc' ) , join ( homedir ( ) , '.npmrc' ) ] : [ join ( homedir ( ) , '.npmrc' ) ]
4953}
5054
5155async function getRegistryFromFile ( paths : string [ ] , scope : string | null ) {
@@ -72,7 +76,7 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {
7276 return null
7377}
7478
75- async function getRegistry ( scope : string | null , cwd : string ) : Promise < string > {
79+ async function getRegistry ( scope : string | null , cwd : string | undefined ) : Promise < string > {
7680 const registry = process . env . COREPACK_NPM_REGISTRY
7781 || await getRegistryFromFile ( getNpmrcPaths ( cwd ) , scope )
7882 || PUBLIC_REGISTRY
@@ -128,7 +132,7 @@ function readCredentials(config: Record<string, string | undefined>, registry: s
128132 }
129133}
130134
131- async function getCredentials ( registry : RegistryMeta [ 'registry' ] , cwd : string ) : Promise < Pick < RegistryMeta , 'authToken' | 'authorization' > > {
135+ async function getCredentials ( registry : RegistryMeta [ 'registry' ] , cwd : string | undefined ) : Promise < Pick < RegistryMeta , 'authToken' | 'authorization' > > {
132136 for ( const npmrcPath of getNpmrcPaths ( cwd ) ) {
133137 let fd : FileHandle | undefined
134138 try {
@@ -152,11 +156,19 @@ async function getCredentials(registry: RegistryMeta['registry'], cwd: string):
152156 return { authToken : null , authorization : null }
153157}
154158
155- export async function detectNpmRegistry ( scope : string | null , cwd = process . cwd ( ) ) : Promise < RegistryMeta > {
156- const registry = await getRegistry ( scope , cwd )
159+ /**
160+ * Registry and credentials for `scope`, from the project's `.npmrc` in `cwd`
161+ * (defaulting to the working directory) and then the user's. Pass `null` as
162+ * `cwd` to ignore project configuration entirely: a project `.npmrc` may name any
163+ * host and, as in `npm`, reference environment variables in its credentials, so
164+ * a request the user did not ask for should not be steered by it.
165+ */
166+ export async function detectNpmRegistry ( scope : string | null , cwd : string | null = process . cwd ( ) ) : Promise < RegistryMeta > {
167+ const paths = cwd ?? undefined
168+ const registry = await getRegistry ( scope , paths )
157169
158170 return {
159171 registry,
160- ...await getCredentials ( registry , cwd ) ,
172+ ...await getCredentials ( registry , paths ) ,
161173 }
162174}
0 commit comments