use std::sync::Arc;
use super::App;
use crate::backend::{CodeCache, Engine};
use crate::bridge::HostBridge;
use crate::{AppHandle, Backend, Error, HostServices, Limits, Module, Result};
#[derive(Clone)]
pub struct AppBuilder {
pub(super) backend: Backend,
pub(super) module: Option<Module>,
pub(super) hosts: HostServices,
pub(super) limits: Limits,
pub(super) handler: Option<String>,
pub(super) key: Option<u64>,
pub(super) host_runtime: Option<tokio::runtime::Handle>,
pub(super) code_cache: Arc<CodeCache>,
#[cfg(fetchlet_process)]
pub(super) isolation: crate::Isolation,
}
impl AppBuilder {
#[cfg(fetchlet_process)]
pub fn isolation(mut self, isolation: crate::Isolation) -> Self {
self.isolation = isolation;
self
}
pub fn module(mut self, module: Module) -> Self {
self.module = Some(module);
self.code_cache = Arc::default();
self
}
pub fn host_services(mut self, hosts: HostServices) -> Self {
self.hosts = hosts;
self
}
pub fn limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
pub fn fetch_handler<'a>(mut self, export: impl Into<Option<&'a str>>) -> Self {
self.handler = export.into().map(str::to_owned);
self
}
pub fn host_runtime(mut self, runtime: tokio::runtime::Handle) -> Self {
self.host_runtime = Some(runtime);
self
}
pub(crate) fn keyed(mut self, key: u64) -> Self {
self.key = Some(key);
self
}
pub async fn spawn(self, options: crate::WorkerOptions) -> Result<AppHandle> {
let limits = self.limits.clone();
AppHandle::spawn(self, options, limits).await
}
pub fn build(self) -> Result<App> {
self.limits.validate()?;
self.backend.validate()?;
self.backend.validate_limits(&self.limits)?;
let module = self
.module
.ok_or_else(|| Error::Configuration("a module is required".into()))?;
let mut host = HostBridge::with_limits(self.hosts, self.limits.clone());
if let Some(key) = self.key {
host = host.with_key(key);
}
if let Some(runtime) = self.host_runtime {
host = host.with_runtime(runtime);
}
#[cfg(fetchlet_process)]
let isolated = match &self.isolation {
crate::Isolation::Process(options) => Some(Engine::isolated(
options,
self.backend,
&module,
host.clone(),
&self.limits,
)),
crate::Isolation::InProcess => None,
};
#[cfg(not(fetchlet_process))]
let isolated: Option<Result<Engine>> = None;
let engine = match isolated {
Some(engine) => engine?,
None => Engine::new(
self.backend,
&module,
host.clone().into(),
self.code_cache,
&self.limits,
)?,
};
Ok(App {
engine,
host,
limits: self.limits,
handler: self.handler,
reusable: true,
background: None,
})
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::{App, Backend, Module};
const COUNTER: &str = "let count = 0;\
globalThis.fetchletExports = { next: () => ++count, name: () => 'counter' };";
fn caching_backends() -> Vec<Backend> {
vec![
#[cfg(feature = "quickjs")]
Backend::quickjs(),
#[cfg(feature = "v8")]
Backend::v8(),
]
}
#[tokio::test(flavor = "current_thread")]
async fn instances_of_one_builder_share_compiled_code_but_not_state() {
for backend in caching_backends() {
let builder = App::builder(backend).module(Module::new(COUNTER));
let mut first = builder.clone().build().unwrap();
let cached = builder.code_cache.bytes();
assert!(cached > 0, "{backend:?} cached nothing");
assert_eq!(first.call_json("next", &[]).await.unwrap(), json!(1));
assert_eq!(first.call_json("next", &[]).await.unwrap(), json!(2));
let mut second = builder.clone().build().unwrap();
assert_eq!(builder.code_cache.bytes(), cached, "{backend:?}");
assert_eq!(second.call_json("next", &[]).await.unwrap(), json!(1));
assert_eq!(
second.call_json("name", &[]).await.unwrap(),
json!("counter")
);
}
}
#[tokio::test(flavor = "current_thread")]
async fn another_module_compiles_its_own_code() {
for backend in caching_backends() {
let builder = App::builder(backend).module(Module::new(COUNTER));
builder.clone().build().unwrap();
let other = COUNTER.replace("'counter'", "'another'");
assert_eq!(other.len(), COUNTER.len());
let mut app = builder.module(Module::new(other)).build().unwrap();
assert_eq!(app.call_json("name", &[]).await.unwrap(), json!("another"));
}
}
#[test]
fn a_module_that_throws_fails_alike_from_compiled_code() {
for backend in caching_backends() {
let builder =
App::builder(backend).module(Module::new("throw new Error('broken at start');"));
for _ in 0..2 {
let error = builder.clone().build().err().unwrap().to_string();
assert!(error.contains("broken at start"), "{backend:?}: {error}");
}
}
}
}