react-native-update-cli 2.29.1 → 2.29.3

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.
Files changed (80) hide show
  1. package/lib/account.js +2 -4
  2. package/lib/api.d.ts +1 -1
  3. package/lib/api.js +26 -44
  4. package/lib/app.js +13 -27
  5. package/lib/auto-update.d.ts +1 -1
  6. package/lib/auto-update.js +6 -16
  7. package/lib/bin.js +4 -6
  8. package/lib/bundle-pack.js +25 -27
  9. package/lib/bundle-runner.js +55 -54
  10. package/lib/bundle.js +18 -26
  11. package/lib/diff.js +32 -91
  12. package/lib/locales/en.d.ts +0 -10
  13. package/lib/locales/en.js +0 -10
  14. package/lib/locales/zh.d.ts +0 -10
  15. package/lib/locales/zh.js +0 -10
  16. package/lib/native-package.js +20 -60
  17. package/lib/package.d.ts +6 -1
  18. package/lib/package.js +59 -74
  19. package/lib/provider.js +3 -5
  20. package/lib/symbolicate.js +1 -16
  21. package/lib/utils/add-gitignore.js +0 -1
  22. package/lib/utils/app-info-parser/aab.d.ts +5 -22
  23. package/lib/utils/app-info-parser/aab.js +87 -192
  24. package/lib/utils/app-info-parser/apk.d.ts +12 -13
  25. package/lib/utils/app-info-parser/apk.js +28 -78
  26. package/lib/utils/app-info-parser/ipa.d.ts +2 -14
  27. package/lib/utils/app-info-parser/ipa.js +9 -70
  28. package/lib/utils/app-info-parser/resource-finder.d.ts +1 -26
  29. package/lib/utils/app-info-parser/resource-finder.js +41 -183
  30. package/lib/utils/app-info-parser/xml-parser/binary.d.ts +6 -55
  31. package/lib/utils/app-info-parser/xml-parser/binary.js +152 -556
  32. package/lib/utils/app-info-parser/zip.d.ts +6 -24
  33. package/lib/utils/app-info-parser/zip.js +19 -86
  34. package/lib/utils/check-plugin.d.ts +7 -5
  35. package/lib/utils/check-plugin.js +15 -25
  36. package/lib/utils/dep-versions.d.ts +0 -6
  37. package/lib/utils/dep-versions.js +2 -27
  38. package/lib/utils/failure-fingerprint.js +3 -4
  39. package/lib/utils/fs-helpers.d.ts +5 -0
  40. package/lib/utils/fs-helpers.js +19 -0
  41. package/lib/utils/git.d.ts +0 -1
  42. package/lib/utils/git.js +0 -8
  43. package/lib/utils/hash.d.ts +3 -0
  44. package/lib/utils/hash.js +19 -0
  45. package/lib/utils/hbcTransform.d.ts +9 -2
  46. package/lib/utils/hbcTransform.js +41 -70
  47. package/lib/utils/hermes-base.d.ts +1 -5
  48. package/lib/utils/hermes-base.js +93 -156
  49. package/lib/utils/hermes-literals.d.ts +5 -3
  50. package/lib/utils/hermes-literals.js +30 -53
  51. package/lib/utils/hermes-raw.d.ts +7 -0
  52. package/lib/utils/hermes-raw.js +26 -9
  53. package/lib/utils/http-helper.d.ts +0 -1
  54. package/lib/utils/http-helper.js +0 -20
  55. package/lib/utils/i18n.js +0 -2
  56. package/lib/utils/index.d.ts +5 -19
  57. package/lib/utils/index.js +95 -189
  58. package/lib/utils/latest-version.d.ts +19 -0
  59. package/lib/utils/latest-version.js +137 -0
  60. package/lib/utils/options.d.ts +2 -1
  61. package/lib/utils/options.js +5 -7
  62. package/lib/utils/zip-entries.d.ts +23 -0
  63. package/lib/utils/zip-entries.js +125 -102
  64. package/lib/utils/zip-options.js +29 -56
  65. package/lib/utils/zip-range.js +25 -65
  66. package/lib/versions.d.ts +5 -0
  67. package/lib/versions.js +86 -153
  68. package/package.json +1 -3
  69. package/lib/utils/app-info-parser/app.d.ts +0 -4
  70. package/lib/utils/app-info-parser/app.js +0 -10
  71. package/lib/utils/app-info-parser/index.d.ts +0 -16
  72. package/lib/utils/app-info-parser/index.js +0 -46
  73. package/lib/utils/app-info-parser/utils.d.ts +0 -31
  74. package/lib/utils/app-info-parser/utils.js +0 -135
  75. package/lib/utils/app-info-parser/xml-parser/manifest.d.ts +0 -10
  76. package/lib/utils/app-info-parser/xml-parser/manifest.js +0 -202
  77. package/lib/utils/latest-version/index.d.ts +0 -149
  78. package/lib/utils/latest-version/index.js +0 -326
  79. package/lib/utils/plugin-config.d.ts +0 -9
  80. package/lib/utils/plugin-config.js +0 -31
@@ -3,7 +3,7 @@
3
3
  //
