react-native-update-cli 2.19.0 → 2.20.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/lib/api.js CHANGED
@@ -230,7 +230,10 @@ async function uploadFile(fn, key, appId) {
230
230
  method: 'PUT',
231
231
  body: fileStream,
232
232
  // 预签名 PUT 不接受 chunked 传输,必须显式声明长度
233
- headers: { 'content-length': String(fileSize), ...(resp.headers || {}) },
233
+ headers: {
234
+ 'content-length': String(fileSize),
235
+ ...(resp.headers || {}),
236
+ },
234
237
  });
235
238
  }
236
239
  catch (error) {
package/lib/diff.js CHANGED
@@ -42,6 +42,7 @@ const global_dirs_1 = require("global-dirs");
42
42
  const os_1 = __importDefault(require("os"));
43
43
  const path_1 = __importDefault(require("path"));
44
44
  const yazl_1 = require("yazl");
45
+ const native_package_1 = require("./native-package");
45
46
  const utils_1 = require("./utils");
46
47
  const constants_1 = require("./utils/constants");
47
48
  const hbcTransform_1 = require("./utils/hbcTransform");
@@ -284,7 +285,7 @@ async function diffFromPPK(origin, next, output, diffFn, bundleOptions = {}) {
284
285
  : {}),
285
286
  });
286
287
  }
