npq 3.2.3 → 3.4.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.
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+
3
+ const BaseMarshall = require('./baseMarshall')
4
+ const { marshallCategories } = require('./constants')
5
+
6
+ const path = require('path')
7
+ const levenshtein = require('fast-levenshtein')
8
+ const topPackagesRawJSON = require(path.join(__dirname, '../../data/top-packages.json'))
9
+
10
+ const MARSHALL_NAME = 'typosquatting'
11
+
12
+ class Marshall extends BaseMarshall {
13
+ constructor(options) {
14
+ super(options)
15
+ this.name = MARSHALL_NAME
16
+ this.categoryId = marshallCategories.PackageHealth.id
17
+ }
18
+
19
+ title() {
20
+ return 'Checking for typosquatting'
21
+ }
22
+
23
+ validate(pkg) {
24
+ let levenshteinDistance = null
25
+ let similarPackages = []
26
+ return new Promise((resolve, reject) => {
27
+ for (const popularPackageNameInRepository of topPackagesRawJSON) {
28
+ levenshteinDistance = levenshtein.get(pkg.packageName, popularPackageNameInRepository)
29
+
30
+ if (levenshteinDistance < 3) {
31
+ similarPackages.push(popularPackageNameInRepository)
32
+ }
33
+ }
34
+
35
+ if (similarPackages.length > 0) {
36
+ return reject(
37
+ new Error(
38
+ `Package name could be a typosquatting attempt for popular package(s): ${similarPackages.join(
39
+ ', '
40
+ )}`
41
+ )
42
+ )
43
+ }
44
+
45
+ return resolve([])
46
+ })
47
+ }
48
+ }
49
+
50
+ module.exports = Marshall
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npq",
3
- "version": "3.2.3",
3
+ "version": "3.4.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",
@@ -169,6 +170,7 @@
169
170
  }
170
171
  },
171
172
  "dependencies": {
173
+ "fast-levenshtein": "^3.0.0",
172
174
  "glob": "^10.3.10",
173
175
  "inquirer": "^8.2.6",
174
176
  "kleur": "^4.1.5",
@@ -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()