Allow disabling WASM runtime behind a feature flag. #148

This commit is contained in:
Sebastian Jeltsch
2025-09-13 09:49:14 +02:00
parent 4fbbb8e451
commit cf16c806a1
4 changed files with 46 additions and 8 deletions

View File

@@ -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 }

View File

@@ -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.

View File

@@ -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<dyn std::error::Error + Send + Sync>;
pub(crate) struct Runtime;
pub(crate) fn build_wasm_runtimes_for_components(
_n_threads: Option<usize>,
_conn: trailbase_sqlite::Connection,
_components_path: PathBuf,
_fs_root_path: Option<PathBuf>,
) -> Result<Vec<Arc<Runtime>>, AnyError> {
return Ok(vec![]);
}
#[cfg(not(feature = "wasm"))]
pub(crate) async fn install_routes_and_jobs(
_state: &AppState,
_runtime: Arc<Runtime>,
) -> Result<Option<Router<AppState>>, AnyError> {
return Ok(None);
}
}
#[cfg(test)]
mod test;

View File

@@ -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<dyn std::error::Error + Send + Sync>;
pub(crate) use trailbase_wasm_runtime_host::Runtime;
pub(crate) fn build_wasm_runtimes_for_components(
n_threads: Option<usize>,
conn: trailbase_sqlite::Connection,
components_path: PathBuf,
fs_root_path: Option<PathBuf>,
) -> Result<Vec<Arc<Runtime>>, AnyError> {
let shared_kv_store = KvStore::new();
let shared_kv_store = trailbase_wasm_runtime_host::KvStore::new();
let mut runtimes: Vec<Arc<Runtime>> = 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<Runtime>,
) -> Result<Option<Router<AppState>>, 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<Bytes, hyper::Error> {
return BoxBody::new(http_body_util::Empty::new().map_err(|_| unreachable!()));
}
fn to_header_value(context: &HttpContext) -> Result<hyper::http::HeaderValue, WasmError> {
fn to_header_value(
context: &HttpContext,
) -> Result<hyper::http::HeaderValue, trailbase_wasm_runtime_host::Error> {
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 {