Skip to content

Commit 0673103

Browse files
committed
perf(dev,module): handle registry trailing slashes internally
1 parent 8ff1c67 commit 0673103

9 files changed

Lines changed: 15 additions & 22 deletions

File tree

packages/nuxt-cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"std-env": "^4.2.0",
8181
"tinyclip": "^1.0.1",
8282
"tinyexec": "^1.3.0",
83-
"ufo": "^1.6.4",
8483
"uqr": "^0.1.3",
8584
"verkit": "^0.3.1"
8685
},

packages/nuxt-cli/src/commands/module/add.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { defineCommand } from 'citty'
1212
import { detectPackageManager, packageManagers } from 'nypm'
1313
import { resolve } from 'pathe'
1414
import { readPackageJSON } from 'pkg-types'
15-
import { joinURL } from 'ufo'
1615
import { findMaxSatisfying, satisfies } from 'verkit'
1716

1817
import { runCommandDef as runCommand } from '../../run-command'
@@ -435,7 +434,7 @@ async function resolveModule(moduleName: string, cwd: string, modulesDB: NuxtMod
435434
}
436435

437436
// TODO: spinner
438-
const pkgUrl = joinURL(meta.registry, `${pkgName}`)
437+
const pkgUrl = `${meta.registry}/${pkgName}`
439438
const pkgDetails = await fetchJson<any>(pkgUrl, { headers }).catch((err: unknown) => {
440439
logNetworkError(err, { url: pkgUrl, prefix: `Failed to fetch package details for ${styleText('cyan', pkgName)}.` })
441440
return null

packages/nuxt-cli/src/dev/utils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { debounce } from 'perfect-debounce'
2121
import { toNodeHandler } from 'srvx/node'
2222
import { provider } from 'std-env'
2323

24-
import { joinURL } from 'ufo'
2524
import { showBanner } from '../utils/banner'
2625
import { clearBuildDir } from '../utils/fs'
2726
import { loadKit } from '../utils/kit'
@@ -589,10 +588,9 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
589588
const nuxt = this.#currentNuxt
590589
if (!nuxt || !nuxt.server)
591590
return
592-
const viteHmrPath = joinURL(
593-
nuxt.options.app.baseURL.startsWith('./') ? nuxt.options.app.baseURL.slice(1) : nuxt.options.app.baseURL,
594-
nuxt.options.app.buildAssetsDir,
595-
)
591+
const baseURL = nuxt.options.app.baseURL.startsWith('./') ? nuxt.options.app.baseURL.slice(1) : nuxt.options.app.baseURL
592+
const assetsDir = nuxt.options.app.buildAssetsDir
593+
const viteHmrPath = `${baseURL.replace(/\/$/, '')}/${assetsDir.replace(/^\//, '')}`
596594
if (req.url?.startsWith(viteHmrPath)) {
597595
return // Skip for Vite HMR
598596
}

packages/nuxt-cli/src/utils/registry.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const PROTOCOL_RE = /^https?:\/\//
1111
const TRAILING_SLASH_RE = /\/$/
1212

1313
export interface RegistryMeta {
14+
/** Registry URL without a trailing slash, so paths can be appended directly. */
1415
registry: string
1516
authToken: string | null
1617
}
@@ -66,15 +67,16 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {
6667
}
6768

6869
async function getRegistry(scope: string | null, cwd: string): Promise<string> {
69-
if (process.env.COREPACK_NPM_REGISTRY) {
70-
return process.env.COREPACK_NPM_REGISTRY
71-
}
72-
return await getRegistryFromFile(getNpmrcPaths(cwd), scope) || 'https://registry.npmjs.org'
70+
const registry = process.env.COREPACK_NPM_REGISTRY
71+
|| await getRegistryFromFile(getNpmrcPaths(cwd), scope)
72+
|| 'https://registry.npmjs.org'
73+
74+
return registry.replace(TRAILING_SLASH_RE, '')
7375
}
7476

7577
async function getAuthToken(registry: RegistryMeta['registry'], cwd: string): Promise<RegistryMeta['authToken']> {
7678
const paths = getNpmrcPaths(cwd)
77-
const registryHost = registry.replace(PROTOCOL_RE, '').replace(TRAILING_SLASH_RE, '')
79+
const registryHost = registry.replace(PROTOCOL_RE, '')
7880
const authTokenKey = `//${registryHost}/:_authToken`
7981

8082
for (const npmrcPath of paths) {

packages/nuxt-cli/src/utils/update-check.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { styleText } from 'node:util'
44

55
import { readUser, updateUser } from 'rc9'
66
import { isCI, isTest, provider } from 'std-env'
7-
import { joinURL } from 'ufo'
87
import { isGreater, tryParse } from 'verkit'
98

109
import { fetchJson } from './fetch'
@@ -85,7 +84,7 @@ async function resolveLatestVersion(name: string): Promise<string | undefined> {
8584
let latest: string | undefined
8685
try {
8786
const { registry, authToken } = await detectNpmRegistry(null)
88-
latest = (await fetchJson<{ latest?: string }>(joinURL(registry, `-/package/${name}/dist-tags`), {
87+
latest = (await fetchJson<{ latest?: string }>(`${registry}/-/package/${name}/dist-tags`, {
8988
headers: authToken ? { Authorization: `Bearer ${authToken}` } : undefined,
9089
timeout: FETCH_TIMEOUT,
9190
retry: 0,

packages/nuxt-cli/src/utils/versions.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { readPackageJSON } from 'pkg-types'
2-
import { joinURL } from 'ufo'
32
import { coerce, findMaxSatisfying } from 'verkit'
43

54
import { resolveCatalogEntry } from './catalog'
@@ -44,7 +43,7 @@ export async function resolveRegistryVersion(pkg: string, range: string): Promis
4443
const scope = pkg.startsWith('@') ? pkg.split('/')[0]! : null
4544
const { registry, authToken } = await detectNpmRegistry(scope)
4645

47-
packument = await fetchJson(joinURL(registry, pkg), {
46+
packument = await fetchJson(`${registry}/${pkg}`, {
4847
headers: {
4948
// The abbreviated packument is a fraction of the size of the full one and
5049
// still carries every version and dist-tag.

packages/nuxt-cli/test/unit/utils/registry.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('detectNpmRegistry', () => {
7777
].join('\n'))
7878

7979
await expect(detectNpmRegistry(null, directory)).resolves.toEqual({
80-
registry: 'https://registry.example.com/',
80+
registry: 'https://registry.example.com',
8181
authToken: 'secret',
8282
})
8383
expect(process.env.COREPACK_NPM_REGISTRY).toBeUndefined()

packages/nuxt-cli/test/unit/utils/update.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('update check', () => {
156156
})
157157

158158
it('queries the configured registry with its auth token', async () => {
159-
registry.current = { registry: 'https://npm.example.com/', authToken: 'secret' }
159+
registry.current = { registry: 'https://npm.example.com', authToken: 'secret' }
160160
fetchMock.mockResolvedValue({ latest: '4.1.0' })
161161
await checkForNuxtUpdate('/project')
162162
expect(fetchMock).toHaveBeenCalledWith(

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)