native-update-capacitor 0.0.0-stage → 4.3.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.
- package/AI-INTEGRATION-GUIDE.md +578 -0
- package/LICENSE +25 -0
- package/NativeUpdate.podspec +19 -0
- package/README.md +574 -2
- package/android/build.gradle +125 -0
- package/android/consumer-rules.pro +9 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/android/gradle.properties +17 -0
- package/android/manifest-additions.xml +28 -0
- package/android/proguard-rules.pro +26 -0
- package/android/settings.gradle +2 -0
- package/android/src/main/AndroidManifest.xml +13 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppReviewPlugin.kt +159 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/AppUpdatePlugin.kt +314 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +406 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +48 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +343 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +236 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/JsonOptional.kt +26 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +1249 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +404 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/NotificationActionReceiver.kt +107 -0
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +240 -0
- package/android/variables.gradle +5 -0
- package/cli/commands/backend-create.js +623 -0
- package/cli/commands/bundle-create.js +145 -0
- package/cli/commands/bundle-sign.js +64 -0
- package/cli/commands/bundle-verify.js +58 -0
- package/cli/commands/init.js +146 -0
- package/cli/commands/keys-generate.js +58 -0
- package/cli/commands/monitor.js +89 -0
- package/cli/commands/remote-shared.js +262 -0
- package/cli/commands/remote.js +622 -0
- package/cli/commands/server-start.js +96 -0
- package/cli/index.js +605 -0
- package/cli/package.json +21 -0
- package/dist/esm/app-review/app-review-manager.d.ts +24 -0
- package/dist/esm/app-review/app-review-manager.js +194 -0
- package/dist/esm/app-review/index.d.ts +5 -0
- package/dist/esm/app-review/index.js +5 -0
- package/dist/esm/app-review/platform-review-handler.d.ts +20 -0
- package/dist/esm/app-review/platform-review-handler.js +137 -0
- package/dist/esm/app-review/review-conditions-checker.d.ts +22 -0
- package/dist/esm/app-review/review-conditions-checker.js +154 -0
- package/dist/esm/app-review/review-rate-limiter.d.ts +23 -0
- package/dist/esm/app-review/review-rate-limiter.js +163 -0
- package/dist/esm/app-review/types.d.ts +41 -0
- package/dist/esm/app-review/types.js +1 -0
- package/dist/esm/app-update/app-update-checker.d.ts +58 -0
- package/dist/esm/app-update/app-update-checker.js +227 -0
- package/dist/esm/app-update/app-update-installer.d.ts +20 -0
- package/dist/esm/app-update/app-update-installer.js +134 -0
- package/dist/esm/app-update/app-update-manager.d.ts +28 -0
- package/dist/esm/app-update/app-update-manager.js +198 -0
- package/dist/esm/app-update/app-update-notifier.d.ts +14 -0
- package/dist/esm/app-update/app-update-notifier.js +99 -0
- package/dist/esm/app-update/index.d.ts +6 -0
- package/dist/esm/app-update/index.js +6 -0
- package/dist/esm/app-update/platform-app-update.d.ts +19 -0
- package/dist/esm/app-update/platform-app-update.js +138 -0
- package/dist/esm/app-update/types.d.ts +58 -0
- package/dist/esm/app-update/types.js +11 -0
- package/dist/esm/background-update/background-scheduler.d.ts +63 -0
- package/dist/esm/background-update/background-scheduler.js +332 -0
- package/dist/esm/background-update/index.d.ts +3 -0
- package/dist/esm/background-update/index.js +2 -0
- package/dist/esm/background-update/notification-manager.d.ts +29 -0
- package/dist/esm/background-update/notification-manager.js +88 -0
- package/dist/esm/config/support.d.ts +9 -0
- package/dist/esm/config/support.js +9 -0
- package/dist/esm/core/analytics.d.ts +83 -0
- package/dist/esm/core/analytics.js +151 -0
- package/dist/esm/core/cache-manager.d.ts +72 -0
- package/dist/esm/core/cache-manager.js +274 -0
- package/dist/esm/core/config.d.ts +69 -0
- package/dist/esm/core/config.js +107 -0
- package/dist/esm/core/errors.d.ts +56 -0
- package/dist/esm/core/errors.js +85 -0
- package/dist/esm/core/event-emitter.d.ts +30 -0
- package/dist/esm/core/event-emitter.js +80 -0
- package/dist/esm/core/logger.d.ts +21 -0
- package/dist/esm/core/logger.js +108 -0
- package/dist/esm/core/performance.d.ts +53 -0
- package/dist/esm/core/performance.js +153 -0
- package/dist/esm/core/plugin-manager.d.ts +88 -0
- package/dist/esm/core/plugin-manager.js +224 -0
- package/dist/esm/core/security.d.ts +123 -0
- package/dist/esm/core/security.js +361 -0
- package/dist/esm/core/update-check.d.ts +121 -0
- package/dist/esm/core/update-check.js +127 -0
- package/dist/esm/definitions.d.ts +872 -0
- package/dist/esm/definitions.js +104 -0
- package/dist/esm/index.d.ts +14 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/live-update/bundle-manager.d.ts +102 -0
- package/dist/esm/live-update/bundle-manager.js +344 -0
- package/dist/esm/live-update/certificate-pinning.d.ts +38 -0
- package/dist/esm/live-update/certificate-pinning.js +77 -0
- package/dist/esm/live-update/delta-processor.d.ts +84 -0
- package/dist/esm/live-update/delta-processor.js +257 -0
- package/dist/esm/live-update/download-manager.d.ts +106 -0
- package/dist/esm/live-update/download-manager.js +412 -0
- package/dist/esm/live-update/rollout-checker.d.ts +86 -0
- package/dist/esm/live-update/rollout-checker.js +304 -0
- package/dist/esm/live-update/update-manager.d.ts +63 -0
- package/dist/esm/live-update/update-manager.js +321 -0
- package/dist/esm/live-update/version-manager.d.ts +93 -0
- package/dist/esm/live-update/version-manager.js +390 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.js +848 -0
- package/dist/esm/security/crypto.d.ts +88 -0
- package/dist/esm/security/crypto.js +228 -0
- package/dist/esm/security/validator.d.ts +60 -0
- package/dist/esm/security/validator.js +142 -0
- package/dist/esm/types/app-identity.d.ts +36 -0
- package/dist/esm/types/app-identity.js +1 -0
- package/dist/esm/types/rollout.d.ts +91 -0
- package/dist/esm/types/rollout.js +25 -0
- package/dist/esm/web.d.ts +130 -0
- package/dist/esm/web.js +981 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.esm.js +1 -0
- package/dist/plugin.js +2 -0
- package/docs/CHANGELOG.md +993 -0
- package/docs/KNOWN_LIMITATIONS.md +235 -0
- package/docs/MIGRATION.md +391 -0
- package/docs/protocol/CLIENT-PROTOCOL.md +178 -0
- package/docs/server-requirements.md +211 -0
- package/ios/Plugin/AppReview/AppReviewPlugin.swift +158 -0
- package/ios/Plugin/AppUpdate/AppUpdatePlugin.swift +249 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +333 -0
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +464 -0
- package/ios/Plugin/Info.plist +43 -0
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +1286 -0
- package/ios/Plugin/LiveUpdate/WebViewConfiguration.swift +45 -0
- package/ios/Plugin/NativeUpdatePlugin.m +53 -0
- package/ios/Plugin/NativeUpdatePlugin.swift +315 -0
- package/ios/Plugin/Security/SecurityManager.swift +289 -0
- package/package.json +135 -4
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { LatestVersion } from '../definitions';
|
|
2
|
+
/**
|
|
3
|
+
* Manages version checking and comparison
|
|
4
|
+
*/
|
|
5
|
+
export declare class VersionManager {
|
|
6
|
+
private readonly VERSION_CHECK_CACHE_KEY;
|
|
7
|
+
private readonly CACHE_DURATION;
|
|
8
|
+
private readonly logger;
|
|
9
|
+
private readonly configManager;
|
|
10
|
+
private readonly securityValidator;
|
|
11
|
+
private preferences;
|
|
12
|
+
private memoryCache;
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Compare two semantic versions
|
|
16
|
+
*/
|
|
17
|
+
static compareVersions(version1: string, version2: string): number;
|
|
18
|
+
/**
|
|
19
|
+
* Validate semantic version format
|
|
20
|
+
*/
|
|
21
|
+
static isValidVersion(version: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Check if update should be performed
|
|
24
|
+
*/
|
|
25
|
+
static shouldUpdate(currentVersion: string, newVersion: string, minAppVersion?: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Initialize the version manager
|
|
28
|
+
*/
|
|
29
|
+
initialize(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Check for latest version from server
|
|
32
|
+
*/
|
|
33
|
+
checkForUpdates(serverUrl: string, channel: string, currentVersion: string, appId: string): Promise<LatestVersion | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Compare two versions
|
|
36
|
+
*/
|
|
37
|
+
isNewerVersion(version1: string, version2: string): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Check if update is mandatory based on minimum version
|
|
40
|
+
*/
|
|
41
|
+
isUpdateMandatory(currentVersion: string, minimumVersion?: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Parse version metadata
|
|
44
|
+
*/
|
|
45
|
+
parseVersion(version: string): {
|
|
46
|
+
major: number;
|
|
47
|
+
minor: number;
|
|
48
|
+
patch: number;
|
|
49
|
+
prerelease?: string;
|
|
50
|
+
build?: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Generate version string from components
|
|
54
|
+
*/
|
|
55
|
+
buildVersionString(components: {
|
|
56
|
+
major: number;
|
|
57
|
+
minor: number;
|
|
58
|
+
patch: number;
|
|
59
|
+
prerelease?: string;
|
|
60
|
+
build?: string;
|
|
61
|
+
}): string;
|
|
62
|
+
/**
|
|
63
|
+
* Check if version is compatible with native version requirements
|
|
64
|
+
*/
|
|
65
|
+
isCompatibleWithNativeVersion(bundleVersion: string, nativeVersion: string, compatibility?: {
|
|
66
|
+
[key: string]: string;
|
|
67
|
+
}): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Get version from cache
|
|
70
|
+
*/
|
|
71
|
+
private getCachedVersionInfo;
|
|
72
|
+
/**
|
|
73
|
+
* Cache version info
|
|
74
|
+
*/
|
|
75
|
+
private cacheVersionInfo;
|
|
76
|
+
/**
|
|
77
|
+
* Clear version cache
|
|
78
|
+
*/
|
|
79
|
+
clearVersionCache(): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Check if downgrade protection should block update
|
|
82
|
+
*/
|
|
83
|
+
shouldBlockDowngrade(currentVersion: string, newVersion: string): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Persisted high-water-mark version per channel. Survives reinstalls that
|
|
86
|
+
* leave Preferences intact; protects against an attacker feeding an older
|
|
87
|
+
* (and potentially vulnerable) bundle even after a full cache wipe of
|
|
88
|
+
* downloaded bundles. Paired with `rememberAppliedVersion`.
|
|
89
|
+
*/
|
|
90
|
+
private maxVersionKey;
|
|
91
|
+
getMaxSeenVersion(channel: string): Promise<string | null>;
|
|
92
|
+
rememberAppliedVersion(channel: string, version: string): Promise<void>;
|
|
93
|
+
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { ConfigManager } from '../core/config';
|
|
2
|
+
import { Logger } from '../core/logger';
|
|
3
|
+
import { SecurityValidator } from '../core/security';
|
|
4
|
+
import { ValidationError, ErrorCode } from '../core/errors';
|
|
5
|
+
import { checkForUpdate } from '../core/update-check';
|
|
6
|
+
/**
|
|
7
|
+
* Manages version checking and comparison
|
|
8
|
+
*/
|
|
9
|
+
export class VersionManager {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.VERSION_CHECK_CACHE_KEY = 'capacitor_native_update_version_cache';
|
|
12
|
+
this.CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
|
|
13
|
+
this.preferences = null;
|
|
14
|
+
this.memoryCache = new Map();
|
|
15
|
+
this.logger = Logger.getInstance();
|
|
16
|
+
this.configManager = ConfigManager.getInstance();
|
|
17
|
+
this.securityValidator = SecurityValidator.getInstance();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Compare two semantic versions
|
|
21
|
+
*/
|
|
22
|
+
static compareVersions(version1, version2) {
|
|
23
|
+
try {
|
|
24
|
+
// Split version and pre-release
|
|
25
|
+
const [v1Base, v1Pre] = version1.split('-');
|
|
26
|
+
const [v2Base, v2Pre] = version2.split('-');
|
|
27
|
+
const v1Parts = v1Base.split('.').map(Number);
|
|
28
|
+
const v2Parts = v2Base.split('.').map(Number);
|
|
29
|
+
// Compare major.minor.patch
|
|
30
|
+
for (let i = 0; i < 3; i++) {
|
|
31
|
+
const v1Part = v1Parts[i] || 0;
|
|
32
|
+
const v2Part = v2Parts[i] || 0;
|
|
33
|
+
if (v1Part > v2Part)
|
|
34
|
+
return 1;
|
|
35
|
+
if (v1Part < v2Part)
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
// If base versions are equal, compare pre-release
|
|
39
|
+
if (v1Pre && !v2Pre)
|
|
40
|
+
return -1; // 1.0.0-alpha < 1.0.0
|
|
41
|
+
if (!v1Pre && v2Pre)
|
|
42
|
+
return 1; // 1.0.0 > 1.0.0-alpha
|
|
43
|
+
if (v1Pre && v2Pre) {
|
|
44
|
+
return v1Pre.localeCompare(v2Pre);
|
|
45
|
+
}
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
catch (_a) {
|
|
49
|
+
if (version1 === version2)
|
|
50
|
+
return 0;
|
|
51
|
+
return version1 > version2 ? 1 : -1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Validate semantic version format
|
|
56
|
+
*/
|
|
57
|
+
static isValidVersion(version) {
|
|
58
|
+
return /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/.test(version);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if update should be performed
|
|
62
|
+
*/
|
|
63
|
+
static shouldUpdate(currentVersion, newVersion, minAppVersion) {
|
|
64
|
+
if (minAppVersion &&
|
|
65
|
+
VersionManager.compareVersions(currentVersion, minAppVersion) < 0) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return VersionManager.compareVersions(currentVersion, newVersion) < 0;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Initialize the version manager
|
|
72
|
+
*/
|
|
73
|
+
async initialize() {
|
|
74
|
+
this.preferences = this.configManager.get('preferences');
|
|
75
|
+
if (!this.preferences) {
|
|
76
|
+
throw new ValidationError(ErrorCode.MISSING_DEPENDENCY, 'Preferences not configured. Please configure the plugin first.');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Check for latest version from server
|
|
81
|
+
*/
|
|
82
|
+
async checkForUpdates(serverUrl, channel, currentVersion, appId) {
|
|
83
|
+
var _a;
|
|
84
|
+
// Validate inputs
|
|
85
|
+
this.securityValidator.validateUrl(serverUrl);
|
|
86
|
+
this.securityValidator.validateVersion(currentVersion);
|
|
87
|
+
if (!channel || !appId) {
|
|
88
|
+
throw new ValidationError(ErrorCode.INVALID_CONFIG, 'Channel and appId are required');
|
|
89
|
+
}
|
|
90
|
+
// Check cache first
|
|
91
|
+
const cacheKey = `${channel}-${appId}`;
|
|
92
|
+
const cached = await this.getCachedVersionInfo(cacheKey);
|
|
93
|
+
if (cached &&
|
|
94
|
+
cached.channel === channel &&
|
|
95
|
+
Date.now() - cached.timestamp < this.CACHE_DURATION) {
|
|
96
|
+
this.logger.debug('Returning cached version info', {
|
|
97
|
+
channel,
|
|
98
|
+
version: cached.data.version,
|
|
99
|
+
});
|
|
100
|
+
return cached.data;
|
|
101
|
+
}
|
|
102
|
+
// The Native Update backend serves update checks at
|
|
103
|
+
// `/v1/updates/check` (API-key auth), NOT `/check`. Route through the
|
|
104
|
+
// shared helper so this path matches plugin.checkForUpdate() exactly.
|
|
105
|
+
// Without an API key the backend returns 401, so bail early with null.
|
|
106
|
+
const apiKey = this.configManager.get('apiKey');
|
|
107
|
+
if (!apiKey) {
|
|
108
|
+
this.logger.warn('VersionManager.checkForUpdates: no apiKey configured — the Native Update backend requires X-API-Key');
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const data = await checkForUpdate(serverUrl, {
|
|
113
|
+
apiKey,
|
|
114
|
+
channel,
|
|
115
|
+
currentVersion,
|
|
116
|
+
// JS-layer fallback identity; native background checks run through
|
|
117
|
+
// the native scheduler with the real device id + platform.
|
|
118
|
+
deviceId: 'web-client',
|
|
119
|
+
platform: 'web',
|
|
120
|
+
appVersion: currentVersion,
|
|
121
|
+
signal: AbortSignal.timeout(this.configManager.get('downloadTimeout')),
|
|
122
|
+
});
|
|
123
|
+
const latest = {
|
|
124
|
+
available: data.available,
|
|
125
|
+
version: data.version,
|
|
126
|
+
url: data.downloadUrl,
|
|
127
|
+
mandatory: data.mandatory,
|
|
128
|
+
notes: (_a = data.releaseNotes) !== null && _a !== void 0 ? _a : undefined,
|
|
129
|
+
size: data.size,
|
|
130
|
+
checksum: data.checksum,
|
|
131
|
+
};
|
|
132
|
+
// No update is a normal, non-error outcome — return it as-is.
|
|
133
|
+
if (!latest.available) {
|
|
134
|
+
return latest;
|
|
135
|
+
}
|
|
136
|
+
// Validate the actionable fields before we cache/return them.
|
|
137
|
+
if (!latest.version) {
|
|
138
|
+
throw new ValidationError(ErrorCode.INVALID_BUNDLE_FORMAT, 'No version in server response');
|
|
139
|
+
}
|
|
140
|
+
this.securityValidator.validateVersion(latest.version);
|
|
141
|
+
if (latest.url) {
|
|
142
|
+
this.securityValidator.validateUrl(latest.url);
|
|
143
|
+
}
|
|
144
|
+
if (data.minNativeVersion) {
|
|
145
|
+
this.securityValidator.validateVersion(data.minNativeVersion);
|
|
146
|
+
}
|
|
147
|
+
// Cache the result
|
|
148
|
+
await this.cacheVersionInfo(cacheKey, channel, latest);
|
|
149
|
+
this.logger.info('Version check completed', {
|
|
150
|
+
channel,
|
|
151
|
+
currentVersion,
|
|
152
|
+
latestVersion: latest.version,
|
|
153
|
+
updateAvailable: this.isNewerVersion(latest.version, currentVersion),
|
|
154
|
+
});
|
|
155
|
+
return latest;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
this.logger.error('Failed to check for updates', error);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Compare two versions
|
|
164
|
+
*/
|
|
165
|
+
isNewerVersion(version1, version2) {
|
|
166
|
+
try {
|
|
167
|
+
const v1 = this.parseVersion(version1);
|
|
168
|
+
const v2 = this.parseVersion(version2);
|
|
169
|
+
if (v1.major !== v2.major)
|
|
170
|
+
return v1.major > v2.major;
|
|
171
|
+
if (v1.minor !== v2.minor)
|
|
172
|
+
return v1.minor > v2.minor;
|
|
173
|
+
if (v1.patch !== v2.patch)
|
|
174
|
+
return v1.patch > v2.patch;
|
|
175
|
+
// If main versions are equal, check pre-release
|
|
176
|
+
if (v1.prerelease && !v2.prerelease)
|
|
177
|
+
return false; // v1 is pre-release, v2 is not
|
|
178
|
+
if (!v1.prerelease && v2.prerelease)
|
|
179
|
+
return true; // v1 is not pre-release, v2 is
|
|
180
|
+
if (v1.prerelease && v2.prerelease) {
|
|
181
|
+
return v1.prerelease > v2.prerelease;
|
|
182
|
+
}
|
|
183
|
+
return false; // Versions are equal
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
this.logger.error('Failed to compare versions', {
|
|
187
|
+
version1,
|
|
188
|
+
version2,
|
|
189
|
+
error,
|
|
190
|
+
});
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Check if update is mandatory based on minimum version
|
|
196
|
+
*/
|
|
197
|
+
isUpdateMandatory(currentVersion, minimumVersion) {
|
|
198
|
+
if (!minimumVersion)
|
|
199
|
+
return false;
|
|
200
|
+
try {
|
|
201
|
+
this.securityValidator.validateVersion(currentVersion);
|
|
202
|
+
this.securityValidator.validateVersion(minimumVersion);
|
|
203
|
+
return (!this.isNewerVersion(currentVersion, minimumVersion) &&
|
|
204
|
+
currentVersion !== minimumVersion);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
this.logger.error('Failed to check mandatory update', error);
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Parse version metadata
|
|
213
|
+
*/
|
|
214
|
+
parseVersion(version) {
|
|
215
|
+
const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/);
|
|
216
|
+
if (!match) {
|
|
217
|
+
throw new ValidationError(ErrorCode.INVALID_BUNDLE_FORMAT, 'Invalid version format');
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
major: parseInt(match[1], 10),
|
|
221
|
+
minor: parseInt(match[2], 10),
|
|
222
|
+
patch: parseInt(match[3], 10),
|
|
223
|
+
prerelease: match[4],
|
|
224
|
+
build: match[5],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Generate version string from components
|
|
229
|
+
*/
|
|
230
|
+
buildVersionString(components) {
|
|
231
|
+
let version = `${components.major}.${components.minor}.${components.patch}`;
|
|
232
|
+
if (components.prerelease) {
|
|
233
|
+
version += `-${components.prerelease}`;
|
|
234
|
+
}
|
|
235
|
+
if (components.build) {
|
|
236
|
+
version += `+${components.build}`;
|
|
237
|
+
}
|
|
238
|
+
return version;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Check if version is compatible with native version requirements
|
|
242
|
+
*/
|
|
243
|
+
isCompatibleWithNativeVersion(bundleVersion, nativeVersion, compatibility) {
|
|
244
|
+
if (!compatibility)
|
|
245
|
+
return true;
|
|
246
|
+
try {
|
|
247
|
+
// Check if there's a specific native version requirement for this bundle version
|
|
248
|
+
const requiredNativeVersion = compatibility[bundleVersion];
|
|
249
|
+
if (!requiredNativeVersion)
|
|
250
|
+
return true;
|
|
251
|
+
this.securityValidator.validateVersion(nativeVersion);
|
|
252
|
+
this.securityValidator.validateVersion(requiredNativeVersion);
|
|
253
|
+
return !this.isNewerVersion(requiredNativeVersion, nativeVersion);
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
this.logger.error('Failed to check compatibility', error);
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Get version from cache
|
|
262
|
+
*/
|
|
263
|
+
async getCachedVersionInfo(cacheKey) {
|
|
264
|
+
// Check memory cache first
|
|
265
|
+
const memCached = this.memoryCache.get(cacheKey);
|
|
266
|
+
if (memCached && Date.now() - memCached.timestamp < this.CACHE_DURATION) {
|
|
267
|
+
return memCached;
|
|
268
|
+
}
|
|
269
|
+
// Check persistent cache
|
|
270
|
+
try {
|
|
271
|
+
const { value } = await this.preferences.get({
|
|
272
|
+
key: this.VERSION_CHECK_CACHE_KEY,
|
|
273
|
+
});
|
|
274
|
+
if (!value)
|
|
275
|
+
return null;
|
|
276
|
+
const allCached = JSON.parse(value);
|
|
277
|
+
const cached = allCached[cacheKey];
|
|
278
|
+
if (cached && Date.now() - cached.timestamp < this.CACHE_DURATION) {
|
|
279
|
+
// Update memory cache
|
|
280
|
+
this.memoryCache.set(cacheKey, cached);
|
|
281
|
+
return cached;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
this.logger.debug('Failed to load cached version info', error);
|
|
286
|
+
}
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Cache version info
|
|
291
|
+
*/
|
|
292
|
+
async cacheVersionInfo(cacheKey, channel, data) {
|
|
293
|
+
const cacheEntry = {
|
|
294
|
+
channel,
|
|
295
|
+
data,
|
|
296
|
+
timestamp: Date.now(),
|
|
297
|
+
};
|
|
298
|
+
// Update memory cache
|
|
299
|
+
this.memoryCache.set(cacheKey, cacheEntry);
|
|
300
|
+
// Update persistent cache
|
|
301
|
+
try {
|
|
302
|
+
const { value } = await this.preferences.get({
|
|
303
|
+
key: this.VERSION_CHECK_CACHE_KEY,
|
|
304
|
+
});
|
|
305
|
+
const allCached = value
|
|
306
|
+
? JSON.parse(value)
|
|
307
|
+
: {};
|
|
308
|
+
// Clean old entries
|
|
309
|
+
const now = Date.now();
|
|
310
|
+
for (const key in allCached) {
|
|
311
|
+
if (now - allCached[key].timestamp > this.CACHE_DURATION * 2) {
|
|
312
|
+
delete allCached[key];
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
allCached[cacheKey] = cacheEntry;
|
|
316
|
+
await this.preferences.set({
|
|
317
|
+
key: this.VERSION_CHECK_CACHE_KEY,
|
|
318
|
+
value: JSON.stringify(allCached),
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
this.logger.warn('Failed to cache version info', error);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Clear version cache
|
|
327
|
+
*/
|
|
328
|
+
async clearVersionCache() {
|
|
329
|
+
this.memoryCache.clear();
|
|
330
|
+
try {
|
|
331
|
+
await this.preferences.remove({ key: this.VERSION_CHECK_CACHE_KEY });
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
this.logger.warn('Failed to clear version cache', error);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Check if downgrade protection should block update
|
|
339
|
+
*/
|
|
340
|
+
shouldBlockDowngrade(currentVersion, newVersion) {
|
|
341
|
+
try {
|
|
342
|
+
return this.securityValidator.isVersionDowngrade(currentVersion, newVersion);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
this.logger.error('Failed to check downgrade', error);
|
|
346
|
+
// Default to safe behavior - block if we can't determine
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Persisted high-water-mark version per channel. Survives reinstalls that
|
|
352
|
+
* leave Preferences intact; protects against an attacker feeding an older
|
|
353
|
+
* (and potentially vulnerable) bundle even after a full cache wipe of
|
|
354
|
+
* downloaded bundles. Paired with `rememberAppliedVersion`.
|
|
355
|
+
*/
|
|
356
|
+
maxVersionKey(channel) {
|
|
357
|
+
return `nu_max_version_${channel}`;
|
|
358
|
+
}
|
|
359
|
+
async getMaxSeenVersion(channel) {
|
|
360
|
+
if (!this.preferences)
|
|
361
|
+
return null;
|
|
362
|
+
try {
|
|
363
|
+
const { value } = await this.preferences.get({
|
|
364
|
+
key: this.maxVersionKey(channel),
|
|
365
|
+
});
|
|
366
|
+
return value !== null && value !== void 0 ? value : null;
|
|
367
|
+
}
|
|
368
|
+
catch (error) {
|
|
369
|
+
this.logger.warn('Failed to read max-seen version', error);
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
async rememberAppliedVersion(channel, version) {
|
|
374
|
+
if (!this.preferences)
|
|
375
|
+
return;
|
|
376
|
+
const current = await this.getMaxSeenVersion(channel);
|
|
377
|
+
if (current && VersionManager.compareVersions(current, version) >= 0) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
try {
|
|
381
|
+
await this.preferences.set({
|
|
382
|
+
key: this.maxVersionKey(channel),
|
|
383
|
+
value: version,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
this.logger.warn('Failed to persist max-seen version', error);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|