Skip to content

Commit

Permalink
Add Script struct
Browse files Browse the repository at this point in the history
  • Loading branch information
max-lt committed Mar 23, 2024
1 parent 19a9442 commit cf21566
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cargo build --release --examples

### Snapshot the runtime
```bash
cargo run --example snapshot
cargo run --bin snapshot
```

### Run the demo server
Expand Down
8 changes: 6 additions & 2 deletions examples/scheduled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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))))
Expand Down
8 changes: 6 additions & 2 deletions examples/serve-new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +38,10 @@ async fn handle_request(data: Data<AppState>, 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::<http_v02::Response<Bytes>>();
let task = Task::Fetch(Some(FetchInit::new(req, res_tx)));
Expand All @@ -47,7 +51,7 @@ async fn handle_request(data: Data<AppState>, 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 {
Expand Down
8 changes: 6 additions & 2 deletions examples/serve-same.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,15 +107,18 @@ 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);

let _tread = std::thread::spawn(move || {
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 {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ pub(crate) fn extensions(for_snapshot: bool) -> Vec<deno_core::Extension> {
exts
}

pub struct Script {
pub specifier: deno_core::ModuleSpecifier,
pub code: Option<deno_core::ModuleCodeString>
}

pub struct Worker {
pub(crate) js_runtime: deno_core::JsRuntime,
pub(crate) trigger_fetch: deno_core::v8::Global<deno_core::v8::Function>,
pub(crate) trigger_scheduled: deno_core::v8::Global<deno_core::v8::Function>,
}

impl Worker {
pub async fn new(main_module: Url) -> Result<Self, AnyError> {
pub async fn new(script: Script) -> Result<Self, AnyError> {
let mut js_runtime = match runtime_snapshot() {
None => {
debug!("no runtime snapshot");
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit cf21566

Please sign in to comment.