@npmcli/config 2.0.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -197,11 +197,6 @@ class Config {
197
197
  throw new Error('call config.load() before setting values')
198
198
  if (!confTypes.has(where))
199
199
  throw new Error('invalid config location param: ' + where)
200
- if (key === '_auth') {
201
- const { email } = this.getCredentialsByURI(this.get('registry'))
202
- if (!email)
203
- throw new Error('Cannot set _auth without first setting email')
204
- }
205
200
  this[_checkDeprecated](key)
206
201
  const { data } = this.data.get(where)
207
202
  data[key] = val
@@ -282,6 +277,14 @@ class Config {
282
277
  // symbols, as that module also does a bunch of get operations
283
278
  this[_loaded] = true
284
279
 
280
+ process.emit('time', 'config:load:credentials')
281
+ const reg = this.get('registry')
282
+ const creds = this.getCredentialsByURI(reg)
283
+ // ignore this error because a failed set will strip out anything that
284
+ // might be a security hazard, which was the intention.
285
+ try { this.setCredentialsByURI(reg, creds) } catch (_) {}
286
+ process.emit('timeEnd', 'config:load:credentials')
287
+
285
288
  // set proper globalPrefix now that everything is loaded
286
289
  this.globalPrefix = this.get('prefix')
287
290
 
@@ -494,6 +497,12 @@ class Config {
494
497
  }
495
498
 
496
499
  async loadProjectConfig () {
500
+ if (this[_get]('global') === true || this[_get]('location') === 'global') {
501
+ this.data.get('project').source = '(global mode enabled, ignored)'
502
+ this.sources.set(this.data.get('project').source, 'project')
503
+ return
504
+ }
505
+
497
506
  // the localPrefix can be set by the CLI config, but otherwise is
498
507
  // found by walking up the folder tree
499
508
  await this.loadLocalPrefix()
@@ -588,14 +597,17 @@ class Config {
588
597
  const nerfed = nerfDart(uri)
589
598
  const def = nerfDart(this.get('registry'))
590
599
  if (def === nerfed) {
600
+ // do not delete email, that shouldn't be nerfed any more.
601
+ // just delete the nerfed copy, if one exists.
591
602
  this.delete(`-authtoken`, 'user')
592
603
  this.delete(`_authToken`, 'user')
604
+ this.delete(`_authtoken`, 'user')
593
605
  this.delete(`_auth`, 'user')
594
606
  this.delete(`_password`, 'user')
595
607
  this.delete(`username`, 'user')
596
- this.delete(`email`, 'user')
597
608
  }
598
609
  this.delete(`${nerfed}:-authtoken`, 'user')
610
+ this.delete(`${nerfed}:_authtoken`, 'user')
599
611
  this.delete(`${nerfed}:_authToken`, 'user')
600
612
  this.delete(`${nerfed}:_auth`, 'user')
601
613
  this.delete(`${nerfed}:_password`, 'user')
@@ -603,7 +615,7 @@ class Config {
603
615
  this.delete(`${nerfed}:email`, 'user')
604
616
  }
605
617
 
606
- setCredentialsByURI (uri, { token, username, password, email, alwaysAuth }) {
618
+ setCredentialsByURI (uri, { token, username, password, email }) {
607
619
  const nerfed = nerfDart(uri)
608
620
  const def = nerfDart(this.get('registry'))
609
621
 
@@ -611,41 +623,45 @@ class Config {
611
623
  // remove old style auth info not limited to a single registry
612
624
  this.delete('_password', 'user')
613
625
  this.delete('username', 'user')
614
- this.delete('email', 'user')
615
626
  this.delete('_auth', 'user')
616
627
  this.delete('_authtoken', 'user')
628
+ this.delete('-authtoken', 'user')
617
629
  this.delete('_authToken', 'user')
618
630
  }
619
631
 
620
- this.delete(`${nerfed}:-authtoken`)
632
+ // email used to be nerfed always. if we're using the default
633
+ // registry, de-nerf it.
634
+ if (nerfed === def) {
635
+ email = email ||
636
+ this.get('email', 'user') ||
637
+ this.get(`${nerfed}:email`, 'user')
638
+ if (email)
639
+ this.set('email', email, 'user')
640
+ }
641
+
642
+ // field that hasn't been used as documented for a LONG time,
643
+ // and as of npm 7.10.0, isn't used at all. We just always
644
+ // send auth if we have it, only to the URIs under the nerf dart.
645
+ this.delete(`${nerfed}:always-auth`, 'user')
646
+
647
+ this.delete(`${nerfed}:-authtoken`, 'user')
648
+ this.delete(`${nerfed}:_authtoken`, 'user')
649
+ this.delete(`${nerfed}:email`, 'user')
621
650
  if (token) {
622
651
  this.set(`${nerfed}:_authToken`, token, 'user')
623
652
  this.delete(`${nerfed}:_password`, 'user')
624
653
  this.delete(`${nerfed}:username`, 'user')
625
- this.delete(`${nerfed}:email`, 'user')
626
- this.delete(`${nerfed}:always-auth`, 'user')
627
- } else if (username || password || email) {
628
- if (username || password) {
629
- if (!username)
630
- throw new Error('must include username')
631
- if (!password)
632
- throw new Error('must include password')
633
- }
634
- if (!email)
635
- throw new Error('must include email')
654
+ } else if (username || password) {
655
+ if (!username)
656
+ throw new Error('must include username')
657
+ if (!password)
658
+ throw new Error('must include password')
636
659
  this.delete(`${nerfed}:_authToken`, 'user')
637
- if (username || password) {
638
- this.set(`${nerfed}:username`, username, 'user')
639
- // note: not encrypted, no idea why we bothered to do this, but oh well
640
- // protects against shoulder-hacks if password is memorable, I guess?
641
- const encoded = Buffer.from(password, 'utf8').toString('base64')
642
- this.set(`${nerfed}:_password`, encoded, 'user')
643
- }
644
- this.set(`${nerfed}:email`, email, 'user')
645
- if (alwaysAuth !== undefined)
646
- this.set(`${nerfed}:always-auth`, alwaysAuth, 'user')
647
- else
648
- this.delete(`${nerfed}:always-auth`, 'user')
660
+ this.set(`${nerfed}:username`, username, 'user')
661
+ // note: not encrypted, no idea why we bothered to do this, but oh well
662
+ // protects against shoulder-hacks if password is memorable, I guess?
663
+ const encoded = Buffer.from(password, 'utf8').toString('base64')
664
+ this.set(`${nerfed}:_password`, encoded, 'user')
649
665
  } else {
650
666
  throw new Error('No credentials to set.')
651
667
  }
@@ -656,18 +672,12 @@ class Config {
656
672
  const nerfed = nerfDart(uri)
657
673
  const creds = {}
658
674
 
659
- // you can set always-auth for a single registry, or as a default
660
- const alwaysAuthReg = this.get(`${nerfed}:always-auth`)
661
- if (alwaysAuthReg !== undefined)
662
- creds.alwaysAuth = !!alwaysAuthReg
663
- else
664
- creds.alwaysAuth = this.get('always-auth')
665
-
666
675
  const email = this.get(`${nerfed}:email`) || this.get('email')
667
676
  if (email)
668
677
  creds.email = email
669
678
 
670
679
  const tokenReg = this.get(`${nerfed}:_authToken`) ||
680
+ this.get(`${nerfed}:_authtoken`) ||
671
681
  this.get(`${nerfed}:-authtoken`) ||
672
682
  nerfed === nerfDart(this.get('registry')) && this.get('_authToken')
673
683
 
@@ -686,6 +696,16 @@ class Config {
686
696
  return creds
687
697
  }
688
698
 
699
+ const authReg = this.get(`${nerfed}:_auth`)
700
+ if (authReg) {
701
+ const authDecode = Buffer.from(authReg, 'base64').toString('utf8')
702
+ const authSplit = authDecode.split(':')
703
+ creds.username = authSplit.shift()
704
+ creds.password = authSplit.join(':')
705
+ creds.auth = authReg
706
+ return creds
707
+ }
708
+
689
709
  // at this point, we can only use the values if the URI is the
690
710
  // default registry.
691
711
  const defaultNerf = nerfDart(this.get('registry'))
package/lib/set-envs.js CHANGED
@@ -67,8 +67,8 @@ const setEnvs = (config) => {
67
67
  const cliSet = new Set(Object.keys(cliConf))
68
68
  const envSet = new Set(Object.keys(envConf))
69
69
  for (const key in cliConf) {
70
- const { deprecated } = definitions[key] || {}
71
- if (deprecated)
70
+ const { deprecated, envExport = true } = definitions[key] || {}
71
+ if (deprecated || envExport === false)
72
72
  continue
73
73
 
74
74
  if (sameConfigValue(defaults[key], cliConf[key])) {
@@ -86,6 +86,8 @@ const setEnvs = (config) => {
86
86
 
87
87
  // also set some other common nice envs that we want to rely on
88
88
  env.HOME = config.home
89
+ env.npm_config_global_prefix = config.globalPrefix
90
+ env.npm_config_local_prefix = config.localPrefix
89
91
  if (cliConf.editor)
90
92
  env.EDITOR = cliConf.editor
91
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "2.0.0",
3
+ "version": "2.3.1",
4
4
  "files": [
5
5
  "lib"
6
6
  ],
@@ -24,7 +24,7 @@
24
24
  "coverage-map": "map.js"
25
25
  },
26
26
  "devDependencies": {
27
- "tap": "^14.10.8"
27
+ "tap": "^15.0.4"
28
28
  },
29
29
  "dependencies": {
30
30
  "ini": "^2.0.0",