4
4
  // Hermes re-sorts its string table by identifier frequency on every compile, so
5
5
  // any source change renumbers most string IDs and the bytecode diff explodes
6
- // (see hermes-base-bytecode-方案.md). Compiling with the previous HBC of the
6
+ // (see docs/hermes-base-verification.md). Compiling with the previous HBC of the
7
7
  // same app as `-base-bytecode` keeps old IDs stable and shrinks patches 5-30×.
8
8
  // This module resolves that base: gating by hermesc build, HBC version probe,
9
9
  // server lookup, a sha256-named local cache, download + verification, and the
@@ -49,8 +49,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
49
49
  exports.LITERAL_SEPARATOR = exports.DEFAULT_CACHE_MAX_FILES = exports.DEFAULT_CACHE_MAX_BYTES = exports.BUNDLE_ENTRY_NAMES = exports.MIN_HERMES_COMPILER_COMMITLY_DATE = exports.MIN_HERMES_COMPILER_BUILD = exports.HERMES_BASE_DETAIL_MAX_CHARS = exports.PLAIN_JS_BYTECODE_VERSION = exports.HbcVersionMismatchError = exports.BundleHashMismatchError = exports.PermanentBaseError = void 0;
50
50
  exports.reportedBytecodeVersion = reportedBytecodeVersion;
51
51
  exports.classifyHermesCommand = classifyHermesCommand;
52
- exports.sha256Hex = sha256Hex;
53
- exports.sha256File = sha256File;
54
52
  exports.readFileHbcVersion = readFileHbcVersion;
55
53
  exports.probeHbcVersion = probeHbcVersion;
56
54
  exports.bundleEntryMatcher = bundleEntryMatcher;
@@ -75,7 +73,6 @@ exports.compareHermesBytecode = compareHermesBytecode;
75
73
  exports.verifyHermesBaseEquivalence = verifyHermesBaseEquivalence;
76
74
  const node_string_decoder_1 = require("node:string_decoder");
77
75
  const child_process_1 = require("child_process");
78
- const crypto_1 = require("crypto");
79
76
  const fs_1 = __importDefault(require("fs"));
80
77
  const os_1 = __importDefault(require("os"));
81
78
  const path_1 = __importDefault(require("path"));
@@ -84,6 +81,7 @@ const promises_1 = require("stream/promises");
84
81
  const constants_1 = require("./constants");
85
82
  const failure_fingerprint_1 = require("./failure-fingerprint");
86
83
  const fs_helpers_1 = require("./fs-helpers");
84
+ const hash_1 = require("./hash");
87
85
  const hbcTransform_1 = require("./hbcTransform");
88
86
  const hermes_cached_object_1 = require("./hermes-cached-object");
89
87
  const hermes_literals_1 = require("./hermes-literals");
@@ -131,8 +129,6 @@ function reportedBytecodeVersion(bundle) {
131
129
  const describeBytecodeVersion = (version) => version === exports.PLAIN_JS_BYTECODE_VERSION
132
130
  ? 'plain JS'
133
131
  : `HBC ${version ?? 'n/a'}`;
134
- /** bytes `getHbcVersion` needs to read the version */
135
- const HBC_HEAD_BYTES = 128;
136
132
  /** time allowed for a response to start (headers) */
137
133
  const FETCH_HEADERS_TIMEOUT_MS = 30_000;
138
134
  /** a download that receives nothing for this long is abandoned */
@@ -173,47 +169,35 @@ function classifyHermesCommand(hermesCommand) {
173
169
  };
174
170
  }
175
171
  const build = /^(\d{9})\./.exec(version);
172
+ const commitly = /^0\.(\d+)\.\d+-commitly-(\d{8})/.exec(version);
173
+ const stable = /^0\.(\d+)\.\d+$/.exec(version);
174
+ let ok;
176
175
  if (build) {
177
- const ok = Number(build[1]) >= exports.MIN_HERMES_COMPILER_BUILD;
178
- return {
179
- allowed: ok,
180
- kind: 'hermes-compiler',
181
- version,
182
- reason: ok
183
- ? undefined
184
- : `hermes-compiler ${version} predates the delta-mode fix`,
185
- };
176
+ ok = Number(build[1]) >= exports.MIN_HERMES_COMPILER_BUILD;
186
177
  }
