react-native-update-cli 2.22.6 → 2.22.7

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.
@@ -151,8 +151,68 @@ export function sha256Hex(data: Buffer): string {
151
151
  return createHash('sha256').update(data).digest('hex');
152
152
  }
153
153
 
154
- /** Compile an empty file to learn which HBC version this hermesc emits. */
154
+ const PROBE_CACHE_FILE = 'hbc-versions.json';
155
+ const PROBE_CACHE_ENTRIES = 16;
156
+
157
+ /** identity of a hermesc binary for the probe cache: path + size + mtime */
158
+ function probeCacheKey(hermesCommand: string): string | null {
159
+ try {
160
+ const stat = fs.statSync(hermesCommand);
161
+ return `${path.resolve(hermesCommand)}|${stat.size}|${Math.floor(stat.mtimeMs)}`;
162
+ } catch {
163
+ return null;
164
+ }
165
+ }
166
+
167
+ function readProbeCache(): Record<string, number> {
168
+ try {
169
+ const parsed = JSON.parse(
170
+ fs.readFileSync(path.join(cacheDir(), PROBE_CACHE_FILE), 'utf8'),
171
+ );
172
+ return parsed && typeof parsed === 'object' ? parsed : {};
173
+ } catch {
174
+ return {};
175
+ }
176
+ }
177
+
178
+ function writeProbeCache(entries: Record<string, number>): void {
179
+ try {
180
+ const keys = Object.keys(entries);
181
+ // keep the most recently inserted entries only
182
+ const trimmed = Object.fromEntries(
183
+ keys.slice(-PROBE_CACHE_ENTRIES).map((key) => [key, entries[key]]),
184
+ );
185
+ fs.ensureDirSync(cacheDir());
186
+ fs.writeFileSync(
187
+ path.join(cacheDir(), PROBE_CACHE_FILE),
188
+ JSON.stringify(trimmed),
189
+ );
190
+ } catch {
191
+ // the cache is a convenience; probing again next time is fine
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Which HBC version this hermesc emits. The answer is remembered per binary
197
+ * (path, size, mtime) in the cache dir so only the first run of a given
198
+ * hermesc pays for the empty-file compile.
199
+ */
155
200
  export function probeHbcVersion(hermesCommand: string): number | null {
201
+ const key = probeCacheKey(hermesCommand);
202
+ if (key) {
203
+ const cached = readProbeCache()[key];
204
+ if (Number.isInteger(cached) && cached > 0) return cached;
205
+ }
206
+ const version = compileProbe(hermesCommand);
207
+ if (key && version !== null) {
208
+ const { [key]: _stale, ...others } = readProbeCache();
209
+ writeProbeCache({ ...others, [key]: version });
210
+ }
211
+ return version;
212
+ }
213
+
214
+ /** Compile an empty file to learn which HBC version this hermesc emits. */
215
+ function compileProbe(hermesCommand: string): number | null {
156
216
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rnu-hermes-probe-'));
157
217
  try {
158
218
  const input = path.join(dir, 'probe.js');
@@ -303,8 +363,9 @@ export async function cacheLookup(bundleHash: string): Promise<string | null> {
303
363
  export async function cachePut(
304
364
  bundle: Buffer,
305
365
  maxMb?: number,
366
+ knownHash?: string,
306
367
  ): Promise<{ path: string; bundleHash: string }> {
307
- const bundleHash = sha256Hex(bundle);
368
+ const bundleHash = knownHash ?? sha256Hex(bundle);
308
369
  const dir = cacheDir();
309
370
  await fs.ensureDir(dir);
310
371
  const file = path.join(dir, bundleHash);
@@ -432,6 +493,8 @@ export interface HermesBaseServerRecord {
432
493
 
433
494
  interface FetchedBaseBundle {
434
495
  bundle: Buffer;
496
+ /** sha256 of `bundle`, computed once here */
497
+ bundleHash: string;
435
498
  /** how the bytes were obtained, for the log line */
436
499
  transport: 'range-entry' | 'range-zip' | 'full';
437
500
  fetchedBytes?: number;
@@ -464,7 +527,8 @@ async function saveResponse(
464
527
  /**
465
528
  * Obtain the base bundle with as little traffic as possible, in order:
466
529
  * 1. one Range request for the bundle's compressed bytes when the server
467
- * knows their location (ppk published by a CLI that reported it);
530
+ * knows their location (ppk / apk / ipa published by a CLI that
531
+ * reported it);
468
532
  * 2. the archive's central directory over Range, then just the bundle
469
533
  * entry (any zip: ppk, apk, ipa — harmony .app nests a second zip and
470
534
  * is skipped);
@@ -481,24 +545,31 @@ export async function fetchBaseBundle(
481
545
  log: (message: string) => void = () => {},
482
546
  ): Promise<FetchedBaseBundle | null> {
483
547
  const matches = bundleEntryMatcher(artifactType);
484
- const verified = (bundle: Buffer | null): Buffer | null => {
548
+ const verified = (
549
+ bundle: Buffer | null,
550
+ ): { bundle: Buffer; bundleHash: string } => {
485
551
  if (!bundle) throw new Error('bundle entry not found');
486
- if (record.bundleHash && sha256Hex(bundle) !== record.bundleHash) {
552
+ const bundleHash = sha256Hex(bundle);
553
+ if (record.bundleHash && bundleHash !== record.bundleHash) {
487
554
  throw new Error('bundleHash mismatch');
488
555
  }
489
- return bundle;
556
+ return { bundle, bundleHash };
490
557
  };
491
558
  const fromFullResponse = async (
492
559
  response: Response,
493
560
  ): Promise<FetchedBaseBundle | null> => {
494
561
  await saveResponse(response, archive);
495
562
  const bundle = await extractBundleFromArchive(archive);
496
- return bundle ? { bundle, transport: 'full' } : null;
563
+ return bundle
564
+ ? { bundle, bundleHash: sha256Hex(bundle), transport: 'full' }
565
+ : null;
497
566
  };
498
567
  const skip = (reason: string) =>
499
568
  log(t('hermesBaseRangeFallback', { reason }));
500
569
 
501
- if (artifactType === 'ppk' && hasBundleLocation(record)) {
570
+ // harmony .app nests the bundle in a second zip, so a location inside the
571
+ // outer archive is never reported for it
572
+ if (artifactType !== 'app' && hasBundleLocation(record)) {
502
573
  try {
503
574
  const { data, fetchedBytes, totalBytes } = await fetchZipEntryData(
504
575
  record.url,
@@ -509,7 +580,7 @@ export async function fetchBaseBundle(
509
580
  },
510
581
  );
511
582
  return {
512
- bundle: verified(data) as Buffer,
583
+ ...verified(data),
513
584
  transport: 'range-entry',
514
585
  fetchedBytes,
515
586
  totalBytes,
@@ -530,9 +601,13 @@ export async function fetchBaseBundle(
530
601
  skip('server ignores Range');
531
602
  return fromFullResponse(remote.response);
532
603
  }
533
- const bundle = await readRemoteZipEntry(remote.zipFile, matches);
604
+ const bundle = await readRemoteZipEntry(
605
+ remote.zipFile,
606
+ matches,
607
+ remote.reader,
608
+ );
534
609
  return {
535
- bundle: verified(bundle) as Buffer,
610
+ ...verified(bundle),
536
611
  transport: 'range-zip',
537
612
  fetchedBytes: remote.reader.fetchedBytes,
538
613
  totalBytes: remote.reader.totalSize,
@@ -548,7 +623,9 @@ export async function fetchBaseBundle(
548
623
 
549
624
  await downloadToFile(record.url, archive);
550
625
  const bundle = await extractBundleFromArchive(archive);
551
- return bundle ? { bundle, transport: 'full' } : null;
626
+ return bundle
627
+ ? { bundle, bundleHash: sha256Hex(bundle), transport: 'full' }
628
+ : null;
552
629
  }
553
630
 
554
631
  export interface ResolveHermesBaseParams {
@@ -688,7 +765,7 @@ export async function resolveHermesBase(
688
765
  const fetched = await fetchBaseBundle(record, artifactType, archive, log);
689
766
  if (!fetched)
690
767
  throw new Error('bundle entry not found in downloaded package');
691
- const { bundle } = fetched;
768
+ const { bundle, bundleHash: actualHash } = fetched;
692
769
  if (fetched.transport !== 'full') {
693
770
  log(
694
771
  t('hermesBaseRangeFetched', {
@@ -697,7 +774,6 @@ export async function resolveHermesBase(
697
774
  }),
698
775
  );
699
776
  }
700
- const actualHash = sha256Hex(bundle);
701
777
  if (record.bundleHash && actualHash !== record.bundleHash) {
702
778
  throw new Error(
703
779
  `bundleHash mismatch (${actualHash.slice(0, 12)} != ${record.bundleHash.slice(0, 12)})`,
@@ -712,7 +788,7 @@ export async function resolveHermesBase(
712
788
  );
713
789
  return null;
714
790
  }
715
- const cached = await cachePut(bundle, params.cacheMaxMb);
791
+ const cached = await cachePut(bundle, params.cacheMaxMb, actualHash);
716
792
  log(
717
793
  t('hermesBaseUsing', {
718
794
  source:
@@ -780,41 +856,84 @@ export function normalizeDisassemblyLine(
780
856
  line: string,
781
857
  strings: Map<number, string>,
782
858
  ): string | null {
783
- if (/^Offset in debug table/.test(line)) return null;
784
- let m =
785
- /^(\s*New(?:Array|Object)WithBuffer)(?:Long)?(?:AndParent)?\s+(r\d+)(.*)$/.exec(
786
- line,
787
- );
788
- if (m) {
789
- const nums = m[3].match(/\d+/g) ?? [];
790
- return `${m[1]} ${m[2]} sizes=${nums.slice(0, 1).join(',')}`;
791
- }
792
- m = /^(\s*J[A-Za-z]+?)(Long)?\s+(L\d+|\d+)(.*)$/.exec(line);
793
- if (m) return `${m[1]} <tgt>${m[4]}`;
794
- m = /^(\s*DefineOwnById\w*\s+r\d+, r\d+, \d+, )(\d+)$/.exec(line);
795
- if (m) line = `${m[1]}"${strings.get(Number(m[2])) ?? `?${m[2]}`}"`;
859
+ // Cheap dispatch on the opcode before touching any regex: dumps run to
860
+ // millions of lines and only a handful of instructions need rewriting.
861
+ let indent = 0;
862
+ while (indent < line.length && isSpace(line.charCodeAt(indent))) indent++;
863
+ let opEnd = indent;
864
+ while (opEnd < line.length && isLetter(line.charCodeAt(opEnd))) opEnd++;
865
+ const opcode = line.slice(indent, opEnd);
866
+ if (opcode === 'Offset' && line.startsWith('Offset in debug table', indent)) {
867
+ return null;
868
+ }
869
+ let m: RegExpExecArray | null;
870
+ if (opcode.startsWith('New') && opcode.includes('WithBuffer')) {
871
+ m =
872
+ /^(\s*New(?:Array|Object)WithBuffer)(?:Long)?(?:AndParent)?\s+(r\d+)(.*)$/.exec(
873
+ line,
874
+ );
875
+ if (m) {
876
+ const nums = m[3].match(/\d+/g) ?? [];
877
+ return `${m[1]} ${m[2]} sizes=${nums.slice(0, 1).join(',')}`;
878
+ }
879
+ }
880
+ if (opcode.charCodeAt(0) === 0x4a /* J */) {
881
+ m = /^(\s*J[A-Za-z]+?)(Long)?\s+(L\d+|\d+)(.*)$/.exec(line);
882
+ if (m) return `${m[1]} <tgt>${m[4]}`;
883
+ }
884
+ if (opcode.startsWith('DefineOwnById')) {
885
+ m = /^(\s*DefineOwnById\w*\s+r\d+, r\d+, \d+, )(\d+)$/.exec(line);
886
+ if (m) line = `${m[1]}"${strings.get(Number(m[2])) ?? `?${m[2]}`}"`;
887
+ }
796
888
  // Operand-width variants of one instruction (GetByIdShort/GetById/GetByIdLong,
797
889
  // LoadConstString/LoadConstStringLongIndex, ...) only differ by how wide a
798
890
  // string/function id or offset is encoded — a foreign base hands the new
799
891
  // code's hot strings large ids, so the delta build legitimately picks the
800
892
  // wider form. Fold the suffix and the column padding that follows it.
801
- m = /^(\s*)([A-Za-z]+?)(?:LongIndex|Long|Short)?(\s+.*|)$/.exec(line);
802
- if (m) line = `${m[1]}${m[2]}${m[3].replace(/\s+/g, ' ')}`;
803
- // Switch jump tables sit after the instructions; their relative offset (and
804
- // the table header hermesc prints for them) moves with instruction widths.
805
- // The two switch instructions carry that offset in different operands:
806
- // StringSwitchImm rX, <id>, <jtOffset>, <defaultLabel>, <count>
807
- // UIntSwitchImm rX, <jtOffset>, <defaultLabel>, <min>, <max>
808
- // Folding only the first shape let a shifted UIntSwitchImm offset read as a
809
- // real difference and drop an otherwise good delta build.
810
- m = /^(\s*StringSwitchImm r\d+, \d+, )\d+(, L\d+, \d+)$/.exec(line);
811
- if (m) line = `${m[1]}<jt>${m[2]}`;
812
- m = /^(\s*UIntSwitchImm r\d+, )\d+(, L\d+, \d+, \d+)$/.exec(line);
813
- if (m) line = `${m[1]}<jt>${m[2]}`;
814
- if (/^\s*offset \d+$/.test(line)) line = line.replace(/\d+$/, '<jt>');
893
+ if (
894
+ opEnd > indent &&
895
+ (opEnd === line.length || isSpace(line.charCodeAt(opEnd)))
896
+ ) {
897
+ const folded = foldWidthSuffix(opcode);
898
+ line = `${line.slice(0, indent)}${folded}${line.slice(opEnd).replace(/\s+/g, ' ')}`;
899
+ // Switch jump tables sit after the instructions; their relative offset (and
900
+ // the table header hermesc prints for them) moves with instruction widths.
901
+ // The two switch instructions carry that offset in different operands:
902
+ // StringSwitchImm rX, <id>, <jtOffset>, <defaultLabel>, <count>
903
+ // UIntSwitchImm rX, <jtOffset>, <defaultLabel>, <min>, <max>
904
+ // Folding only the first shape let a shifted UIntSwitchImm offset read as a
905
+ // real difference and drop an otherwise good delta build.
906
+ if (folded === 'StringSwitchImm') {
907
+ m = /^(\s*StringSwitchImm r\d+, \d+, )\d+(, L\d+, \d+)$/.exec(line);
908
+ if (m) line = `${m[1]}<jt>${m[2]}`;
909
+ } else if (folded === 'UIntSwitchImm') {
910
+ m = /^(\s*UIntSwitchImm r\d+, )\d+(, L\d+, \d+, \d+)$/.exec(line);
911
+ if (m) line = `${m[1]}<jt>${m[2]}`;
912
+ } else if (folded === 'offset' && /^\s*offset \d+$/.test(line)) {
913
+ line = line.replace(/\d+$/, '<jt>');
914
+ }
915
+ }
815
916
  return line;
816
917
  }
817
918
 
919
+ function isSpace(code: number): boolean {
920
+ return code === 0x20 || code === 0x09 || code === 0x0d;
921
+ }
922
+
923
+ function isLetter(code: number): boolean {
924
+ return (code >= 0x41 && code <= 0x5a) || (code >= 0x61 && code <= 0x7a);
925
+ }
926
+
927
+ /** strip one operand-width suffix (LongIndex, Long, Short) off an opcode */
928
+ function foldWidthSuffix(opcode: string): string {
929
+ for (const suffix of ['LongIndex', 'Long', 'Short']) {
930
+ if (opcode.length > suffix.length && opcode.endsWith(suffix)) {
931
+ return opcode.slice(0, -suffix.length);
932
+ }
933
+ }
934
+ return opcode;
935
+ }
936
+
818
937
  /** Split a readable stream into lines without readline (Bun's readline async iterator misbehaves when pulled manually). */
819
938
  async function* streamLines(
820
939
  stream: NodeJS.ReadableStream,