Skip to content
File

Blob: src/worker/keys.ts

typescript56 lines
1// Centralized helpers for building repo IDs and R2/DO key paths
2// Keep all key/prefix formats here to avoid divergence between tests and runtime.
3 
4// Compute the Durable Object R2 prefix for a given DO instance ID.
5// DO-backed data in R2 is stored under: "do/<durable-object-id>/..."
6export function doPrefix(doId: string): string {
7 return `do/${doId}`;
8}
9 
10// R2 key for a loose object under a given DO prefix
11export function r2LooseKey(prefix: string, oid: string): string {
12 return `${prefix}/objects/loose/${oid}`;
13}
14 
15// R2 key for a pack file under a given DO prefix
16export function r2PackKey(prefix: string, name: string): string {
17 return `${prefix}/objects/pack/${name}`;
18}
19 
20// Given a .pack key, return its matching .idx key
21export function packIndexKey(packKey: string): string {
22 return packKey.replace(/\.pack$/, ".idx");
23}
24 
25// Given a .pack key, return its matching logical-reference sidecar key.
26export function packRefsKey(packKey: string): string {
27 return packKey.replace(/\.pack$/, ".refs");
28}
29 
30// Directory prefix for pack objects under a given DO prefix
31export function r2PackDirPrefix(prefix: string): string {
32 return `${prefix}/objects/pack/`;
33}
34 
35// Return true if the key ends with .pack
36export function isPackKey(key: string): boolean {
37 return key.endsWith(".pack");
38}
39 
40// Return true if the key ends with .idx
41export function isIdxKey(key: string): boolean {
42 return key.endsWith(".idx");
43}
44 
45// Given an .idx key, return the corresponding .pack key
46export function packKeyFromIndexKey(idxKey: string): string {
47 return idxKey.replace(/\.idx$/, ".pack");
48}
49 
50// Extract the Durable Object ID from a path starting with "do/<id>/..."
51// Returns null when the path does not include the expected prefix.
52export function getDoIdFromPath(path: string): string | null {
53 const m = /^do\/([^/]+)/.exec(path);
54 return m ? m[1] : null;
55}