187
- const commitly = /^0\.(\d+)\.\d+-commitly-(\d{8})/.exec(version);
188
- if (commitly) {
189
- const ok = Number(commitly[1]) >= 14 &&
190
- Number(commitly[2]) >= exports.MIN_HERMES_COMPILER_COMMITLY_DATE;
191
- return {
192
- allowed: ok,
193
- kind: 'hermes-compiler',
194
- version,
195
- reason: ok
196
- ? undefined
197
- : `hermes-compiler ${version} predates the delta-mode fix`,
198
- };
178
+ else if (commitly) {
179
+ ok =
180
+ Number(commitly[1]) >= 14 &&
181
+ Number(commitly[2]) >= exports.MIN_HERMES_COMPILER_COMMITLY_DATE;
199
182
  }
200
- const stable = /^0\.(\d+)\.\d+$/.exec(version);
201
- if (stable) {
202
- const ok = Number(stable[1]) >= 14;
183
+ else if (stable) {
184
+ ok = Number(stable[1]) >= 14;
185
+ }
186
+ else {
203
187
  return {
204
- allowed: ok,
188
+ allowed: false,
205
189
  kind: 'hermes-compiler',
206
190
  version,
207
- reason: ok
208
- ? undefined
209
- : `hermes-compiler ${version} predates the delta-mode fix`,
191
+ reason: `unrecognized hermes-compiler version ${version}`,
210
192
  };
211
193
  }
212
194
  return {
213
- allowed: false,
195
+ allowed: ok,
214
196
  kind: 'hermes-compiler',
215
197
  version,
216
- reason: `unrecognized hermes-compiler version ${version}`,
198
+ reason: ok
199
+ ? undefined
200
+ : `hermes-compiler ${version} predates the delta-mode fix`,
217
201
  };
218
202
  }
219
203
  return {
@@ -225,30 +209,10 @@ function classifyHermesCommand(hermesCommand) {
225
209
  // ---------------------------------------------------------------------------
226
210
  // HBC helpers
227
211
  // ---------------------------------------------------------------------------
228
- function sha256Hex(data) {
229
- return (0, crypto_1.createHash)('sha256').update(data).digest('hex');
230
- }
231
- /** sha256 of a file, streamed (a cached base can be tens of MB) */
232
- async function sha256File(file) {
233
- const hash = (0, crypto_1.createHash)('sha256');
234
- for await (const chunk of fs_1.default.createReadStream(file))
235
- hash.update(chunk);
236
- return hash.digest('hex');
237
- }
238
212
  /** HBC version from the first bytes of a file, without reading the rest */
239
213
  async function readFileHbcVersion(file) {
240
- const header = Buffer.alloc(128);
241
- const handle = await fs_1.default.promises.open(file, 'r');
242
- try {
243
- const { bytesRead } = await handle.read(header, 0, 128, 0);
244
- return (0, hbcTransform_1.getHbcVersion)(header.subarray(0, bytesRead));
245
- }
246
- catch {
247
- return null;
248
- }
249
- finally {
250
- await handle.close();
251
- }
214
+ const info = await (0, hbcTransform_1.readHbcFileHeader)(file);
215
+ return info && (0, hbcTransform_1.getHbcVersion)(info.header);
252
216
  }
253
217
  const PROBE_CACHE_FILE = 'hbc-versions.json';
254
218
  const PROBE_CACHE_ENTRIES = 16;
@@ -317,15 +281,8 @@ function compileProbe(hermesCommand) {
317
281
  if (result.status !== 0 || !fs_1.default.existsSync(output)) {
318
282
  return null;
319
283
  }
320
- const header = Buffer.alloc(128);
321
- const fd = fs_1.default.openSync(output, 'r');
322
- try {
323
- fs_1.default.readSync(fd, header, 0, 128, 0);
324
- }
325
- finally {
326
- fs_1.default.closeSync(fd);
327
- }
328
- return (0, hbcTransform_1.getHbcVersion)(header);
284
+ // the probe output is a few hundred bytes
285
+ return (0, hbcTransform_1.getHbcVersion)(fs_1.default.readFileSync(output));
329
286
  }
330
287
  catch {
331
288
  return null;
@@ -366,35 +323,27 @@ async function extractBundleFromArchive(filePath) {
366
323
  return fs_1.default.promises.readFile(filePath);
367
324
  }
368
325
  if (ext === '.ppk' || ext === '.apk' || ext === '.ipa') {
369
- return readFirstZipEntry(filePath, bundleEntryMatcher(ext.slice(1)));
326
+ return (0, zip_entries_1.readFirstEntry)(filePath, bundleEntryMatcher(ext.slice(1)));
370
327
  }
371
328
  if (ext === '.app') {
372
- // harmony .app wraps .hap archives; reuse the existing parser
373
- const { default: AppInfoParser } = await Promise.resolve().then(() => __importStar(require('./app-info-parser')));
374
- const parser = new AppInfoParser(filePath);
375
- const [bundle] = await parser.parser.getEntriesFromHarmonyApp([
329
+ // harmony .app wraps .hap archives; readZipEntries walks into them
330
+ const { readZipEntries } = await Promise.resolve().then(() => __importStar(require('./app-info-parser/zip')));
331
+ const [bundle] = await readZipEntries(filePath, [
376
332
  /^resources\/rawfile\/bundle\.harmony\.js$/,
377
333
  ]);
378
334
  return bundle ?? null;
379
335
  }
380
336
  // unknown extension: try as zip, then as raw file
381
- const fromZip = await readFirstZipEntry(filePath, (name) => exports.BUNDLE_ENTRY_NAMES.includes(name)).catch(() => null);
337
+ const fromZip = await (0, zip_entries_1.readFirstEntry)(filePath, (name) => exports.BUNDLE_ENTRY_NAMES.includes(name)).catch(() => null);
382
338
  return fromZip ?? fs_1.default.promises.readFile(filePath);
383
339
  }
384
- async function readFirstZipEntry(zipPath, matches) {
385
- let found = null;
386
- await (0, zip_entries_1.enumZipEntries)(zipPath, async (entry, zipFile) => {
387
- if (!found && matches(entry.fileName)) {
388
- found = await (0, zip_entries_1.readEntry)(entry, zipFile);
389
- }
390
- });
391
- return found;
392
- }
393
340
  // ---------------------------------------------------------------------------
394
341
  // cache (sha256-named bundles)
395
342
  // ---------------------------------------------------------------------------
396
343
  exports.DEFAULT_CACHE_MAX_BYTES = 500 * 1024 * 1024;
397
344
  exports.DEFAULT_CACHE_MAX_FILES = 20;
345
+ /** cache entries are named by the sha256 of their bundle */
346
+ const isBundleHash = (name) => /^[0-9a-f]{64}$/.test(name);
398
347
  function cacheDir() {
399
348
  return process.env.PUSHY_CACHE_DIR || path_1.default.join(constants_1.tempDir, 'cache');
400
349
  }
@@ -412,12 +361,12 @@ function cacheLimits(overrideMaxMb) {
412
361
  }
413
362
  /** Returns the cached bundle path when present and its sha256 still matches. */
414
363
  async function cacheLookup(bundleHash) {
415
- if (!/^[0-9a-f]{64}$/.test(bundleHash))
364
+ if (!isBundleHash(bundleHash))
416
365
  return null;
417
366
  const file = path_1.default.join(cacheDir(), bundleHash);
418
367
  if (!(await (0, fs_helpers_1.pathExists)(file)))
419
368
  return null;
420
- if ((await sha256File(file).catch(() => '')) !== bundleHash) {
369
+ if ((await (0, hash_1.sha256File)(file).catch(() => '')) !== bundleHash) {
421
370
  // corrupt entry: never trust cache content
422
371
  await (0, fs_helpers_1.remove)(file).catch(() => { });
423
372
  return null;
@@ -428,7 +377,7 @@ async function cacheLookup(bundleHash) {
428
377
  }
429
378
  /** Store a bundle under its sha256 (LRU-evicting to the configured limits). */
430
379
  async function cachePut(bundle, maxMb, knownHash) {
431
- const bundleHash = knownHash ?? sha256Hex(bundle);
380
+ const bundleHash = knownHash ?? (0, hash_1.sha256Hex)(bundle);
432
381
  const dir = cacheDir();
433
382
  await (0, fs_helpers_1.ensureDir)(dir);
434
383
  const file = path_1.default.join(dir, bundleHash);
@@ -453,7 +402,7 @@ async function enforceCacheLimits(maxMb, keep) {
453
402
  const staleTmpCutoff = Date.now() - STALE_CACHE_TMP_MS;
454
403
  for (const name of await fs_1.default.promises.readdir(dir)) {
455
404
  const isTmp = isCacheTmpName(name);
456
- if (!isTmp && !/^[0-9a-f]{64}$/.test(name))
405
+ if (!isTmp && !isBundleHash(name))
457
406
  continue;
458
407
  const stat = await fs_1.default.promises.stat(path_1.default.join(dir, name)).catch(() => null);
459
408
  if (!stat?.isFile())
@@ -488,7 +437,7 @@ async function cleanCache() {
488
437
  return 0;
489
438
  let bundles = 0;
490
439
  for (const name of await fs_1.default.promises.readdir(dir)) {
491
- const isBundle = /^[0-9a-f]{64}$/.test(name);
440
+ const isBundle = isBundleHash(name);
492
441
  if (!isBundle && !isCacheTmpName(name))
493
442
  continue;
494
443
  await (0, fs_helpers_1.remove)(path_1.default.join(dir, name));
@@ -503,7 +452,7 @@ async function cacheStats() {
503
452
  let bytes = 0;
504
453
  if (await (0, fs_helpers_1.pathExists)(dir)) {
505
454
  for (const name of await fs_1.default.promises.readdir(dir)) {
506
- if (!/^[0-9a-f]{64}$/.test(name))
455
+ if (!isBundleHash(name))
507
456
  continue;
508
457
  const stat = await fs_1.default.promises
509
458
  .stat(path_1.default.join(dir, name))
@@ -609,20 +558,23 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
609
558
  const verified = (bundle) => {
610
559
  if (!bundle)
611
560
  throw new PermanentBaseError('bundle entry not found');
612
- const bundleHash = sha256Hex(bundle);
561
+ const bundleHash = (0, hash_1.sha256Hex)(bundle);
613
562
  if (record.bundleHash && bundleHash !== record.bundleHash) {
614
563
  throw new BundleHashMismatchError(bundleHash, record.bundleHash);
615
564
  }
616
565
  return { bundle, bundleHash };
617
566
  };
618
- const fromFullResponse = async (response) => {
567
+ const fromArchive = async () => ({
568
+ ...verified(await extractBundleFromArchive(archive)),
569
+ transport: 'full',
570
+ });
571
+ const skip = (reason) => log((0, i18n_1.t)('hermesBaseRangeFallback', { reason }));
572
+ // a 200 to a Range request already carries the whole archive
573
+ const rangeIgnored = async (response) => {
574
+ skip('server ignores Range');
619
575
  await saveResponse(response, archive);
620
- return {
621
- ...verified(await extractBundleFromArchive(archive)),
622
- transport: 'full',
623
- };
576
+ return fromArchive();
624
577
  };
625
- const skip = (reason) => log((0, i18n_1.t)('hermesBaseRangeFallback', { reason }));
626
578
  // harmony .app nests the bundle in a second zip, so a location inside the
627
579
  // outer archive is never reported for it
628
580
  if (artifactType !== 'app' && hasBundleLocation(record)) {
@@ -633,7 +585,7 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
633
585
  };
634
586
  try {
635
587
  const headMatches = expectHbcVersion === undefined ||
636
- (0, hbcTransform_1.getHbcVersion)(await (0, zip_range_1.fetchZipEntryHead)(record.url, location, HBC_HEAD_BYTES)) === expectHbcVersion;
588
+ (0, hbcTransform_1.getHbcVersion)(await (0, zip_range_1.fetchZipEntryHead)(record.url, location, hbcTransform_1.HBC_HEADER_SIZE)) === expectHbcVersion;
637
589
  if (!headMatches)
638
590
  throw new StaleHeadError();
639
591
  const { data, fetchedBytes, totalBytes } = await (0, zip_range_1.fetchZipEntryData)(record.url, location);
@@ -646,8 +598,7 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
646
598
  }
647
599
  catch (error) {
648
600
  if (error instanceof zip_range_1.RangeUnsupportedError) {
649
- skip('server ignores Range');
650
- return fromFullResponse(error.response);
601
+ return rangeIgnored(error.response);
651
602
  }
652
603
  // not a transport failure: the directory read below settles it
653
604
  if (!(error instanceof StaleHeadError)) {
@@ -659,13 +610,12 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
659
610
  try {
660
611
  const remote = await (0, zip_range_1.openRemoteZip)(record.url);
661
612
  if (remote.kind === 'full') {
662
- skip('server ignores Range');
663
- return fromFullResponse(remote.response);
613
+ return rangeIgnored(remote.response);
664
614
  }
665
615
  const bundle = await (0, zip_range_1.readRemoteZipEntry)(remote.zipFile, matches, remote.reader, expectHbcVersion === undefined
666
616
  ? undefined
667
617
  : {
668
- bytes: HBC_HEAD_BYTES,
618
+ bytes: hbcTransform_1.HBC_HEADER_SIZE,
669
619
  check: (head) => {
670
620
  const version = (0, hbcTransform_1.getHbcVersion)(head);
671
621
  if (version !== expectHbcVersion) {
@@ -682,8 +632,7 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
682
632
  }
683
633
  catch (error) {
684
634
  if (error instanceof zip_range_1.RangeUnsupportedError) {
685
- skip('server ignores Range');
686
- return fromFullResponse(error.response);
635
+ return rangeIgnored(error.response);
687
636
  }
688
637
  // the real entry was read and is not the recorded one: final
689
638
  if (error instanceof PermanentBaseError)
@@ -692,10 +641,7 @@ async function fetchBaseBundle(record, artifactType, archive, log = () => { }, e
692
641
  }
693
642
  }
694
643
  await downloadToFile(record.url, archive);
695
- return {
696
- ...verified(await extractBundleFromArchive(archive)),
697
- transport: 'full',
698
- };
644
+ return fromArchive();
699
645
  }
700
646
  /**
701
647
  * Pick and materialize the base bundle for this compile. Returns null (with
@@ -749,11 +695,6 @@ async function resolveHermesBase(params) {
749
695
  }));
750
696
  return null;
751
697
  }
752
- const source = record?.artifactType && record.artifactType !== 'ppk'
753
- ? 'native-package'
754
- : record?.bytecodeVersion == null
755
- ? 'latest-version'
756
- : 'download';
757
698
  if (!record) {
758
699
  log((0, i18n_1.t)('hermesBaseNone', { reason: 'no published version yet' }));
759
700
  return null;
@@ -768,18 +709,18 @@ async function resolveHermesBase(params) {
768
709
  // 1. cache by bundleHash
769
710
  if (record.bundleHash) {
770
711
  const hit = await cacheLookup(record.bundleHash);
771
- if (hit && record.bytecodeVersion == null) {
772
- // the server does not know the HBC version (legacy version, native
773
- // package): the download path checks it, the cache path must too
774
- const version = await readFileHbcVersion(hit);
775
- if (version !== bytecodeVersion) {
776
- log((0, i18n_1.t)('hermesBaseNone', {
777
- reason: `cached base is HBC ${version ?? 'n/a'}, need ${bytecodeVersion}`,
778
- }));
779
- return null;
780
- }
781
- }
782
712
  if (hit) {
713
+ if (record.bytecodeVersion == null) {
714
+ // the server does not know the HBC version (legacy version, native
715
+ // package): the download path checks it, the cache path must too
716
+ const version = await readFileHbcVersion(hit);
717
+ if (version !== bytecodeVersion) {
718
+ log((0, i18n_1.t)('hermesBaseNone', {
719
+ reason: `cached base is HBC ${version ?? 'n/a'}, need ${bytecodeVersion}`,
720
+ }));
721
+ return null;
722
+ }
723
+ }
783
724
  log((0, i18n_1.t)('hermesBaseUsing', {
784
725
  source: `cache ${record.bundleHash.slice(0, 12)}${record.versionId == null ? '' : ` (version ${record.versionId})`}`,
785
726
  version: bytecodeVersion,
@@ -795,17 +736,17 @@ async function resolveHermesBase(params) {
795
736
  }
796
737
  }
797
738
  // 2. download, extract, verify, cache — transport errors get a second try
739
+ const artifactType = ['ppk', 'apk', 'ipa', 'app'].includes(record.artifactType ?? '')
740
+ ? record.artifactType
741
+ : 'ppk';
742
+ const source = record.artifactType && record.artifactType !== 'ppk'
743
+ ? 'native-package'
744
+ : record.bytecodeVersion == null
745
+ ? 'latest-version'
746
+ : 'download';
798
747
  for (let attempt = 1; attempt <= 2; attempt++) {
799
748
  const dir = tmpDir();
800
749
  await (0, fs_helpers_1.ensureDir)(dir);
801
- const artifactType = [
802
- 'ppk',
803
- 'apk',
804
- 'ipa',
805
- 'app',
806
- ].includes(record.artifactType ?? '')
807
- ? record.artifactType
808
- : 'ppk';
809
750
  const archive = path_1.default.join(dir, `base-${process.pid}-${Date.now()}-${attempt}.${artifactType}`);
810
751
  try {
811
752
  log((0, i18n_1.t)('hermesBaseDownloading', { url: record.url }));
@@ -1023,7 +964,7 @@ function normalizeDisassemblyLine(line, strings, literals) {
1023
964
  // wider form. Fold the suffix and the column padding that follows it.
1024
965
  if (opEnd > indent &&
1025
966
  (opEnd === line.length || isSpace(line.charCodeAt(opEnd)))) {
1026
- const folded = foldWidthSuffix(opcode);
967
+ const folded = (0, hermes_raw_1.foldWidthSuffix)(opcode);
1027
968
  line = `${line.slice(0, indent)}${folded}${normalizeOperandSpacing(line.slice(opEnd))}`;
1028
969
  // Switch jump tables sit after the instructions; their relative offset (and
1029
970
  // the table header hermesc prints for them) moves with instruction widths.
@@ -1095,14 +1036,6 @@ function isLetter(code) {
1095
1036
  return (code >= 0x41 && code <= 0x5a) || (code >= 0x61 && code <= 0x7a);
1096
1037
  }
1097
1038
  /** strip one operand-width suffix (LongIndex, Long, Short) off an opcode */
1098
- function foldWidthSuffix(opcode) {
1099
- for (const suffix of ['LongIndex', 'Long', 'Short']) {
1100
- if (opcode.length > suffix.length && opcode.endsWith(suffix)) {
1101
- return opcode.slice(0, -suffix.length);
1102
- }
1103
- }
1104
- return opcode;
1105
- }
1106
1039
  /** Split a readable stream into lines without readline (Bun's readline async iterator misbehaves when pulled manually). */
1107
1040
  async function* streamLines(stream) {
1108
1041
  const decoder = new node_string_decoder_1.StringDecoder('utf8');
@@ -1130,9 +1063,6 @@ const TRAILER_SECTION = /^(?:Debug |Textified callees table)/;
1130
1063
  const DUMP_STDERR_KEEP = 4 * 1024;
1131
1064
  /** a diagnostic line is cut to this many characters */
1132
1065
  const DETAIL_LINE_MAX = 120;
1133
- function isFunctionHeader(line) {
1134
- return /^(?:Function|NCFunction|Constructor)</.test(line);
1135
- }
1136
1066
  /**
1137
1067
  * Structured reader over one `hermesc -dump-bytecode -pretty-disassemble`
1138
1068
  * stream. Everything before the functions is either the string table (kept
@@ -1227,7 +1157,7 @@ class DumpReader {
1227
1157
  const line = await this.nextLine();
1228
1158
  if (line === null)
1229
1159
  return null;
1230
- if (isFunctionHeader(line)) {
1160
+ if ((0, hermes_raw_1.isFunctionHeader)(line)) {
1231
1161
  this.inFunctions = true;
1232
1162
  return line;
1233
1163
  }
@@ -1265,14 +1195,14 @@ class DumpReader {
1265
1195
  if (header === null)
1266
1196
  return null;
1267
1197
  // a non-function header here is a trailer section: no more functions
1268
- if (!isFunctionHeader(header))
1198
+ if (!(0, hermes_raw_1.isFunctionHeader)(header))
1269
1199
  return null;
1270
1200
  const lines = [];
1271
1201
  for (;;) {
1272
1202
  const line = await this.nextLine();
1273
1203
  if (line === null)
1274
1204
  break;
1275
- if (isFunctionHeader(line) || TRAILER_SECTION.test(line)) {
1205
+ if ((0, hermes_raw_1.isFunctionHeader)(line) || TRAILER_SECTION.test(line)) {
1276
1206
  this.pending = line;
1277
1207
  break;
1278
1208
  }
@@ -1390,16 +1320,23 @@ async function compareHermesBytecode(hermesCommand, withBase, plain, options = {
1390
1320
  signal: controller.signal,
1391
1321
  killSignal: 'SIGKILL',
1392
1322
  });
1393
- // The literal buffers are read from both files up front (header + three
1394
- // byte ranges). Only when both sides can be read do the instructions
1395
- // compare decoded literals; otherwise both fall back to the dumped buffers
1396
- // as a whole, so the two sides always normalize the same way.
1397
- const [buffersA, buffersB, dataA, dataB] = await Promise.all([
1398
- (0, hermes_literals_1.readLiteralBuffers)(withBase).catch(() => null),
1399
- (0, hermes_literals_1.readLiteralBuffers)(plain).catch(() => null),
1400
- (0, hermes_raw_1.readHermesSemanticData)(withBase).catch(() => null),
1401
- (0, hermes_raw_1.readHermesSemanticData)(plain).catch(() => null),
1402
- ]);
1323
+ // Each file is read once; its literal buffers and semantic data both come
1324
+ // from those bytes. Only when both sides' buffers can be read do the
1325
+ // instructions compare decoded literals; otherwise both fall back to the
1326
+ // dumped buffers as a whole, so the two sides always normalize the same way.
1327
+ const [bytesA, bytesB] = await Promise.all([withBase, plain].map((file) => fs_1.default.promises.readFile(file).catch(() => null)));
1328
+ const orNull = (read) => {
1329
+ try {
1330
+ return read();
1331
+ }
1332
+ catch {
1333
+ return null;
1334
+ }
1335
+ };
1336
+ const buffersA = bytesA && orNull(() => (0, hermes_literals_1.literalBuffersFromBytes)(bytesA));
1337
+ const buffersB = bytesB && orNull(() => (0, hermes_literals_1.literalBuffersFromBytes)(bytesB));
1338
+ const dataA = bytesA && orNull(() => (0, hermes_raw_1.hermesSemanticData)(bytesA));
1339
+ const dataB = bytesB && orNull(() => (0, hermes_raw_1.hermesSemanticData)(bytesB));
1403
1340
  const binary = buffersA && buffersB && buffersA.version === buffersB.version;
1404
1341
  const literals = binary ? 'instruction' : 'buffer';
1405
1342
  const a = new DumpReader(spawnDump(withBase), options.dumpTo?.withBase, binary ? buffersA : null, dataA?.strings);
@@ -25,10 +25,12 @@ export type LiteralBuffers = {
25
25
  shapes: Buffer;
26
26
  };
27
27
  /**
28
- * Read only the literal buffers of an HBC file (header + three byte ranges,
29
- * never the whole file). Null for a file whose layout is unknown: the caller
30
- * then falls back to comparing the dumped buffers as a whole.
28
+ * The literal buffers of an HBC file's bytes, as views into them. Null for a
29
+ * file whose layout is unknown: the caller then falls back to comparing the
30
+ * dumped buffers as a whole.
31
31
  */
32
+ export declare function literalBuffersFromBytes(bytes: Buffer): LiteralBuffers | null;
33
+ /** {@link literalBuffersFromBytes} for a file. */
32
34
  export declare function readLiteralBuffers(file: string): Promise<LiteralBuffers | null>;
33
35
  /**
34
36
  * Decode `count` literal values starting at byte `offset`. Null when the
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LiteralResolver = void 0;
4
+ exports.literalBuffersFromBytes = literalBuffersFromBytes;
4
5
  exports.readLiteralBuffers = readLiteralBuffers;
5
6
  exports.decodeSerializedLiterals = decodeSerializedLiterals;
6
7
  exports.renderLiteral = renderLiteral;
@@ -30,7 +31,7 @@ exports.renderLiteral = renderLiteral;
30
31
  * - HBC v87–96 (`split`): arrayBuffer, objKeyBuffer, objValueBuffer.
31
32
  * `NewArrayWithBuffer dst, sizeHint, count, arrayOffset`;
32
33
  * `NewObjectWithBuffer dst, sizeHint, count, keyOffset, valueOffset`.
33
- * - HBC v98 (`shaped`): literalValueBuffer (array elements and object values
34
+ * - HBC v98 and v99 (`shaped`): literalValueBuffer (array elements and object values
34
35
  * alike), objKeyBuffer, objShapeTable (8-byte entries: keyOffset u32,
35
36
  * numProps u32). `NewArrayWithBuffer dst, sizeHint, count, valueOffset`;
36
37
  * `NewObjectWithBuffer dst, shapeIndex, valueOffset` and
@@ -38,7 +39,6 @@ exports.renderLiteral = renderLiteral;
38
39
  */
39
40
  const promises_1 = require("node:fs/promises");
40
41
  const hbcTransform_1 = require("./hbcTransform");
41
- const HBC_HEADER_SIZE = 128;
42
42
  const TAG_TYPE_MASK = 0x70;
43
43
  const TAG_LENGTH_CONTINUES = 0x80;
44
44
  const TAG_LENGTH_LOW = 0x0f;
@@ -56,61 +56,38 @@ var Tag;
56
56
  })(Tag || (Tag = {}));
57
57
  const SHAPE_ENTRY_SIZE = 8;
58
58
  /**
59
- * Read only the literal buffers of an HBC file (header + three byte ranges,
60
- * never the whole file). Null for a file whose layout is unknown: the caller
61
- * then falls back to comparing the dumped buffers as a whole.
59
+ * The literal buffers of an HBC file's bytes, as views into them. Null for a
60
+ * file whose layout is unknown: the caller then falls back to comparing the
61
+ * dumped buffers as a whole.
62
62
  */
63
- async function readLiteralBuffers(file) {
64
- const handle = await (0, promises_1.open)(file, 'r');
65
- try {
66
- const { size } = await handle.stat();
67
- if (size < HBC_HEADER_SIZE)
68
- return null;
69
- const header = Buffer.alloc(HBC_HEADER_SIZE);
70
- const read = await handle.read(header, 0, HBC_HEADER_SIZE, 0);
71
- if (read.bytesRead !== HBC_HEADER_SIZE)
72
- return null;
73
- const resolved = (0, hbcTransform_1.resolveHbcSections)(header, size);
74
- if (!resolved)
75
- return null;
76
- const readRanges = async (names) => {
77
- const ranges = names.map((name) => resolved.sections.get(name));
78
- if (ranges.some((r) => !r))
79
- return null;
80
- const bufs = await Promise.all(ranges.map(async (r) => {
81
- const buf = Buffer.alloc(r.size);
82
- if (r.size === 0)
83
- return buf;
84
- const { bytesRead } = await handle.read(buf, 0, r.size, r.start);
85
- return bytesRead === r.size ? buf : null;
86
- }));
87
- return bufs.some((b) => !b) ? null : bufs;
88
- };
89
- const { version } = resolved;
90
- if (resolved.sections.has('objShapeTable')) {
91
- const bufs = await readRanges([
92
- 'literalValueBuffer',
93
- 'objKeyBuffer',
94
- 'objShapeTable',
95
- ]);
96
- if (!bufs)
97
- return null;
98
- const [values, objectKeys, shapes] = bufs;
99
- return { layout: 'shaped', version, values, objectKeys, shapes };
100
- }
101
- const bufs = await readRanges([
102
- 'arrayBuffer',
103
- 'objKeyBuffer',
104
- 'objValueBuffer',
105
- ]);
63
+ function literalBuffersFromBytes(bytes) {
64
+ const resolved = (0, hbcTransform_1.resolveHbcSections)(bytes, bytes.length);
65
+ if (!resolved)
66
+ return null;
67
+ // resolveHbcSections keeps every section inside the file
68
+ const views = (names) => {
69
+ const ranges = names.map((name) => resolved.sections.get(name));
70
+ return ranges.every(Boolean)
71
+ ? ranges.map((r) => bytes.subarray(r.start, r.start + r.size))
72
+ : null;
73
+ };
74
+ const { version } = resolved;
75
+ if (resolved.sections.has('objShapeTable')) {
76
+ const bufs = views(['literalValueBuffer', 'objKeyBuffer', 'objShapeTable']);
106
77
  if (!bufs)
107
78
  return null;
108
- const [array, objectKeys, objectValues] = bufs;
109
- return { layout: 'split', version, array, objectKeys, objectValues };
110
- }
111
- finally {
112
- await handle.close();
79
+ const [values, objectKeys, shapes] = bufs;
80
+ return { layout: 'shaped', version, values, objectKeys, shapes };
113
81
  }
82
+ const bufs = views(['arrayBuffer', 'objKeyBuffer', 'objValueBuffer']);
83
+ if (!bufs)
84
+ return null;
85
+ const [array, objectKeys, objectValues] = bufs;
86
+ return { layout: 'split', version, array, objectKeys, objectValues };
87
+ }
88
+ /** {@link literalBuffersFromBytes} for a file. */
89
+ async function readLiteralBuffers(file) {
90
+ return literalBuffersFromBytes(await (0, promises_1.readFile)(file));
114
91
  }
115
92
  /**
116
93
  * Decode `count` literal values starting at byte `offset`. Null when the
@@ -26,6 +26,13 @@ export interface HermesSemanticData {
26
26
  }
27
27
  /** Read exact strings (including UTF-16 code units) and function identities. */
28
28
  export declare function readHermesSemanticData(file: string): Promise<HermesSemanticData>;
29
+ /** {@link readHermesSemanticData} for HBC bytes already in memory. */
30
+ export declare function hermesSemanticData(bytes: Buffer): HermesSemanticData;
31
+ /** Remove encoding-width suffixes; operand values remain part of the audit. */
32
+ /** The opcode without its operand-width suffix (`GetByIdLong` → `GetById`). */
33
+ export declare function foldWidthSuffix(opcode: string): string;
34
+ /** The first line of a function in either dump. */
35
+ export declare function isFunctionHeader(line: string): boolean;
29
36
  /** Canonicalize one complete function; addresses become instruction ordinals. */
30
37
  export declare function normalizeRawHermesFunction(lines: string[], data: HermesSemanticData, functionIndex: number, buffers: LiteralBuffers): string[];
31
38
  export interface RawAuditResult {