Skip to content

Commit

Permalink
feat: add option to not replace magic registry host (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzy committed Apr 5, 2022
1 parent c11dce6 commit f519cf4
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/fetcher.js
Expand Up @@ -105,6 +105,9 @@ class FetcherBase {
this[_readPackageJson] = readPackageJsonFast
}

// config values: npmjs (default), never
this.replaceRegistryHost = opts.replaceRegistryHost === 'never' ? 'never' : 'npmjs'

this.defaultTag = opts.defaultTag || 'latest'
this.registry = removeTrailingSlashes(opts.registry || 'https://registry.npmjs.org')

Expand Down
4 changes: 3 additions & 1 deletion lib/remote.js
Expand Up @@ -13,7 +13,9 @@ class RemoteFetcher extends Fetcher {
constructor (spec, opts) {
super(spec, opts)
this.resolved = this.spec.fetchSpec
if (magic.test(this.resolved) && !magic.test(this.registry + '/')) {
if (this.replaceRegistryHost === 'npmjs'
&& magic.test(this.resolved)
&& !magic.test(this.registry + '/')) {
this.resolved = this.resolved.replace(magic, this.registry + '/')
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"@npmcli/template-oss": "3.2.2",
"hosted-git-info": "^5.0.0",
"mutate-fs": "^2.1.1",
"nock": "^13.2.4",
"npm-registry-mock": "^1.3.1",
"tap": "^16.0.1"
},
Expand Down
14 changes: 14 additions & 0 deletions test/fetcher.js
Expand Up @@ -517,3 +517,17 @@ t.test('set integrity, pick default algo', t => {
t.equal(g.pickIntegrityAlgorithm(), 'sha256')
t.end()
})

t.test('replace opts defaults to npmjs', t => {
const f = new FileFetcher('pkg.tgz', {
})
t.equal(f.replaceRegistryHost, 'npmjs')
t.end()
})
t.test('replace opts never', t => {
const f = new FileFetcher('pkg.tgz', {
replaceRegistryHost: 'never',
})
t.equal(f.replaceRegistryHost, 'never')
t.end()
})
14 changes: 14 additions & 0 deletions test/fixtures/tnock.js
@@ -0,0 +1,14 @@
'use strict'

const nock = require('nock')

module.exports = tnock
function tnock (t, host) {
const server = nock(host)
nock.disableNetConnect()
t.teardown(function () {
nock.enableNetConnect()
server.done()
})
return server
}
91 changes: 91 additions & 0 deletions test/registry.js
@@ -1,6 +1,7 @@
const RegistryFetcher = require('../lib/registry.js')
const t = require('tap')
const mr = require('npm-registry-mock')
const tnock = require('./fixtures/tnock')
const port = 18000 + (+process.env.TAP_CHILD_ID || 0)
const me = t.testdir()

Expand Down Expand Up @@ -239,3 +240,93 @@ t.test('packument that falls back to fullMetadata', t => {
t.end()
})
})

t.test('option replaceRegistryHost', rhTest => {
const { join, resolve } = require('path')
const fs = require('fs')

const abbrev = resolve(__dirname, 'fixtures/abbrev-1.1.1.tgz')
const abbrevTGZ = fs.readFileSync(abbrev)

const abbrevPackument = JSON.stringify({
_id: 'abbrev',
_rev: 'lkjadflkjasdf',
name: 'abbrev',
'dist-tags': { latest: '1.1.1' },
versions: {
'1.1.1': {
name: 'abbrev',
version: '1.1.1',
dist: {
tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
},
},
},
})

rhTest.test('host should not be replaced', async ct => {
const testdir = t.testdir()
tnock(ct, 'https://registry.github.com')
.get('/abbrev')
.reply(200, abbrevPackument)

tnock(ct, 'https://registry.npmjs.org')
.get('/abbrev/-/abbrev-1.1.1.tgz')
.reply(200, abbrevTGZ)

const fetcher = new RegistryFetcher('abbrev', {
registry: 'https://registry.github.com',
cache: join(testdir, 'cache'),
fullReadJson: true,
replaceRegistryHost: 'never',
})
ct.equal(fetcher.replaceRegistryHost, 'never')
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()
ct.match(tarball, abbrevTGZ)
})

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

const fetcher = new RegistryFetcher('abbrev', {
registry: 'https://registry.github.com',
cache: join(testdir, 'cache'),
fullReadJson: true,
})
ct.equal(fetcher.replaceRegistryHost, 'npmjs')
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()
ct.match(tarball, abbrevTGZ)
})

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

const fetcher = new RegistryFetcher('abbrev', {
registry: 'https://registry.github.com',
cache: join(testdir, 'cache'),
fullReadJson: true,
replaceRegistryHost: 'npmjs',
})
ct.equal(fetcher.replaceRegistryHost, 'npmjs')
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()
ct.match(tarball, abbrevTGZ)
})

rhTest.end()
})

0 comments on commit f519cf4

Please sign in to comment.