Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add always option to replaceRegistryHost #164

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,15 @@ class FetcherBase {
this[_readPackageJson] = readPackageJsonFast
}

// config values: npmjs (default), never
this.replaceRegistryHost = opts.replaceRegistryHost === 'never' ? 'never' : 'npmjs'
// 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
}

this.defaultTag = opts.defaultTag || 'latest'
this.registry = removeTrailingSlashes(opts.registry || 'https://registry.npmjs.org')
Expand Down
12 changes: 8 additions & 4 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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 magic = /^https?:\/\/registry\.npmjs\.org\//
const magicHost = /^https?:\/\/registry\.npmjs\.org\//
fritzy marked this conversation as resolved.
Show resolved Hide resolved
const allHost = /^https?:\/\/([^/?#]+)(?:[/?#]|$)/

const _cacheFetches = Symbol.for('pacote.Fetcher._cacheFetches')
const _headers = Symbol('_headers')
Expand All @@ -14,9 +15,12 @@ class RemoteFetcher extends Fetcher {
super(spec, opts)
this.resolved = this.spec.fetchSpec
if (this.replaceRegistryHost === 'npmjs'
&& magic.test(this.resolved)
&& !magic.test(this.registry + '/')) {
this.resolved = this.resolved.replace(magic, this.registry + '/')
&& magicHost.test(this.resolved)
&& !magicHost.test(this.registry + '/')) {
this.resolved = this.resolved.replace(magicHost, this.registry + '/')
} else if (this.replaceRegistryHost === 'always'
&& allHost.test(this.resolved)) {
this.resolved = this.resolved.replace(allHost, this.registry + '/')
}

// nam is a fermented pork sausage that is good to eat
Expand Down
7 changes: 7 additions & 0 deletions test/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,10 @@ t.test('replace opts never', t => {
t.equal(f.replaceRegistryHost, 'never')
t.end()
})
t.test('replace opts always', t => {
const f = new FileFetcher('pkg.tgz', {
replaceRegistryHost: 'always',
})
t.equal(f.replaceRegistryHost, 'always')
t.end()
})
46 changes: 46 additions & 0 deletions test/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,49 @@ t.test('get a timeout error from the http fetch', t => {
code: /FETCH_ERROR|ERR_SOCKET_TIMEOUT/,
})
})

t.test('option replaceRegistryHost', rhTest => {
const tnock = require('./fixtures/tnock')
const { join } = require('path')
const abbrevTGZ = fs.readFileSync(abbrev)

rhTest.test('host should be replaced if set to always on npmjs registry', async ct => {
const testdir = t.testdir()
tnock(ct, 'https://registry.github.com')
.get('/abbrev/-/abbrev-1.1.1.tgz')
.reply(200, abbrevTGZ)

const fetcher = new RemoteFetcher(
'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
{
registry: 'https://registry.github.com',
cache: join(testdir, 'cache'),
fullReadJson: true,
replaceRegistryHost: 'always',
})
ct.equal(fetcher.replaceRegistryHost, 'always')
const tarball = await fetcher.tarball()
ct.match(tarball, abbrevTGZ)
})

rhTest.test('host should be replaced if set to always on other registry', async ct => {
const testdir = t.testdir()
tnock(ct, 'https://registry.github.com')
.get('/abbrev/-/abbrev-1.1.1.tgz')
.reply(200, abbrevTGZ)

const fetcher = new RemoteFetcher(
'https://registry.somethingelse.org/abbrev/-/abbrev-1.1.1.tgz',
{
registry: 'https://registry.github.com',
cache: join(testdir, 'cache'),
fullReadJson: true,
replaceRegistryHost: 'always',
})
ct.equal(fetcher.replaceRegistryHost, 'always')
const tarball = await fetcher.tarball()
ct.match(tarball, abbrevTGZ)
})

rhTest.end()
})