Skip to content

Commit e61e68d

Browse files
committedMay 31, 2017
publish: adapt config for publish RegClient (#16762)
Fixes: #16746 PR-URL: npm/npm#16762 Credit: @zkat
1 parent 9aac984 commit e61e68d

File tree

4 files changed

+67
-53
lines changed

4 files changed

+67
-53
lines changed
 

‎lib/config/reg-client.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
module.exports = regClientConfig
4+
function regClientConfig (npm, log, config) {
5+
return {
6+
proxy: {
7+
http: config.get('proxy'),
8+
https: config.get('https-proxy'),
9+
localAddress: config.get('local-address')
10+
},
11+
ssl: {
12+
certificate: config.get('cert'),
13+
key: config.get('key'),
14+
ca: config.get('ca'),
15+
strict: config.get('strict-ssl')
16+
},
17+
retry: {
18+
retries: config.get('fetch-retries'),
19+
factor: config.get('fetch-retry-factor'),
20+
minTimeout: config.get('fetch-retry-mintimeout'),
21+
maxTimeout: config.get('fetch-retry-maxtimeout')
22+
},
23+
userAgent: config.get('user-agent'),
24+
log: log,
25+
defaultTag: config.get('tag'),
26+
maxSockets: config.get('maxsockets'),
27+
scope: npm.projectScope
28+
}
29+
}

‎lib/npm.js

+2-28
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
var rimraf = require('rimraf')
3434
var lazyProperty = require('lazy-property')
3535
var parseJSON = require('./utils/parse-json.js')
36+
var clientConfig = require('./config/reg-client.js')
3637
var aliases = require('./config/cmd-list').aliases
3738
var cmdList = require('./config/cmd-list').cmdList
3839
var plumbing = require('./config/cmd-list').plumbing
@@ -344,7 +345,7 @@
344345
lazyProperty(npm, 'registry', function () {
345346
registryLoaded = true
346347
var RegClient = require('npm-registry-client')
347-
var registry = new RegClient(adaptClientConfig(npm.config))
348+
var registry = new RegClient(clientConfig(npm, log, npm.config))
348349
registry.version = npm.version
349350
registry.refer = registryRefer
350351
return registry
@@ -467,31 +468,4 @@
467468
return ''
468469
}
469470
}
470-
471-
function adaptClientConfig (config) {
472-
return {
473-
proxy: {
474-
http: config.get('proxy'),
475-
https: config.get('https-proxy'),
476-
localAddress: config.get('local-address')
477-
},
478-
ssl: {
479-
certificate: config.get('cert'),
480-
key: config.get('key'),
481-
ca: config.get('ca'),
482-
strict: config.get('strict-ssl')
483-
},
484-
retry: {
485-
retries: config.get('fetch-retries'),
486-
factor: config.get('fetch-retry-factor'),
487-
minTimeout: config.get('fetch-retry-mintimeout'),
488-
maxTimeout: config.get('fetch-retry-maxtimeout')
489-
},
490-
userAgent: config.get('user-agent'),
491-
log: log,
492-
defaultTag: config.get('tag'),
493-
maxSockets: config.get('maxsockets'),
494-
scope: npm.projectScope
495-
}
496-
}
497471
})()

‎lib/utils/get-publish-config.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
var Conf = require('../config/core.js').Conf
2-
var RegClient = require('npm-registry-client')
3-
var log = require('npmlog')
1+
'use strict'
2+
3+
const clientConfig = require('../config/reg-client.js')
4+
const Conf = require('../config/core.js').Conf
5+
const log = require('npmlog')
6+
const npm = require('../npm.js')
7+
const RegClient = require('npm-registry-client')
48

59
module.exports = getPublishConfig
610

711
function getPublishConfig (publishConfig, defaultConfig, defaultClient) {
8-
var config = defaultConfig
9-
var client = defaultClient
12+
let config = defaultConfig
13+
let client = defaultClient
1014
log.verbose('getPublishConfig', publishConfig)
1115
if (publishConfig) {
1216
config = new Conf(defaultConfig)
@@ -18,7 +22,7 @@ function getPublishConfig (publishConfig, defaultConfig, defaultClient) {
1822
s[k] = publishConfig[k]
1923
return s
2024
}, {}))
21-
client = new RegClient(config)
25+
client = new RegClient(clientConfig(npm, log, config))
2226
}
2327

2428
return { config: config, client: client }

‎test/tap/publish-config.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
var common = require('../common-tap.js')
2-
var test = require('tap').test
3-
var fs = require('fs')
4-
var osenv = require('osenv')
5-
var pkg = process.env.npm_config_tmp || '/tmp'
6-
pkg += '/npm-test-publish-config'
1+
'use strict'
2+
3+
const common = require('../common-tap.js')
4+
const test = require('tap').test
5+
const fs = require('fs')
6+
const osenv = require('osenv')
7+
const pkg = `${process.env.npm_config_tmp || '/tmp'}/npm-test-publish-config`
78

89
require('mkdirp').sync(pkg)
910

1011
fs.writeFileSync(pkg + '/package.json', JSON.stringify({
1112
name: 'npm-test-publish-config',
1213
version: '1.2.3',
13-
publishConfig: { registry: common.registry }
14+
publishConfig: {
15+
registry: common.registry
16+
}
1417
}), 'utf8')
1518

1619
fs.writeFileSync(pkg + '/fixture_npmrc',
1720
'//localhost:1337/:email = fancy@feast.net\n' +
1821
'//localhost:1337/:username = fancy\n' +
19-
'//localhost:1337/:_password = ' + new Buffer('feast').toString('base64') + '\n' +
20-
'registry = http://localhost:1337/')
22+
'//localhost:1337/:_password = ' + new Buffer('feast').toString('base64'))
2123

2224
test(function (t) {
23-
var child
24-
t.plan(4)
25+
let child
26+
t.plan(5)
2527
require('http').createServer(function (req, res) {
2628
t.pass('got request on the fakey fake registry')
27-
this.close()
28-
res.statusCode = 500
29-
res.end(JSON.stringify({
30-
error: 'sshhh. naptime nao. \\^O^/ <(YAWWWWN!)'
31-
}))
32-
child.kill('SIGINT')
33-
}).listen(common.port, function () {
29+
let body = ''
30+
req.on('data', (d) => { body += d })
31+
req.on('end', () => {
32+
this.close()
33+
res.statusCode = 500
34+
res.end(JSON.stringify({
35+
error: 'sshhh. naptime nao. \\^O^/ <(YAWWWWN!)'
36+
}))
37+
t.match(body, /"beta"/, 'got expected tag')
38+
child.kill('SIGINT')
39+
})
40+
}).listen(common.port, () => {
3441
t.pass('server is listening')
3542

3643
// don't much care about listening to the child's results
@@ -40,7 +47,7 @@ test(function (t) {
4047
// itself functions normally.
4148
//
4249
// Make sure that we don't sit around waiting for lock files
43-
child = common.npm(['publish', '--userconfig=' + pkg + '/fixture_npmrc'], {
50+
child = common.npm(['publish', '--userconfig=' + pkg + '/fixture_npmrc', '--tag=beta'], {
4451
cwd: pkg,
4552
stdio: 'inherit',
4653
env: {

0 commit comments

Comments
 (0)
Please sign in to comment.