fix: 打通小程序代码上传链路
小程序上传此前连续失败,根因分散在状态机、产物路径、模块导入、私钥解析和进程隔离上,逐项修复: - 构建进度改用 updateBuild 只写 buildStatus/buildLogs,不再用 updateStatus 记录进度,避免状态机拒绝自我迁移而返回 409 - 产物目录锚定仓库根目录,不再随 API 进程工作目录变成 apps/api/apps/taro/dist - miniprogram-ci 为 CommonJS 包,动态导入后从 default 归一化取 Project/upload,替换原有的类型断言 - 上传私钥统一还原成合法 PEM;输入框曾把换行吞成单行,导致 OpenSSL 解码失败,现保存时校验、上传时自动修复存量数据 - 上传改在独立子进程执行,miniprogram-ci 崩溃或超时不再终止 API 进程,失败返回 502 并保留微信原始错误 - 构建超过 MINIAPP_BUILD_STALE_MS 无进度即视为中断,允许重新上传,避免任务永久卡在 building - 构建前校验请求地址与 AppID,请求地址支持在品牌配置中维护,环境变量仅作回落,避免再打出指向 localhost 的包 - 小程序 /media/ 按请求地址的 origin 解析,不再依赖网关剥离 /api 前缀 新增对应单测与集成断言,并更新部署说明。
This commit is contained in:
1 parent
f52577ff9e
commit
44fe7a0142
27 files changed
+986
-62
No files matched your search
@@ -0,0 +1,58 @@
|
||||
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "::1"]);
|
||||
|
||||
function normalizeBaseUrl(value: string): string {
|
||||
return value.trim().replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求地址优先取品牌在后台维护的值,其次才是环境变量。
|
||||
* 小程序包内的 `API_BASE_URL` 是编译期常量,改地址需要重新构建上传,
|
||||
* 但地址本身属于可在后台修改的配置,不再写死在服务端环境变量里。
|
||||
*/
|
||||
export function miniappApiBaseUrl(
|
||||
storedApiBaseUrl?: unknown,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): string {
|
||||
const stored =
|
||||
typeof storedApiBaseUrl === "string" && storedApiBaseUrl.trim()
|
||||
? storedApiBaseUrl.trim()
|
||||
: undefined;
|
||||
if (stored) return validate(normalizeBaseUrl(stored));
|
||||
const explicit = env.MINIAPP_API_BASE_URL?.trim();
|
||||
if (explicit) return validate(normalizeBaseUrl(explicit));
|
||||
const publicBase = env.PUBLIC_BASE_URL?.trim();
|
||||
if (publicBase)
|
||||
return validate(
|
||||
normalizeBaseUrl(`${normalizeBaseUrl(publicBase)}${env.MINIAPP_API_PATH ?? "/api"}`),
|
||||
);
|
||||
throw new Error(
|
||||
"未配置小程序请求地址:请在“配置与发布 → 小程序上传配置”里填写“小程序请求地址”,例如 https://wechat.app.que01.top/api",
|
||||
);
|
||||
}
|
||||
|
||||
/** 保存前校验后台填写的请求地址,错误信息直接给到管理员。 */
|
||||
export function validateApiBaseUrl(value: string): string {
|
||||
return validate(normalizeBaseUrl(value));
|
||||
}
|
||||
|
||||
function validate(value: string): string {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch {
|
||||
throw new Error(`小程序请求地址不是合法 URL:${value}`);
|
||||
}
|
||||
if (url.protocol !== "https:" && !LOCAL_HOSTS.has(url.hostname))
|
||||
throw new Error(`小程序请求地址必须使用 https(当前 ${value}),否则真机无法访问`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function weappAppId(storedAppId: unknown, env: NodeJS.ProcessEnv = process.env): string {
|
||||
const candidate =
|
||||
typeof storedAppId === "string" && storedAppId.trim()
|
||||
? storedAppId.trim()
|
||||
: (env.WEAPP_APP_ID?.trim() ?? "");
|
||||
if (!candidate || candidate === "undefined" || candidate === "touristappid")
|
||||
throw new Error("未配置微信 AppID:请在“小程序上传配置”里填写或设置 WEAPP_APP_ID");
|
||||
return candidate;
|
||||
}
|
||||
Reference in new issue
Block a user