Skip to content

Commit

Permalink
refactor: use a plain http request to get package metadata (vuejs#5045)
Browse files Browse the repository at this point in the history
* refactor: use a plain http request to get package metadata

fixes vuejs#4895
fixes vuejs#4995

* chore: add link to the package metadata documentation
  • Loading branch information
sodatea authored and mactanxin committed Feb 11, 2020
1 parent 71bcb0a commit c57d18b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/@vue/cli-shared-utils/lib/request.js
@@ -1,13 +1,14 @@
exports.request = {
get (uri) {
get (uri, opts) {
// lazy require
const request = require('request-promise-native')
const reqOpts = {
method: 'GET',
timeout: 30000,
resolveWithFullResponse: true,
json: true,
uri
uri,
...opts
}

return request(reqOpts)
Expand Down
28 changes: 17 additions & 11 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
Expand Up @@ -8,6 +8,7 @@ const {
chalk,
execa,
semver,
request,

hasYarn,
hasProjectYarn,
Expand All @@ -19,7 +20,8 @@ const {
resolvePluginId,

log,
warn
warn,
error
} = require('@vue/cli-shared-utils')

const { loadOptions } = require('../options')
Expand Down Expand Up @@ -174,7 +176,8 @@ class PackageManager {
}
}

async getMetadata (packageName, { field = '' } = {}) {
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
async getMetadata (packageName, { full = false } = {}) {
const registry = await this.getRegistry()

const metadataKey = `${this.bin}-${registry}-${packageName}`
Expand All @@ -184,17 +187,20 @@ class PackageManager {
return metadata
}

const args = await this.addRegistryToArgs(['info', packageName, field, '--json'])
const { stdout } = await execa(this.bin, args)

metadata = JSON.parse(stdout)
if (this.bin === 'yarn') {
// `yarn info` outputs messages in the form of `{"type": "inspect", data: {}}`
metadata = metadata.data
const headers = {}
if (!full) {
headers.Accept = 'application/vnd.npm.install-v1+json'
}

metadataCache.set(metadataKey, metadata)
return metadata
const url = `${registry}/${packageName}`
try {
metadata = (await request.get(url, { headers })).body
metadataCache.set(metadataKey, metadata)
return metadata
} catch (e) {
error(`Failed to get response from ${url}`)
throw e
}
}

async getRemoteVersion (packageName, versionRange = 'latest') {
Expand Down

0 comments on commit c57d18b

Please sign in to comment.