react-native-update-cli 2.22.7 → 2.22.9
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 +1 -1
- package/README.zh-CN.md +1 -1
- package/cli.json +5 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +106 -38
- package/lib/app.js +3 -2
- package/lib/bin.js +16 -2
- package/lib/bundle-runner.d.ts +11 -4
- package/lib/bundle-runner.js +176 -90
- package/lib/bundle.d.ts +1 -0
- package/lib/bundle.js +2 -0
- package/lib/diff.d.ts +9 -0
- package/lib/diff.js +162 -120
- package/lib/locales/en.d.ts +1 -0
- package/lib/locales/en.js +1 -0
- package/lib/locales/zh.d.ts +1 -0
- package/lib/locales/zh.js +1 -0
- package/lib/native-package.d.ts +5 -1
- package/lib/native-package.js +145 -84
- package/lib/package.d.ts +7 -3
- package/lib/package.js +22 -12
- package/lib/types.d.ts +2 -0
- package/lib/utils/dep-versions.d.ts +7 -0
- package/lib/utils/dep-versions.js +66 -31
- package/lib/utils/hbcTransform.d.ts +21 -0
- package/lib/utils/hbcTransform.js +146 -5
- package/lib/utils/hermes-base.d.ts +21 -4
- package/lib/utils/hermes-base.js +146 -37
- package/lib/utils/http-helper.d.ts +6 -1
- package/lib/utils/http-helper.js +23 -11
- package/lib/utils/index.d.ts +30 -1
- package/lib/utils/index.js +98 -24
- package/lib/utils/latest-version/index.d.ts +11 -8
- package/lib/utils/latest-version/index.js +91 -25
- package/lib/utils/zip-entries.js +20 -15
- package/lib/utils/zip-options.d.ts +2 -0
- package/lib/utils/zip-options.js +9 -4
- package/lib/utils/zip-range.d.ts +47 -15
- package/lib/utils/zip-range.js +192 -83
- package/lib/utils/zip-raw-writer.d.ts +47 -0
- package/lib/utils/zip-raw-writer.js +204 -0
- package/lib/versions.js +19 -9
- package/package.json +10 -12
- package/src/api.ts +126 -46
- package/src/app.ts +3 -2
- package/src/bin.ts +17 -2
- package/src/bundle-runner.ts +225 -113
- package/src/bundle.ts +3 -0
- package/src/diff.ts +210 -153
- package/src/locales/en.ts +2 -0
- package/src/locales/zh.ts +2 -0
- package/src/native-package.ts +186 -96
- package/src/package.ts +26 -13
- package/src/types.ts +2 -0
- package/src/utils/dep-versions.ts +76 -33
- package/src/utils/hbcTransform.ts +194 -5
- package/src/utils/hermes-base.ts +156 -45
- package/src/utils/http-helper.ts +24 -13
- package/src/utils/index.ts +136 -26
- package/src/utils/latest-version/index.ts +109 -33
- package/src/utils/zip-entries.ts +18 -16
- package/src/utils/zip-options.ts +8 -3
- package/src/utils/zip-range.ts +244 -88
- package/src/utils/zip-raw-writer.ts +247 -0
- package/src/versions.ts +17 -7
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
* (只要两侧使用同一份描述表)。
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
+
import { copyFile, open as openFile, rm, stat } from 'node:fs/promises';
|
|
19
|
+
|
|
18
20
|
const HBC_MAGIC = Buffer.from([0xc6, 0x1f, 0xbc, 0x03, 0xc1, 0x03, 0x19, 0x1f]);
|
|
19
21
|
const HEADER_COUNTS_OFFSET = 32; // magic(8) + version(4) + sourceHash(20)
|
|
20
22
|
const HBC_HEADER_SIZE = 128;
|
|
@@ -308,21 +310,39 @@ const align4 = (x: number) => (x + 3) & ~3;
|
|
|
308
310
|
function resolveSections(
|
|
309
311
|
buf: Buffer,
|
|
310
312
|
layout: HbcLayout,
|
|
313
|
+
): ResolvedSection[] | null {
|
|
314
|
+
return resolveSectionsFromHeader(buf, buf.length, layout);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 只依赖头部 128 字节与文件总长即可解析段边界(表偏移全部来自头部计数),
|
|
319
|
+
* 文件版变换据此无需把整个 bundle 读进内存。
|
|
320
|
+
*/
|
|
321
|
+
function resolveSectionsFromHeader(
|
|
322
|
+
header: Buffer,
|
|
323
|
+
fileLength: number,
|
|
324
|
+
layout: HbcLayout,
|
|
311
325
|
): ResolvedSection[] | null {
|
|
312
326
|
const headerEnd = HEADER_COUNTS_OFFSET + layout.headerFields.length * 4;
|
|
313
|
-
if (
|
|
327
|
+
if (
|
|
328
|
+
header.length < HBC_HEADER_SIZE ||
|
|
329
|
+
fileLength < HBC_HEADER_SIZE ||
|
|
330
|
+
headerEnd > HBC_HEADER_SIZE
|
|
331
|
+
) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
314
334
|
|
|
315
335
|
const counts: Record<string, number> = {};
|
|
316
336
|
layout.headerFields.forEach((name, i) => {
|
|
317
|
-
counts[name] =
|
|
337
|
+
counts[name] = header.readUInt32LE(HEADER_COUNTS_OFFSET + i * 4);
|
|
318
338
|
});
|
|
319
339
|
|
|
320
|
-
if (counts.fileLength !==
|
|
340
|
+
if (counts.fileLength !== fileLength) return null;
|
|
321
341
|
const debugInfoOffset = counts.debugInfoOffset;
|
|
322
342
|
if (
|
|
323
343
|
!Number.isInteger(debugInfoOffset) ||
|
|
324
344
|
debugInfoOffset < HBC_HEADER_SIZE ||
|
|
325
|
-
debugInfoOffset >
|
|
345
|
+
debugInfoOffset > fileLength
|
|
326
346
|
) {
|
|
327
347
|
return null;
|
|
328
348
|
}
|
|
@@ -409,10 +429,134 @@ export function transformHbcWithLayout(
|
|
|
409
429
|
const sections = resolveSections(buf, layout);
|
|
410
430
|
if (!sections) return null;
|
|
411
431
|
const out = Buffer.from(buf);
|
|
432
|
+
applyDeltaSections(out, sections, inverse);
|
|
433
|
+
return out;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* 原地变换:直接改写 buf,不再分配整块副本。调用方须持有 buf 的所有权
|
|
438
|
+
* (例如 patch() 刚返回的新 Buffer)。结构不匹配时返回 false 且 buf 不变。
|
|
439
|
+
*/
|
|
440
|
+
export function transformHbcWithLayoutInPlace(
|
|
441
|
+
buf: Buffer,
|
|
442
|
+
layout: HbcLayout,
|
|
443
|
+
inverse: boolean,
|
|
444
|
+
): boolean {
|
|
445
|
+
const sections = resolveSections(buf, layout);
|
|
446
|
+
if (!sections) return false;
|
|
447
|
+
applyDeltaSections(buf, sections, inverse);
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function applyDeltaSections(
|
|
452
|
+
out: Buffer,
|
|
453
|
+
sections: ResolvedSection[],
|
|
454
|
+
inverse: boolean,
|
|
455
|
+
): void {
|
|
412
456
|
for (const s of sections) {
|
|
413
457
|
if (s.desc.deltaFields?.length) applyDelta(out, s, inverse);
|
|
414
458
|
}
|
|
415
|
-
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
type HbcFileHeader = { header: Buffer; size: number };
|
|
462
|
+
|
|
463
|
+
/** 读取文件头 128 字节与总长;文件过短或非 HBC 时返回 null。 */
|
|
464
|
+
async function readHbcFileHeader(
|
|
465
|
+
filePath: string,
|
|
466
|
+
): Promise<HbcFileHeader | null> {
|
|
467
|
+
const { size } = await stat(filePath);
|
|
468
|
+
if (size < HBC_HEADER_SIZE) return null;
|
|
469
|
+
const header = Buffer.alloc(HBC_HEADER_SIZE);
|
|
470
|
+
const handle = await openFile(filePath, 'r');
|
|
471
|
+
try {
|
|
472
|
+
await readExact(handle, header, 0);
|
|
473
|
+
} finally {
|
|
474
|
+
await handle.close();
|
|
475
|
+
}
|
|
476
|
+
if (getHbcVersion(header) === null) return null;
|
|
477
|
+
return { header, size };
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
type FileHandleLike = Awaited<ReturnType<typeof openFile>>;
|
|
481
|
+
|
|
482
|
+
async function readExact(
|
|
483
|
+
handle: FileHandleLike,
|
|
484
|
+
buffer: Buffer,
|
|
485
|
+
position: number,
|
|
486
|
+
): Promise<void> {
|
|
487
|
+
let done = 0;
|
|
488
|
+
while (done < buffer.length) {
|
|
489
|
+
const { bytesRead } = await handle.read(
|
|
490
|
+
buffer,
|
|
491
|
+
done,
|
|
492
|
+
buffer.length - done,
|
|
493
|
+
position + done,
|
|
494
|
+
);
|
|
495
|
+
if (bytesRead <= 0) {
|
|
496
|
+
throw new Error('unexpected end of file while reading HBC section');
|
|
497
|
+
}
|
|
498
|
+
done += bytesRead;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
async function writeExact(
|
|
503
|
+
handle: FileHandleLike,
|
|
504
|
+
buffer: Buffer,
|
|
505
|
+
position: number,
|
|
506
|
+
): Promise<void> {
|
|
507
|
+
let done = 0;
|
|
508
|
+
while (done < buffer.length) {
|
|
509
|
+
const { bytesWritten } = await handle.write(
|
|
510
|
+
buffer,
|
|
511
|
+
done,
|
|
512
|
+
buffer.length - done,
|
|
513
|
+
position + done,
|
|
514
|
+
);
|
|
515
|
+
if (bytesWritten <= 0) {
|
|
516
|
+
throw new Error('short write while rewriting HBC section');
|
|
517
|
+
}
|
|
518
|
+
done += bytesWritten;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* 文件版 transformHbcWithLayout:把 src 复制到 dst,再仅对含差分字段的
|
|
524
|
+
* 段(位于 [128, 最后一段末尾) 区间内)做定位读写改写。任一时刻内存中
|
|
525
|
+
* 只有一个段的内容,不会持有整个 bundle。输出与内存版逐字节一致。
|
|
526
|
+
* 结构不匹配时返回 false 并清理 dst。
|
|
527
|
+
*/
|
|
528
|
+
export async function transformHbcFile(
|
|
529
|
+
src: string,
|
|
530
|
+
dst: string,
|
|
531
|
+
layout: HbcLayout,
|
|
532
|
+
inverse: boolean,
|
|
533
|
+
): Promise<boolean> {
|
|
534
|
+
const info = await readHbcFileHeader(src);
|
|
535
|
+
const sections = info
|
|
536
|
+
? resolveSectionsFromHeader(info.header, info.size, layout)
|
|
537
|
+
: null;
|
|
538
|
+
if (!sections) {
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
await copyFile(src, dst);
|
|
542
|
+
try {
|
|
543
|
+
const handle = await openFile(dst, 'r+');
|
|
544
|
+
try {
|
|
545
|
+
for (const s of sections) {
|
|
546
|
+
if (!s.desc.deltaFields?.length || s.size === 0) continue;
|
|
547
|
+
const chunk = Buffer.alloc(s.size);
|
|
548
|
+
await readExact(handle, chunk, s.start);
|
|
549
|
+
applyDelta(chunk, { start: 0, size: s.size, desc: s.desc }, inverse);
|
|
550
|
+
await writeExact(handle, chunk, s.start);
|
|
551
|
+
}
|
|
552
|
+
} finally {
|
|
553
|
+
await handle.close();
|
|
554
|
+
}
|
|
555
|
+
} catch (error) {
|
|
556
|
+
await rm(dst, { force: true });
|
|
557
|
+
throw error;
|
|
558
|
+
}
|
|
559
|
+
return true;
|
|
416
560
|
}
|
|
417
561
|
|
|
418
562
|
export const HBC_TRANSFORM_VERSION = 1;
|
|
@@ -458,3 +602,48 @@ export function tryTransformPair(
|
|
|
458
602
|
}
|
|
459
603
|
return null;
|
|
460
604
|
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* tryTransformPair 的文件版:只读两侧头部即可选定布局,变换结果直接写到
|
|
608
|
+
* tOldPath / tNewPath,全程不把 bundle 读进内存。无可用布局时返回 null
|
|
609
|
+
* (不产生输出文件)。
|
|
610
|
+
*/
|
|
611
|
+
export async function tryTransformPairFiles(
|
|
612
|
+
oldPath: string,
|
|
613
|
+
newPath: string,
|
|
614
|
+
tOldPath: string,
|
|
615
|
+
tNewPath: string,
|
|
616
|
+
): Promise<{ layout: HbcLayout; meta: HbcTransformMeta } | null> {
|
|
617
|
+
const [oldInfo, newInfo] = await Promise.all([
|
|
618
|
+
readHbcFileHeader(oldPath),
|
|
619
|
+
readHbcFileHeader(newPath),
|
|
620
|
+
]);
|
|
621
|
+
if (!oldInfo || !newInfo) return null;
|
|
622
|
+
const oldVersion = getHbcVersion(oldInfo.header);
|
|
623
|
+
const newVersion = getHbcVersion(newInfo.header);
|
|
624
|
+
if (oldVersion === null || newVersion === null || oldVersion !== newVersion) {
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
for (const layout of findLayouts(newVersion)) {
|
|
628
|
+
if (
|
|
629
|
+
!resolveSectionsFromHeader(oldInfo.header, oldInfo.size, layout) ||
|
|
630
|
+
!resolveSectionsFromHeader(newInfo.header, newInfo.size, layout)
|
|
631
|
+
) {
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
const [tOld, tNew] = await Promise.all([
|
|
635
|
+
transformHbcFile(oldPath, tOldPath, layout, false),
|
|
636
|
+
transformHbcFile(newPath, tNewPath, layout, false),
|
|
637
|
+
]);
|
|
638
|
+
if (!tOld || !tNew) continue;
|
|
639
|
+
return {
|
|
640
|
+
layout,
|
|
641
|
+
meta: {
|
|
642
|
+
v: HBC_TRANSFORM_VERSION,
|
|
643
|
+
hbcVersion: newVersion,
|
|
644
|
+
layout: compileLayoutToWire(layout),
|
|
645
|
+
},
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
return null;
|
|
649
|
+
}
|
package/src/utils/hermes-base.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { createHash } from 'crypto';
|
|
|
13
13
|
import fs from 'fs-extra';
|
|
14
14
|
import os from 'os';
|
|
15
15
|
import path from 'path';
|
|
16
|
+
import { PassThrough, Readable } from 'stream';
|
|
16
17
|
import { pipeline } from 'stream/promises';
|
|
17
18
|
import { tempDir } from './constants';
|
|
18
19
|
import { getHbcVersion } from './hbcTransform';
|
|
@@ -29,6 +30,27 @@ import {
|
|
|
29
30
|
|
|
30
31
|
export type HermesBaseOption = 'auto' | 'none' | string;
|
|
31
32
|
|
|
33
|
+
/**
|
|
34
|
+
* The base cannot be obtained whatever the transport or how often it is
|
|
35
|
+
* retried (wrong content on the server, no bundle in the package, ...).
|
|
36
|
+
*/
|
|
37
|
+
export class PermanentBaseError extends Error {}
|
|
38
|
+
/** The fetched bundle's sha256 differs from what the server recorded. */
|
|
39
|
+
export class BundleHashMismatchError extends PermanentBaseError {
|
|
40
|
+
constructor(actual: string, expected: string) {
|
|
41
|
+
super(
|
|
42
|
+
`bundleHash mismatch (${actual.slice(0, 12)} != ${expected.slice(0, 12)})`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** time allowed for a response to start (headers) */
|
|
48
|
+
const FETCH_HEADERS_TIMEOUT_MS = 30_000;
|
|
49
|
+
/** a download that receives nothing for this long is abandoned */
|
|
50
|
+
const STREAM_IDLE_TIMEOUT_MS = 60_000;
|
|
51
|
+
/** cachePut leftovers (`<hash>.<pid>.tmp`) older than this are evicted */
|
|
52
|
+
const STALE_CACHE_TMP_MS = 60 * 60 * 1000;
|
|
53
|
+
|
|
32
54
|
export interface HermesBaseSelection {
|
|
33
55
|
/** path of the bundle (HBC) handed to hermesc as -base-bytecode */
|
|
34
56
|
path: string;
|
|
@@ -151,6 +173,27 @@ export function sha256Hex(data: Buffer): string {
|
|
|
151
173
|
return createHash('sha256').update(data).digest('hex');
|
|
152
174
|
}
|
|
153
175
|
|
|
176
|
+
/** sha256 of a file, streamed (a cached base can be tens of MB) */
|
|
177
|
+
export async function sha256File(file: string): Promise<string> {
|
|
178
|
+
const hash = createHash('sha256');
|
|
179
|
+
for await (const chunk of fs.createReadStream(file)) hash.update(chunk);
|
|
180
|
+
return hash.digest('hex');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** HBC version from the first bytes of a file, without reading the rest */
|
|
184
|
+
export async function readFileHbcVersion(file: string): Promise<number | null> {
|
|
185
|
+
const header = Buffer.alloc(128);
|
|
186
|
+
const fd = await fs.open(file, 'r');
|
|
187
|
+
try {
|
|
188
|
+
const { bytesRead } = await fs.read(fd, header, 0, 128, 0);
|
|
189
|
+
return getHbcVersion(header.subarray(0, bytesRead));
|
|
190
|
+
} catch {
|
|
191
|
+
return null;
|
|
192
|
+
} finally {
|
|
193
|
+
await fs.close(fd);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
154
197
|
const PROBE_CACHE_FILE = 'hbc-versions.json';
|
|
155
198
|
const PROBE_CACHE_ENTRIES = 16;
|
|
156
199
|
|
|
@@ -348,8 +391,7 @@ export async function cacheLookup(bundleHash: string): Promise<string | null> {
|
|
|
348
391
|
if (!/^[0-9a-f]{64}$/.test(bundleHash)) return null;
|
|
349
392
|
const file = path.join(cacheDir(), bundleHash);
|
|
350
393
|
if (!(await fs.pathExists(file))) return null;
|
|
351
|
-
|
|
352
|
-
if (sha256Hex(data) !== bundleHash) {
|
|
394
|
+
if ((await sha256File(file).catch(() => '')) !== bundleHash) {
|
|
353
395
|
// corrupt entry: never trust cache content
|
|
354
396
|
await fs.remove(file).catch(() => {});
|
|
355
397
|
return null;
|
|
@@ -389,11 +431,19 @@ export async function enforceCacheLimits(
|
|
|
389
431
|
if (!(await fs.pathExists(dir))) return;
|
|
390
432
|
const { maxBytes, maxFiles } = cacheLimits(maxMb);
|
|
391
433
|
const entries: { name: string; size: number; mtime: number }[] = [];
|
|
434
|
+
const staleTmpCutoff = Date.now() - STALE_CACHE_TMP_MS;
|
|
392
435
|
for (const name of await fs.readdir(dir)) {
|
|
393
|
-
|
|
436
|
+
const isTmp = isCacheTmpName(name);
|
|
437
|
+
if (!isTmp && !/^[0-9a-f]{64}$/.test(name)) continue;
|
|
394
438
|
const stat = await fs.stat(path.join(dir, name)).catch(() => null);
|
|
395
|
-
if (stat?.isFile())
|
|
396
|
-
|
|
439
|
+
if (!stat?.isFile()) continue;
|
|
440
|
+
if (isTmp) {
|
|
441
|
+
// an interrupted cachePut; nothing will ever rename it into place
|
|
442
|
+
if (stat.mtimeMs < staleTmpCutoff)
|
|
443
|
+
await fs.remove(path.join(dir, name)).catch(() => {});
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
entries.push({ name, size: stat.size, mtime: stat.mtimeMs });
|
|
397
447
|
}
|
|
398
448
|
entries.sort((a, b) => b.mtime - a.mtime); // newest first
|
|
399
449
|
let total = 0;
|
|
@@ -407,12 +457,23 @@ export async function enforceCacheLimits(
|
|
|
407
457
|
}
|
|
408
458
|
}
|
|
409
459
|
|
|
460
|
+
/** `<sha256>.<pid>.tmp`, the staging name cachePut writes before renaming */
|
|
461
|
+
function isCacheTmpName(name: string): boolean {
|
|
462
|
+
return /^[0-9a-f]{64}\.\d+\.tmp$/.test(name);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Remove every cached bundle (and staging leftovers); returns the bundle count. */
|
|
410
466
|
export async function cleanCache(): Promise<number> {
|
|
411
467
|
const dir = cacheDir();
|
|
412
468
|
if (!(await fs.pathExists(dir))) return 0;
|
|
413
|
-
|
|
414
|
-
for (const name of
|
|
415
|
-
|
|
469
|
+
let bundles = 0;
|
|
470
|
+
for (const name of await fs.readdir(dir)) {
|
|
471
|
+
const isBundle = /^[0-9a-f]{64}$/.test(name);
|
|
472
|
+
if (!isBundle && !isCacheTmpName(name)) continue;
|
|
473
|
+
await fs.remove(path.join(dir, name));
|
|
474
|
+
if (isBundle) bundles += 1;
|
|
475
|
+
}
|
|
476
|
+
return bundles;
|
|
416
477
|
}
|
|
417
478
|
|
|
418
479
|
export async function cacheStats(): Promise<{
|
|
@@ -458,16 +519,21 @@ export async function downloadToFile(
|
|
|
458
519
|
url: string,
|
|
459
520
|
destination: string,
|
|
460
521
|
): Promise<void> {
|
|
461
|
-
const
|
|
522
|
+
const controller = new AbortController();
|
|
523
|
+
const headersTimer = setTimeout(
|
|
524
|
+
() => controller.abort(new Error('timeout waiting for response')),
|
|
525
|
+
FETCH_HEADERS_TIMEOUT_MS,
|
|
526
|
+
);
|
|
527
|
+
let response: Response;
|
|
528
|
+
try {
|
|
529
|
+
response = await fetch(url, { signal: controller.signal });
|
|
530
|
+
} finally {
|
|
531
|
+
clearTimeout(headersTimer);
|
|
532
|
+
}
|
|
462
533
|
if (!response.ok || !response.body) {
|
|
463
534
|
throw new Error(`download failed: HTTP ${response.status} ${url}`);
|
|
464
535
|
}
|
|
465
|
-
await
|
|
466
|
-
const { Readable } = await import('stream');
|
|
467
|
-
await pipeline(
|
|
468
|
-
Readable.fromWeb(response.body as any),
|
|
469
|
-
fs.createWriteStream(destination),
|
|
470
|
-
);
|
|
536
|
+
await saveResponse(response, destination, controller);
|
|
471
537
|
}
|
|
472
538
|
|
|
473
539
|
// ---------------------------------------------------------------------------
|
|
@@ -511,17 +577,40 @@ function hasBundleLocation(record: HermesBaseServerRecord): boolean {
|
|
|
511
577
|
);
|
|
512
578
|
}
|
|
513
579
|
|
|
580
|
+
/**
|
|
581
|
+
* Stream a response body to disk, giving up when no bytes arrive for
|
|
582
|
+
* STREAM_IDLE_TIMEOUT_MS. `controller` is the one the request was made with
|
|
583
|
+
* (aborting it also tears down the socket); a foreign response gets its own.
|
|
584
|
+
*/
|
|
514
585
|
async function saveResponse(
|
|
515
586
|
response: Response,
|
|
516
587
|
destination: string,
|
|
588
|
+
controller = new AbortController(),
|
|
517
589
|
): Promise<void> {
|
|
518
590
|
if (!response.body) throw new Error('empty response body');
|
|
519
591
|
await fs.ensureDir(path.dirname(destination));
|
|
520
|
-
const
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
);
|
|
592
|
+
const abort = () =>
|
|
593
|
+
controller.abort(
|
|
594
|
+
new Error(`download stalled for ${STREAM_IDLE_TIMEOUT_MS / 1000}s`),
|
|
595
|
+
);
|
|
596
|
+
let idle = setTimeout(abort, STREAM_IDLE_TIMEOUT_MS);
|
|
597
|
+
const watchdog = new PassThrough({
|
|
598
|
+
transform(chunk, _encoding, callback) {
|
|
599
|
+
clearTimeout(idle);
|
|
600
|
+
idle = setTimeout(abort, STREAM_IDLE_TIMEOUT_MS);
|
|
601
|
+
callback(null, chunk);
|
|
602
|
+
},
|
|
603
|
+
});
|
|
604
|
+
try {
|
|
605
|
+
await pipeline(
|
|
606
|
+
Readable.fromWeb(response.body as any),
|
|
607
|
+
watchdog,
|
|
608
|
+
fs.createWriteStream(destination),
|
|
609
|
+
{ signal: controller.signal },
|
|
610
|
+
);
|
|
611
|
+
} finally {
|
|
612
|
+
clearTimeout(idle);
|
|
613
|
+
}
|
|
525
614
|
}
|
|
526
615
|
|
|
527
616
|
/**
|
|
@@ -534,35 +623,37 @@ async function saveResponse(
|
|
|
534
623
|
* is skipped);
|
|
535
624
|
* 3. the whole archive, as before.
|
|
536
625
|
* A server that ignores Range (HTTP 200) streams the full archive into
|
|
537
|
-
* `archive` without a second request.
|
|
538
|
-
* `record.bundleHash
|
|
539
|
-
*
|
|
626
|
+
* `archive` without a second request. Every result is verified against
|
|
627
|
+
* `record.bundleHash`. A mismatch of the located bytes (1) may only mean the
|
|
628
|
+
* recorded location is stale, so the directory transport (2) still runs; a
|
|
629
|
+
* mismatch of the entry itself (2, 3) is final — the same bytes would come
|
|
630
|
+
* back however they are fetched — and raises BundleHashMismatchError.
|
|
540
631
|
*/
|
|
541
632
|
export async function fetchBaseBundle(
|
|
542
633
|
record: HermesBaseServerRecord,
|
|
543
634
|
artifactType: BaseArtifactType,
|
|
544
635
|
archive: string,
|
|
545
636
|
log: (message: string) => void = () => {},
|
|
546
|
-
): Promise<FetchedBaseBundle
|
|
637
|
+
): Promise<FetchedBaseBundle> {
|
|
547
638
|
const matches = bundleEntryMatcher(artifactType);
|
|
548
639
|
const verified = (
|
|
549
640
|
bundle: Buffer | null,
|
|
550
641
|
): { bundle: Buffer; bundleHash: string } => {
|
|
551
|
-
if (!bundle) throw new
|
|
642
|
+
if (!bundle) throw new PermanentBaseError('bundle entry not found');
|
|
552
643
|
const bundleHash = sha256Hex(bundle);
|
|
553
644
|
if (record.bundleHash && bundleHash !== record.bundleHash) {
|
|
554
|
-
throw new
|
|
645
|
+
throw new BundleHashMismatchError(bundleHash, record.bundleHash);
|
|
555
646
|
}
|
|
556
647
|
return { bundle, bundleHash };
|
|
557
648
|
};
|
|
558
649
|
const fromFullResponse = async (
|
|
559
650
|
response: Response,
|
|
560
|
-
): Promise<FetchedBaseBundle
|
|
651
|
+
): Promise<FetchedBaseBundle> => {
|
|
561
652
|
await saveResponse(response, archive);
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
653
|
+
return {
|
|
654
|
+
...verified(await extractBundleFromArchive(archive)),
|
|
655
|
+
transport: 'full',
|
|
656
|
+
};
|
|
566
657
|
};
|
|
567
658
|
const skip = (reason: string) =>
|
|
568
659
|
log(t('hermesBaseRangeFallback', { reason }));
|
|
@@ -617,15 +708,17 @@ export async function fetchBaseBundle(
|
|
|
617
708
|
skip('server ignores Range');
|
|
618
709
|
return fromFullResponse(error.response);
|
|
619
710
|
}
|
|
711
|
+
// the real entry was read and is not the recorded one: final
|
|
712
|
+
if (error instanceof PermanentBaseError) throw error;
|
|
620
713
|
skip(`zip range: ${error?.message ?? error}`);
|
|
621
714
|
}
|
|
622
715
|
}
|
|
623
716
|
|
|
624
717
|
await downloadToFile(record.url, archive);
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
718
|
+
return {
|
|
719
|
+
...verified(await extractBundleFromArchive(archive)),
|
|
720
|
+
transport: 'full',
|
|
721
|
+
};
|
|
629
722
|
}
|
|
630
723
|
|
|
631
724
|
export interface ResolveHermesBaseParams {
|
|
@@ -694,7 +787,9 @@ export async function resolveHermesBase(
|
|
|
694
787
|
}
|
|
695
788
|
let record: HermesBaseServerRecord | null = null;
|
|
696
789
|
try {
|
|
697
|
-
record = await
|
|
790
|
+
record = await retryOnce(() =>
|
|
791
|
+
params.fetchBase(params.appId as string, bytecodeVersion),
|
|
792
|
+
);
|
|
698
793
|
} catch (error: any) {
|
|
699
794
|
log(
|
|
700
795
|
t('hermesBaseNone', {
|
|
@@ -727,6 +822,19 @@ export async function resolveHermesBase(
|
|
|
727
822
|
// 1. cache by bundleHash
|
|
728
823
|
if (record.bundleHash) {
|
|
729
824
|
const hit = await cacheLookup(record.bundleHash);
|
|
825
|
+
if (hit && record.bytecodeVersion == null) {
|
|
826
|
+
// the server does not know the HBC version (legacy version, native
|
|
827
|
+
// package): the download path checks it, the cache path must too
|
|
828
|
+
const version = await readFileHbcVersion(hit);
|
|
829
|
+
if (version !== bytecodeVersion) {
|
|
830
|
+
log(
|
|
831
|
+
t('hermesBaseNone', {
|
|
832
|
+
reason: `cached base is HBC ${version ?? 'n/a'}, need ${bytecodeVersion}`,
|
|
833
|
+
}),
|
|
834
|
+
);
|
|
835
|
+
return null;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
730
838
|
if (hit) {
|
|
731
839
|
log(
|
|
732
840
|
t('hermesBaseUsing', {
|
|
@@ -744,7 +852,7 @@ export async function resolveHermesBase(
|
|
|
744
852
|
};
|
|
745
853
|
}
|
|
746
854
|
}
|
|
747
|
-
// 2. download, extract, verify, cache —
|
|
855
|
+
// 2. download, extract, verify, cache — transport errors get a second try
|
|
748
856
|
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
749
857
|
const dir = tmpDir();
|
|
750
858
|
await fs.ensureDir(dir);
|
|
@@ -763,8 +871,6 @@ export async function resolveHermesBase(
|
|
|
763
871
|
try {
|
|
764
872
|
log(t('hermesBaseDownloading', { url: record.url }));
|
|
765
873
|
const fetched = await fetchBaseBundle(record, artifactType, archive, log);
|
|
766
|
-
if (!fetched)
|
|
767
|
-
throw new Error('bundle entry not found in downloaded package');
|
|
768
874
|
const { bundle, bundleHash: actualHash } = fetched;
|
|
769
875
|
if (fetched.transport !== 'full') {
|
|
770
876
|
log(
|
|
@@ -774,11 +880,6 @@ export async function resolveHermesBase(
|
|
|
774
880
|
}),
|
|
775
881
|
);
|
|
776
882
|
}
|
|
777
|
-
if (record.bundleHash && actualHash !== record.bundleHash) {
|
|
778
|
-
throw new Error(
|
|
779
|
-
`bundleHash mismatch (${actualHash.slice(0, 12)} != ${record.bundleHash.slice(0, 12)})`,
|
|
780
|
-
);
|
|
781
|
-
}
|
|
782
883
|
const version = getHbcVersion(bundle);
|
|
783
884
|
if (version !== bytecodeVersion) {
|
|
784
885
|
log(
|
|
@@ -807,7 +908,7 @@ export async function resolveHermesBase(
|
|
|
807
908
|
source,
|
|
808
909
|
};
|
|
809
910
|
} catch (error: any) {
|
|
810
|
-
if (attempt === 2) {
|
|
911
|
+
if (attempt === 2 || error instanceof PermanentBaseError) {
|
|
811
912
|
log(
|
|
812
913
|
t('hermesBaseNone', {
|
|
813
914
|
reason: `download/verify failed: ${error?.message ?? error}`,
|
|
@@ -822,6 +923,16 @@ export async function resolveHermesBase(
|
|
|
822
923
|
return null;
|
|
823
924
|
}
|
|
824
925
|
|
|
926
|
+
/** one retry after a short pause, for transient network failures */
|
|
927
|
+
async function retryOnce<T>(action: () => Promise<T>): Promise<T> {
|
|
928
|
+
try {
|
|
929
|
+
return await action();
|
|
930
|
+
} catch {
|
|
931
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
932
|
+
return action();
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
825
936
|
export function hermescArgsWithBase(
|
|
826
937
|
baseArgs: string[],
|
|
827
938
|
basePath: string | null,
|
package/src/utils/http-helper.ts
CHANGED
|
@@ -122,20 +122,31 @@ export const testUrls = async (
|
|
|
122
122
|
});
|
|
123
123
|
};
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
let baseUrlPromise: Promise<string> | undefined;
|
|
126
|
+
|
|
127
|
+
async function resolveBaseUrl(): Promise<string> {
|
|
126
128
|
const testEndpoint = process.env.PUSHY_REGISTRY || process.env.RNU_API;
|
|
127
129
|
if (testEndpoint) {
|
|
128
130
|
return testEndpoint;
|
|
129
131
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
132
|
+
const ret = await testUrls(defaultEndpoints.map((url) => `${url}/status`));
|
|
133
|
+
let baseUrl = defaultEndpoints[0];
|
|
134
|
+
if (ret) {
|
|
135
|
+
// remove /status
|
|
136
|
+
baseUrl = ret.replace('/status', '');
|
|
137
|
+
}
|
|
138
|
+
// console.log('baseUrl', baseUrl);
|
|
139
|
+
return baseUrl;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The API base url: the env override, or the first default endpoint that
|
|
144
|
+
* answers a HEAD ping. Resolved lazily on the first API call (offline commands
|
|
145
|
+
* such as `hdiff`/`help` never ping) and memoized for the process lifetime.
|
|
146
|
+
*/
|
|
147
|
+
export const getBaseUrl = (): Promise<string> => {
|
|
148
|
+
if (!baseUrlPromise) {
|
|
149
|
+
baseUrlPromise = resolveBaseUrl();
|
|
150
|
+
}
|
|
151
|
+
return baseUrlPromise;
|
|
152
|
+
};
|