better-dsh-pet 0.3.2 → 0.3.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/README.md +133 -5
- package/lib/index.js +1 -1
- package/package.json +1 -1
- package/scripts/download-sensevoice.mjs +61 -26
- package/scripts/ensure-electron.mjs +86 -32
package/README.md
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
- [✨ 功能特性](#-功能特性)
|
|
32
32
|
- [📸 效果预览](#-效果预览)
|
|
33
33
|
- [🚀 快速开始](#-快速开始)
|
|
34
|
+
- [⬇️ 下载说明](#️-下载说明)
|
|
34
35
|
- [🖱️ 使用指南](#️-使用指南)
|
|
35
36
|
- [⚙️ 配置说明](#️-配置说明)
|
|
36
37
|
- [🎭 情绪系统](#-情绪系统)
|
|
@@ -276,7 +277,7 @@ dsh plugin --profile web add better-dsh-pet
|
|
|
276
277
|
> 如果刚发布新版本不到 24 小时,pnpm 11 的 `minimumReleaseAge` 安全策略可能不会自动选最新版,而是退回旧版。此时请指定精确版本安装:
|
|
277
278
|
>
|
|
278
279
|
> ```bash
|
|
279
|
-
> dsh plugin --profile web add better-dsh-pet@0.3.
|
|
280
|
+
> dsh plugin --profile web add better-dsh-pet@0.3.4
|
|
280
281
|
> ```
|
|
281
282
|
>
|
|
282
283
|
> pnpm 会自动把该版本加入 `minimumReleaseAgeExclude` 并完成安装;也可以等待 24 小时后直接用上面的普通命令。
|
|
@@ -299,6 +300,113 @@ dsh plugin --profile web remove better-dsh-pet
|
|
|
299
300
|
|
|
300
301
|
---
|
|
301
302
|
|
|
303
|
+
## ⬇️ 下载说明
|
|
304
|
+
|
|
305
|
+
better-dsh-pet 有两类下载:
|
|
306
|
+
|
|
307
|
+
1. **Electron**:桌宠窗口运行环境,首次启动自动下载;
|
|
308
|
+
2. **SenseVoice 语音模型**:本地离线语音识别模型,需要手动下载(可选增强)。
|
|
309
|
+
|
|
310
|
+
> 路径会自动识别 DSH 主目录:`DSH_HOME` 环境变量 → 从插件安装路径自动推断 → 默认 `~/.dsh`。所以在不同电脑、不同 DSH 安装目录下都能保持一致。
|
|
311
|
+
|
|
312
|
+
### Electron 自动下载
|
|
313
|
+
|
|
314
|
+
- 首次启动 DSH / 桌宠时,如果找不到 Electron,会自动下载到:
|
|
315
|
+
```text
|
|
316
|
+
C:\Users\Administrator\.dsh\electron\electron.exe
|
|
317
|
+
```
|
|
318
|
+
- 下载进度会显示在:
|
|
319
|
+
- 终端(如果你用 `dsh web` 启动且能看到宿主输出)
|
|
320
|
+
- 日志文件:`C:\Users\Administrator\.dsh\logs\better-dsh-pet-electron.log`
|
|
321
|
+
- 查看最新进度:
|
|
322
|
+
```powershell
|
|
323
|
+
Get-Content "C:\Users\Administrator\.dsh\logs\better-dsh-pet-electron.log" -Tail 20
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
手动触发下载(可在终端直接看进度):
|
|
327
|
+
|
|
328
|
+
```powershell
|
|
329
|
+
node "C:\Users\Administrator\.dsh\profiles\web\node_modules\better-dsh-pet\scripts\ensure-electron.mjs"
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### SenseVoice 语音模型下载
|
|
333
|
+
|
|
334
|
+
语音模型**不会自动下载**,需要手动执行一次:
|
|
335
|
+
|
|
336
|
+
```powershell
|
|
337
|
+
cd C:\Users\Administrator\.dsh\profiles\web\node_modules\better-dsh-pet
|
|
338
|
+
npm run download:sensevoice
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
脚本会自动尝试多个镜像源,哪个可用就用哪个:
|
|
342
|
+
|
|
343
|
+
```text
|
|
344
|
+
1. GitHub 官方
|
|
345
|
+
2. ghproxy.net
|
|
346
|
+
3. ghfast.top
|
|
347
|
+
4. gh-proxy.com
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
下载进度显示在终端,也会写入:
|
|
351
|
+
|
|
352
|
+
```text
|
|
353
|
+
C:\Users\Administrator\.dsh\logs\better-dsh-pet-sensevoice.log
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
查看进度:
|
|
357
|
+
|
|
358
|
+
```powershell
|
|
359
|
+
Get-Content "C:\Users\Administrator\.dsh\logs\better-dsh-pet-sensevoice.log" -Tail 20
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
模型下载完成后位于:
|
|
363
|
+
|
|
364
|
+
```text
|
|
365
|
+
C:\Users\Administrator\.dsh\voice\sensevoice\model.int8.onnx
|
|
366
|
+
C:\Users\Administrator\.dsh\voice\sensevoice\tokens.txt
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### 如何判断下载完成
|
|
370
|
+
|
|
371
|
+
| 项目 | 完成标志 |
|
|
372
|
+
|---|---|
|
|
373
|
+
| Electron | `C:\Users\Administrator\.dsh\electron\electron.exe` 存在(约 215MB) |
|
|
374
|
+
| 语音模型 | `C:\Users\Administrator\.dsh\voice\sensevoice\model.int8.onnx` 和 `tokens.txt` 存在 |
|
|
375
|
+
| 下载日志 | 日志末尾出现 `下载完成` |
|
|
376
|
+
|
|
377
|
+
### 重新下载 / 清除缓存
|
|
378
|
+
|
|
379
|
+
```powershell
|
|
380
|
+
# 删除 Electron
|
|
381
|
+
Remove-Item "C:\Users\Administrator\.dsh\electron" -Recurse -Force
|
|
382
|
+
|
|
383
|
+
# 删除语音模型
|
|
384
|
+
Remove-Item "C:\Users\Administrator\.dsh\voice" -Recurse -Force
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
然后重新启动 DSH 或重新运行上面的下载命令。
|
|
388
|
+
|
|
389
|
+
### 下载失败排查
|
|
390
|
+
|
|
391
|
+
- **Electron 解压报“文件正由另一进程使用”**:
|
|
392
|
+
- 先关闭 DSH / 桌宠,再重新下载;
|
|
393
|
+
- 或手动运行 `ensure-electron.mjs`。
|
|
394
|
+
- **语音模型一直 0% / 进度条半天不动**:
|
|
395
|
+
- 脚本会在 30 秒无数据后自动切换到下一个镜像;
|
|
396
|
+
- 如果所有镜像都失败才会报错;
|
|
397
|
+
- 也可以手动指定镜像:
|
|
398
|
+
```powershell
|
|
399
|
+
$env:DSH_VOICE_MODEL_URL = "https://ghfast.top/https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2024-07-17.tar.bz2"
|
|
400
|
+
npm run download:sensevoice
|
|
401
|
+
```
|
|
402
|
+
- **升级时提示 EPERM**:
|
|
403
|
+
- 先完全退出 DSH / 桌宠,再执行:
|
|
404
|
+
```bash
|
|
405
|
+
dsh plugin --profile web add better-dsh-pet@0.3.4
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
---
|
|
409
|
+
|
|
302
410
|
## 🖱️ 使用指南
|
|
303
411
|
|
|
304
412
|
### 右键菜单
|
|
@@ -451,9 +559,9 @@ dsh plugin --profile web remove better-dsh-pet
|
|
|
451
559
|
|
|
452
560
|
### 识别引擎
|
|
453
561
|
|
|
454
|
-
-
|
|
455
|
-
-
|
|
456
|
-
-
|
|
562
|
+
- 默认使用 **Windows 系统语音识别(SAPI)**,开箱即用
|
|
563
|
+
- 可选安装 **SenseVoice** 本地模型 + `sherpa-onnx-node`,完全离线、更准、不上传音频
|
|
564
|
+
- 首次使用 SenseVoice 需要下载模型:
|
|
457
565
|
|
|
458
566
|
```bash
|
|
459
567
|
cd node_modules/better-dsh-pet
|
|
@@ -461,6 +569,7 @@ npm run download:sensevoice
|
|
|
461
569
|
```
|
|
462
570
|
|
|
463
571
|
> 模型会下载到 `~/.dsh/voice/sensevoice`。
|
|
572
|
+
> 下载脚本会自动尝试多个镜像源,详细说明见 [⬇️ 下载说明](#️-下载说明)。
|
|
464
573
|
|
|
465
574
|
### 唤醒词
|
|
466
575
|
|
|
@@ -538,7 +647,8 @@ npm run download:sensevoice
|
|
|
538
647
|
不需要手动安装。
|
|
539
648
|
|
|
540
649
|
- 本插件**不打包 Electron**(npm 包体积限制)
|
|
541
|
-
-
|
|
650
|
+
- 首次启动会自动下载到 `~/.dsh/electron/electron.exe`
|
|
651
|
+
- 下载方式 / 进度查看见 [⬇️ 下载说明](#️-下载说明)
|
|
542
652
|
- 也可以通过环境变量指定:
|
|
543
653
|
```powershell
|
|
544
654
|
$env:DSH_PET_ELECTRON_PATH = "C:\path\to\electron.exe"
|
|
@@ -552,6 +662,24 @@ npm run download:sensevoice
|
|
|
552
662
|
better-dsh-pet: cannot resolve Electron executable.
|
|
553
663
|
```
|
|
554
664
|
|
|
665
|
+
### Q:语音模型怎么下载?
|
|
666
|
+
|
|
667
|
+
语音模型是可选增强,不会自动下载。手动执行:
|
|
668
|
+
|
|
669
|
+
```powershell
|
|
670
|
+
cd C:\Users\Administrator\.dsh\profiles\web\node_modules\better-dsh-pet
|
|
671
|
+
npm run download:sensevoice
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
详细说明见 [⬇️ 下载说明](#️-下载说明)。
|
|
675
|
+
|
|
676
|
+
### Q:没有语音模型能使用语音功能吗?
|
|
677
|
+
|
|
678
|
+
能。
|
|
679
|
+
|
|
680
|
+
- 没有 SenseVoice 模型时,会自动回退到 **Windows 系统语音识别(SAPI)**
|
|
681
|
+
- 下载 SenseVoice 后,会使用更准的本地离线识别
|
|
682
|
+
|
|
555
683
|
### Q:为什么任务识别后没有自动执行?
|
|
556
684
|
|
|
557
685
|
这是故意的。识别为任务后需要你点击「执行任务」按钮确认,防止误识别白烧 Token。
|
package/lib/index.js
CHANGED
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
const PACKAGE_ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
|
|
32
32
|
|
|
33
33
|
export const name = 'better-dsh-pet'
|
|
34
|
-
export const inject = ['sessions', 'subagents', 'agents'
|
|
34
|
+
export const inject = ['sessions', 'subagents', 'agents']
|
|
35
35
|
export const CONFIG_ENDPOINT = '/plugins/better-dsh-pet/config'
|
|
36
36
|
export const STATUS_ENDPOINT = '/plugins/better-dsh-pet/status'
|
|
37
37
|
export const CLOSE_ENDPOINT = '/plugins/better-dsh-pet/close'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-dsh-pet",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "A desktop-native pet bubble driven by DeepSeek Harness session events: transparent always-on-top Electron window, status-aware animations, voice chat/wake, real DSH task execution, emotion, festival, balance, pomodoro, roast, and DSH settings card.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* 下载后放到 ~/.dsh/voice/sensevoice,供宿主端 sherpa-onnx-node 转写使用。
|
|
9
9
|
*/
|
|
10
10
|
import { appendFileSync, createWriteStream, existsSync, mkdirSync, readdirSync, rmSync, renameSync } from 'node:fs'
|
|
11
|
-
import { join, resolve } from 'node:path'
|
|
11
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
12
12
|
import { homedir } from 'node:os'
|
|
13
13
|
import { spawnSync } from 'node:child_process'
|
|
14
14
|
import { Transform } from 'node:stream'
|
|
@@ -16,7 +16,20 @@ import { pipeline } from 'node:stream/promises'
|
|
|
16
16
|
import { fileURLToPath } from 'node:url'
|
|
17
17
|
|
|
18
18
|
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
function inferDshHomeFromPackage() {
|
|
21
|
+
let dir = ROOT
|
|
22
|
+
while (true) {
|
|
23
|
+
if (basename(dir) === 'profiles') return dirname(dir)
|
|
24
|
+
const parent = dirname(dir)
|
|
25
|
+
if (parent === dir) return undefined
|
|
26
|
+
dir = parent
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const DSH_HOME = (process.env.DSH_HOME && process.env.DSH_HOME.trim())
|
|
31
|
+
|| inferDshHomeFromPackage()
|
|
32
|
+
|| join(homedir(), '.dsh')
|
|
20
33
|
const MODEL_DIR = process.env.DSH_VOICE_MODEL_DIR || join(DSH_HOME, 'voice', 'sensevoice')
|
|
21
34
|
const DEFAULT_MODEL_URL = 'https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2024-07-17.tar.bz2'
|
|
22
35
|
const MODEL_URLS = [
|
|
@@ -53,31 +66,53 @@ function hasModel() {
|
|
|
53
66
|
}
|
|
54
67
|
|
|
55
68
|
async function download(url, dest, label = 'download') {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
const controller = new AbortController()
|
|
70
|
+
let connected = false
|
|
71
|
+
let lastChunkAt = Date.now()
|
|
72
|
+
const connectTimer = setTimeout(() => {
|
|
73
|
+
if (!connected) controller.abort(new Error('连接超时(30秒未响应)'))
|
|
74
|
+
}, 30000)
|
|
75
|
+
let stallTimer
|
|
76
|
+
try {
|
|
77
|
+
log(`[${label}] 等待响应...`)
|
|
78
|
+
const response = await fetch(url, { redirect: 'follow', signal: controller.signal })
|
|
79
|
+
if (!response.ok) throw new Error(`HTTP ${response.status} ${response.statusText}`)
|
|
80
|
+
connected = true
|
|
81
|
+
clearTimeout(connectTimer)
|
|
82
|
+
const total = Number(response.headers.get('content-length')) || 0
|
|
83
|
+
let received = 0
|
|
84
|
+
let lastLoggedAt = 0
|
|
85
|
+
let lastPercent = -1
|
|
86
|
+
lastChunkAt = Date.now()
|
|
87
|
+
stallTimer = setInterval(() => {
|
|
88
|
+
if (Date.now() - lastChunkAt > 30000) {
|
|
89
|
+
controller.abort(new Error('下载停滞(30秒无数据)'))
|
|
75
90
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
}, 5000)
|
|
92
|
+
const progress = new Transform({
|
|
93
|
+
transform(chunk, _encoding, callback) {
|
|
94
|
+
received += chunk.length
|
|
95
|
+
lastChunkAt = Date.now()
|
|
96
|
+
const percent = total ? Math.floor((received / total) * 100) : 0
|
|
97
|
+
const shouldLog = total
|
|
98
|
+
? (percent !== lastPercent && (percent % 10 === 0 || percent === 100))
|
|
99
|
+
: (received - lastLoggedAt >= 10 * 1024 * 1024)
|
|
100
|
+
if (shouldLog) {
|
|
101
|
+
lastPercent = percent
|
|
102
|
+
lastLoggedAt = received
|
|
103
|
+
const mb = (received / 1024 / 1024).toFixed(1)
|
|
104
|
+
const totalMb = total ? ` / ${(total / 1024 / 1024).toFixed(1)}MB` : ''
|
|
105
|
+
log(`[${label}] ${total ? `${percent}%` : mb} (${mb}MB${totalMb})`)
|
|
106
|
+
}
|
|
107
|
+
callback(null, chunk)
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
await pipeline(response.body, progress, createWriteStream(dest))
|
|
111
|
+
log(`[${label}] 下载完成:${(received / 1024 / 1024).toFixed(1)}MB`)
|
|
112
|
+
} finally {
|
|
113
|
+
clearTimeout(connectTimer)
|
|
114
|
+
if (stallTimer) clearInterval(stallTimer)
|
|
115
|
+
}
|
|
81
116
|
}
|
|
82
117
|
|
|
83
118
|
function extract(archive, target) {
|
|
@@ -16,14 +16,34 @@
|
|
|
16
16
|
|
|
17
17
|
import { appendFileSync, createWriteStream, existsSync, mkdirSync, rmSync } from 'node:fs'
|
|
18
18
|
import { tmpdir } from 'node:os'
|
|
19
|
-
import { join, resolve } from 'node:path'
|
|
19
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
20
20
|
import { spawnSync } from 'node:child_process'
|
|
21
21
|
import { Transform } from 'node:stream'
|
|
22
22
|
import { pipeline } from 'node:stream/promises'
|
|
23
|
+
import { fileURLToPath } from 'node:url'
|
|
23
24
|
|
|
24
|
-
const
|
|
25
|
+
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)))
|
|
26
|
+
|
|
27
|
+
function inferDshHomeFromPackage() {
|
|
28
|
+
let dir = ROOT
|
|
29
|
+
while (true) {
|
|
30
|
+
if (basename(dir) === 'profiles') return dirname(dir)
|
|
31
|
+
const parent = dirname(dir)
|
|
32
|
+
if (parent === dir) return undefined
|
|
33
|
+
dir = parent
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const HOME = (process.env.DSH_HOME && process.env.DSH_HOME.trim())
|
|
38
|
+
|| inferDshHomeFromPackage()
|
|
39
|
+
|| join(process.env.USERPROFILE || process.env.HOME || '', '.dsh')
|
|
25
40
|
const VERSION = process.env.DSH_PET_ELECTRON_VERSION || '43.3.0'
|
|
26
|
-
const
|
|
41
|
+
const MIRROR_TEMPLATES = [
|
|
42
|
+
(version, zip) => `${(process.env.DSH_PET_ELECTRON_MIRROR || 'https://npmmirror.com/mirrors/electron/').replace(/\/$/, '')}/${version}/${zip}`,
|
|
43
|
+
(version, zip) => `https://github.com/electron/electron/releases/download/v${version}/${zip}`,
|
|
44
|
+
(version, zip) => `https://ghfast.top/https://github.com/electron/electron/releases/download/v${version}/${zip}`,
|
|
45
|
+
(version, zip) => `https://gh-proxy.com/https://github.com/electron/electron/releases/download/v${version}/${zip}`,
|
|
46
|
+
]
|
|
27
47
|
const TARGET_DIR = resolve(HOME, 'electron')
|
|
28
48
|
const EXE = join(TARGET_DIR, 'electron.exe')
|
|
29
49
|
const REQUIRED_FILES = [
|
|
@@ -47,33 +67,55 @@ function log(msg) {
|
|
|
47
67
|
}
|
|
48
68
|
|
|
49
69
|
async function download(url, dest, label = 'download') {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
const controller = new AbortController()
|
|
71
|
+
let connected = false
|
|
72
|
+
let lastChunkAt = Date.now()
|
|
73
|
+
const connectTimer = setTimeout(() => {
|
|
74
|
+
if (!connected) controller.abort(new Error('连接超时(30秒未响应)'))
|
|
75
|
+
}, 30000)
|
|
76
|
+
let stallTimer
|
|
77
|
+
try {
|
|
78
|
+
log(`[${label}] 等待响应...`)
|
|
79
|
+
const response = await fetch(url, { redirect: 'follow', signal: controller.signal })
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
throw new Error(`download failed: ${response.status} ${response.statusText} (${url})`)
|
|
82
|
+
}
|
|
83
|
+
connected = true
|
|
84
|
+
clearTimeout(connectTimer)
|
|
85
|
+
const total = Number(response.headers.get('content-length')) || 0
|
|
86
|
+
let received = 0
|
|
87
|
+
let lastLoggedAt = 0
|
|
88
|
+
let lastPercent = -1
|
|
89
|
+
lastChunkAt = Date.now()
|
|
90
|
+
stallTimer = setInterval(() => {
|
|
91
|
+
if (Date.now() - lastChunkAt > 30000) {
|
|
92
|
+
controller.abort(new Error('下载停滞(30秒无数据)'))
|
|
71
93
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
94
|
+
}, 5000)
|
|
95
|
+
const progress = new Transform({
|
|
96
|
+
transform(chunk, _encoding, callback) {
|
|
97
|
+
received += chunk.length
|
|
98
|
+
lastChunkAt = Date.now()
|
|
99
|
+
const percent = total ? Math.floor((received / total) * 100) : 0
|
|
100
|
+
const shouldLog = total
|
|
101
|
+
? (percent !== lastPercent && (percent % 10 === 0 || percent === 100))
|
|
102
|
+
: (received - lastLoggedAt >= 10 * 1024 * 1024)
|
|
103
|
+
if (shouldLog) {
|
|
104
|
+
lastPercent = percent
|
|
105
|
+
lastLoggedAt = received
|
|
106
|
+
const mb = (received / 1024 / 1024).toFixed(1)
|
|
107
|
+
const totalMb = total ? ` / ${(total / 1024 / 1024).toFixed(1)}MB` : ''
|
|
108
|
+
log(`[${label}] ${total ? `${percent}%` : mb} (${mb}MB${totalMb})`)
|
|
109
|
+
}
|
|
110
|
+
callback(null, chunk)
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
await pipeline(response.body, progress, createWriteStream(dest))
|
|
114
|
+
log(`[${label}] 下载完成:${(received / 1024 / 1024).toFixed(1)}MB`)
|
|
115
|
+
} finally {
|
|
116
|
+
clearTimeout(connectTimer)
|
|
117
|
+
if (stallTimer) clearInterval(stallTimer)
|
|
118
|
+
}
|
|
77
119
|
}
|
|
78
120
|
|
|
79
121
|
function extractZip(zipPath, targetDir) {
|
|
@@ -108,13 +150,25 @@ async function main() {
|
|
|
108
150
|
mkdirSync(TARGET_DIR, { recursive: true })
|
|
109
151
|
|
|
110
152
|
const zipName = `electron-v${VERSION}-win32-x64.zip`
|
|
111
|
-
const url = `${MIRROR.replace(/\/$/, '')}/${VERSION}/${zipName}`
|
|
112
153
|
const zipPath = join(tmpdir(), zipName)
|
|
113
154
|
try { rmSync(zipPath, { force: true }) } catch { /* 清理残留 zip */ }
|
|
114
155
|
|
|
115
156
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
157
|
+
let lastError
|
|
158
|
+
for (const makeUrl of MIRROR_TEMPLATES) {
|
|
159
|
+
const url = makeUrl(VERSION, zipName)
|
|
160
|
+
log(`[ensure-electron] trying: ${url}`)
|
|
161
|
+
try {
|
|
162
|
+
await download(url, zipPath, 'Electron')
|
|
163
|
+
lastError = undefined
|
|
164
|
+
break
|
|
165
|
+
} catch (error) {
|
|
166
|
+
lastError = error
|
|
167
|
+
log(`[ensure-electron] mirror failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
168
|
+
try { rmSync(zipPath, { force: true }) } catch { /* ignore */ }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (lastError) throw lastError
|
|
118
172
|
extractZip(zipPath, TARGET_DIR)
|
|
119
173
|
if (!existsSync(EXE)) {
|
|
120
174
|
throw new Error('Electron zip extracted, but electron.exe not found')
|