diff --git a/Cargo.lock b/Cargo.lock index 251642c..dd6353f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1664,7 +1664,7 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openworkers-runtime" -version = "0.1.1" +version = "0.1.2" dependencies = [ "actix-web", "bytes", diff --git a/Cargo.toml b/Cargo.toml index c71ee23..fa65ec8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openworkers-runtime" -version = "0.1.1" +version = "0.1.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 4fd1a66..b1bfcf8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cargo build --release --examples ### Snapshot the runtime ```bash -cargo run --example snapshot +cargo run --bin snapshot ``` ### Run the demo server diff --git a/examples/scheduled.rs b/examples/scheduled.rs index 44528e4..84377b7 100644 --- a/examples/scheduled.rs +++ b/examples/scheduled.rs @@ -2,6 +2,7 @@ use log::debug; use log::error; use openworkers_runtime::module_url; use openworkers_runtime::ScheduledInit; +use openworkers_runtime::Script; use openworkers_runtime::Task; use openworkers_runtime::Worker; use tokio::sync::oneshot; @@ -35,7 +36,10 @@ async fn main() -> Result<(), ()> { let (res_tx, res_rx) = oneshot::channel::<()>(); let (end_tx, end_rx) = oneshot::channel::<()>(); - let url = module_url(file_path.as_str()); + let script = Script { + specifier: module_url(file_path.as_str()), + code: None, + }; let time = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -46,7 +50,7 @@ async fn main() -> Result<(), ()> { let local = tokio::task::LocalSet::new(); local.spawn_local(async move { - let mut worker = Worker::new(url).await.unwrap(); + let mut worker = Worker::new(script).await.unwrap(); match worker .exec(Task::Scheduled(Some(ScheduledInit::new(res_tx, time)))) diff --git a/examples/serve-new.rs b/examples/serve-new.rs index 7a06e15..811b520 100644 --- a/examples/serve-new.rs +++ b/examples/serve-new.rs @@ -3,6 +3,7 @@ use bytes::Bytes; use log::debug; use log::error; use openworkers_runtime::FetchInit; +use openworkers_runtime::Script; use openworkers_runtime::Task; use openworkers_runtime::Url; use openworkers_runtime::Worker; @@ -37,7 +38,10 @@ async fn handle_request(data: Data, req: HttpRequest) -> HttpResponse .body(Default::default()) .unwrap(); - let url_clone = url.clone(); + let script = Script { + specifier: url.clone(), + code: None, + }; let (res_tx, res_rx) = channel::>(); let task = Task::Fetch(Some(FetchInit::new(req, res_tx))); @@ -47,7 +51,7 @@ async fn handle_request(data: Data, req: HttpRequest) -> HttpResponse let tasks = local.spawn_local(async move { debug!("create worker"); - let mut worker = Worker::new(url_clone).await.unwrap(); + let mut worker = Worker::new(script).await.unwrap(); debug!("exec fetch task"); match worker.exec(task).await { diff --git a/examples/serve-same.rs b/examples/serve-same.rs index c58f90f..3374546 100644 --- a/examples/serve-same.rs +++ b/examples/serve-same.rs @@ -3,6 +3,7 @@ use bytes::Bytes; use log::debug; use log::error; use openworkers_runtime::FetchInit; +use openworkers_runtime::Script; use openworkers_runtime::Task; use openworkers_runtime::Url; use openworkers_runtime::Worker; @@ -106,7 +107,10 @@ async fn main() -> std::io::Result<()> { .app_data(Data::new({ let path = get_path(); let url: Url = openworkers_runtime::module_url(path.as_str()); - let url_clone = url.clone(); + let script = Script { + specifier: url.clone(), + code: None, + }; let (task_tx, mut task_rx) = tokio::sync::mpsc::channel(1); @@ -114,7 +118,7 @@ async fn main() -> std::io::Result<()> { let local = tokio::task::LocalSet::new(); let tasks = local.spawn_local(async move { - let mut worker = Worker::new(url_clone).await.unwrap(); + let mut worker = Worker::new(script).await.unwrap(); loop { match task_rx.recv().await { diff --git a/src/lib.rs b/src/lib.rs index 0ff4040..2ec944d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,10 +7,12 @@ pub (crate) mod util; pub (crate) use runtime::extensions; +pub use runtime::Script; pub use runtime::Worker; pub use ext::FetchInit; pub use ext::ScheduledInit; pub use deno_core::error::AnyError; +pub use deno_core::FastString; pub use task::Task; pub use task::TaskType; pub use deno_core::Snapshot; diff --git a/src/runtime.rs b/src/runtime.rs index 945bd89..ebe235e 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -75,6 +75,11 @@ pub(crate) fn extensions(for_snapshot: bool) -> Vec { exts } +pub struct Script { + pub specifier: deno_core::ModuleSpecifier, + pub code: Option +} + pub struct Worker { pub(crate) js_runtime: deno_core::JsRuntime, pub(crate) trigger_fetch: deno_core::v8::Global, @@ -82,7 +87,7 @@ pub struct Worker { } impl Worker { - pub async fn new(main_module: Url) -> Result { + pub async fn new(script: Script) -> Result { let mut js_runtime = match runtime_snapshot() { None => { debug!("no runtime snapshot"); @@ -140,7 +145,7 @@ impl Worker { // Eval main module { - let mod_id = js_runtime.load_main_module(&main_module, None).await?; + let mod_id = js_runtime.load_main_module(&script.specifier, script.code).await?; let result = js_runtime.mod_evaluate(mod_id);