react-native-update-cli 2.16.0 → 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.
@@ -0,0 +1,460 @@
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
+
18
+ const HBC_MAGIC = Buffer.from([0xc6, 0x1f, 0xbc, 0x03, 0xc1, 0x03, 0x19, 0x1f]);
19
+ const HEADER_COUNTS_OFFSET = 32; // magic(8) + version(4) + sourceHash(20)
20
+ const HBC_HEADER_SIZE = 128;
21
+
22
+ /** 差分字段:条目内 byte 偏移处按小端读 u32,取 [bit, bit+bits) 位。 */
23
+ type DeltaField = { byte: number; bit: number; bits: number };
24
+
25
+ type SectionDesc = {
26
+ name: string;
27
+ /** 段大小 = header[countField] × entrySize;字节段 entrySize 为 1 */
28
+ countField: string;
29
+ entrySize: number;
30
+ deltaFields?: DeltaField[];
31
+ };
32
+
33
+ export type HbcLayout = {
34
+ minVersion: number;
35
+ maxVersion: number;
36
+ /** 头部 u32 计数字段名序列,从文件偏移 32 开始连续排列 */
37
+ headerFields: string[];
38
+ /** 各段按文件顺序排列(其后是函数字节码区,以 debugInfoOffset 为界) */
39
+ sections: SectionDesc[];
40
+ };
41
+
42
+ const FUNC_OFFSET_25: DeltaField = { byte: 0, bit: 0, bits: 25 };
43
+ const STRING_OFFSET_23: DeltaField = { byte: 0, bit: 1, bits: 23 };
44
+ const PAIR_OFFSET_32: DeltaField = { byte: 0, bit: 0, bits: 32 };
45
+
46
+ // v98 两种头部变体共享的段布局
47
+ const V98_SECTIONS: SectionDesc[] = [
48
+ {
49
+ name: 'functionHeaders',
50
+ countField: 'functionCount',
51
+ entrySize: 12,
52
+ deltaFields: [FUNC_OFFSET_25],
53
+ },
54
+ { name: 'stringKinds', countField: 'stringKindCount', entrySize: 4 },
55
+ { name: 'identifierHashes', countField: 'identifierCount', entrySize: 4 },
56
+ {
57
+ name: 'smallStringTable',
58
+ countField: 'stringCount',
59
+ entrySize: 4,
60
+ deltaFields: [STRING_OFFSET_23],
61
+ },
62
+ {
63
+ name: 'overflowStringTable',
64
+ countField: 'overflowStringCount',
65
+ entrySize: 8,
66
+ deltaFields: [PAIR_OFFSET_32],
67
+ },
68
+ { name: 'stringStorage', countField: 'stringStorageSize', entrySize: 1 },
69
+ {
70
+ name: 'literalValueBuffer',
71
+ countField: 'literalValueBufferSize',
72
+ entrySize: 1,
73
+ },
74
+ { name: 'objKeyBuffer', countField: 'objKeyBufferSize', entrySize: 1 },
75
+ // ShapeTableEntry 8B:{keyBufferOffset u32, numProps u32}
76
+ {
77
+ name: 'objShapeTable',
78
+ countField: 'objShapeTableCount',
79
+ entrySize: 8,
80
+ deltaFields: [PAIR_OFFSET_32],
81
+ },
82
+ {
83
+ name: 'bigIntTable',
84
+ countField: 'bigIntCount',
85
+ entrySize: 8,
86
+ deltaFields: [PAIR_OFFSET_32],
87
+ },
88
+ { name: 'bigIntStorage', countField: 'bigIntStorageSize', entrySize: 1 },
89
+ {
90
+ name: 'regExpTable',
91
+ countField: 'regExpCount',
92
+ entrySize: 8,
93
+ deltaFields: [PAIR_OFFSET_32],
94
+ },
95
+ { name: 'regExpStorage', countField: 'regExpStorageSize', entrySize: 1 },
96
+ { name: 'cjsModuleTable', countField: 'cjsModuleCount', entrySize: 8 },
97
+ {
98
+ name: 'functionSourceTable',
99
+ countField: 'functionSourceCount',
100
+ entrySize: 8,
101
+ },
102
+ ];
103
+
104
+ /**
105
+ * 布局描述表(唯一需要跟随 Hermes 版本演进的数据)。
106
+ * 字段序照抄 hermes 源码 include/hermes/BCGen/HBC/BytecodeFileFormat.h。
107
+ * v97 为 Static Hermes 短期预览版本,生产占比为零,不支持(自动回退)。
108
+ */
109
+ export const HBC_LAYOUTS: HbcLayout[] = [
110
+ {
111
+ // hermes 0.12 主线(RN 0.69~0.81+),v87 引入 bigInt 字段后布局稳定
112
+ minVersion: 87,
113
+ maxVersion: 96,
114
+ headerFields: [
115
+ 'fileLength',
116
+ 'globalCodeIndex',
117
+ 'functionCount',
118
+ 'stringKindCount',
119
+ 'identifierCount',
120
+ 'stringCount',
121
+ 'overflowStringCount',
122
+ 'stringStorageSize',
123
+ 'bigIntCount',
124
+ 'bigIntStorageSize',
125
+ 'regExpCount',
126
+ 'regExpStorageSize',
127
+ 'arrayBufferSize',
128
+ 'objKeyBufferSize',
129
+ 'objValueBufferSize',
130
+ 'segmentID',
131
+ 'cjsModuleCount',
132
+ 'functionSourceCount',
133
+ 'debugInfoOffset',
134
+ ],
135
+ sections: [
136
+ // SmallFuncHeader 16B:word0 offset:25|paramCount:7,word2 infoOffset:25|frameSize:7
137
+ {
138
+ name: 'functionHeaders',
139
+ countField: 'functionCount',
140
+ entrySize: 16,
141
+ deltaFields: [FUNC_OFFSET_25, { byte: 8, bit: 0, bits: 25 }],
142
+ },
143
+ { name: 'stringKinds', countField: 'stringKindCount', entrySize: 4 },
144
+ { name: 'identifierHashes', countField: 'identifierCount', entrySize: 4 },
145
+ // SmallStringTableEntry 4B:isUTF16:1|offset:23|length:8
146
+ {
147
+ name: 'smallStringTable',
148
+ countField: 'stringCount',
149
+ entrySize: 4,
150
+ deltaFields: [STRING_OFFSET_23],
151
+ },
152
+ // OverflowStringTableEntry 8B:{offset u32, length u32}
153
+ {
154
+ name: 'overflowStringTable',
155
+ countField: 'overflowStringCount',
156
+ entrySize: 8,
157
+ deltaFields: [PAIR_OFFSET_32],
158
+ },
159
+ { name: 'stringStorage', countField: 'stringStorageSize', entrySize: 1 },
160
+ { name: 'arrayBuffer', countField: 'arrayBufferSize', entrySize: 1 },
161
+ { name: 'objKeyBuffer', countField: 'objKeyBufferSize', entrySize: 1 },
162
+ {
163
+ name: 'objValueBuffer',
164
+ countField: 'objValueBufferSize',
165
+ entrySize: 1,
166
+ },
167
+ {
168
+ name: 'bigIntTable',
169
+ countField: 'bigIntCount',
170
+ entrySize: 8,
171
+ deltaFields: [PAIR_OFFSET_32],
172
+ },
173
+ { name: 'bigIntStorage', countField: 'bigIntStorageSize', entrySize: 1 },
174
+ {
175
+ name: 'regExpTable',
176
+ countField: 'regExpCount',
177
+ entrySize: 8,
178
+ deltaFields: [PAIR_OFFSET_32],
179
+ },
180
+ { name: 'regExpStorage', countField: 'regExpStorageSize', entrySize: 1 },
181
+ { name: 'cjsModuleTable', countField: 'cjsModuleCount', entrySize: 8 },
182
+ {
183
+ name: 'functionSourceTable',
184
+ countField: 'functionSourceCount',
185
+ entrySize: 8,
186
+ },
187
+ ],
188
+ },
189
+ {
190
+ // Static Hermes 晚期 v98(生产观测到的主流形态):相对早期 v98 头部
191
+ // 多一个 numStringSwitchImms 槽(hermes static_h 在不 bump 版本号的
192
+ // 情况下插入,同一版本号 98 因此存在两种布局,依结构校验区分)。
193
+ // 段布局与早期 v98 相同。
194
+ minVersion: 98,
195
+ maxVersion: 98,
196
+ headerFields: [
197
+ 'fileLength',
198
+ 'globalCodeIndex',
199
+ 'functionCount',
200
+ 'stringKindCount',
201
+ 'identifierCount',
202
+ 'stringCount',
203
+ 'overflowStringCount',
204
+ 'stringStorageSize',
205
+ 'bigIntCount',
206
+ 'bigIntStorageSize',
207
+ 'regExpCount',
208
+ 'regExpStorageSize',
209
+ 'literalValueBufferSize',
210
+ 'objKeyBufferSize',
211
+ 'objShapeTableCount',
212
+ 'numStringSwitchImms',
213
+ 'segmentID',
214
+ 'cjsModuleCount',
215
+ 'functionSourceCount',
216
+ 'debugInfoOffset',
217
+ ],
218
+ sections: V98_SECTIONS,
219
+ },
220
+ {
221
+ // Static Hermes 早期 v98(hermes c00cc57595 时点,无 numStringSwitchImms)。
222
+ // 相对 v96:SmallFuncHeader 16B→12B(去 infoOffset/environmentSize);
223
+ // objValueBuffer(字节段)→ objShapeTable(8B 条目);
224
+ // arrayBufferSize 更名 literalValueBufferSize(槽位不变)。
225
+ minVersion: 98,
226
+ maxVersion: 98,
227
+ headerFields: [
228
+ 'fileLength',
229
+ 'globalCodeIndex',
230
+ 'functionCount',
231
+ 'stringKindCount',
232
+ 'identifierCount',
233
+ 'stringCount',
234
+ 'overflowStringCount',
235
+ 'stringStorageSize',
236
+ 'bigIntCount',
237
+ 'bigIntStorageSize',
238
+ 'regExpCount',
239
+ 'regExpStorageSize',
240
+ 'literalValueBufferSize',
241
+ 'objKeyBufferSize',
242
+ 'objShapeTableCount',
243
+ 'segmentID',
244
+ 'cjsModuleCount',
245
+ 'functionSourceCount',
246
+ 'debugInfoOffset',
247
+ ],
248
+ sections: V98_SECTIONS,
249
+ },
250
+ ];
251
+
252
+ /**
253
+ * 随 patch 下发的 wire 格式(客户端 C 解释器的输入,全数字、无名字):
254
+ * counts: 头部 u32 计数字段个数(从偏移 32 起连续读取)
255
+ * sections: [countIndex, entrySize, [[byte, bit, bits], ...]][]
256
+ *
257
+ * 位置约定(C 端校验依赖,两端必须一致):
258
+ * counts 槽位 0 = fileLength,最后一个槽位 = debugInfoOffset。
259
+ */
260
+ export type HbcLayoutWire = {
261
+ counts: number;
262
+ sections: [number, number, [number, number, number][]][];
263
+ };
264
+
265
+ export function compileLayoutToWire(layout: HbcLayout): HbcLayoutWire {
266
+ if (
267
+ layout.headerFields[0] !== 'fileLength' ||
268
+ layout.headerFields[layout.headerFields.length - 1] !== 'debugInfoOffset'
269
+ ) {
270
+ throw new Error(
271
+ 'HbcLayout must start with fileLength and end with debugInfoOffset',
272
+ );
273
+ }
274
+ return {
275
+ counts: layout.headerFields.length,
276
+ sections: layout.sections.map((s) => [
277
+ layout.headerFields.indexOf(s.countField),
278
+ s.entrySize,
279
+ (s.deltaFields ?? []).map(
280
+ (f) => [f.byte, f.bit, f.bits] as [number, number, number],
281
+ ),
282
+ ]),
283
+ };
284
+ }
285
+
286
+ export function getHbcVersion(buf: Buffer): number | null {
287
+ if (buf.length < HBC_HEADER_SIZE) return null;
288
+ if (!buf.subarray(0, 8).equals(HBC_MAGIC)) return null;
289
+ return buf.readUInt32LE(8);
290
+ }
291
+
292
+ /** 同一版本号可能有多个候选布局(按声明顺序即优先级)。 */
293
+ export function findLayouts(version: number): HbcLayout[] {
294
+ return HBC_LAYOUTS.filter(
295
+ (l) => version >= l.minVersion && version <= l.maxVersion,
296
+ );
297
+ }
298
+
299
+ type ResolvedSection = { start: number; size: number; desc: SectionDesc };
300
+
301
+ const align4 = (x: number) => (x + 3) & ~3;
302
+
303
+ /**
304
+ * 按描述表解析并校验段边界。任何不一致(截断、计数越界、段区间超出
305
+ * debugInfoOffset)都返回 null——调用方回退到不变换路径。
306
+ * 描述表视为不可信输入,所有算术做显式边界检查。
307
+ */
308
+ function resolveSections(
309
+ buf: Buffer,
310
+ layout: HbcLayout,
311
+ ): ResolvedSection[] | null {
312
+ const headerEnd = HEADER_COUNTS_OFFSET + layout.headerFields.length * 4;
313
+ if (buf.length < HBC_HEADER_SIZE || headerEnd > HBC_HEADER_SIZE) return null;
314
+
315
+ const counts: Record<string, number> = {};
316
+ layout.headerFields.forEach((name, i) => {
317
+ counts[name] = buf.readUInt32LE(HEADER_COUNTS_OFFSET + i * 4);
318
+ });
319
+
320
+ if (counts.fileLength !== buf.length) return null;
321
+ const debugInfoOffset = counts.debugInfoOffset;
322
+ if (
323
+ !Number.isInteger(debugInfoOffset) ||
324
+ debugInfoOffset < HBC_HEADER_SIZE ||
325
+ debugInfoOffset > buf.length
326
+ ) {
327
+ return null;
328
+ }
329
+
330
+ const resolved: ResolvedSection[] = [];
331
+ let off = HBC_HEADER_SIZE;
332
+ for (const desc of layout.sections) {
333
+ const count = counts[desc.countField];
334
+ if (count === undefined || count > 0x0fffffff) return null;
335
+ const size = count * desc.entrySize;
336
+ off = align4(off);
337
+ if (off + size > debugInfoOffset) return null;
338
+ for (const f of desc.deltaFields ?? []) {
339
+ // 差分字段必须完整落在条目内且可用一次 u32 读写覆盖
340
+ if (
341
+ f.bits < 1 ||
342
+ f.bits > 32 ||
343
+ f.bit + f.bits > 32 ||
344
+ f.byte + 4 > desc.entrySize
345
+ ) {
346
+ return null;
347
+ }
348
+ }
349
+ resolved.push({ start: off, size, desc });
350
+ off += size;
351
+ }
352
+ return resolved;
353
+ }
354
+
355
+ function applyDelta(
356
+ out: Buffer,
357
+ section: ResolvedSection,
358
+ inverse: boolean,
359
+ ): void {
360
+ const { start, size, desc } = section;
361
+ for (const f of desc.deltaFields ?? []) {
362
+ // bits === 32 时掩码为全 1;>>> 保持无符号语义
363
+ const mask = f.bits === 32 ? 0xffffffff : ((1 << f.bits) - 1) << f.bit;
364
+ const fieldMask = f.bits === 32 ? 0xffffffff : (1 << f.bits) - 1;
365
+ let prev = 0;
366
+ for (let p = start + f.byte; p < start + size; p += desc.entrySize) {
367
+ const word = out.readUInt32LE(p);
368
+ const val = (word >>> f.bit) & fieldMask;
369
+ let enc: number;
370
+ if (!inverse) {
371
+ enc = (val - prev) & fieldMask;
372
+ prev = val;
373
+ } else {
374
+ enc = (val + prev) & fieldMask;
375
+ prev = enc;
376
+ }
377
+ out.writeUInt32LE(((word & ~mask) | (enc << f.bit)) >>> 0, p);
378
+ }
379
+ }
380
+ }
381
+
382
+ /**
383
+ * 对 HBC buffer 执行变换(inverse=false)或逆变换(inverse=true)。
384
+ * 同一版本号可能存在多个候选布局(如 v98 早/晚两种头部),按声明顺序
385
+ * 取第一个通过结构校验的。非 HBC / 无可用布局 → 返回 null。
386
+ * 注意:变换不改动头部计数,同一文件正/逆向解析选中的布局必然一致。
387
+ */
388
+ export function transformHbc(buf: Buffer, inverse = false): Buffer | null {
389
+ const layout = pickLayout(buf);
390
+ if (!layout) return null;
391
+ return transformHbcWithLayout(buf, layout, inverse);
392
+ }
393
+
394
+ /** 返回该 buffer 结构上匹配的布局(候选中第一个校验通过者)。 */
395
+ export function pickLayout(buf: Buffer): HbcLayout | null {
396
+ const version = getHbcVersion(buf);
397
+ if (version === null) return null;
398
+ for (const layout of findLayouts(version)) {
399
+ if (resolveSections(buf, layout)) return layout;
400
+ }
401
+ return null;
402
+ }
403
+
404
+ export function transformHbcWithLayout(
405
+ buf: Buffer,
406
+ layout: HbcLayout,
407
+ inverse: boolean,
408
+ ): Buffer | null {
409
+ const sections = resolveSections(buf, layout);
410
+ if (!sections) return null;
411
+ const out = Buffer.from(buf);
412
+ for (const s of sections) {
413
+ if (s.desc.deltaFields?.length) applyDelta(out, s, inverse);
414
+ }
415
+ return out;
416
+ }
417
+
418
+ export const HBC_TRANSFORM_VERSION = 1;
419
+
420
+ export type HbcTransformMeta = {
421
+ v: number;
422
+ hbcVersion: number;
423
+ layout: HbcLayoutWire;
424
+ };
425
+
426
+ /**
427
+ * 若 old/new 均为同版本且存在两侧都通过结构校验的候选布局,返回变换结果、
428
+ * 选中布局与写入 __diff.json 的元数据;否则返回 null。
429
+ */
430
+ export function tryTransformPair(
431
+ oldBuf: Buffer,
432
+ newBuf: Buffer,
433
+ ): {
434
+ tOld: Buffer;
435
+ tNew: Buffer;
436
+ layout: HbcLayout;
437
+ meta: HbcTransformMeta;
438
+ } | null {
439
+ const oldVersion = getHbcVersion(oldBuf);
440
+ const newVersion = getHbcVersion(newBuf);
441
+ if (oldVersion === null || newVersion === null || oldVersion !== newVersion) {
442
+ return null;
443
+ }
444
+ for (const layout of findLayouts(newVersion)) {
445
+ const tOld = transformHbcWithLayout(oldBuf, layout, false);
446
+ const tNew = transformHbcWithLayout(newBuf, layout, false);
447
+ if (!tOld || !tNew) continue;
448
+ return {
449
+ tOld,
450
+ tNew,
451
+ layout,
452
+ meta: {
453
+ v: HBC_TRANSFORM_VERSION,
454
+ hbcVersion: newVersion,
455
+ layout: compileLayoutToWire(layout),
456
+ },
457
+ };
458
+ }
459
+ return null;
460
+ }
package/src/versions.ts CHANGED
@@ -13,6 +13,8 @@ import { getBooleanOption, getStringListOption } from './utils/options';
13
13
 
