diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 257a0d3d..e00a94dc 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -21,8 +21,9 @@ name = "benchmark" harness = false [features] -default = ["v8"] +default = ["v8", "wasm"] v8 = ["dep:trailbase-js"] +wasm = ["dep:trailbase-wasm-runtime-host"] [dependencies] aes-gcm = { version = "0.10.3", default-features = false, features = ["aes", "alloc", "getrandom", "rand_core"] } @@ -89,7 +90,7 @@ trailbase-qs = { workspace = true } trailbase-refinery = { workspace = true } trailbase-schema = { workspace = true } trailbase-sqlite = { workspace = true } -trailbase-wasm-runtime-host = { workspace = true } +trailbase-wasm-runtime-host = { workspace = true, optional = true } trailbase-wasm-common = { workspace = true } ts-rs = { workspace = true } url = { version = "^2.4.1", default-features = false } diff --git a/crates/core/src/app_state.rs b/crates/core/src/app_state.rs index fdccdf34..f0e889c5 100644 --- a/crates/core/src/app_state.rs +++ b/crates/core/src/app_state.rs @@ -4,7 +4,6 @@ use reactivate::{Merge, Reactive}; use std::path::PathBuf; use std::sync::Arc; use trailbase_schema::QualifiedName; -use trailbase_wasm_runtime_host::Runtime; use crate::auth::jwt::JwtHelper; use crate::auth::options::AuthOptions; @@ -17,6 +16,7 @@ use crate::records::RecordApi; use crate::records::subscribe::SubscriptionManager; use crate::scheduler::{JobRegistry, build_job_registry_from_config}; use crate::schema_metadata::SchemaMetadataCache; +use crate::wasm::Runtime; /// The app's internal state. AppState needs to be clonable which puts unnecessary constraints on /// the internals. Thus rather arc once than many times. diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 11af2d25..7a907432 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -22,8 +22,40 @@ mod scheduler; mod schema_metadata; mod server; mod transaction; + +#[cfg(feature = "wasm")] mod wasm; +#[cfg(not(feature = "wasm"))] +mod wasm { + use axum::Router; + use std::path::PathBuf; + use std::sync::Arc; + + use crate::AppState; + + type AnyError = Box; + + pub(crate) struct Runtime; + + pub(crate) fn build_wasm_runtimes_for_components( + _n_threads: Option, + _conn: trailbase_sqlite::Connection, + _components_path: PathBuf, + _fs_root_path: Option, + ) -> Result>, AnyError> { + return Ok(vec![]); + } + + #[cfg(not(feature = "wasm"))] + pub(crate) async fn install_routes_and_jobs( + _state: &AppState, + _runtime: Arc, + ) -> Result>, AnyError> { + return Ok(None); + } +} + #[cfg(test)] mod test; diff --git a/crates/core/src/wasm/mod.rs b/crates/core/src/wasm/mod.rs index e575db09..e3a7cd4e 100644 --- a/crates/core/src/wasm/mod.rs +++ b/crates/core/src/wasm/mod.rs @@ -8,8 +8,6 @@ use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; use trailbase_wasm_common::{HttpContext, HttpContextKind, HttpContextUser}; -use trailbase_wasm_runtime_host::exports::trailbase::runtime::init_endpoint::MethodType; -use trailbase_wasm_runtime_host::{Error as WasmError, KvStore, Runtime}; use crate::AppState; use crate::User; @@ -17,13 +15,15 @@ use crate::util::urlencode; type AnyError = Box; +pub(crate) use trailbase_wasm_runtime_host::Runtime; + pub(crate) fn build_wasm_runtimes_for_components( n_threads: Option, conn: trailbase_sqlite::Connection, components_path: PathBuf, fs_root_path: Option, ) -> Result>, AnyError> { - let shared_kv_store = KvStore::new(); + let shared_kv_store = trailbase_wasm_runtime_host::KvStore::new(); let mut runtimes: Vec> = vec![]; if let Ok(entries) = std::fs::read_dir(components_path) { @@ -71,6 +71,9 @@ pub(crate) async fn install_routes_and_jobs( state: &AppState, runtime: Arc, ) -> Result>, AnyError> { + use trailbase_wasm_runtime_host::Error as WasmError; + use trailbase_wasm_runtime_host::exports::trailbase::runtime::init_endpoint::MethodType; + let init_result = runtime .call(async |instance| { return instance.call_init().await; @@ -223,9 +226,11 @@ fn empty() -> BoxBody { return BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!())); } -fn to_header_value(context: &HttpContext) -> Result { +fn to_header_value( + context: &HttpContext, +) -> Result { return hyper::http::HeaderValue::from_bytes(&serde_json::to_vec(&context).unwrap_or_default()) - .map_err(|_err| WasmError::Encoding); + .map_err(|_err| trailbase_wasm_runtime_host::Error::Encoding); } fn internal_error_response(err: impl std::string::ToString) -> axum::response::Response {