Skip to content

Commit

Permalink
npm/fetchPackageInfo: Use json.stream for better performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Nov 24, 2023
1 parent d7727ff commit c5a6dc4
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const isExactVersion = (version: Version) =>
/** Fetches a packument or dist-tag from the npm registry. */
const fetchPackageInfo = async (
name: string,
tag: string | null = 'latest',
tag: string | null,
opts: npmRegistryFetch.FetchOptions = {},
): Promise<any> => {
const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
Expand All @@ -62,20 +62,33 @@ const fetchPackageInfo = async (
...opts.headers,
}
const url = path.join(registry, name)
const fetchOptions = {
...opts,
headers,
spec: name,
}

try {
const result = await npmRegistryFetch.json(url, {
...opts,
headers,
spec: name,
})
return opts.fullMetadata
? result
: {
name,
'dist-tags': result['dist-tags'],
versions: result.versions,
if (opts.fullMetadata) {
return npmRegistryFetch.json(url, fetchOptions)
} else {
tag = tag || 'latest'
// typescript does not type async iteratable stream correctly
const stream = npmRegistryFetch.json.stream(url, '$*', fetchOptions) as any

const partialPackument: Partial<Packument> = { name }

for await (const { key, value } of stream) {
if (key === 'dist-tags') {
partialPackument['dist-tags'] = value
} else if (key === 'versions') {
partialPackument.versions = value
break
}
}

return partialPackument
}
} catch (err: any) {
if (err.code !== 'E404' || opts.fullMetadata) {
throw err
Expand Down

0 comments on commit c5a6dc4

Please sign in to comment.