287
- async function diffFromPackage(origin, next, output, diffFn, originBundleName, transformPackagePath = (v) => v, bundleOptions = {}) {
288
+ async function diffFromPackage(origin, next, output, diffFn, platform, bundleOptions = {}) {
288
289
  const originEntries = {};
289
290
  const originMap = {};
290
291
  let originSource;
@@ -296,15 +297,16 @@ async function diffFromPackage(origin, next, output, diffFn, originBundleName, t
296
297
  const copiesCrc = {};
297
298
  await (0, zip_entries_1.enumZipEntries)(origin, async (entry, zipFile) => {
298
299
  if (!/\/$/.test(entry.fileName)) {
299
- const fn = transformPackagePath(entry.fileName);
300
- if (!fn) {
300
+ const resolvedEntry = (0, native_package_1.resolveNativePackageEntry)(platform, entry.fileName);
301
+ if (!resolvedEntry) {
301
302
  return;
302
303
  }
304
+ const fn = resolvedEntry.diffPath;
303
305
  //console.log(fn);
304
306
  // isFile
305
307
  originEntries[fn] = entry.crc32;
306
308
  originMap[entry.crc32] = fn;
307
- if (fn === originBundleName) {
309
+ if (resolvedEntry.kind === 'bundle') {
308
310
  // This is source.
309
311
  originSource = await (0, zip_entries_1.readEntry)(entry, zipFile);
310
312
  }
@@ -431,10 +433,6 @@ function diffArgsCheck(args, options, diffFnName) {
431
433
  realOutput: output.replace(/\$\{time\}/g, `${Date.now()}`),
432
434
  };
433
435
  }
434
- const transformIpaPackagePath = (v) => {
435
- const match = /^Payload\/[^/]+\/(.+)$/.exec(v);
436
- return match?.[1];
437
- };
438
436
  const createDiffCommand = ({ diffFnName, target }) => async ({ args, options }) => {
439
437
  const { origin, next, realOutput, diffFn } = diffArgsCheck(args, options, diffFnName);
440
438
  const bundleOptions = resolveBundleDiffOptions(options);
@@ -442,7 +440,7 @@ const createDiffCommand = ({ diffFnName, target }) => async ({ args, options })
442
440
  await diffFromPPK(origin, next, realOutput, diffFn, bundleOptions);
443
441
  }
444
442
  else {
445
- await diffFromPackage(origin, next, realOutput, diffFn, target.originBundleName, target.transformPackagePath, bundleOptions);
443
+ await diffFromPackage(origin, next, realOutput, diffFn, target.platform, bundleOptions);
446
444
  }
447
445
  console.log((0, i18n_1.t)('diffPackageGenerated', { output: realOutput }));
448
446
  };
@@ -455,22 +453,21 @@ exports.diffCommands = {
455
453
  diffFnName: 'hdiffFromApk',
456
454
  target: {
457
455
  kind: 'package',
458
- originBundleName: 'assets/index.android.bundle',
456
+ platform: 'android',
459
457
  },
460
458
  }),
461
459
  hdiffFromApp: createDiffCommand({
462
460
  diffFnName: 'hdiffFromApp',
463
461
  target: {
464
462
  kind: 'package',
465
- originBundleName: 'resources/rawfile/bundle.harmony.js',
463
+ platform: 'harmony',
466
464
  },
467
465
  }),
468
466
  hdiffFromIpa: createDiffCommand({
469
467
  diffFnName: 'hdiffFromIpa',
470
468
  target: {
471
469
  kind: 'package',
472
- originBundleName: 'main.jsbundle',
473
- transformPackagePath: transformIpaPackagePath,
470
+ platform: 'ios',
474
471
  },
475
472
  }),
476
473
  };
@@ -0,0 +1,17 @@
1
+ import type { Platform } from './types';
2
+ export type NativePackageEntry = {
3
+ diffPath: string;
4
+ kind: 'bundle' | 'resource';
5
+ };
6
+ /**
7
+ * Resolve an archive entry that can be used as a native-package diff origin.
8
+ * Upload extraction and diff indexing deliberately share this function so a
9
+ * file cannot be removed from the uploaded baseline while still being
10
+ * considered by the local diff implementation (or vice versa).
11
+ */
12
+ export declare function resolveNativePackageEntry(platform: Platform, entryName: string): NativePackageEntry | undefined;
13
+ /**
14
+ * Repack a native package with only the bundle and resources required by
15
+ * package-origin diffs. Entry names and nested Harmony HAP structure are kept.
16
+ */
17
+ export declare function createSlimNativePackage(source: string, output: string, platform: Platform): Promise<void>;
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.resolveNativePackageEntry = resolveNativePackageEntry;
40
+ exports.createSlimNativePackage = createSlimNativePackage;
41
+ const fs = __importStar(require("fs-extra"));
42
+ const os_1 = __importDefault(require("os"));
43
+ const path_1 = __importDefault(require("path"));
44
+ const yauzl_1 = require("yauzl");
45
+ const yazl_1 = require("yazl");
46
+ const nativePackageRules = {
47
+ android: {
48
+ resolveEntry: (entryName) => {
49
+ if (entryName === 'assets/index.android.bundle') {
50
+ return { diffPath: entryName, kind: 'bundle' };
51
+ }
52
+ if (entryName.startsWith('assets/') || entryName.startsWith('res/')) {
53
+ return { diffPath: entryName, kind: 'resource' };
54
+ }
55
+ },
56
+ },
57
+ ios: {
58
+ resolveEntry: (entryName) => {
59
+ const match = /^Payload\/[^/]+\.app\/(.+)$/.exec(entryName);
60
+ const appPath = match?.[1];
61
+ if (appPath === 'main.jsbundle') {
62
+ return { diffPath: appPath, kind: 'bundle' };
63
+ }
64
+ if (appPath?.startsWith('assets/')) {
65
+ return { diffPath: appPath, kind: 'resource' };
66
+ }
67
+ },
68
+ },
69
+ harmony: {
70
+ resolveEntry: (entryName) => {
71
+ if (entryName === 'resources/rawfile/bundle.harmony.js') {
72
+ return { diffPath: entryName, kind: 'bundle' };
73
+ }
74
+ if (entryName.startsWith('resources/rawfile/assets/')) {
75
+ return { diffPath: entryName, kind: 'resource' };
76
+ }
77
+ },
78
+ },
79
+ };
80
+ /**
81
+ * Resolve an archive entry that can be used as a native-package diff origin.
82
+ * Upload extraction and diff indexing deliberately share this function so a
83
+ * file cannot be removed from the uploaded baseline while still being
84
+ * considered by the local diff implementation (or vice versa).
85
+ */
86
+ function resolveNativePackageEntry(platform, entryName) {
87
+ return nativePackageRules[platform].resolveEntry(entryName);
88
+ }
89
+ function copyEntryToZip(sourceZip, entry, outputZip) {
90
+ return new Promise((resolve, reject) => {
91
+ sourceZip.openReadStream(entry, (error, readStream) => {
92
+ if (error) {
93
+ reject(error);
94
+ return;
95
+ }
96
+ if (!readStream) {
97
+ reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
98
+ return;
99
+ }
100
+ readStream.once('error', reject);
101
+ readStream.once('end', () => resolve());
102
+ outputZip.addReadStream(readStream, entry.fileName, {
103
+ compress: entry.compressionMethod !== 0,
104
+ mtime: entry.getLastModDate(),
105
+ });
106
+ });
107
+ });
108
+ }
109
+ function extractEntryToFile(sourceZip, entry, outputPath) {
110
+ return new Promise((resolve, reject) => {
111
+ sourceZip.openReadStream(entry, (error, readStream) => {
112
+ if (error) {
113
+ reject(error);
114
+ return;
115
+ }
116
+ if (!readStream) {
117
+ reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
118
+ return;
119
+ }
120
+ const writeStream = fs.createWriteStream(outputPath);
121
+ readStream.once('error', reject);
122
+ writeStream.once('error', reject);
123
+ writeStream.once('close', () => resolve());
124
+ readStream.pipe(writeStream);
125
+ });
126
+ });
127
+ }
128
+ async function filterNativePackageArchive(source, output, platform) {
129
+ await fs.ensureDir(path_1.default.dirname(output));
130
+ const scratchDir = await fs.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'rnu-native-package-entry-'));
131
+ const outputZip = new yazl_1.ZipFile();
132
+ const writePromise = new Promise((resolve, reject) => {
133
+ outputZip.outputStream.once('error', reject);
134
+ outputZip.outputStream
135
+ .pipe(fs.createWriteStream(output))
136
+ .once('error', reject)
137
+ .once('close', () => resolve());
138
+ });
139
+ let includedEntries = 0;
140
+ let includedBundles = 0;
141
+ let nestedArchiveIndex = 0;
142
+ try {
143
+ await new Promise((resolve, reject) => {
144
+ (0, yauzl_1.open)(source, { lazyEntries: true }, (openError, sourceZip) => {
145
+ if (openError || !sourceZip) {
146
+ reject(openError ?? new Error(`Unable to open zip file: ${source}`));
147
+ return;
148
+ }
149
+ let settled = false;
150
+ const fail = (error) => {
151
+ if (settled) {
152
+ return;
153
+ }
154
+ settled = true;
155
+ sourceZip.close();
156
+ reject(error);
157
+ };
158
+ sourceZip.once('error', fail);
159
+ sourceZip.once('end', () => {
160
+ if (!settled) {
161
+ settled = true;
162
+ resolve();
163
+ }
164
+ });
165
+ sourceZip.on('entry', async (entry) => {
166
+ try {
167
+ if (entry.fileName.endsWith('/')) {
168
+ sourceZip.readEntry();
169
+ return;
170
+ }
171
+ // Harmony .app packages contain one or more nested .hap archives.
172
+ // Rebuild those archives recursively and keep their outer path so
173
+ // the slim package has the same nesting as the original package.
174
+ if (platform === 'harmony' &&
175
+ entry.fileName.toLowerCase().endsWith('.hap')) {
176
+ const index = nestedArchiveIndex++;
177
+ const nestedSource = path_1.default.join(scratchDir, `${index}.source.hap`);
178
+ const nestedOutput = path_1.default.join(scratchDir, `${index}.slim.hap`);
179
+ await extractEntryToFile(sourceZip, entry, nestedSource);
180
+ const nestedResult = await filterNativePackageArchive(nestedSource, nestedOutput, platform);
181
+ if (nestedResult.entries > 0) {
182
+ outputZip.addFile(nestedOutput, entry.fileName, {
183
+ compress: entry.compressionMethod !== 0,
184
+ mtime: entry.getLastModDate(),
185
+ });
186
+ includedEntries += nestedResult.entries;
187
+ includedBundles += nestedResult.bundles;
188
+ }
189
+ sourceZip.readEntry();
190
+ return;
191
+ }
192
+ const resolvedEntry = resolveNativePackageEntry(platform, entry.fileName);
193
+ if (resolvedEntry) {
194
+ await copyEntryToZip(sourceZip, entry, outputZip);
195
+ includedEntries += 1;
196
+ if (resolvedEntry.kind === 'bundle') {
197
+ includedBundles += 1;
198
+ }
199
+ }
200
+ sourceZip.readEntry();
201
+ }
202
+ catch (error) {
203
+ fail(error);
204
+ }
205
+ });
206
+ sourceZip.readEntry();
207
+ });
208
+ });
209
+ outputZip.end();
210
+ await writePromise;
211
+ return { bundles: includedBundles, entries: includedEntries };
212
+ }
213
+ catch (error) {
214
+ outputZip.end();
215
+ await writePromise.catch(() => { });
216
+ throw error;
217
+ }
218
+ finally {
219
+ await fs.remove(scratchDir);
220
+ }
221
+ }
222
+ /**
223
+ * Repack a native package with only the bundle and resources required by
224
+ * package-origin diffs. Entry names and nested Harmony HAP structure are kept.
225
+ */
226
+ async function createSlimNativePackage(source, output, platform) {
227
+ const result = await filterNativePackageArchive(source, output, platform);
228
+ if (result.bundles === 0) {
229
+ await fs.remove(output);
230
+ throw new Error(`Bundle entry not found in native package: ${source}`);
231
+ }
232
+ }
package/lib/package.js CHANGED
@@ -46,6 +46,7 @@ const path_1 = __importDefault(require("path"));
46
46
  const tty_table_1 = __importDefault(require("tty-table"));
47
47
  const api_1 = require("./api");
48
48
  const app_1 = require("./app");
49
+ const native_package_1 = require("./native-package");
49
50
  const utils_1 = require("./utils");
50
51
  const aab_1 = require("./utils/app-info-parser/aab");
51
52
  const dep_versions_1 = require("./utils/dep-versions");
@@ -114,23 +115,31 @@ async function uploadNativePackage(filePath, options, config) {
114
115
  if (customVersion !== undefined) {
115
116
  console.log((0, i18n_1.t)('usingCustomVersion', { version: versionName }));
116
117
  }
117
- const { hash } = await (0, api_1.uploadFile)(filePath, undefined, appId);
118
- const normalizedBuildTime = config.normalizeBuildTime
119
- ? config.normalizeBuildTime(buildTime)
120
- : buildTime;
121
- const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
122
- const { id } = await (0, api_1.post)(`/app/${appId}/package/create`, {
123
- name: versionName,
124
- hash,
125
- buildTime: uploadBuildTime,
126
- deps: dep_versions_1.depVersions,
127
- commit: await (0, git_1.getCommitInfo)(),
128
- });
129
- console.log((0, i18n_1.t)(config.successKey, {
130
- id,
131
- version: versionName,
132
- buildTime: uploadBuildTime,
133
- }));
118
+ const tempRoot = await fs.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'rnu-native-package-upload-'));
119
+ const slimPackagePath = path_1.default.join(tempRoot, `package${config.extension}`);
120
+ try {
121
+ await (0, native_package_1.createSlimNativePackage)(filePath, slimPackagePath, config.platform);
122
+ const { hash } = await (0, api_1.uploadFile)(slimPackagePath, undefined, appId);
123
+ const normalizedBuildTime = config.normalizeBuildTime
124
+ ? config.normalizeBuildTime(buildTime)
125
+ : buildTime;
126
+ const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
127
+ const { id } = await (0, api_1.post)(`/app/${appId}/package/create`, {
128
+ name: versionName,
129
+ hash,
130
+ buildTime: uploadBuildTime,
131
+ deps: dep_versions_1.depVersions,
132
+ commit: await (0, git_1.getCommitInfo)(),
133
+ });
134
+ console.log((0, i18n_1.t)(config.successKey, {
135
+ id,
136
+ version: versionName,
137
+ buildTime: uploadBuildTime,
138
+ }));
139
+ }
140
+ finally {
141
+ await fs.remove(tempRoot);
142
+ }
134
143
  }
