Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

3.6 KB
Raw Blame
import { createHash } from "node:crypto";
import { readFileSync, readdirSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import { APP_PACKAGE_TARGETS, ANDROID_APP_PACKAGE_TARGET } from "./app-package-targets.mjs";
import { SERVER_PACKAGE_TARGETS } from "./server-package-targets.mjs";

export function readReleaseArtifacts(directory, version, revision) {
  if (!directory || !/^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(version ?? "") ||
      !/^[0-9a-f]{40}$/.test(revision ?? "")) {
    throw new Error("An artifact directory, stable version and full source SHA are required");
  }
  const root = resolve(directory);
  const files = new Map();
  const packages = [];
  const mirrorFiles = [];
  const servers = [];
  const targets = new Set();
  function asset(name) {
    if (typeof name !== "string" || basename(name) !== name || !/^[a-zA-Z0-9._-]+$/.test(name)) {
      throw new Error("Release asset name is invalid");
    }
    const path = join(root, name);
    if (!files.has(name)) {
      const bytes = readFileSync(path);
      files.set(name, { name, path, size: bytes.length, sha256: createHash("sha256").update(bytes).digest("hex") });
    }
    return files.get(name);
  }
  for (const name of readdirSync(root).filter((name) => name.endsWith(".release.json"))) {
    const descriptor = JSON.parse(readFileSync(asset(name).path, "utf8"));
    if (descriptor.schema !== 2 || descriptor.version !== version || descriptor.revision !== revision) {
      throw new Error(`Release identity mismatch: ${name}`);
    }
    const target = descriptor.target ?? "server";
    if (targets.has(target)) throw new Error(`Duplicate release target: ${target}`);
    targets.add(target);
    const archive = asset(descriptor.artifact);
    if (archive.sha256 !== descriptor.artifactSha256) {
      throw new Error(`Release artifact checksum mismatch: ${name}`);
    }
    packages.push(archive);
    let mirror = archive;
    if (descriptor.mirrorArtifact !== undefined || descriptor.mirrorArtifactSha256 !== undefined) {
      if (target !== ANDROID_APP_PACKAGE_TARGET.id || descriptor.mirrorArtifact !== ANDROID_APP_PACKAGE_TARGET.mirrorArtifact) {
        throw new Error(`Release mirror artifact is invalid: ${target}`);
      }
      mirror = asset(descriptor.mirrorArtifact);
      if (mirror.sha256 !== descriptor.mirrorArtifactSha256 ||
          readFileSync(asset(`${mirror.name}.sha256`).path, "utf8").trim() !== `${mirror.sha256}  ${mirror.name}`) {
        throw new Error(`Release mirror artifact checksum mismatch: ${target}`);
      }
      packages.push(mirror);
    }
    mirrorFiles.push(mirror);
    const serverTarget = SERVER_PACKAGE_TARGETS.find(candidate => candidate.id === target);
    if (serverTarget) {
      if (asset(descriptor.manifest).sha256 !== descriptor.manifestSha256) {
        throw new Error("Server manifest checksum mismatch");
      }
      servers.push({ ...archive, arch: serverTarget.goarch, goos: serverTarget.goos });
    } else if (readFileSync(asset(`${descriptor.artifact}.sha256`).path, "utf8").trim() !==
        `${descriptor.artifactSha256}  ${descriptor.artifact}`) {
      throw new Error(`App checksum file mismatch: ${target}`);
    }
  }
  const expected = [...SERVER_PACKAGE_TARGETS, ...APP_PACKAGE_TARGETS, ANDROID_APP_PACKAGE_TARGET].map(target => target.id);
  if (targets.size !== expected.length || expected.some((target) => !targets.has(target))) {
    throw new Error("Publication requires every registered Server and App target");
  }
  // Sidecars prove the local build; only distribution packages are public assets.
  return { files: packages, mirrorFiles, servers, targets: [...targets] };
}