npq 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,6 +51,28 @@ class PackageRepoUtils {
51
51
  parsePackageVersion(version) {
52
52
  return semver.coerce(version)
53
53
  }
54
+
55
+ async getSemVer(packageName, packageVersion) {
56
+ if (semver.valid(packageVersion)) {
57
+ return packageVersion
58
+ } else {
59
+ // this is probably an alias such as `latest` that we need to match
60
+ // via dist-tags:
61
+ const packageInfo = await this.getPackageInfo(packageName)
62
+
63
+ if (packageInfo['dist-tags'] === undefined) {
64
+ throw new Error(`could not find dist-tags for package ${packageName}`)
65
+ }
66
+
67
+ if (packageInfo['dist-tags'][packageVersion] === undefined) {
68
+ throw new Error(`could not find dist-tag ${packageVersion} for package ${packageName}`)
69
+ }
70
+
71
+ const semverVersion = packageInfo['dist-tags'][packageVersion]
72
+
73
+ return semverVersion
74
+ }
75
+ }
54
76
  }
55
77
 
56
78
  module.exports = PackageRepoUtils
@@ -34,9 +34,14 @@ class Marshall extends BaseMarshall {
34
34
  // published, date diff, etc into the package repo utils
35
35
  const pakument = await this.packageRepoUtils.getPackageInfo(pkg.packageName)
36
36
 
37
+ const packageVersion = await this.packageRepoUtils.getSemVer(
38
+ pkg.packageName,
39
+ pkg.packageVersion
40
+ )
41
+
37
42
  // @TODO fix to work for both explicit versions (1.0.0) and also
38
43
  // for dis-tags (latest)
39
- const npmUser = pakument.versions[pkg.packageVersion]._npmUser
44
+ const npmUser = pakument.versions[packageVersion]._npmUser
40
45
  if (!npmUser || !npmUser.email) {
41
46
  throw new Error('could not determine publishing user for this package version')
42
47
  }
@@ -46,7 +51,7 @@ class Marshall extends BaseMarshall {
46
51
  }
47
52
 
48
53
  let firstVersionForUser = null
49
- const versionPublishedDateString = pakument.time[pkg.packageVersion]
54
+ const versionPublishedDateString = pakument.time[packageVersion]
50
55
  for (const [version, versionMetadata] of Object.entries(pakument.versions)) {
51
56
  if (versionMetadata._npmUser && versionMetadata._npmUser.email === npmUser.email) {
52
57
  firstVersionForUser = versionMetadata
@@ -54,9 +59,9 @@ class Marshall extends BaseMarshall {
54
59
  }
55
60
  }
56
61
 
57
- if (!firstVersionForUser || firstVersionForUser.version === pkg.packageVersion) {
62
+ if (!firstVersionForUser || firstVersionForUser.version === packageVersion) {
58
63
  throw new Error(
59
- `The user ${npmUser.name} <${npmUser.email}> published this package for the first time only ${dateDiffInDays} days ago. Proceed with care.`
64
+ `This is first version the user ${npmUser.name} <${npmUser.email}> published this package`
60
65
  )
61
66
  }
62
67
 
@@ -70,7 +75,7 @@ class Marshall extends BaseMarshall {
70
75
 
71
76
  if (dateDiffInDays <= 30) {
72
77
  throw new Error(
73
- `The user ${npmUser.name} <${npmUser.email}> published this package for the first time only ${dateDiffInDays} days ago. Proceed with care.`
78
+ `The user ${npmUser.name} <${npmUser.email}> published this package for the first time only ${dateDiffInDays} days ago.`
74
79
  )
75
80
  }
76
81
 
@@ -32,6 +32,10 @@ class Marshall extends BaseMarshall {
32
32
 
33
33
  validationMetadata.name = packageName
34
34
  validationMetadata.version = packageVersion
35
+
36
+ if (!validationMetadata.version) {
37
+ throw new Error('Unable to find version or dist-tag for package')
38
+ }
35
39
  })
36
40
  .then(() => {
37
41
  return this.fetchRegistryKeys()
@@ -48,15 +52,16 @@ class Marshall extends BaseMarshall {
48
52
  })
49
53
  .then((metadata) => {
50
54
  if (!metadata || !metadata._attestations) {
51
- throw new Warning(
52
- 'the package was published without any attestations. Proceed with care.'
53
- )
55
+ throw new Warning('the package was published without any attestations.')
54
56
  }
55
57
 
56
58
  const attestations = metadata._attestations
57
59
 
58
60
  return attestations
59
61
  })
62
+ .catch((error) => {
63
+ throw new Error(`Unable to verify provenance: ${error.message}`)
64
+ })
60
65
  }
61
66
 
62
67
  fetchRegistryKeys() {
@@ -32,6 +32,9 @@ class Marshall extends BaseMarshall {
32
32
  .then((metadata) => {
33
33
  return metadata
34
34
  })
35
+ .catch((error) => {
36
+ throw new Error(`Unable to grab package manifest: ${error.message}`)
37
+ })
35
38
  }
36
39
 
37
40
  fetchRegistryKeys() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npq",
3
- "version": "3.2.2",
3
+ "version": "3.3.0",
4
4
  "description": "marshall your npm/npm package installs with high quality and class 🎖",
5
5
  "bin": {
6
6
  "npq": "./bin/npq.js",
@@ -10,6 +10,7 @@
10
10
  "lint": "eslint . --ignore-path .gitignore && npm run lint:lockfile",
11
11
  "lint:lockfile": "lockfile-lint --path package-lock.json --type npm --validate-https --allowed-hosts npm npm",
12
12
  "test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
13
+ "build": "node scripts/build.js",
13
14
  "test:watch": "NODE_OPTIONS=\"--experimental-vm-modules\" jest --watch",
14
15
  "coverage:view": "opn coverage/lcov-report/index.html",
15
16
  "commit": "git-cz",
@@ -0,0 +1,25 @@
1
+ const fs = require('fs/promises')
2
+ const path = require('path')
3
+
4
+ const TOP_PACKAGES_ASSET_URL =
5
+ 'https://github.com/lirantal/npm-rank/releases/download/latest/list-package-names.json'
6
+
7
+ const TOP_PACKAGES_FILE_PATH = path.join(__dirname, '../data/top-packages.json')
8
+
9
+ async function downloadTopPackages() {
10
+ const response = await fetch(TOP_PACKAGES_ASSET_URL)
11
+ const data = await response.json()
12
+ return data
13
+ }
14
+
15
+ async function saveTopPackagesToFile() {
16
+ const topPackages = await downloadTopPackages()
17
+ await fs.writeFile(TOP_PACKAGES_FILE_PATH, JSON.stringify(topPackages, null, 2))
18
+ }
19
+
20
+ async function main() {
21
+ const topPackages = await downloadTopPackages()
22
+ await saveTopPackagesToFile()
23
+ }
24
+
25
+ main()