pr_01m47d24b0e6n91zwymwxg0vpx/services/repos/src/store.rs

284 lines9,633 bytesCodeBlame
1//! The storage that actually holds git repositories.
2//!
3//! The service depends on the [`GitStore`] and [`GitRepo`] ports;
4//! [`ArtifactsStore`] is the adapter for Cloudflare Artifacts.
5
6use g1t_contracts::repos::{Branch, Commit, EntryKind, GitAccess, Signature, TreeEntry};
7use g1t_contracts::time::rfc3339;
8use g1t_kit::js;
9use serde::Deserialize;
10use worker::js_sys::{Reflect, Uint8Array};
11use worker::wasm_bindgen::{JsCast, JsValue};
12use worker::{Env, Result};
13
14/// How long a credential handed to git stays valid.
15const TOKEN_TTL_SECONDS: u32 = 300;
16
17#[derive(Clone, Copy)]
18pub enum Scope {
19 Read,
20 Write,
21}
22
23/// A place repositories live. `key` is the store's own name for a repo.
24#[allow(async_fn_in_trait)]
25pub trait GitStore {
26 type Repo: GitRepo;
27
28 /// Creates an empty repository. Succeeds if it already exists.
29 async fn create(
30 &self,
31 key: &str,
32 description: Option<&str>,
33 default_branch: &str,
34 ) -> Result<()>;
35 async fn open(&self, key: &str) -> Result<Self::Repo>;
36}
37
38/// One open repository.
39#[allow(async_fn_in_trait)]
40pub trait GitRepo {
41 /// A remote URL and short-lived credential for git itself.
42 async fn access(&self, scope: Scope) -> Result<GitAccess>;
43 /// Every branch and the commit it points to.
44 async fn branches(&self) -> Result<Vec<Branch>>;
45 /// Newest first along the first-parent chain; empty for an unknown ref.
46 async fn log(&self, git_ref: &str, limit: u32) -> Result<Vec<Commit>>;
47 /// The parents of a commit, or `None` if the commit does not exist.
48 async fn parents(&self, commit_hash: &str) -> Result<Option<Vec<String>>>;
49 async fn read_tree(&self, tree_hash: &str) -> Result<Option<Vec<TreeEntry>>>;
50 async fn read_blob(&self, blob_hash: &str) -> Result<Option<Vec<u8>>>;
51 /// `None` when the ref or path does not resolve to a file.
52 async fn read_file(&self, git_ref: &str, path: &str) -> Result<Option<Vec<u8>>>;
53 /// Makes a copy-on-write copy of this repository under `target_key`.
54 async fn fork(&self, target_key: &str) -> Result<()>;
55}
56
57pub struct ArtifactsStore {
58 binding: JsValue,
59}
60
61impl ArtifactsStore {
62 pub fn new(env: &Env) -> Result<Self> {
63 Ok(Self {
64 binding: js::binding(env, "ARTIFACTS")?,
65 })
66 }
67}
68
69impl GitStore for ArtifactsStore {
70 type Repo = ArtifactsRepo;
71
72 async fn create(
73 &self,
74 key: &str,
75 description: Option<&str>,
76 default_branch: &str,
77 ) -> Result<()> {
78 let options = js::to_js(&serde_json::json!({
79 "description": description,
80 "setDefaultBranch": default_branch,
81 }))?;
82 match js::call(&self.binding, "create", &[key.into(), options]).await {
83 // Left behind by an earlier failed attempt; adopt it.
84 Err(thrown) if !thrown.is("ALREADY_EXISTS") => Err(thrown.into()),
85 _ => Ok(()),
86 }
87 }
88
89 async fn open(&self, key: &str) -> Result<ArtifactsRepo> {
90 Ok(ArtifactsRepo {
91 handle: js::call(&self.binding, "get", &[key.into()]).await?,
92 key: key.to_owned(),
93 })
94 }
95}
96
97/// A handle to one Artifacts repository. It is an RPC stub, so it is
98/// released when dropped.
99pub struct ArtifactsRepo {
100 handle: JsValue,
101 /// The repository's store key, which scopes its cached objects.
102 key: String,
103}
104
105/// Where cached git objects live. Trees and blobs are named by their
106/// content, so a cached one is never stale; each is kept under its own
107/// repository's key, so a repository only ever finds its own objects.
108const OBJECT_CACHE: &str = "https://objects.g1t.internal/";
109/// Blobs larger than this are not cached.
110const MAX_CACHED_BLOB: usize = 1024 * 1024;
111const OBJECT_MAX_AGE: &str = "public, max-age=31536000, immutable";
112
113impl ArtifactsRepo {
114 fn cache_url(&self, kind: &str, hash: &str) -> String {
115 format!("{OBJECT_CACHE}{}/{kind}/{hash}", self.key)
116 }
117
118 async fn cached(&self, kind: &str, hash: &str) -> Option<Vec<u8>> {
119 let mut response = worker::Cache::default()
120 .get(self.cache_url(kind, hash), false)
121 .await
122 .ok()??;
123 response.bytes().await.ok()
124 }
125
126 /// Keeps an object for next time. A failure only costs a later read.
127 async fn keep(&self, kind: &str, hash: &str, bytes: Vec<u8>) {
128 let Ok(mut response) = worker::Response::from_bytes(bytes) else {
129 return;
130 };
131 let _ = response.headers_mut().set("cache-control", OBJECT_MAX_AGE);
132 let _ = worker::Cache::default()
133 .put(self.cache_url(kind, hash), response)
134 .await;
135 }
136}
137
138impl Drop for ArtifactsRepo {
139 fn drop(&mut self) {
140 let symbol = js::get(&worker::js_sys::global(), "Symbol");
141 let dispose = js::get(&symbol, "dispose");
142 if let Ok(function) = Reflect::get(&self.handle, &dispose)
143 .and_then(|value| value.dyn_into::<worker::js_sys::Function>())
144 {
145 let _ = function.call0(&self.handle);
146 }
147 }
148}
149
150#[derive(Deserialize)]
151#[serde(rename_all = "camelCase")]
152struct RawCommit {
153 hash: String,
154 tree_hash: String,
155 message: String,
156 author: Signature,
157 parents: Vec<String>,
158 /// Seconds since the epoch.
159 authored_at: u64,
160}
161
162#[derive(Deserialize)]
163struct RawEntry {
164 name: String,
165 hash: String,
166 #[serde(rename = "type")]
167 kind: EntryKind,
168}
169
170#[derive(Deserialize)]
171struct RawInfo {
172 remote: String,
173}
174
175#[derive(Deserialize)]
176struct RawToken {
177 plaintext: String,
178}
179
180/// The bytes of a `Blob`, or `None` for null.
181async fn blob_bytes(blob: JsValue) -> Result<Option<Vec<u8>>> {
182 if blob.is_null() || blob.is_undefined() {
183 return Ok(None);
184 }
185 let buffer = js::call(&blob, "arrayBuffer", &[]).await?;
186 Ok(Some(Uint8Array::new(&buffer).to_vec()))
187}
188
189impl GitRepo for ArtifactsRepo {
190 async fn access(&self, scope: Scope) -> Result<GitAccess> {
191 let scope = match scope {
192 Scope::Read => "read",
193 Scope::Write => "write",
194 };
195 let info: RawInfo = js::from_js(&js::call(&self.handle, "info", &[]).await?)?;
196 let token: RawToken = js::from_js(
197 &js::call(
198 &self.handle,
199 "createToken",
200 &[scope.into(), TOKEN_TTL_SECONDS.into()],
201 )
202 .await?,
203 )?;
204 Ok(GitAccess {
205 remote: info.remote,
206 token: token.plaintext,
207 })
208 }
209
210 async fn branches(&self) -> Result<Vec<Branch>> {
211 crate::refs::branches(&self.access(Scope::Read).await?).await
212 }
213
214 async fn log(&self, git_ref: &str, limit: u32) -> Result<Vec<Commit>> {
215 let options = js::to_js(&serde_json::json!({ "ref": git_ref, "limit": limit }))?;
216 let commits: Vec<RawCommit> =
217 js::from_js(&js::call(&self.handle, "log", &[options]).await?)?;
218 Ok(commits
219 .into_iter()
220 .map(|commit| Commit {
221 hash: commit.hash,
222 tree_hash: commit.tree_hash,
223 message: commit.message,
224 author: commit.author,
225 parents: commit.parents,
226 authored_at: rfc3339(commit.authored_at * 1000),
227 })
228 .collect())
229 }
230
231 async fn parents(&self, commit_hash: &str) -> Result<Option<Vec<String>>> {
232 let commit: Option<RawCommit> =
233 js::from_js(&js::call(&self.handle, "readCommit", &[commit_hash.into()]).await?)?;
234 Ok(commit.map(|commit| commit.parents))
235 }
236
237 async fn read_tree(&self, tree_hash: &str) -> Result<Option<Vec<TreeEntry>>> {
238 if let Some(bytes) = self.cached("tree", tree_hash).await
239 && let Ok(entries) = serde_json::from_slice::<Vec<TreeEntry>>(&bytes) {
240 return Ok(Some(entries));
241 }
242 let entries: Option<Vec<RawEntry>> =
243 js::from_js(&js::call(&self.handle, "readTree", &[tree_hash.into()]).await?)?;
244 let entries: Option<Vec<TreeEntry>> = entries.map(|entries| {
245 entries
246 .into_iter()
247 .map(|entry| TreeEntry {
248 name: entry.name,
249 hash: entry.hash,
250 kind: entry.kind,
251 })
252 .collect()
253 });
254 if let Some(entries) = &entries
255 && let Ok(bytes) = serde_json::to_vec(entries) {
256 self.keep("tree", tree_hash, bytes).await;
257 }
258 Ok(entries)
259 }
260
261 async fn read_blob(&self, blob_hash: &str) -> Result<Option<Vec<u8>>> {
262 if let Some(bytes) = self.cached("blob", blob_hash).await {
263 return Ok(Some(bytes));
264 }
265 let bytes = blob_bytes(js::call(&self.handle, "readBlob", &[blob_hash.into()]).await?).await?;
266 if let Some(bytes) = bytes.as_ref().filter(|bytes| bytes.len() <= MAX_CACHED_BLOB) {
267 self.keep("blob", blob_hash, bytes.clone()).await;
268 }
269 Ok(bytes)
270 }
271
272 async fn read_file(&self, git_ref: &str, path: &str) -> Result<Option<Vec<u8>>> {
273 let args = js::to_js(&serde_json::json!({ "ref": git_ref, "path": path }))?;
274 blob_bytes(js::call(&self.handle, "readFile", &[args]).await?).await
275 }
276
277 async fn fork(&self, target_key: &str) -> Result<()> {
278 let options = js::to_js(&serde_json::json!({ "defaultBranchOnly": true }))?;
279 match js::call(&self.handle, "fork", &[target_key.into(), options]).await {
280 Err(thrown) if !thrown.is("ALREADY_EXISTS") => Err(thrown.into()),
281 _ => Ok(()),
282 }
283 }
284}