Skip to content

Commit c57d18b

Browse files
haoqunjiangmactanxin
authored andcommitted
refactor: use a plain http request to get package metadata (vuejs#5045)
* refactor: use a plain http request to get package metadata fixes vuejs#4895 fixes vuejs#4995 * chore: add link to the package metadata documentation
1 parent 71bcb0a commit c57d18b

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

packages/@vue/cli-shared-utils/lib/request.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
exports.request = {
2-
get (uri) {
2+
get (uri, opts) {
33
// lazy require
44
const request = require('request-promise-native')
55
const reqOpts = {
66
method: 'GET',
77
timeout: 30000,
88
resolveWithFullResponse: true,
99
json: true,
10-
uri
10+
uri,
11+
...opts
1112
}
1213

1314
return request(reqOpts)

packages/@vue/cli/lib/util/ProjectPackageManager.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
chalk,
99
execa,
1010
semver,
11+
request,
1112

1213
hasYarn,
1314
hasProjectYarn,
@@ -19,7 +20,8 @@ const {
1920
resolvePluginId,
2021

2122
log,
22-
warn
23+
warn,
24+
error
2325
} = require('@vue/cli-shared-utils')
2426

2527
const { loadOptions } = require('../options')
@@ -174,7 +176,8 @@ class PackageManager {
174176
}
175177
}
176178

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

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

187-
const args = await this.addRegistryToArgs(['info', packageName, field, '--json'])
188-
const { stdout } = await execa(this.bin, args)
189-
190-
metadata = JSON.parse(stdout)
191-
if (this.bin === 'yarn') {
192-
// `yarn info` outputs messages in the form of `{"type": "inspect", data: {}}`
193-
metadata = metadata.data
190+
const headers = {}
191+
if (!full) {
192+
headers.Accept = 'application/vnd.npm.install-v1+json'
194193
}
195194

196-
metadataCache.set(metadataKey, metadata)
197-
return metadata
195+
const url = `${registry}/${packageName}`
196+
try {
197+
metadata = (await request.get(url, { headers })).body
198+
metadataCache.set(metadataKey, metadata)
199+
return metadata
200+
} catch (e) {
201+
error(`Failed to get response from ${url}`)
202+
throw e
203+
}
198204
}
199205

200206
async getRemoteVersion (packageName, versionRange = 'latest') {

0 commit comments

Comments
 (0)