react-native-update-cli 2.16.1 → 2.17.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
@@ -25,6 +25,7 @@ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
25
25
  const _yazl = require("yazl");
26
26
  const _utils = require("./utils");
27
27
  const _constants = require("./utils/constants");
28
+ const _hbcTransform = require("./utils/hbcTransform");
28
29
  const _i18n = require("./utils/i18n");
29
30
  const _zipentries = require("./utils/zip-entries");
30
31
  const _zipoptions = require("./utils/zip-options");
@@ -109,9 +110,61 @@ function createOutputZip(output) {
109
110
  writePromise
110
111
  };
111
112
  }
112
- async function addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource) {
113
+ // baseline patch 小于该值时跳过变换尝试:节省一次 diff 的 CPU,
114
+ // 且此时变换收益上限(~25%)也抵不过元数据开销
115
+ const HBC_TRANSFORM_MIN_BASELINE_BYTES = 64 * 1024;
116
+ /**
117
+ * 生成 bundle patch:默认现状路径;开启 hbcTransform 时对 Hermes bundle
118
+ * 做双候选择优(baseline vs 变换域 + 元数据开销),且变换候选必须通过
119
+ * 本地 round-trip 还原自检才会被采用——收益永不为负,未知格式天然安全。
120
+ */ async function buildBundlePatch(diffFn, originSource, newSource, options) {
121
+ var _options_minBaselineBytes;
122
+ const baseline = await diffFn(originSource, newSource);
123
+ if (!options.hbcTransform || !options.patchFn || !originSource || baseline.length < ((_options_minBaselineBytes = options.minBaselineBytes) != null ? _options_minBaselineBytes : HBC_TRANSFORM_MIN_BASELINE_BYTES)) {
124
+ return {
125
+ patchData: baseline
126
+ };
127
+ }
128
+ const pair = (0, _hbcTransform.tryTransformPair)(originSource, newSource);
129
+ if (!pair) {
130
+ return {
131
+ patchData: baseline
132
+ };
133
+ }
134
+ try {
135
+ const candidate = await diffFn(pair.tOld, pair.tNew);
136
+ const metaOverhead = Buffer.byteLength(JSON.stringify(pair.meta));
137
+ if (candidate.length + metaOverhead >= baseline.length) {
138
+ return {
139
+ patchData: baseline
140
+ };
141
+ }
142
+ const patched = options.patchFn(pair.tOld, candidate);
143
+ const restored = (0, _hbcTransform.transformHbcWithLayout)(patched, pair.layout, true);
144
+ if (!restored || Buffer.compare(restored, newSource) !== 0) {
145
+ console.warn((0, _i18n.t)('hbcTransformRoundTripFailed'));
146
+ return {
147
+ patchData: baseline
148
+ };
149
+ }
150
+ return {
151
+ patchData: candidate,
152
+ hbcTransform: pair.meta
153
+ };
154
+ } catch (unused) {
155
+ return {
156
+ patchData: baseline
157
+ };
158
+ }
159
+ }
160
+ async function addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas) {
113
161
  const newSource = await (0, _zipentries.readEntry)(entry, nextZipfile);
114
- zipfile.addBuffer(await diffFn(originSource, newSource), `${entry.fileName}.patch`, (0, _zipoptions.zipOptionsForPatchEntry)());
162
+ const { patchData, hbcTransform } = await buildBundlePatch(diffFn, originSource, newSource, bundleOptions);
163
+ const patchEntryName = `${entry.fileName}.patch`;
164
+ if (hbcTransform) {
165
+ hbcTransformMetas[patchEntryName] = hbcTransform;
166
+ }
167
+ zipfile.addBuffer(patchData, patchEntryName, (0, _zipoptions.zipOptionsForPatchEntry)());
115
168
  }
