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/cli.json CHANGED
@@ -319,6 +319,9 @@
319
319
  "output": {
320
320
  "default": "${tempDir}/output/hdiff",
321
321
  "hasValue": true
322
+ },
323
+ "hbcTransform": {
324
+ "default": false
322
325
  }
323
326
  }
324
327
  },
package/lib/diff.js CHANGED
@@ -21,10 +21,12 @@ _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");
27
28
  const _constants = require("./utils/constants");
29
+ const _hbcTransform = require("./utils/hbcTransform");
28
30
  const _i18n = require("./utils/i18n");
29
31
  const _zipentries = require("./utils/zip-entries");
30
32
  const _zipoptions = require("./utils/zip-options");
@@ -109,9 +111,105 @@ function createOutputZip(output) {
109
111
  writePromise
110
112
  };
111
113
  }
112
- async function addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource) {
114
+ // baseline patch 小于该值时跳过变换尝试:节省一次 diff 的 CPU,
115
+ // 且此时变换收益上限(~25%)也抵不过元数据开销
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
+ }
158
+ /**
159
+ * 生成 bundle patch:默认现状路径;开启 hbcTransform 时对 Hermes bundle
160
+ * 做双候选择优(baseline vs 变换域 + 元数据开销),且变换候选必须通过
161
+ * 本地 round-trip 还原自检才会被采用——收益永不为负,未知格式天然安全。
162
+ */ async function buildBundlePatch(diffFn, originSource, newSource, options) {
163
+ var _options_minBaselineBytes;
164
+ if (options.streamThresholdBytes && originSource && Math.max(originSource.length, newSource.length) >= options.streamThresholdBytes) {
165
+ return buildStreamBundlePatch(originSource, newSource, options);
166
+ }
167
+ const baseline = await diffFn(originSource, newSource);
168
+ if (!options.hbcTransform || !options.patchFn || !originSource || baseline.length < ((_options_minBaselineBytes = options.minBaselineBytes) != null ? _options_minBaselineBytes : HBC_TRANSFORM_MIN_BASELINE_BYTES)) {
169
+ return {
170
+ patchData: baseline
171
+ };
172
+ }
173
+ const pair = (0, _hbcTransform.tryTransformPair)(originSource, newSource);
174
+ if (!pair) {
175
+ return {
176
+ patchData: baseline
177
+ };
178
+ }
179
+ try {
180
+ const candidate = await diffFn(pair.tOld, pair.tNew);
181
+ const metaOverhead = Buffer.byteLength(JSON.stringify(pair.meta));
182
+ if (candidate.length + metaOverhead >= baseline.length) {
183
+ return {
184
+ patchData: baseline
185
+ };
186
+ }
187
+ const patched = options.patchFn(pair.tOld, candidate);
188
+ const restored = (0, _hbcTransform.transformHbcWithLayout)(patched, pair.layout, true);
189
+ if (!restored || Buffer.compare(restored, newSource) !== 0) {
190
+ console.warn((0, _i18n.t)('hbcTransformRoundTripFailed'));
191
+ return {
192
+ patchData: baseline
193
+ };
194
+ }
195
+ return {
196
+ patchData: candidate,
197
+ hbcTransform: pair.meta
198
+ };
199
+ } catch (unused) {
200
+ return {
201
+ patchData: baseline
202
+ };
203
+ }
204
+ }
205
+ async function addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas) {
113
206
  const newSource = await (0, _zipentries.readEntry)(entry, nextZipfile);
114
- zipfile.addBuffer(await diffFn(originSource, newSource), `${entry.fileName}.patch`, (0, _zipoptions.zipOptionsForPatchEntry)());
207
+ const { patchData, hbcTransform } = await buildBundlePatch(diffFn, originSource, newSource, bundleOptions);
208
+ const patchEntryName = `${entry.fileName}.patch`;
209
+ if (hbcTransform) {
210
+ hbcTransformMetas[patchEntryName] = hbcTransform;
211
+ }
212
+ zipfile.addBuffer(patchData, patchEntryName, (0, _zipoptions.zipOptionsForPatchEntry)());
115
213
  }
