@oadank/dsh-input-tools 0.3.4 → 0.3.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oadank/dsh-input-tools",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -58,6 +58,49 @@ New-Item -ItemType Directory -Force -Path "$InstallDir\models" | Out-Null
58
58
  New-Item -ItemType Directory -Force -Path "$InstallDir\bin" | Out-Null
59
59
  New-Item -ItemType Directory -Force -Path "$InstallDir\tmp" | Out-Null
60
60
 
61
+ # ---- 1b. 下载/解压工具函数(2026-08-21 增强:断点续传 + 完整性校验 + 损坏自动重试)----
62
+ # 之前 XDN 实测:GitHub 大文件下载中断 → 只下了 42.7MB/230MB → tar 报 Truncated → 脚本抛错退出,
63
+ # 用户只看到"跑到最后报错"毫无提示。现在 curl -C - 断点续传 + 解压后校验,损坏文件自动删除重下。
64
+ # ⚠️ PowerShell 坑:$ErrorActionPreference=Stop 时 curl/tar 写 stderr(进度/警告)会抛 NativeCommandError
65
+ # 中断脚本(0.3.5 实测踩中)。因此所有原生命令用 cmd /c 包装 + 2>nul 吞 stderr + 临时放宽 EAP,
66
+ # 只依据 $LASTEXITCODE 判断成败。
67
+ function Download-Resume {
68
+ param([string]$Url, [string]$OutFile)
69
+ for ($attempt = 1; $attempt -le 3; $attempt++) {
70
+ Write-Host " 下载(第 $attempt 次尝试): $Url" -ForegroundColor Yellow
71
+ $prevEAP = $ErrorActionPreference
72
+ $ErrorActionPreference = "Continue"
73
+ cmd /c "curl.exe -sL -C - --retry 3 --retry-delay 2 --connect-timeout 20 -o `"$OutFile`" `"$Url`" 2>nul"
74
+ $code = $LASTEXITCODE
75
+ $ErrorActionPreference = $prevEAP
76
+ if ($code -eq 0) { return $true }
77
+ Write-Host " 下载中断(exit=$code),3 秒后重试..." -ForegroundColor Yellow
78
+ Start-Sleep -Seconds 3
79
+ }
80
+ return $false
81
+ }
82
+ function Expand-TarBz2 {
83
+ param([string]$Archive, [string]$Dest, [int]$Strip = 0)
84
+ $tmpDir = Join-Path $Dest ".extract-tmp"
85
+ New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null
86
+ $stripArgs = ""
87
+ if ($Strip -gt 0) { $stripArgs = "--strip-components=$Strip" }
88
+ $prevEAP = $ErrorActionPreference
89
+ $ErrorActionPreference = "Continue"
90
+ cmd /c "tar -xjf `"$Archive`" -C `"$tmpDir`" $stripArgs 2>nul"
91
+ $code = $LASTEXITCODE
92
+ $ErrorActionPreference = $prevEAP
93
+ if ($code -ne 0) {
94
+ Write-Host " 解压失败:压缩包损坏或不完整($Archive)" -ForegroundColor Red
95
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
96
+ Remove-Item -Force $Archive -ErrorAction SilentlyContinue
97
+ return $false
98
+ }
99
+ Get-ChildItem $tmpDir | Move-Item -Destination $Dest -Force -ErrorAction SilentlyContinue
100
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
101
+ return $true
102
+ }
103
+
61
104
  # ---- 2. 检查 ffmpeg(ASR 转码必需)----
62
105
  Write-Host "`n[1/4] 检查 ffmpeg..." -ForegroundColor Yellow
63
106
  $ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue
@@ -79,12 +122,15 @@ Write-Host "`n[2/4] 下载 sherpa-onnx $Version ..." -ForegroundColor Yellow
79
122
  $pkgUrl = "https://github.com/k2-fsa/sherpa-onnx/releases/download/$Version/sherpa-onnx-$Version-win-x64-shared-MD-Release.tar.bz2"
80
123
  $pkgFile = "$InstallDir\tmp\sherpa-onnx.tar.bz2"