116
169
  async function addFileFromZipEntry(zipfile, entry, nextZipfile) {
117
170
  const entryPrefix = await (0, _zipentries.readEntryPrefix)(entry, nextZipfile, _zipoptions.ZIP_ENTRY_SNIFF_BYTES);
@@ -137,7 +190,7 @@ function finishDiffZip(zipfile, writePromise, manifest) {
137
190
  zipfile.end();
138
191
  return writePromise;
139
192
  }
140
- async function diffFromPPK(origin, next, output, diffFn) {
193
+ async function diffFromPPK(origin, next, output, diffFn, bundleOptions = {}) {
141
194
  const originEntries = {};
142
195
  const originMap = {};
143
196
  let originSource;
@@ -171,6 +224,7 @@ async function diffFromPPK(origin, next, output, diffFn) {
171
224
  zipfile.addEmptyDirectory(fn);
172
225
  }
173
226
  const newEntries = {};
227
+ const hbcTransformMetas = {};
174
228
  await (0, _zipentries.enumZipEntries)(next, async (entry, nextZipfile)=>{
175
229
  newEntries[entry.fileName] = entry;
176
230
  if (/\/$/.test(entry.fileName)) {
@@ -179,7 +233,7 @@ async function diffFromPPK(origin, next, output, diffFn) {
179
233
  addEntry(entry.fileName);
180
234
  }
181
235
  } else if ((0, _constants.isPPKBundleFileName)(entry.fileName)) {
182
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
236
+ await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas);
183
237
  } else {
184
238
  // If same file.
185
239
  const originEntry = originEntries[entry.fileName];
@@ -216,10 +270,13 @@ async function diffFromPPK(origin, next, output, diffFn) {
216
270
  }
217
271
  await finishDiffZip(zipfile, writePromise, {
218
272
  copies,
219
- deletes
273
+ deletes,
274
+ ...Object.keys(hbcTransformMetas).length ? {
275
+ hbcTransform: hbcTransformMetas
276
+ } : {}
220
277
  });
221
278
  }
222
- async function diffFromPackage(origin, next, output, diffFn, originBundleName, transformPackagePath = (v)=>v) {
279
+ async function diffFromPackage(origin, next, output, diffFn, originBundleName, transformPackagePath = (v)=>v, bundleOptions = {}) {
223
280
  const originEntries = {};
224
281
  const originMap = {};
225
282
  let originSource;
@@ -250,12 +307,13 @@ async function diffFromPackage(origin, next, output, diffFn, originBundleName, t
250
307
  }
251
308
  const copies = {};
252
309
  const { zipfile, writePromise } = createOutputZip(output);
310
+ const hbcTransformMetas = {};
253
311
  await (0, _zipentries.enumZipEntries)(next, async (entry, nextZipfile)=>{
254
312
  if (/\/$/.test(entry.fileName)) {
255
313
  // Directory
256
314
  zipfile.addEmptyDirectory(entry.fileName);
257
315
  } else if ((0, _constants.isPPKBundleFileName)(entry.fileName)) {
258
- await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource);
316
+ await addBundlePatch(zipfile, entry, nextZipfile, diffFn, originSource, bundleOptions, hbcTransformMetas);
259
317
  } else {
260
318
  // If same file.
261
319
  if (originEntries[entry.fileName] === entry.crc32) {
@@ -277,7 +335,10 @@ async function diffFromPackage(origin, next, output, diffFn, originBundleName, t
277
335
  });
278
336
  await finishDiffZip(zipfile, writePromise, {
279
337
  copies,
280
- copiesCrc
338
+ copiesCrc,
339
+ ...Object.keys(hbcTransformMetas).length ? {
340
+ hbcTransform: hbcTransformMetas
341
+ } : {}
281
342
  });
282
343
  }
283
344
  function resolveDiffImplementation(options) {
@@ -293,6 +354,32 @@ function resolveDiffImplementation(options) {
293
354
  }
294
355
  return hdiffModule.diff;
295
356
  }
357
+ // round-trip 自检需要 patch 能力;拿不到时禁用变换(安全优先),不报错
358
+ function resolvePatchImplementation(options) {
359
+ var _options_customHdiffModule;
360
+ if (options.customPatch) {
361
+ return options.customPatch;
362
+ }
363
+ const hdiffModule = (_options_customHdiffModule = options.customHdiffModule) != null ? _options_customHdiffModule : hdiff;
364
+ return hdiffModule == null ? void 0 : hdiffModule.patch;
365
+ }
366
+ function resolveBundleDiffOptions(options) {
367
+ if (!options.hbcTransform) {
368
+ return {};
369
+ }
370
+ const patchFn = resolvePatchImplementation(options);
371
+ if (!patchFn) {
372
+ console.warn((0, _i18n.t)('hbcTransformNeedsPatch'));
373
+ return {};
374
+ }
375
+ return {
376
+ hbcTransform: true,
377
+ patchFn,
378
+ ...typeof options.hbcTransformMinBaseline === 'number' ? {
379
+ minBaselineBytes: options.hbcTransformMinBaseline
380
+ } : {}
381
+ };
382
+ }
296
383
  function diffArgsCheck(args, options, diffFnName) {
297
384
  const [origin, next] = args;
298
385
  if (!origin || !next) {
@@ -321,10 +408,11 @@ const transformIpaPackagePath = (v)=>{
321
408
  };
322
409
  const createDiffCommand = ({ diffFnName, target })=>async ({ args, options })=>{
323
410
  const { origin, next, realOutput, diffFn } = diffArgsCheck(args, options, diffFnName);
411
+ const bundleOptions = resolveBundleDiffOptions(options);
324
412
  if (target.kind === 'ppk') {
325
- await diffFromPPK(origin, next, realOutput, diffFn);
413
+ await diffFromPPK(origin, next, realOutput, diffFn, bundleOptions);
326
414
  } else {
327
- await diffFromPackage(origin, next, realOutput, diffFn, target.originBundleName, target.transformPackagePath);
415
+ await diffFromPackage(origin, next, realOutput, diffFn, target.originBundleName, target.transformPackagePath, bundleOptions);
328
416
  }
329
417
  console.log((0, _i18n.t)('diffPackageGenerated', {
330
418
  output: realOutput
@@ -136,6 +136,8 @@ declare const _default: {
136
136
  bundleFileNotFound: string;
137
137
  diffPackageGenerated: string;
138
138
  nodeHdiffpatchRequired: string;
139
+ hbcTransformRoundTripFailed: string;
140
+ hbcTransformNeedsPatch: string;
139
141
  apkExtracted: string;
140
142
  composeSourceMapsNotFound: string;
141
143
  installPackageRequired: string;
package/lib/locales/en.js CHANGED
@@ -152,6 +152,8 @@ 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.',
155
157
  apkExtracted: 'APK extracted to {{output}}',
156
158
  composeSourceMapsNotFound: 'Cannot find react-native/scripts/compose-source-maps.js, skipping hermes sourcemap composing. The uploaded sourcemap may not match the compiled bundle.',
157
159
  installPackageRequired: 'Please specify the package to install, e.g. "{{scriptName}} install node-hdiffpatch"',
@@ -135,6 +135,8 @@ declare const _default: {
135
135
  bundleFileNotFound: string;
136
136
  diffPackageGenerated: string;
137
137
  nodeHdiffpatchRequired: string;
138
+ hbcTransformRoundTripFailed: string;
139
+ hbcTransformNeedsPatch: string;
138
140
  apkExtracted: string;
139
141
  composeSourceMapsNotFound: string;
140
142
  installPackageRequired: string;
package/lib/locales/zh.js CHANGED
@@ -151,6 +151,8 @@ 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 能力,选项已忽略。',
154
156
  apkExtracted: 'APK 已提取到 {{output}}',
155
157
  composeSourceMapsNotFound: '找不到 react-native/scripts/compose-source-maps.js,跳过 hermes sourcemap 合成。上传的 sourcemap 可能与编译后的 bundle 不匹配。',
156
158
  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 {};