示例工程 56-auto-update

56-auto-update/app/shared/feed.ts

typescript · 4.5 KB

export type Platform = "macos" | "windows" | "linux";

export type Scheme = {
  id: "static" | "official" | "server" | "store";
  name: string;
  feed: string;
  updates: string;
  limits: string;
};

export type FeedKind = "mac" | "win" | "invalid";

export type Feed = {
  id: string;
  label: string;
  kind: FeedKind;
  raw: string;
};

export type Decision = {
  status: "update" | "none" | "error";
  current: string;
  latest: string;
  notes: string;
  reason: string;
};

export const SCHEMES: Scheme[] = [
  {
    id: "static",
    name: "自建静态存储",
    feed: "releases.json / RELEASES",
    updates: "自己",
    limits: "需要对象存储、版本签名与回滚",
  },
  {
    id: "official",
    name: "update.electronjs.org",
    feed: "官方免费服务(GitHub Releases)",
    updates: "官方服务",
    limits: "公开仓库;仅 macOS/Windows;macOS 需签名;不支持 Linux",
  },
  {
    id: "server",
    name: "自建更新服务器",
    feed: "Hazel / Nuts / electron-release-server / Nucleus",
    updates: "自己",
    limits: "运维成本高;要处理认证、限流与灰度",
  },
  {
    id: "store",
    name: "商店 / 包管理器",
    feed: "MAS / MS Store / Flathub / Snap / apt",
    updates: "平台或包管理器",
    limits: "关闭应用内自更新;审核与沙箱限制",
  },
];

export function compareVersions(a: string, b: string): number {
  const left = a.split(".").map((part) => Number(part) || 0);
  const right = b.split(".").map((part) => Number(part) || 0);
  for (let index = 0; index < Math.max(left.length, right.length); index += 1) {
    const l = left[index] ?? 0;
    const r = right[index] ?? 0;
    if (l !== r) return l - r;
  }
  return 0;
}

export function parseMacFeed(raw: string): {
  version: string;
  url: string;
  notes: string;
} {
  const parsed = JSON.parse(raw) as {
    releases?: Array<{
      version?: string;
      updateTo?: { url?: string; name?: string };
      releaseNotes?: string;
    }>;
  };
  if (!Array.isArray(parsed.releases) || parsed.releases.length === 0) {
    throw new Error("BAD_MAC_FEED");
  }
  let best: { version: string; url: string; notes: string } | null = null;
  for (const release of parsed.releases) {
    const version = release.version ?? release.updateTo?.name ?? "";
    if (!/^\d+\.\d+\.\d+$/.test(version)) continue;
    if (!best || compareVersions(version, best.version) > 0) {
      best = {
        version,
        url: release.updateTo?.url ?? "",
        notes: release.releaseNotes ?? "",
      };
    }
  }
  if (!best) throw new Error("BAD_MAC_FEED");
  return best;
}

export function parseWinFeed(raw: string): {
  version: string;
  url: string;
  notes: string;
} {
  const versions: string[] = [];
  for (const line of raw.split(/\r?\n/)) {
    const match = /App-(\d+\.\d+\.\d+)-(?:full|delta)\.nupkg/.exec(line);
    if (match?.[1]) versions.push(match[1]);
  }
  if (versions.length === 0) throw new Error("BAD_WIN_FEED");
  const version = versions.sort(compareVersions).at(-1) ?? "";
  return {
    version,
    url: `https://updates.example.com/win/App-${version}-full.nupkg`,
    notes: "Windows Squirrel feed",
  };
}

export function decideUpdate(current: string, feed: Feed): Decision {
  try {
    const latest =
      feed.kind === "mac"
        ? parseMacFeed(feed.raw)
        : feed.kind === "win"
          ? parseWinFeed(feed.raw)
          : (() => {
              throw new Error("BAD_FEED");
            })();
    if (compareVersions(latest.version, current) > 0) {
      return {
        status: "update",
        current,
        latest: latest.version,
        notes: latest.notes,
        reason: `${latest.version} > ${current}`,
      };
    }
    return {
      status: "none",
      current,
      latest: latest.version,
      notes: latest.notes,
      reason: "already latest",
    };
  } catch (error) {
    return {
      status: "error",
      current,
      latest: "",
      notes: "",
      reason: error instanceof Error ? error.message : String(error),
    };
  }
}

export function officialEligible(input: {
  platform: Platform;
  publicRepo: boolean;
  signed: boolean;
}): { ok: boolean; reason: string } {
  if (input.platform === "linux") {
    return { ok: false, reason: "官方服务不支持 Linux" };
  }
  if (!input.publicRepo) {
    return { ok: false, reason: "需要公开 GitHub 仓库" };
  }
  if (!input.signed) {
    return { ok: false, reason: "macOS 需要签名" };
  }
  return { ok: true, reason: "公开仓库 + macOS/Windows + 签名" };
}

export function storeInAppUpdate(): boolean {
  return false;
}