135
144
  async function listPackage(appId) {
136
145
  const allPkgs = (await (0, api_1.getAllPackages)(appId)) || [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update-cli",
3
- "version": "2.19.0",
3
+ "version": "2.20.0",
4
4
  "description": "command line tool for react-native-update (remote updates for react native)",
5
5
  "main": "lib/exports.js",
6
6
  "types": "lib/exports.d.ts",
package/src/api.ts CHANGED
@@ -260,7 +260,10 @@ export async function uploadFile(
260
260
  method: 'PUT',
261
261
  body: fileStream,
262
262
  // 预签名 PUT 不接受 chunked 传输,必须显式声明长度
263
- headers: { 'content-length': String(fileSize), ...(resp.headers || {}) },
263
+ headers: {
264
+ 'content-length': String(fileSize),
265
+ ...(resp.headers || {}),
266
+ },
264
267
  });
265
268
  } catch (error) {
266
269
  if (isProxyRelatedError(error)) {
package/src/diff.ts CHANGED
@@ -4,7 +4,8 @@ import os from 'os';
4
4
  import path from 'path';
5
5
  import type { Entry, ZipFile as YauzlZipFile } from 'yauzl';
6
6
  import { ZipFile as YazlZipFile } from 'yazl';
7
- import type { CommandContext } from './types';
7
+ import { resolveNativePackageEntry } from './native-package';
8
+ import type { CommandContext, Platform } from './types';
8
9
  import { translateOptions } from './utils';
9
10
  import { isPPKBundleFileName, scriptName, tempDir } from './utils/constants';
10
11
  import {
@@ -51,13 +52,11 @@ type HdiffModule = {
51
52
  type EntryMap = Record<string, { crc32: number; fileName: string }>;
52
53
  type CrcMap = Record<number, string>;
53
54
  type CopyMap = Record<string, string>;
54
- type PackagePathTransform = (v: string) => string | undefined;
55
55
  type DiffTarget =
56
56
  | { kind: 'ppk' }
57
57
  | {
58
58
  kind: 'package';
59
- originBundleName: string;
60
- transformPackagePath?: PackagePathTransform;
59
+ platform: Platform;
61
60
  };
62
61
  type DiffCommandConfig = {
63
62
  diffFnName: string;
@@ -419,8 +418,7 @@ async function diffFromPackage(
419
418
  next: string,
420
419
  output: string,
421
420
  diffFn: Diff,
422
- originBundleName: string,
423
- transformPackagePath: (v: string) => string | undefined = (v: string) => v,
421
+ platform: Platform,
424
422
  bundleOptions: BundleDiffOptions = {},
425
423
  ) {
426
424
  const originEntries: Record<string, number> = {};
@@ -437,17 +435,18 @@ async function diffFromPackage(
437
435
 
438
436
  await enumZipEntries(origin, async (entry, zipFile) => {
439
437
  if (!/\/$/.test(entry.fileName)) {
440
- const fn = transformPackagePath(entry.fileName);
441
- if (!fn) {
438
+ const resolvedEntry = resolveNativePackageEntry(platform, entry.fileName);
439
+ if (!resolvedEntry) {
442
440
  return;
443
441
  }
442
+ const fn = resolvedEntry.diffPath;
444
443
 
445
444
  //console.log(fn);
446
445
  // isFile
447
446
  originEntries[fn] = entry.crc32;
448
447
  originMap[entry.crc32] = fn;
449
448
 
450
- if (fn === originBundleName) {
449
+ if (resolvedEntry.kind === 'bundle') {
451
450
  // This is source.
452
451
  originSource = await readEntry(entry, zipFile);
453
452
  }
@@ -625,11 +624,6 @@ function diffArgsCheck(
625
624
  };
626
625
  }
627
626
 
628
- const transformIpaPackagePath: PackagePathTransform = (v) => {
629
- const match = /^Payload\/[^/]+\/(.+)$/.exec(v);
630
- return match?.[1];
631
- };
632
-
633
627
  const createDiffCommand =
634
628
  ({ diffFnName, target }: DiffCommandConfig) =>
635
629
  async ({ args, options }: CommandContext) => {
@@ -650,8 +644,7 @@ const createDiffCommand =
650
644
  next,
651
645
  realOutput,
652
646
  diffFn,
653
- target.originBundleName,
654
- target.transformPackagePath,
647
+ target.platform,
655
648
  bundleOptions,
656
649
  );
657
650
  }
@@ -668,22 +661,21 @@ export const diffCommands = {
668
661
  diffFnName: 'hdiffFromApk',
669
662
  target: {
670
663
  kind: 'package',
671
- originBundleName: 'assets/index.android.bundle',
664
+ platform: 'android',
672
665
  },
673
666
  }),
674
667
  hdiffFromApp: createDiffCommand({
675
668
  diffFnName: 'hdiffFromApp',
676
669
  target: {
677
670
  kind: 'package',
678
- originBundleName: 'resources/rawfile/bundle.harmony.js',
671
+ platform: 'harmony',
679
672
  },
680
673
  }),
681
674
  hdiffFromIpa: createDiffCommand({
682
675
  diffFnName: 'hdiffFromIpa',
683
676
  target: {
684
677
  kind: 'package',
685
- originBundleName: 'main.jsbundle',
686
- transformPackagePath: transformIpaPackagePath,
678
+ platform: 'ios',
687
679
  },
688
680
  }),
689
681
  };
@@ -0,0 +1,248 @@
1
+ import * as fs from 'fs-extra';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import {
5
+ type Entry,
6
+ open as openZipFile,
7
+ type ZipFile as YauzlZipFile,
8
+ } from 'yauzl';
9
+ import { ZipFile as YazlZipFile } from 'yazl';
10
+ import type { Platform } from './types';
11
+
12
+ export type NativePackageEntry = {
13
+ diffPath: string;
14
+ kind: 'bundle' | 'resource';
15
+ };
16
+
17
+ type NativePackageRule = {
18
+ resolveEntry: (entryName: string) => NativePackageEntry | undefined;
19
+ };
20
+
21
+ const nativePackageRules: Record<Platform, NativePackageRule> = {
22
+ android: {
23
+ resolveEntry: (entryName) => {
24
+ if (entryName === 'assets/index.android.bundle') {
25
+ return { diffPath: entryName, kind: 'bundle' };
26
+ }
27
+ if (entryName.startsWith('assets/') || entryName.startsWith('res/')) {
28
+ return { diffPath: entryName, kind: 'resource' };
29
+ }
30
+ },
31
+ },
32
+ ios: {
33
+ resolveEntry: (entryName) => {
34
+ const match = /^Payload\/[^/]+\.app\/(.+)$/.exec(entryName);
35
+ const appPath = match?.[1];
36
+ if (appPath === 'main.jsbundle') {
37
+ return { diffPath: appPath, kind: 'bundle' };
38
+ }
39
+ if (appPath?.startsWith('assets/')) {
40
+ return { diffPath: appPath, kind: 'resource' };
41
+ }
42
+ },
43
+ },
44
+ harmony: {
45
+ resolveEntry: (entryName) => {
46
+ if (entryName === 'resources/rawfile/bundle.harmony.js') {
47
+ return { diffPath: entryName, kind: 'bundle' };
48
+ }
49
+ if (entryName.startsWith('resources/rawfile/assets/')) {
50
+ return { diffPath: entryName, kind: 'resource' };
51
+ }
52
+ },
53
+ },
54
+ };
55
+
56
+ /**
57
+ * Resolve an archive entry that can be used as a native-package diff origin.
58
+ * Upload extraction and diff indexing deliberately share this function so a
59
+ * file cannot be removed from the uploaded baseline while still being
60
+ * considered by the local diff implementation (or vice versa).
61
+ */
62
+ export function resolveNativePackageEntry(
63
+ platform: Platform,
64
+ entryName: string,
65
+ ): NativePackageEntry | undefined {
66
+ return nativePackageRules[platform].resolveEntry(entryName);
67
+ }
68
+
69
+ function copyEntryToZip(
70
+ sourceZip: YauzlZipFile,
71
+ entry: Entry,
72
+ outputZip: YazlZipFile,
73
+ ): Promise<void> {
74
+ return new Promise((resolve, reject) => {
75
+ sourceZip.openReadStream(entry, (error, readStream) => {
76
+ if (error) {
77
+ reject(error);
78
+ return;
79
+ }
80
+ if (!readStream) {
81
+ reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
82
+ return;
83
+ }
84
+
85
+ readStream.once('error', reject);
86
+ readStream.once('end', () => resolve());
87
+ outputZip.addReadStream(readStream, entry.fileName, {
88
+ compress: entry.compressionMethod !== 0,
89
+ mtime: entry.getLastModDate(),
90
+ });
91
+ });
92
+ });
93
+ }
94
+
95
+ function extractEntryToFile(
96
+ sourceZip: YauzlZipFile,
97
+ entry: Entry,
98
+ outputPath: string,
99
+ ): Promise<void> {
100
+ return new Promise((resolve, reject) => {
101
+ sourceZip.openReadStream(entry, (error, readStream) => {
102
+ if (error) {
103
+ reject(error);
104
+ return;
105
+ }
106
+ if (!readStream) {
107
+ reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
108
+ return;
109
+ }
110
+
111
+ const writeStream = fs.createWriteStream(outputPath);
112
+ readStream.once('error', reject);
113
+ writeStream.once('error', reject);
114
+ writeStream.once('close', () => resolve());
115
+ readStream.pipe(writeStream);
116
+ });
117
+ });
118
+ }
119
+
120
+ async function filterNativePackageArchive(
121
+ source: string,
122
+ output: string,
123
+ platform: Platform,
124
+ ): Promise<{ bundles: number; entries: number }> {
125
+ await fs.ensureDir(path.dirname(output));
126
+ const scratchDir = await fs.mkdtemp(
127
+ path.join(os.tmpdir(), 'rnu-native-package-entry-'),
128
+ );
129
+ const outputZip = new YazlZipFile();
130
+ const writePromise = new Promise<void>((resolve, reject) => {
131
+ outputZip.outputStream.once('error', reject);
132
+ outputZip.outputStream
133
+ .pipe(fs.createWriteStream(output))
134
+ .once('error', reject)
135
+ .once('close', () => resolve());
136
+ });
137
+ let includedEntries = 0;
138
+ let includedBundles = 0;
139
+ let nestedArchiveIndex = 0;
140
+
141
+ try {
142
+ await new Promise<void>((resolve, reject) => {
143
+ openZipFile(source, { lazyEntries: true }, (openError, sourceZip) => {
144
+ if (openError || !sourceZip) {
145
+ reject(openError ?? new Error(`Unable to open zip file: ${source}`));
146
+ return;
147
+ }
148
+
149
+ let settled = false;
150
+ const fail = (error: unknown) => {
151
+ if (settled) {
152
+ return;
153
+ }
154
+ settled = true;
155
+ sourceZip.close();
156
+ reject(error);
157
+ };
158
+
159
+ sourceZip.once('error', fail);
160
+ sourceZip.once('end', () => {
161
+ if (!settled) {
162
+ settled = true;
163
+ resolve();
164
+ }
165
+ });
166
+ sourceZip.on('entry', async (entry) => {
167
+ try {
168
+ if (entry.fileName.endsWith('/')) {
169
+ sourceZip.readEntry();
170
+ return;
171
+ }
172
+
173
+ // Harmony .app packages contain one or more nested .hap archives.
174
+ // Rebuild those archives recursively and keep their outer path so
175
+ // the slim package has the same nesting as the original package.
176
+ if (
177
+ platform === 'harmony' &&
178
+ entry.fileName.toLowerCase().endsWith('.hap')
179
+ ) {
180
+ const index = nestedArchiveIndex++;
181
+ const nestedSource = path.join(scratchDir, `${index}.source.hap`);
182
+ const nestedOutput = path.join(scratchDir, `${index}.slim.hap`);
183
+ await extractEntryToFile(sourceZip, entry, nestedSource);
184
+ const nestedResult = await filterNativePackageArchive(
185
+ nestedSource,
186
+ nestedOutput,
187
+ platform,
188
+ );
189
+ if (nestedResult.entries > 0) {
190
+ outputZip.addFile(nestedOutput, entry.fileName, {
191
+ compress: entry.compressionMethod !== 0,
192
+ mtime: entry.getLastModDate(),
193
+ });
194
+ includedEntries += nestedResult.entries;
195
+ includedBundles += nestedResult.bundles;
196
+ }
197
+ sourceZip.readEntry();
198
+ return;
199
+ }
200
+
201
+ const resolvedEntry = resolveNativePackageEntry(
202
+ platform,
203
+ entry.fileName,
204
+ );
205
+ if (resolvedEntry) {
206
+ await copyEntryToZip(sourceZip, entry, outputZip);
207
+ includedEntries += 1;
208
+ if (resolvedEntry.kind === 'bundle') {
209
+ includedBundles += 1;
210
+ }
211
+ }
212
+ sourceZip.readEntry();
213
+ } catch (error) {
214
+ fail(error);
215
+ }
216
+ });
217
+
218
+ sourceZip.readEntry();
219
+ });
220
+ });
221
+
222
+ outputZip.end();
223
+ await writePromise;
224
+ return { bundles: includedBundles, entries: includedEntries };
225
+ } catch (error) {
226
+ outputZip.end();
227
+ await writePromise.catch(() => {});
228
+ throw error;
229
+ } finally {
230
+ await fs.remove(scratchDir);
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Repack a native package with only the bundle and resources required by
236
+ * package-origin diffs. Entry names and nested Harmony HAP structure are kept.
237
+ */
238
+ export async function createSlimNativePackage(
239
+ source: string,
240
+ output: string,
241
+ platform: Platform,
242
+ ): Promise<void> {
243
+ const result = await filterNativePackageArchive(source, output, platform);
244
+ if (result.bundles === 0) {
245
+ await fs.remove(output);
246
+ throw new Error(`Bundle entry not found in native package: ${source}`);
247
+ }
248
+ }
package/src/package.ts CHANGED
@@ -4,6 +4,7 @@ import path from 'path';
4
4
  import Table from 'tty-table';
5
5
  import { doDelete, getAllPackages, post, uploadFile } from './api';
6
6
  import { getPlatform, getSelectedApp } from './app';
7
+ import { createSlimNativePackage } from './native-package';
7
8
  import type { Platform } from './types';
8
9
  import {
9
10
  getAabInfo,
@@ -141,26 +142,35 @@ async function uploadNativePackage(
141
142
  console.log(t('usingCustomVersion', { version: versionName }));
142
143
  }
143
144
 
144
- const { hash } = await uploadFile(filePath, undefined, appId);
145
- const normalizedBuildTime = config.normalizeBuildTime
146
- ? config.normalizeBuildTime(buildTime)
147
- : buildTime;
148
- const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
149
-
150
- const { id } = await post(`/app/${appId}/package/create`, {
151
- name: versionName,
152
- hash,
153
- buildTime: uploadBuildTime,
154
- deps: depVersions,
155
- commit: await getCommitInfo(),
156
- });
157
- console.log(
158
- t(config.successKey, {
159
- id,
160
- version: versionName,
161
- buildTime: uploadBuildTime,
162
- }),
145
+ const tempRoot = await fs.mkdtemp(
146
+ path.join(os.tmpdir(), 'rnu-native-package-upload-'),
163
147
  );
148
+ const slimPackagePath = path.join(tempRoot, `package${config.extension}`);
149
+ try {
150
+ await createSlimNativePackage(filePath, slimPackagePath, config.platform);
151
+ const { hash } = await uploadFile(slimPackagePath, undefined, appId);
152
+ const normalizedBuildTime = config.normalizeBuildTime
153
+ ? config.normalizeBuildTime(buildTime)
154
+ : buildTime;
155
+ const uploadBuildTime = normalizeUploadBuildTime(normalizedBuildTime);
156
+
157
+ const { id } = await post(`/app/${appId}/package/create`, {
158
+ name: versionName,
159
+ hash,
160
+ buildTime: uploadBuildTime,
161
+ deps: depVersions,
162
+ commit: await getCommitInfo(),
163
+ });
164
+ console.log(
165
+ t(config.successKey, {
166
+ id,
167
+ version: versionName,
168
+ buildTime: uploadBuildTime,
169
+ }),
170
+ );
171
+ } finally {
172
+ await fs.remove(tempRoot);
173
+ }
164
174
  }
165
175
 
166
176
  export async function listPackage(appId: string) {