81
124
  if (-not (Test-Path "$InstallDir\bin\sherpa-onnx-offline.exe")) {
82
- if (-not (Test-Path $pkgFile)) {
83
- Write-Host " 下载 $pkgUrl"
84
- Invoke-WebRequest -Uri $pkgUrl -OutFile $pkgFile -UseBasicParsing
125
+ if (-not (Download-Resume $pkgUrl $pkgFile)) {
126
+ Write-Host " sherpa-onnx 下载失败(网络不稳定)。请检查网络后重跑本脚本" -ForegroundColor Red
127
+ exit 1
85
128
  }
86
129
  Write-Host " 解压..."
87
- tar -xjf $pkgFile -C "$InstallDir\tmp"
130
+ if (-not (Expand-TarBz2 $pkgFile "$InstallDir\tmp")) {
131
+ Write-Host " sherpa-onnx 压缩包损坏已删除,请重跑本脚本自动重新下载" -ForegroundColor Red
132
+ exit 1
133
+ }
88
134
  # 解压后目录结构:sherpa-onnx-v1.13.6-win-x64-shared-MD-Release/ 内含 bin/ lib/
89
135
  $extracted = Get-ChildItem "$InstallDir\tmp" -Directory | Where-Object { $_.Name -like "sherpa-onnx-*win*" } | Select-Object -First 1
90
136
  if ($extracted) {
@@ -99,19 +145,28 @@ if (-not (Test-Path "$InstallDir\bin\sherpa-onnx-offline.exe")) {
99
145
  Write-Host " sherpa-onnx 已存在,跳过下载" -ForegroundColor Green
100
146
  }
101
147
 
102
- # ---- 4. 下载 SenseVoice 模型 ----
148
+ # ---- 4. 下载 SenseVoice 模型(断点续传 + 完整性校验)----
103
149
  Write-Host "`n[3/4] 下载 SenseVoice int8 模型..." -ForegroundColor Yellow
104
150
  $modelDir = "$InstallDir\models\sensevoice-int8"
105
151
  $modelUrl = "https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2025-09-09.tar.bz2"
106
152
  $modelFile = "$InstallDir\tmp\sensevoice.tar.bz2"
107
153
  if (-not (Test-Path "$modelDir\model.int8.onnx")) {
108
- if (-not (Test-Path $modelFile)) {
109
- Write-Host " 下载 $modelUrl"
110
- Invoke-WebRequest -Uri $modelUrl -OutFile $modelFile -UseBasicParsing
154
+ if (-not (Download-Resume $modelUrl $modelFile)) {
155
+ Write-Host " SenseVoice 模型下载失败(网络不稳定)。请检查网络后重跑本脚本" -ForegroundColor Red
156
+ exit 1
111
157
  }
112
158
  New-Item -ItemType Directory -Force -Path $modelDir | Out-Null
113
159
  Write-Host " 解压模型..."
114
- tar -xjf $modelFile -C $modelDir --strip-components=1
160
+ if (-not (Expand-TarBz2 $modelFile $modelDir 1)) {
161
+ Write-Host " 模型压缩包损坏已删除,请重跑本脚本自动重新下载" -ForegroundColor Red
162
+ exit 1
163
+ }
164
+ if (-not (Test-Path "$modelDir\model.int8.onnx")) {
165
+ Write-Host " 解压后未找到 model.int8.onnx(压缩包异常),已清理。请重跑本脚本" -ForegroundColor Red
166
+ Remove-Item -Recurse -Force $modelDir -ErrorAction SilentlyContinue
167
+ Remove-Item -Force $modelFile -ErrorAction SilentlyContinue
168
+ exit 1
169
+ }
115
170
  Write-Host " 模型解压完成" -ForegroundColor Green
116
171
  } else {
117
172
  Write-Host " 模型已存在,跳过下载" -ForegroundColor Green
@@ -17,6 +17,46 @@
17
17
  $ErrorActionPreference = "Stop"
18
18
  $Version = "v1.13.6"
19
19
 
20
+ # ---- 下载/解压工具函数(2026-08-21 增强:断点续传 + 完整性校验 + 损坏自动重试)----
21
+ # ⚠️ PowerShell 坑:$ErrorActionPreference=Stop 时 curl/tar 写 stderr 会抛 NativeCommandError(0.3.5 实测踩中)。
22
+ # 原生命令一律 cmd /c 包装 + 2>nul + 临时放宽 EAP,只依据 $LASTEXITCODE。
23
+ function Download-Resume {
24
+ param([string]$Url, [string]$OutFile)
25
+ for ($attempt = 1; $attempt -le 3; $attempt++) {
26
+ Write-Host " 下载(第 $attempt 次尝试): $Url" -ForegroundColor Yellow
27
+ $prevEAP = $ErrorActionPreference
28
+ $ErrorActionPreference = "Continue"
29
+ cmd /c "curl.exe -sL -C - --retry 3 --retry-delay 2 --connect-timeout 20 -o `"$OutFile`" `"$Url`" 2>nul"
30
+ $code = $LASTEXITCODE
31
+ $ErrorActionPreference = $prevEAP
32
+ if ($code -eq 0) { return $true }
33
+ Write-Host " 下载中断(exit=$code),3 秒后重试..." -ForegroundColor Yellow
34
+ Start-Sleep -Seconds 3
35
+ }
36
+ return $false
37
+ }
38
+ function Expand-TarBz2 {
39
+ param([string]$Archive, [string]$Dest, [int]$Strip = 0)
40
+ $tmpDir = Join-Path $Dest ".extract-tmp"
41
+ New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null
42
+ $stripArgs = ""
43
+ if ($Strip -gt 0) { $stripArgs = "--strip-components=$Strip" }
44
+ $prevEAP = $ErrorActionPreference
45
+ $ErrorActionPreference = "Continue"
46
+ cmd /c "tar -xjf `"$Archive`" -C `"$tmpDir`" $stripArgs 2>nul"
47
+ $code = $LASTEXITCODE
48
+ $ErrorActionPreference = $prevEAP
49
+ if ($code -ne 0) {
50
+ Write-Host " 解压失败:压缩包损坏或不完整($Archive)" -ForegroundColor Red
51
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
52
+ Remove-Item -Force $Archive -ErrorAction SilentlyContinue
53
+ return $false
54
+ }
55
+ Get-ChildItem $tmpDir | Move-Item -Destination $Dest -Force -ErrorAction SilentlyContinue
56
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
57
+ return $true
58
+ }
59
+
20
60
  # ---- 0. 自动推导安装目录:脚本在 <插件包>/scripts/,安装到 <插件包>/sherpa-onnx/ ----
21
61
  $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
22
62
  $PluginRoot = Split-Path -Parent $ScriptDir
@@ -62,12 +102,15 @@ $pkgUrl = "https://github.com/k2-fsa/sherpa-onnx/releases/download/$Version/sher
62
102
  $pkgFile = "$InstallDir\tmp\sherpa-onnx.tar.bz2"
63
103
  if (-not (Test-Path "$InstallDir\bin\sherpa-onnx-offline-tts.exe")) {
64
104
  if (-not (Test-Path "$InstallDir\bin\sherpa-onnx-offline.exe")) {
65
- if (-not (Test-Path $pkgFile)) {
66
- Write-Host " 下载 $pkgUrl"
67
- Invoke-WebRequest -Uri $pkgUrl -OutFile $pkgFile -UseBasicParsing
105
+ if (-not (Download-Resume $pkgUrl $pkgFile)) {
106
+ Write-Host " sherpa-onnx 下载失败(网络不稳定)。请检查网络后重跑本脚本" -ForegroundColor Red
107
+ exit 1
68
108
  }
69
109
  Write-Host " 解压..."
70
- tar -xjf $pkgFile -C "$InstallDir\tmp"
110
+ if (-not (Expand-TarBz2 $pkgFile "$InstallDir\tmp")) {
111
+ Write-Host " sherpa-onnx 压缩包损坏已删除,请重跑本脚本自动重新下载" -ForegroundColor Red
112
+ exit 1
113
+ }
71
114
  $extracted = Get-ChildItem "$InstallDir\tmp" -Directory | Where-Object { $_.Name -like "sherpa-onnx-*win*" } | Select-Object -First 1
72
115
  if ($extracted) {
73
116
  Copy-Item "$($extracted.FullName)\bin\*" "$InstallDir\bin\" -Force
@@ -84,19 +127,28 @@ if (-not (Test-Path "$InstallDir\bin\sherpa-onnx-offline-tts.exe")) {
84
127
  Write-Host " sherpa-onnx 已存在,跳过下载" -ForegroundColor Green
85
128
  }
86
129
 
87
- # ---- 4. 下载 MeloTTS 中文模型 ----
130
+ # ---- 4. 下载 MeloTTS 中文模型(断点续传 + 完整性校验)----
88
131
  Write-Host "`n[3/3] 下载中文 MeloTTS VITS 模型..." -ForegroundColor Yellow
89
132
  $modelDir = "$InstallDir\models\melo"
90
133
  $modelUrl = "https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-melo-tts-zh_en.tar.bz2"
91
134
  $modelFile = "$InstallDir\tmp\melo.tar.bz2"
92
135
  if (-not (Test-Path "$modelDir\model.onnx")) {
93
- if (-not (Test-Path $modelFile)) {
94
- Write-Host " 下载 $modelUrl"
95
- Invoke-WebRequest -Uri $modelUrl -OutFile $modelFile -UseBasicParsing
136
+ if (-not (Download-Resume $modelUrl $modelFile)) {
137
+ Write-Host " MeloTTS 模型下载失败(网络不稳定)。请检查网络后重跑本脚本" -ForegroundColor Red
138
+ exit 1
96
139
  }
97
140
  New-Item -ItemType Directory -Force -Path $modelDir | Out-Null
98
141
  Write-Host " 解压模型..."
99
- tar -xjf $modelFile -C $modelDir --strip-components=1
142
+ if (-not (Expand-TarBz2 $modelFile $modelDir 1)) {
143
+ Write-Host " 模型压缩包损坏已删除,请重跑本脚本自动重新下载" -ForegroundColor Red
144
+ exit 1
145
+ }
146
+ if (-not (Test-Path "$modelDir\model.onnx")) {
147
+ Write-Host " 解压后未找到 model.onnx(压缩包异常),已清理。请重跑本脚本" -ForegroundColor Red
148
+ Remove-Item -Recurse -Force $modelDir -ErrorAction SilentlyContinue
149
+ Remove-Item -Force $modelFile -ErrorAction SilentlyContinue
150
+ exit 1
151
+ }
100
152
  Write-Host " 模型解压完成" -ForegroundColor Green
101
153
  } else {
102
154
  Write-Host " 模型已存在,跳过下载" -ForegroundColor Green