Skip to content

Commit a049c52

Browse files
committedJun 27, 2024··
perf: use npm-registry-fetch instead of pacote to deduce the package size
1 parent 760f149 commit a049c52

File tree

4 files changed

+172
-489
lines changed

4 files changed

+172
-489
lines changed
 

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"@nuxt/schema": "^3.12.2",
3232
"@types/markdown-it": "^14.1.1",
3333
"@types/node": "^20.14.7",
34-
"@types/pacote": "^11.1.8",
3534
"@types/which": "^3.0.4",
3635
"@types/ws": "^8.5.10",
3736
"@unocss/eslint-config": "^0.61.0",

‎packages/devtools/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
"launch-editor": "^2.8.0",
6464
"local-pkg": "^0.5.0",
6565
"magicast": "^0.3.4",
66+
"npm-registry-fetch": "^17.1.0",
6667
"nypm": "^0.3.8",
6768
"ohash": "^1.1.3",
68-
"pacote": "^18.0.6",
6969
"pathe": "^1.1.2",
7070
"perfect-debounce": "^1.0.0",
7171
"pkg-types": "^1.1.1",
@@ -92,6 +92,7 @@
9292
"@nuxt/test-utils": "3.9.0",
9393
"@parcel/watcher": "^2.4.1",
9494
"@types/markdown-it-link-attributes": "^3.0.5",
95+
"@types/npm-registry-fetch": "^8.0.7",
9596
"@types/ua-parser-js": "^0.7.39",
9697
"@unocss/nuxt": "^0.61.0",
9798
"@unocss/preset-icons": "^0.61.0",

‎packages/devtools/src/npm/index.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@ import { createRequire } from 'node:module'
22
import { logger, useNuxt } from '@nuxt/kit'
33
import { readPackageJSON } from 'pkg-types'
44
import semver from 'semver'
5+
import { joinURL } from 'ufo'
56
import { getPackageInfo } from 'local-pkg'
67
import type { PackageUpdateInfo } from '../types'
78

89
export async function getMainPackageJSON(nuxt = useNuxt()) {
910
return readPackageJSON(nuxt.options.rootDir)
1011
}
1112

13+
export interface Packument {
14+
'name': string
15+
/**
16+
* An object where each key is a version, and each value is the manifest for
17+
* that version.
18+
*/
19+
'versions': Record<string, Omit<Packument, 'versions'>>
20+
/**
21+
* An object mapping dist-tags to version numbers. This is how `foo@latest`
22+
* gets turned into `foo@1.2.3`.
23+
*/
24+
'dist-tags': { latest: string } & Record<string, string>
25+
/**
26+
* In the full packument, an object mapping version numbers to publication
27+
* times, for the `opts.before` functionality.
28+
*/
29+
'time': Record<string, string> & {
30+
created: string
31+
modified: string
32+
}
33+
}
34+
1235
export async function checkForUpdateOf(name: string, current?: string, nuxt = useNuxt()): Promise<PackageUpdateInfo | undefined> {
1336
try {
1437
if (!current) {
@@ -22,11 +45,18 @@ export async function checkForUpdateOf(name: string, current?: string, nuxt = us
2245
if (!current)
2346
return
2447

25-
const packument = await import('pacote').then(r => r.default?.packument || r.packument)
26-
const manifest = await packument(name)
48+
const { pickRegistry, json: fetchMeta } = await import('npm-registry-fetch')
49+
const reg = pickRegistry(name)
50+
const manifest = await fetchMeta(joinURL(reg, name), {
51+
headers: {
52+
'user-agent': `npm node/${process.version}`,
53+
'accept': 'application/json',
54+
},
55+
spec: name,
56+
}) as unknown as Packument
2757

28-
const latest = manifest['dist-tags'].latest
29-
const needsUpdate = latest !== current && semver.lt(current, latest)
58+
const latest = manifest?.['dist-tags']?.latest
59+
const needsUpdate = !!latest && latest !== current && semver.lt(current, latest)
3060

3161
return {
3262
name,

‎pnpm-lock.yaml

+136-483
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.