116
214
  async function addFileFromZipEntry(zipfile, entry, nextZipfile) {
117
215
  const entryPrefix = await (0, _zipentries.readEntryPrefix)(entry, nextZipfile, _zipoptions.ZIP_ENTRY_SNIFF_BYTES);
@@ -137,7 +235,7 @@ function finishDiffZip(zipfile, writePromise, manifest) {
137
235
  zipfile.end();
138
236
  return writePromise;
139
237
  }
140
- async function diffFromPPK(origin, next, output, diffFn) {
238
+ async function diffFromPPK(origin, next, output, diffFn, bundleOptions = {}) {
141
239
  const originEntries = {};
142
240
  const originMap = {};
143
241
  let originSource;
@@ -171,6 +269,7 @@ async function diffFromPPK(origin, next, output, diffFn) {
171
269
  zipfile.addEmptyDirectory(fn);
172
270
  }
173
271
  const newEntries = {};
272
+ const hbcTransformMetas = {};
174
273
  await (0, _zipentries.enumZipEntries)(next, async (entry, nextZipfile)=>{
175
274
  newEntries[entry.fileName] = entry;
176
275
  if (/\/$/.test(entry.fileName)) {
@@ -179,7 +278,7 @@ async function diffFromPPK(origin, next, output, diffFn) {
179
278
  addEntry(entry.fileName);
180
279
  }
181
280
  } else if ((0, _constants.isPPKBundleFileName)(entry.fileName)) {
182
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
281
+ await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas);
183
282
  } else {
184
283
  // If same file.
185
284
  const originEntry = originEntries[entry.fileName];
@@ -216,10 +315,13 @@ async function diffFromPPK(origin, next, output, diffFn) {
216
315
  }
217
316
  await finishDiffZip(zipfile, writePromise, {
218
317
  copies,
219
- deletes
318
+ deletes,
319
+ ...Object.keys(hbcTransformMetas).length ? {
320
+ hbcTransform: hbcTransformMetas
321
+ } : {}
220
322
  });
221
323
  }
222
- async function diffFromPackage(origin, next, output, diffFn, originBundleName, transformPackagePath = (v)=>v) {
324
+ async function diffFromPackage(origin, next, output, diffFn, originBundleName, transformPackagePath = (v)=>v, bundleOptions = {}) {
223
325
  const originEntries = {};
224
326
  const originMap = {};
225
327
  let originSource;
@@ -250,12 +352,13 @@ async function diffFromPackage(origin, next, output, diffFn, originBundleName, t
250
352
  }
251
353
  const copies = {};
252
354
  const { zipfile, writePromise } = createOutputZip(output);
355
+ const hbcTransformMetas = {};
253
356
  await (0, _zipentries.enumZipEntries)(next, async (entry, nextZipfile)=>{
254
357
  if (/\/$/.test(entry.fileName)) {
255
358
  // Directory
256
359
  zipfile.addEmptyDirectory(entry.fileName);
257
360
  } else if ((0, _constants.isPPKBundleFileName)(entry.fileName)) {
258
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
361
+ await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas);
259
362
  } else {
260
363
  // If same file.
261
364
  if (originEntries[entry.fileName] === entry.crc32) {
@@ -277,7 +380,10 @@ async function diffFromPackage(origin, next, output, diffFn, originBundleName, t
277
380
  });
278
381
  await finishDiffZip(zipfile, writePromise, {
279
382
  copies,
280
- copiesCrc
383
+ copiesCrc,
384
+ ...Object.keys(hbcTransformMetas).length ? {
385
+ hbcTransform: hbcTransformMetas
386
+ } : {}
281
387
  });
282
388
  }
283
389
  function resolveDiffImplementation(options) {
@@ -293,6 +399,60 @@ function resolveDiffImplementation(options) {
293
399
  }
294
400
  return hdiffModule.diff;
295
401
  }
402
+ // round-trip 自检需要 patch 能力;拿不到时禁用变换(安全优先),不报错
403
+ function resolvePatchImplementation(options) {
404
+ var _options_customHdiffModule;
405
+ if (options.customPatch) {
406
+ return options.customPatch;
407
+ }
408
+ const hdiffModule = (_options_customHdiffModule = options.customHdiffModule) != null ? _options_customHdiffModule : hdiff;
409
+ return hdiffModule == null ? void 0 : hdiffModule.patch;
410
+ }
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
+ }
435
+ if (!options.hbcTransform) {
436
+ return {
437
+ ...streamOptions
438
+ };
439
+ }
440
+ const patchFn = resolvePatchImplementation(options);
441
+ if (!patchFn) {
442
+ console.warn((0, _i18n.t)('hbcTransformNeedsPatch'));
443
+ return {
444
+ ...streamOptions
445
+ };
446
+ }
447
+ return {
448
+ hbcTransform: true,
449
+ patchFn,
450
+ ...streamOptions,
451
+ ...typeof options.hbcTransformMinBaseline === 'number' ? {
452
+ minBaselineBytes: options.hbcTransformMinBaseline
453
+ } : {}
454
+ };
455
+ }
296
456
  function diffArgsCheck(args, options, diffFnName) {
297
457
  const [origin, next] = args;
298
458
  if (!origin || !next) {
@@ -321,10 +481,11 @@ const transformIpaPackagePath = (v)=>{
321
481
  };
322
482
  const createDiffCommand = ({ diffFnName, target })=>async ({ args, options })=>{
323
483
  const { origin, next, realOutput, diffFn } = diffArgsCheck(args, options, diffFnName);
484
+ const bundleOptions = resolveBundleDiffOptions(options);
324
485
  if (target.kind === 'ppk') {
325
- await diffFromPPK(origin, next, realOutput, diffFn);
486
+ await diffFromPPK(origin, next, realOutput, diffFn, bundleOptions);
326
487
  } else {
327
- await diffFromPackage(origin, next, realOutput, diffFn, target.originBundleName, target.transformPackagePath);
488
+ await diffFromPackage(origin, next, realOutput, diffFn, target.originBundleName, target.transformPackagePath, bundleOptions);
328
489
  }
329
490
  console.log((0, _i18n.t)('diffPackageGenerated', {
330
491
  output: realOutput
@@ -136,6 +136,9 @@ declare const _default: {
136
136
  bundleFileNotFound: string;
137
137
  diffPackageGenerated: string;
138
138
  nodeHdiffpatchRequired: string;
139
+ hbcTransformRoundTripFailed: string;
140
+ hbcTransformNeedsPatch: string;
141
+ bundleStreamNeedsHdiffpatch: string;
139
142
  apkExtracted: string;
140
143
  composeSourceMapsNotFound: string;
141
144
  installPackageRequired: string;
package/lib/locales/en.js CHANGED
@@ -152,6 +152,9 @@ This can reduce the risk of inconsistent dependencies and supply chain attacks.
152
152
  bundleFileNotFound: 'Bundle file not found! Please use default bundle file name and path.',
153
153
  diffPackageGenerated: '{{- output}} generated.',
154
154
  nodeHdiffpatchRequired: 'This function needs "node-hdiffpatch". Please run "{{scriptName}} install node-hdiffpatch" to install',
155
+ hbcTransformRoundTripFailed: 'HBC transform round-trip verification failed; falling back to plain diff.',
156
+ hbcTransformNeedsPatch: 'hbcTransform requires patch support from node-hdiffpatch; option ignored.',
157
+ bundleStreamNeedsHdiffpatch: 'bundleStreamThreshold requires diffStream/patchStream from node-hdiffpatch; option ignored.',
155
158
  apkExtracted: 'APK extracted to {{output}}',
156
159
  composeSourceMapsNotFound: 'Cannot find react-native/scripts/compose-source-maps.js, skipping hermes sourcemap composing. The uploaded sourcemap may not match the compiled bundle.',
157
160
  installPackageRequired: 'Please specify the package to install, e.g. "{{scriptName}} install node-hdiffpatch"',
@@ -135,6 +135,9 @@ declare const _default: {
135
135
  bundleFileNotFound: string;
136
136
  diffPackageGenerated: string;
137
137
  nodeHdiffpatchRequired: string;
138
+ hbcTransformRoundTripFailed: string;
139
+ hbcTransformNeedsPatch: string;
140
+ bundleStreamNeedsHdiffpatch: string;
138
141
  apkExtracted: string;
139
142
  composeSourceMapsNotFound: string;
140
143
  installPackageRequired: string;
package/lib/locales/zh.js CHANGED
@@ -151,6 +151,9 @@ const _default = {
151
151
  bundleFileNotFound: '未找到 bundle 文件!请使用默认的 bundle 文件名和路径。',
152
152
  diffPackageGenerated: '{{- output}} 已生成。',
153
153
  nodeHdiffpatchRequired: '此功能需要 "node-hdiffpatch"。请运行 "{{scriptName}} install node-hdiffpatch" 来安装',
154
+ hbcTransformRoundTripFailed: 'HBC 变换 round-trip 自检未通过,已回退普通 diff。',
155
+ hbcTransformNeedsPatch: 'hbcTransform 需要 node-hdiffpatch 的 patch 能力,选项已忽略。',
156
+ bundleStreamNeedsHdiffpatch: 'bundleStreamThreshold 需要 node-hdiffpatch 的 diffStream/patchStream 能力,选项已忽略。',
154
157
  apkExtracted: 'APK 已提取到 {{output}}',
155
158
  composeSourceMapsNotFound: '找不到 react-native/scripts/compose-source-maps.js,跳过 hermes sourcemap 合成。上传的 sourcemap 可能与编译后的 bundle 不匹配。',
156
159
  installPackageRequired: '请指定要安装的包,例如 "{{scriptName}} install node-hdiffpatch"',
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Hermes 字节码(HBC)delta-friendly 可逆变换。
3
+ *
4
+ * 原理:HBC 中 functionHeaders / smallStringTable 等表的条目携带绝对偏移,
5
+ * 任意一处内容增删都会让其后所有条目的偏移整体 +N,精确匹配型 diff(hdiff)
6
+ * 无法复用这些被移位的条目。对这些偏移字段做前项差分(wrapping,模字段位宽)
7
+ * 后,整体移位在差分域退化为单点变化,diff 显著变小。
8
+ *
9
+ * 布局描述表是纯数据:解释器(本文件 + 客户端 C 版)不含任何版本分支。
10
+ * 新 Hermes 版本只需在 HBC_LAYOUTS 增加/扩展一个条目。CLI 生成 diff 时把
11
+ * 实际使用的描述表以 wire 格式写入 __diff.json,客户端按表执行 T⁻¹,
12
+ * 因此客户端无需跟进 HBC 版本。
13
+ *
14
+ * wrapping 差分与单调性无关、恒可逆:T⁻¹(T(x)) === x 对任意输入成立
15
+ * (只要两侧使用同一份描述表)。
16
+ */
17
+ /** 差分字段:条目内 byte 偏移处按小端读 u32,取 [bit, bit+bits) 位。 */
18
+ type DeltaField = {
19
+ byte: number;
20
+ bit: number;
21
+ bits: number;
22
+ };
23
+ type SectionDesc = {
24
+ name: string;
25
+ /** 段大小 = header[countField] × entrySize;字节段 entrySize 为 1 */
26
+ countField: string;
27
+ entrySize: number;
28
+ deltaFields?: DeltaField[];
29
+ };
30
+ export type HbcLayout = {
31
+ minVersion: number;
32
+ maxVersion: number;
33
+ /** 头部 u32 计数字段名序列,从文件偏移 32 开始连续排列 */
34
+ headerFields: string[];
35
+ /** 各段按文件顺序排列(其后是函数字节码区,以 debugInfoOffset 为界) */
36
+ sections: SectionDesc[];
37
+ };
38
+ /**
39
+ * 布局描述表(唯一需要跟随 Hermes 版本演进的数据)。
40
+ * 字段序照抄 hermes 源码 include/hermes/BCGen/HBC/BytecodeFileFormat.h。
41
+ * v97 为 Static Hermes 短期预览版本,生产占比为零,不支持(自动回退)。
42
+ */
43
+ export declare const HBC_LAYOUTS: HbcLayout[];
44
+ /**
45
+ * 随 patch 下发的 wire 格式(客户端 C 解释器的输入,全数字、无名字):
46
+ * counts: 头部 u32 计数字段个数(从偏移 32 起连续读取)
47
+ * sections: [countIndex, entrySize, [[byte, bit, bits], ...]][]
48
+ *
49
+ * 位置约定(C 端校验依赖,两端必须一致):
50
+ * counts 槽位 0 = fileLength,最后一个槽位 = debugInfoOffset。
51
+ */
52
+ export type HbcLayoutWire = {
53
+ counts: number;
54
+ sections: [number, number, [number, number, number][]][];
55
+ };
56
+ export declare function compileLayoutToWire(layout: HbcLayout): HbcLayoutWire;
57
+ export declare function getHbcVersion(buf: Buffer): number | null;
58
+ /** 同一版本号可能有多个候选布局(按声明顺序即优先级)。 */
59
+ export declare function findLayouts(version: number): HbcLayout[];
60
+ /**
61
+ * 对 HBC buffer 执行变换(inverse=false)或逆变换(inverse=true)。
62
+ * 同一版本号可能存在多个候选布局(如 v98 早/晚两种头部),按声明顺序
63
+ * 取第一个通过结构校验的。非 HBC / 无可用布局 → 返回 null。
64
+ * 注意:变换不改动头部计数,同一文件正/逆向解析选中的布局必然一致。
65
+ */
66
+ export declare function transformHbc(buf: Buffer, inverse?: boolean): Buffer | null;
67
+ /** 返回该 buffer 结构上匹配的布局(候选中第一个校验通过者)。 */
68
+ export declare function pickLayout(buf: Buffer): HbcLayout | null;
69
+ export declare function transformHbcWithLayout(buf: Buffer, layout: HbcLayout, inverse: boolean): Buffer | null;
70
+ export declare const HBC_TRANSFORM_VERSION = 1;
71
+ export type HbcTransformMeta = {
72
+ v: number;
73
+ hbcVersion: number;
74
+ layout: HbcLayoutWire;
75
+ };
76
+ /**
77
+ * 若 old/new 均为同版本且存在两侧都通过结构校验的候选布局,返回变换结果、
78
+ * 选中布局与写入 __diff.json 的元数据;否则返回 null。
79
+ */
80
+ export declare function tryTransformPair(oldBuf: Buffer, newBuf: Buffer): {
81
+ tOld: Buffer;
82
+ tNew: Buffer;
83
+ layout: HbcLayout;
84
+ meta: HbcTransformMeta;
85
+ } | null;
86
+ export {};