14
14
  interface VersionCommandOptions {
15
15
  [key: string]: unknown;
16
+ config?: string;
17
+ appKey?: string;
16
18
  appId?: string;
17
19
  name?: string;
18
20
  description?: string;
@@ -464,7 +466,12 @@ export const versionCommands = {
464
466
  }
465
467
 
466
468
  const platform = await getPlatform(options.platform);
467
- const { appId } = await getSelectedApp(platform);
469
+ let appId = options.appId;
470
+ if (!appId) {
471
+ appId = (
472
+ await getSelectedApp(platform, options.config as string | undefined)
473
+ ).appId;
474
+ }
468
475
  const nonInteractive =
469
476
  getBooleanOption(options, 'no-interactive', false) || isNonInteractive();
470
477
 
@@ -539,7 +546,9 @@ export const versionCommands = {
539
546
  let appId = options.appId;
540
547
  if (!appId) {
541
548
  const platform = await getPlatform(options.platform);
542
- appId = (await getSelectedApp(platform)).appId;
549
+ appId = (
550
+ await getSelectedApp(platform, options.config as string | undefined)
551
+ ).appId;
543
552
  }
544
553
  const interactive = !(
545
554
  getBooleanOption(options, 'no-interactive', false) || isNonInteractive()
@@ -553,7 +562,9 @@ export const versionCommands = {
553
562
  let platform = options.platform;
554
563
  if (!appId) {
555
564
  platform = await getPlatform(platform);
556
- appId = (await getSelectedApp(platform)).appId;
565
+ appId = (
566
+ await getSelectedApp(platform, options.config as string | undefined)
567
+ ).appId;
557
568
  } else if (platform) {
558
569
  platform = await getPlatform(platform);
559
570
  }
@@ -683,7 +694,9 @@ export const versionCommands = {
683
694
  let platform = options.platform;
684
695
  if (!appId) {
685
696
  platform = await getPlatform(platform);
686
- appId = (await getSelectedApp(platform)).appId;
697
+ appId = (
698
+ await getSelectedApp(platform, options.config as string | undefined)
699
+ ).appId;
687
700
  } else if (platform) {
688
701
  await getPlatform(platform);
689
702
  }
@@ -710,7 +723,9 @@ export const versionCommands = {
710
723
  let appId = options.appId;
711
724
  if (!appId) {
712
725
  const platform = await getPlatform(options.platform);
713
- appId = (await getSelectedApp(platform)).appId;
726
+ appId = (
727
+ await getSelectedApp(platform, options.config as string | undefined)
728
+ ).appId;
714
729
  }
715
730
 
716
731
  const parsedVersionIds =