native-update 3.4.2 → 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.
Files changed (47) hide show
  1. package/AI-INTEGRATION-GUIDE.md +26 -16
  2. package/NativeUpdate.podspec +2 -2
  3. package/README.md +23 -20
  4. package/android/build.gradle +37 -33
  5. package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  6. package/android/proguard-rules.pro +0 -3
  7. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundNotificationManager.kt +45 -49
  8. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateManager.kt +12 -10
  9. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdatePlugin.kt +12 -21
  10. package/android/src/main/java/com/aoneahsan/nativeupdate/BackgroundUpdateWorker.kt +3 -34
  11. package/android/src/main/java/com/aoneahsan/nativeupdate/LiveUpdatePlugin.kt +206 -72
  12. package/android/src/main/java/com/aoneahsan/nativeupdate/NativeUpdatePlugin.kt +74 -37
  13. package/android/src/main/java/com/aoneahsan/nativeupdate/SecurityManager.kt +169 -179
  14. package/android/variables.gradle +2 -2
  15. package/cli/.yarn/install-state.gz +0 -0
  16. package/cli/commands/bundle-verify.js +4 -1
  17. package/cli/commands/keys-generate.js +12 -46
  18. package/cli/index.js +4 -7
  19. package/cli/package.json +2 -1
  20. package/dist/esm/core/config.d.ts +3 -0
  21. package/dist/esm/core/config.js +17 -0
  22. package/dist/esm/core/plugin-manager.js +4 -2
  23. package/dist/esm/core/security.js +2 -5
  24. package/dist/esm/core/update-check.d.ts +2 -0
  25. package/dist/esm/core/update-check.js +5 -3
  26. package/dist/esm/definitions.d.ts +43 -2
  27. package/dist/esm/live-update/delta-processor.d.ts +6 -20
  28. package/dist/esm/live-update/delta-processor.js +75 -119
  29. package/dist/esm/plugin.d.ts +0 -3
  30. package/dist/esm/plugin.js +172 -12
  31. package/dist/esm/security/crypto.js +2 -5
  32. package/dist/esm/web.d.ts +1 -9
  33. package/dist/esm/web.js +4 -56
  34. package/dist/plugin.cjs +1 -1
  35. package/dist/plugin.esm.js +1 -1
  36. package/dist/plugin.js +2 -2
  37. package/docs/CHANGELOG.md +42 -0
  38. package/docs/KNOWN_LIMITATIONS.md +4 -3
  39. package/docs/MIGRATION.md +32 -3
  40. package/docs/server-requirements.md +40 -29
  41. package/ios/Plugin/BackgroundUpdate/BackgroundNotificationManager.swift +6 -2
  42. package/ios/Plugin/BackgroundUpdate/BackgroundUpdatePlugin.swift +21 -30
  43. package/ios/Plugin/LiveUpdate/LiveUpdatePlugin.swift +269 -61
  44. package/ios/Plugin/NativeUpdatePlugin.m +8 -1
  45. package/ios/Plugin/NativeUpdatePlugin.swift +71 -34
  46. package/ios/Plugin/Security/SecurityManager.swift +100 -99
  47. package/package.json +25 -14
@@ -1,7 +1,6 @@
1
1
  import chalk from 'chalk';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
- import crypto from 'crypto';
5
4
  import { generateKeyPairSync } from 'crypto';
6
5
 
