Skip to content

Commit 3021ad6

Browse files
feat(arborist): extend replace-registry-host with URL prefix matching (#6110) (#9694)
Backport of #9672 to `release/v11`. Co-authored-by: Justin Clark <[email protected]>
1 parent 598ffdb commit 3021ad6

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
@@ -1694,7 +1694,14 @@ registry (https://registry.npmjs.org) to the configured registry. If set to
16941694
"never", then use the registry value. If set to "always", then replace the
16951695
registry host with the configured host every time.
16961696
1697-
You may also specify a bare hostname (e.g., "registry.npmjs.org").
1697+
You may also specify a bare hostname (e.g., "registry.npmjs.org") to only
1698+
replace URLs coming from that host.
1699+
1700+
You may also specify a full URL including a path (e.g.,
1701+
"https://old-registry.example.com/npm/path"). In that case, resolved URLs
1702+
whose host and path begin with that prefix will have the entire prefix
1703+
replaced with the configured registry URL (host and path), without
1704+
duplicating path segments.
16981705
16991706
17001707

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

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

1001-
if ((this.options.replaceRegistryHost === resolvedURL.hostname) ||
1002-
this.options.replaceRegistryHost === 'always') {
1003-
const registryURL = new URL(this.registry)
1003+
let matchURL = null
1004+
try {
1005+
matchURL = new URL(this.options.replaceRegistryHost)
1006+
} catch {
1007+
// keep matchURL null
1008+
}
10041009

1005-
// Replace the host with the registry host while keeping the path intact
1006-
resolvedURL.hostname = registryURL.hostname
1007-
resolvedURL.port = registryURL.port
1008-
resolvedURL.protocol = registryURL.protocol
1010+
const matchHost = matchURL?.hostname ?? this.options.replaceRegistryHost
1011+
const matchPath = matchURL?.pathname.replace(/\/$/, '') ?? null
1012+
const hasPathPrefix = (pathname, prefix) =>
1013+
pathname === prefix || pathname.startsWith(`${prefix}/`)
10091014

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

1013-
if (registryPath && registryPath !== '/') {
1014-
// Check if the resolved pathname already starts with the registry path
1015-
// We need to ensure it's a proper path prefix, not just a string prefix
1016-
// e.g., registry path '/npm' should not match '/npm-run-path'
1017-
const hasRegistryPath = resolvedURL.pathname === registryPath ||
1018-
resolvedURL.pathname.startsWith(registryPath + '/')
1018+
if (!hostMatches || !pathMatches) {
1019+
return resolved
1020+
}
10191021

1020-
if (!hasRegistryPath) {
1021-
// Since hostname is changed, we need to ensure the registry path is included
1022-
resolvedURL.pathname = registryPath + resolvedURL.pathname
1023-
}
1024-
}
1022+
resolvedURL.protocol = registryURL.protocol
1023+
resolvedURL.hostname = registryURL.hostname
1024+
resolvedURL.port = registryURL.port
10251025

1026-
return resolvedURL.toString()
1026+
if (matchPath) {
1027+
// full-URL prefix: swap old path prefix for the registry path
1028+
resolvedURL.pathname = registryPath + resolvedURL.pathname.slice(matchPath.length)
1029+
} else if (registryPath && !hasPathPrefix(resolvedURL.pathname, registryPath)) {
1030+
// host-only: prepend registry path if not already present
1031+
resolvedURL.pathname = registryPath + resolvedURL.pathname
10271032
}
1028-
return resolved
1029-
} catch (e) {
1033+
1034+
return resolvedURL.toString()
1035+
} catch {
10301036
// if we could not parse the url at all then returning nothing
10311037
// here means it will get removed from the tree in the next step
10321038
return undefined

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

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

3908+
// Validates both URL-prefix matching modes for replace-registry-host:
3909+
// A) full URL with path → entire prefix (host + old path) is replaced with registry URL
3910+
// B) host-only URL → only the host is swapped, resolved path is left unchanged
3911+
t.test('replace-registry-host as full URL with path replaces entire prefix', async t => {
3912+
const packument = JSON.stringify({
3913+
_id: 'abbrev',
3914+
_rev: 'lkjadflkjasdf',
3915+
name: 'abbrev',
3916+
'dist-tags': { latest: '1.1.1' },
3917+
versions: {
3918+
'1.1.1': {
3919+
name: 'abbrev',
3920+
version: '1.1.1',
3921+
dist: {
3922+
// tarball lives under /npm/b on the old host
3923+
tarball: 'https://old.example.com/npm/b/abbrev/-/abbrev-1.1.1.tgz',
3924+
},
3925+
},
3926+
},
3927+
})
3928+
3929+
const testdir = t.testdir({
3930+
project: {
3931+
'package.json': JSON.stringify({
3932+
name: 'myproject',
3933+
version: '1.0.0',
3934+
dependencies: { abbrev: '1.1.1' },
3935+
}),
3936+
},
3937+
})
3938+
3939+
// packument lookup goes through new host + new path prefix
3940+
tnock(t, 'https://new.example.com')
3941+
.get('/npm/a/abbrev')
3942+
.reply(200, packument)
3943+
3944+
// tarball: /npm/b prefix replaced with /npm/a — NOT /npm/a/npm/b/…
3945+
tnock(t, 'https://new.example.com')
3946+
.get('/npm/a/abbrev/-/abbrev-1.1.1.tgz')
3947+
.reply(200, abbrevTGZ)
3948+
3949+
const arb = new Arborist({
3950+
path: resolve(testdir, 'project'),
3951+
registry: 'https://new.example.com/npm/a',
3952+
cache: resolve(testdir, 'cache'),
3953+
replaceRegistryHost: 'https://old.example.com/npm/b',
3954+
})
3955+
3956+
await t.resolves(arb.reify(), 'prefix is replaced without duplication')
3957+
})
3958+
3959+
t.test('replace-registry-host as host-only URL leaves resolved path unchanged', async t => {
3960+
const packument = JSON.stringify({
3961+
_id: 'abbrev',
3962+
_rev: 'lkjadflkjasdf',
3963+
name: 'abbrev',
3964+
'dist-tags': { latest: '1.1.1' },
3965+
versions: {
3966+
'1.1.1': {
3967+
name: 'abbrev',
3968+
version: '1.1.1',
3969+
dist: {
3970+
// tarball has its own path on the old host
3971+
tarball: 'https://old.example.com/abbrev/-/abbrev-1.1.1.tgz',
3972+
},
3973+
},
3974+
},
3975+
})
3976+
3977+
const testdir = t.testdir({
3978+
project: {
3979+
'package.json': JSON.stringify({
3980+
name: 'myproject',
3981+
version: '1.0.0',
3982+
dependencies: { abbrev: '1.1.1' },
3983+
}),
3984+
},
3985+
})
3986+
3987+
// packument lookup: host swapped, path unchanged
3988+
tnock(t, 'https://new.example.com')
3989+
.get('/abbrev')
3990+
.reply(200, packument)
3991+
3992+
// tarball: host swapped only — /abbrev/-/… path is preserved as-is
3993+
tnock(t, 'https://new.example.com')
3994+
.get('/abbrev/-/abbrev-1.1.1.tgz')
3995+
.reply(200, abbrevTGZ)
3996+
3997+
const arb = new Arborist({
3998+
path: resolve(testdir, 'project'),
3999+
registry: 'https://new.example.com/',
4000+
cache: resolve(testdir, 'cache'),
4001+
// trailing slash only → host-only replacement, path left unchanged
4002+
replaceRegistryHost: 'https://old.example.com/',
4003+
})
4004+
4005+
await t.resolves(arb.reify(), 'only host is replaced; resolved path is unchanged')
4006+
})
4007+
4008+
t.test('replace-registry-host as full URL with path does not replace non-matching path', async t => {
4009+
const packument = JSON.stringify({
4010+
_id: 'abbrev',
4011+
_rev: 'lkjadflkjasdf',
4012+
name: 'abbrev',
4013+
'dist-tags': { latest: '1.1.1' },
4014+
versions: {
4015+
'1.1.1': {
4016+
name: 'abbrev',
4017+
version: '1.1.1',
4018+
dist: {
4019+
// tarball is under /npm/b, but replaceRegistryHost specifies /npm/c
4020+
tarball: 'https://old.example.com/npm/b/abbrev/-/abbrev-1.1.1.tgz',
4021+
},
4022+
},
4023+
},
4024+
})
4025+
4026+
const testdir = t.testdir({
4027+
project: {
4028+
'package.json': JSON.stringify({
4029+
name: 'myproject',
4030+
version: '1.0.0',
4031+
dependencies: { abbrev: '1.1.1' },
4032+
}),
4033+
},
4034+
})
4035+
4036+
// packument comes from configured registry
4037+
tnock(t, 'https://new.example.com')
4038+
.get('/npm/a/abbrev')
4039+
.reply(200, packument)
4040+
4041+
// tarball is NOT replaced because /npm/b does not start with /npm/c
4042+
tnock(t, 'https://old.example.com')
4043+
.get('/npm/b/abbrev/-/abbrev-1.1.1.tgz')
4044+
.reply(200, abbrevTGZ)
4045+
4046+
const arb = new Arborist({
4047+
path: resolve(testdir, 'project'),
4048+
registry: 'https://new.example.com/npm/a',
4049+
cache: resolve(testdir, 'cache'),
4050+
replaceRegistryHost: 'https://old.example.com/npm/c',
4051+
})
4052+
4053+
await t.resolves(arb.reify(), 'non-matching path prefix leaves resolved URL unchanged')
4054+
})
4055+
39084056
t.test('allowRemote=none allows registry tarball under registry path without trailing slash', async t => {
39094057
const abbrevPackument5 = JSON.stringify({
39104058
_id: 'abbrev',

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ const definitions = {
19301930
}),
19311931
'replace-registry-host': new Definition('replace-registry-host', {
19321932
default: 'npmjs',
1933-
hint: '<npmjs|never|always> | hostname',
1933+
hint: '<npmjs|never|always> | hostname | url',
19341934
type: ['npmjs', 'never', 'always', String],
19351935
description: `
19361936
Defines behavior for replacing the registry host in a lockfile with the
@@ -1941,7 +1941,14 @@ const definitions = {
19411941
"never", then use the registry value. If set to "always", then replace the
19421942
registry host with the configured host every time.
19431943
1944-
You may also specify a bare hostname (e.g., "registry.npmjs.org").
1944+
You may also specify a bare hostname (e.g., "registry.npmjs.org") to only
1945+
replace URLs coming from that host.
1946+
1947+
You may also specify a full URL including a path (e.g.,
1948+
"https://old-registry.example.com/npm/path"). In that case, resolved URLs
1949+
whose host and path begin with that prefix will have the entire prefix
1950+
replaced with the configured registry URL (host and path), without
1951+
duplicating path segments.
19451952
`,
19461953
flatten,
19471954
}),

0 commit comments

Comments
 (0)