react-native-update-cli 2.21.2 → 2.22.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/README.md +15 -7
- package/README.zh-CN.md +15 -7
- package/cli.json +18 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +22 -0
- package/lib/bin.js +2 -0
- package/lib/bundle-runner.d.ts +26 -1
- package/lib/bundle-runner.js +137 -14
- package/lib/bundle.d.ts +3 -0
- package/lib/bundle.js +38 -2
- package/lib/cache.d.ts +5 -0
- package/lib/cache.js +20 -0
- package/lib/locales/en.d.ts +9 -0
- package/lib/locales/en.js +9 -0
- package/lib/locales/zh.d.ts +9 -0
- package/lib/locales/zh.js +9 -0
- package/lib/package.js +7 -0
- package/lib/provider.js +2 -0
- package/lib/types.d.ts +4 -0
- package/lib/utils/hermes-base.d.ts +109 -0
- package/lib/utils/hermes-base.js +629 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +3 -0
- package/lib/versions.d.ts +3 -0
- package/lib/versions.js +28 -0
- package/package.json +1 -1
- package/src/api.ts +28 -0
- package/src/bin.ts +2 -0
- package/src/bundle-runner.ts +185 -21
- package/src/bundle.ts +56 -3
- package/src/cache.ts +20 -0
- package/src/locales/en.ts +14 -0
- package/src/locales/zh.ts +12 -0
- package/src/package.ts +7 -0
- package/src/provider.ts +2 -0
- package/src/types.ts +4 -0
- package/src/utils/hermes-base.ts +718 -0
- package/src/utils/index.ts +3 -0
- package/src/versions.ts +38 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
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 = void 0;
|
|
40
|
+
exports.classifyHermesCommand = classifyHermesCommand;
|
|
41
|
+
exports.sha256Hex = sha256Hex;
|
|
42
|
+
exports.probeHbcVersion = probeHbcVersion;
|
|
43
|
+
exports.extractBundleFromArchive = extractBundleFromArchive;
|
|
44
|
+
exports.cacheDir = cacheDir;
|
|
45
|
+
exports.tmpDir = tmpDir;
|
|
46
|
+
exports.cacheLimits = cacheLimits;
|
|
47
|
+
exports.cacheLookup = cacheLookup;
|
|
48
|
+
exports.cachePut = cachePut;
|
|
49
|
+
exports.enforceCacheLimits = enforceCacheLimits;
|
|
50
|
+
exports.cleanCache = cleanCache;
|
|
51
|
+
exports.cacheStats = cacheStats;
|
|
52
|
+
exports.cleanStaleTmp = cleanStaleTmp;
|
|
53
|
+
exports.downloadToFile = downloadToFile;
|
|
54
|
+
exports.resolveHermesBase = resolveHermesBase;
|
|
55
|
+
exports.hermescArgsWithBase = hermescArgsWithBase;
|
|
56
|
+
exports.hermesBaseMeta = hermesBaseMeta;
|
|
57
|
+
exports.normalizeDisassemblyLine = normalizeDisassemblyLine;
|
|
58
|
+
exports.verifyHermesBaseEquivalence = verifyHermesBaseEquivalence;
|
|
59
|
+
// Hermes `-base-bytecode` (delta optimizing mode) support.
|
|
60
|
+
//
|
|
61
|
+
// Hermes re-sorts its string table by identifier frequency on every compile, so
|
|
62
|
+
// any source change renumbers most string IDs and the bytecode diff explodes
|
|
63
|
+
// (see hermes-base-bytecode-方案.md). Compiling with the previous HBC of the
|
|
64
|
+
// same app as `-base-bytecode` keeps old IDs stable and shrinks patches 5-30×.
|
|
65
|
+
// This module resolves that base: gating by hermesc build, HBC version probe,
|
|
66
|
+
// server lookup, a sha256-named local cache, download + verification, and the
|
|
67
|
+
// optional disassembly equivalence check. Every failure degrades to the plain
|
|
68
|
+
// compile — nothing here may block a release.
|
|
69
|
+
const child_process_1 = require("child_process");
|
|
70
|
+
const crypto_1 = require("crypto");
|
|
71
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
72
|
+
const os_1 = __importDefault(require("os"));
|
|
73
|
+
const path_1 = __importDefault(require("path"));
|
|
74
|
+
const promises_1 = require("stream/promises");
|
|
75
|
+
const constants_1 = require("./constants");
|
|
76
|
+
const hbcTransform_1 = require("./hbcTransform");
|
|
77
|
+
const i18n_1 = require("./i18n");
|
|
78
|
+
const zip_entries_1 = require("./zip-entries");
|
|
79
|
+
// ---------------------------------------------------------------------------
|
|
80
|
+
// hermesc gating
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
/**
|
|
83
|
+
* Hermes V1 (static_h) builds before 2025-07-11 carried a delta-mode bug when a
|
|
84
|
+
* delta-produced base is reused; every released `hermes-compiler` is newer, and
|
|
85
|
+
* classic hermesc (HBC ≤96, react-native/sdks or hermes-engine) upgrades kinds
|
|
86
|
+
* in place and was never affected. Anything else is refused.
|
|
87
|
+
*/
|
|
88
|
+
exports.MIN_HERMES_COMPILER_BUILD = 250829098;
|
|
89
|
+
exports.MIN_HERMES_COMPILER_COMMITLY_DATE = 20250901;
|
|
90
|
+
function classifyHermesCommand(hermesCommand) {
|
|
91
|
+
const normalized = hermesCommand.split(path_1.default.sep).join('/');
|
|
92
|
+
if (/\/react-native\/sdks\/hermesc\//.test(normalized) ||
|
|
93
|
+
/\/hermes-engine\//.test(normalized)) {
|
|
94
|
+
return { allowed: true, kind: 'classic' };
|
|
95
|
+
}
|
|
96
|
+
const match = /\/hermes-compiler\//.exec(normalized);
|
|
97
|
+
if (match) {
|
|
98
|
+
const packageRoot = normalized.slice(0, match.index + match[0].length);
|
|
99
|
+
let version = '';
|
|
100
|
+
try {
|
|
101
|
+
version = String(JSON.parse(fs_extra_1.default.readFileSync(path_1.default.join(packageRoot, 'package.json'), 'utf8')).version ?? '');
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return {
|
|
105
|
+
allowed: false,
|
|
106
|
+
kind: 'hermes-compiler',
|
|
107
|
+
reason: 'unreadable hermes-compiler version',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const build = /^(\d{9})\./.exec(version);
|
|
111
|
+
if (build) {
|
|
112
|
+
const ok = Number(build[1]) >= exports.MIN_HERMES_COMPILER_BUILD;
|
|
113
|
+
return {
|
|
114
|
+
allowed: ok,
|
|
115
|
+
kind: 'hermes-compiler',
|
|
116
|
+
version,
|
|
117
|
+
reason: ok
|
|
118
|
+
? undefined
|
|
119
|
+
: `hermes-compiler ${version} predates the delta-mode fix`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const commitly = /^0\.(\d+)\.\d+-commitly-(\d{8})/.exec(version);
|
|
123
|
+
if (commitly) {
|
|
124
|
+
const ok = Number(commitly[1]) >= 14 &&
|
|
125
|
+
Number(commitly[2]) >= exports.MIN_HERMES_COMPILER_COMMITLY_DATE;
|
|
126
|
+
return {
|
|
127
|
+
allowed: ok,
|
|
128
|
+
kind: 'hermes-compiler',
|
|
129
|
+
version,
|
|
130
|
+
reason: ok
|
|
131
|
+
? undefined
|
|
132
|
+
: `hermes-compiler ${version} predates the delta-mode fix`,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const stable = /^0\.(\d+)\.\d+$/.exec(version);
|
|
136
|
+
if (stable) {
|
|
137
|
+
const ok = Number(stable[1]) >= 14;
|
|
138
|
+
return {
|
|
139
|
+
allowed: ok,
|
|
140
|
+
kind: 'hermes-compiler',
|
|
141
|
+
version,
|
|
142
|
+
reason: ok
|
|
143
|
+
? undefined
|
|
144
|
+
: `hermes-compiler ${version} predates the delta-mode fix`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
allowed: false,
|
|
149
|
+
kind: 'hermes-compiler',
|
|
150
|
+
version,
|
|
151
|
+
reason: `unrecognized hermes-compiler version ${version}`,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
allowed: false,
|
|
156
|
+
kind: 'unknown',
|
|
157
|
+
reason: 'hermesc location not recognized',
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
// HBC helpers
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
function sha256Hex(data) {
|
|
164
|
+
return (0, crypto_1.createHash)('sha256').update(data).digest('hex');
|
|
165
|
+
}
|
|
166
|
+
/** Compile an empty file to learn which HBC version this hermesc emits. */
|
|
167
|
+
function probeHbcVersion(hermesCommand) {
|
|
168
|
+
const dir = fs_extra_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'rnu-hermes-probe-'));
|
|
169
|
+
try {
|
|
170
|
+
const input = path_1.default.join(dir, 'probe.js');
|
|
171
|
+
const output = path_1.default.join(dir, 'probe.hbc');
|
|
172
|
+
fs_extra_1.default.writeFileSync(input, '');
|
|
173
|
+
const result = (0, child_process_1.spawnSync)(hermesCommand, ['-emit-binary', '-out', output, input, '-O', '-w'], { stdio: 'ignore' });
|
|
174
|
+
if (result.status !== 0 || !fs_extra_1.default.existsSync(output)) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const header = Buffer.alloc(128);
|
|
178
|
+
const fd = fs_extra_1.default.openSync(output, 'r');
|
|
179
|
+
try {
|
|
180
|
+
fs_extra_1.default.readSync(fd, header, 0, 128, 0);
|
|
181
|
+
}
|
|
182
|
+
finally {
|
|
183
|
+
fs_extra_1.default.closeSync(fd);
|
|
184
|
+
}
|
|
185
|
+
return (0, hbcTransform_1.getHbcVersion)(header);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
fs_extra_1.default.removeSync(dir);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.BUNDLE_ENTRY_NAMES = [
|
|
195
|
+
'index.bundlejs',
|
|
196
|
+
'main.jsbundle',
|
|
197
|
+
'bundle.harmony.js',
|
|
198
|
+
];
|
|
199
|
+
const APK_BUNDLE = /^assets\/index\.android\.bundle$/;
|
|
200
|
+
const IPA_BUNDLE = /^payload\/[^/]+\.app\/main\.jsbundle$/i;
|
|
201
|
+
/**
|
|
202
|
+
* Extract the JS/HBC bundle from a local artifact: a raw bundle file, a ppk,
|
|
203
|
+
* or a native package (apk / ipa / harmony app). Returns null when not found.
|
|
204
|
+
*/
|
|
205
|
+
async function extractBundleFromArchive(filePath) {
|
|
206
|
+
const ext = path_1.default.extname(filePath).toLowerCase();
|
|
207
|
+
if (ext === '.hbc' ||
|
|
208
|
+
ext === '.bundle' ||
|
|
209
|
+
ext === '.jsbundle' ||
|
|
210
|
+
ext === '.bundlejs' ||
|
|
211
|
+
ext === '.js') {
|
|
212
|
+
return fs_extra_1.default.readFile(filePath);
|
|
213
|
+
}
|
|
214
|
+
if (ext === '.ppk') {
|
|
215
|
+
return readFirstZipEntry(filePath, (name) => exports.BUNDLE_ENTRY_NAMES.includes(name));
|
|
216
|
+
}
|
|
217
|
+
if (ext === '.apk') {
|
|
218
|
+
return readFirstZipEntry(filePath, (name) => APK_BUNDLE.test(name));
|
|
219
|
+
}
|
|
220
|
+
if (ext === '.ipa') {
|
|
221
|
+
return readFirstZipEntry(filePath, (name) => IPA_BUNDLE.test(name.toLowerCase()));
|
|
222
|
+
}
|
|
223
|
+
if (ext === '.app') {
|
|
224
|
+
// harmony .app wraps .hap archives; reuse the existing parser
|
|
225
|
+
const { default: AppInfoParser } = await Promise.resolve().then(() => __importStar(require('./app-info-parser')));
|
|
226
|
+
const parser = new AppInfoParser(filePath);
|
|
227
|
+
const [bundle] = await parser.parser.getEntriesFromHarmonyApp([
|
|
228
|
+
/^resources\/rawfile\/bundle\.harmony\.js$/,
|
|
229
|
+
]);
|
|
230
|
+
return bundle ?? null;
|
|
231
|
+
}
|
|
232
|
+
// unknown extension: try as zip, then as raw file
|
|
233
|
+
const fromZip = await readFirstZipEntry(filePath, (name) => exports.BUNDLE_ENTRY_NAMES.includes(name)).catch(() => null);
|
|
234
|
+
return fromZip ?? fs_extra_1.default.readFile(filePath);
|
|
235
|
+
}
|
|
236
|
+
async function readFirstZipEntry(zipPath, matches) {
|
|
237
|
+
let found = null;
|
|
238
|
+
await (0, zip_entries_1.enumZipEntries)(zipPath, async (entry, zipFile) => {
|
|
239
|
+
if (!found && matches(entry.fileName)) {
|
|
240
|
+
found = await (0, zip_entries_1.readEntry)(entry, zipFile);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
return found;
|
|
244
|
+
}
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
// cache (sha256-named bundles)
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
exports.DEFAULT_CACHE_MAX_BYTES = 500 * 1024 * 1024;
|
|
249
|
+
exports.DEFAULT_CACHE_MAX_FILES = 20;
|
|
250
|
+
function cacheDir() {
|
|
251
|
+
return process.env.PUSHY_CACHE_DIR || path_1.default.join(constants_1.tempDir, 'cache');
|
|
252
|
+
}
|
|
253
|
+
function tmpDir() {
|
|
254
|
+
return path_1.default.join(constants_1.tempDir, 'tmp');
|
|
255
|
+
}
|
|
256
|
+
function cacheLimits(overrideMaxMb) {
|
|
257
|
+
const envMb = Number(process.env.PUSHY_CACHE_MAX_MB);
|
|
258
|
+
const maxBytes = overrideMaxMb && overrideMaxMb > 0
|
|
259
|
+
? overrideMaxMb * 1024 * 1024
|
|
260
|
+
: Number.isFinite(envMb) && envMb > 0
|
|
261
|
+
? envMb * 1024 * 1024
|
|
262
|
+
: exports.DEFAULT_CACHE_MAX_BYTES;
|
|
263
|
+
return { maxBytes, maxFiles: exports.DEFAULT_CACHE_MAX_FILES };
|
|
264
|
+
}
|
|
265
|
+
/** Returns the cached bundle path when present and its sha256 still matches. */
|
|
266
|
+
async function cacheLookup(bundleHash) {
|
|
267
|
+
if (!/^[0-9a-f]{64}$/.test(bundleHash))
|
|
268
|
+
return null;
|
|
269
|
+
const file = path_1.default.join(cacheDir(), bundleHash);
|
|
270
|
+
if (!(await fs_extra_1.default.pathExists(file)))
|
|
271
|
+
return null;
|
|
272
|
+
const data = await fs_extra_1.default.readFile(file);
|
|
273
|
+
if (sha256Hex(data) !== bundleHash) {
|
|
274
|
+
// corrupt entry: never trust cache content
|
|
275
|
+
await fs_extra_1.default.remove(file).catch(() => { });
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
const now = new Date();
|
|
279
|
+
await fs_extra_1.default.utimes(file, now, now).catch(() => { });
|
|
280
|
+
return file;
|
|
281
|
+
}
|
|
282
|
+
/** Store a bundle under its sha256 (LRU-evicting to the configured limits). */
|
|
283
|
+
async function cachePut(bundle, maxMb) {
|
|
284
|
+
const bundleHash = sha256Hex(bundle);
|
|
285
|
+
const dir = cacheDir();
|
|
286
|
+
await fs_extra_1.default.ensureDir(dir);
|
|
287
|
+
const file = path_1.default.join(dir, bundleHash);
|
|
288
|
+
if (!(await fs_extra_1.default.pathExists(file))) {
|
|
289
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
290
|
+
await fs_extra_1.default.writeFile(tmp, bundle);
|
|
291
|
+
await fs_extra_1.default.rename(tmp, file);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
const now = new Date();
|
|
295
|
+
await fs_extra_1.default.utimes(file, now, now).catch(() => { });
|
|
296
|
+
}
|
|
297
|
+
await enforceCacheLimits(maxMb, bundleHash);
|
|
298
|
+
return { path: file, bundleHash };
|
|
299
|
+
}
|
|
300
|
+
async function enforceCacheLimits(maxMb, keep) {
|
|
301
|
+
const dir = cacheDir();
|
|
302
|
+
if (!(await fs_extra_1.default.pathExists(dir)))
|
|
303
|
+
return;
|
|
304
|
+
const { maxBytes, maxFiles } = cacheLimits(maxMb);
|
|
305
|
+
const entries = [];
|
|
306
|
+
for (const name of await fs_extra_1.default.readdir(dir)) {
|
|
307
|
+
if (!/^[0-9a-f]{64}$/.test(name))
|
|
308
|
+
continue;
|
|
309
|
+
const stat = await fs_extra_1.default.stat(path_1.default.join(dir, name)).catch(() => null);
|
|
310
|
+
if (stat?.isFile())
|
|
311
|
+
entries.push({ name, size: stat.size, mtime: stat.mtimeMs });
|
|
312
|
+
}
|
|
313
|
+
entries.sort((a, b) => b.mtime - a.mtime); // newest first
|
|
314
|
+
let total = 0;
|
|
315
|
+
let count = 0;
|
|
316
|
+
for (const entry of entries) {
|
|
317
|
+
total += entry.size;
|
|
318
|
+
count += 1;
|
|
319
|
+
if (entry.name !== keep && (total > maxBytes || count > maxFiles)) {
|
|
320
|
+
await fs_extra_1.default.remove(path_1.default.join(dir, entry.name)).catch(() => { });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async function cleanCache() {
|
|
325
|
+
const dir = cacheDir();
|
|
326
|
+
if (!(await fs_extra_1.default.pathExists(dir)))
|
|
327
|
+
return 0;
|
|
328
|
+
const names = (await fs_extra_1.default.readdir(dir)).filter((n) => /^[0-9a-f]{64}$/.test(n));
|
|
329
|
+
for (const name of names)
|
|
330
|
+
await fs_extra_1.default.remove(path_1.default.join(dir, name));
|
|
331
|
+
return names.length;
|
|
332
|
+
}
|
|
333
|
+
async function cacheStats() {
|
|
334
|
+
const dir = cacheDir();
|
|
335
|
+
let files = 0;
|
|
336
|
+
let bytes = 0;
|
|
337
|
+
if (await fs_extra_1.default.pathExists(dir)) {
|
|
338
|
+
for (const name of await fs_extra_1.default.readdir(dir)) {
|
|
339
|
+
if (!/^[0-9a-f]{64}$/.test(name))
|
|
340
|
+
continue;
|
|
341
|
+
const stat = await fs_extra_1.default.stat(path_1.default.join(dir, name)).catch(() => null);
|
|
342
|
+
if (stat?.isFile()) {
|
|
343
|
+
files += 1;
|
|
344
|
+
bytes += stat.size;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return { dir, files, bytes };
|
|
349
|
+
}
|
|
350
|
+
/** Remove leftovers of interrupted runs (older than 24h) from the tmp dir. */
|
|
351
|
+
async function cleanStaleTmp(maxAgeMs = 24 * 60 * 60 * 1000) {
|
|
352
|
+
const dir = tmpDir();
|
|
353
|
+
if (!(await fs_extra_1.default.pathExists(dir)))
|
|
354
|
+
return;
|
|
355
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
356
|
+
for (const name of await fs_extra_1.default.readdir(dir)) {
|
|
357
|
+
const file = path_1.default.join(dir, name);
|
|
358
|
+
const stat = await fs_extra_1.default.stat(file).catch(() => null);
|
|
359
|
+
if (stat && stat.mtimeMs < cutoff)
|
|
360
|
+
await fs_extra_1.default.remove(file).catch(() => { });
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// ---------------------------------------------------------------------------
|
|
364
|
+
// download
|
|
365
|
+
// ---------------------------------------------------------------------------
|
|
366
|
+
async function downloadToFile(url, destination) {
|
|
367
|
+
const response = await fetch(url);
|
|
368
|
+
if (!response.ok || !response.body) {
|
|
369
|
+
throw new Error(`download failed: HTTP ${response.status} ${url}`);
|
|
370
|
+
}
|
|
371
|
+
await fs_extra_1.default.ensureDir(path_1.default.dirname(destination));
|
|
372
|
+
const { Readable } = await Promise.resolve().then(() => __importStar(require('stream')));
|
|
373
|
+
await (0, promises_1.pipeline)(Readable.fromWeb(response.body), fs_extra_1.default.createWriteStream(destination));
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Pick and materialize the base bundle for this compile. Returns null (with
|
|
377
|
+
* the reason logged) whenever a base cannot be used; callers then compile
|
|
378
|
+
* without one.
|
|
379
|
+
*/
|
|
380
|
+
async function resolveHermesBase(params) {
|
|
381
|
+
const log = params.log ?? (() => { });
|
|
382
|
+
const { option, bytecodeVersion } = params;
|
|
383
|
+
if (option === 'none') {
|
|
384
|
+
log((0, i18n_1.t)('hermesBaseNone', { reason: 'disabled by option' }));
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
if (option !== 'auto') {
|
|
388
|
+
// explicit local artifact
|
|
389
|
+
try {
|
|
390
|
+
const bundle = await extractBundleFromArchive(option);
|
|
391
|
+
if (!bundle)
|
|
392
|
+
throw new Error('bundle entry not found');
|
|
393
|
+
const version = (0, hbcTransform_1.getHbcVersion)(bundle);
|
|
394
|
+
if (version !== bytecodeVersion) {
|
|
395
|
+
throw new Error(`HBC version ${version ?? 'n/a'} != ${bytecodeVersion}`);
|
|
396
|
+
}
|
|
397
|
+
const cached = await cachePut(bundle, params.cacheMaxMb);
|
|
398
|
+
log((0, i18n_1.t)('hermesBaseUsing', { source: option, version: bytecodeVersion }));
|
|
399
|
+
return {
|
|
400
|
+
path: cached.path,
|
|
401
|
+
bytecodeVersion,
|
|
402
|
+
bundleHash: cached.bundleHash,
|
|
403
|
+
source: 'local',
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
log((0, i18n_1.t)('hermesBaseNone', {
|
|
408
|
+
reason: `${option}: ${error?.message ?? error}`,
|
|
409
|
+
}));
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (!params.appId) {
|
|
414
|
+
log((0, i18n_1.t)('hermesBaseNone', { reason: 'app not selected' }));
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
let record = null;
|
|
418
|
+
try {
|
|
419
|
+
record = await params.fetchBase(params.appId, bytecodeVersion);
|
|
420
|
+
}
|
|
421
|
+
catch (error) {
|
|
422
|
+
log((0, i18n_1.t)('hermesBaseNone', {
|
|
423
|
+
reason: `server lookup failed: ${error?.message ?? error}`,
|
|
424
|
+
}));
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
const source = record?.bytecodeVersion == null ? 'latest-version' : 'download';
|
|
428
|
+
if (!record) {
|
|
429
|
+
log((0, i18n_1.t)('hermesBaseNone', { reason: 'no published version yet' }));
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
if (record.bytecodeVersion != null &&
|
|
433
|
+
record.bytecodeVersion !== bytecodeVersion) {
|
|
434
|
+
log((0, i18n_1.t)('hermesBaseNone', {
|
|
435
|
+
reason: `server base is HBC ${record.bytecodeVersion}, need ${bytecodeVersion}`,
|
|
436
|
+
}));
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
// 1. cache by bundleHash
|
|
440
|
+
if (record.bundleHash) {
|
|
441
|
+
const hit = await cacheLookup(record.bundleHash);
|
|
442
|
+
if (hit) {
|
|
443
|
+
log((0, i18n_1.t)('hermesBaseUsing', {
|
|
444
|
+
source: `cache ${record.bundleHash.slice(0, 12)} (version ${record.versionId})`,
|
|
445
|
+
version: bytecodeVersion,
|
|
446
|
+
}));
|
|
447
|
+
return {
|
|
448
|
+
path: hit,
|
|
449
|
+
bytecodeVersion,
|
|
450
|
+
bundleHash: record.bundleHash,
|
|
451
|
+
versionId: record.versionId,
|
|
452
|
+
hash: record.hash,
|
|
453
|
+
source: 'cache',
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// 2. download, extract, verify, cache — up to two attempts
|
|
458
|
+
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
459
|
+
const dir = tmpDir();
|
|
460
|
+
await fs_extra_1.default.ensureDir(dir);
|
|
461
|
+
const archive = path_1.default.join(dir, `base-${process.pid}-${Date.now()}-${attempt}.ppk`);
|
|
462
|
+
try {
|
|
463
|
+
log((0, i18n_1.t)('hermesBaseDownloading', { url: record.url }));
|
|
464
|
+
await downloadToFile(record.url, archive);
|
|
465
|
+
const bundle = await extractBundleFromArchive(archive);
|
|
466
|
+
if (!bundle)
|
|
467
|
+
throw new Error('bundle entry not found in downloaded package');
|
|
468
|
+
const actualHash = sha256Hex(bundle);
|
|
469
|
+
if (record.bundleHash && actualHash !== record.bundleHash) {
|
|
470
|
+
throw new Error(`bundleHash mismatch (${actualHash.slice(0, 12)} != ${record.bundleHash.slice(0, 12)})`);
|
|
471
|
+
}
|
|
472
|
+
const version = (0, hbcTransform_1.getHbcVersion)(bundle);
|
|
473
|
+
if (version !== bytecodeVersion) {
|
|
474
|
+
log((0, i18n_1.t)('hermesBaseNone', {
|
|
475
|
+
reason: `downloaded base is HBC ${version ?? 'n/a'}, need ${bytecodeVersion}`,
|
|
476
|
+
}));
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
const cached = await cachePut(bundle, params.cacheMaxMb);
|
|
480
|
+
log((0, i18n_1.t)('hermesBaseUsing', {
|
|
481
|
+
source: `version ${record.versionId} ${record.hash.slice(0, 8)}`,
|
|
482
|
+
version: bytecodeVersion,
|
|
483
|
+
}));
|
|
484
|
+
return {
|
|
485
|
+
path: cached.path,
|
|
486
|
+
bytecodeVersion,
|
|
487
|
+
bundleHash: cached.bundleHash,
|
|
488
|
+
versionId: record.versionId,
|
|
489
|
+
hash: record.hash,
|
|
490
|
+
source,
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
catch (error) {
|
|
494
|
+
if (attempt === 2) {
|
|
495
|
+
log((0, i18n_1.t)('hermesBaseNone', {
|
|
496
|
+
reason: `download/verify failed: ${error?.message ?? error}`,
|
|
497
|
+
}));
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
finally {
|
|
502
|
+
await fs_extra_1.default.remove(archive).catch(() => { });
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
function hermescArgsWithBase(baseArgs, basePath) {
|
|
508
|
+
return basePath ? [...baseArgs, `-base-bytecode=${basePath}`] : baseArgs;
|
|
509
|
+
}
|
|
510
|
+
function hermesBaseMeta(selection, bytecodeVersion) {
|
|
511
|
+
return {
|
|
512
|
+
bytecodeVersion,
|
|
513
|
+
baseVersionId: selection?.versionId ?? null,
|
|
514
|
+
baseHash: selection?.hash ?? null,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
// ---------------------------------------------------------------------------
|
|
518
|
+
// equivalence check (streams two `-dump-bytecode` outputs, never writes them)
|
|
519
|
+
// ---------------------------------------------------------------------------
|
|
520
|
+
const STRING_TABLE_LINE = /^\s*[is](\d+)\[[^\]]*\](?: #[0-9A-F]+)?: (.*)$/;
|
|
521
|
+
/**
|
|
522
|
+
* Normalize one disassembly line so that a delta-mode compile and a plain
|
|
523
|
+
* compile of the same source compare equal: string IDs are renumbered, literal
|
|
524
|
+
* buffers are laid out differently (offsets, short/long variants), and jump
|
|
525
|
+
* distances follow instruction widths. Everything else must match exactly.
|
|
526
|
+
*/
|
|
527
|
+
function normalizeDisassemblyLine(line, strings) {
|
|
528
|
+
if (/^Offset in debug table/.test(line))
|
|
529
|
+
return null;
|
|
530
|
+
let m = /^(\s*New(?:Array|Object)WithBuffer)(?:Long)?(?:AndParent)?\s+(r\d+)(.*)$/.exec(line);
|
|
531
|
+
if (m) {
|
|
532
|
+
const nums = m[3].match(/\d+/g) ?? [];
|
|
533
|
+
return `${m[1]} ${m[2]} sizes=${nums.slice(0, 1).join(',')}`;
|
|
534
|
+
}
|
|
535
|
+
m = /^(\s*J[A-Za-z]+?)(Long)?\s+(L\d+|\d+)(.*)$/.exec(line);
|
|
536
|
+
if (m)
|
|
537
|
+
return `${m[1]} <tgt>${m[4]}`;
|
|
538
|
+
m = /^(\s*DefineOwnById\w*\s+r\d+, r\d+, \d+, )(\d+)$/.exec(line);
|
|
539
|
+
if (m)
|
|
540
|
+
return `${m[1]}"${strings.get(Number(m[2])) ?? `?${m[2]}`}"`;
|
|
541
|
+
return line;
|
|
542
|
+
}
|
|
543
|
+
/** Split a readable stream into lines without readline (Bun's readline async iterator misbehaves when pulled manually). */
|
|
544
|
+
async function* streamLines(stream) {
|
|
545
|
+
let rest = '';
|
|
546
|
+
for await (const chunk of stream) {
|
|
547
|
+
rest += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
548
|
+
let index = rest.indexOf('\n');
|
|
549
|
+
while (index >= 0) {
|
|
550
|
+
yield rest.slice(0, index);
|
|
551
|
+
rest = rest.slice(index + 1);
|
|
552
|
+
index = rest.indexOf('\n');
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (rest)
|
|
556
|
+
yield rest;
|
|
557
|
+
}
|
|
558
|
+
class DumpReader {
|
|
559
|
+
proc;
|
|
560
|
+
iterator;
|
|
561
|
+
strings = new Map();
|
|
562
|
+
inFunctions = false;
|
|
563
|
+
finished = false;
|
|
564
|
+
constructor(proc) {
|
|
565
|
+
this.proc = proc;
|
|
566
|
+
this.iterator = streamLines(proc.stdout);
|
|
567
|
+
}
|
|
568
|
+
/** next normalized function-body line, or null at end */
|
|
569
|
+
async next() {
|
|
570
|
+
if (this.finished)
|
|
571
|
+
return null;
|
|
572
|
+
for (;;) {
|
|
573
|
+
const { value, done } = await this.iterator.next();
|
|
574
|
+
if (done) {
|
|
575
|
+
this.finished = true;
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
const line = value;
|
|
579
|
+
if (!this.inFunctions) {
|
|
580
|
+
if (line.startsWith('Function<')) {
|
|
581
|
+
this.inFunctions = true;
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
const m = STRING_TABLE_LINE.exec(line);
|
|
585
|
+
if (m)
|
|
586
|
+
this.strings.set(Number(m[1]), m[2]);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (line.trim() === '')
|
|
591
|
+
continue;
|
|
592
|
+
const normalized = normalizeDisassemblyLine(line, this.strings);
|
|
593
|
+
if (normalized !== null)
|
|
594
|
+
return normalized;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Compare two HBC files by disassembly (see normalizeDisassemblyLine). `-b`
|
|
600
|
+
* forces hermesc to treat inputs as bytecode whatever their extension. Both
|
|
601
|
+
* hermesc dumps are consumed as streams so the ~100 MB of text never touches
|
|
602
|
+
* the disk. Resolves true when equivalent.
|
|
603
|
+
*/
|
|
604
|
+
async function verifyHermesBaseEquivalence(hermesCommand, withBase, plain) {
|
|
605
|
+
const spawnDump = (file) => (0, child_process_1.spawn)(hermesCommand, ['-b', '-dump-bytecode', '-pretty-disassemble', file], {
|
|
606
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
607
|
+
});
|
|
608
|
+
const a = new DumpReader(spawnDump(withBase));
|
|
609
|
+
const b = new DumpReader(spawnDump(plain));
|
|
610
|
+
let equal = true;
|
|
611
|
+
let lines = 0;
|
|
612
|
+
try {
|
|
613
|
+
for (;;) {
|
|
614
|
+
const [la, lb] = await Promise.all([a.next(), b.next()]);
|
|
615
|
+
if (la === null && lb === null)
|
|
616
|
+
break;
|
|
617
|
+
if (la !== lb) {
|
|
618
|
+
equal = false;
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
lines++;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
finally {
|
|
625
|
+
a.proc.kill();
|
|
626
|
+
b.proc.kill();
|
|
627
|
+
}
|
|
628
|
+
return equal && lines > 0;
|
|
629
|
+
}
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -7,17 +7,20 @@ export declare function getApkInfo(fn: string): Promise<{
|
|
|
7
7
|
versionName: string;
|
|
8
8
|
buildTime: number;
|
|
9
9
|
bundleHash: string;
|
|
10
|
+
bundleFile: Blob | Buffer<ArrayBufferLike>;
|
|
10
11
|
}>;
|
|
11
12
|
export declare function getAppInfo(fn: string): Promise<{
|
|
12
13
|
versionName: string | undefined;
|
|
13
14
|
buildTime: number;
|
|
14
15
|
bundleHash: string;
|
|
16
|
+
bundleFile: Buffer<ArrayBufferLike>;
|
|
15
17
|
}>;
|
|
16
18
|
export declare const IpaBundleFileName: RegExp;
|
|
17
19
|
export declare function getIpaInfo(fn: string): Promise<{
|
|
18
20
|
versionName: string;
|
|
19
21
|
buildTime: string;
|
|
20
22
|
bundleHash: string;
|
|
23
|
+
bundleFile: Blob | Buffer<ArrayBufferLike>;
|
|
21
24
|
}>;
|
|
22
25
|
export declare function getAabInfo(fn: string): Promise<{
|
|
23
26
|
versionName: string;
|
package/lib/utils/index.js
CHANGED
|
@@ -130,6 +130,7 @@ async function getApkInfo(fn) {
|
|
|
130
130
|
versionName,
|
|
131
131
|
buildTime,
|
|
132
132
|
bundleHash: await sha256(bundleFile),
|
|
133
|
+
bundleFile,
|
|
133
134
|
...appCredential,
|
|
134
135
|
};
|
|
135
136
|
}
|
|
@@ -167,6 +168,7 @@ async function getAppInfo(fn) {
|
|
|
167
168
|
versionName,
|
|
168
169
|
buildTime,
|
|
169
170
|
bundleHash: await sha256(bundleFile),
|
|
171
|
+
bundleFile,
|
|
170
172
|
...appCredential,
|
|
171
173
|
};
|
|
172
174
|
}
|
|
@@ -209,6 +211,7 @@ async function getIpaInfo(fn) {
|
|
|
209
211
|
versionName,
|
|
210
212
|
buildTime,
|
|
211
213
|
bundleHash: await sha256(bundleFile),
|
|
214
|
+
bundleFile,
|
|
212
215
|
...appCredential,
|
|
213
216
|
};
|
|
214
217
|
}
|
package/lib/versions.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Package, Platform, Version } from './types';
|
|
2
|
+
import { type HermesBaseMeta } from './utils/hermes-base';
|
|
2
3
|
interface VersionCommandOptions {
|
|
3
4
|
[key: string]: unknown;
|
|
4
5
|
config?: string;
|
|
@@ -19,6 +20,8 @@ interface VersionCommandOptions {
|
|
|
19
20
|
dryRun?: boolean;
|
|
20
21
|
versionDeps?: Record<string, string>;
|
|
21
22
|
warnDepsChanges?: boolean;
|
|
23
|
+
/** internal: chain metadata from the bundle step (not a CLI flag) */
|
|
24
|
+
hermesBase?: HermesBaseMeta;
|
|
22
25
|
'no-interactive'?: boolean | string;
|
|
23
26
|
}
|
|
24
27
|
type Deps = Record<string, string>;
|