7
6
  export async function generateKeys(options) {
@@ -12,51 +11,18 @@ export async function generateKeys(options) {
12
11
  const outputDir = path.resolve(options.output);
13
12
  await fs.mkdir(outputDir, { recursive: true });
14
13
 
15
- let publicKey, privateKey;
16
-
17
- if (options.type === 'rsa') {
18
- // Generate RSA key pair
19
- const keySize = parseInt(options.size);
20
- if (![2048, 4096].includes(keySize)) {
21
- throw new Error('RSA key size must be 2048 or 4096');
22
- }
23
-
24
- ({ publicKey, privateKey } = generateKeyPairSync('rsa', {
25
- modulusLength: keySize,
26
- publicKeyEncoding: {
27
- type: 'spki',
28
- format: 'pem'
29
- },
30
- privateKeyEncoding: {
31
- type: 'pkcs8',
32
- format: 'pem'
33
- }
34
- }));
35
- } else if (options.type === 'ec') {
36
- // Generate EC key pair
37
- const curveMap = {
38
- '256': 'prime256v1',
39
- '384': 'secp384r1'
40
- };
41
- const namedCurve = curveMap[options.size];
42
- if (!namedCurve) {
43
- throw new Error('EC key size must be 256 or 384');
44
- }
45
-
46
- ({ publicKey, privateKey } = generateKeyPairSync('ec', {
47
- namedCurve,
48
- publicKeyEncoding: {
49
- type: 'spki',
50
- format: 'pem'
51
- },
52
- privateKeyEncoding: {
53
- type: 'pkcs8',
54
- format: 'pem'
55
- }
56
- }));
57
- } else {
58
- throw new Error('Key type must be "rsa" or "ec"');
14
+ if (options.type !== 'rsa') {
15
+ throw new Error('Only RSA keys are supported by the RSA-SHA256 bundle contract');
16
+ }
17
+ const keySize = parseInt(options.size);
18
+ if (![2048, 4096].includes(keySize)) {
19
+ throw new Error('RSA key size must be 2048 or 4096');
59
20
  }
21
+ const { publicKey, privateKey } = generateKeyPairSync('rsa', {
22
+ modulusLength: keySize,
23
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
24
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
25
+ });
60
26
 
61
27
  // Save keys
62
28
  const timestamp = Date.now();
@@ -89,4 +55,4 @@ export async function generateKeys(options) {
89
55
  console.error(chalk.red('❌ Failed to generate keys:'), error.message);
90
56
  process.exit(1);
91
57
  }
92
- }
58
+ }
package/cli/index.js CHANGED
@@ -120,13 +120,10 @@ ${chalk.bold('Examples:')}
120
120
  ${chalk.green('npx native-update keys generate')}
121
121
 
122
122
  ${chalk.gray('# Generate strong RSA 4096-bit keys for production')}
123
- ${chalk.green('npx native-update keys generate --type rsa --size 4096')}
124
-
125
- ${chalk.gray('# Generate EC keys for smaller signatures')}
126
- ${chalk.green('npx native-update keys generate --type ec --size 256 --output ./my-keys')}`)
123
+ ${chalk.green('npx native-update keys generate --type rsa --size 4096 --output ./my-keys')}`)
127
124
  .option('-o, --output <dir>', 'Output directory for keys', './keys')
128
- .option('-t, --type <type>', 'Key type (rsa or ec)', 'rsa')
129
- .option('-s, --size <size>', 'Key size (RSA: 2048/4096, EC: 256/384)', '2048')
125
+ .option('-t, --type <type>', 'Key type (rsa)', 'rsa')
126
+ .option('-s, --size <size>', 'RSA key size (2048 or 4096)', '2048')
130
127
  .action(async (options) => {
131
128
  const { generateKeys } = await import('./commands/keys-generate.js');
132
129
  await generateKeys(options);
@@ -599,4 +596,4 @@ try {
599
596
  console.error(chalk.red(`Error: ${error.message}`));
600
597
  }
601
598
  process.exit(error.exitCode ?? 1);
602
- }
599
+ }
package/cli/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "native-update-cli",
3
3
  "private": true,
4
+ "packageManager": "yarn@4.17.0",
4
5
  "description": "CLI for Native Update. No version field on purpose: the CLI ships inside the native-update npm package and reads its version from the ROOT package.json (single source of truth) via cli/index.js.",
5
6
  "type": "module",
6
7
  "engines": {
@@ -14,7 +15,7 @@
14
15
  "chalk": "^5.6.2",
15
16
  "commander": "^15.0.0",
16
17
  "express": "^5.2.1",
17
- "ora": "^9.4.0",
18
+ "ora": "^9.4.1",
18
19
  "prompts": "^2.4.2"
19
20
  }
20
21
  }
