@mbws/plugin-sdk 0.3.22 → 0.5.3
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 +231 -2
- package/dist/caps.d.ts +1 -1
- package/dist/caps.js +4 -0
- package/dist/client.d.ts +77 -0
- package/dist/client.js +117 -3
- package/dist/config.d.ts +62 -0
- package/dist/dev-mock.js +128 -1
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +6 -0
- package/dist/manifest.d.ts +38 -0
- package/dist/manifest.js +69 -0
- package/dist/protocol.d.ts +35 -1
- package/dist/protocol.js +67 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/vite.d.ts +9 -0
- package/dist/vite.js +103 -4
- package/package.json +1 -1
package/dist/vite.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Plugin } from "vite";
|
|
2
2
|
import { type MbwsUserConfig } from "./config.js";
|
|
3
|
+
import { type AttachmentDecl, type AttachmentRef } from "./manifest.js";
|
|
3
4
|
export { type ContractTypesInput, generateContractTypes } from "./schema-types.js";
|
|
4
5
|
export interface MbwsVitePluginOptions {
|
|
5
6
|
/** mbws.config.ts 的配置(defineConfig 回传值)——静态合同与动态开发配置同住 */
|
|
@@ -22,6 +23,14 @@ export declare function markerOf(installed: InstalledSdkState): string;
|
|
|
22
23
|
* - v1 裸版本标记(历史形态):仅版本不一致才清;版本一致 → 不清(调用方原地升级标记)
|
|
23
24
|
*/
|
|
24
25
|
export declare function shouldClearViteCache(installed: InstalledSdkState | null, marker: string | null): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* 构建期附件解析的逃生门(仅本地自检):MBWS_BUILD_ALLOW_UNRESOLVED_ATTACHMENTS=1
|
|
28
|
+
* 时解析失败降级为「空附件声明 + 醒目告警」而非炸构建——附件尚未发布到官方库时
|
|
29
|
+
* 本地 pnpm build 可走通(CI 与验收流程解串行)。产物 manifest 因此不含附件声明,
|
|
30
|
+
* **不可发布上架**(宿主按未声明附件拒绝 spawn,不会静默错误);服务端 ticket
|
|
31
|
+
* 构建不设此 env,附件未发布即炸构建的验收防线不动。
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveBuildAttachments(refs: AttachmentRef[], warn: (message: string) => void): Promise<AttachmentDecl[]>;
|
|
25
34
|
/**
|
|
26
35
|
* mbws vite 插件。dev 预览下契约拉取同源化:SDK fetchContract 读到注入的
|
|
27
36
|
* `__MBWS_DEV_CONTRACT_BASE__` 后改打 `/__mbws__/api/...`,由本插件的中间件
|
package/dist/vite.js
CHANGED
|
@@ -24,6 +24,7 @@ import zlib from "node:zlib";
|
|
|
24
24
|
import { attachmentsOssBase, buildManifest, resolveAttachmentRefs, } from "./config.js";
|
|
25
25
|
import { applyStorageQuota, isStorageKeyValid, StorageQuotaError } from "./dev-storage.js";
|
|
26
26
|
import { ExpandZipError, expandZipVideos } from "./expand-zip.js";
|
|
27
|
+
import { isCriticalEnvName, } from "./manifest.js";
|
|
27
28
|
import { generateContractTypes } from "./schema-types.js";
|
|
28
29
|
// 契约类型生成经本子路径暴露(Node 端工具链用;依赖 prettier,不进根入口)
|
|
29
30
|
export { generateContractTypes } from "./schema-types.js";
|
|
@@ -140,6 +141,27 @@ function readRequestBody(req) {
|
|
|
140
141
|
req.on("error", reject);
|
|
141
142
|
});
|
|
142
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* 构建期附件解析的逃生门(仅本地自检):MBWS_BUILD_ALLOW_UNRESOLVED_ATTACHMENTS=1
|
|
146
|
+
* 时解析失败降级为「空附件声明 + 醒目告警」而非炸构建——附件尚未发布到官方库时
|
|
147
|
+
* 本地 pnpm build 可走通(CI 与验收流程解串行)。产物 manifest 因此不含附件声明,
|
|
148
|
+
* **不可发布上架**(宿主按未声明附件拒绝 spawn,不会静默错误);服务端 ticket
|
|
149
|
+
* 构建不设此 env,附件未发布即炸构建的验收防线不动。
|
|
150
|
+
*/
|
|
151
|
+
export async function resolveBuildAttachments(refs, warn) {
|
|
152
|
+
if (refs.length === 0)
|
|
153
|
+
return [];
|
|
154
|
+
try {
|
|
155
|
+
return await resolveAttachmentRefs(refs);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (process.env.MBWS_BUILD_ALLOW_UNRESOLVED_ATTACHMENTS?.trim() !== "1")
|
|
159
|
+
throw error;
|
|
160
|
+
warn(`[mbws:build] 附件官方库解析失败,已按逃生门降级为空附件声明(仅限本地自检,产物不可发布):` +
|
|
161
|
+
`${error instanceof Error ? error.message : error}`);
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
143
165
|
/**
|
|
144
166
|
* mbws vite 插件。dev 预览下契约拉取同源化:SDK fetchContract 读到注入的
|
|
145
167
|
* `__MBWS_DEV_CONTRACT_BASE__` 后改打 `/__mbws__/api/...`,由本插件的中间件
|
|
@@ -368,6 +390,15 @@ export function mbwsVitePlugin(opts) {
|
|
|
368
390
|
// caps.exec.writeFile 的 dev 落点:面板产出的文本写进工程目录(spawn cwd = 工程根)
|
|
369
391
|
return handleDevWriteFile(projectRoot, req, res).catch(next);
|
|
370
392
|
}
|
|
393
|
+
if (pathname === "/__mbws__/dev/remove") {
|
|
394
|
+
if (req.method !== "POST") {
|
|
395
|
+
res.statusCode = 405;
|
|
396
|
+
res.end();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
// caps.exec.remove 的 dev 执行点:删工程目录内文件/目录(与宿主工作区删除同形)
|
|
400
|
+
return handleDevRemove(projectRoot, req, res).catch(next);
|
|
401
|
+
}
|
|
371
402
|
if (pathname === "/__mbws__/dev/storage") {
|
|
372
403
|
if (req.method === "GET") {
|
|
373
404
|
// dev-mock storage.get:读 .mbws/storage.json 里本插件的一个键
|
|
@@ -410,13 +441,16 @@ export function mbwsVitePlugin(opts) {
|
|
|
410
441
|
},
|
|
411
442
|
async closeBundle() {
|
|
412
443
|
// 产物直出解包形态目录 <id>@<version>/,桌面 MBWS_DEV_PLUGIN_DIR 指向 dist 即装载。
|
|
413
|
-
// 附件声明新鲜解析(不缓存):产物钉官方库当前 hash
|
|
444
|
+
// 附件声明新鲜解析(不缓存):产物钉官方库当前 hash,解析失败炸构建是正确行为——
|
|
445
|
+
// 唯一例外是本地自检逃生门(resolveBuildAttachments:env 显式开启时降级空附件声明)
|
|
414
446
|
const outDir = distDir();
|
|
415
447
|
// mkdir 容错:在线构建场景 outDir 被平台覆盖,本目录可能不存在(写失败会炸构建)
|
|
416
448
|
fs.mkdirSync(outDir, { recursive: true });
|
|
417
449
|
const refs = config.attachments ?? [];
|
|
418
|
-
const decls =
|
|
419
|
-
fs.writeFileSync(path.join(outDir, "manifest.json"), `${JSON.stringify(
|
|
450
|
+
const decls = await resolveBuildAttachments(refs, (message) => this.warn(message));
|
|
451
|
+
fs.writeFileSync(path.join(outDir, "manifest.json"), `${JSON.stringify(decls.length > 0
|
|
452
|
+
? buildManifest(config, { resolvedAttachments: decls })
|
|
453
|
+
: buildManifest(config, { allowEmptyAttachments: true }), null, 2)}\n`);
|
|
420
454
|
},
|
|
421
455
|
};
|
|
422
456
|
}
|
|
@@ -753,6 +787,29 @@ async function prepareDevAttachment(config, root, attachment, write, res) {
|
|
|
753
787
|
}
|
|
754
788
|
return binPath;
|
|
755
789
|
}
|
|
790
|
+
/**
|
|
791
|
+
* dev 凭据 env(无宿主联调用):按 config.credentials 声明取值,优先级
|
|
792
|
+
* `MBWS_DEV_CRED_<ENV>` 环境变量 > `config.plugin["credential.<ENV>"]`(仅字符串)。
|
|
793
|
+
* 与宿主同口径:只注入声明过的名字,宿主关键变量(PATH/HOME/NODE_* 等)跳过。
|
|
794
|
+
*/
|
|
795
|
+
function devCredentialEnv(config) {
|
|
796
|
+
const env = {};
|
|
797
|
+
for (const cred of (config.credentials ?? [])) {
|
|
798
|
+
if (!cred || typeof cred.env !== "string")
|
|
799
|
+
continue;
|
|
800
|
+
if (isCriticalEnvName(cred.env))
|
|
801
|
+
continue;
|
|
802
|
+
const fromEnv = process.env[`MBWS_DEV_CRED_${cred.env}`];
|
|
803
|
+
if (typeof fromEnv === "string" && fromEnv !== "") {
|
|
804
|
+
env[cred.env] = fromEnv;
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
const fromConfig = config.plugin?.[`credential.${cred.env}`];
|
|
808
|
+
if (typeof fromConfig === "string" && fromConfig !== "")
|
|
809
|
+
env[cred.env] = fromConfig;
|
|
810
|
+
}
|
|
811
|
+
return env;
|
|
812
|
+
}
|
|
756
813
|
/** POST /__mbws__/dev/exec:spawn 本机二进制(cwd = 工程根,插件 args 里 samples/xxx 相对可用) */
|
|
757
814
|
async function handleDevExec(config, root, req, res) {
|
|
758
815
|
let body;
|
|
@@ -783,7 +840,12 @@ async function handleDevExec(config, root, req, res) {
|
|
|
783
840
|
// 插件的 `const job = await spawn(...)` 被卡满整个作业时长——宿主的 spawn 是立即
|
|
784
841
|
// 回 jobId 的。放在 prepareDevAttachment 之后:501(附件不可用)分支还能改状态码
|
|
785
842
|
res.flushHeaders();
|
|
786
|
-
const proc = spawn(binPath, args, {
|
|
843
|
+
const proc = spawn(binPath, args, {
|
|
844
|
+
cwd: root,
|
|
845
|
+
...(Object.keys(devCredentialEnv(config)).length > 0
|
|
846
|
+
? { env: { ...process.env, ...devCredentialEnv(config) } }
|
|
847
|
+
: {}),
|
|
848
|
+
});
|
|
787
849
|
const stderrTailChunks = [];
|
|
788
850
|
let stderrTailBytes = 0;
|
|
789
851
|
const pendingLines = [];
|
|
@@ -1385,6 +1447,43 @@ async function handleDevWriteFile(root, req, res) {
|
|
|
1385
1447
|
res.setHeader("cache-control", "no-store");
|
|
1386
1448
|
res.end(JSON.stringify({ path: path.relative(path.resolve(root), target).split(path.sep).join("/") }));
|
|
1387
1449
|
}
|
|
1450
|
+
/**
|
|
1451
|
+
* POST /__mbws__/dev/remove:body { path }——删除工程目录内的文件/目录(递归、
|
|
1452
|
+
* 幂等:不存在即成功)。与宿主 caps.exec.remove 同规则同错误码:越界/绝对路径/
|
|
1453
|
+
* 等于根 → 400 E_INVALID_ARGS(resolveInsideRoot 一起拦)。上传引用联动
|
|
1454
|
+
* (E_PATH_IN_USE)仅桌面宿主有,dev 无队列。
|
|
1455
|
+
*/
|
|
1456
|
+
async function handleDevRemove(root, req, res) {
|
|
1457
|
+
const body = await readJsonBody(req);
|
|
1458
|
+
const relPath = body?.path;
|
|
1459
|
+
if (typeof relPath !== "string") {
|
|
1460
|
+
res.statusCode = 400;
|
|
1461
|
+
res.setHeader("content-type", "application/json");
|
|
1462
|
+
res.end(JSON.stringify({ code: "E_INVALID_ARGS", message: "caps.exec.remove 需要 path(string)" }));
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
const target = resolveInsideRoot(root, relPath);
|
|
1466
|
+
if (!target) {
|
|
1467
|
+
res.statusCode = 400;
|
|
1468
|
+
res.setHeader("content-type", "application/json");
|
|
1469
|
+
res.end(JSON.stringify({ code: "E_INVALID_ARGS", message: "path 必须是工程目录内的相对路径" }));
|
|
1470
|
+
return;
|
|
1471
|
+
}
|
|
1472
|
+
try {
|
|
1473
|
+
await fs.promises.rm(target, { recursive: true, force: true });
|
|
1474
|
+
}
|
|
1475
|
+
catch (error) {
|
|
1476
|
+
res.statusCode = 500;
|
|
1477
|
+
res.setHeader("content-type", "application/json");
|
|
1478
|
+
res.end(JSON.stringify({
|
|
1479
|
+
message: `删除失败:${error instanceof Error ? error.message : String(error)}`,
|
|
1480
|
+
}));
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
res.setHeader("content-type", "application/json");
|
|
1484
|
+
res.setHeader("cache-control", "no-store");
|
|
1485
|
+
res.end(JSON.stringify({ ok: true }));
|
|
1486
|
+
}
|
|
1388
1487
|
/** dev storage 落盘文件(.mbws/ 已在脚手架 .gitignore 里——纯本机状态,不进 git 不进产物) */
|
|
1389
1488
|
const DEV_STORAGE_FILE = path.join(".mbws", "storage.json");
|
|
1390
1489
|
/** 读某插件的键值表(文件不存在/损坏/插件未写过 → 空表) */
|