pi-flash 0.1.2 → 0.1.4
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/bin/fetch-payload.js +1 -1
- package/bin/lib.js +199 -51
- package/bin/pi-flash.js +6 -0
- package/package.json +1 -1
package/bin/fetch-payload.js
CHANGED
|
@@ -13,7 +13,7 @@ if (require.main === module) {
|
|
|
13
13
|
console.error(`\n✗ pi-flash 载荷获取失败:\n ${error.message}`);
|
|
14
14
|
console.error(
|
|
15
15
|
" 重试:npm rebuild pi-flash(或重跑 npm install -g pi-flash@latest)\n" +
|
|
16
|
-
"
|
|
16
|
+
" 换源:PI_FLASH_MIRROR=<base> 指定唯一源;PI_FLASH_MIRRORS=<逗号分隔> 追加候选\n" +
|
|
17
17
|
` 手动下载:${lib.assetUrls(lib.platformTag()).releasePage}`,
|
|
18
18
|
);
|
|
19
19
|
process.exit(1);
|
package/bin/lib.js
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
// <pkg>/payload/pi-flash.exe win32-x64(release zip 根为 pi-flash/ 包装目录)
|
|
8
8
|
// <pkg>/payload/pi-flash.app/… darwin-arm64(release zip 根即 .app)
|
|
9
9
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
10
|
+
// 下载源:GitHub 直连优先,测速不过阈值自动按序切默认镜像列表(080 §3.2);
|
|
11
|
+
// PI_FLASH_MIRROR 指定唯一源、PI_FLASH_MIRRORS 追加候选。镜像须保持同构布局
|
|
12
|
+
// <base>/v<version>/<asset>。
|
|
12
13
|
const fs = require("fs");
|
|
13
14
|
const path = require("path");
|
|
14
15
|
const crypto = require("crypto");
|
|
@@ -70,10 +71,31 @@ function platformTag() {
|
|
|
70
71
|
return tag;
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
// 候选源:直连优先(官方源健康时不经第三方);首段测速不过阈值按序切换。
|
|
75
|
+
// 默认镜像为 2026-10 实测可用的前缀加速服务(同构布局);失效后用
|
|
76
|
+
// PI_FLASH_MIRROR / PI_FLASH_MIRRORS 指定,或直接 PR 更新此表。
|
|
77
|
+
const DIRECT_BASE = "https://github.com/danielbill/pi-flash/releases/download";
|
|
78
|
+
const DEFAULT_MIRRORS = [
|
|
79
|
+
"https://gh-proxy.com/https://github.com/danielbill/pi-flash/releases/download",
|
|
80
|
+
"https://ghfast.top/https://github.com/danielbill/pi-flash/releases/download",
|
|
81
|
+
"https://ghproxy.net/https://github.com/danielbill/pi-flash/releases/download",
|
|
82
|
+
];
|
|
83
|
+
|
|
73
84
|
function releaseBase() {
|
|
74
85
|
const mirror = process.env.PI_FLASH_MIRROR;
|
|
75
86
|
if (mirror) return mirror.replace(/\/+$/, "");
|
|
76
|
-
return
|
|
87
|
+
return DIRECT_BASE;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 显式 PI_FLASH_MIRROR = 只用这一个(跳过测速切换);否则直连 + 追加 + 默认
|
|
91
|
+
function downloadBases() {
|
|
92
|
+
if (process.env.PI_FLASH_MIRROR) return [releaseBase()];
|
|
93
|
+
const extra = (process.env.PI_FLASH_MIRRORS || "").split(/[\s,]+/).filter(Boolean);
|
|
94
|
+
return [DIRECT_BASE, ...extra, ...DEFAULT_MIRRORS];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function baseLabel(base) {
|
|
98
|
+
return base === DIRECT_BASE ? "GitHub 直连" : `镜像 ${new URL(base).host}`;
|
|
77
99
|
}
|
|
78
100
|
|
|
79
101
|
function assetUrls(tag, version = pkgVersion) {
|
|
@@ -223,8 +245,45 @@ function applyStagedUpdate() {
|
|
|
223
245
|
|
|
224
246
|
// --- 网络 -------------------------------------------------------------------
|
|
225
247
|
|
|
226
|
-
|
|
227
|
-
|
|
248
|
+
// 测速切换:开传 probeMs(默认 3s)时平均速度仍低于 minSpeedBps(默认
|
|
249
|
+
// 128KB/s)→ 放弃当前源换下一个。环境变量可调,便于慢网测试。
|
|
250
|
+
function minSpeedBps() {
|
|
251
|
+
return (parseFloat(process.env.PI_FLASH_MIN_KBPS) || 128) * 1024;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function probeMs() {
|
|
255
|
+
return parseInt(process.env.PI_FLASH_PROBE_MS, 10) || 3000;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function fmtMB(n) {
|
|
259
|
+
return `${(n / 1024 / 1024).toFixed(1)}MB`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function fmtSpeed(bps) {
|
|
263
|
+
return bps >= 1024 * 1024
|
|
264
|
+
? `${(bps / 1048576).toFixed(1)}MB/s`
|
|
265
|
+
: `${Math.round(bps / 1024)}KB/s`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 进度行:\r 原地刷新,尾部补空格清残影(postinstall 的 stderr 直接进终端)
|
|
269
|
+
let progressLen = 0;
|
|
270
|
+
|
|
271
|
+
function drawProgress(got, total, bps) {
|
|
272
|
+
const speedText = bps > 0 ? ` ${fmtSpeed(bps)}` : "";
|
|
273
|
+
const line =
|
|
274
|
+
total > 0
|
|
275
|
+
? ` [${String(Math.min(99, Math.floor((got / total) * 100))).padStart(3)}%] ${fmtMB(got)}/${fmtMB(total)}${speedText}`
|
|
276
|
+
: ` ${fmtMB(got)}${speedText}`;
|
|
277
|
+
const pad = " ".repeat(Math.max(0, progressLen - line.length));
|
|
278
|
+
process.stderr.write(`\r${line}${pad}`);
|
|
279
|
+
progressLen = line.length;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function clearProgress() {
|
|
283
|
+
if (progressLen > 0) {
|
|
284
|
+
process.stderr.write(`\r${" ".repeat(progressLen)}\r`);
|
|
285
|
+
progressLen = 0;
|
|
286
|
+
}
|
|
228
287
|
}
|
|
229
288
|
|
|
230
289
|
function httpGet(url, redirectsLeft = 5) {
|
|
@@ -250,17 +309,79 @@ function httpGet(url, redirectsLeft = 5) {
|
|
|
250
309
|
});
|
|
251
310
|
}
|
|
252
311
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
312
|
+
// 单源下载:进度回调 + 速度守卫——开传满 probeMs 平均速度仍不达标则中止
|
|
313
|
+
// (ETOO_SLOW),由上层换源;30s 无活动超时兜底完全断流。
|
|
314
|
+
async function downloadToFile(url, dest, { onProgress } = {}) {
|
|
315
|
+
const res = await httpGet(url);
|
|
316
|
+
const total = parseInt(res.headers["content-length"] || "", 10) || 0;
|
|
317
|
+
return new Promise((resolve, reject) => {
|
|
256
318
|
const out = fs.createWriteStream(dest);
|
|
319
|
+
let got = 0;
|
|
320
|
+
let settled = false;
|
|
321
|
+
const t0 = Date.now();
|
|
322
|
+
const settle = (fail, err) => {
|
|
323
|
+
if (settled) return;
|
|
324
|
+
settled = true;
|
|
325
|
+
clearInterval(timer);
|
|
326
|
+
if (fail) {
|
|
327
|
+
out.destroy();
|
|
328
|
+
res.destroy();
|
|
329
|
+
reject(err);
|
|
330
|
+
} else {
|
|
331
|
+
resolve();
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const timer = setInterval(() => {
|
|
335
|
+
const elapsed = Date.now() - t0;
|
|
336
|
+
const speed = elapsed > 0 ? got / (elapsed / 1000) : 0;
|
|
337
|
+
if (onProgress) onProgress(got, total, speed);
|
|
338
|
+
if (elapsed >= probeMs() && speed < minSpeedBps()) {
|
|
339
|
+
const err = new Error(
|
|
340
|
+
`${fmtSpeed(speed)} < 阈值 ${fmtSpeed(minSpeedBps())}`,
|
|
341
|
+
);
|
|
342
|
+
err.code = "ETOO_SLOW";
|
|
343
|
+
settle(true, err);
|
|
344
|
+
}
|
|
345
|
+
}, 500);
|
|
346
|
+
res.on("data", (chunk) => {
|
|
347
|
+
got += chunk.length;
|
|
348
|
+
});
|
|
257
349
|
res.pipe(out);
|
|
258
|
-
out.on("finish",
|
|
259
|
-
out.on("error",
|
|
260
|
-
res.on("error",
|
|
350
|
+
out.on("finish", () => settle(false));
|
|
351
|
+
out.on("error", (err) => settle(true, err));
|
|
352
|
+
res.on("error", (err) => settle(true, err));
|
|
261
353
|
});
|
|
262
354
|
}
|
|
263
355
|
|
|
356
|
+
// 多源 failover:按序试 downloadBases(),速度过慢/网络错误都换下一个,
|
|
357
|
+
// 全灭才报错(附 release 页与 PI_FLASH_MIRROR 用法)。返回命中的 base,
|
|
358
|
+
// 供校验和钉定官方源用(080 §3.2)。
|
|
359
|
+
async function downloadAssetFile(asset, dest) {
|
|
360
|
+
const errors = [];
|
|
361
|
+
for (const base of downloadBases()) {
|
|
362
|
+
const label = baseLabel(base);
|
|
363
|
+
process.stderr.write(` 源:${label}\n`);
|
|
364
|
+
try {
|
|
365
|
+
await downloadToFile(`${base}/v${pkgVersion}/${asset}`, dest, {
|
|
366
|
+
onProgress: drawProgress,
|
|
367
|
+
});
|
|
368
|
+
clearProgress();
|
|
369
|
+
return base;
|
|
370
|
+
} catch (err) {
|
|
371
|
+
clearProgress();
|
|
372
|
+
const why =
|
|
373
|
+
err.code === "ETOO_SLOW" ? `测速不过(${err.message})` : err.message;
|
|
374
|
+
console.error(` ${label} 不可用(${why}),换下一个源`);
|
|
375
|
+
errors.push(`${label}:${why}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
throw new Error(
|
|
379
|
+
`所有下载源均失败:\n ${errors.join("\n ")}\n` +
|
|
380
|
+
` 可设 PI_FLASH_MIRROR=<base> 指定唯一源(布局 <base>/v<version>/<asset>),\n` +
|
|
381
|
+
` 或手动下载:${assetUrls(platformTag()).releasePage}`,
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
264
385
|
async function fetchText(url) {
|
|
265
386
|
const res = await httpGet(url);
|
|
266
387
|
const chunks = [];
|
|
@@ -276,23 +397,6 @@ async function fetchText(url) {
|
|
|
276
397
|
return Buffer.concat(chunks).toString("utf8");
|
|
277
398
|
}
|
|
278
399
|
|
|
279
|
-
// 有限次重试 + 线性退避(仅网络类操作;校验失败不做无谓重试)
|
|
280
|
-
async function retry(op, label, attempts = 3) {
|
|
281
|
-
let lastErr;
|
|
282
|
-
for (let i = 1; i <= attempts; i += 1) {
|
|
283
|
-
try {
|
|
284
|
-
return await op();
|
|
285
|
-
} catch (err) {
|
|
286
|
-
lastErr = err;
|
|
287
|
-
if (i < attempts) {
|
|
288
|
-
console.error(` ${label}失败(${err.message}),${i}s 后重试 ${i}/${attempts - 1}`);
|
|
289
|
-
await sleep(i * 1000);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
throw lastErr;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
400
|
// --- 校验与解压 ---------------------------------------------------------------
|
|
297
401
|
|
|
298
402
|
function sha256File(filePath) {
|
|
@@ -314,22 +418,31 @@ function parseHashFor(text, filename) {
|
|
|
314
418
|
return null;
|
|
315
419
|
}
|
|
316
420
|
|
|
317
|
-
//
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
421
|
+
// 校验和取用顺序:直连 sidecar → 直连 SHA256SUMS → 命中源 sidecar → 命中源
|
|
422
|
+
// SHA256SUMS。直连优先钉定官方哈希(zip 走镜像时防串包);单请求不做重试,
|
|
423
|
+
// 源足够多,网络不通(非 404)直接换下一源。
|
|
424
|
+
async function resolveExpectedHash(asset, chosenBase) {
|
|
425
|
+
const bases =
|
|
426
|
+
chosenBase === DIRECT_BASE ? [DIRECT_BASE] : [DIRECT_BASE, chosenBase];
|
|
427
|
+
for (const base of bases) {
|
|
428
|
+
for (const url of [
|
|
429
|
+
`${base}/v${pkgVersion}/${asset}.sha256`,
|
|
430
|
+
`${base}/v${pkgVersion}/SHA256SUMS`,
|
|
431
|
+
]) {
|
|
432
|
+
let text;
|
|
433
|
+
try {
|
|
434
|
+
text = await fetchText(url);
|
|
435
|
+
} catch (err) {
|
|
436
|
+
if (err.status === 404) continue; // sidecar 缺失 → 同源兜底 SHA256SUMS
|
|
437
|
+
break; // 该源网络不可用 → 换源
|
|
438
|
+
}
|
|
439
|
+
const hash = parseHashFor(text, asset);
|
|
440
|
+
if (hash) return hash;
|
|
326
441
|
}
|
|
327
|
-
const hash = parseHashFor(text, urls.asset);
|
|
328
|
-
if (hash) return hash;
|
|
329
442
|
}
|
|
330
443
|
throw new Error(
|
|
331
|
-
`在 release 资产里找不到 ${
|
|
332
|
-
` 请到 ${
|
|
444
|
+
`在 release 资产里找不到 ${asset} 的 SHA-256(试过直连与 ${chosenBase})。\n` +
|
|
445
|
+
` 请到 ${assetUrls(platformTag()).releasePage} 核对;不要在未校验的情况下手动安装载荷。`,
|
|
333
446
|
);
|
|
334
447
|
}
|
|
335
448
|
|
|
@@ -384,11 +497,35 @@ function busyMessage() {
|
|
|
384
497
|
return "载荷目录被占用——请先退出正在运行的 pi-flash,再重新执行安装/更新。";
|
|
385
498
|
}
|
|
386
499
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
500
|
+
// pid 存活探测:ESRCH = 进程不存在;EPERM = 存在但无权限(按活的处理)
|
|
501
|
+
function pidAlive(pid) {
|
|
502
|
+
try {
|
|
503
|
+
process.kill(pid, 0);
|
|
504
|
+
return true;
|
|
505
|
+
} catch (err) {
|
|
506
|
+
return err.code === "EPERM";
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// 清本机残留(080 §3.3):半截下载 payload.tmp-<pid> / update.tmp-<pid>
|
|
511
|
+
// (含应用内更新器 081 留下的)按 pid 存活判断删除,活进程的不碰;
|
|
512
|
+
// payload.old-* 只在 swap 中断后遗留,无条件删。启动器每次启动与下载前都跑。
|
|
513
|
+
function rmLeftovers() {
|
|
514
|
+
let entries;
|
|
515
|
+
try {
|
|
516
|
+
entries = fs.readdirSync(pkgDir);
|
|
517
|
+
} catch {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
for (const entry of entries) {
|
|
521
|
+
const tmp = /^(?:payload|update)\.tmp-(\d+)$/.exec(entry);
|
|
522
|
+
if (tmp) {
|
|
523
|
+
const pid = parseInt(tmp[1], 10);
|
|
524
|
+
if (pid === process.pid || pidAlive(pid)) continue;
|
|
525
|
+
} else if (!entry.startsWith(OLD_PREFIX)) {
|
|
526
|
+
continue;
|
|
391
527
|
}
|
|
528
|
+
fs.rmSync(path.join(pkgDir, entry), { recursive: true, force: true });
|
|
392
529
|
}
|
|
393
530
|
}
|
|
394
531
|
|
|
@@ -413,24 +550,27 @@ function replacePayload(contentRoot) {
|
|
|
413
550
|
}
|
|
414
551
|
}
|
|
415
552
|
|
|
416
|
-
// 主流程:版本戳不合 →
|
|
553
|
+
// 主流程:版本戳不合 → 清残留 → 测速选源下载 → 校验 → 解压 → 落位 → 最后写版本戳
|
|
417
554
|
async function ensure({ force = false } = {}) {
|
|
418
555
|
const tag = platformTag(); // 不支持的平台在这里就明确报错
|
|
419
556
|
if (!force && payloadOk(tag)) return;
|
|
420
557
|
|
|
421
558
|
const urls = assetUrls(tag);
|
|
559
|
+
rmLeftovers();
|
|
422
560
|
const tmp = path.join(pkgDir, `${TMP_PREFIX}${process.pid}`);
|
|
423
561
|
fs.rmSync(tmp, { recursive: true, force: true });
|
|
424
|
-
rmStaleTmp();
|
|
425
562
|
fs.mkdirSync(tmp, { recursive: true });
|
|
426
563
|
|
|
427
564
|
try {
|
|
428
565
|
const zipPath = path.join(tmp, urls.asset);
|
|
566
|
+
const t0 = Date.now();
|
|
429
567
|
console.error(` 下载载荷 ${urls.asset}`);
|
|
430
|
-
|
|
431
|
-
|
|
568
|
+
const chosen = await downloadAssetFile(urls.asset, zipPath);
|
|
569
|
+
console.error(
|
|
570
|
+
` 下载完成,用时 ${Math.max(1, Math.round((Date.now() - t0) / 1000))}s(源:${baseLabel(chosen)})`,
|
|
571
|
+
);
|
|
432
572
|
|
|
433
|
-
const expected = await resolveExpectedHash(urls);
|
|
573
|
+
const expected = await resolveExpectedHash(urls.asset, chosen);
|
|
434
574
|
const actual = await sha256File(zipPath);
|
|
435
575
|
if (actual !== expected) {
|
|
436
576
|
throw new Error(
|
|
@@ -460,6 +600,8 @@ module.exports = {
|
|
|
460
600
|
nodeVersionMessage,
|
|
461
601
|
platformTag,
|
|
462
602
|
releaseBase,
|
|
603
|
+
downloadBases,
|
|
604
|
+
baseLabel,
|
|
463
605
|
assetUrls,
|
|
464
606
|
exeRelPath,
|
|
465
607
|
exePath,
|
|
@@ -467,11 +609,17 @@ module.exports = {
|
|
|
467
609
|
compareVersions,
|
|
468
610
|
readStaged,
|
|
469
611
|
applyStagedUpdate,
|
|
470
|
-
|
|
612
|
+
minSpeedBps,
|
|
613
|
+
probeMs,
|
|
614
|
+
fmtMB,
|
|
615
|
+
fmtSpeed,
|
|
471
616
|
sha256File,
|
|
472
617
|
parseHashFor,
|
|
473
618
|
resolveExpectedHash,
|
|
619
|
+
downloadToFile,
|
|
620
|
+
downloadAssetFile,
|
|
474
621
|
extractZip,
|
|
475
622
|
ensure,
|
|
476
623
|
busyMessage,
|
|
624
|
+
rmLeftovers,
|
|
477
625
|
};
|
package/bin/pi-flash.js
CHANGED
|
@@ -19,6 +19,7 @@ const {
|
|
|
19
19
|
ensure,
|
|
20
20
|
busyMessage,
|
|
21
21
|
applyStagedUpdate,
|
|
22
|
+
rmLeftovers,
|
|
22
23
|
} = require("./lib");
|
|
23
24
|
|
|
24
25
|
if (!isNodeVersionSupported(process.versions.node)) {
|
|
@@ -35,6 +36,11 @@ if (!isNodeVersionSupported(process.versions.node)) {
|
|
|
35
36
|
process.exit(1);
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
// 清上次中断的半截下载/swap 残留(死进程的才删,活的不碰);失败不拦启动
|
|
40
|
+
try {
|
|
41
|
+
rmLeftovers();
|
|
42
|
+
} catch {}
|
|
43
|
+
|
|
38
44
|
// 081:应用内更新已把新载荷下到 staging → 先 swap 再启动
|
|
39
45
|
try {
|
|
40
46
|
const applied = applyStagedUpdate();
|
package/package.json
CHANGED