@npmcli/config 2.1.0 → 2.3.2

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
 
@@ -495,8 +498,16 @@ class Config {
495
498
 
496
499
  async loadProjectConfig () {
497
500
  // the localPrefix can be set by the CLI config, but otherwise is
498
- // found by walking up the folder tree
501
+ // found by walking up the folder tree. either way, we load it before
502
+ // we return to make sure localPrefix is set
499
503
  await this.loadLocalPrefix()
504
+
505
+ if (this[_get]('global') === true || this[_get]('location') === 'global') {
506
+ this.data.get('project').source = '(global mode enabled, ignored)'
507
+ this.sources.set(this.data.get('project').source, 'project')
508
+ return
509
+ }
510
+
500
511
  const projectFile = resolve(this.localPrefix, '.npmrc')
501
512
  // if we're in the ~ directory, and there happens to be a node_modules
502
513
  // folder (which is not TOO uncommon, it turns out), then we can end
@@ -588,14 +599,17 @@ class Config {
588
599
  const nerfed = nerfDart(uri)
589
600
  const def = nerfDart(this.get('registry'))
590
601
  if (def === nerfed) {
602
+ // do not delete email, that shouldn't be nerfed any more.
603
+ // just delete the nerfed copy, if one exists.
591
604
  this.delete(`-authtoken`, 'user')
592
605
  this.delete(`_authToken`, 'user')
606
+ this.delete(`_authtoken`, 'user')
593
607
  this.delete(`_auth`, 'user')
594
608
  this.delete(`_password`, 'user')
595
609
  this.delete(`username`, 'user')
596
- this.delete(`email`, 'user')
597
610
  }
598
611
  this.delete(`${nerfed}:-authtoken`, 'user')
612
+ this.delete(`${nerfed}:_authtoken`, 'user')
599
613
  this.delete(`${nerfed}:_authToken`, 'user')
600
614
  this.delete(`${nerfed}:_auth`, 'user')
601
615
  this.delete(`${nerfed}:_password`, 'user')
@@ -603,7 +617,7 @@ class Config {
603
617
  this.delete(`${nerfed}:email`, 'user')
604
618
  }
605
619
 
606
- setCredentialsByURI (uri, { token, username, password, email, alwaysAuth }) {
620
+ setCredentialsByURI (uri, { token, username, password, email }) {
607
621
  const nerfed = nerfDart(uri)
608
622
  const def = nerfDart(this.get('registry'))
609
623
 
@@ -611,41 +625,45 @@ class Config {
611
625
  // remove old style auth info not limited to a single registry
612
626
  this.delete('_password', 'user')
613
627
  this.delete('username', 'user')
614
- this.delete('email', 'user')
615
628
  this.delete('_auth', 'user')
616
629
  this.delete('_authtoken', 'user')
630
+ this.delete('-authtoken', 'user')
617
631
  this.delete('_authToken', 'user')
618
632
  }
619
633
 
620
- this.delete(`${nerfed}:-authtoken`)
634
+ // email used to be nerfed always. if we're using the default
635
+ // registry, de-nerf it.
636
+ if (nerfed === def) {
637
+ email = email ||
638
+ this.get('email', 'user') ||
639
+ this.get(`${nerfed}:email`, 'user')
640
+ if (email)
641
+ this.set('email', email, 'user')
642
+ }
643
+
644
+ // field that hasn't been used as documented for a LONG time,
645
+ // and as of npm 7.10.0, isn't used at all. We just always
646
+ // send auth if we have it, only to the URIs under the nerf dart.
647
+ this.delete(`${nerfed}:always-auth`, 'user')
648
+
649
+ this.delete(`${nerfed}:-authtoken`, 'user')
650
+ this.delete(`${nerfed}:_authtoken`, 'user')
651
+ this.delete(`${nerfed}:email`, 'user')
621
652
  if (token) {
622
653
  this.set(`${nerfed}:_authToken`, token, 'user')
623
654
  this.delete(`${nerfed}:_password`, 'user')
624
655
  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')
656
+ } else if (username || password) {
657
+ if (!username)
658
+ throw new Error('must include username')
659
+ if (!password)
660
+ throw new Error('must include password')
636
661
  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')
662
+ this.set(`${nerfed}:username`, username, 'user')
663
+ // note: not encrypted, no idea why we bothered to do this, but oh well
664
+ // protects against shoulder-hacks if password is memorable, I guess?
665
+ const encoded = Buffer.from(password, 'utf8').toString('base64')
666
+ this.set(`${nerfed}:_password`, encoded, 'user')
649
667
  } else {
650
668
  throw new Error('No credentials to set.')
651
669
  }
@@ -656,18 +674,12 @@ class Config {
656
674
  const nerfed = nerfDart(uri)
657
675
  const creds = {}
658
676
 
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
677
  const email = this.get(`${nerfed}:email`) || this.get('email')
667
678
  if (email)
668
679
  creds.email = email
669
680
 
670
681
  const tokenReg = this.get(`${nerfed}:_authToken`) ||
682
+ this.get(`${nerfed}:_authtoken`) ||
671
683
  this.get(`${nerfed}:-authtoken`) ||
672
684
  nerfed === nerfDart(this.get('registry')) && this.get('_authToken')
673
685
 
@@ -686,6 +698,16 @@ class Config {
686
698
  return creds
687
699
  }
688
700
 
701
+ const authReg = this.get(`${nerfed}:_auth`)
702
+ if (authReg) {
703
+ const authDecode = Buffer.from(authReg, 'base64').toString('utf8')
704
+ const authSplit = authDecode.split(':')
705
+ creds.username = authSplit.shift()
706
+ creds.password = authSplit.join(':')
707
+ creds.auth = authReg
708
+ return creds
709
+ }
710
+
689
711
  // at this point, we can only use the values if the URI is the
690
712
  // default registry.
691
713
  const defaultNerf = nerfDart(this.get('registry'))
package/lib/set-envs.js CHANGED
@@ -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.1.0",
3
+ "version": "2.3.2",
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",