Skip to content

Commit

Permalink
feat: replaceRegistryHost can now be a hostname (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzy committed Jun 1, 2022
1 parent d69e524 commit a9a4cdd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
13 changes: 4 additions & 9 deletions lib/fetcher.js
Expand Up @@ -105,15 +105,10 @@ class FetcherBase {
this[_readPackageJson] = readPackageJsonFast
}

// config values: npmjs (default), never, always
// we don't want to mutate the original value
if (opts.replaceRegistryHost !== 'never'
&& opts.replaceRegistryHost !== 'always'
) {
this.replaceRegistryHost = 'npmjs'
} else {
this.replaceRegistryHost = opts.replaceRegistryHost
}
// rrh is a registry hostname or 'never' or 'always'
// defaults to registry.npmjs.org
this.replaceRegistryHost = (!opts.replaceRegistryHost || opts.replaceRegistryHost === 'npmjs') ?
'registry.npmjs.org' : opts.replaceRegistryHost

this.defaultTag = opts.defaultTag || 'latest'
this.registry = removeTrailingSlashes(opts.registry || 'https://registry.npmjs.org')
Expand Down
10 changes: 3 additions & 7 deletions lib/remote.js
Expand Up @@ -4,8 +4,6 @@ const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
const pacoteVersion = require('../package.json').version
const fetch = require('npm-registry-fetch')
const Minipass = require('minipass')
// The default registry URL is a string of great magic.
const magicHost = 'https://registry.npmjs.org'

const _cacheFetches = Symbol.for('pacote.Fetcher._cacheFetches')
const _headers = Symbol('_headers')
Expand All @@ -14,11 +12,9 @@ class RemoteFetcher extends Fetcher {
super(spec, opts)
this.resolved = this.spec.fetchSpec
const resolvedURL = new URL(this.resolved)
if (
(this.replaceRegistryHost === 'npmjs'
&& resolvedURL.origin === magicHost)
|| this.replaceRegistryHost === 'always'
) {
if (this.replaceRegistryHost !== 'never'
&& (this.replaceRegistryHost === 'always'
|| this.replaceRegistryHost === resolvedURL.host)) {
this.resolved = new URL(resolvedURL.pathname, this.registry).href
}

Expand Down
4 changes: 2 additions & 2 deletions test/fetcher.js
Expand Up @@ -518,10 +518,10 @@ t.test('set integrity, pick default algo', t => {
t.end()
})

t.test('replace opts defaults to npmjs', t => {
t.test('replace opts defaults to default registry', t => {
const f = new FileFetcher('pkg.tgz', {
})
t.equal(f.replaceRegistryHost, 'npmjs')
t.equal(f.replaceRegistryHost, 'registry.npmjs.org')
t.end()
})
t.test('replace opts never', t => {
Expand Down
4 changes: 2 additions & 2 deletions test/registry.js
Expand Up @@ -444,7 +444,7 @@ t.test('option replaceRegistryHost', rhTest => {
cache: join(testdir, 'cache'),
fullReadJson: true,
})
ct.equal(fetcher.replaceRegistryHost, 'npmjs')
ct.equal(fetcher.replaceRegistryHost, 'registry.npmjs.org')
const manifest = await fetcher.manifest()
ct.equal(manifest.dist.tarball, 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz')
const tarball = await fetcher.tarball()
Expand All @@ -465,7 +465,7 @@ t.test('option replaceRegistryHost', rhTest => {
fullReadJson: true,
replaceRegistryHost: 'npmjs',
})
ct.equal(fetcher.replaceRegistryHost, 'npmjs')
ct.equal(fetcher.replaceRegistryHost, 'registry.npmjs.org')
const manifest = await fetcher.manifest()
ct.equal(manifest.dist.tarball, 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz')
const tarball = await fetcher.tarball()
Expand Down

0 comments on commit a9a4cdd

Please sign in to comment.