fix(smartupdate): improve update check caching, validation, and error handling

This commit is contained in:
2026-05-10 15:05:00 +00:00
parent d049d1a1e9
commit 3d1a73cf9e
12 changed files with 376 additions and 114 deletions
+97 -25
View File
@@ -3,13 +3,18 @@ import { UpdateCacheManager } from './smartupdate.classes.cachemanager.js';
import { UpdateNotifier } from './smartupdate.classes.notifier.js';
import { DEFAULT_CACHE_DURATION_HOURS, MILLISECONDS_PER_MINUTE, MINUTES_PER_HOUR } from './smartupdate.constants.js';
import type {
ICacheStatus,
ISmartUpdateOptions,
IUpdateCheckOptions,
IUpdateCheckResult,
TCachedUpdateStatus,
} from './smartupdate.interfaces.js';
import {
SmartUpdateError,
RegistryUnavailableError,
PackageNotFoundError,
InvalidPackageNameError,
InvalidVersionError,
} from './smartupdate.errors.js';
/**
@@ -38,7 +43,7 @@ export class SmartUpdate {
/**
* @deprecated Use the options parameter instead of kvStore property
*/
public kvStore: plugins.npmextra.KeyValueStore;
public kvStore: plugins.npmextra.KeyValueStore<Record<string, ICacheStatus>>;
constructor(options: ISmartUpdateOptions = {}) {
this.options = options;
@@ -56,6 +61,9 @@ export class SmartUpdate {
// Initialize cache manager
this.cacheManager = new UpdateCacheManager({
durationMs: cacheDurationMs || DEFAULT_CACHE_DURATION_HOURS * MINUTES_PER_HOUR * MILLISECONDS_PER_MINUTE,
storeIdentifier: options.cacheStore?.storeIdentifier,
storeType: options.cacheStore?.storeType,
customPath: options.cacheStore?.customPath,
});
// Initialize notifier
@@ -72,7 +80,7 @@ export class SmartUpdate {
/**
* Check for updates with caching (primarily for CLI use)
*
* @deprecated Use checkForUpdate with cacheStrategy: 'always' instead
* @deprecated Use checkForUpdate with cacheStrategy: 'time-based' instead
* @param packageName - The npm package name to check
* @param currentVersion - The current version to compare against
* @param changelogUrl - Optional URL to open if update is available
@@ -92,10 +100,15 @@ export class SmartUpdate {
packageName,
currentVersion,
changelogUrl,
cacheStrategy: 'always',
cacheStrategy: 'time-based',
});
return result.status === 'update-available';
return (
result.status === 'update-available' ||
(result.status === 'check-skipped' &&
!!result.latestVersion &&
this.safeIsUpdateAvailable(result.latestVersion, currentVersion))
);
}
/**
@@ -129,12 +142,19 @@ export class SmartUpdate {
const checkTime = new Date();
try {
this.validatePackageName(packageName);
const versionLocal = this.createSmartVersion(currentVersion);
const registryUrl = this.getRegistryUrl();
// Check if we should use cache or check registry
const cacheCheck = await this.cacheManager.shouldCheckRegistry(packageName, cacheStrategy);
const cacheCheck = await this.cacheManager.shouldCheckRegistry(packageName, cacheStrategy, {
currentVersion,
registryUrl,
});
// If we should skip the check due to cache
if (!cacheCheck.shouldCheck) {
return {
const skippedResult: IUpdateCheckResult = {
status: 'check-skipped',
packageName,
currentVersion,
@@ -142,23 +162,26 @@ export class SmartUpdate {
checkTime,
cacheHit: true,
nextCheckTime: cacheCheck.nextCheckTime,
reason: 'Rate limited - checked recently',
reason:
cacheStrategy === 'always'
? 'Cached result reused'
: 'Rate limited - checked recently',
};
this.notifier.notifyUpdateCheckResult(skippedResult);
return skippedResult;
}
// Fetch package info from registry
const npmPackage = await this.getNpmPackageFromRegistry(packageName);
if (!npmPackage) {
throw new RegistryUnavailableError();
}
// Compare versions
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version);
const versionLocal = new plugins.smartversion.SmartVersion(currentVersion);
const versionNpm = this.createSmartVersion(npmPackage.version);
const status: TCachedUpdateStatus = versionNpm.greaterThan(versionLocal)
? 'update-available'
: 'up-to-date';
const result: IUpdateCheckResult = {
status: versionNpm.greaterThan(versionLocal) ? 'update-available' : 'up-to-date',
status,
packageName: npmPackage.name,
currentVersion,
latestVersion: npmPackage.version,
@@ -172,14 +195,20 @@ export class SmartUpdate {
// If update is available, handle changelog
if (result.status === 'update-available' && changelogUrl && openChangelog && !process.env.CI) {
this.notifier.notifyOpeningChangelog();
plugins.smartopen.openUrl(changelogUrl);
await plugins.smartopen.openUrl(changelogUrl).catch((error) => {
this.notifier.warn(
`Could not open changelog: ${error instanceof Error ? error.message : String(error)}`
);
});
}
// Update cache if there's an update
if (result.status === 'update-available') {
const cacheStatus = this.cacheManager.createCacheStatus(npmPackage.version, false);
await this.cacheManager.setCached(packageName, cacheStatus);
}
// Cache both positive and negative checks to avoid repeated registry hits.
const cacheStatus = this.cacheManager.createCacheStatus(npmPackage.version, false, {
currentVersion,
registryUrl,
status,
});
await this.cacheManager.setCached(packageName, cacheStatus);
return result;
} catch (error) {
@@ -242,10 +271,8 @@ export class SmartUpdate {
* ```
*/
public async getLatestVersion(packageName: string): Promise<string> {
this.validatePackageName(packageName);
const npmPackage = await this.getNpmPackageFromRegistry(packageName);
if (!npmPackage) {
throw new PackageNotFoundError(packageName);
}
return npmPackage.version;
}
@@ -267,13 +294,58 @@ export class SmartUpdate {
* Fetch package information from the npm registry
* @private
*/
private async getNpmPackageFromRegistry(packageName: string): Promise<plugins.smartnpm.NpmPackage | null> {
private async getNpmPackageFromRegistry(packageName: string): Promise<plugins.smartnpm.NpmPackage> {
this.notifier.notifyCheckingForUpdate(packageName);
try {
const npmPackage = await this.npmRegistry.getPackageInfo(packageName);
if (!npmPackage?.version) {
throw new PackageNotFoundError(packageName);
}
return npmPackage;
} catch (error) {
return null;
if (error instanceof SmartUpdateError) {
throw error;
}
const message = error instanceof Error ? error.message : String(error);
const lowerMessage = message.toLowerCase();
if (lowerMessage.includes('not found') || lowerMessage.includes('404')) {
throw new PackageNotFoundError(packageName);
}
throw new RegistryUnavailableError(message);
}
}
private validatePackageName(packageName: string): void {
if (!packageName || packageName.trim() !== packageName || /\s/.test(packageName)) {
throw new InvalidPackageNameError(packageName);
}
}
private createSmartVersion(version: string): plugins.smartversion.SmartVersion {
try {
return new plugins.smartversion.SmartVersion(version);
} catch (error) {
throw new InvalidVersionError(version, error instanceof Error ? error.message : String(error));
}
}
private isUpdateAvailable(latestVersion: string, currentVersion: string): boolean {
const versionNpm = this.createSmartVersion(latestVersion);
const versionLocal = this.createSmartVersion(currentVersion);
return versionNpm.greaterThan(versionLocal);
}
private safeIsUpdateAvailable(latestVersion: string, currentVersion: string): boolean {
try {
return this.isUpdateAvailable(latestVersion, currentVersion);
} catch {
return false;
}
}
private getRegistryUrl(): string {
return this.npmRegistry.options.npmRegistryUrl || 'https://registry.npmjs.org';
}
}