Skip to content

Commit db9c411

Browse files
fix(connector): force npmjs registry for login and npm CLI ops
1 parent 7f2fc1a commit db9c411

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

cli/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as p from '@clack/prompts'
66
import { defineCommand, runMain } from 'citty'
77
import { serve } from 'srvx'
88
import { createConnectorApp, generateToken, CONNECTOR_VERSION } from './server.ts'
9-
import { getNpmUser } from './npm-client.ts'
9+
import { getNpmUser, NPM_REGISTRY_URL } from './npm-client.ts'
1010
import { initLogger, showToken, logInfo, logWarning, logError } from './logger.ts'
1111

1212
const DEFAULT_PORT = 31415
@@ -15,7 +15,7 @@ const DEV_FRONTEND_URL = 'http://127.0.0.1:3000/'
1515

1616
async function runNpmLogin(): Promise<boolean> {
1717
return new Promise(resolve => {
18-
const child = spawn('npm', ['login'], {
18+
const child = spawn('npm', ['login', `--registry=${NPM_REGISTRY_URL}`], {
1919
stdio: 'inherit',
2020
shell: true,
2121
})

cli/src/npm-client.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { PackageNameSchema, UsernameSchema, OrgNameSchema, ScopeTeamSchema } fro
1010
import { logCommand, logSuccess, logError, logDebug } from './logger.ts'
1111

1212
const execFileAsync = promisify(execFile)
13+
export const NPM_REGISTRY_URL = 'https://registry.npmjs.org/'
14+
15+
export function createNpmEnv(overrides: Record<string, string> = {}): Record<string, string> {
16+
return {
17+
...(process.env as Record<string, string>),
18+
...overrides,
19+
FORCE_COLOR: '0',
20+
npm_config_registry: NPM_REGISTRY_URL,
21+
}
22+
}
1323

1424
/**
1525
* Validates an npm package name using the official npm validation package
@@ -191,10 +201,7 @@ async function execNpmInteractive(
191201
let authUrlTimeout: ReturnType<typeof setTimeout> | null = null
192202
let authUrlTimedOut = false
193203

194-
const env: Record<string, string> = {
195-
...(process.env as Record<string, string>),
196-
FORCE_COLOR: '0',
197-
}
204+
const env = createNpmEnv()
198205

199206
// When openUrls is false, tell npm not to open the browser.
200207
// npm still prints the auth URL and polls doneUrl
@@ -330,7 +337,7 @@ async function execNpm(args: string[], options: ExecNpmOptions = {}): Promise<Np
330337
// On Unix, we keep it false for better security and performance
331338
const { stdout, stderr } = await execFileAsync('npm', npmArgs, {
332339
timeout: 60000,
333-
env: { ...process.env, FORCE_COLOR: '0' },
340+
env: createNpmEnv(),
334341
shell: process.platform === 'win32',
335342
})
336343

@@ -606,7 +613,7 @@ export async function packageInit(
606613
const { stdout, stderr } = await execFileAsync('npm', npmArgs, {
607614
timeout: 60000,
608615
cwd: tempDir,
609-
env: { ...process.env, FORCE_COLOR: '0' },
616+
env: createNpmEnv(),
610617
shell: process.platform === 'win32',
611618
})
612619

test/unit/cli/npm-client.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
validateScopeTeam,
66
validatePackageName,
77
extractUrls,
8+
createNpmEnv,
9+
NPM_REGISTRY_URL,
810
} from '../../../cli/src/npm-client'
911

1012
describe('validateUsername', () => {
@@ -184,3 +186,15 @@ describe('extractUrls', () => {
184186
expect(extractUrls(npmOutput)).toEqual(['https://www.npmjs.com/login?next=/login/cli/abc123'])
185187
})
186188
})
189+
190+
describe('createNpmEnv', () => {
191+
it('enforces npmjs registry for all npm commands', () => {
192+
const env = createNpmEnv()
193+
expect(env.npm_config_registry).toBe(NPM_REGISTRY_URL)
194+
})
195+
196+
it('does not allow overriding enforced registry', () => {
197+
const env = createNpmEnv({ npm_config_registry: 'https://registry.npmmirror.com/' })
198+
expect(env.npm_config_registry).toBe(NPM_REGISTRY_URL)
199+
})
200+
})

0 commit comments

Comments
 (0)