Skip to content

Commit

Permalink
fix: support auth token when retrieving package metadata (#5586)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodatea committed Jun 19, 2020
1 parent 2691266 commit 4891d91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
@@ -1,6 +1,7 @@
const fs = require('fs-extra')
const path = require('path')

const ini = require('ini')
const minimist = require('minimist')
const LRU = require('lru-cache')

Expand Down Expand Up @@ -154,6 +155,38 @@ class PackageManager {
return this._registry
}

async getAuthToken () {
// get npmrc (https://docs.npmjs.com/configuring-npm/npmrc.html#files)
const possibleRcPaths = [
path.resolve(this.context, '.npmrc'),
path.resolve(require('os').homedir(), '.npmrc')
]
if (process.env.PREFIX) {
possibleRcPaths.push(path.resolve(process.env.PREFIX, '/etc/npmrc'))
}
// there's also a '/path/to/npm/npmrc', skipped for simplicity of implementation

let npmConfig = {}
for (const loc of possibleRcPaths) {
if (fs.existsSync(loc)) {
try {
// the closer config file (the one with lower index) takes higher precedence
npmConfig = Object.assign({}, ini.parse(fs.readFileSync(loc, 'utf-8')), npmConfig)
} catch (e) {
// in case of file permission issues, etc.
}
}
}

const registry = await this.getRegistry()
const registryWithoutProtocol = registry
.replace(/https?:/, '') // remove leading protocol
.replace(/([^/])$/, '$1/') // ensure ending with slash
const authTokenKey = `${registryWithoutProtocol}:_authToken`

return npmConfig[authTokenKey]
}

async setRegistryEnvs () {
const registry = await this.getRegistry()

Expand Down Expand Up @@ -204,6 +237,7 @@ class PackageManager {
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
async getMetadata (packageName, { full = false } = {}) {
const registry = await this.getRegistry()
const authToken = await this.getAuthToken()

const metadataKey = `${this.bin}-${registry}-${packageName}`
let metadata = metadataCache.get(metadataKey)
Expand All @@ -217,6 +251,10 @@ class PackageManager {
headers.Accept = 'application/vnd.npm.install-v1+json'
}

if (authToken) {
headers.Authorization = `Bearer ${authToken}`
}

const url = `${registry.replace(/\/$/g, '')}/${packageName}`
try {
metadata = (await request.get(url, { headers })).body
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli/package.json
Expand Up @@ -39,6 +39,7 @@
"fs-extra": "^7.0.1",
"globby": "^9.2.0",
"import-global": "^0.1.0",
"ini": "^1.3.5",
"inquirer": "^7.1.0",
"isbinaryfile": "^4.0.6",
"javascript-stringify": "^1.6.0",
Expand Down

0 comments on commit 4891d91

Please sign in to comment.