//! The Worker's door into cotton-rules: `call(name, input)` with JSON text in //! and `{"ok": …}` or `{"error": …}` out (`cotton_rules::boundary`), and //! `plan(name, input, ask)` — the messages engine, driver-in, asking the //! Worker's object every read through the JS function `ask(question, args)` //! (`cotton_rules::import::asked`). Nothing else lives here, so every rule is //! tested natively in cotton-rules. use js_sys::Function; use serde_json::Value; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsValue; #[wasm_bindgen] pub fn call(name: &str, input: &str) -> String { cotton_rules::boundary::call_json(name, input) } /// `ask(question, argsJson)` answering JSON text, synchronously: the Worker's /// storage is synchronous, so a read is answered from inside the plan. struct JsAsk<'a>(&'a Function); impl cotton_rules::import::asked::Ask for JsAsk<'_> { fn ask(&self, question: &str, args: Value) -> Result { let answer = self .0 .call2(&JsValue::NULL, &JsValue::from_str(question), &JsValue::from_str(&args.to_string())) .map_err(|thrown| format!("{question}: the driver threw: {}", js_sys::Error::from(thrown).message()))?; let text = answer.as_string().ok_or_else(|| format!("{question}: the driver answered no JSON text"))?; serde_json::from_str(&text).map_err(|error| format!("{question}: the driver's answer is not JSON: {error}")) } } #[wasm_bindgen] pub fn plan(name: &str, input: &str, ask: &Function) -> String { cotton_rules::import::asked::run_json(name, input, &JsAsk(ask)) } /// Mail that arrives on a person's own domain (cotton_rules::mail::arrive): /// the raw message's bytes, and `{ database, account, received }` as JSON; /// `{"ok": …rows…}` or `{"error": "…"}`. Bytes go in as bytes, never as JSON /// text: a message may be 25 MiB. #[wasm_bindgen] pub fn mail_arrive(raw: &[u8], input: &str) -> String { let answer = (|| -> Result { let input: Value = serde_json::from_str(input).map_err(|error| format!("the input is not JSON: {error}"))?; let database = input["database"].as_str().ok_or("database is required")?; let account = input["account"].as_str().ok_or("account is required")?; // `fetched` (Cotton's fetcher, Post CONTRACT row 13): { msgid?, thrid? (decimal strings), labels: [], flags: [], inbox }. let fetched = input.get("fetched").filter(|f| f.is_object()).map(|f| { let id = |key: &str| f[key].as_str().and_then(|s| s.parse::().ok()).or_else(|| f[key].as_u64()); let list = |key: &str| f[key].as_array().map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default(); cotton_rules::mail::arrive::Fetched { msgid: id("msgid"), thrid: id("thrid"), gm_labels: list("labels"), flags: list("flags"), in_inbox: f["inbox"].as_bool().unwrap_or(false) } }); let at = cotton_rules::mail::arrive::Arriving { database, account, received: input["received"].as_i64(), fetched }; cotton_rules::mail::arrive::arrive(raw, &at) })(); match answer { Ok(ok) => serde_json::json!({ "ok": ok }).to_string(), Err(error) => serde_json::json!({ "error": error }).to_string(), } } /// The `index`th attachment's decoded bytes, in `mail_arrive`'s order; none past the last. #[wasm_bindgen] pub fn mail_attachment(raw: &[u8], index: u32) -> Option> { cotton_rules::mail::arrive::attachment(raw, index as usize) }