react-native-update-cli 2.17.0 → 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/lib/diff.js CHANGED
@@ -21,6 +21,7 @@ _export(exports, {
21
21
  });
22
22
  const _fsextra = /*#__PURE__*/ _interop_require_wildcard(require("fs-extra"));
23
23
  const _globaldirs = require("global-dirs");
24
+ const _os = /*#__PURE__*/ _interop_require_default(require("os"));
24
25
  const _path = /*#__PURE__*/ _interop_require_default(require("path"));
25
26
  const _yazl = require("yazl");
26
27
  const _utils = require("./utils");
@@ -113,12 +114,56 @@ function createOutputZip(output) {
113
114
  // baseline patch 小于该值时跳过变换尝试:节省一次 diff 的 CPU,
114
115
  // 且此时变换收益上限(~25%)也抵不过元数据开销
115
116
  const HBC_TRANSFORM_MIN_BASELINE_BYTES = 64 * 1024;
117
+ /**
118
+ * 大 bundle 流式路径:可选 HBC 变换(在 buffer 上做,写临时文件)后
119
+ * diffStream 生成 HDIFF13,并以 patchStream + T⁻¹ 做 round-trip 自检。
120
+ * 自检失败直接抛错(此路径没有 baseline 可回退——内存 diff 正是要避免的),
121
+ * 由上层按任务失败重试/告警处理。
122
+ */ async function buildStreamBundlePatch(originSource, newSource, options) {
123
+ const diffStreamFn = options.diffStreamFn;
124
+ const patchStreamFn = options.patchStreamFn;
125
+ if (!diffStreamFn || !patchStreamFn) {
126
+ throw new Error('stream diff requires diffStream/patchStream from node-hdiffpatch');
127
+ }
128
+ const pair = options.hbcTransform ? (0, _hbcTransform.tryTransformPair)(originSource, newSource) : null;
129
+ const tempRoot = _fsextra.mkdtempSync(_path.default.join(_os.default.tmpdir(), 'rnu-stream-diff-'));
130
+ try {
131
+ const oldFile = _path.default.join(tempRoot, 'old.bin');
132
+ const newFile = _path.default.join(tempRoot, 'new.bin');
133
+ const patchFile = _path.default.join(tempRoot, 'patch.bin');
134
+ const restoredFile = _path.default.join(tempRoot, 'restored.bin');
135
+ _fsextra.writeFileSync(oldFile, pair ? pair.tOld : originSource);
136
+ _fsextra.writeFileSync(newFile, pair ? pair.tNew : newSource);
137
+ await diffStreamFn(oldFile, newFile, patchFile);
138
+ // round-trip 自检:patchStream 还原(变换域则再做 T⁻¹)必须与新 bundle 一致
139
+ await patchStreamFn(oldFile, patchFile, restoredFile);
140
+ const restoredRaw = _fsextra.readFileSync(restoredFile);
141
+ const restored = pair ? (0, _hbcTransform.transformHbcWithLayout)(restoredRaw, pair.layout, true) : restoredRaw;
142
+ if (!restored || Buffer.compare(restored, newSource) !== 0) {
143
+ throw new Error('stream diff round-trip verification failed');
144
+ }
145
+ return {
146
+ patchData: _fsextra.readFileSync(patchFile),
147
+ ...pair ? {
148
+ hbcTransform: pair.meta
149
+ } : {}
150
+ };
151
+ } finally{
152
+ _fsextra.rmSync(tempRoot, {
153
+ recursive: true,
154
+ force: true
155
+ });
156
+ }
157
+ }
116
158
  /**
117
159
  * 生成 bundle patch:默认现状路径;开启 hbcTransform 时对 Hermes bundle
118
160
  * 做双候选择优(baseline vs 变换域 + 元数据开销),且变换候选必须通过
119
161
  * 本地 round-trip 还原自检才会被采用——收益永不为负,未知格式天然安全。
120
162
  */ async function buildBundlePatch(diffFn, originSource, newSource, options) {
121
163
  var _options_minBaselineBytes;
164
+ if (options.streamThresholdBytes && originSource && Math.max(originSource.length, newSource.length) >= options.streamThresholdBytes) {
165
+ return buildStreamBundlePatch(originSource, newSource, options);
166
+ }
122
167
  const baseline = await diffFn(originSource, newSource);
123
168
  if (!options.hbcTransform || !options.patchFn || !originSource || baseline.length < ((_options_minBaselineBytes = options.minBaselineBytes) != null ? _options_minBaselineBytes : HBC_TRANSFORM_MIN_BASELINE_BYTES)) {
124
169
  return {
@@ -364,17 +409,45 @@ function resolvePatchImplementation(options) {
364
409
  return hdiffModule == null ? void 0 : hdiffModule.patch;
365
410
  }
366
411
  function resolveBundleDiffOptions(options) {
412
+ var _options_customHdiffModule;
413
+ const hdiffModule = (_options_customHdiffModule = options.customHdiffModule) != null ? _options_customHdiffModule : hdiff;
414
+ const streamOptions = {};
415
+ if (typeof options.bundleStreamThreshold === 'number' && options.bundleStreamThreshold > 0) {
416
+ var _options_customDiffSingleStream, _options_customPatchSingleStream, _options_customDiffStream, _options_customPatchStream;
417
+ const singleDiffStreamFn = (_options_customDiffSingleStream = options.customDiffSingleStream) != null ? _options_customDiffSingleStream : hdiffModule == null ? void 0 : hdiffModule.diffSingleStream;
418
+ const singlePatchStreamFn = (_options_customPatchSingleStream = options.customPatchSingleStream) != null ? _options_customPatchSingleStream : hdiffModule == null ? void 0 : hdiffModule.patchSingleStream;
419
+ const diffStreamFn = (_options_customDiffStream = options.customDiffStream) != null ? _options_customDiffStream : hdiffModule == null ? void 0 : hdiffModule.diffStream;
420
+ const patchStreamFn = (_options_customPatchStream = options.customPatchStream) != null ? _options_customPatchStream : hdiffModule == null ? void 0 : hdiffModule.patchStream;
421
+ if (singleDiffStreamFn && singlePatchStreamFn) {
422
+ streamOptions.streamThresholdBytes = options.bundleStreamThreshold;
423
+ streamOptions.diffStreamFn = singleDiffStreamFn;
424
+ streamOptions.patchStreamFn = singlePatchStreamFn;
425
+ streamOptions.streamEmitsSingleFormat = true;
426
+ } else if (diffStreamFn && patchStreamFn) {
427
+ streamOptions.streamThresholdBytes = options.bundleStreamThreshold;
428
+ streamOptions.diffStreamFn = diffStreamFn;
429
+ streamOptions.patchStreamFn = patchStreamFn;
430
+ streamOptions.streamEmitsSingleFormat = false;
431
+ } else {
432
+ console.warn((0, _i18n.t)('bundleStreamNeedsHdiffpatch'));
433
+ }
434
+ }
367
435
  if (!options.hbcTransform) {
368
- return {};
436
+ return {
437
+ ...streamOptions
438
+ };
369
439
  }
370
440
  const patchFn = resolvePatchImplementation(options);
371
441
  if (!patchFn) {
372
442
  console.warn((0, _i18n.t)('hbcTransformNeedsPatch'));
373
- return {};
443
+ return {
444
+ ...streamOptions
445
+ };
374
446
  }
375
447
  return {
376
448
  hbcTransform: true,
377
449
  patchFn,
450
+ ...streamOptions,
378
451
  ...typeof options.hbcTransformMinBaseline === 'number' ? {
379
452
  minBaselineBytes: options.hbcTransformMinBaseline
380
453
  } : {}
@@ -138,6 +138,7 @@ declare const _default: {
138
138
  nodeHdiffpatchRequired: string;
139
139
  hbcTransformRoundTripFailed: string;
140
140
  hbcTransformNeedsPatch: string;
141
+ bundleStreamNeedsHdiffpatch: string;
141
142
  apkExtracted: string;
142
143
  composeSourceMapsNotFound: string;
143
144
  installPackageRequired: string;
package/lib/locales/en.js CHANGED
@@ -154,6 +154,7 @@ This can reduce the risk of inconsistent dependencies and supply chain attacks.
154
154
  nodeHdiffpatchRequired: 'This function needs "node-hdiffpatch". Please run "{{scriptName}} install node-hdiffpatch" to install',
155
155
  hbcTransformRoundTripFailed: 'HBC transform round-trip verification failed; falling back to plain diff.',
156
156
  hbcTransformNeedsPatch: 'hbcTransform requires patch support from node-hdiffpatch; option ignored.',
157
+ bundleStreamNeedsHdiffpatch: 'bundleStreamThreshold requires diffStream/patchStream from node-hdiffpatch; option ignored.',
157
158
  apkExtracted: 'APK extracted to {{output}}',
158
159
  composeSourceMapsNotFound: 'Cannot find react-native/scripts/compose-source-maps.js, skipping hermes sourcemap composing. The uploaded sourcemap may not match the compiled bundle.',
159
160
  installPackageRequired: 'Please specify the package to install, e.g. "{{scriptName}} install node-hdiffpatch"',
@@ -137,6 +137,7 @@ declare const _default: {
137
137
  nodeHdiffpatchRequired: string;
138
138
  hbcTransformRoundTripFailed: string;
139
139
  hbcTransformNeedsPatch: string;
140
+ bundleStreamNeedsHdiffpatch: string;
140
141
  apkExtracted: string;
141
142
  composeSourceMapsNotFound: string;
142
143
  installPackageRequired: string;
package/lib/locales/zh.js CHANGED
@@ -153,6 +153,7 @@ const _default = {
153
153
  nodeHdiffpatchRequired: '此功能需要 "node-hdiffpatch"。请运行 "{{scriptName}} install node-hdiffpatch" 来安装',
154
154
  hbcTransformRoundTripFailed: 'HBC 变换 round-trip 自检未通过,已回退普通 diff。',
155
155
  hbcTransformNeedsPatch: 'hbcTransform 需要 node-hdiffpatch 的 patch 能力,选项已忽略。',
156
+ bundleStreamNeedsHdiffpatch: 'bundleStreamThreshold 需要 node-hdiffpatch 的 diffStream/patchStream 能力,选项已忽略。',
156
157
  apkExtracted: 'APK 已提取到 {{output}}',
157
158
  composeSourceMapsNotFound: '找不到 react-native/scripts/compose-source-maps.js,跳过 hermes sourcemap 合成。上传的 sourcemap 可能与编译后的 bundle 不匹配。',
158
159
  installPackageRequired: '请指定要安装的包,例如 "{{scriptName}} install node-hdiffpatch"',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update-cli",
3
- "version": "2.17.0",
3
+ "version": "2.18.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",
@@ -102,6 +102,7 @@
102
102
  "@types/semver": "^7.7.1",
103
103
  "@types/yauzl": "^2.10.3",
104
104
  "@types/yazl": "^2.4.6",
105
+ "node-hdiffpatch": "^2.0.3",
105
106
  "typescript": "^6.0.2"
106
107
  },
107
108
  "trustedDependencies": [
package/src/diff.ts CHANGED
@@ -1,5 +1,6 @@
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';
@@ -29,9 +30,23 @@ type Diff = (
29
30
  newSource?: Buffer,
30
31
  ) => Buffer | Promise<Buffer>;
31
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>;
32
43
  type HdiffModule = {
33
44
  diff?: Diff;
34
45
  patch?: Patch;
46
+ diffStream?: DiffStream;
47
+ patchStream?: PatchStream;
48
+ diffSingleStream?: DiffStream;
49
+ patchSingleStream?: PatchStream;
35
50
  };
36
51
  type EntryMap = Record<string, { crc32: number; fileName: string }>;
37
52
  type CrcMap = Record<number, string>;
@@ -93,8 +108,72 @@ type BundleDiffOptions = {
93
108
  patchFn?: Patch;
94
109
  /** 测试用:覆写跳过变换尝试的 baseline 大小下限 */
95
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;
96
123
  };
97
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
+
98
177
  /**
99
178
  * 生成 bundle patch:默认现状路径;开启 hbcTransform 时对 Hermes bundle
100
179
  * 做双候选择优(baseline vs 变换域 + 元数据开销),且变换候选必须通过
@@ -106,6 +185,15 @@ async function buildBundlePatch(
106
185
  newSource: Buffer,
107
186
  options: BundleDiffOptions,
108
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
+
109
197
  const baseline = await diffFn(originSource, newSource);
110
198
  if (
111
199
  !options.hbcTransform ||
@@ -422,8 +510,13 @@ async function diffFromPackage(
422
510
  type DiffCommandOptions = {
423
511
  customDiff?: Diff;
424
512
  customPatch?: Patch;
513
+ customDiffStream?: DiffStream;
514
+ customPatchStream?: PatchStream;
515
+ customDiffSingleStream?: DiffStream;
516
+ customPatchSingleStream?: PatchStream;
425
517
  customHdiffModule?: HdiffModule;
426
518
  hbcTransform?: boolean;
519
+ bundleStreamThreshold?: number;
427
520
  [key: string]: any;
428
521
  };
429
522
 
@@ -453,17 +546,51 @@ function resolvePatchImplementation(
453
546
  function resolveBundleDiffOptions(
454
547
  options: DiffCommandOptions,
455
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
+
456
582
  if (!options.hbcTransform) {
457
- return {};
583
+ return { ...streamOptions };
458
584
  }
459
585
  const patchFn = resolvePatchImplementation(options);
460
586
  if (!patchFn) {
461
587
  console.warn(t('hbcTransformNeedsPatch'));
462
- return {};
588
+ return { ...streamOptions };
463
589
  }
464
590
  return {
465
591
  hbcTransform: true,
466
592
  patchFn,
593
+ ...streamOptions,
467
594
  ...(typeof options.hbcTransformMinBaseline === 'number'
468
595
  ? { minBaselineBytes: options.hbcTransformMinBaseline }
469
596
  : {}),
package/src/locales/en.ts CHANGED
@@ -187,6 +187,8 @@ This can reduce the risk of inconsistent dependencies and supply chain attacks.
187
187
  'HBC transform round-trip verification failed; falling back to plain diff.',
188
188
  hbcTransformNeedsPatch:
189
189
  'hbcTransform requires patch support from node-hdiffpatch; option ignored.',
190
+ bundleStreamNeedsHdiffpatch:
191
+ 'bundleStreamThreshold requires diffStream/patchStream from node-hdiffpatch; option ignored.',
190
192
  apkExtracted: 'APK extracted to {{output}}',
191
193
  composeSourceMapsNotFound:
192
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
@@ -172,6 +172,8 @@ export default {
172
172
  'HBC 变换 round-trip 自检未通过,已回退普通 diff。',
173
173
  hbcTransformNeedsPatch:
174
174
  'hbcTransform 需要 node-hdiffpatch 的 patch 能力,选项已忽略。',
175
+ bundleStreamNeedsHdiffpatch:
176
+ 'bundleStreamThreshold 需要 node-hdiffpatch 的 diffStream/patchStream 能力,选项已忽略。',
175
177
  apkExtracted: 'APK 已提取到 {{output}}',
176
178
  composeSourceMapsNotFound:
177
179
  '找不到 react-native/scripts/compose-source-maps.js,跳过 hermes sourcemap 合成。上传的 sourcemap 可能与编译后的 bundle 不匹配。',