Files
pnpm/resolving/git-resolver/src/parseBareSpecifier.ts
Zoltan Kochan 4a36b9a110 refactor: rename internal packages to @pnpm/<domain>.<leaf> convention (#10997)
## Summary

Rename all internal packages so their npm names follow the `@pnpm/<domain>.<leaf>` convention, matching their directory structure. Also rename directories to remove redundancy and improve clarity.

### Bulk rename (94 packages)

All `@pnpm/` packages now derive their name from their directory path using dot-separated segments. Exceptions: `packages/`, `__utils__/`, and `pnpm/artifacts/` keep leaf names only.

### Directory renames (removing redundant prefixes)

- `cli/cli-meta` → `cli/meta`, `cli/cli-utils` → `cli/utils`
- `config/config` → `config/reader`, `config/config-writer` → `config/writer`
- `fetching/fetching-types` → `fetching/types`
- `lockfile/lockfile-to-pnp` → `lockfile/to-pnp`
- `store/store-connection-manager` → `store/connection-manager`
- `store/store-controller-types` → `store/controller-types`
- `store/store-path` → `store/path`

### Targeted renames (clarity improvements)

- `deps/dependency-path` → `deps/path` (`@pnpm/deps.path`)
- `deps/calc-dep-state` → `deps/graph-hasher` (`@pnpm/deps.graph-hasher`)
- `deps/inspection/dependencies-hierarchy` → `deps/inspection/tree-builder` (`@pnpm/deps.inspection.tree-builder`)
- `bins/link-bins` → `bins/linker`, `bins/remove-bins` → `bins/remover`, `bins/package-bins` → `bins/resolver`
- `installing/get-context` → `installing/context`
- `store/package-store` → `store/controller`
- `pkg-manifest/manifest-utils` → `pkg-manifest/utils`

### Manifest reader/writer renames

- `workspace/read-project-manifest` → `workspace/project-manifest-reader` (`@pnpm/workspace.project-manifest-reader`)
- `workspace/write-project-manifest` → `workspace/project-manifest-writer` (`@pnpm/workspace.project-manifest-writer`)
- `workspace/read-manifest` → `workspace/workspace-manifest-reader` (`@pnpm/workspace.workspace-manifest-reader`)
- `workspace/manifest-writer` → `workspace/workspace-manifest-writer` (`@pnpm/workspace.workspace-manifest-writer`)

### Workspace package renames

- `workspace/find-packages` → `workspace/projects-reader`
- `workspace/find-workspace-dir` → `workspace/root-finder`
- `workspace/resolve-workspace-range` → `workspace/range-resolver`
- `workspace/filter-packages-from-dir` merged into `workspace/filter-workspace-packages` → `workspace/projects-filter`

### Domain moves

- `pkg-manifest/read-project-manifest` → `workspace/project-manifest-reader`
- `pkg-manifest/write-project-manifest` → `workspace/project-manifest-writer`
- `pkg-manifest/exportable-manifest` → `releasing/exportable-manifest`

### Scope

- 1206 files changed
- Updated: package.json names/deps, TypeScript imports, tsconfig references, changeset files, renovate.json, test fixtures, import ordering
2026-03-17 21:50:40 +01:00

194 lines
5.9 KiB
TypeScript

// cspell:ignore sshurl
import urlLib, { URL } from 'node:url'
import type { AgentOptions } from '@pnpm/network.agent'
import { fetchWithAgent } from '@pnpm/network.fetch'
import { gracefulGit as git } from 'graceful-git'
import HostedGit from 'hosted-git-info'
export interface HostedPackageSpec {
fetchSpec: string
hosted?: {
type: string
user: string
project: string
committish: string
tarball: () => string | undefined
}
normalizedBareSpecifier: string
gitCommittish: string | null
gitRange?: string
path?: string
}
const gitProtocols = new Set([
'git',
'git+http',
'git+https',
'git+rsync',
'git+ftp',
'git+file',
'git+ssh',
'ssh',
])
export function parseBareSpecifier (bareSpecifier: string, opts: AgentOptions): null | (() => Promise<HostedPackageSpec>) {
const hosted = HostedGit.fromUrl(bareSpecifier)
if (hosted != null) {
return () => fromHostedGit(hosted, opts)
}
const colonsPos = bareSpecifier.indexOf(':')
if (colonsPos === -1) return null
const protocol = bareSpecifier.slice(0, colonsPos)
// Also detect http/https URLs ending in .git as git repositories
const isGitUrl = gitProtocols.has(protocol.toLocaleLowerCase()) ||
((protocol === 'http' || protocol === 'https') && /\.git(?:#|$)/.test(bareSpecifier))
if (protocol && isGitUrl) {
const correctBareSpecifier = correctUrl(bareSpecifier)
const url = new URL(correctBareSpecifier)
if (!url?.protocol) return null
const hash = (url.hash?.length > 1) ? decodeURIComponent(url.hash.slice(1)) : null
return async () => ({
fetchSpec: urlToFetchSpec(url),
normalizedBareSpecifier: bareSpecifier,
...parseGitParams(hash),
})
}
return null
}
function urlToFetchSpec (url: URL): string {
url.hash = ''
const fetchSpec = urlLib.format(url)
if (fetchSpec.startsWith('git+')) {
return fetchSpec.slice(4)
}
return fetchSpec
}
async function fromHostedGit (hosted: any, agentOptions: AgentOptions): Promise<HostedPackageSpec> { // eslint-disable-line
let fetchSpec: string | null = null
// try git/https url before fallback to ssh url
const gitHttpsUrl = hosted.https({ noCommittish: true, noGitPlus: true })
if (gitHttpsUrl && await isRepoPublic(gitHttpsUrl, agentOptions) && await accessRepository(gitHttpsUrl)) {
fetchSpec = gitHttpsUrl
} else {
const gitSshUrl = hosted.ssh({ noCommittish: true })
if (gitSshUrl && await accessRepository(gitSshUrl)) {
fetchSpec = gitSshUrl
}
}
if (!fetchSpec) {
const httpsUrl: string | null = hosted.https({ noGitPlus: true, noCommittish: true })
if (httpsUrl) {
if ((hosted.auth || !await isRepoPublic(httpsUrl, agentOptions)) && await accessRepository(httpsUrl)) {
return {
fetchSpec: httpsUrl,
hosted: {
...hosted,
_fill: hosted._fill,
tarball: undefined,
},
normalizedBareSpecifier: `git+${httpsUrl}`,
...parseGitParams(hosted.committish),
}
} else {
try {
// when git ls-remote private repo, it asks for login credentials.
// use HTTP HEAD request to test whether this is a private repo, to avoid login prompt.
// this is very similar to yarn classic's behavior.
// npm instead tries git ls-remote directly which prompts user for login credentials.
// HTTP HEAD on https://domain/user/repo, strip out ".git"
const response = await fetchWithAgent(httpsUrl.replace(/\.git$/, ''), { method: 'HEAD', follow: 0, retry: { retries: 0 }, agentOptions })
if (response.ok) {
fetchSpec = httpsUrl
}
} catch {
// ignore
}
}
}
}
if (!fetchSpec) {
// use ssh url for likely private repo
fetchSpec = hosted.sshurl({ noCommittish: true })
}
return {
fetchSpec: fetchSpec!,
hosted: {
...hosted,
_fill: hosted._fill,
tarball: hosted.tarball,
},
normalizedBareSpecifier: hosted.shortcut(),
...parseGitParams(hosted.committish),
}
}
async function isRepoPublic (httpsUrl: string, agentOptions: AgentOptions): Promise<boolean> {
try {
const response = await fetchWithAgent(httpsUrl.replace(/\.git$/, ''), { method: 'HEAD', follow: 0, retry: { retries: 0 }, agentOptions })
return response.ok
} catch {
return false
}
}
async function accessRepository (repository: string): Promise<boolean> {
try {
await git(['ls-remote', '--exit-code', repository, 'HEAD'], { retries: 0 })
return true
} catch {
return false
}
}
type GitParsedParams = Pick<HostedPackageSpec, 'gitCommittish' | 'gitRange' | 'path'>
function parseGitParams (committish: string | null): GitParsedParams {
const result: GitParsedParams = { gitCommittish: null }
if (!committish) {
return result
}
const params = committish.split('&')
for (const param of params) {
if (param.length >= 7 && param.slice(0, 7) === 'semver:') {
result.gitRange = param.slice(7)
} else if (param.slice(0, 5) === 'path:') {
result.path = param.slice(5)
} else {
result.gitCommittish = param
}
}
return result
}
// handle SCP-like URLs
// see https://github.com/yarnpkg/yarn/blob/5682d55/src/util/git.js#L103
function correctUrl (gitUrl: string): string {
let _gitUrl = gitUrl.replace(/^git\+/, '')
if (_gitUrl.startsWith('ssh://')) {
const hashIndex = _gitUrl.indexOf('#')
let hash = ''
if (hashIndex !== -1) {
hash = _gitUrl.slice(hashIndex)
_gitUrl = _gitUrl.slice(0, hashIndex)
}
const [auth, ...pathname] = _gitUrl.slice(6).split('/')
const [, host] = auth.split('@')
if (host.includes(':') && !/:\d+$/.test(host)) {
const authArr = auth.split(':')
const protocol = gitUrl.split('://')[0]
gitUrl = `${protocol}://${authArr.slice(0, -1).join(':') + '/' + authArr[authArr.length - 1]}${pathname.length ? '/' + pathname.join('/') : ''}${hash}`
}
}
return gitUrl
}