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
+13
View File
@@ -1,5 +1,6 @@
{ {
"@git.zone/cli": { "@git.zone/cli": {
"schemaVersion": 2,
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "code.foss.global", "githost": "code.foss.global",
@@ -11,11 +12,23 @@
"projectDomain": "push.rocks" "projectDomain": "push.rocks"
}, },
"release": { "release": {
"targets": {
"git": {
"enabled": true,
"remote": "origin"
},
"npm": {
"enabled": false,
"registries": [ "registries": [
"https://verdaccio.lossless.digital", "https://verdaccio.lossless.digital",
"https://registry.npmjs.org" "https://registry.npmjs.org"
], ],
"accessLevel": "public" "accessLevel": "public"
},
"docker": {
"enabled": false
}
}
} }
}, },
"@git.zone/tsdoc": { "@git.zone/tsdoc": {
+10
View File
@@ -1,5 +1,15 @@
# Changelog # Changelog
## Pending
### Fixes
- improve update check caching, validation, and error handling (smartupdate)
- cache both update-available and up-to-date results with package version and registry context to avoid stale or incorrect cache reuse
- return cached CLI update results correctly for time-based checks and handle changelog opening failures without unhandled rejections
- validate package names and versions before registry access, preserve specific error types, and add deterministic cache-focused tests
- add configurable cache store options and remove the unused smarttime dependency
## 2026-05-01 - 2.0.7 - fix(build) ## 2026-05-01 - 2.0.7 - fix(build)
modernize build configuration and tighten TypeScript typings modernize build configuration and tighten TypeScript typings
Generated
-2
View File
@@ -8,7 +8,6 @@
"npm:@push.rocks/npmextra@^5.3.3": "5.3.3", "npm:@push.rocks/npmextra@^5.3.3": "5.3.3",
"npm:@push.rocks/smartnpm@^2.0.6": "2.0.6", "npm:@push.rocks/smartnpm@^2.0.6": "2.0.6",
"npm:@push.rocks/smartopen@2": "2.0.0", "npm:@push.rocks/smartopen@2": "2.0.0",
"npm:@push.rocks/smarttime@^4.2.3": "4.2.3",
"npm:@push.rocks/smartversion@^3.1.0": "3.1.0", "npm:@push.rocks/smartversion@^3.1.0": "3.1.0",
"npm:@types/lodash.clonedeep@^4.5.9": "4.5.9", "npm:@types/lodash.clonedeep@^4.5.9": "4.5.9",
"npm:@types/node@^25.6.0": "25.6.0" "npm:@types/node@^25.6.0": "25.6.0"
@@ -6742,7 +6741,6 @@
"npm:@push.rocks/npmextra@^5.3.3", "npm:@push.rocks/npmextra@^5.3.3",
"npm:@push.rocks/smartnpm@^2.0.6", "npm:@push.rocks/smartnpm@^2.0.6",
"npm:@push.rocks/smartopen@2", "npm:@push.rocks/smartopen@2",
"npm:@push.rocks/smarttime@^4.2.3",
"npm:@push.rocks/smartversion@^3.1.0", "npm:@push.rocks/smartversion@^3.1.0",
"npm:@types/lodash.clonedeep@^4.5.9", "npm:@types/lodash.clonedeep@^4.5.9",
"npm:@types/node@^25.6.0" "npm:@types/node@^25.6.0"
-1
View File
@@ -25,7 +25,6 @@
"@push.rocks/npmextra": "^5.3.3", "@push.rocks/npmextra": "^5.3.3",
"@push.rocks/smartnpm": "^2.0.6", "@push.rocks/smartnpm": "^2.0.6",
"@push.rocks/smartopen": "^2.0.0", "@push.rocks/smartopen": "^2.0.0",
"@push.rocks/smarttime": "^4.2.3",
"@push.rocks/smartversion": "^3.1.0" "@push.rocks/smartversion": "^3.1.0"
}, },
"files": [ "files": [
-3
View File
@@ -20,9 +20,6 @@ importers:
'@push.rocks/smartopen': '@push.rocks/smartopen':
specifier: ^2.0.0 specifier: ^2.0.0
version: 2.0.0 version: 2.0.0
'@push.rocks/smarttime':
specifier: ^4.2.3
version: 4.2.3
'@push.rocks/smartversion': '@push.rocks/smartversion':
specifier: ^3.1.0 specifier: ^3.1.0
version: 3.1.0 version: 3.1.0
+22
View File
@@ -0,0 +1,22 @@
# smartupdate improvement plan
Status: implemented.
## Goal
Make update checks predictable, cache-safe, and testable without changing the core public API shape.
## Implementation
- Fix CLI cache behavior so cached update information still reports an available update and time-based cache entries expire.
- Cache both `update-available` and `up-to-date` results so normal repeated CLI runs do not keep hitting the registry.
- Store cache context with each entry and only reuse cache entries for the same package, current version, and registry.
- Preserve more useful error types for invalid package names, invalid versions, package lookup failures, and registry failures.
- Await and catch changelog opening so browser failures do not become unhandled promise rejections.
- Replace live npm tests with deterministic registry stubs and ephemeral cache storage.
- Remove unused dependency surface.
## Verification
- Run `pnpm test`.
- Run `pnpm run build`.
+148 -61
View File
@@ -1,126 +1,213 @@
import { expect, tap } from '@git.zone/tstest/tapbundle'; import { expect, tap } from '@git.zone/tstest/tapbundle';
import type * as smartnpm from '@push.rocks/smartnpm';
import * as smartupdate from '../ts/index.js'; import * as smartupdate from '../ts/index.js';
let testSmartUpdate: smartupdate.SmartUpdate; const createMockedSmartUpdate = (versionsArg: Record<string, string>) => {
let silentSmartUpdate: smartupdate.SmartUpdate; const checkedPackages: string[] = [];
const testSmartUpdate = new smartupdate.SmartUpdate({
logLevel: 'SILENT',
noColor: true,
cacheDuration: { minutes: 30 },
cacheStore: { storeType: 'ephemeral' },
});
testSmartUpdate.npmRegistry.getPackageInfo = async (packageNameArg: string) => {
checkedPackages.push(packageNameArg);
const version = versionsArg[packageNameArg];
if (!version) {
throw new Error(`Package not found: ${packageNameArg}`);
}
return {
name: packageNameArg,
version,
} as unknown as smartnpm.NpmPackage;
};
return { testSmartUpdate, checkedPackages };
};
// Test suite for backward compatibility
tap.test('backward compatibility: should create an instance of SmartUpdate', async () => { tap.test('backward compatibility: should create an instance of SmartUpdate', async () => {
testSmartUpdate = new smartupdate.SmartUpdate(); const { testSmartUpdate } = createMockedSmartUpdate({ lodash: '4.17.21' });
expect(testSmartUpdate).toBeInstanceOf(smartupdate.SmartUpdate); expect(testSmartUpdate).toBeInstanceOf(smartupdate.SmartUpdate);
}); });
tap.test('backward compatibility: should check for a npm module using old API', async () => { tap.test('backward compatibility: should check for a npm module using old API', async () => {
const { testSmartUpdate } = createMockedSmartUpdate({ lodash: '4.17.21' });
const result = await testSmartUpdate.check('lodash', '1.0.5'); const result = await testSmartUpdate.check('lodash', '1.0.5');
expect(result).toBeTrue(); expect(result).toBeTrue();
}); });
// Test suite for new modern API
tap.test('modern API: should create SmartUpdate with custom options', async () => {
silentSmartUpdate = new smartupdate.SmartUpdate({
logLevel: 'SILENT',
noColor: true,
cacheDuration: { minutes: 30 },
});
expect(silentSmartUpdate).toBeInstanceOf(smartupdate.SmartUpdate);
});
tap.test('modern API: checkForUpdate should return rich result object', async () => { tap.test('modern API: checkForUpdate should return rich result object', async () => {
const result = await silentSmartUpdate.checkForUpdate({ const { testSmartUpdate } = createMockedSmartUpdate({ lodash: '4.17.21' });
const result = await testSmartUpdate.checkForUpdate({
packageName: 'lodash', packageName: 'lodash',
currentVersion: '1.0.0', currentVersion: '1.0.0',
cacheStrategy: 'never', cacheStrategy: 'never',
}); });
// Verify result structure
expect(result).toBeTypeOf('object'); expect(result).toBeTypeOf('object');
expect(result.status).toBeTypeOf('string'); expect(result.status).toEqual('update-available');
expect(result.packageName).toEqual('lodash'); expect(result.packageName).toEqual('lodash');
expect(result.currentVersion).toEqual('1.0.0'); expect(result.currentVersion).toEqual('1.0.0');
expect(result.latestVersion).toEqual('4.17.21');
expect(result.checkTime).toBeInstanceOf(Date); expect(result.checkTime).toBeInstanceOf(Date);
expect(result.cacheHit).toBeTypeOf('boolean'); expect(result.cacheHit).toBeFalse();
// Should have update available (1.0.0 is old)
expect(result.status).toEqual('update-available');
expect(result.latestVersion).toBeTypeOf('string');
}); });
tap.test('modern API: checkForUpdate with up-to-date version', async () => { tap.test('modern API: checkForUpdate with up-to-date version', async () => {
const result = await silentSmartUpdate.checkForUpdate({ const { testSmartUpdate } = createMockedSmartUpdate({ '@push.rocks/smartversion': '3.1.0' });
const result = await testSmartUpdate.checkForUpdate({
packageName: '@push.rocks/smartversion', packageName: '@push.rocks/smartversion',
currentVersion: '999.999.999', // Future version currentVersion: '999.999.999',
cacheStrategy: 'never', cacheStrategy: 'never',
}); });
expect(result.status).toEqual('up-to-date'); expect(result.status).toEqual('up-to-date');
expect(result.latestVersion).toBeTypeOf('string'); expect(result.latestVersion).toEqual('3.1.0');
}); });
tap.test('modern API: getLatestVersion utility method', async () => { tap.test('modern API: getLatestVersion utility method', async () => {
const latestVersion = await silentSmartUpdate.getLatestVersion('lodash'); const { testSmartUpdate } = createMockedSmartUpdate({ lodash: '4.17.21' });
expect(latestVersion).toBeTypeOf('string'); const latestVersion = await testSmartUpdate.getLatestVersion('lodash');
expect(latestVersion).toMatch(/^\d+\.\d+\.\d+/); // Semver format expect(latestVersion).toEqual('4.17.21');
}); });
tap.test('modern API: error handling for non-existent package', async () => { tap.test('modern API: error handling for non-existent package', async () => {
const result = await silentSmartUpdate.checkForUpdate({ const { testSmartUpdate } = createMockedSmartUpdate({});
const result = await testSmartUpdate.checkForUpdate({
packageName: 'this-package-definitely-does-not-exist-12345', packageName: 'this-package-definitely-does-not-exist-12345',
currentVersion: '1.0.0', currentVersion: '1.0.0',
cacheStrategy: 'never', cacheStrategy: 'never',
}); });
expect(result.status).toEqual('error'); expect(result.status).toEqual('error');
expect(result.error).toBeInstanceOf(Error); expect(result.error).toBeInstanceOf(smartupdate.PackageNotFoundError);
}); });
tap.test('modern API: cache strategy works', async () => { tap.test('modern API: invalid versions are reported before registry access', async () => {
// Clear cache first to ensure clean state const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ lodash: '4.17.21' });
const testPackage = 'express'; const result = await testSmartUpdate.checkForUpdate({
try { packageName: 'lodash',
await silentSmartUpdate.clearCache(testPackage); currentVersion: 'not-a-version',
} catch (e) { cacheStrategy: 'never',
// Cache might not exist, that's fine });
}
// First check - should hit registry with 'never' strategy expect(result.status).toEqual('error');
const result1 = await silentSmartUpdate.checkForUpdate({ expect(result.error).toBeInstanceOf(smartupdate.InvalidVersionError);
packageName: testPackage, expect(checkedPackages).toHaveLength(0);
});
tap.test('modern API: invalid package names are reported before registry access', async () => {
const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ lodash: '4.17.21' });
const result = await testSmartUpdate.checkForUpdate({
packageName: 'invalid package name',
currentVersion: '1.0.0', currentVersion: '1.0.0',
cacheStrategy: 'never', cacheStrategy: 'never',
}); });
expect(result1.cacheHit).toBeFalse(); expect(result.status).toEqual('error');
expect(result1.status).toEqual('update-available'); expect(result.error).toBeInstanceOf(smartupdate.InvalidPackageNameError);
expect(checkedPackages).toHaveLength(0);
});
// Do a second check with time-based that will create cache tap.test('modern API: caches update-available results for time-based checks', async () => {
const result2 = await silentSmartUpdate.checkForUpdate({ const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ express: '5.0.0' });
packageName: testPackage,
const result1 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '1.0.0',
cacheStrategy: 'time-based',
});
const result2 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '1.0.0', currentVersion: '1.0.0',
cacheStrategy: 'time-based', cacheStrategy: 'time-based',
}); });
// Third immediate check - should use cache with 'always' strategy expect(result1.status).toEqual('update-available');
const result3 = await silentSmartUpdate.checkForUpdate({ expect(result2.status).toEqual('check-skipped');
packageName: testPackage, expect(result2.cacheHit).toBeTrue();
currentVersion: '1.0.0', expect(result2.latestVersion).toEqual('5.0.0');
cacheStrategy: 'always', expect(checkedPackages).toHaveLength(1);
});
expect(result3.cacheHit).toBeTrue();
expect(result3.status).toEqual('check-skipped');
// nextCheckTime may or may not be set depending on cache strategy implementation
if (result3.nextCheckTime) {
expect(result3.nextCheckTime).toBeInstanceOf(Date);
}
}); });
tap.test('modern API: exports all types and classes', async () => { tap.test('modern API: caches up-to-date results for time-based checks', async () => {
// Verify exports const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ express: '5.0.0' });
const result1 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '5.0.0',
cacheStrategy: 'time-based',
});
const result2 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '5.0.0',
cacheStrategy: 'time-based',
});
expect(result1.status).toEqual('up-to-date');
expect(result2.status).toEqual('check-skipped');
expect(result2.cacheHit).toBeTrue();
expect(result2.latestVersion).toEqual('5.0.0');
expect(checkedPackages).toHaveLength(1);
});
tap.test('modern API: cache entries are scoped to the checked current version', async () => {
const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ express: '5.0.0' });
const result1 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '1.0.0',
cacheStrategy: 'time-based',
});
const result2 = await testSmartUpdate.checkForUpdate({
packageName: 'express',
currentVersion: '5.0.0',
cacheStrategy: 'time-based',
});
expect(result1.status).toEqual('update-available');
expect(result2.status).toEqual('up-to-date');
expect(result2.cacheHit).toBeFalse();
expect(checkedPackages).toHaveLength(2);
});
tap.test('modern API: checkForCli returns true for cached known updates', async () => {
const { testSmartUpdate, checkedPackages } = createMockedSmartUpdate({ 'my-cli': '2.0.0' });
const result1 = await testSmartUpdate.checkForCli('my-cli', '1.0.0');
const result2 = await testSmartUpdate.checkForCli('my-cli', '1.0.0');
expect(result1).toBeTrue();
expect(result2).toBeTrue();
expect(checkedPackages).toHaveLength(1);
});
tap.test('cache manager: clears package-specific and complete cache data', async () => {
const cacheManager = new smartupdate.UpdateCacheManager({
durationMs: 60_000,
storeType: 'ephemeral',
});
await cacheManager.setCached('one', cacheManager.createCacheStatus('1.0.0'));
await cacheManager.setCached('two', cacheManager.createCacheStatus('2.0.0'));
await cacheManager.clearCache('one');
expect(await cacheManager.getCached('one')).toBeNull();
expect(await cacheManager.getCached('two')).toBeTypeOf('object');
await cacheManager.clearCache();
expect(await cacheManager.getCached('two')).toBeNull();
});
tap.test('modern API: exports all runtime classes and constants', async () => {
expect(smartupdate.SmartUpdate).toBeTypeOf('function'); expect(smartupdate.SmartUpdate).toBeTypeOf('function');
expect(smartupdate.UpdateCacheManager).toBeTypeOf('function'); expect(smartupdate.UpdateCacheManager).toBeTypeOf('function');
expect(smartupdate.UpdateNotifier).toBeTypeOf('function'); expect(smartupdate.UpdateNotifier).toBeTypeOf('function');
expect(smartupdate.RegistryUnavailableError).toBeTypeOf('function'); expect(smartupdate.RegistryUnavailableError).toBeTypeOf('function');
expect(smartupdate.PackageNotFoundError).toBeTypeOf('function'); expect(smartupdate.PackageNotFoundError).toBeTypeOf('function');
expect(smartupdate.InvalidVersionError).toBeTypeOf('function');
expect(smartupdate.InvalidPackageNameError).toBeTypeOf('function');
expect(smartupdate.LOG_LEVELS).toBeTypeOf('object'); expect(smartupdate.LOG_LEVELS).toBeTypeOf('object');
}); });
+3
View File
@@ -13,6 +13,9 @@ export type {
ICacheStatus, ICacheStatus,
ICacheOptions, ICacheOptions,
INotificationOptions, INotificationOptions,
TCacheStrategy,
TCacheStoreType,
TCachedUpdateStatus,
} from './smartupdate.interfaces.js'; } from './smartupdate.interfaces.js';
// Error classes // Error classes
+47 -12
View File
@@ -1,18 +1,21 @@
import * as plugins from './smartupdate.plugins.js'; import * as plugins from './smartupdate.plugins.js';
import type { ICacheStatus, ICacheOptions } from './smartupdate.interfaces.js'; import type { ICacheStatus, ICacheOptions, TCacheStrategy, TCachedUpdateStatus } from './smartupdate.interfaces.js';
type TCacheStoreData = Record<string, ICacheStatus>;
/** /**
* Manages caching of update check results * Manages caching of update check results
*/ */
export class UpdateCacheManager { export class UpdateCacheManager {
public readonly kvStore: plugins.npmextra.KeyValueStore; public readonly kvStore: plugins.npmextra.KeyValueStore<TCacheStoreData>;
private cacheDurationMs: number; private cacheDurationMs: number;
constructor(options: ICacheOptions) { constructor(options: ICacheOptions) {
this.cacheDurationMs = options.durationMs; this.cacheDurationMs = options.durationMs;
this.kvStore = new plugins.npmextra.KeyValueStore({ this.kvStore = new plugins.npmextra.KeyValueStore<TCacheStoreData>({
typeArg: 'userHomeDir', typeArg: options.storeType || 'userHomeDir',
identityArg: options.storeIdentifier || 'global_smartupdate', identityArg: options.storeIdentifier || 'global_smartupdate',
customPath: options.customPath,
}); });
} }
@@ -27,7 +30,7 @@ export class UpdateCacheManager {
* Get cached status for a package * Get cached status for a package
*/ */
public async getCached(packageName: string): Promise<ICacheStatus | null> { public async getCached(packageName: string): Promise<ICacheStatus | null> {
return await this.kvStore.readKey(packageName); return (await this.kvStore.readKey(packageName)) || null;
} }
/** /**
@@ -42,11 +45,12 @@ export class UpdateCacheManager {
*/ */
public async clearCache(packageName?: string): Promise<void> { public async clearCache(packageName?: string): Promise<void> {
if (packageName) { if (packageName) {
await this.kvStore.deleteKey(packageName); const cacheData = await this.kvStore.readAll();
delete cacheData[packageName];
await this.kvStore.wipe();
await this.kvStore.writeAll(cacheData);
} else { } else {
// Clear all keys - this requires reading all keys first await this.kvStore.wipe();
// For now, we'll skip implementing full cache clear as it requires more kvStore API
throw new Error('Clearing all cache entries is not yet implemented');
} }
} }
@@ -56,7 +60,11 @@ export class UpdateCacheManager {
*/ */
public async shouldCheckRegistry( public async shouldCheckRegistry(
packageName: string, packageName: string,
strategy: 'always' | 'never' | 'time-based' = 'time-based' strategy: TCacheStrategy = 'time-based',
cacheContext: {
currentVersion?: string;
registryUrl?: string;
} = {}
): Promise<{ ): Promise<{
shouldCheck: boolean; shouldCheck: boolean;
cacheStatus?: ICacheStatus; cacheStatus?: ICacheStatus;
@@ -72,7 +80,7 @@ export class UpdateCacheManager {
const cacheStatus = await this.getCached(packageName); const cacheStatus = await this.getCached(packageName);
// No cache exists // No cache exists
if (!cacheStatus) { if (!cacheStatus || !this.cacheMatchesContext(cacheStatus, cacheContext)) {
return { shouldCheck: true }; return { shouldCheck: true };
} }
@@ -106,11 +114,38 @@ export class UpdateCacheManager {
/** /**
* Create a new cache status object * Create a new cache status object
*/ */
public createCacheStatus(latestVersion: string, performedUpgrade: boolean = false): ICacheStatus { public createCacheStatus(
latestVersion: string,
performedUpgrade: boolean = false,
metadata: {
currentVersion?: string;
registryUrl?: string;
status?: TCachedUpdateStatus;
} = {}
): ICacheStatus {
return { return {
lastCheck: Date.now(), lastCheck: Date.now(),
latestVersion, latestVersion,
currentVersion: metadata.currentVersion,
registryUrl: metadata.registryUrl,
status: metadata.status,
performedUpgrade, performedUpgrade,
}; };
} }
private cacheMatchesContext(
cacheStatus: ICacheStatus,
cacheContext: {
currentVersion?: string;
registryUrl?: string;
}
): boolean {
if (cacheContext.currentVersion && cacheStatus.currentVersion !== cacheContext.currentVersion) {
return false;
}
if (cacheContext.registryUrl && cacheStatus.registryUrl !== cacheContext.registryUrl) {
return false;
}
return true;
}
} }
+96 -24
View File
@@ -3,13 +3,18 @@ import { UpdateCacheManager } from './smartupdate.classes.cachemanager.js';
import { UpdateNotifier } from './smartupdate.classes.notifier.js'; import { UpdateNotifier } from './smartupdate.classes.notifier.js';
import { DEFAULT_CACHE_DURATION_HOURS, MILLISECONDS_PER_MINUTE, MINUTES_PER_HOUR } from './smartupdate.constants.js'; import { DEFAULT_CACHE_DURATION_HOURS, MILLISECONDS_PER_MINUTE, MINUTES_PER_HOUR } from './smartupdate.constants.js';
import type { import type {
ICacheStatus,
ISmartUpdateOptions, ISmartUpdateOptions,
IUpdateCheckOptions, IUpdateCheckOptions,
IUpdateCheckResult, IUpdateCheckResult,
TCachedUpdateStatus,
} from './smartupdate.interfaces.js'; } from './smartupdate.interfaces.js';
import { import {
SmartUpdateError,
RegistryUnavailableError, RegistryUnavailableError,
PackageNotFoundError, PackageNotFoundError,
InvalidPackageNameError,
InvalidVersionError,
} from './smartupdate.errors.js'; } from './smartupdate.errors.js';
/** /**
@@ -38,7 +43,7 @@ export class SmartUpdate {
/** /**
* @deprecated Use the options parameter instead of kvStore property * @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 = {}) { constructor(options: ISmartUpdateOptions = {}) {
this.options = options; this.options = options;
@@ -56,6 +61,9 @@ export class SmartUpdate {
// Initialize cache manager // Initialize cache manager
this.cacheManager = new UpdateCacheManager({ this.cacheManager = new UpdateCacheManager({
durationMs: cacheDurationMs || DEFAULT_CACHE_DURATION_HOURS * MINUTES_PER_HOUR * MILLISECONDS_PER_MINUTE, 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 // Initialize notifier
@@ -72,7 +80,7 @@ export class SmartUpdate {
/** /**
* Check for updates with caching (primarily for CLI use) * 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 packageName - The npm package name to check
* @param currentVersion - The current version to compare against * @param currentVersion - The current version to compare against
* @param changelogUrl - Optional URL to open if update is available * @param changelogUrl - Optional URL to open if update is available
@@ -92,10 +100,15 @@ export class SmartUpdate {
packageName, packageName,
currentVersion, currentVersion,
changelogUrl, 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(); const checkTime = new Date();
try { try {
this.validatePackageName(packageName);
const versionLocal = this.createSmartVersion(currentVersion);
const registryUrl = this.getRegistryUrl();
// Check if we should use cache or check registry // 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 we should skip the check due to cache
if (!cacheCheck.shouldCheck) { if (!cacheCheck.shouldCheck) {
return { const skippedResult: IUpdateCheckResult = {
status: 'check-skipped', status: 'check-skipped',
packageName, packageName,
currentVersion, currentVersion,
@@ -142,23 +162,26 @@ export class SmartUpdate {
checkTime, checkTime,
cacheHit: true, cacheHit: true,
nextCheckTime: cacheCheck.nextCheckTime, 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 // Fetch package info from registry
const npmPackage = await this.getNpmPackageFromRegistry(packageName); const npmPackage = await this.getNpmPackageFromRegistry(packageName);
if (!npmPackage) {
throw new RegistryUnavailableError();
}
// Compare versions // Compare versions
const versionNpm = new plugins.smartversion.SmartVersion(npmPackage.version); const versionNpm = this.createSmartVersion(npmPackage.version);
const versionLocal = new plugins.smartversion.SmartVersion(currentVersion); const status: TCachedUpdateStatus = versionNpm.greaterThan(versionLocal)
? 'update-available'
: 'up-to-date';
const result: IUpdateCheckResult = { const result: IUpdateCheckResult = {
status: versionNpm.greaterThan(versionLocal) ? 'update-available' : 'up-to-date', status,
packageName: npmPackage.name, packageName: npmPackage.name,
currentVersion, currentVersion,
latestVersion: npmPackage.version, latestVersion: npmPackage.version,
@@ -172,14 +195,20 @@ export class SmartUpdate {
// If update is available, handle changelog // If update is available, handle changelog
if (result.status === 'update-available' && changelogUrl && openChangelog && !process.env.CI) { if (result.status === 'update-available' && changelogUrl && openChangelog && !process.env.CI) {
this.notifier.notifyOpeningChangelog(); 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 // Cache both positive and negative checks to avoid repeated registry hits.
if (result.status === 'update-available') { const cacheStatus = this.cacheManager.createCacheStatus(npmPackage.version, false, {
const cacheStatus = this.cacheManager.createCacheStatus(npmPackage.version, false); currentVersion,
registryUrl,
status,
});
await this.cacheManager.setCached(packageName, cacheStatus); await this.cacheManager.setCached(packageName, cacheStatus);
}
return result; return result;
} catch (error) { } catch (error) {
@@ -242,10 +271,8 @@ export class SmartUpdate {
* ``` * ```
*/ */
public async getLatestVersion(packageName: string): Promise<string> { public async getLatestVersion(packageName: string): Promise<string> {
this.validatePackageName(packageName);
const npmPackage = await this.getNpmPackageFromRegistry(packageName); const npmPackage = await this.getNpmPackageFromRegistry(packageName);
if (!npmPackage) {
throw new PackageNotFoundError(packageName);
}
return npmPackage.version; return npmPackage.version;
} }
@@ -267,13 +294,58 @@ export class SmartUpdate {
* Fetch package information from the npm registry * Fetch package information from the npm registry
* @private * @private
*/ */
private async getNpmPackageFromRegistry(packageName: string): Promise<plugins.smartnpm.NpmPackage | null> { private async getNpmPackageFromRegistry(packageName: string): Promise<plugins.smartnpm.NpmPackage> {
this.notifier.notifyCheckingForUpdate(packageName); this.notifier.notifyCheckingForUpdate(packageName);
try { try {
const npmPackage = await this.npmRegistry.getPackageInfo(packageName); const npmPackage = await this.npmRegistry.getPackageInfo(packageName);
if (!npmPackage?.version) {
throw new PackageNotFoundError(packageName);
}
return npmPackage; return npmPackage;
} catch (error) { } 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';
} }
} }
+29 -2
View File
@@ -1,12 +1,19 @@
import type { TLogLevel } from './smartupdate.constants.js'; import type { TLogLevel } from './smartupdate.constants.js';
import type * as smartnpm from '@push.rocks/smartnpm'; import type * as smartnpm from '@push.rocks/smartnpm';
export type TCacheStrategy = 'always' | 'never' | 'time-based';
export type TCacheStoreType = 'custom' | 'userHomeDir' | 'ephemeral';
export type TCachedUpdateStatus = 'up-to-date' | 'update-available';
/** /**
* Cache status stored for each package * Cache status stored for each package
*/ */
export interface ICacheStatus { export interface ICacheStatus {
lastCheck: number; lastCheck: number;
latestVersion: string; latestVersion: string;
currentVersion?: string;
registryUrl?: string;
status?: TCachedUpdateStatus;
performedUpgrade: boolean; performedUpgrade: boolean;
} }
@@ -46,6 +53,16 @@ export interface ISmartUpdateOptions {
* @default false * @default false
*/ */
noColor?: boolean; noColor?: boolean;
/**
* Cache storage configuration
* @default { storeType: 'userHomeDir', storeIdentifier: 'global_smartupdate' }
*/
cacheStore?: {
storeType?: TCacheStoreType;
storeIdentifier?: string;
customPath?: string;
};
} }
/** /**
@@ -77,12 +94,12 @@ export interface IUpdateCheckOptions {
/** /**
* Cache strategy for this check * Cache strategy for this check
* - 'always': Always check cache first (default for CLI) * - 'always': Always use an existing matching cache entry
* - 'never': Always check registry, bypass cache * - 'never': Always check registry, bypass cache
* - 'time-based': Check based on cache duration * - 'time-based': Check based on cache duration
* @default 'time-based' * @default 'time-based'
*/ */
cacheStrategy?: 'always' | 'never' | 'time-based'; cacheStrategy?: TCacheStrategy;
} }
/** /**
@@ -148,6 +165,16 @@ export interface ICacheOptions {
* Identifier for the key-value store * Identifier for the key-value store
*/ */
storeIdentifier?: string; storeIdentifier?: string;
/**
* Key-value store backend
*/
storeType?: TCacheStoreType;
/**
* Custom path for custom key-value stores
*/
customPath?: string;
} }
/** /**
+1 -2
View File
@@ -2,7 +2,6 @@ import * as consolecolor from '@push.rocks/consolecolor';
import * as npmextra from '@push.rocks/npmextra'; import * as npmextra from '@push.rocks/npmextra';
import * as smartnpm from '@push.rocks/smartnpm'; import * as smartnpm from '@push.rocks/smartnpm';
import * as smartopen from '@push.rocks/smartopen'; import * as smartopen from '@push.rocks/smartopen';
import * as smarttime from '@push.rocks/smarttime';
import * as smartversion from '@push.rocks/smartversion'; import * as smartversion from '@push.rocks/smartversion';
export { consolecolor, npmextra, smartnpm, smartopen, smarttime, smartversion }; export { consolecolor, npmextra, smartnpm, smartopen, smartversion };