@mbws/plugin-sdk 0.3.15 → 0.3.17
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 +32 -17
- package/dist/caps.d.ts +14 -1
- package/dist/caps.js +2 -0
- package/dist/client.d.ts +8 -1
- package/dist/client.js +1 -0
- package/dist/dev-mock.d.ts +6 -4
- package/dist/dev-mock.js +63 -12
- package/dist/expand-zip.d.ts +15 -0
- package/dist/expand-zip.js +279 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/vite.d.ts +18 -4
- package/dist/vite.js +508 -58
- package/package.json +6 -2
- package/dist/viewer.d.ts +0 -30
- package/dist/viewer.js +0 -56
package/dist/vite.js
CHANGED
|
@@ -17,10 +17,12 @@
|
|
|
17
17
|
import { spawn, spawnSync } from "node:child_process";
|
|
18
18
|
import crypto from "node:crypto";
|
|
19
19
|
import fs from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
20
21
|
import path from "node:path";
|
|
21
22
|
import { pipeline } from "node:stream/promises";
|
|
22
23
|
import zlib from "node:zlib";
|
|
23
24
|
import { attachmentsOssBase, buildManifest, resolveAttachmentRefs, } from "./config.js";
|
|
25
|
+
import { ExpandZipError, expandZipVideos } from "./expand-zip.js";
|
|
24
26
|
import { generateContractTypes } from "./schema-types.js";
|
|
25
27
|
// 契约类型生成经本子路径暴露(Node 端工具链用;依赖 prettier,不进根入口)
|
|
26
28
|
export { generateContractTypes } from "./schema-types.js";
|
|
@@ -281,6 +283,28 @@ export function mbwsVitePlugin(opts) {
|
|
|
281
283
|
// 附件状态快照:面板首屏提示(missing/ready + 尽力而为的 totalBytes)
|
|
282
284
|
return handleDevAttachmentsSnapshot(config, projectRoot, res).catch(next);
|
|
283
285
|
}
|
|
286
|
+
if (pathname === "/__mbws__/dev/attachment-overrides") {
|
|
287
|
+
if (req.method === "GET") {
|
|
288
|
+
// 本地覆盖读面:每个声明附件的覆盖值与生效来源(env > .mbws 文件 > devPath > 官方库)
|
|
289
|
+
return handleDevAttachmentOverridesGet(config, projectRoot, res).catch(next);
|
|
290
|
+
}
|
|
291
|
+
if (req.method === "POST") {
|
|
292
|
+
// 本地覆盖写面:写 .mbws/attachment-overrides.json(欢迎页配置卡消费)
|
|
293
|
+
return handleDevAttachmentOverridesSet(projectRoot, req, res).catch(next);
|
|
294
|
+
}
|
|
295
|
+
res.statusCode = 405;
|
|
296
|
+
res.end();
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
if (pathname === "/__mbws__/dev/test-attachment-url") {
|
|
300
|
+
if (req.method !== "POST") {
|
|
301
|
+
res.statusCode = 405;
|
|
302
|
+
res.end();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
// 发布前自检:直链下载 → sha256/size → 解包 → 冒烟跑 entry,防给加工者配死链
|
|
306
|
+
return handleDevTestAttachmentUrl(req, res).catch(next);
|
|
307
|
+
}
|
|
284
308
|
if (pathname === "/__mbws__/dev/pick-directory") {
|
|
285
309
|
if (req.method !== "POST") {
|
|
286
310
|
res.statusCode = 405;
|
|
@@ -306,7 +330,20 @@ export function mbwsVitePlugin(opts) {
|
|
|
306
330
|
return;
|
|
307
331
|
}
|
|
308
332
|
// dev-mock data.inputs 供数:samples/inputs.json 原样返回;缺省回落 per-key 合并
|
|
309
|
-
return handleDevSamples(projectRoot, res).catch(next);
|
|
333
|
+
return handleDevSamples(projectRoot, res, "inputs").catch(next);
|
|
334
|
+
}
|
|
335
|
+
if (pathname === "/__mbws__/dev/samples/outputs") {
|
|
336
|
+
if (req.method === "GET") {
|
|
337
|
+
// dev-mock viewer.outputs / data.outputs 供数(与 inputs 侧同约定)
|
|
338
|
+
return handleDevSamples(projectRoot, res, "outputs").catch(next);
|
|
339
|
+
}
|
|
340
|
+
if (req.method === "POST") {
|
|
341
|
+
// dev-mock data.apply/commit 写回:deep-merge 进 samples/outputs.json(唯一数据源)
|
|
342
|
+
return handleDevSamplesWrite(projectRoot, req, res).catch(next);
|
|
343
|
+
}
|
|
344
|
+
res.statusCode = 405;
|
|
345
|
+
res.end();
|
|
346
|
+
return undefined;
|
|
310
347
|
}
|
|
311
348
|
if (pathname === "/__mbws__/dev/localize") {
|
|
312
349
|
const ref = new URL(req.url ?? "/", "http://dev").searchParams.get("ref") ?? "";
|
|
@@ -317,6 +354,15 @@ export function mbwsVitePlugin(opts) {
|
|
|
317
354
|
}
|
|
318
355
|
return handleDevLocalize(projectRoot, res, ref).catch(next);
|
|
319
356
|
}
|
|
357
|
+
if (pathname === "/__mbws__/dev/expand-zip") {
|
|
358
|
+
const rel = new URL(req.url ?? "/", "http://dev").searchParams.get("path") ?? "";
|
|
359
|
+
if (!rel) {
|
|
360
|
+
res.statusCode = 400;
|
|
361
|
+
res.end(JSON.stringify({ message: "缺少 path" }));
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
return handleDevExpandZip(projectRoot, res, rel).catch(next);
|
|
365
|
+
}
|
|
320
366
|
if (pathname === "/__mbws__/dev/file") {
|
|
321
367
|
const relPath = new URL(req.url ?? "/", "http://dev").searchParams.get("path") ?? "";
|
|
322
368
|
if (!relPath) {
|
|
@@ -384,20 +430,70 @@ export function currentAttachmentPlatform() {
|
|
|
384
430
|
}
|
|
385
431
|
/** dev 附件缓存目录(工程内,应进插件工程 .gitignore) */
|
|
386
432
|
const DEV_ATTACHMENT_CACHE = ".mbws/attachments";
|
|
433
|
+
/** 本机命令是否 PATH 可达(which / where 探测;探测失败一律按不可达回落官方库) */
|
|
434
|
+
function isCommandAvailable(cmd) {
|
|
435
|
+
const probe = process.platform === "win32" ? spawnSync("where", [cmd]) : spawnSync("which", [cmd]);
|
|
436
|
+
return probe.status === 0;
|
|
437
|
+
}
|
|
387
438
|
/**
|
|
388
|
-
* dev 用附件本地覆盖:env MBWS_DEV_ATTACHMENT_<NAME>(名字大写、- → _
|
|
389
|
-
*
|
|
390
|
-
*
|
|
439
|
+
* dev 用附件本地覆盖:env MBWS_DEV_ATTACHMENT_<NAME>(名字大写、- → _)优先,取值三形态——
|
|
440
|
+
* 本机命令名(如 ffmpeg,dev exec 直接 spawn,PATH 解析零下载)/ 本地路径(existsSync 命中)/
|
|
441
|
+
* https 临时直链(走直链下载分支,无 hash 校验);其次工程内 `.mbws/attachment-overrides.json`
|
|
442
|
+
* 本地文件(同三形态;dev 起来后在欢迎页可视化编辑,不进 git);再其次 attachments 声明里
|
|
443
|
+
* 的 devPath(线上还没有的新版本指本机路径)。
|
|
444
|
+
* 未配置 / 全不可用返回 null(走官方附件库自动下载)。
|
|
391
445
|
*/
|
|
392
|
-
export function resolveDevAttachmentOverride(config, name) {
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
446
|
+
export function resolveDevAttachmentOverride(config, name, root) {
|
|
447
|
+
const classify = (value) => {
|
|
448
|
+
if (/^https?:\/\//i.test(value))
|
|
449
|
+
return { kind: "url", url: value };
|
|
450
|
+
if (fs.existsSync(value))
|
|
451
|
+
return { kind: "path", path: value };
|
|
452
|
+
if (isCommandAvailable(value))
|
|
453
|
+
return { kind: "cmd", cmd: value };
|
|
454
|
+
// 配了但三形态都不成立(路径不存在且命令不在 PATH):忽略该值,让下一层/官方库说话
|
|
455
|
+
return null;
|
|
456
|
+
};
|
|
457
|
+
const envValue = process.env[`MBWS_DEV_ATTACHMENT_${name.replace(/-/g, "_").toUpperCase()}`]?.trim();
|
|
458
|
+
if (envValue) {
|
|
459
|
+
const hit = classify(envValue);
|
|
460
|
+
if (hit)
|
|
461
|
+
return hit;
|
|
462
|
+
}
|
|
463
|
+
if (root) {
|
|
464
|
+
const fileValue = readAttachmentOverridesFile(root)[name]?.trim();
|
|
465
|
+
if (fileValue) {
|
|
466
|
+
const hit = classify(fileValue);
|
|
467
|
+
if (hit)
|
|
468
|
+
return hit;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
396
471
|
const ref = (config.attachments ?? []).find((a) => a.name === name);
|
|
397
|
-
if (typeof ref?.devPath === "string" && fs.existsSync(ref.devPath))
|
|
398
|
-
return ref.devPath;
|
|
472
|
+
if (typeof ref?.devPath === "string" && fs.existsSync(ref.devPath)) {
|
|
473
|
+
return { kind: "path", path: ref.devPath };
|
|
474
|
+
}
|
|
399
475
|
return null;
|
|
400
476
|
}
|
|
477
|
+
/** 工程内本地覆盖文件(dev 配置页写入;.mbws 本就是 dev 缓存目录,不进 git) */
|
|
478
|
+
const ATTACHMENT_OVERRIDES_FILE = path.join(".mbws", "attachment-overrides.json");
|
|
479
|
+
function readAttachmentOverridesFile(root) {
|
|
480
|
+
try {
|
|
481
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(root, ATTACHMENT_OVERRIDES_FILE), "utf8"));
|
|
482
|
+
if (parsed && typeof parsed.overrides === "object" && parsed.overrides !== null) {
|
|
483
|
+
return parsed.overrides;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
// 不存在/损坏都当空(首次写会建文件)
|
|
488
|
+
}
|
|
489
|
+
return {};
|
|
490
|
+
}
|
|
491
|
+
function writeAttachmentOverridesFile(root, overrides) {
|
|
492
|
+
const file = path.join(root, ATTACHMENT_OVERRIDES_FILE);
|
|
493
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
494
|
+
const body = { version: 1, overrides };
|
|
495
|
+
fs.writeFileSync(file, `${JSON.stringify(body, null, 2)}\n`);
|
|
496
|
+
}
|
|
401
497
|
/** 同一附件并发解析去重(下载进行中第二个 spawn 等同一 Promise) */
|
|
402
498
|
const inflightDevAttachments = new Map();
|
|
403
499
|
/**
|
|
@@ -475,6 +571,45 @@ async function downloadToFile(url, dest, log, sizeHint) {
|
|
|
475
571
|
* 附件未声明 / 无 url 且库未登记返回 null(调用方回 501);下载/校验/解包失败抛错。
|
|
476
572
|
* log 收下载进度行(调用方转 NDJSON 事件流)。
|
|
477
573
|
*/
|
|
574
|
+
/**
|
|
575
|
+
* 直链下载就绪(声明 url 与 env 覆盖直链共用):无 sha256,缓存目录不带 hash 层
|
|
576
|
+
* (`.mbws/attachments/<name>@<version>/`,同 version 重传视为修正);按后缀分派
|
|
577
|
+
* tar.gz/zip 解包、.gz 单文件解压、其余当裸二进制。返回入口绝对路径。
|
|
578
|
+
*/
|
|
579
|
+
async function ensureDirectUrlAttachment(root, name, version, entry, directUrl, log) {
|
|
580
|
+
const platform = currentAttachmentPlatform();
|
|
581
|
+
const dir = path.join(root, DEV_ATTACHMENT_CACHE, `${name}@${version}`);
|
|
582
|
+
const entryPath = path.join(dir, entry);
|
|
583
|
+
if (fs.existsSync(entryPath))
|
|
584
|
+
return entryPath;
|
|
585
|
+
log(`[mbws:dev] 下载附件 ${name}@${version}(${platform} 直链,无 hash 校验)`);
|
|
586
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
587
|
+
const tmpPath = path.join(dir, "download.tmp");
|
|
588
|
+
try {
|
|
589
|
+
await downloadToFile(directUrl, tmpPath, log);
|
|
590
|
+
const lower = directUrl.toLowerCase();
|
|
591
|
+
if (/\.(tar\.gz|tgz|zip)$/.test(lower)) {
|
|
592
|
+
const extracted = spawnSync("tar", ["-xf", tmpPath, "-C", dir]);
|
|
593
|
+
if (extracted.status !== 0) {
|
|
594
|
+
throw new Error(`解包失败:${extracted.stderr?.toString().trim() || `tar exit ${extracted.status}`}`);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
else if (/\.gz$/.test(lower)) {
|
|
598
|
+
await pipeline(fs.createReadStream(tmpPath), zlib.createGunzip(), fs.createWriteStream(entryPath));
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
fs.copyFileSync(tmpPath, entryPath);
|
|
602
|
+
}
|
|
603
|
+
if (!fs.existsSync(entryPath))
|
|
604
|
+
throw new Error(`解包后未见入口 ${entry}`);
|
|
605
|
+
fs.chmodSync(entryPath, 0o755);
|
|
606
|
+
log(`[mbws:dev] 附件 ${name}@${version} 就绪(已缓存,后续零下载)`);
|
|
607
|
+
return entryPath;
|
|
608
|
+
}
|
|
609
|
+
finally {
|
|
610
|
+
fs.rmSync(tmpPath, { force: true });
|
|
611
|
+
}
|
|
612
|
+
}
|
|
478
613
|
async function ensureLibraryAttachment(config, root, name, log) {
|
|
479
614
|
const key = `${root}::${name}`;
|
|
480
615
|
const inflight = inflightDevAttachments.get(key);
|
|
@@ -490,37 +625,7 @@ async function ensureLibraryAttachment(config, root, name, log) {
|
|
|
490
625
|
// —— url 直链分支(开发者提供的官方地址)——
|
|
491
626
|
const directUrl = typeof ref.url === "string" ? ref.url : ref.url?.[platform];
|
|
492
627
|
if (directUrl) {
|
|
493
|
-
|
|
494
|
-
const entryPath = path.join(dir, entry);
|
|
495
|
-
if (fs.existsSync(entryPath))
|
|
496
|
-
return entryPath;
|
|
497
|
-
log(`[mbws:dev] 下载附件 ${name}@${ref.version}(${platform} 直链,无 hash 校验)`);
|
|
498
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
499
|
-
const tmpPath = path.join(dir, "download.tmp");
|
|
500
|
-
try {
|
|
501
|
-
await downloadToFile(directUrl, tmpPath, log);
|
|
502
|
-
const lower = directUrl.toLowerCase();
|
|
503
|
-
if (/\.(tar\.gz|tgz|zip)$/.test(lower)) {
|
|
504
|
-
const extracted = spawnSync("tar", ["-xf", tmpPath, "-C", dir]);
|
|
505
|
-
if (extracted.status !== 0) {
|
|
506
|
-
throw new Error(`解包失败:${extracted.stderr?.toString().trim() || `tar exit ${extracted.status}`}`);
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
else if (/\.gz$/.test(lower)) {
|
|
510
|
-
await pipeline(fs.createReadStream(tmpPath), zlib.createGunzip(), fs.createWriteStream(entryPath));
|
|
511
|
-
}
|
|
512
|
-
else {
|
|
513
|
-
fs.copyFileSync(tmpPath, entryPath);
|
|
514
|
-
}
|
|
515
|
-
if (!fs.existsSync(entryPath))
|
|
516
|
-
throw new Error(`解包后未见入口 ${entry}`);
|
|
517
|
-
fs.chmodSync(entryPath, 0o755);
|
|
518
|
-
log(`[mbws:dev] 附件 ${name}@${ref.version} 就绪(已缓存,后续零下载)`);
|
|
519
|
-
return entryPath;
|
|
520
|
-
}
|
|
521
|
-
finally {
|
|
522
|
-
fs.rmSync(tmpPath, { force: true });
|
|
523
|
-
}
|
|
628
|
+
return await ensureDirectUrlAttachment(root, name, ref.version, entry, directUrl, log);
|
|
524
629
|
}
|
|
525
630
|
// —— 官方附件库分支(OSS 公共读直连:index.json 拿 hash,下载地址内容寻址自拼) ——
|
|
526
631
|
const resolved = await resolveLibraryIndex(name, ref.version, platform);
|
|
@@ -561,16 +666,41 @@ async function ensureLibraryAttachment(config, root, name, log) {
|
|
|
561
666
|
}
|
|
562
667
|
}
|
|
563
668
|
/**
|
|
564
|
-
* dev 附件准备(exec / ensure-attachment 共用):本地覆盖(env / devPath
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
669
|
+
* dev 附件准备(exec / ensure-attachment 共用):本地覆盖(env / devPath)零网络——
|
|
670
|
+
* env 覆盖三形态:本机命令名(直接返回命令名,spawn 走 PATH)/ 本地路径 / https 临时
|
|
671
|
+
* 直链(与声明 url 同一条直链下载分支);否则按 attachments 声明自动下载官方附件库
|
|
672
|
+
* /直链缓存(下载进度行经 write 流回)。返回入口路径(或命令名)。
|
|
673
|
+
* 不可用(未声明 / 库未登记)时已写好 501 响应;下载/校验/解包失败时已按「附件 X
|
|
674
|
+
* 下载失败」exit 事件收尾——两种失败都返回 null 且响应已结束,调用方直接返回。
|
|
568
675
|
*/
|
|
569
676
|
async function prepareDevAttachment(config, root, attachment, write, res) {
|
|
570
|
-
|
|
677
|
+
const log = (text) => write({ kind: "lines", lines: [{ stream: "stderr", text }] });
|
|
678
|
+
const override = resolveDevAttachmentOverride(config, attachment, root);
|
|
679
|
+
let binPath = null;
|
|
680
|
+
if (override?.kind === "path") {
|
|
681
|
+
binPath = override.path;
|
|
682
|
+
}
|
|
683
|
+
else if (override?.kind === "cmd") {
|
|
684
|
+
binPath = override.cmd;
|
|
685
|
+
}
|
|
686
|
+
else if (override?.kind === "url") {
|
|
687
|
+
const ref = (config.attachments ?? []).find((a) => a.name === attachment);
|
|
688
|
+
const platform = currentAttachmentPlatform();
|
|
689
|
+
const entry = ref?.entry ?? `${attachment}${platform.startsWith("win") ? ".exe" : ""}`;
|
|
690
|
+
try {
|
|
691
|
+
// 覆盖直链与声明 url 同一条分支(无 hash 校验,缓存同目录同 version 语义)
|
|
692
|
+
binPath = await ensureDirectUrlAttachment(root, attachment, ref?.version ?? "override", entry, override.url, log);
|
|
693
|
+
}
|
|
694
|
+
catch (error) {
|
|
695
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
696
|
+
write({ kind: "exit", exitCode: -1, stderrTail: `附件 ${attachment} 下载失败:${message}` });
|
|
697
|
+
res.end();
|
|
698
|
+
return null;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
571
701
|
if (!binPath) {
|
|
572
702
|
try {
|
|
573
|
-
binPath = await ensureLibraryAttachment(config, root, attachment,
|
|
703
|
+
binPath = await ensureLibraryAttachment(config, root, attachment, log);
|
|
574
704
|
}
|
|
575
705
|
catch (error) {
|
|
576
706
|
// 下载/校验/解包失败:已经开始流式响应(可能已写过进度行),按 job 失败收尾
|
|
@@ -582,11 +712,12 @@ async function prepareDevAttachment(config, root, attachment, write, res) {
|
|
|
582
712
|
if (!binPath) {
|
|
583
713
|
// 501 = 能力明确不可用:SDK dev-mock 收到非 2xx 退模拟执行
|
|
584
714
|
const declared = (config.attachments ?? []).some((a) => a.name === attachment);
|
|
715
|
+
const envName = `MBWS_DEV_ATTACHMENT_${attachment.replace(/-/g, "_").toUpperCase()}`;
|
|
585
716
|
res.statusCode = 501;
|
|
586
717
|
res.end(JSON.stringify({
|
|
587
718
|
message: declared
|
|
588
|
-
? `附件 ${attachment} 无 url 直链且未在官方附件库(OSS
|
|
589
|
-
: `mbws.config.ts 的 attachments 未声明附件 ${attachment}(dev
|
|
719
|
+
? `附件 ${attachment} 无 url 直链且未在官方附件库(OSS)登记当前平台产物(或不可达);本机覆盖可配 env ${envName}(取值:本机命令名 / 本地路径 / https 直链),或 attachments 的 url 直链 / devPath`
|
|
720
|
+
: `mbws.config.ts 的 attachments 未声明附件 ${attachment}(dev 自动下载依赖声明);本机覆盖可配 env ${envName}(取值:本机命令名 / 本地路径 / https 直链),或在该项声明 devPath`,
|
|
590
721
|
}));
|
|
591
722
|
return null;
|
|
592
723
|
}
|
|
@@ -735,7 +866,7 @@ async function handleDevAttachmentsSnapshot(config, root, res) {
|
|
|
735
866
|
const entry = ref.entry ?? `${ref.name}${platform.startsWith("win") ? ".exe" : ""}`;
|
|
736
867
|
let status = "missing";
|
|
737
868
|
let totalBytes;
|
|
738
|
-
if (resolveDevAttachmentOverride(config, ref.name)) {
|
|
869
|
+
if (resolveDevAttachmentOverride(config, ref.name, root)) {
|
|
739
870
|
status = "ready";
|
|
740
871
|
}
|
|
741
872
|
else {
|
|
@@ -770,6 +901,229 @@ async function handleDevAttachmentsSnapshot(config, root, res) {
|
|
|
770
901
|
res.setHeader("cache-control", "no-store");
|
|
771
902
|
res.end(JSON.stringify(items));
|
|
772
903
|
}
|
|
904
|
+
/**
|
|
905
|
+
* GET /__mbws__/dev/attachment-overrides:本地覆盖读面(欢迎页配置卡消费)。
|
|
906
|
+
* 每个声明附件一条:当前各层配置值(env / .mbws 文件)+ 生效来源与形态
|
|
907
|
+
* (env > file > devPath > library)。
|
|
908
|
+
*/
|
|
909
|
+
async function handleDevAttachmentOverridesGet(config, root, res) {
|
|
910
|
+
const fileOverrides = readAttachmentOverridesFile(root);
|
|
911
|
+
const items = (config.attachments ?? []).map((ref) => {
|
|
912
|
+
const envName = `MBWS_DEV_ATTACHMENT_${ref.name.replace(/-/g, "_").toUpperCase()}`;
|
|
913
|
+
const envValue = process.env[envName]?.trim() ?? "";
|
|
914
|
+
const fileValue = fileOverrides[ref.name]?.trim() ?? "";
|
|
915
|
+
const classify = (value) => {
|
|
916
|
+
if (/^https?:\/\//i.test(value))
|
|
917
|
+
return { kind: "url", url: value };
|
|
918
|
+
if (fs.existsSync(value))
|
|
919
|
+
return { kind: "path", path: value };
|
|
920
|
+
if (isCommandAvailable(value))
|
|
921
|
+
return { kind: "cmd", cmd: value };
|
|
922
|
+
return null;
|
|
923
|
+
};
|
|
924
|
+
let source = "library";
|
|
925
|
+
let effective = null;
|
|
926
|
+
const envHit = classify(envValue);
|
|
927
|
+
const fileHit = classify(fileValue);
|
|
928
|
+
if (envHit) {
|
|
929
|
+
source = "env";
|
|
930
|
+
effective = envHit;
|
|
931
|
+
}
|
|
932
|
+
else if (fileHit) {
|
|
933
|
+
source = "file";
|
|
934
|
+
effective = fileHit;
|
|
935
|
+
}
|
|
936
|
+
else if (typeof ref.devPath === "string" && fs.existsSync(ref.devPath)) {
|
|
937
|
+
source = "devPath";
|
|
938
|
+
effective = { kind: "path", path: ref.devPath };
|
|
939
|
+
}
|
|
940
|
+
return {
|
|
941
|
+
name: ref.name,
|
|
942
|
+
version: ref.version,
|
|
943
|
+
// 生效覆盖(null = 走官方附件库自动下载)
|
|
944
|
+
override: effective,
|
|
945
|
+
source,
|
|
946
|
+
envName,
|
|
947
|
+
envValue,
|
|
948
|
+
fileValue,
|
|
949
|
+
// env 配了但不合法的提示(路径不存在且不在 PATH)
|
|
950
|
+
...(envValue && !classify(envValue) ? { envInvalid: true } : {}),
|
|
951
|
+
};
|
|
952
|
+
});
|
|
953
|
+
res.setHeader("content-type", "application/json");
|
|
954
|
+
res.setHeader("cache-control", "no-store");
|
|
955
|
+
res.end(JSON.stringify(items));
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* POST /__mbws__/dev/attachment-overrides:本地覆盖写面(欢迎页配置卡消费)。
|
|
959
|
+
* body { name, value }——value 非空须能归类(https 直链 / 真实路径 / PATH 命令名),
|
|
960
|
+
* 空串 = 删除该项。写 `.mbws/attachment-overrides.json`(纯本机配置,不进 git、不进产物)。
|
|
961
|
+
*/
|
|
962
|
+
async function handleDevAttachmentOverridesSet(root, req, res) {
|
|
963
|
+
let body;
|
|
964
|
+
try {
|
|
965
|
+
body = JSON.parse((await readRequestBody(req)).toString("utf8"));
|
|
966
|
+
}
|
|
967
|
+
catch {
|
|
968
|
+
res.statusCode = 400;
|
|
969
|
+
res.end(JSON.stringify({ message: "body 不是合法 JSON" }));
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
const { name, value } = body;
|
|
973
|
+
if (typeof name !== "string" ||
|
|
974
|
+
!/^[a-z][a-z0-9-]{1,63}$/.test(name) ||
|
|
975
|
+
typeof value !== "string") {
|
|
976
|
+
res.statusCode = 400;
|
|
977
|
+
res.end(JSON.stringify({ message: "name(附件名) 与 value(string,空=删除) 必填" }));
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
const trimmed = value.trim();
|
|
981
|
+
if (trimmed) {
|
|
982
|
+
const looksValid = /^https?:\/\//i.test(trimmed) || fs.existsSync(trimmed) || isCommandAvailable(trimmed);
|
|
983
|
+
if (!looksValid) {
|
|
984
|
+
res.statusCode = 400;
|
|
985
|
+
res.end(JSON.stringify({
|
|
986
|
+
message: `覆盖值无法识别:既不是 https 直链,路径也不存在,也不在 PATH——检查拼写(可填本机命令名 / 本地路径 / https 临时直链)`,
|
|
987
|
+
}));
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
const overrides = readAttachmentOverridesFile(root);
|
|
992
|
+
if (trimmed) {
|
|
993
|
+
overrides[name] = trimmed;
|
|
994
|
+
}
|
|
995
|
+
else {
|
|
996
|
+
delete overrides[name];
|
|
997
|
+
}
|
|
998
|
+
writeAttachmentOverridesFile(root, overrides);
|
|
999
|
+
res.setHeader("content-type", "application/json");
|
|
1000
|
+
res.setHeader("cache-control", "no-store");
|
|
1001
|
+
res.end(JSON.stringify({ name, value: trimmed, saved: true }));
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* POST /__mbws__/dev/test-attachment-url:发布前直链自检——下载 → sha256/size →
|
|
1005
|
+
* 按后缀解包 → 冒烟跑 entry(--version,短超时)→ 清理临时目录。
|
|
1006
|
+
* body { url, entry?, smoke? };返回 { ok, sha256, size, archive, smoke: { exitCode, output } | "skipped" }。
|
|
1007
|
+
* 冒烟非零退出不算失败(不少工具不认 --version),只回显 exitCode 与输出供人判断。
|
|
1008
|
+
*/
|
|
1009
|
+
async function handleDevTestAttachmentUrl(req, res) {
|
|
1010
|
+
let body;
|
|
1011
|
+
try {
|
|
1012
|
+
body = JSON.parse((await readRequestBody(req)).toString("utf8"));
|
|
1013
|
+
}
|
|
1014
|
+
catch {
|
|
1015
|
+
res.statusCode = 400;
|
|
1016
|
+
res.end(JSON.stringify({ message: "body 不是合法 JSON" }));
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const { url, entry } = body;
|
|
1020
|
+
if (typeof url !== "string" ||
|
|
1021
|
+
!/^https?:\/\//i.test(url) ||
|
|
1022
|
+
(entry !== undefined && typeof entry !== "string")) {
|
|
1023
|
+
res.statusCode = 400;
|
|
1024
|
+
res.end(JSON.stringify({ message: "url(https 直链) 必填,entry(string) 选填" }));
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
const smoke = body.smoke !== false;
|
|
1028
|
+
const entryName = (typeof entry === "string" && entry.trim()) || "attachment-entry";
|
|
1029
|
+
const log = (_text) => { }; // 静默(一次性自检,不打进度)
|
|
1030
|
+
// 系统临时目录(不落工程 .mbws——自检完即清理,避免污染附件缓存目录)
|
|
1031
|
+
const dir = fs.mkdtempSync(path.join(tmpdir(), "mbws-url-test-"));
|
|
1032
|
+
try {
|
|
1033
|
+
const tmpPath = path.join(dir, "download.bin");
|
|
1034
|
+
let sha256;
|
|
1035
|
+
try {
|
|
1036
|
+
sha256 = await downloadToFile(url, tmpPath, log);
|
|
1037
|
+
}
|
|
1038
|
+
catch (error) {
|
|
1039
|
+
res.setHeader("content-type", "application/json");
|
|
1040
|
+
res.end(JSON.stringify({
|
|
1041
|
+
ok: false,
|
|
1042
|
+
stage: "download",
|
|
1043
|
+
message: error instanceof Error ? error.message : String(error),
|
|
1044
|
+
}));
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
const size = fs.statSync(tmpPath).size;
|
|
1048
|
+
const lower = url.toLowerCase().split("?")[0];
|
|
1049
|
+
const archive = /\.(tar\.gz|tgz)$/.test(lower)
|
|
1050
|
+
? "tar.gz"
|
|
1051
|
+
: /\.zip$/.test(lower)
|
|
1052
|
+
? "zip"
|
|
1053
|
+
: /\.gz$/.test(lower)
|
|
1054
|
+
? "gz"
|
|
1055
|
+
: "bare";
|
|
1056
|
+
let entryPath;
|
|
1057
|
+
if (archive === "tar.gz" || archive === "zip") {
|
|
1058
|
+
const extracted = spawnSync("tar", ["-xf", tmpPath, "-C", dir]);
|
|
1059
|
+
if (extracted.status !== 0) {
|
|
1060
|
+
res.setHeader("content-type", "application/json");
|
|
1061
|
+
res.end(JSON.stringify({
|
|
1062
|
+
ok: false,
|
|
1063
|
+
stage: "extract",
|
|
1064
|
+
archive,
|
|
1065
|
+
sha256,
|
|
1066
|
+
size,
|
|
1067
|
+
message: extracted.stderr?.toString().trim() || `tar exit ${extracted.status}`,
|
|
1068
|
+
}));
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
// 优先匹配 entry 名(含 .exe 变体),缺省取第一个文件
|
|
1072
|
+
const files = fs
|
|
1073
|
+
.readdirSync(dir, { recursive: true })
|
|
1074
|
+
.map((f) => String(f))
|
|
1075
|
+
.filter((f) => f !== "download.bin" && fs.statSync(path.join(dir, f)).isFile());
|
|
1076
|
+
const picked = files.find((f) => {
|
|
1077
|
+
const base = path.basename(f);
|
|
1078
|
+
return base === entryName || base === `${entryName}.exe`;
|
|
1079
|
+
}) ?? files[0];
|
|
1080
|
+
if (!picked) {
|
|
1081
|
+
res.setHeader("content-type", "application/json");
|
|
1082
|
+
res.end(JSON.stringify({
|
|
1083
|
+
ok: false,
|
|
1084
|
+
stage: "extract",
|
|
1085
|
+
archive,
|
|
1086
|
+
sha256,
|
|
1087
|
+
size,
|
|
1088
|
+
message: "包内没有文件条目",
|
|
1089
|
+
}));
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
entryPath = path.join(dir, picked);
|
|
1093
|
+
}
|
|
1094
|
+
else if (archive === "gz") {
|
|
1095
|
+
entryPath = path.join(dir, entryName);
|
|
1096
|
+
await pipeline(fs.createReadStream(tmpPath), zlib.createGunzip(), fs.createWriteStream(entryPath));
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
entryPath = tmpPath;
|
|
1100
|
+
}
|
|
1101
|
+
if (archive !== "tar.gz" && archive !== "zip")
|
|
1102
|
+
fs.chmodSync(entryPath, 0o755);
|
|
1103
|
+
let smokeResult = "skipped";
|
|
1104
|
+
if (smoke) {
|
|
1105
|
+
const run = spawnSync(entryPath, ["--version"], { timeout: 15_000, encoding: "utf8" });
|
|
1106
|
+
const output = `${run.stdout ?? ""}${run.stderr ?? ""}`
|
|
1107
|
+
.trim()
|
|
1108
|
+
.split("\n")
|
|
1109
|
+
.slice(0, 3)
|
|
1110
|
+
.join("\n");
|
|
1111
|
+
smokeResult = { exitCode: run.status ?? -1, output };
|
|
1112
|
+
}
|
|
1113
|
+
res.setHeader("content-type", "application/json");
|
|
1114
|
+
res.end(JSON.stringify({
|
|
1115
|
+
ok: true,
|
|
1116
|
+
sha256,
|
|
1117
|
+
size,
|
|
1118
|
+
archive,
|
|
1119
|
+
entry: path.basename(entryPath),
|
|
1120
|
+
smoke: smokeResult,
|
|
1121
|
+
}));
|
|
1122
|
+
}
|
|
1123
|
+
finally {
|
|
1124
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
773
1127
|
/**
|
|
774
1128
|
* POST /__mbws__/dev/pick-directory:dev 无系统选择器——固定指 `.mbws/dev-outputs/`
|
|
775
1129
|
* (绝对路径,目录即建)。插件拿路径拼产物输出位置,spawn(cwd=工程根)直接写绝对路径。
|
|
@@ -808,17 +1162,19 @@ async function handleDevPickFile(root, req, res) {
|
|
|
808
1162
|
res.setHeader("cache-control", "no-store");
|
|
809
1163
|
res.end(JSON.stringify({ path: picked }));
|
|
810
1164
|
}
|
|
1165
|
+
/** per-key 回落的保留文件名:整对象单文件不进 per-key 合并(inputs/outputs 互不串门) */
|
|
1166
|
+
const SAMPLES_RESERVED = new Set(["inputs.json", "outputs.json"]);
|
|
811
1167
|
/**
|
|
812
|
-
* GET /__mbws__/dev/samples
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
1168
|
+
* GET /__mbws__/dev/samples(inputs 侧)/ GET /__mbws__/dev/samples/outputs(outputs 侧):
|
|
1169
|
+
* dev-mock 夹具供数端点。优先 `samples/<kind>.json`——内容原样返回(写什么得什么,
|
|
1170
|
+
* 直觉一致);文件不存在时回落 per-key 合并(`samples/<键>.json`,文件名即键,
|
|
1171
|
+
* 保留文件名两侧均排除)。目录不存在/单文件解析失败均容错(跳过该键),不阻塞 dev。
|
|
816
1172
|
*/
|
|
817
|
-
async function handleDevSamples(root, res) {
|
|
1173
|
+
async function handleDevSamples(root, res, kind) {
|
|
818
1174
|
const dir = path.join(root, "samples");
|
|
819
|
-
// 单文件约定:samples
|
|
1175
|
+
// 单文件约定:samples/<kind>.json 即整个对象,原样返回
|
|
820
1176
|
try {
|
|
821
|
-
const whole = JSON.parse(await fs.promises.readFile(path.join(dir,
|
|
1177
|
+
const whole = JSON.parse(await fs.promises.readFile(path.join(dir, `${kind}.json`), "utf8"));
|
|
822
1178
|
res.setHeader("content-type", "application/json");
|
|
823
1179
|
res.setHeader("cache-control", "no-store");
|
|
824
1180
|
res.end(JSON.stringify(whole));
|
|
@@ -831,7 +1187,7 @@ async function handleDevSamples(root, res) {
|
|
|
831
1187
|
try {
|
|
832
1188
|
const entries = await fs.promises.readdir(dir, { withFileTypes: true });
|
|
833
1189
|
await Promise.all(entries
|
|
834
|
-
.filter((e) => e.isFile() && e.name.endsWith(".json") && e.name
|
|
1190
|
+
.filter((e) => e.isFile() && e.name.endsWith(".json") && !SAMPLES_RESERVED.has(e.name))
|
|
835
1191
|
.map(async (e) => {
|
|
836
1192
|
try {
|
|
837
1193
|
result[e.name.slice(0, -".json".length)] = JSON.parse(await fs.promises.readFile(path.join(dir, e.name), "utf8"));
|
|
@@ -848,6 +1204,60 @@ async function handleDevSamples(root, res) {
|
|
|
848
1204
|
res.setHeader("cache-control", "no-store");
|
|
849
1205
|
res.end(JSON.stringify(result));
|
|
850
1206
|
}
|
|
1207
|
+
/** deep-merge:纯对象递归合并(null/数组/标量整体覆盖)——apply/commit 增量写回用 */
|
|
1208
|
+
function deepMergeRecord(base, patch) {
|
|
1209
|
+
const merged = base && typeof base === "object" && !Array.isArray(base)
|
|
1210
|
+
? { ...base }
|
|
1211
|
+
: {};
|
|
1212
|
+
if (patch && typeof patch === "object" && !Array.isArray(patch)) {
|
|
1213
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
1214
|
+
merged[key] =
|
|
1215
|
+
value && typeof value === "object" && !Array.isArray(value)
|
|
1216
|
+
? deepMergeRecord(merged[key], value)
|
|
1217
|
+
: value;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
return merged;
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* POST /__mbws__/dev/samples/outputs:dev-mock `data.apply`/`data.commit` 写回端点。
|
|
1224
|
+
* body = outputs 对象,与既有 `samples/outputs.json` deep-merge 后 pretty 写盘
|
|
1225
|
+
* (文件/目录不存在即建)——samples 目录是 dev 下 panel/viewer 的唯一数据源,
|
|
1226
|
+
* 面板提交后 viewer.html 刷新即反演。每次现读现写,改文件即时生效。
|
|
1227
|
+
*/
|
|
1228
|
+
async function handleDevSamplesWrite(root, req, res) {
|
|
1229
|
+
let body;
|
|
1230
|
+
try {
|
|
1231
|
+
body = JSON.parse((await readRequestBody(req)).toString("utf8"));
|
|
1232
|
+
}
|
|
1233
|
+
catch {
|
|
1234
|
+
res.statusCode = 400;
|
|
1235
|
+
res.setHeader("content-type", "application/json");
|
|
1236
|
+
res.end(JSON.stringify({ message: "body 不是合法 JSON" }));
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
1240
|
+
res.statusCode = 400;
|
|
1241
|
+
res.setHeader("content-type", "application/json");
|
|
1242
|
+
res.end(JSON.stringify({ message: "body 须是 outputs 对象" }));
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
const dir = path.join(root, "samples");
|
|
1246
|
+
const file = path.join(dir, "outputs.json");
|
|
1247
|
+
let existing = {};
|
|
1248
|
+
try {
|
|
1249
|
+
existing = JSON.parse(await fs.promises.readFile(file, "utf8"));
|
|
1250
|
+
}
|
|
1251
|
+
catch {
|
|
1252
|
+
// 不存在/坏 JSON 从空对象起步(坏文件被本次写回覆盖,属修复而非损失)
|
|
1253
|
+
}
|
|
1254
|
+
const merged = deepMergeRecord(existing, body);
|
|
1255
|
+
await fs.promises.mkdir(dir, { recursive: true });
|
|
1256
|
+
await fs.promises.writeFile(file, `${JSON.stringify(merged, null, 2)}\n`, "utf8");
|
|
1257
|
+
res.setHeader("content-type", "application/json");
|
|
1258
|
+
res.setHeader("cache-control", "no-store");
|
|
1259
|
+
res.end(JSON.stringify(merged));
|
|
1260
|
+
}
|
|
851
1261
|
/** GET /__mbws__/dev/localize?ref=:映射到工程内 samples/ 夹具(存在才 200) */
|
|
852
1262
|
async function handleDevLocalize(root, res, ref) {
|
|
853
1263
|
const basename = ref.split("/").pop() ?? "";
|
|
@@ -866,6 +1276,46 @@ async function handleDevLocalize(root, res, ref) {
|
|
|
866
1276
|
res.statusCode = 404;
|
|
867
1277
|
res.end(JSON.stringify({ message: `samples/${basename} 不存在` }));
|
|
868
1278
|
}
|
|
1279
|
+
/** GET /__mbws__/dev/expand-zip?path=:把工程内 zip 解到 .mbws/expanded,返回可预览视频。 */
|
|
1280
|
+
async function handleDevExpandZip(root, res, relPath) {
|
|
1281
|
+
const rootResolved = path.resolve(root);
|
|
1282
|
+
const archive = path.resolve(root, relPath);
|
|
1283
|
+
if (archive !== rootResolved && !archive.startsWith(`${rootResolved}${path.sep}`)) {
|
|
1284
|
+
res.statusCode = 403;
|
|
1285
|
+
res.end(JSON.stringify({ message: "path 超出工程目录" }));
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
const id = crypto.createHash("sha256").update(relPath).digest("hex").slice(0, 16);
|
|
1289
|
+
const dest = path.join(root, ".mbws", "expanded", id);
|
|
1290
|
+
await fs.promises.rm(dest, { recursive: true, force: true });
|
|
1291
|
+
try {
|
|
1292
|
+
const videos = await expandZipVideos(archive, dest);
|
|
1293
|
+
if (videos.length === 0) {
|
|
1294
|
+
res.statusCode = 422;
|
|
1295
|
+
res.end(JSON.stringify({ message: "压缩包内没有可识别的视频" }));
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
const payload = {
|
|
1299
|
+
videos: videos.map((video) => {
|
|
1300
|
+
const rel = path.posix.join(".mbws", "expanded", id, video.relPath);
|
|
1301
|
+
return {
|
|
1302
|
+
path: rel,
|
|
1303
|
+
filename: video.filename,
|
|
1304
|
+
entryName: video.entryName,
|
|
1305
|
+
previewUrl: `/__mbws__/dev/file?path=${encodeURIComponent(rel)}`,
|
|
1306
|
+
};
|
|
1307
|
+
}),
|
|
1308
|
+
};
|
|
1309
|
+
res.setHeader("content-type", "application/json");
|
|
1310
|
+
res.setHeader("cache-control", "no-store");
|
|
1311
|
+
res.end(JSON.stringify(payload));
|
|
1312
|
+
}
|
|
1313
|
+
catch (error) {
|
|
1314
|
+
res.statusCode = 400;
|
|
1315
|
+
const message = error instanceof ExpandZipError ? error.message : "解压失败";
|
|
1316
|
+
res.end(JSON.stringify({ message }));
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
869
1319
|
const DEV_FILE_MIME = {
|
|
870
1320
|
".mp4": "video/mp4",
|
|
871
1321
|
".webm": "video/webm",
|