native-update 3.4.1 → 4.0.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 +26 -16
- package/NativeUpdate.podspec +2 -2
- package/README.md +554 -0
- package/android/build.gradle +43 -34
- package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/android/proguard-rules.pro +0 -3
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +45 -49
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +12 -10
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +12 -21
- package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +3 -34
- package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +206 -72
- package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +74 -37
- package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +169 -179
- package/android/variables.gradle +2 -2
- package/cli/.yarn/install-state.gz +0 -0
- package/cli/commands/bundle-verify.js +4 -1
- package/cli/commands/keys-generate.js +12 -46
- package/cli/index.js +4 -7
- package/cli/package.json +2 -1
- package/dist/esm/core/config.d.ts +3 -0
- package/dist/esm/core/config.js +17 -0
- package/dist/esm/core/plugin-manager.js +4 -2
- package/dist/esm/core/security.js +2 -5
- package/dist/esm/core/update-check.d.ts +2 -0
- package/dist/esm/core/update-check.js +5 -3
- package/dist/esm/definitions.d.ts +43 -2
- package/dist/esm/live-update/delta-processor.d.ts +6 -20
- package/dist/esm/live-update/delta-processor.js +75 -119
- package/dist/esm/plugin.d.ts +0 -3
- package/dist/esm/plugin.js +172 -12
- package/dist/esm/security/crypto.js +2 -5
- package/dist/esm/web.d.ts +1 -9
- package/dist/esm/web.js +4 -56
- package/dist/plugin.cjs +1 -1
- package/dist/plugin.esm.js +1 -1
- package/dist/plugin.js +2 -2
- package/docs/CHANGELOG.md +66 -0
- package/docs/KNOWN_LIMITATIONS.md +4 -3
- package/docs/MIGRATION.md +32 -3
- package/docs/server-requirements.md +40 -29
- package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +6 -2
- package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +21 -30
- package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +269 -61
- package/ios/Plugin/NativeUpdatePlugin.m +8 -1
- package/ios/Plugin/NativeUpdatePlugin.swift +71 -34
- package/ios/Plugin/Security/SecurityManager.swift +100 -99
- package/package.json +27 -15
- package/Readme.md +0 -565
|
@@ -42,8 +42,6 @@ export class PluginManager {
|
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
try {
|
|
45
|
-
// Configure the plugin
|
|
46
|
-
this.configManager.configure(config);
|
|
47
45
|
// Use Capacitor plugins directly if not provided
|
|
48
46
|
if (!config.filesystem) {
|
|
49
47
|
config.filesystem = Filesystem;
|
|
@@ -51,6 +49,9 @@ export class PluginManager {
|
|
|
51
49
|
if (!config.preferences) {
|
|
52
50
|
config.preferences = Preferences;
|
|
53
51
|
}
|
|
52
|
+
// Configure only after defaults are injected so isConfigured() sees
|
|
53
|
+
// the same complete configuration used by all managers.
|
|
54
|
+
this.configManager.configure(config);
|
|
54
55
|
// Initialize managers
|
|
55
56
|
this.bundleManager = new BundleManager();
|
|
56
57
|
await this.bundleManager.initialize();
|
|
@@ -185,6 +186,7 @@ export class PluginManager {
|
|
|
185
186
|
this.appUpdateManager = null;
|
|
186
187
|
this.backgroundScheduler = null;
|
|
187
188
|
this.initialized = false;
|
|
189
|
+
this.configManager.reset();
|
|
188
190
|
// Clear all event listeners
|
|
189
191
|
this.eventEmitter.removeAllListeners();
|
|
190
192
|
this.logger.info('Plugin reset complete');
|
|
@@ -131,14 +131,11 @@ export class SecurityValidator {
|
|
|
131
131
|
try {
|
|
132
132
|
// Import public key
|
|
133
133
|
const cryptoKey = await crypto.subtle.importKey('spki', this.pemToArrayBuffer(publicKey), {
|
|
134
|
-
name: '
|
|
134
|
+
name: 'RSASSA-PKCS1-v1_5',
|
|
135
135
|
hash: 'SHA-256',
|
|
136
136
|
}, false, ['verify']);
|
|
137
137
|
// Verify signature
|
|
138
|
-
const isValid = await crypto.subtle.verify({
|
|
139
|
-
name: 'RSA-PSS',
|
|
140
|
-
saltLength: 32,
|
|
141
|
-
}, cryptoKey, this.base64ToArrayBuffer(signature), data);
|
|
138
|
+
const isValid = await crypto.subtle.verify({ name: 'RSASSA-PKCS1-v1_5' }, cryptoKey, this.base64ToArrayBuffer(signature), data);
|
|
142
139
|
if (!isValid) {
|
|
143
140
|
this.logger.error('Signature verification failed');
|
|
144
141
|
}
|
|
@@ -89,6 +89,8 @@ export interface BackendUpdateCheckResponse {
|
|
|
89
89
|
downloadUrl?: string;
|
|
90
90
|
checksum?: string;
|
|
91
91
|
signature?: string | null;
|
|
92
|
+
signatureAlgorithm?: 'RSA-SHA256' | null;
|
|
93
|
+
delta?: import('../definitions').DeltaUpdateInfo | null;
|
|
92
94
|
size?: number;
|
|
93
95
|
mandatory?: boolean;
|
|
94
96
|
releaseNotes?: string | null;
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
* callers decide whether to swallow or surface them.
|
|
63
63
|
*/
|
|
64
64
|
export async function checkForUpdate(baseUrl, req) {
|
|
65
|
-
var _a, _b, _c;
|
|
65
|
+
var _a, _b, _c, _d, _e;
|
|
66
66
|
const url = `${baseUrl.replace(/\/$/, '')}/v1/updates/check?channel=${encodeURIComponent(req.channel)}`;
|
|
67
67
|
const headers = {
|
|
68
68
|
'Content-Type': 'application/json',
|
|
@@ -103,10 +103,12 @@ export async function checkForUpdate(baseUrl, req) {
|
|
|
103
103
|
downloadUrl: data.downloadUrl,
|
|
104
104
|
checksum: data.checksum,
|
|
105
105
|
signature: (_a = data.signature) !== null && _a !== void 0 ? _a : undefined,
|
|
106
|
+
signatureAlgorithm: (_b = data.signatureAlgorithm) !== null && _b !== void 0 ? _b : undefined,
|
|
107
|
+
delta: (_c = data.delta) !== null && _c !== void 0 ? _c : undefined,
|
|
106
108
|
size: data.size,
|
|
107
109
|
mandatory: data.mandatory,
|
|
108
|
-
releaseNotes: (
|
|
109
|
-
minNativeVersion: (
|
|
110
|
+
releaseNotes: (_d = data.releaseNotes) !== null && _d !== void 0 ? _d : undefined,
|
|
111
|
+
minNativeVersion: (_e = data.minNativeVersion) !== null && _e !== void 0 ? _e : undefined,
|
|
110
112
|
expiresAt: data.expiresAt,
|
|
111
113
|
message: data.message,
|
|
112
114
|
};
|
|
@@ -198,6 +198,11 @@ export interface PluginConfig {
|
|
|
198
198
|
updateStrategy?: UpdateStrategy;
|
|
199
199
|
publicKey?: string;
|
|
200
200
|
requireSignature?: boolean;
|
|
201
|
+
allowUnsignedDevelopmentUpdates?: boolean;
|
|
202
|
+
enableDeltaUpdates?: boolean;
|
|
203
|
+
maxBundleSize?: number;
|
|
204
|
+
maxUncompressedBundleSize?: number;
|
|
205
|
+
allowedHosts?: string[];
|
|
201
206
|
checksumAlgorithm?: ChecksumAlgorithm;
|
|
202
207
|
checkInterval?: number;
|
|
203
208
|
security?: SecurityConfig;
|
|
@@ -237,7 +242,15 @@ export interface LiveUpdateConfig {
|
|
|
237
242
|
mandatoryInstallMode?: InstallMode;
|
|
238
243
|
optionalInstallMode?: InstallMode;
|
|
239
244
|
maxBundleSize?: number;
|
|
245
|
+
/**
|
|
246
|
+
* Maximum total uncompressed size of an installed ZIP bundle in bytes.
|
|
247
|
+
* Native platforms default to 500 MiB and reject archives above the cap.
|
|
248
|
+
*/
|
|
249
|
+
maxUncompressedBundleSize?: number;
|
|
240
250
|
allowedHosts?: string[];
|
|
251
|
+
allowUnsignedDevelopmentUpdates?: boolean;
|
|
252
|
+
/** Use verified NUDELTA/1 patches when available (default: true). */
|
|
253
|
+
enableDeltaUpdates?: boolean;
|
|
241
254
|
}
|
|
242
255
|
export interface AppUpdateConfig {
|
|
243
256
|
minimumVersion?: string;
|
|
@@ -279,7 +292,7 @@ export interface SecurityConfig {
|
|
|
279
292
|
validateInputs?: boolean;
|
|
280
293
|
/**
|
|
281
294
|
* @deprecated since v2. Secure storage is always on. Android uses
|
|
282
|
-
*
|
|
295
|
+
* Android Keystore-backed AES-256-GCM, iOS uses Keychain,
|
|
283
296
|
* web does not persist sensitive config fields to localStorage.
|
|
284
297
|
* Setting this to `false` is ignored.
|
|
285
298
|
*/
|
|
@@ -313,6 +326,8 @@ export interface DownloadOptions {
|
|
|
313
326
|
version: string;
|
|
314
327
|
checksum: string;
|
|
315
328
|
signature?: string;
|
|
329
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
330
|
+
delta?: DeltaUpdateInfo;
|
|
316
331
|
maxRetries?: number;
|
|
317
332
|
timeout?: number;
|
|
318
333
|
}
|
|
@@ -325,6 +340,7 @@ export interface BundleInfo {
|
|
|
325
340
|
status: BundleStatus;
|
|
326
341
|
checksum: string;
|
|
327
342
|
signature?: string;
|
|
343
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
328
344
|
verified: boolean;
|
|
329
345
|
metadata?: Record<string, unknown>;
|
|
330
346
|
}
|
|
@@ -345,6 +361,9 @@ export interface LatestVersion {
|
|
|
345
361
|
notes?: string;
|
|
346
362
|
size?: number;
|
|
347
363
|
checksum?: string;
|
|
364
|
+
signature?: string;
|
|
365
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
366
|
+
delta?: DeltaUpdateInfo;
|
|
348
367
|
}
|
|
349
368
|
/**
|
|
350
369
|
* Result from checkForUpdate convenience method
|
|
@@ -359,6 +378,16 @@ export interface CheckForUpdateResult {
|
|
|
359
378
|
size?: number;
|
|
360
379
|
checksum?: string;
|
|
361
380
|
signature?: string;
|
|
381
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
382
|
+
delta?: DeltaUpdateInfo;
|
|
383
|
+
}
|
|
384
|
+
export interface DeltaUpdateInfo {
|
|
385
|
+
format: 'NUDELTA/1';
|
|
386
|
+
fromVersion: string;
|
|
387
|
+
patchUrl: string;
|
|
388
|
+
patchSize: number;
|
|
389
|
+
patchChecksum: string;
|
|
390
|
+
targetChecksum: string;
|
|
362
391
|
}
|
|
363
392
|
/**
|
|
364
393
|
* Options for downloadUpdate convenience method
|
|
@@ -368,6 +397,8 @@ export interface DownloadUpdateOptions {
|
|
|
368
397
|
url?: string;
|
|
369
398
|
checksum?: string;
|
|
370
399
|
signature?: string;
|
|
400
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
401
|
+
delta?: DeltaUpdateInfo;
|
|
371
402
|
onProgress?: (event: DownloadProgressEvent) => void;
|
|
372
403
|
}
|
|
373
404
|
export interface ValidateOptions {
|
|
@@ -467,6 +498,7 @@ export interface SecurityInfo {
|
|
|
467
498
|
certificatePinning: CertificatePinning;
|
|
468
499
|
validateInputs: boolean;
|
|
469
500
|
secureStorage: boolean;
|
|
501
|
+
signatureAlgorithm?: 'RSA-SHA256';
|
|
470
502
|
}
|
|
471
503
|
/**
|
|
472
504
|
* Error Types
|
|
@@ -698,10 +730,17 @@ export interface PluginInitConfig {
|
|
|
698
730
|
* List of allowed hosts for security
|
|
699
731
|
*/
|
|
700
732
|
allowedHosts?: string[];
|
|
733
|
+
/** Explicit development-only escape hatch; native release builds ignore it. */
|
|
734
|
+
allowUnsignedDevelopmentUpdates?: boolean;
|
|
701
735
|
/**
|
|
702
736
|
* Maximum bundle size in bytes (default: 100MB)
|
|
703
737
|
*/
|
|
704
738
|
maxBundleSize?: number;
|
|
739
|
+
/**
|
|
740
|
+
* Maximum total uncompressed size of an installed ZIP bundle in bytes.
|
|
741
|
+
* Native platforms default to 500 MiB.
|
|
742
|
+
*/
|
|
743
|
+
maxUncompressedBundleSize?: number;
|
|
705
744
|
/**
|
|
706
745
|
* Download timeout in milliseconds (default: 30000)
|
|
707
746
|
*/
|
|
@@ -757,6 +796,8 @@ export interface PluginInitConfig {
|
|
|
757
796
|
* Enable automatic updates
|
|
758
797
|
*/
|
|
759
798
|
autoUpdate?: boolean;
|
|
799
|
+
/** Use verified NUDELTA/1 patches when available (default: true). */
|
|
800
|
+
enableDeltaUpdates?: boolean;
|
|
760
801
|
/**
|
|
761
802
|
* Update strategy
|
|
762
803
|
*/
|
|
@@ -801,7 +842,7 @@ export interface NativeUpdatePlugin extends NativeUpdateCombinedPlugin, NativeUp
|
|
|
801
842
|
/**
|
|
802
843
|
* Check if plugin is initialized
|
|
803
844
|
*/
|
|
804
|
-
isInitialized(): boolean
|
|
845
|
+
isInitialized(): Promise<boolean>;
|
|
805
846
|
/**
|
|
806
847
|
* Reset plugin state
|
|
807
848
|
*/
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* Delta Processor
|
|
3
3
|
*
|
|
4
4
|
* Handles delta/patch-based updates for reduced download sizes.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* for better performance on binary files.
|
|
5
|
+
* Applies the bounded, deterministic NUDELTA/1 copy/literal format emitted by
|
|
6
|
+
* the canonical backend. Unknown legacy patch formats fail closed and trigger
|
|
7
|
+
* the normal full-bundle fallback.
|
|
9
8
|
*/
|
|
10
9
|
import type { UpdateCheckResponse } from '../types/rollout';
|
|
11
10
|
/**
|
|
@@ -61,24 +60,11 @@ export declare class DeltaProcessor {
|
|
|
61
60
|
/**
|
|
62
61
|
* Apply a delta patch to create new bundle
|
|
63
62
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
63
|
+
* Only NUDELTA/1 is accepted. Target checksum verification still runs after
|
|
64
|
+
* reconstruction, providing an independent integrity boundary.
|
|
66
65
|
*/
|
|
67
66
|
private applyPatch;
|
|
68
|
-
|
|
69
|
-
* Apply file-level patch (JSON format)
|
|
70
|
-
* Supports adding, modifying, and removing files from the bundle
|
|
71
|
-
*/
|
|
72
|
-
private applyFileLevelPatch;
|
|
73
|
-
/**
|
|
74
|
-
* Apply binary patch using a simplified approach
|
|
75
|
-
* For production with large files, consider integrating a WASM bsdiff library
|
|
76
|
-
*/
|
|
77
|
-
private applyBinaryPatch;
|
|
78
|
-
/**
|
|
79
|
-
* Decode base64 string to UTF-8 string
|
|
80
|
-
*/
|
|
81
|
-
private decodeBase64;
|
|
67
|
+
private applyNuDelta;
|
|
82
68
|
/**
|
|
83
69
|
* Calculate SHA-256 checksum
|
|
84
70
|
*/
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* Delta Processor
|
|
3
3
|
*
|
|
4
4
|
* Handles delta/patch-based updates for reduced download sizes.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* for better performance on binary files.
|
|
5
|
+
* Applies the bounded, deterministic NUDELTA/1 copy/literal format emitted by
|
|
6
|
+
* the canonical backend. Unknown legacy patch formats fail closed and trigger
|
|
7
|
+
* the normal full-bundle fallback.
|
|
9
8
|
*/
|
|
10
9
|
import { Logger } from '../core/logger';
|
|
11
10
|
import { ConfigManager } from '../core/config';
|
|
@@ -39,6 +38,10 @@ export class DeltaProcessor {
|
|
|
39
38
|
onProgress === null || onProgress === void 0 ? void 0 : onProgress(10);
|
|
40
39
|
// Download delta patch
|
|
41
40
|
const patchData = await this.downloadPatch(updateInfo.delta.patchUrl);
|
|
41
|
+
if (updateInfo.delta.patchChecksum &&
|
|
42
|
+
!await this.verifyChecksum(patchData, updateInfo.delta.patchChecksum)) {
|
|
43
|
+
throw new ValidationError(ErrorCode.CHECKSUM_MISMATCH, 'Delta patch checksum mismatch');
|
|
44
|
+
}
|
|
42
45
|
onProgress === null || onProgress === void 0 ? void 0 : onProgress(50);
|
|
43
46
|
// Apply delta patch
|
|
44
47
|
const patchedBundle = await this.applyPatch(currentBundleData, patchData, updateInfo.delta.targetChecksum || updateInfo.checksum);
|
|
@@ -98,7 +101,16 @@ export class DeltaProcessor {
|
|
|
98
101
|
if (!response.ok) {
|
|
99
102
|
throw new DownloadError(ErrorCode.DOWNLOAD_FAILED, `Failed to download delta patch: ${response.status}`);
|
|
100
103
|
}
|
|
101
|
-
|
|
104
|
+
const maxSize = this.configManager.get('maxBundleSize');
|
|
105
|
+
const declaredSize = Number(response.headers.get('content-length') || 0);
|
|
106
|
+
if (declaredSize > maxSize) {
|
|
107
|
+
throw new DownloadError(ErrorCode.BUNDLE_TOO_LARGE, 'Delta patch exceeds the bundle size limit');
|
|
108
|
+
}
|
|
109
|
+
const data = await response.arrayBuffer();
|
|
110
|
+
if (data.byteLength > maxSize) {
|
|
111
|
+
throw new DownloadError(ErrorCode.BUNDLE_TOO_LARGE, 'Delta patch exceeds the bundle size limit');
|
|
112
|
+
}
|
|
113
|
+
return data;
|
|
102
114
|
}
|
|
103
115
|
/**
|
|
104
116
|
* Download full bundle
|
|
@@ -112,6 +124,10 @@ export class DeltaProcessor {
|
|
|
112
124
|
// If we can track progress
|
|
113
125
|
const contentLength = response.headers.get('content-length');
|
|
114
126
|
const totalBytes = contentLength ? parseInt(contentLength, 10) : 0;
|
|
127
|
+
const maxSize = this.configManager.get('maxBundleSize');
|
|
128
|
+
if (totalBytes > maxSize) {
|
|
129
|
+
throw new DownloadError(ErrorCode.BUNDLE_TOO_LARGE, 'Bundle exceeds the configured size limit');
|
|
130
|
+
}
|
|
115
131
|
if (totalBytes && response.body) {
|
|
116
132
|
const reader = response.body.getReader();
|
|
117
133
|
const chunks = [];
|
|
@@ -122,6 +138,10 @@ export class DeltaProcessor {
|
|
|
122
138
|
break;
|
|
123
139
|
chunks.push(value);
|
|
124
140
|
receivedBytes += value.length;
|
|
141
|
+
if (receivedBytes > maxSize) {
|
|
142
|
+
await reader.cancel();
|
|
143
|
+
throw new DownloadError(ErrorCode.BUNDLE_TOO_LARGE, 'Bundle exceeds the configured size limit');
|
|
144
|
+
}
|
|
125
145
|
if (onProgress) {
|
|
126
146
|
const percent = Math.round((receivedBytes / totalBytes) * 90) + 10;
|
|
127
147
|
onProgress(Math.min(percent, 99));
|
|
@@ -139,137 +159,73 @@ export class DeltaProcessor {
|
|
|
139
159
|
}
|
|
140
160
|
// Simple download without progress
|
|
141
161
|
const data = await response.arrayBuffer();
|
|
162
|
+
if (data.byteLength > maxSize) {
|
|
163
|
+
throw new DownloadError(ErrorCode.BUNDLE_TOO_LARGE, 'Bundle exceeds the configured size limit');
|
|
164
|
+
}
|
|
142
165
|
onProgress === null || onProgress === void 0 ? void 0 : onProgress(100);
|
|
143
166
|
return data;
|
|
144
167
|
}
|
|
145
168
|
/**
|
|
146
169
|
* Apply a delta patch to create new bundle
|
|
147
170
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
171
|
+
* Only NUDELTA/1 is accepted. Target checksum verification still runs after
|
|
172
|
+
* reconstruction, providing an independent integrity boundary.
|
|
150
173
|
*/
|
|
151
|
-
async applyPatch(currentBundle, patchData,
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (patch.type === 'file-level') {
|
|
157
|
-
return this.applyFileLevelPatch(currentBundle, patch);
|
|
158
|
-
}
|
|
159
|
-
// Binary patch format
|
|
160
|
-
return this.applyBinaryPatch(currentBundle, patchData);
|
|
161
|
-
}
|
|
162
|
-
catch (_a) {
|
|
163
|
-
// Treat as binary patch
|
|
164
|
-
return this.applyBinaryPatch(currentBundle, patchData);
|
|
174
|
+
async applyPatch(currentBundle, patchData, expectedChecksum) {
|
|
175
|
+
const patchText = new TextDecoder().decode(patchData);
|
|
176
|
+
const patch = JSON.parse(patchText);
|
|
177
|
+
if (patch.format !== 'NUDELTA/1' || patch.targetChecksum !== expectedChecksum) {
|
|
178
|
+
throw new Error('Unsupported or inconsistent delta patch');
|
|
165
179
|
}
|
|
180
|
+
const baseChecksum = await this.calculateChecksum(currentBundle);
|
|
181
|
+
if (patch.baseChecksum !== baseChecksum)
|
|
182
|
+
throw new Error('NUDELTA base checksum mismatch');
|
|
183
|
+
return this.applyNuDelta(currentBundle, patch);
|
|
166
184
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
async applyFileLevelPatch(currentBundle, patch) {
|
|
172
|
-
this.logger.debug('Applying file-level patch', {
|
|
173
|
-
fromVersion: patch.fromVersion,
|
|
174
|
-
toVersion: patch.toVersion,
|
|
175
|
-
added: patch.changes.added.length,
|
|
176
|
-
modified: patch.changes.modified.length,
|
|
177
|
-
removed: patch.changes.removed.length,
|
|
178
|
-
});
|
|
179
|
-
// Parse current bundle as a simple file map (JSON-based bundle format)
|
|
180
|
-
// This is a lightweight implementation that works without heavy dependencies
|
|
181
|
-
let bundleData;
|
|
182
|
-
try {
|
|
183
|
-
const bundleText = new TextDecoder().decode(currentBundle);
|
|
184
|
-
bundleData = JSON.parse(bundleText);
|
|
185
|
-
}
|
|
186
|
-
catch (_a) {
|
|
187
|
-
// If current bundle is not JSON, treat it as a single-file bundle
|
|
188
|
-
bundleData = { 'bundle.js': new TextDecoder().decode(currentBundle) };
|
|
189
|
-
}
|
|
190
|
-
// Apply removals
|
|
191
|
-
for (const path of patch.changes.removed) {
|
|
192
|
-
delete bundleData[path];
|
|
193
|
-
this.logger.debug(`Removed file: ${path}`);
|
|
185
|
+
applyNuDelta(currentBundle, patch) {
|
|
186
|
+
const maxSize = this.configManager.get('maxBundleSize');
|
|
187
|
+
if (!Number.isSafeInteger(patch.targetSize) || patch.targetSize <= 0 || patch.targetSize > maxSize) {
|
|
188
|
+
throw new Error('NUDELTA target size is invalid');
|
|
194
189
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// Decode base64 data
|
|
198
|
-
const content = this.decodeBase64(file.data);
|
|
199
|
-
bundleData[file.path] = content;
|
|
200
|
-
this.logger.debug(`Added file: ${file.path}`);
|
|
190
|
+
if (!Array.isArray(patch.operations) || patch.operations.length === 0 || patch.operations.length > 100000) {
|
|
191
|
+
throw new Error('NUDELTA operation count is invalid');
|
|
201
192
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return new TextEncoder().encode(resultText).buffer;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Apply binary patch using a simplified approach
|
|
215
|
-
* For production with large files, consider integrating a WASM bsdiff library
|
|
216
|
-
*/
|
|
217
|
-
async applyBinaryPatch(currentBundle, patchData) {
|
|
218
|
-
this.logger.debug('Applying binary patch', {
|
|
219
|
-
currentSize: currentBundle.byteLength,
|
|
220
|
-
patchSize: patchData.byteLength,
|
|
221
|
-
});
|
|
222
|
-
// Parse patch as JSON instructions (simplified binary diff format)
|
|
223
|
-
try {
|
|
224
|
-
const patchText = new TextDecoder().decode(patchData);
|
|
225
|
-
const patchInstructions = JSON.parse(patchText);
|
|
226
|
-
if (patchInstructions.type !== 'binary-diff') {
|
|
227
|
-
throw new Error('Invalid binary patch format');
|
|
228
|
-
}
|
|
229
|
-
const currentView = new Uint8Array(currentBundle);
|
|
230
|
-
const resultChunks = [];
|
|
231
|
-
// Apply patch operations
|
|
232
|
-
for (const op of patchInstructions.operations) {
|
|
233
|
-
if (op.type === 'copy' && op.sourceOffset !== undefined && op.length !== undefined) {
|
|
234
|
-
// Copy from original bundle
|
|
235
|
-
const chunk = currentView.slice(op.sourceOffset, op.sourceOffset + op.length);
|
|
236
|
-
resultChunks.push(chunk);
|
|
193
|
+
const base = new Uint8Array(currentBundle);
|
|
194
|
+
const chunks = [];
|
|
195
|
+
let resultSize = 0;
|
|
196
|
+
for (const operation of patch.operations) {
|
|
197
|
+
let chunk;
|
|
198
|
+
if (operation.op === 'copy') {
|
|
199
|
+
if (!Number.isSafeInteger(operation.offset) || !Number.isSafeInteger(operation.length) ||
|
|
200
|
+
operation.offset < 0 || operation.length <= 0 || operation.offset + operation.length > base.length) {
|
|
201
|
+
throw new Error('NUDELTA copy operation is out of bounds');
|
|
237
202
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
203
|
+
chunk = base.slice(operation.offset, operation.offset + operation.length);
|
|
204
|
+
}
|
|
205
|
+
else if (operation.op === 'literal' && typeof operation.data === 'string') {
|
|
206
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(operation.data)) {
|
|
207
|
+
throw new Error('NUDELTA literal is not valid base64');
|
|
242
208
|
}
|
|
209
|
+
const binary = atob(operation.data);
|
|
210
|
+
chunk = Uint8Array.from(binary, character => character.charCodeAt(0));
|
|
243
211
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const result = new Uint8Array(totalLength);
|
|
247
|
-
let offset = 0;
|
|
248
|
-
for (const chunk of resultChunks) {
|
|
249
|
-
result.set(chunk, offset);
|
|
250
|
-
offset += chunk.length;
|
|
212
|
+
else {
|
|
213
|
+
throw new Error('NUDELTA operation is invalid');
|
|
251
214
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
catch (error) {
|
|
258
|
-
this.logger.warn('Binary patch parsing failed, falling back to full download', { error });
|
|
259
|
-
throw new Error('Binary patch format not recognized');
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Decode base64 string to UTF-8 string
|
|
264
|
-
*/
|
|
265
|
-
decodeBase64(base64) {
|
|
266
|
-
try {
|
|
267
|
-
return atob(base64);
|
|
215
|
+
resultSize += chunk.length;
|
|
216
|
+
if (resultSize > patch.targetSize)
|
|
217
|
+
throw new Error('NUDELTA output exceeds declared size');
|
|
218
|
+
chunks.push(chunk);
|
|
268
219
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
220
|
+
if (resultSize !== patch.targetSize)
|
|
221
|
+
throw new Error('NUDELTA output size mismatch');
|
|
222
|
+
const result = new Uint8Array(resultSize);
|
|
223
|
+
let offset = 0;
|
|
224
|
+
for (const chunk of chunks) {
|
|
225
|
+
result.set(chunk, offset);
|
|
226
|
+
offset += chunk.length;
|
|
272
227
|
}
|
|
228
|
+
return result.buffer;
|
|
273
229
|
}
|
|
274
230
|
/**
|
|
275
231
|
* Calculate SHA-256 checksum
|