Files
image-workbench-desktop/scripts/plugins-publish.sh
T

148 lines
8.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# A2 插件发布:把插件上传到 KV(同源 /plugins/* 由 Functions 提供)并更新清单。
# 上传后**刷新浏览器即生效** —— 不需要重编译、不需要 deploy 主仓。
# 会为插件算 SHA-256(SRI integrity)写入清单 —— 运行时 fail-closed 校验所必需(缺失即拒载)。
#
# 用法:
# scripts/plugins-publish.sh <插件 id> <插件文件路径> [版本号]
# scripts/plugins-publish.sh <插件 id> --remove # 从清单移除(KV 里的文件保留)
#
# 例:
# scripts/plugins-publish.sh whitebg /path/to/iwb-plugins/whitebg/src/index.js 1.2.0
set -euo pipefail
usage() { sed -n '2,10p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; }
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ID="${1:-}"
ARG="${2:-}"
VERSION="${3:-}"
if [ -z "$ID" ] || [ -z "$ARG" ]; then usage; exit 2; fi
if ! [[ "$ID" =~ ^[a-z][a-z0-9-]{2,31}$ ]]; then echo "插件 id 不合法:须匹配 ^[a-z][a-z0-9-]{2,31}$" >&2; exit 2; fi
NS_ID="$(awk '/^\[\[kv_namespaces\]\]/{f=1} f&&/^id *=/{gsub(/[" ]/,"",$3); print $3; exit}' "$ROOT/wrangler.toml")"
if [ -z "$NS_ID" ]; then echo "未能在 wrangler.toml 找到 KV namespace id" >&2; exit 1; fi
# ── 读取线上清单(fail-closed)────────────────────────────────────────────
# 旧写法 `... 2>/dev/null || true` 把网络/授权/代理失败也吞成空串,随后被当成 '[]',
# 于是写回时**只剩本次发布的插件**,其余全部从清单消失。
# 2026-11-08 真实发生:image-annotate 1.8.2 发布后清单从 7 条塌成 1 条。
# 现在只有「键确实不存在」才允许从空清单开始,其余情况一律中止。
KV_ERR="$(mktemp)"
if CUR_RAW="$(npx --yes wrangler kv key get --namespace-id="$NS_ID" --remote "plugins/index.json" 2>"$KV_ERR")"; then
CUR="$CUR_RAW"
case "$CUR" in
\[*|{*) ;;
*)
echo "✗ 线上清单读取成功但不是合法 JSON,拒绝继续(避免把它覆盖成单条清单)" >&2
printf '%s\n' "$CUR" | head -c 200 >&2; echo >&2
rm -f "$KV_ERR"; exit 1 ;;
esac
else
if grep -qiE "not found|does not exist|no such key|404" "$KV_ERR" 2>/dev/null; then
CUR='[]'
echo "[plugins-publish] 线上清单尚不存在,将新建"
else
echo "✗ 读取线上清单失败 —— 拒绝继续。" >&2
echo " 继续下去会用「只有本次插件」的清单覆盖线上,把其他插件全部下架。" >&2
sed -n '1,4p' "$KV_ERR" >&2
rm -f "$KV_ERR"; exit 1
fi
fi
rm -f "$KV_ERR"
if [ "$ARG" = "--remove" ]; then
NEXT="$(node -e 'const cur=JSON.parse(process.argv[1]||"[]");const id=process.argv[2];process.stdout.write(JSON.stringify(cur.filter(x=>x&&x.id!==id),null,2))' "$CUR" "$ID")"
TMP="$(mktemp)"; printf '%s\n' "$NEXT" > "$TMP"
npx --yes wrangler kv key put --namespace-id="$NS_ID" --remote "plugins/index.json" --path="$TMP"
echo "[plugins-publish] 已从清单移除 $ID"
exit 0
fi
FILE="$ARG"
if [ ! -f "$FILE" ]; then echo "插件文件不存在:$FILE" >&2; exit 1; fi
if ! head -c 4096 "$FILE" | grep -q "IWBPluginHost"; then
echo "警告:$FILE 里没看到 IWBPluginHost.register —— 请确认这确实是插件产物。" >&2
fi
# ── 主干卫生前置校验(2026-11-05 立规,详见 HANDBOOK 第八节)─────────────
# 背景:本脚本直写 CF KV、绕开 git,曾导致 iwb-plugins 的 main 落后 40 个提交、
# 线上版本与 commit 断链。发布前强制:插件所在仓库工作区干净 + HEAD 已在 origin/main。
# 紧急热修可 IWB_PUBLISH_SKIP_CHECKS=1 跳过(事后必须补 tag 与合并)。
PLUGIN_REPO="$(git -C "$(dirname "$FILE")" rev-parse --show-toplevel 2>/dev/null || true)"
if [ "${IWB_PUBLISH_SKIP_CHECKS:-0}" != "1" ]; then
if [ -z "$PLUGIN_REPO" ]; then
echo "⚠ 未识别插件所属 git 仓库(${FILE}),跳过主干校验" >&2
else
if [ -n "$(git -C "$PLUGIN_REPO" status --porcelain 2>/dev/null)" ]; then
echo "✗ 工作区不干净:$PLUGIN_REPO" >&2
git -C "$PLUGIN_REPO" status --porcelain | sed -n '1,5p' >&2
echo " 先提交并推送,再发布;紧急热修可设 IWB_PUBLISH_SKIP_CHECKS=1" >&2
exit 1
fi
if ! git -C "$PLUGIN_REPO" fetch -q origin main 2>/dev/null; then
echo "⚠ 无法 fetch origin/main(离线发布可设 IWB_PUBLISH_SKIP_CHECKS=1)" >&2
fi
if git -C "$PLUGIN_REPO" rev-parse --verify -q origin/main >/dev/null 2>&1 \
&& ! git -C "$PLUGIN_REPO" merge-base --is-ancestor HEAD origin/main; then
echo "✗ 当前 HEAD 不在 origin/main 上(分支:$(git -C "$PLUGIN_REPO" rev-parse --abbrev-ref HEAD))" >&2
echo " 先把分支合进 main(纯祖先用 --ff-only)再发布,否则线上版本会与主干断链。" >&2
exit 1
fi
fi
fi
# 存储名内容寻址:plugin-<id>.<sha256前12位hex>.js(与 plugins-publish.mjs 的 storedNameOf 一致)
#
# 曾用固定名 plugin-<id>.js,2026-11-08 因此出过一次线上事故:
# 客户端缓存里还是旧字节,清单却已是新 integrity ⇒ 浏览器 SRI 判定失败,
# 报「脚本完整性校验失败(integrity 不符或加载失败)」,插件直接不可用。
# 固定名的 no-cache + ETag 只能要求「回源校验」,管不住中间层与浏览器内存缓存的旧副本。
# 内容寻址后:内容变 ⇒ 文件名变 ⇒ 新版本必然是新 URL,SRI 必中;
# 同时命中 Functions 的 IMMUTABLE_JS_RE ⇒ public, max-age=31536000, immutable。
# 代价:KV 会累积不再被清单引用的旧哈希键(无害),需要时用 plugins-publish.mjs --gc 清理。
HASH12="$(node -e 'const c=require("crypto"),fs=require("fs");process.stdout.write(c.createHash("sha256").update(fs.readFileSync(process.argv[1])).digest("hex").slice(0,12))' "$FILE")"
INTEGRITY="$(node -e 'const c=require("crypto"),fs=require("fs");process.stdout.write("sha256-"+c.createHash("sha256").update(fs.readFileSync(process.argv[1])).digest("base64"))' "$FILE")"
BASENAME="plugin-$ID.$HASH12.js"
echo "[plugins-publish] 上传 $BASENAME 到 KV(integrity=${INTEGRITY})"
npx --yes wrangler kv key put --namespace-id="$NS_ID" --remote "plugins/$BASENAME" --path="$FILE"
NEXT="$(node -e 'const cur=JSON.parse(process.argv[1]||"[]");const [id,file,ver,integ]=process.argv.slice(2);const out=cur.filter(x=>x&&x.id!==id);out.push(Object.assign({id,file},ver?{version:ver}:{},integ?{integrity:integ}:{}));out.sort((a,b)=>String(a.id).localeCompare(String(b.id)));process.stdout.write(JSON.stringify(out,null,2))' "$CUR" "$ID" "$BASENAME" "$VERSION" "$INTEGRITY")"
# 第二道保险:清单条数只允许「不变」或「+1」(新增插件),减少即视为异常。
# 只替换本次插件那一条时,条数必须与旧清单相同。
COUNT_OF='const v=JSON.parse(process.argv[1]||"[]");process.stdout.write(String(Array.isArray(v)?v.length:0))'
OLD_N="$(node -e "$COUNT_OF" "$CUR")"
NEW_N="$(node -e "$COUNT_OF" "$NEXT")"
if [ "$OLD_N" -ge 2 ] && [ "$NEW_N" -lt "$OLD_N" ]; then
echo "✗ 清单条数将从 ${OLD_N} 降到 ${NEW_N} —— 拒绝写入。" >&2
echo " 正常发布只替换本插件那一条,条数不应减少。检查线上清单是否被并发改动。" >&2
exit 1
fi
echo "[plugins-publish] 清单:${OLD_N} 条 → ${NEW_N} 条"
TMP="$(mktemp)"; printf '%s\n' "$NEXT" > "$TMP"
npx --yes wrangler kv key put --namespace-id="$NS_ID" --remote "plugins/index.json" --path="$TMP"
# 本地快照同步:本脚本直写 KV、绕开 git,但仓库里那份 plugins/index.json 是「线上清单应有的样子」
# 的记录。不同步的后果不是「少个文件」,而是下次有人(或旧脚本)拿这份旧文件当基线发布时,
# 把线上清单**整体回退**到旧版本 —— 2026-11-03 就因此把 7 条塌成 1 条。
# 这里只写文件不提交,让 git status 把它亮出来。
printf '%s\n' "$NEXT" > "$ROOT/plugins/index.json"
echo "[plugins-publish] 本地 plugins/index.json 已同步(记得提交)"
echo "[plugins-publish] 已发布 ${ID} (${BASENAME}${VERSION:+ @$VERSION})"
# 发布即可反查:打 插件id@版本 tag(仅在 HEAD 已确认在 origin/main 上时)
if [ -n "$VERSION" ] && [ -n "${PLUGIN_REPO:-}" ] && [ "${IWB_PUBLISH_SKIP_CHECKS:-0}" != "1" ]; then
TAG="$ID@$VERSION"
if git -C "$PLUGIN_REPO" rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
echo "[plugins-publish] tag $TAG 已存在,跳过"
else
git -C "$PLUGIN_REPO" tag -a "$TAG" -m "插件发布:$ID ${VERSION}(KV 已上线)"
echo "[plugins-publish] 已打 tag $TAG → 记得:git -C \"$PLUGIN_REPO\" push origin \"$TAG\""
fi
fi
echo "→ 刷新浏览器即生效。管理面板「插件管理」→「远程加载(KV)」可看到状态。"