npq 3.1.0 → 3.2.1

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.
@@ -2,7 +2,6 @@
2
2
 
3
3
  const BaseMarshall = require('./baseMarshall')
4
4
  const validator = require('validator')
5
- const Warning = require('../helpers/warning')
6
5
  const { marshallCategories } = require('./constants')
7
6
 
8
7
  const MARSHALL_NAME = 'author'
@@ -11,39 +10,71 @@ class Marshall extends BaseMarshall {
11
10
  constructor(options) {
12
11
  super(options)
13
12
  this.name = MARSHALL_NAME
14
- this.categoryId = marshallCategories.PackageHealth.id
13
+ this.categoryId = marshallCategories.SupplyChainSecurity.id
15
14
  }
16
15
 
17
16
  title() {
18
17
  return 'Identifying package author...'
19
18
  }
20
19
 
21
- validate(pkg) {
22
- return this.packageRepoUtils.getPackageInfo(pkg.packageName).then((data) => {
23
- const lastVersionData =
24
- data.versions && data['dist-tags'] && data.versions[data['dist-tags'].latest]
25
-
26
- const hasAuthorEmail =
27
- (lastVersionData &&
28
- lastVersionData.author &&
29
- lastVersionData.author.email &&
30
- validator.isEmail(lastVersionData.author.email)) ||
31
- (data.versions &&
32
- lastVersionData &&
33
- lastVersionData.authors &&
34
- lastVersionData.authors.filter &&
35
- lastVersionData.authors.filter(
36
- (author) => author.email && validator.isEmail(author.email).length
37
- ))
38
-
39
- if (!hasAuthorEmail) {
40
- throw new Warning(
41
- 'the package description has no e-mail associated with author(s). Proceed with care.'
42
- )
20
+ /**
21
+ * What do we check?
22
+ * 1. Who is the user who published the package? in the pakument's `_npmUser` field
23
+ * 2. Is this the first time we see this user publishing a package?
24
+ * If so, halt and report.
25
+ * 3. Start from first version to latest version and check if they published
26
+ * the package before.
27
+ * If they did, and the difference between this published version date and
28
+ * first is more than 30 days then halt and report.
29
+ * @param {*} pkg
30
+ * @returns
31
+ */
32
+ async validate(pkg) {
33
+ // @TODO move some of these utility functions about first package vesion
34
+ // published, date diff, etc into the package repo utils
35
+ const pakument = await this.packageRepoUtils.getPackageInfo(pkg.packageName)
36
+
37
+ // @TODO fix to work for both explicit versions (1.0.0) and also
38
+ // for dis-tags (latest)
39
+ const npmUser = pakument.versions[pkg.packageVersion]._npmUser
40
+ if (!npmUser || !npmUser.email) {
41
+ throw new Error('could not determine publishing user for this package version')
42
+ }
43
+
44
+ if (!validator.isEmail(npmUser.email)) {
45
+ throw new Error('the publishing user has no valid email address')
46
+ }
47
+
48
+ let firstVersionForUser = null
49
+ const versionPublishedDateString = pakument.time[pkg.packageVersion]
50
+ for (const [version, versionMetadata] of Object.entries(pakument.versions)) {
51
+ if (versionMetadata._npmUser && versionMetadata._npmUser.email === npmUser.email) {
52
+ firstVersionForUser = versionMetadata
53
+ break
43
54
  }
55
+ }
56
+
57
+ if (!firstVersionForUser || firstVersionForUser.version === pkg.packageVersion) {
58
+ throw new Error(
59
+ `The user ${npmUser.name} <${npmUser.email}> published this package for the first time only ${dateDiffInDays} days ago. Proceed with care.`
60
+ )
61
+ }
62
+
63
+ const firstPublishedDateString = pakument.time[firstVersionForUser.version]
64
+
65
+ const dateDiffInMs = new Date(versionPublishedDateString) - new Date(firstPublishedDateString)
66
+ let dateDiffInDays = 0
67
+ if (dateDiffInMs > 0) {
68
+ dateDiffInDays = Math.round(dateDiffInMs / (1000 * 60 * 60 * 24))
69
+ }
70
+
71
+ if (dateDiffInDays <= 30) {
72
+ throw new Error(
73
+ `The user ${npmUser.name} <${npmUser.email}> published this package for the first time only ${dateDiffInDays} days ago. Proceed with care.`
74
+ )
75
+ }
44
76
 
45
- return data
46
- })
77
+ return versionPublishedDateString
47
78
  }
48
79
  }
49
80
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npq",
3
- "version": "3.1.0",
3
+ "version": "3.2.1",
4
4
  "description": "marshall your npm/npm package installs with high quality and class 🎖",
5
5
  "bin": {
6
6
  "npq": "./bin/npq.js",