dsh-stm32 0.1.0 → 0.1.2
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 +127 -29
- package/README_EN.md +125 -29
- package/client/client.js +682 -0
- package/docs/development-notes.md +1385 -5
- package/docs/logic-audit.md +607 -0
- package/docs/reading-guide.md +738 -0
- package/lib/client.js +682 -0
- package/lib/commands/register.js +214 -12
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +316 -0
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/firmware-packages.js +352 -0
- package/lib/data/install-guides.js +190 -0
- package/lib/data/search-roots.js +135 -15
- package/lib/data/stm32f1.js +186 -0
- package/lib/data/toolchain-config.js +310 -0
- package/lib/engine/build.js +121 -16
- package/lib/engine/cache.js +336 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/code-patch.js +18 -6
- package/lib/engine/debug-session.js +163 -4
- package/lib/engine/debug.js +84 -19
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +287 -0
- package/lib/engine/firmware-install.js +668 -0
- package/lib/engine/flash.js +145 -15
- package/lib/engine/fs-longpath.js +107 -0
- package/lib/engine/generate-cache.js +118 -0
- package/lib/engine/generate-script.js +30 -8
- package/lib/engine/generate.js +296 -14
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +47 -8
- package/lib/engine/msys2-install.js +1870 -0
- package/lib/engine/probe.js +57 -6
- package/lib/engine/project-export.js +129 -0
- package/lib/engine/register-clock.js +132 -0
- package/lib/engine/register-linker.js +129 -0
- package/lib/engine/register-template.js +449 -0
- package/lib/engine/regs.js +18 -4
- package/lib/engine/route-plan.js +553 -0
- package/lib/engine/scaffold.js +303 -72
- package/lib/engine/subprocess-runner.js +2 -0
- package/lib/engine/tar.js +250 -0
- package/lib/engine/text-file.js +150 -0
- package/lib/engine/thirdparty.js +243 -0
- package/lib/engine/tool-env.js +51 -0
- package/lib/engine/toolchain-gate.js +386 -0
- package/lib/engine/vscode-config.js +99 -0
- package/lib/engine/web-api.js +375 -0
- package/lib/engine/web-routes.js +100 -0
- package/lib/engine/xcube-catalog.js +102 -0
- package/lib/engine/xcube.js +254 -0
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +44 -2
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +55 -3
- package/lib/tools/cache.js +250 -0
- package/lib/tools/code-patch.js +29 -2
- package/lib/tools/debug-session.js +56 -14
- package/lib/tools/debug.js +43 -2
- package/lib/tools/doctor.js +16 -3
- package/lib/tools/env-install.js +472 -0
- package/lib/tools/env-probe.js +6 -2
- package/lib/tools/firmware-install.js +255 -0
- package/lib/tools/flash.js +41 -2
- package/lib/tools/generate.js +96 -10
- package/lib/tools/ioc-apply.js +27 -11
- package/lib/tools/ioc-inspect.js +4 -2
- package/lib/tools/ioc-plan.js +209 -11
- package/lib/tools/project-create.js +229 -21
- package/lib/tools/project-dir.js +95 -0
- package/lib/tools/project-export.js +100 -0
- package/lib/tools/regs.js +82 -7
- package/lib/tools/route-plan.js +467 -0
- package/package.json +11 -4
- package/templates/register/CMakeLists.txt +52 -0
- package/templates/register/Core/Src/main.c +72 -0
- package/templates/scaffold/openocd.cfg +8 -0
- package/templates/scaffold/vscode/launch.json +8 -6
- package/templates/scaffold/vscode/settings.json +5 -11
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简下载器:按候选 URL 逐个试,成功一个就停。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有"候选 URL":同一份 ST 头文件,raw.githubusercontent.com 在有些网络里会超时、
|
|
5
|
+
* jsDelivr 在另一些网络里被墙——写死一个源就是把插件的可用性押在某条线路上。
|
|
6
|
+
* 实测(2026-10,本机):raw.githubusercontent 超时、github.com/codeload/api 正常、
|
|
7
|
+
* fastly.jsdelivr.net 与 ghproxy.net 正常 —— 所以候选顺序是 jsDelivr → raw → ghproxy。
|
|
8
|
+
*
|
|
9
|
+
* 只用全局 fetch(Node 18+ 自带),不引运行时依赖;测试可注入 fetchImpl。
|
|
10
|
+
*/
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
const HTML_HEAD_BYTES = 512;
|
|
15
|
+
/** F3c:10 s 内平均不足 1 KB/s 就 abort。半截文件留在 .part 里,下一秒从断点续。 */
|
|
16
|
+
const DEFAULT_STALL_MS = 10_000;
|
|
17
|
+
const DEFAULT_MIN_BYTES_PER_SEC = 1024;
|
|
18
|
+
let partSeq = 0;
|
|
19
|
+
/** 单个 .part 文件的大小;没有就是 0。 */
|
|
20
|
+
function partSize(file) {
|
|
21
|
+
try {
|
|
22
|
+
return fs.statSync(file).size;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 下一次文件到 partPath(F3c):支持从 startAt 字节续传。
|
|
30
|
+
*
|
|
31
|
+
* 三个必须的边界:
|
|
32
|
+
* 1. 服务器回 200 而不是 206 = 它忽略了 Range,必须**从头写**(截断),不能接在旧字节后面;
|
|
33
|
+
* 2. 10 s 观察窗口内平均速率不足 1 KB/s 就 abort(半截文件留着,交给上层续传或换源);
|
|
34
|
+
* 3. 外层 signal 取消要能立刻停(不能让一个 30 s 的下载拖住取消)。
|
|
35
|
+
*/
|
|
36
|
+
async function streamOnce(url, partPath, startAt, fetchImpl, options) {
|
|
37
|
+
const stallMs = options.stallMs ?? DEFAULT_STALL_MS;
|
|
38
|
+
const minRate = options.minBytesPerSec ?? DEFAULT_MIN_BYTES_PER_SEC;
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const overall = setTimeout(() => controller.abort(), options.timeoutMs ?? 30_000);
|
|
41
|
+
const onAbort = () => controller.abort();
|
|
42
|
+
if (options.signal !== undefined) {
|
|
43
|
+
if (options.signal.aborted)
|
|
44
|
+
controller.abort();
|
|
45
|
+
else
|
|
46
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
47
|
+
}
|
|
48
|
+
let slow = false;
|
|
49
|
+
let lastProgress = Date.now();
|
|
50
|
+
let windowStart = Date.now();
|
|
51
|
+
let windowBytes = 0;
|
|
52
|
+
const watchdog = setInterval(() => {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
if (now - lastProgress > stallMs) {
|
|
55
|
+
slow = true;
|
|
56
|
+
controller.abort();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const elapsed = now - windowStart;
|
|
60
|
+
if (elapsed >= stallMs) {
|
|
61
|
+
if ((windowBytes * 1000) / elapsed < minRate) {
|
|
62
|
+
slow = true;
|
|
63
|
+
controller.abort();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
windowStart = now;
|
|
67
|
+
windowBytes = 0;
|
|
68
|
+
}
|
|
69
|
+
}, Math.min(1000, Math.max(100, Math.floor(stallMs / 4))));
|
|
70
|
+
try {
|
|
71
|
+
const headers = {};
|
|
72
|
+
if (startAt > 0)
|
|
73
|
+
headers["Range"] = "bytes=" + String(startAt) + "-";
|
|
74
|
+
const response = await fetchImpl(url, { headers, signal: controller.signal, redirect: "follow" });
|
|
75
|
+
if (startAt > 0 && response.status === 416) {
|
|
76
|
+
return { ok: false, bytes: startAt, reason: "服务器说 Range 越界(416)", slow: false };
|
|
77
|
+
}
|
|
78
|
+
if (!response.ok && response.status !== 206) {
|
|
79
|
+
return { ok: false, bytes: 0, reason: "HTTP " + String(response.status), slow: false };
|
|
80
|
+
}
|
|
81
|
+
// 只有 206 才是"接着上次的字节";200 说明它整份重发,必须截断重写。
|
|
82
|
+
const append = startAt > 0 && response.status === 206;
|
|
83
|
+
const body = response.body;
|
|
84
|
+
if (body === null)
|
|
85
|
+
return { ok: false, bytes: 0, reason: "响应没有 body", slow: false };
|
|
86
|
+
const reader = body.getReader();
|
|
87
|
+
const fd = fs.openSync(partPath, append ? "a" : "w");
|
|
88
|
+
let written = append ? startAt : 0;
|
|
89
|
+
try {
|
|
90
|
+
for (;;) {
|
|
91
|
+
const chunk = await reader.read();
|
|
92
|
+
if (chunk.done)
|
|
93
|
+
break;
|
|
94
|
+
const value = chunk.value;
|
|
95
|
+
if (value === undefined)
|
|
96
|
+
continue;
|
|
97
|
+
fs.writeSync(fd, value);
|
|
98
|
+
written += value.byteLength;
|
|
99
|
+
windowBytes += value.byteLength;
|
|
100
|
+
lastProgress = Date.now();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
fs.closeSync(fd);
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, bytes: written, reason: "", slow: false };
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
const name = error instanceof Error ? error.name : "";
|
|
110
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
111
|
+
if (slow) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
bytes: partSize(partPath),
|
|
115
|
+
reason: "低速/停滞(" + String(Math.round(stallMs / 1000)) + " s 内平均不足 " + String(minRate) + " B/s)",
|
|
116
|
+
slow: true,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const timedOut = name === "AbortError";
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
bytes: partSize(partPath),
|
|
123
|
+
reason: timedOut
|
|
124
|
+
? "超时/取消(" + String(options.timeoutMs ?? 30_000) + " ms)"
|
|
125
|
+
: message,
|
|
126
|
+
slow: false,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(overall);
|
|
131
|
+
clearInterval(watchdog);
|
|
132
|
+
options.signal?.removeEventListener?.("abort", onAbort);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function looksLikeHtml(buffer) {
|
|
136
|
+
const head = buffer.subarray(0, HTML_HEAD_BYTES).toString("utf8").trimStart().toLowerCase();
|
|
137
|
+
return head.startsWith("<!doctype html") || head.startsWith("<html");
|
|
138
|
+
}
|
|
139
|
+
/** 内容特征校验:markers 是"至少命中一个",命中返回空数组,否则返回没命中的那几个(给报错用)。 */
|
|
140
|
+
function missingMarkers(buffer, markers) {
|
|
141
|
+
if (markers.length === 0)
|
|
142
|
+
return [];
|
|
143
|
+
const text = buffer.toString("utf8");
|
|
144
|
+
return markers.some((marker) => text.includes(marker)) ? [] : markers;
|
|
145
|
+
}
|
|
146
|
+
function removeQuietly(file) {
|
|
147
|
+
try {
|
|
148
|
+
fs.rmSync(file, { force: true });
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// 临时文件删不掉不影响这次的结论。
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 下载单个文件(多候选,F3c:同一源内断点续传)。失败不抛,把原因写进 outcome
|
|
156
|
+
* —— 上层要一次性报告所有失败。
|
|
157
|
+
*
|
|
158
|
+
* 为什么"同一个源内续传、换源必须从零":同一条线路上的 Range 续的是同一份文件;
|
|
159
|
+
* 换到另一个源时,镜像与上游的字节不保证是同一份(同步时点不同),接起来必然是坏文件。
|
|
160
|
+
*/
|
|
161
|
+
export async function fetchOne(target, destRoot, options = {}) {
|
|
162
|
+
const attempts = [];
|
|
163
|
+
const minBytes = target.minBytes ?? 16;
|
|
164
|
+
const dest = path.join(destRoot, target.path);
|
|
165
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
166
|
+
partSeq += 1;
|
|
167
|
+
const tag = String(process.pid) + "-" + String(partSeq);
|
|
168
|
+
const part = dest + ".part-" + tag;
|
|
169
|
+
const old = dest + ".old-" + tag;
|
|
170
|
+
// 取消判定包一层:直接比较 signal.aborted 会被上面的分支narrow 成 false,续传那行的条件就永远不成立。
|
|
171
|
+
const isCancelled = () => options.signal?.aborted === true;
|
|
172
|
+
for (const url of target.urls) {
|
|
173
|
+
if (isCancelled()) {
|
|
174
|
+
attempts.push(url + " → 已取消(没再试)");
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// 目录建不了会在写盘那一步报出来,这里不抢戏。
|
|
182
|
+
}
|
|
183
|
+
removeQuietly(part);
|
|
184
|
+
let streamed = await streamOnce(url, part, 0, fetchImpl, options);
|
|
185
|
+
// 只有"太慢/停滞"才值得在同一源上续传一次:断点已落在 .part 里,再从那里接着要。
|
|
186
|
+
if (!streamed.ok && streamed.slow && streamed.bytes > 0 && !isCancelled()) {
|
|
187
|
+
attempts.push(url + " → " + streamed.reason + "," + String(streamed.bytes) + " 字节已落盘,续传重试");
|
|
188
|
+
streamed = await streamOnce(url, part, streamed.bytes, fetchImpl, options);
|
|
189
|
+
}
|
|
190
|
+
if (!streamed.ok) {
|
|
191
|
+
attempts.push(url + " → " + streamed.reason);
|
|
192
|
+
removeQuietly(part);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const buffer = fs.readFileSync(part);
|
|
197
|
+
// 校验不过时必须连 .part 一起删(T1):早先只 continue,42 MB 的半成品会留在缓存目录里,
|
|
198
|
+
// 既不参与续传(换源从零)也没人清,几次失败就攒出几百 MB 垃圾。
|
|
199
|
+
const reject = (reason) => {
|
|
200
|
+
attempts.push(url + " → " + reason);
|
|
201
|
+
removeQuietly(part);
|
|
202
|
+
};
|
|
203
|
+
if (buffer.byteLength < minBytes) {
|
|
204
|
+
reject("只有 " + String(buffer.byteLength) + " 字节(< " + String(minBytes) + ",不像真文件)");
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (looksLikeHtml(buffer)) {
|
|
208
|
+
reject("拿回来是网页(代理拦截或 404 页),不是文件");
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const missed = missingMarkers(buffer, target.markers ?? []);
|
|
212
|
+
if (missed.length > 0) {
|
|
213
|
+
reject("内容对不上(找不到 " + missed.join(" / ") + "),不是要的那个文件");
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (target.sha256 !== undefined && target.sha256 !== "") {
|
|
217
|
+
const actual = createHash("sha256").update(buffer).digest("hex");
|
|
218
|
+
if (actual !== target.sha256.toLowerCase()) {
|
|
219
|
+
reject("sha256 对不上(期望 " + target.sha256.toLowerCase().slice(0, 16) + "…,实际 " + actual.slice(0, 16) +
|
|
220
|
+
"…)。多半是上游 master 挪动了内容:用真实 URL 重新实测哈希并更新 cmsis-sources。");
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// 先给旧文件改名、再把新课搬上来(A15):旧写法是「先删旧文件再 rename」,
|
|
225
|
+
// 第二步失败(Windows 上杀软扫描、目录只读、权限刚变)就新旧一起没了。
|
|
226
|
+
let movedOld = false;
|
|
227
|
+
try {
|
|
228
|
+
if (fs.existsSync(dest)) {
|
|
229
|
+
fs.renameSync(dest, old);
|
|
230
|
+
movedOld = true;
|
|
231
|
+
}
|
|
232
|
+
fs.renameSync(part, dest);
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
try {
|
|
236
|
+
if (movedOld && !fs.existsSync(dest))
|
|
237
|
+
fs.renameSync(old, dest);
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// 放不回去就只能留着 .old:把路径写进 attempts,至少人能手工找回。
|
|
241
|
+
}
|
|
242
|
+
removeQuietly(part);
|
|
243
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
244
|
+
attempts.push(url + " → 写盘失败(" + message + ")" + (movedOld ? ",旧文件已改名成 " + old : ""));
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (movedOld)
|
|
248
|
+
removeQuietly(old);
|
|
249
|
+
return { path: target.path, ok: true, bytes: buffer.byteLength, url, error: "", attempts };
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
253
|
+
attempts.push(url + " → " + message);
|
|
254
|
+
removeQuietly(part);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return { path: target.path, ok: false, bytes: 0, url: "", error: "所有源都没下下来:" + attempts.join(";"), attempts };
|
|
258
|
+
}
|
|
259
|
+
/** 并发下载一批(默认 3 路)。一个失败不影响其它,最后一次性汇总。 */
|
|
260
|
+
export async function fetchAll(targets, destRoot, options = {}) {
|
|
261
|
+
const results = [];
|
|
262
|
+
const total = targets.length;
|
|
263
|
+
const concurrency = Math.max(1, Math.min(options.concurrency ?? 3, total === 0 ? 1 : total));
|
|
264
|
+
let next = 0;
|
|
265
|
+
let done = 0;
|
|
266
|
+
const worker = async () => {
|
|
267
|
+
for (;;) {
|
|
268
|
+
const index = next;
|
|
269
|
+
next += 1;
|
|
270
|
+
const target = targets[index];
|
|
271
|
+
if (target === undefined)
|
|
272
|
+
return;
|
|
273
|
+
const outcome = await fetchOne(target, destRoot, options);
|
|
274
|
+
results.push(outcome);
|
|
275
|
+
done += 1;
|
|
276
|
+
options.onProgress?.(outcome, done, total);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
await Promise.all(Array.from({ length: concurrency }, () => worker()));
|
|
280
|
+
const ok = results.filter((item) => item.ok).length;
|
|
281
|
+
return {
|
|
282
|
+
results,
|
|
283
|
+
ok,
|
|
284
|
+
failed: results.length - ok,
|
|
285
|
+
bytes: results.reduce((sum, item) => sum + item.bytes, 0),
|
|
286
|
+
};
|
|
287
|
+
}
|