native-update-capacitor 4.3.0 → 4.4.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/README.md CHANGED
@@ -33,7 +33,7 @@ are available as an HTTP API for CI and coding agents.
33
33
 
34
34
  | | |
35
35
  |---|---|
36
- | **Version** | `4.3.0` |
36
+ | **Version** | `4.4.0` |
37
37
  | **License** | Proprietary — free to use in your apps ([LICENSE](./LICENSE)) |
38
38
  | **Node** | `>=24.16.0` (install + CLI toolchain) |
39
39
  | **Platforms** | iOS 14+ · Android 8.0+ (API 26) · Web |
@@ -518,9 +518,9 @@ indexed at [`/raw/manifest.json`](https://nativeupdate-docs.aoneahsan.com/raw/ma
518
518
  <a id="changelog"></a>
519
519
  ## 🔄 Changelog&nbsp;[#](#changelog)
520
520
 
521
- Latest release: **`4.3.0`**. Every update check now says which SDK is asking (`X-Client-Sdk: capacitor`) and its
522
- version, so the server only ever offers a Capacitor app a web bundle, never a native asset set built for another
523
- platform. No API change; 4.2.4's Android fixes are included.
521
+ Latest release: **`4.4.0`**. The CLI builds and uploads Dart code patches for Flutter apps
522
+ (`bundle flutter-patch`, `deploy --payload-type dart_aot`), for the Flutter SDK 1.1.0. The Capacitor plugin's API is
523
+ unchanged.
524
524
 
525
525
  Full history ships inside the package at `docs/CHANGELOG.md`, so it always matches the version you
526
526
  installed. It is also published as the
@@ -61,7 +61,7 @@ class LiveUpdatePlugin(
61
61
 
62
62
  companion object {
63
63
  /** This plugin's version, sent as X-Sdk-Version. Kept equal to package.json by tests/sdk-version.test.ts. */
64
- const val SDK_VERSION = "4.3.0"
64
+ const val SDK_VERSION = "4.4.0"
65
65
 
66
66
  // SharedPreferences keys. Kept as constants so A2 (boot-time
67
67
  // re-verification) and A3 (crash-loop auto-rollback) stay in sync
@@ -0,0 +1,107 @@
1
+ import chalk from 'chalk';
2
+ import crypto from 'crypto';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import zlib from 'zlib';
6
+ import { ZipArchive } from 'archiver';
7
+ import { createWriteStream } from 'fs';
8
+
9
+ /**
10
+ * Reads the entries of a ZIP (an APK) from its central directory. Handles stored and deflated entries, which is
11
+ * everything the Android build tools write. Returns a Map of name -> { read(): Buffer }.
12
+ */
13
+ async function readZip(filePath) {
14
+ const buf = await fs.readFile(filePath);
15
+ const minEnd = Math.max(0, buf.length - 0xffff - 22);
16
+ let end = -1;
17
+ for (let i = buf.length - 22; i >= minEnd; i--) {
18
+ if (buf.readUInt32LE(i) === 0x06054b50) {
19
+ end = i;
20
+ break;
21
+ }
22
+ }
23
+ if (end < 0) throw new Error(`${filePath} is not a ZIP/APK file`);
24
+ const count = buf.readUInt16LE(end + 10);
25
+ let offset = buf.readUInt32LE(end + 16);
26
+ const entries = new Map();
27
+ for (let i = 0; i < count; i++) {
28
+ if (buf.readUInt32LE(offset) !== 0x02014b50) throw new Error('Corrupt ZIP central directory');
29
+ const method = buf.readUInt16LE(offset + 10);
30
+ const compressed = buf.readUInt32LE(offset + 20);
31
+ const nameLength = buf.readUInt16LE(offset + 28);
32
+ const extraLength = buf.readUInt16LE(offset + 30);
33
+ const commentLength = buf.readUInt16LE(offset + 32);
34
+ const local = buf.readUInt32LE(offset + 42);
35
+ const name = buf.toString('utf8', offset + 46, offset + 46 + nameLength);
36
+ entries.set(name, {
37
+ read() {
38
+ const localName = buf.readUInt16LE(local + 26);
39
+ const localExtra = buf.readUInt16LE(local + 28);
40
+ const start = local + 30 + localName + localExtra;
41
+ const data = buf.subarray(start, start + compressed);
42
+ if (method === 0) return Buffer.from(data);
43
+ if (method === 8) return zlib.inflateRawSync(data);
44
+ throw new Error(`Unsupported ZIP compression method ${method} for ${name}`);
45
+ },
46
+ });
47
+ offset += 46 + nameLength + extraLength + commentLength;
48
+ }
49
+ return entries;
50
+ }
51
+
52
+ const sha256 = (data) => crypto.createHash('sha256').update(data).digest('hex');
53
+
54
+ /**
55
+ * `native-update bundle flutter-patch <apk>`: turns a Flutter RELEASE APK into a Dart code update.
56
+ *
57
+ * The patch holds lib/<abi>/libapp.so for every architecture in the APK, plus the files of --assets. Next to it,
58
+ * <out>.runtime-keys lists the SHA-256 of each libflutter.so: a device is offered the patch only when its own engine
59
+ * is one of them, because compiled Dart code runs only on the engine it was built against. Build the patch APK from
60
+ * the same Flutter version and the same native plugins as the app build it updates.
61
+ */
62
+ export async function createFlutterPatch(apkPath, options) {
63
+ const entries = await readZip(apkPath);
64
+ const abis = [...entries.keys()]
65
+ .map((name) => /^lib\/([^/]+)\/libapp\.so$/.exec(name)?.[1])
66
+ .filter(Boolean);
67
+ if (abis.length === 0) {
68
+ throw new Error(
69
+ `${apkPath} has no lib/<abi>/libapp.so. Build it with \`flutter build apk --release\` (debug builds carry no compiled Dart code).`
70
+ );
71
+ }
72
+
73
+ const out = path.resolve(options.output ?? 'flutter-patch.zip');
74
+ const stage = await fs.mkdtemp(path.join(path.dirname(out), '.flutter-patch-'));
75
+ const keys = [];
76
+ try {
77
+ for (const abi of abis) {
78
+ const engine = entries.get(`lib/${abi}/libflutter.so`);
79
+ if (!engine) throw new Error(`${apkPath} has libapp.so for ${abi} but no libflutter.so`);
80
+ keys.push({ abi, key: sha256(engine.read()) });
81
+ await fs.mkdir(path.join(stage, 'lib', abi), { recursive: true });
82
+ await fs.writeFile(path.join(stage, 'lib', abi, 'libapp.so'), entries.get(`lib/${abi}/libapp.so`).read());
83
+ }
84
+ if (options.assets) {
85
+ await fs.cp(path.resolve(options.assets), stage, { recursive: true, filter: (src) => !src.includes(`${path.sep}lib${path.sep}`) });
86
+ }
87
+
88
+ await new Promise((resolve, reject) => {
89
+ const output = createWriteStream(out);
90
+ const zip = new ZipArchive({ zlib: { level: 9 } });
91
+ output.on('close', resolve);
92
+ zip.on('error', reject);
93
+ zip.pipe(output);
94
+ zip.directory(stage, false);
95
+ zip.finalize();
96
+ });
97
+ } finally {
98
+ await fs.rm(stage, { recursive: true, force: true });
99
+ }
100
+
101
+ await fs.writeFile(`${out}.runtime-keys`, keys.map((k) => k.key).join('\n') + '\n');
102
+ const size = (await fs.stat(out)).size;
103
+ console.log(chalk.green(`✓ Dart code patch: ${out} (${(size / 1024 / 1024).toFixed(2)} MB)`));
104
+ for (const { abi, key } of keys) console.log(chalk.gray(` ${abi.padEnd(12)} engine ${key}`));
105
+ console.log(chalk.gray(` Engine keys written to ${out}.runtime-keys; deploy picks them up:`));
106
+ console.log(chalk.gray(` npx native-update deploy ${path.relative(process.cwd(), out)} --app <app> --version <v> --payload-type dart_aot --wait`));
107
+ }
@@ -117,7 +117,9 @@ export function apiClient(options) {
117
117
  const buffer = await fs.readFile(filePath);
118
118
  form.append('file', new Blob([buffer], { type: 'application/zip' }), path_basename(filePath));
119
119
  for (const [key, value] of Object.entries(fields)) {
120
- if (value !== undefined && value !== null && value !== '') {
120
+ if (Array.isArray(value)) {
121
+ for (const item of value) form.append(`${key}[]`, String(item));
122
+ } else if (value !== undefined && value !== null && value !== '') {
121
123
  form.append(key, String(value));
122
124
  }
123
125
  }
@@ -101,13 +101,25 @@ export async function deploy(target, options) {
101
101
  }
102
102
 
103
103
  const payloadType = options.payloadType ?? 'web_bundle';
104
- if (!['web_bundle', 'assets'].includes(payloadType)) {
105
- throw new Error(`--payload-type must be web_bundle or assets, not "${payloadType}"`);
104
+ if (!['web_bundle', 'assets', 'dart_aot'].includes(payloadType)) {
105
+ throw new Error(`--payload-type must be web_bundle, assets or dart_aot, not "${payloadType}"`);
106
106
  }
107
107
  console.log(chalk.gray(` Payload: ${payloadType}`));
108
+ let runtimeKeys;
109
+ if (payloadType === 'dart_aot') {
110
+ const listed = options.runtimeKeys
111
+ ? options.runtimeKeys
112
+ : await fs.readFile(`${target}.runtime-keys`, 'utf8').catch(() => '');
113
+ runtimeKeys = listed.split(/[\s,]+/).filter(Boolean);
114
+ if (runtimeKeys.length === 0) {
115
+ throw new Error('dart_aot needs engine keys: run `bundle flutter-patch` (it writes <zip>.runtime-keys) or pass --runtime-keys');
116
+ }
117
+ console.log(chalk.gray(` Engines: ${runtimeKeys.length}`));
118
+ }
108
119
 
109
120
  const { data } = await client.upload(`/apps/${options.app}/builds`, bundle.path, {
110
121
  payload_type: payloadType,
122
+ runtime_keys: runtimeKeys,
111
123
  version,
112
124
  channel: options.channel,
113
125
  release_notes: options.notes,
package/cli/index.js CHANGED
@@ -50,6 +50,25 @@ const bundleCmd = program
50
50
  .enablePositionalOptions()
51
51
  .description('Bundle management commands');
52
52
 
53
+ bundleCmd
54
+ .command('flutter-patch <apk>')
55
+ .description(`Turn a Flutter release APK into a signed-ready Dart code update (sideloaded apps only)
56
+
57
+ ${chalk.bold('Examples:')}
58
+ ${chalk.green('npx native-update bundle flutter-patch build/app/outputs/flutter-apk/app-release.apk -o patch.zip')}
59
+ ${chalk.green('npx native-update deploy patch.zip --app com.example.app --version 1.0.1 --payload-type dart_aot --wait')}`)
60
+ .option('-o, --output <file>', 'Patch ZIP to write', 'flutter-patch.zip')
61
+ .option('--assets <dir>', 'Also ship the files in this folder, read with NativeUpdate.file()')
62
+ .action(async (apk, options) => {
63
+ const { createFlutterPatch } = await import('./commands/flutter-patch.js');
64
+ try {
65
+ await createFlutterPatch(apk, options);
66
+ } catch (error) {
67
+ console.error(chalk.red(`Error: ${error.message}`));
68
+ process.exit(1);
69
+ }
70
+ });
71
+
53
72
  bundleCmd
54
73
  .command('create <webDir>')
55
74
  .alias('create-bundle')
@@ -236,9 +255,10 @@ ${chalk.bold('Examples:')}
236
255
  .option('--min-native <version>', 'Minimum native app version required')
237
256
  .option(
238
257
  '--payload-type <type>',
239
- 'web_bundle (Capacitor / React Native code) or assets (files a native Android or Flutter app reads)',
258
+ 'web_bundle (Capacitor / React Native code), assets (files a native Android or Flutter app reads) or dart_aot (a Flutter code patch from `bundle flutter-patch`)',
240
259
  'web_bundle'
241
260
  )
261
+ .option('--runtime-keys <keys>', 'dart_aot only: comma-separated engine keys (auto-read from a sibling .runtime-keys)')
242
262
  .option('--signature <file>', 'Detached signature from `bundle sign` (auto-detects a sibling .sig)')
243
263
  .option('-w, --wait', 'Wait for processing to finish (exit non-zero if it fails)', false)
244
264
  ).action(async (target, options) => {
@@ -60,7 +60,7 @@ import type { AppIdentity } from '../types/app-identity';
60
60
  * This plugin's version, sent as `X-Sdk-Version`. Kept equal to package.json (and to the Kotlin and Swift
61
61
  * constants) by tests/sdk-version.test.ts.
62
62
  */
63
- export declare const SDK_VERSION = "4.3.0";
63
+ export declare const SDK_VERSION = "4.4.0";
64
64
  /** Request inputs for a single backend update check. */
65
65
  export interface UpdateCheckRequest {
66
66
  /** API key issued by the dashboard; sent as `X-API-Key`. Required. */
@@ -59,7 +59,7 @@
59
59
  * This plugin's version, sent as `X-Sdk-Version`. Kept equal to package.json (and to the Kotlin and Swift
60
60
  * constants) by tests/sdk-version.test.ts.
61
61
  */
62
- export const SDK_VERSION = '4.3.0';
62
+ export const SDK_VERSION = '4.4.0';
63
63
  /**
64
64
  * Perform one update check against the Native Update backend.
65
65
  *