@@ -7,6 +7,7 @@ export interface PluginConfig {
7
7
  baseUrl?: string;
8
8
  allowedHosts?: string[];
9
9
  maxBundleSize?: number;
10
+ maxUncompressedBundleSize?: number;
10
11
  downloadTimeout?: number;
11
12
  retryAttempts?: number;
12
13
  retryDelay?: number;
@@ -31,6 +32,7 @@ export interface PluginConfig {
31
32
  checksumAlgorithm?: ChecksumAlgorithm;
32
33
  checkInterval?: number;
33
34
  security?: SecurityConfig;
35
+ allowUnsignedDevelopmentUpdates?: boolean;
34
36
  promptAfterPositiveEvents?: boolean;
35
37
  maxPromptsPerVersion?: number;
36
38
  minimumDaysSinceLastPrompt?: number;
@@ -58,6 +60,7 @@ export declare class ConfigManager {
58
60
  static getInstance(): ConfigManager;
59
61
  private getDefaultConfig;
60
62
  configure(config: PluginConfig): void;
63
+ reset(): void;
61
64
  private validateConfig;
62
65
  get<K extends keyof Required<PluginConfig>>(key: K): Required<PluginConfig>[K];
63
66
  set<K extends keyof Required<PluginConfig>>(key: K, value: Required<PluginConfig>[K]): void;
@@ -15,6 +15,7 @@ export class ConfigManager {
15
15
  baseUrl: '',
16
16
  allowedHosts: [],
17
17
  maxBundleSize: 100 * 1024 * 1024, // 100MB
18
+ maxUncompressedBundleSize: 500 * 1024 * 1024, // 500MB
18
19
  downloadTimeout: 30000, // 30 seconds
19
20
  retryAttempts: 3,
20
21
  retryDelay: 1000, // 1 second
@@ -38,6 +39,7 @@ export class ConfigManager {
38
39
  secureStorage: true,
39
40
  logSecurityEvents: false,
40
41
  },
42
+ allowUnsignedDevelopmentUpdates: false,
41
43
  // App review config
42
44
  promptAfterPositiveEvents: false,
43
45
  maxPromptsPerVersion: 1,
@@ -57,6 +59,9 @@ export class ConfigManager {
57
59
  this.config = Object.assign(Object.assign({}, this.config), config);
58
60
  this.validateConfig();
59
61
  }
62
+ reset() {
63
+ this.config = this.getDefaultConfig();
64
+ }
60
65
  validateConfig() {
61
66
  // Validate HTTPS for baseUrl (security requirement)
62
67
  if (this.config.baseUrl && !this.config.baseUrl.startsWith('https://')) {
@@ -65,6 +70,9 @@ export class ConfigManager {
65
70
  if (this.config.maxBundleSize <= 0) {
66
71
  throw new Error('maxBundleSize must be greater than 0');
67
72
  }
73
+ if (this.config.maxUncompressedBundleSize <= 0) {
74
+ throw new Error('maxUncompressedBundleSize must be greater than 0');
75
+ }
68
76
  if (this.config.downloadTimeout <= 0) {
69
77
  throw new Error('downloadTimeout must be greater than 0');
70
78
  }
@@ -74,6 +82,15 @@ export class ConfigManager {
74
82
  if (this.config.retryDelay < 0) {
75
83
  throw new Error('retryDelay must be non-negative');
76
84
  }
85
+ if (this.config.security.enforceHttps === false) {
86
+ throw new Error('HTTPS enforcement cannot be disabled');
87
+ }
88
+ if ((this.config.serverUrl || this.config.apiKey) && this.config.requireSignature && !this.config.publicKey) {
89
+ throw new Error('publicKey is required when requireSignature is enabled');
90
+ }
91
+ if (!this.config.requireSignature && !this.config.allowUnsignedDevelopmentUpdates) {
92
+ throw new Error('Disabling signatures requires allowUnsignedDevelopmentUpdates for an explicit development-only setup');
93
+ }
77
94
  }
78
95
  get(key) {
79
96
  return this.config[key];
@@ -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: 'RSA-PSS',
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: (_b = data.releaseNotes) !== null && _b !== void 0 ? _b : undefined,
109
- minNativeVersion: (_c = data.minNativeVersion) !== null && _c !== void 0 ? _c : undefined,
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
- * EncryptedSharedPreferences (Keystore-backed), iOS uses Keychain,
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
- * Uses a simple file-level patching strategy that works in JavaScript.
6
- *
7
- * For production, a WASM-based bsdiff/bspatch implementation can be added
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
- * This is a simplified patch format using JSON metadata + file-level changes.
65
- * For production binary patching, a WASM bspatch implementation would be used.
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
  */