Skip to content
File

Blob: src/worker/do/repo/catalog/shared.ts

typescript54 lines
1import type { Head, RepoStateSchema, RepoLease, TypedStorage } from "../repoState";
2import type { PackCatalogRow } from "../db/schema";
3import type { Ref } from "../repoState";
4 
5export const RECEIVE_LEASE_TTL_MS = 30 * 60_000;
6export const COMPACT_LEASE_TTL_MS = 20 * 60_000;
7export const LEASE_RETRY_AFTER_SECONDS = 10;
8export const COMPACTION_REARM_DELAY_MS = 60_000;
9export const COMPACTION_WAKE_DELAY_MS = 5_000;
10export const DEFAULT_HEAD: Head = { target: "refs/heads/main", unborn: true };
11export const IDX_HEADER_LEN = 8 + 256 * 4;
12 
13export type BeginReceiveResult =
14 | { ok: false; retryAfter: number }
15 | {
16 ok: true;
17 lease: RepoLease;
18 refs: Ref[];
19 head: Head;
20 refsVersion: number;
21 packsetVersion: number;
22 nextPackSeq: number;
23 activeCatalog: PackCatalogRow[];
24 };
25 
26export function uniq(items: Array<string | undefined>): string[] {
27 const seen = new Set<string>();
28 const out: string[] = [];
29 for (const item of items) {
30 if (!item || seen.has(item)) continue;
31 seen.add(item);
32 out.push(item);
33 }
34 return out;
35}
36 
37/**
38 * Ensures all required repo metadata keys exist with sensible defaults.
39 * Initializes refsVersion, packsetVersion, and nextPackSeq if not already set.
40 */
41export async function ensureRepoMetadataDefaults(
42 store: TypedStorage<RepoStateSchema>
43): Promise<void> {
44 if ((await store.get("refsVersion")) === undefined) await store.put("refsVersion", 0);
45 if ((await store.get("packsetVersion")) === undefined) await store.put("packsetVersion", 0);
46 if ((await store.get("nextPackSeq")) === undefined) await store.put("nextPackSeq", 1);
47}
48 
49export async function bumpPacksetVersion(store: TypedStorage<RepoStateSchema>): Promise<number> {
50 const next = ((await store.get("packsetVersion")) || 0) + 1;
51 await store.put("packsetVersion", next);
52 return next;
53}