Skip to content

Commit 674b001

Browse files
refactor: improve package validation and cache hit tracking
- replace custom regex in sanitizePackageName with validate-npm-package-name for alignment with npm’s official rules - add local type declaration for validate-npm-package-name to fix TS7016 error - refactor package filtering to use a loop instead of .filter() with side effects, ensuring accurate cache hit metrics Notes: - kept existing setTimeout(...).unref() pattern as-is (did not switch to timers/promises) per decision
1 parent 80787b9 commit 674b001

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,8 @@
114114
"volta": {
115115
"node": "20.1.0",
116116
"npm": "8.19.4"
117+
},
118+
"dependencies": {
119+
"validate-npm-package-name": "^6.0.2"
117120
}
118121
}

src/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as fs from "fs/promises";
33
import * as fsSync from "fs";
44
import * as path from "path";
55
import { setTimeout as nodeSetTimeout } from "timers";
6-
6+
import validate = require("validate-npm-package-name");
77
import {
88
combinePaths,
99
createGetCanonicalFileName,
@@ -198,10 +198,9 @@ class NpmClient {
198198
}
199199

200200
private sanitizePackageName(name: string): string {
201-
// Allow scoped packages (@scope/name) and regular packages
202-
// See: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#name
203-
const validPattern = /^(?:@([a-z0-9-]+)\/)?[a-z0-9][a-z0-9.-]{0,213}$/;
204-
if (!validPattern.test(name)) {
201+
const result = validate(name);
202+
203+
if (!result.validForNewPackages && !result.validForOldPackages) {
205204
throw new Error(`Invalid package name: ${name}`);
206205
}
207206

@@ -815,14 +814,14 @@ class NodeTypingsInstaller extends ts.server.typingsInstaller.TypingsInstaller {
815814

816815
try {
817816
// Filter packages that are already successfully installed
818-
const packagesToInstall = packageNames.filter((pkg) => {
819-
const isCached =
820-
this.installationCache.isRecentlyInstalled(pkg);
821-
if (isCached) {
817+
const packagesToInstall: string[] = [];
818+
for (const pkg of packageNames) {
819+
if (this.installationCache.isRecentlyInstalled(pkg)) {
822820
this.metrics.cacheHits++;
821+
} else {
822+
packagesToInstall.push(pkg);
823823
}
824-
return !isCached;
825-
});
824+
}
826825

827826
if (packagesToInstall.length === 0) {
828827
if (this.log.isEnabled()) {

src/typingsInstaller/tsconfig.json

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
{
2-
"extends": "../tsconfig-base",
3-
"compilerOptions": {
4-
"types": [
5-
"node"
6-
]
7-
},
8-
"references": [
9-
{ "path": "../typescript" }
10-
],
11-
"include": ["**/*"]
12-
}
1+
{
2+
"extends": "../tsconfig-base",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
},
6+
"references": [{ "path": "../typescript" }],
7+
"include": ["**/*"]
8+
}

0 commit comments

Comments
 (0)