Skip to content

Commit b51d156

Browse files
authored
feat(arborist): extend replace-registry-host with URL prefix matching (#6110) (#9672)
Resolves where registries with a path component (e.g. https://myregistry.example.com/npm/b) caused resolved URLs in the lockfile to have their path duplicated when reify rewrote them. Rewrote `#registryResolved` to unify all match-and-rewrite logic into a single pass. A single `new URL()` parse of `replaceRegistryHost` now drives all three match modes: - **Keyword / bare hostname** (`never`, `always`, `npmjs`, `registry.npmjs.org`): behaviour unchanged. `new URL()` throws for keywords, leaving `matchURL` null and falling through to the existing host-comparison or keyword short-circuit. - **Host-only URL** (e.g. `https://old.example.com/`): trailing slash normalises to an empty `matchPath`; treated identically to a bare hostname — only the host is replaced, and the registry path is prepended if not already present. - **Full URL with path** (e.g. `https://old.example.com/npm/b`): when the resolved URL's host and path both match the prefix, the entire matched prefix is replaced with the configured registry's full URL (host + path). The remainder of the resolved path is appended without duplication. If the resolved URL's path does not start with the configured prefix, `#registryResolved` returns the URL unchanged. Updated the `hint` and `description` for `replace-registry-host` to document the new full-URL-with-path mode and the host-only-URL mode. Added three integration tests using `tnock` (mismatched URL → nock throws, making URL assertions implicit and precise): 1. Full URL prefix match: `replaceRegistryHost=https://old.example.com/npm/b`, registry `https://new.example.com/npm/a` — verifies the old `/npm/b` prefix is replaced with `/npm/a`, not doubled to `/npm/a/npm/b/…`. 2. Host-only URL: `replaceRegistryHost=https://old.example.com/` — verifies only the host is swapped and the resolved path is preserved. 3. Path mismatch: `replaceRegistryHost=https://old.example.com/npm/c` but tarball under `/npm/b` — verifies the URL is left unchanged when the path prefix does not match. <!-- What / Why --> <!-- Describe the request in detail. What it does and why it's being changed. --> ## References - Fixes #6110
1 parent 024e6d9 commit b51d156

4 files changed

Lines changed: 194 additions & 26 deletions

File tree

‎tap-snapshots/test/lib/docs.js.test.cjs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,14 @@ registry (https://registry.npmjs.org) to the configured registry. If set to
18131813
"never", then use the registry value. If set to "always", then replace the
18141814
registry host with the configured host every time.
18151815
1816-
You may also specify a bare hostname (e.g., "registry.npmjs.org").
1816+
You may also specify a bare hostname (e.g., "registry.npmjs.org") to only
1817+
replace URLs coming from that host.
1818+
1819+
You may also specify a full URL including a path (e.g.,
1820+
"https://old-registry.example.com/npm/path"). In that case, resolved URLs
1821+
whose host and path begin with that prefix will have the entire prefix
1822+
replaced with the configured registry URL (host and path), without
1823+
duplicating path segments.
18171824
18181825
18191826

‎workspaces/arborist/lib/arborist/reify.js‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,36 +1014,42 @@ module.exports = cls => class Reifier extends cls {
10141014
// the default reg as the magical animal that it has been.
10151015
try {
10161016
const resolvedURL = hgi.parseUrl(resolved)
1017+
const registryURL = new URL(this.registry)
1018+
const registryPath = registryURL.pathname.replace(/\/$/, '')
10171019

1018-
if ((this.options.replaceRegistryHost === resolvedURL.hostname) ||
1019-
this.options.replaceRegistryHost === 'always') {
1020-
const registryURL = new URL(this.registry)
1020+
let matchURL = null
1021+
try {
1022+
matchURL = new URL(this.options.replaceRegistryHost)
1023+
} catch {
1024+
// keep matchURL null
1025+
}
10211026

1022-
// Replace the host with the registry host while keeping the path intact
1023-
resolvedURL.hostname = registryURL.hostname
1024-
resolvedURL.port = registryURL.port
1025-
resolvedURL.protocol = registryURL.protocol
1027+
const matchHost = matchURL?.hostname ?? this.options.replaceRegistryHost
1028+
const matchPath = matchURL?.pathname.replace(/\/$/, '') ?? null
1029+
const hasPathPrefix = (pathname, prefix) =>
1030+
pathname === prefix || pathname.startsWith(`${prefix}/`)
10261031

1027-
// Make sure we don't double-include the path if it's already there
1028-
const registryPath = registryURL.pathname.replace(/\/$/, '')
1032+
const hostMatches = this.options.replaceRegistryHost === 'always' || matchHost === resolvedURL.hostname
1033+
const pathMatches = !matchPath || hasPathPrefix(resolvedURL.pathname, matchPath)
10291034

1030-
if (registryPath && registryPath !== '/') {
1031-
// Check if the resolved pathname already starts with the registry path
1032-
// We need to ensure it's a proper path prefix, not just a string prefix
1033-
// e.g., registry path '/npm' should not match '/npm-run-path'
1034-
const hasRegistryPath = resolvedURL.pathname === registryPath ||
1035-
resolvedURL.pathname.startsWith(registryPath + '/')
1035+
if (!hostMatches || !pathMatches) {
1036+
return resolved
1037+
}
10361038

1037-
if (!hasRegistryPath) {
1038-
// Since hostname is changed, we need to ensure the registry path is included
1039-
resolvedURL.pathname = registryPath + resolvedURL.pathname
1040-
}
1041-
}
1039+
resolvedURL.protocol = registryURL.protocol
1040+
resolvedURL.hostname = registryURL.hostname
1041+
resolvedURL.port = registryURL.port
10421042

1043-
return resolvedURL.toString()
1043+
if (matchPath) {
1044+
// full-URL prefix: swap old path prefix for the registry path
1045+
resolvedURL.pathname = registryPath + resolvedURL.pathname.slice(matchPath.length)
1046+
} else if (registryPath && !hasPathPrefix(resolvedURL.pathname, registryPath)) {
1047+
// host-only: prepend registry path if not already present
1048+
resolvedURL.pathname = registryPath + resolvedURL.pathname
10441049
}
1045-
return resolved
1046-
} catch (e) {
1050+
1051+
return resolvedURL.toString()
1052+
} catch {
10471053
// if we could not parse the url at all then returning nothing
10481054
// here means it will get removed from the tree in the next step
10491055
return undefined

‎workspaces/arborist/test/arborist/reify.js‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,154 @@ t.test('should preserve exact ranges, missing actual tree', async (t) => {
37923792
await t.resolves(arb.reify(), 'reify should complete successfully')
37933793
})
37943794

3795+
// Validates both URL-prefix matching modes for replace-registry-host:
3796+
// A) full URL with path → entire prefix (host + old path) is replaced with registry URL
3797+
// B) host-only URL → only the host is swapped, resolved path is left unchanged
3798+
t.test('replace-registry-host as full URL with path replaces entire prefix', async t => {
3799+
const packument = JSON.stringify({
3800+
_id: 'abbrev',
3801+
_rev: 'lkjadflkjasdf',
3802+
name: 'abbrev',
3803+
'dist-tags': { latest: '1.1.1' },
3804+
versions: {
3805+
'1.1.1': {
3806+
name: 'abbrev',
3807+
version: '1.1.1',
3808+
dist: {
3809+
// tarball lives under /npm/b on the old host
3810+
tarball: 'https://old.example.com/npm/b/abbrev/-/abbrev-1.1.1.tgz',
3811+
},
3812+
},
3813+
},
3814+
})
3815+
3816+
const testdir = t.testdir({
3817+
project: {
3818+
'package.json': JSON.stringify({
3819+
name: 'myproject',
3820+
version: '1.0.0',
3821+
dependencies: { abbrev: '1.1.1' },
3822+
}),
3823+
},
3824+
})
3825+
3826+
// packument lookup goes through new host + new path prefix
3827+
tnock(t, 'https://new.example.com')
3828+
.get('/npm/a/abbrev')
3829+
.reply(200, packument)
3830+
3831+
// tarball: /npm/b prefix replaced with /npm/a — NOT /npm/a/npm/b/…
3832+
tnock(t, 'https://new.example.com')
3833+
.get('/npm/a/abbrev/-/abbrev-1.1.1.tgz')
3834+
.reply(200, abbrevTGZ)
3835+
3836+
const arb = new Arborist({
3837+
path: resolve(testdir, 'project'),
3838+
registry: 'https://new.example.com/npm/a',
3839+
cache: resolve(testdir, 'cache'),
3840+
replaceRegistryHost: 'https://old.example.com/npm/b',
3841+
})
3842+
3843+
await t.resolves(arb.reify(), 'prefix is replaced without duplication')
3844+
})
3845+
3846+
t.test('replace-registry-host as host-only URL leaves resolved path unchanged', async t => {
3847+
const packument = JSON.stringify({
3848+
_id: 'abbrev',
3849+
_rev: 'lkjadflkjasdf',
3850+
name: 'abbrev',
3851+
'dist-tags': { latest: '1.1.1' },
3852+
versions: {
3853+
'1.1.1': {
3854+
name: 'abbrev',
3855+
version: '1.1.1',
3856+
dist: {
3857+
// tarball has its own path on the old host
3858+
tarball: 'https://old.example.com/abbrev/-/abbrev-1.1.1.tgz',
3859+
},
3860+
},
3861+
},
3862+
})
3863+
3864+
const testdir = t.testdir({
3865+
project: {
3866+
'package.json': JSON.stringify({
3867+
name: 'myproject',
3868+
version: '1.0.0',
3869+
dependencies: { abbrev: '1.1.1' },
3870+
}),
3871+
},
3872+
})
3873+
3874+
// packument lookup: host swapped, path unchanged
3875+
tnock(t, 'https://new.example.com')
3876+
.get('/abbrev')
3877+
.reply(200, packument)
3878+
3879+
// tarball: host swapped only — /abbrev/-/… path is preserved as-is
3880+
tnock(t, 'https://new.example.com')
3881+
.get('/abbrev/-/abbrev-1.1.1.tgz')
3882+
.reply(200, abbrevTGZ)
3883+
3884+
const arb = new Arborist({
3885+
path: resolve(testdir, 'project'),
3886+
registry: 'https://new.example.com/',
3887+
cache: resolve(testdir, 'cache'),
3888+
// trailing slash only → host-only replacement, path left unchanged
3889+
replaceRegistryHost: 'https://old.example.com/',
3890+
})
3891+
3892+
await t.resolves(arb.reify(), 'only host is replaced; resolved path is unchanged')
3893+
})
3894+
3895+
t.test('replace-registry-host as full URL with path does not replace non-matching path', async t => {
3896+
const packument = JSON.stringify({
3897+
_id: 'abbrev',
3898+
_rev: 'lkjadflkjasdf',
3899+
name: 'abbrev',
3900+
'dist-tags': { latest: '1.1.1' },
3901+
versions: {
3902+
'1.1.1': {
3903+
name: 'abbrev',
3904+
version: '1.1.1',
3905+
dist: {
3906+
// tarball is under /npm/b, but replaceRegistryHost specifies /npm/c
3907+
tarball: 'https://old.example.com/npm/b/abbrev/-/abbrev-1.1.1.tgz',
3908+
},
3909+
},
3910+
},
3911+
})
3912+
3913+
const testdir = t.testdir({
3914+
project: {
3915+
'package.json': JSON.stringify({
3916+
name: 'myproject',
3917+
version: '1.0.0',
3918+
dependencies: { abbrev: '1.1.1' },
3919+
}),
3920+
},
3921+
})
3922+
3923+
// packument comes from configured registry
3924+
tnock(t, 'https://new.example.com')
3925+
.get('/npm/a/abbrev')
3926+
.reply(200, packument)
3927+
3928+
// tarball is NOT replaced because /npm/b does not start with /npm/c
3929+
tnock(t, 'https://old.example.com')
3930+
.get('/npm/b/abbrev/-/abbrev-1.1.1.tgz')
3931+
.reply(200, abbrevTGZ)
3932+
3933+
const arb = new Arborist({
3934+
path: resolve(testdir, 'project'),
3935+
registry: 'https://new.example.com/npm/a',
3936+
cache: resolve(testdir, 'cache'),
3937+
replaceRegistryHost: 'https://old.example.com/npm/c',
3938+
})
3939+
3940+
await t.resolves(arb.reify(), 'non-matching path prefix leaves resolved URL unchanged')
3941+
})
3942+
37953943
t.test('allowRemote=none allows registry tarball under registry path without trailing slash', async t => {
37963944
const abbrevPackument5 = JSON.stringify({
37973945
_id: 'abbrev',

‎workspaces/config/lib/definitions/definitions.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ const definitions = {
20562056
}),
20572057
'replace-registry-host': new Definition('replace-registry-host', {
20582058
default: 'npmjs',
2059-
hint: '<npmjs|never|always> | hostname',
2059+
hint: '<npmjs|never|always> | hostname | url',
20602060
type: ['npmjs', 'never', 'always', String],
20612061
description: `
20622062
Defines behavior for replacing the registry host in a lockfile with the
@@ -2067,7 +2067,14 @@ const definitions = {
20672067
"never", then use the registry value. If set to "always", then replace the
20682068
registry host with the configured host every time.
20692069
2070-
You may also specify a bare hostname (e.g., "registry.npmjs.org").
2070+
You may also specify a bare hostname (e.g., "registry.npmjs.org") to only
2071+
replace URLs coming from that host.
2072+
2073+
You may also specify a full URL including a path (e.g.,
2074+
"https://old-registry.example.com/npm/path"). In that case, resolved URLs
2075+
whose host and path begin with that prefix will have the entire prefix
2076+
replaced with the configured registry URL (host and path), without
2077+
duplicating path segments.
20712078
`,
20722079
flatten,
20732080
}),

0 commit comments

Comments
 (0)