react-native-update-cli 2.16.1 → 2.18.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/src/diff.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import * as fs from 'fs-extra';
2
2
  import { npm, yarn } from 'global-dirs';
3
+ import os from 'os';
3
4
  import path from 'path';
4
5
  import type { Entry, ZipFile as YauzlZipFile } from 'yauzl';
5
6
  import { ZipFile as YazlZipFile } from 'yazl';
6
7
  import type { CommandContext } from './types';
7
8
  import { translateOptions } from './utils';
8
9
  import { isPPKBundleFileName, scriptName, tempDir } from './utils/constants';
10
+ import {
11
+ type HbcTransformMeta,
12
+ transformHbcWithLayout,
13
+ tryTransformPair,
14
+ } from './utils/hbcTransform';
9
15
  import { t } from './utils/i18n';
10
16
  import {
11
17
  enumZipEntries,
@@ -23,8 +29,24 @@ type Diff = (
23
29
  oldSource?: Buffer,
24
30
  newSource?: Buffer,
25
31
  ) => Buffer | Promise<Buffer>;
32
+ type Patch = (oldSource: Buffer, diffData: Buffer) => Buffer;
33
+ type DiffStream = (
34
+ oldPath: string,
35
+ newPath: string,
36
+ outDiffPath: string,
37
+ ) => unknown | Promise<unknown>;
38
+ type PatchStream = (
39
+ oldPath: string,
40
+ diffPath: string,
41
+ outNewPath: string,
42
+ ) => unknown | Promise<unknown>;
26
43
  type HdiffModule = {
27
44
  diff?: Diff;
45
+ patch?: Patch;
46
+ diffStream?: DiffStream;
47
+ patchStream?: PatchStream;
48
+ diffSingleStream?: DiffStream;
49
+ patchSingleStream?: PatchStream;
28
50
  };
29
51
  type EntryMap = Record<string, { crc32: number; fileName: string }>;
30
52
  type CrcMap = Record<number, string>;
@@ -77,19 +99,157 @@ function createOutputZip(output: string) {
77
99
  return { zipfile, writePromise };
78
100
  }
79
101
 
102
+ // baseline patch 小于该值时跳过变换尝试:节省一次 diff 的 CPU,
103
+ // 且此时变换收益上限(~25%)也抵不过元数据开销
104
+ const HBC_TRANSFORM_MIN_BASELINE_BYTES = 64 * 1024;
105
+
106
+ type BundleDiffOptions = {
107
+ hbcTransform?: boolean;
108
+ patchFn?: Patch;
109
+ /** 测试用:覆写跳过变换尝试的 baseline 大小下限 */
110
+ minBaselineBytes?: number;
111
+ /**
112
+ * bundle 尺寸达到该字节数时改走流式 diff:生成端内存 O(块匹配),
113
+ * 彻底绕开内存 diff 的后缀数组峰值(~6× bundle)。0/未设 = 关闭。
114
+ * 优先使用 single 格式流式生成(diffSingleStream,产物与 diff() 同格式,
115
+ * 老客户端可直接应用,任何轨道均可开启);仅当 single 流式不可用时回退
116
+ * HDIFF13(diffStream)——该格式老客户端不识别,只应在 v2 轨道下开启。
117
+ */
118
+ streamThresholdBytes?: number;
119
+ diffStreamFn?: DiffStream;
120
+ patchStreamFn?: PatchStream;
121
+ /** 流式产物是否为 single 格式(决定 baseline 轨道是否可用) */
122
+ streamEmitsSingleFormat?: boolean;
123
+ };
124
+
125
+ /**
126
+ * 大 bundle 流式路径:可选 HBC 变换(在 buffer 上做,写临时文件)后
127
+ * diffStream 生成 HDIFF13,并以 patchStream + T⁻¹ 做 round-trip 自检。
128
+ * 自检失败直接抛错(此路径没有 baseline 可回退——内存 diff 正是要避免的),
129
+ * 由上层按任务失败重试/告警处理。
130
+ */
131
+ async function buildStreamBundlePatch(
132
+ originSource: Buffer,
133
+ newSource: Buffer,
134
+ options: BundleDiffOptions,
135
+ ): Promise<{ patchData: Buffer; hbcTransform?: HbcTransformMeta }> {
136
+ const diffStreamFn = options.diffStreamFn;
137
+ const patchStreamFn = options.patchStreamFn;
138
+ if (!diffStreamFn || !patchStreamFn) {
139
+ throw new Error(
140
+ 'stream diff requires diffStream/patchStream from node-hdiffpatch',
141
+ );
142
+ }
143
+
144
+ const pair = options.hbcTransform
145
+ ? tryTransformPair(originSource, newSource)
146
+ : null;
147
+ const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'rnu-stream-diff-'));
148
+ try {
149
+ const oldFile = path.join(tempRoot, 'old.bin');
150
+ const newFile = path.join(tempRoot, 'new.bin');
151
+ const patchFile = path.join(tempRoot, 'patch.bin');
152
+ const restoredFile = path.join(tempRoot, 'restored.bin');
153
+ fs.writeFileSync(oldFile, pair ? pair.tOld : originSource);
154
+ fs.writeFileSync(newFile, pair ? pair.tNew : newSource);
155
+
156
+ await diffStreamFn(oldFile, newFile, patchFile);
157
+
158
+ // round-trip 自检:patchStream 还原(变换域则再做 T⁻¹)必须与新 bundle 一致
159
+ await patchStreamFn(oldFile, patchFile, restoredFile);
160
+ const restoredRaw: Buffer = fs.readFileSync(restoredFile);
161
+ const restored = pair
162
+ ? transformHbcWithLayout(restoredRaw, pair.layout, true)
163
+ : restoredRaw;
164
+ if (!restored || Buffer.compare(restored, newSource) !== 0) {
165
+ throw new Error('stream diff round-trip verification failed');
166
+ }
167
+
168
+ return {
169
+ patchData: fs.readFileSync(patchFile),
170
+ ...(pair ? { hbcTransform: pair.meta } : {}),
171
+ };
172
+ } finally {
173
+ fs.rmSync(tempRoot, { recursive: true, force: true });
174
+ }
175
+ }
176
+
177
+ /**
178
+ * 生成 bundle patch:默认现状路径;开启 hbcTransform 时对 Hermes bundle
179
+ * 做双候选择优(baseline vs 变换域 + 元数据开销),且变换候选必须通过
180
+ * 本地 round-trip 还原自检才会被采用——收益永不为负,未知格式天然安全。
181
+ */
182
+ async function buildBundlePatch(
183
+ diffFn: Diff,
184
+ originSource: Buffer | undefined,
185
+ newSource: Buffer,
186
+ options: BundleDiffOptions,
187
+ ): Promise<{ patchData: Buffer; hbcTransform?: HbcTransformMeta }> {
188
+ if (
189
+ options.streamThresholdBytes &&
190
+ originSource &&
191
+ Math.max(originSource.length, newSource.length) >=
192
+ options.streamThresholdBytes
193
+ ) {
194
+ return buildStreamBundlePatch(originSource, newSource, options);
195
+ }
196
+
197
+ const baseline = await diffFn(originSource, newSource);
198
+ if (
199
+ !options.hbcTransform ||
200
+ !options.patchFn ||
201
+ !originSource ||
202
+ baseline.length <
203
+ (options.minBaselineBytes ?? HBC_TRANSFORM_MIN_BASELINE_BYTES)
204
+ ) {
205
+ return { patchData: baseline };
206
+ }
207
+
208
+ const pair = tryTransformPair(originSource, newSource);
209
+ if (!pair) {
210
+ return { patchData: baseline };
211
+ }
212
+
213
+ try {
214
+ const candidate = await diffFn(pair.tOld, pair.tNew);
215
+ const metaOverhead = Buffer.byteLength(JSON.stringify(pair.meta));
216
+ if (candidate.length + metaOverhead >= baseline.length) {
217
+ return { patchData: baseline };
218
+ }
219
+
220
+ const patched = options.patchFn(pair.tOld, candidate);
221
+ const restored = transformHbcWithLayout(patched, pair.layout, true);
222
+ if (!restored || Buffer.compare(restored, newSource) !== 0) {
223
+ console.warn(t('hbcTransformRoundTripFailed'));
224
+ return { patchData: baseline };
225
+ }
226
+ return { patchData: candidate, hbcTransform: pair.meta };
227
+ } catch {
228
+ return { patchData: baseline };
229
+ }
230
+ }
231
+
80
232
  async function addBundlePatch(
81
233
  zipfile: YazlZipFile,
82
234
  entry: Entry,
83
235
  nextZipfile: YauzlZipFile,
84
236
  diffFn: Diff,
85
237
  originSource: Buffer | undefined,
238
+ bundleOptions: BundleDiffOptions,
239
+ hbcTransformMetas: Record<string, HbcTransformMeta>,
86
240
  ) {
87
241
  const newSource = await readEntry(entry, nextZipfile);
88
- zipfile.addBuffer(
89
- await diffFn(originSource, newSource),
90
- `${entry.fileName}.patch`,
91
- zipOptionsForPatchEntry(),
242
+ const { patchData, hbcTransform } = await buildBundlePatch(
243
+ diffFn,
244
+ originSource,
245
+ newSource,
246
+ bundleOptions,
92
247
  );
248
+ const patchEntryName = `${entry.fileName}.patch`;
249
+ if (hbcTransform) {
250
+ hbcTransformMetas[patchEntryName] = hbcTransform;
251
+ }
252
+ zipfile.addBuffer(patchData, patchEntryName, zipOptionsForPatchEntry());
93
253
  }
94
254
 
95
255
  async function addFileFromZipEntry(
@@ -143,6 +303,7 @@ async function diffFromPPK(
143
303
  next: string,
144
304
  output: string,
145
305
  diffFn: Diff,
306
+ bundleOptions: BundleDiffOptions = {},
146
307
  ) {
147
308
  const originEntries: EntryMap = {};
148
309
  const originMap: CrcMap = {};
@@ -186,6 +347,7 @@ async function diffFromPPK(
186
347
  }
187
348
 
188
349
  const newEntries: EntryMap = {};
350
+ const hbcTransformMetas: Record<string, HbcTransformMeta> = {};
189
351
 
190
352
  await enumZipEntries(next, async (entry, nextZipfile) => {
191
353
  newEntries[entry.fileName] = entry;
@@ -196,7 +358,15 @@ async function diffFromPPK(
196
358
  addEntry(entry.fileName);
197
359
  }
198
360
  } else if (isPPKBundleFileName(entry.fileName)) {
199
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
361
+ await addBundlePatch(
362
+ zipfile,
363
+ entry,
364
+ nextZipfile,
365
+ diffFn,
366
+ originSource,
367
+ bundleOptions,
368
+ hbcTransformMetas,
369
+ );
200
370
  } else {
201
371
  // If same file.
202
372
  const originEntry = originEntries[entry.fileName];
@@ -235,7 +405,13 @@ async function diffFromPPK(
235
405
  }
236
406
  }
237
407
 
238
- await finishDiffZip(zipfile, writePromise, { copies, deletes });
408
+ await finishDiffZip(zipfile, writePromise, {
409
+ copies,
410
+ deletes,
411
+ ...(Object.keys(hbcTransformMetas).length
412
+ ? { hbcTransform: hbcTransformMetas }
413
+ : {}),
414
+ });
239
415
  }
240
416
 
241
417
  async function diffFromPackage(
@@ -245,6 +421,7 @@ async function diffFromPackage(
245
421
  diffFn: Diff,
246
422
  originBundleName: string,
247
423
  transformPackagePath: (v: string) => string | undefined = (v: string) => v,
424
+ bundleOptions: BundleDiffOptions = {},
248
425
  ) {
249
426
  const originEntries: Record<string, number> = {};
250
427
  const originMap: CrcMap = {};
@@ -284,13 +461,22 @@ async function diffFromPackage(
284
461
  const copies: CopyMap = {};
285
462
 
286
463
  const { zipfile, writePromise } = createOutputZip(output);
464
+ const hbcTransformMetas: Record<string, HbcTransformMeta> = {};
287
465
 
288
466
  await enumZipEntries(next, async (entry, nextZipfile) => {
289
467
  if (/\/$/.test(entry.fileName)) {
290
468
  // Directory
291
469
  zipfile.addEmptyDirectory(entry.fileName);
292
470
  } else if (isPPKBundleFileName(entry.fileName)) {
293
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
471
+ await addBundlePatch(
472
+ zipfile,
473
+ entry,
474
+ nextZipfile,
475
+ diffFn,
476
+ originSource,
477
+ bundleOptions,
478
+ hbcTransformMetas,
479
+ );
294
480
  } else {
295
481
  // If same file.
296
482
  if (originEntries[entry.fileName] === entry.crc32) {
@@ -312,12 +498,25 @@ async function diffFromPackage(
312
498
  }
313
499
  });
314
500
 
315
- await finishDiffZip(zipfile, writePromise, { copies, copiesCrc });
501
+ await finishDiffZip(zipfile, writePromise, {
502
+ copies,
503
+ copiesCrc,
504
+ ...(Object.keys(hbcTransformMetas).length
505
+ ? { hbcTransform: hbcTransformMetas }
506
+ : {}),
507
+ });
316
508
  }
317
509
 
318
510
  type DiffCommandOptions = {
319
511
  customDiff?: Diff;
512
+ customPatch?: Patch;
513
+ customDiffStream?: DiffStream;
514
+ customPatchStream?: PatchStream;
515
+ customDiffSingleStream?: DiffStream;
516
+ customPatchSingleStream?: PatchStream;
320
517
  customHdiffModule?: HdiffModule;
518
+ hbcTransform?: boolean;
519
+ bundleStreamThreshold?: number;
321
520
  [key: string]: any;
322
521
  };
323
522
 
@@ -333,6 +532,71 @@ function resolveDiffImplementation(options: DiffCommandOptions): Diff {
333
532
  return hdiffModule.diff;
334
533
  }
335
534
 
535
+ // round-trip 自检需要 patch 能力;拿不到时禁用变换(安全优先),不报错
536
+ function resolvePatchImplementation(
537
+ options: DiffCommandOptions,
538
+ ): Patch | undefined {
539
+ if (options.customPatch) {
540
+ return options.customPatch;
541
+ }
542
+ const hdiffModule = options.customHdiffModule ?? hdiff;
543
+ return hdiffModule?.patch;
544
+ }
545
+
546
+ function resolveBundleDiffOptions(
547
+ options: DiffCommandOptions,
548
+ ): BundleDiffOptions {
549
+ const hdiffModule = options.customHdiffModule ?? hdiff;
550
+ const streamOptions: Pick<
551
+ BundleDiffOptions,
552
+ | 'streamThresholdBytes'
553
+ | 'diffStreamFn'
554
+ | 'patchStreamFn'
555
+ | 'streamEmitsSingleFormat'
556
+ > = {};
557
+ if (
558
+ typeof options.bundleStreamThreshold === 'number' &&
559
+ options.bundleStreamThreshold > 0
560
+ ) {
561
+ const singleDiffStreamFn =
562
+ options.customDiffSingleStream ?? hdiffModule?.diffSingleStream;
563
+ const singlePatchStreamFn =
564
+ options.customPatchSingleStream ?? hdiffModule?.patchSingleStream;
565
+ const diffStreamFn = options.customDiffStream ?? hdiffModule?.diffStream;
566
+ const patchStreamFn = options.customPatchStream ?? hdiffModule?.patchStream;
567
+ if (singleDiffStreamFn && singlePatchStreamFn) {
568
+ streamOptions.streamThresholdBytes = options.bundleStreamThreshold;
569
+ streamOptions.diffStreamFn = singleDiffStreamFn;
570
+ streamOptions.patchStreamFn = singlePatchStreamFn;
571
+ streamOptions.streamEmitsSingleFormat = true;
572
+ } else if (diffStreamFn && patchStreamFn) {
573
+ streamOptions.streamThresholdBytes = options.bundleStreamThreshold;
574
+ streamOptions.diffStreamFn = diffStreamFn;
575
+ streamOptions.patchStreamFn = patchStreamFn;
576
+ streamOptions.streamEmitsSingleFormat = false;
577
+ } else {
578
+ console.warn(t('bundleStreamNeedsHdiffpatch'));
579
+ }
580
+ }
581
+
582
+ if (!options.hbcTransform) {
583
+ return { ...streamOptions };
584
+ }
585
+ const patchFn = resolvePatchImplementation(options);
586
+ if (!patchFn) {
587
+ console.warn(t('hbcTransformNeedsPatch'));
588
+ return { ...streamOptions };
589
+ }
590
+ return {
591
+ hbcTransform: true,
592
+ patchFn,
593
+ ...streamOptions,
594
+ ...(typeof options.hbcTransformMinBaseline === 'number'
595
+ ? { minBaselineBytes: options.hbcTransformMinBaseline }
596
+ : {}),
597
+ };
598
+ }
599
+
336
600
  function diffArgsCheck(
337
601
  args: string[],
338
602
  options: DiffCommandOptions,
@@ -374,9 +638,12 @@ const createDiffCommand =
374
638
  options as DiffCommandOptions,
375
639
  diffFnName,
376
640
  );
641
+ const bundleOptions = resolveBundleDiffOptions(
642
+ options as DiffCommandOptions,
643
+ );
377
644
 
378
645
  if (target.kind === 'ppk') {
379
- await diffFromPPK(origin, next, realOutput, diffFn);
646
+ await diffFromPPK(origin, next, realOutput, diffFn, bundleOptions);
380
647
  } else {
381
648
  await diffFromPackage(
382
649
  origin,
@@ -385,6 +652,7 @@ const createDiffCommand =
385
652
  diffFn,
386
653
  target.originBundleName,
387
654
  target.transformPackagePath,
655
+ bundleOptions,
388
656
  );
389
657
  }
390
658
 
package/src/locales/en.ts CHANGED
@@ -183,6 +183,12 @@ This can reduce the risk of inconsistent dependencies and supply chain attacks.
183
183
  diffPackageGenerated: '{{- output}} generated.',
184
184
  nodeHdiffpatchRequired:
185
185
  'This function needs "node-hdiffpatch". Please run "{{scriptName}} install node-hdiffpatch" to install',
186
+ hbcTransformRoundTripFailed:
187
+ 'HBC transform round-trip verification failed; falling back to plain diff.',
188
+ hbcTransformNeedsPatch:
189
+ 'hbcTransform requires patch support from node-hdiffpatch; option ignored.',
190
+ bundleStreamNeedsHdiffpatch:
191
+ 'bundleStreamThreshold requires diffStream/patchStream from node-hdiffpatch; option ignored.',
186
192
  apkExtracted: 'APK extracted to {{output}}',
187
193
  composeSourceMapsNotFound:
188
194
  'Cannot find react-native/scripts/compose-source-maps.js, skipping hermes sourcemap composing. The uploaded sourcemap may not match the compiled bundle.',
package/src/locales/zh.ts CHANGED
@@ -168,6 +168,12 @@ export default {
168
168
  diffPackageGenerated: '{{- output}} 已生成。',
169
169
  nodeHdiffpatchRequired:
170
170
  '此功能需要 "node-hdiffpatch"。请运行 "{{scriptName}} install node-hdiffpatch" 来安装',
171
+ hbcTransformRoundTripFailed:
172
+ 'HBC 变换 round-trip 自检未通过,已回退普通 diff。',
173
+ hbcTransformNeedsPatch:
174
+ 'hbcTransform 需要 node-hdiffpatch 的 patch 能力,选项已忽略。',
175
+ bundleStreamNeedsHdiffpatch:
176
+ 'bundleStreamThreshold 需要 node-hdiffpatch 的 diffStream/patchStream 能力,选项已忽略。',
171
177
  apkExtracted: 'APK 已提取到 {{output}}',
172
178
  composeSourceMapsNotFound:
173
179
  '找不到 react-native/scripts/compose-source-maps.js,跳过 hermes sourcemap 合成。上传的 sourcemap 可能与编译后的 bundle 不匹配。',