Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| API and MCP server, Rust identity service, registration, site redesign | 1 | //! Plumbing shared by g1t services that run on Workers. |
| 2 | //! | |
| 3 | //! Services talk to each other over service bindings with a small JSON | |
| 4 | //! protocol: `POST /rpc/<method>` with the method's arguments as the body, | |
| 5 | //! answered with the method's return value. | |
| 6 | ||
| 7 | use serde::Serialize; | |
| 8 | use serde::de::DeserializeOwned; | |
| 9 | use worker::{Date, Fetcher, Headers, Method, Request, RequestInit, Response, Result}; | |
| 10 | ||
| 11 | /// The current time in milliseconds since the epoch. | |
| 12 | pub fn now_ms() -> u64 { | |
| 13 | Date::now().as_millis() | |
| 14 | } | |
| 15 | ||
| 16 | /// The method name of an RPC request, or `None` if it is not one. | |
| 17 | pub fn rpc_method(request: &Request) -> Option<String> { | |
| 18 | if request.method() != Method::Post { | |
| 19 | return None; | |
| 20 | } | |
| 21 | request | |
| 22 | .path() | |
| 23 | .strip_prefix("/rpc/") | |
| 24 | .map(|method| method.to_owned()) | |
| 25 | } | |
| 26 | ||
| 27 | /// Deserializes a method's arguments. | |
| 28 | pub fn args<A: DeserializeOwned>(body: serde_json::Value) -> Result<A> { | |
| 29 | serde_json::from_value(body) | |
| 30 | .map_err(|error| worker::Error::RustError(format!("bad arguments: {error}"))) | |
| 31 | } | |
| 32 | ||
| 33 | /// Serializes a method's return value as the response body. | |
| 34 | pub fn reply<R: Serialize>(value: &R) -> Result<Response> { | |
| 35 | Response::from_json(value) | |
| 36 | } | |
| 37 | ||
| 38 | /// Calls `method` on another service through its binding. | |
| 39 | pub async fn call<A: Serialize, R: DeserializeOwned>( | |
| 40 | service: &Fetcher, | |
| 41 | method: &str, | |
| 42 | arguments: &A, | |
| 43 | ) -> Result<R> { | |
| 44 | let headers = Headers::new(); | |
| 45 | headers.set("content-type", "application/json")?; | |
| 46 | let mut init = RequestInit::new(); | |
| 47 | init.with_method(Method::Post) | |
| 48 | .with_headers(headers) | |
| 49 | .with_body(Some(serde_json::to_string(arguments)?.into())); | |
| 50 | // The hostname is ignored; a service binding always reaches its service. | |
| 51 | let request = Request::new_with_init(&format!("https://service/rpc/{method}"), &init)?; | |
| 52 | let mut response = service.fetch_request(request).await?; | |
| 53 | if response.status_code() != 200 { | |
| 54 | return Err(worker::Error::RustError(format!( | |
| 55 | "{method} failed with status {}: {}", | |
| 56 | response.status_code(), | |
| 57 | response.text().await.unwrap_or_default() | |
| 58 | ))); | |
| 59 | } | |
| 60 | response.json().await | |
| 61 | } | |
| Email verification, password reset, and Git for AI scale positioning | 62 | |
| 63 | /// Helpers for bindings that workers-rs has no typed wrapper for, such as | |
| 64 | /// Artifacts and Email Sending. Values cross the boundary as JSON. | |
| 65 | pub mod js { | |
| Rust repos service with shipping; pull requests kept in the model | 66 | use std::fmt; |
| 67 | ||
| Email verification, password reset, and Git for AI scale positioning | 68 | use serde::Serialize; |
| 69 | use serde::de::DeserializeOwned; | |
| Rust repos service with shipping; pull requests kept in the model | 70 | use worker::js_sys::{Array, Function, JSON, Promise, Reflect}; |
| Email verification, password reset, and Git for AI scale positioning | 71 | use worker::wasm_bindgen::{JsCast, JsValue}; |
| 72 | use worker::wasm_bindgen_futures::JsFuture; | |
| 73 | use worker::{Env, Error, Result}; | |
| 74 | ||
| Rust repos service with shipping; pull requests kept in the model | 75 | /// An exception thrown by JavaScript, with its `code` if it had one. |
| 76 | #[derive(Debug)] | |
| 77 | pub struct Thrown { | |
| 78 | pub code: Option<String>, | |
| 79 | pub message: String, | |
| 80 | } | |
| 81 | ||
| 82 | impl Thrown { | |
| 83 | fn from_value(value: JsValue) -> Self { | |
| 84 | let property = |name: &str| { | |
| 85 | Reflect::get(&value, &name.into()) | |
| Email verification, password reset, and Git for AI scale positioning | 86 | .ok() |
| Rust repos service with shipping; pull requests kept in the model | 87 | .and_then(|property| property.as_string()) |
| 88 | }; | |
| 89 | Thrown { | |
| 90 | code: property("code"), | |
| 91 | message: property("message").unwrap_or_else(|| format!("{value:?}")), | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | pub fn is(&self, code: &str) -> bool { | |
| 96 | self.code.as_deref() == Some(code) | |
| 97 | } | |
| Email verification, password reset, and Git for AI scale positioning | 98 | } |
| 99 | ||
| Rust repos service with shipping; pull requests kept in the model | 100 | impl fmt::Display for Thrown { |
| 101 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 102 | match &self.code { | |
| 103 | Some(code) => write!(f, "{code}: {}", self.message), | |
| 104 | None => f.write_str(&self.message), | |
| 105 | } | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | impl From<Thrown> for Error { | |
| 110 | fn from(thrown: Thrown) -> Self { | |
| 111 | Error::RustError(thrown.to_string()) | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| Email verification, password reset, and Git for AI scale positioning | 115 | /// The binding called `name`, as a raw JavaScript value. |
| 116 | pub fn binding(env: &Env, name: &str) -> Result<JsValue> { | |
| Rust repos service with shipping; pull requests kept in the model | 117 | let value = Reflect::get(env.as_ref(), &name.into()).map_err(Thrown::from_value)?; |
| Email verification, password reset, and Git for AI scale positioning | 118 | if value.is_undefined() { |
| 119 | return Err(Error::RustError(format!( | |
| 120 | "binding {name} is not configured" | |
| 121 | ))); | |
| 122 | } | |
| 123 | Ok(value) | |
| 124 | } | |
| 125 | ||
| Rust repos service with shipping; pull requests kept in the model | 126 | /// Reads a property of a JavaScript object. |
| 127 | pub fn get(target: &JsValue, name: &str) -> JsValue { | |
| 128 | Reflect::get(target, &name.into()).unwrap_or(JsValue::UNDEFINED) | |
| 129 | } | |
| 130 | ||
| Events service in Rust, with RFC 3339 times and accurate push events | 131 | /// Sets a property on an object. |
| 132 | pub fn set(target: &JsValue, name: &str, value: &JsValue) { | |
| 133 | let _ = Reflect::set(target, &name.into(), value); | |
| 134 | } | |
| 135 | ||
| Email verification, password reset, and Git for AI scale positioning | 136 | pub fn to_js<T: Serialize>(value: &T) -> Result<JsValue> { |
| Rust repos service with shipping; pull requests kept in the model | 137 | Ok(JSON::parse(&serde_json::to_string(value)?).map_err(Thrown::from_value)?) |
| Email verification, password reset, and Git for AI scale positioning | 138 | } |
| 139 | ||
| 140 | pub fn from_js<T: DeserializeOwned>(value: &JsValue) -> Result<T> { | |
| Rust repos service with shipping; pull requests kept in the model | 141 | let text = if value.is_undefined() { |
| 142 | None | |
| 143 | } else { | |
| 144 | JSON::stringify(value) | |
| 145 | .map_err(Thrown::from_value)? | |
| 146 | .as_string() | |
| 147 | }; | |
| 148 | let text = text.as_deref().unwrap_or("null"); | |
| 149 | serde_json::from_str(text).map_err(|error| { | |
| 150 | // Say what arrived; a bare serde error is useless in a log. | |
| 151 | let seen: String = text.chars().take(300).collect(); | |
| 152 | Error::RustError(format!( | |
| 153 | "unexpected value from JavaScript ({error}): {seen}" | |
| 154 | )) | |
| 155 | }) | |
| Email verification, password reset, and Git for AI scale positioning | 156 | } |
| 157 | ||
| Rust repos service with shipping; pull requests kept in the model | 158 | /// Calls `target[method](...args)` and awaits the result if it is a |
| 159 | /// thenable. `method` may be a name or a symbol. | |
| 160 | pub async fn call_key( | |
| 161 | target: &JsValue, | |
| 162 | method: &JsValue, | |
| 163 | args: &[JsValue], | |
| 164 | ) -> std::result::Result<JsValue, Thrown> { | |
| 165 | let function: Function = Reflect::get(target, method) | |
| 166 | .map_err(Thrown::from_value)? | |
| Email verification, password reset, and Git for AI scale positioning | 167 | .dyn_into() |
| Rust repos service with shipping; pull requests kept in the model | 168 | .map_err(|_| Thrown { |
| 169 | code: None, | |
| 170 | message: format!("{method:?} is not a function"), | |
| 171 | })?; | |
| 172 | let arguments: Array = args.iter().collect(); | |
| 173 | // An RPC stub treats every property access as a remote method, so | |
| 174 | // `function.apply(...)` would be sent over the wire as a call to | |
| 175 | // "apply". Reflect.apply invokes the function without touching it. | |
| 176 | let returned = Reflect::apply(&function, target, &arguments).map_err(Thrown::from_value)?; | |
| 177 | // Worker RPC returns its own thenable rather than a Promise, so | |
| 178 | // resolve whatever came back instead of testing its type. | |
| 179 | JsFuture::from(Promise::resolve(&returned)) | |
| 180 | .await | |
| 181 | .map_err(Thrown::from_value) | |
| 182 | } | |
| 183 | ||
| 184 | /// Calls `target.method(...args)`; see [`call_key`]. | |
| 185 | pub async fn call( | |
| 186 | target: &JsValue, | |
| 187 | method: &str, | |
| 188 | args: &[JsValue], | |
| 189 | ) -> std::result::Result<JsValue, Thrown> { | |
| 190 | call_key(target, &method.into(), args).await | |
| Email verification, password reset, and Git for AI scale positioning | 191 | } |
| 192 | } | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 193 | |
| 194 | /// Moving a service's rows when a workspace is renamed. | |
| 195 | pub mod rename { | |
| 196 | use std::collections::HashMap; | |
| 197 | ||
| 198 | use g1t_contracts::events::{Event, WorkspaceRenamed}; | |
| 199 | use g1t_contracts::identity::UsernamesArgs; | |
| 200 | use worker::wasm_bindgen::JsValue; | |
| 201 | use worker::{D1Database, Env, Result}; | |
| 202 | ||
| 203 | /// Handles `workspace.renamed` with `statements`, and says whether | |
| 204 | /// `event` was one. Each statement uses `?1` for the workspace's current | |
| 205 | /// slug (asked of identity by id, so renames delivered twice or out of | |
| 206 | /// order converge) and `?2` for a slug its rows may still be under; the | |
| 207 | /// statements run in one batch per such slug. A statement that matches | |
| 208 | /// nothing changes nothing, so running them again is harmless. | |
| 209 | pub async fn on_event(env: &Env, db: &D1Database, event: &Event, statements: &[&str]) -> Result<bool> { | |
| 210 | if event.kind != "workspace.renamed" { | |
| 211 | return Ok(false); | |
| 212 | } | |
| 213 | let Ok(renamed) = serde_json::from_value::<WorkspaceRenamed>(event.data.clone()) else { | |
| 214 | worker::console_error!("workspace.renamed {} could not be read", event.id); | |
| 215 | return Ok(true); | |
| 216 | }; | |
| 217 | let names: HashMap<String, String> = crate::call( | |
| 218 | &env.service("IDENTITY")?, | |
| 219 | "usernames", | |
| 220 | &UsernamesArgs { | |
| 221 | ids: vec![renamed.workspace_id.clone()], | |
| 222 | }, | |
| 223 | ) | |
| 224 | .await?; | |
| 225 | let current = names | |
| 226 | .get(&renamed.workspace_id) | |
| 227 | .cloned() | |
| 228 | .unwrap_or_else(|| renamed.to.clone()); | |
| 229 | for stale in renamed.stale_slugs(¤t) { | |
| 230 | let values: [JsValue; 2] = [current.as_str().into(), stale.as_str().into()]; | |
| 231 | let mut batch = Vec::with_capacity(statements.len()); | |
| 232 | for sql in statements { | |
| 233 | batch.push(db.prepare(*sql).bind(&values[..parameters(sql)])?); | |
| 234 | } | |
| 235 | db.batch(batch).await?; | |
| 236 | } | |
| 237 | Ok(true) | |
| 238 | } | |
| 239 | ||
| 240 | /// How many values a statement takes: its highest `?N`. | |
| 241 | pub fn parameters(sql: &str) -> usize { | |
| 242 | sql.split('?') | |
| 243 | .skip(1) | |
| 244 | .filter_map(|rest| { | |
| 245 | let digits: String = rest.chars().take_while(char::is_ascii_digit).collect(); | |
| 246 | digits.parse().ok() | |
| 247 | }) | |
| 248 | .max() | |
| 249 | .unwrap_or(0) | |
| 250 | } | |
| 251 | ||
| 252 | #[cfg(test)] | |
| 253 | mod tests { | |
| 254 | use super::parameters; | |
| 255 | ||
| 256 | #[test] | |
| 257 | fn counts_numbered_parameters() { | |
| 258 | assert_eq!(parameters("UPDATE t SET a = ?1 WHERE a = ?2"), 2); | |
| 259 | assert_eq!(parameters("DELETE FROM t WHERE a = ?2"), 2); | |
| 260 | assert_eq!(parameters("UPDATE t SET a = ?1"), 1); | |
| 261 | assert_eq!(parameters("DELETE FROM t"), 0); | |
| 262 | } | |
| 263 | } | |
| 264 | } |