165 lines · 5.6 KB
import { env, waitUntil } from "cloudflare:workers";
import { openArtifactsRepo } from "./artifacts";
import type { CommitSummary, TreeEntry } from "~/lib/types";

const OBJECT_CACHE_TTL_SECONDS = 60 * 60 * 24 * 30;
const SHORT_SHA_LENGTH = 7;
const WALK_CONCURRENCY = 16;
const MAX_WALK_TREE_READS = 2000;
export const ZERO_SHA = "0000000000000000000000000000000000000000";
const SHA_PATTERN = /^[0-9a-f]{40}$/;

export function isSha(value: string): boolean {
  return SHA_PATTERN.test(value);
}

export function shortSha(sha: string): string {
  return sha.slice(0, SHORT_SHA_LENGTH);
}

export function toCommitSummary(meta: ArtifactsCommitMetadata): CommitSummary {
  return {
    sha: meta.hash,
    shortSha: shortSha(meta.hash),
    subject: meta.message.split("\n", 1)[0] ?? "",
    message: meta.message,
    authorName: meta.author.name,
    authorEmail: meta.author.email,
    committerName: meta.committer.name,
    authoredAt: meta.authoredAt * 1000,
    committedAt: meta.committedAt * 1000,
    parents: meta.parents,
    treeSha: meta.treeHash,
  };
}

function joinPath(parent: string, name: string): string {
  return parent ? `${parent}/${name}` : name;
}

/** Read-side access to a single Artifacts repo with request memoization and KV caching of immutable objects. */