diff --git a/Cargo.lock b/Cargo.lock index d6299ea..bd75888 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -451,6 +451,8 @@ dependencies = [ "r2d2", "serde", "serde_json", + "tokio", + "v8", ] [[package]] @@ -566,6 +568,16 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fslock" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57eafdd0c16f57161105ae1b98a1238f97645f2f588438b2949c99a2af9616bf" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "futures-core" version = "0.3.21" @@ -748,9 +760,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.119" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "local-channel" @@ -1314,22 +1326,36 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.17.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" +checksum = "7a8325f63a7d4774dd041e363b2409ed1c5cbbd0f867795e661df066b2b0a581" dependencies = [ + "autocfg", "bytes", "libc", "memchr", "mio", + "num_cpus", "once_cell", "parking_lot 0.12.0", "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "winapi", ] +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-rustls" version = "0.23.2" @@ -1435,6 +1461,19 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "v8" +version = "0.44.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee18f5327bc32aeacce7027886683f4f9bd06ee2a17db17860bf0ab008a4013" +dependencies = [ + "bitflags", + "fslock", + "lazy_static", + "libc", + "which", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -1542,6 +1581,17 @@ dependencies = [ "webpki", ] +[[package]] +name = "which" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c4fb54e6113b6a8772ee41c3404fb0301ac79604489467e0a9ce1f3e97c24ae" +dependencies = [ + "either", + "lazy_static", + "libc", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 9f53627..50e55d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,6 @@ chrono = "^0" anyhow = "1.0.57" r2d2 = "0.8.9" either = "1.6.1" +v8 = "0.44.2" +tokio = { version = "1.20.1", features = ["full"] } diff --git a/src/dev_server/dev_runner_api.rs b/src/dev_server/dev_runner_api.rs index ddb0023..8f73398 100644 --- a/src/dev_server/dev_runner_api.rs +++ b/src/dev_server/dev_runner_api.rs @@ -1,10 +1,10 @@ +use crate::dev_server::server::RunnerContext; use crate::runner::script_exec_model::{save_script_entry, Pool}; use crate::runner::{pkg_json_utils, scripts_exec_utils, Configuration, Script, Scripts}; use actix_web::{get, post, web, Either, Error, HttpResponse}; +use either::Left; use serde::{Deserialize, Serialize}; use std::sync::RwLock; -use either::Left; -use crate::dev_server::server::RunnerContext; // POST: url: /set-runnable-project, payload: { path: string } // DELETE: url: /remove-runnable-project, payload: { path: string } diff --git a/src/dev_server/mod.rs b/src/dev_server/mod.rs index c2b51a5..d173dc8 100644 --- a/src/dev_server/mod.rs +++ b/src/dev_server/mod.rs @@ -1,2 +1,2 @@ -pub(crate) mod server; pub mod dev_runner_api; +pub(crate) mod server; diff --git a/src/dev_server/server.rs b/src/dev_server/server.rs index f985d5b..a8b6da1 100644 --- a/src/dev_server/server.rs +++ b/src/dev_server/server.rs @@ -53,7 +53,7 @@ pub async fn start() -> std::io::Result<()> { .wrap(cors()) .wrap(Logger::default()) }) - .bind(format!("{}:{}", host, port))? - .run() - .await + .bind(format!("{}:{}", host, port))? + .run() + .await } diff --git a/src/main.rs b/src/main.rs index 3100f8f..c5e4bb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ #[macro_use] extern crate diesel; +use crate::v8::js_run; + mod cli; +mod dev_server; mod runner; mod schema; -mod dev_server; +mod v8; #[actix_web::main] async fn main() -> std::io::Result<()> { + // js_run("/Users/lironh_1/dev-liron/dev-runner/dev-runner/src/v8/foo.js".to_string()).await; + // Ok(()) dev_server::server::start().await } - diff --git a/src/runner/pkg_json_utils.rs b/src/runner/pkg_json_utils.rs index 288f2ad..81e4613 100644 --- a/src/runner/pkg_json_utils.rs +++ b/src/runner/pkg_json_utils.rs @@ -66,4 +66,4 @@ pub fn extract_scripts(projects: Vec) -> Vec { fn test_is_valid_path() { let is_valid = is_valid_path("foo/bar"); assert_eq!(is_valid, false); -} \ No newline at end of file +} diff --git a/src/runner/script_exec_model.rs b/src/runner/script_exec_model.rs index d7d1617..431d9bc 100644 --- a/src/runner/script_exec_model.rs +++ b/src/runner/script_exec_model.rs @@ -1,5 +1,5 @@ -use actix_web::web; use crate::schema::scripts_exec_log; +use actix_web::web; use chrono::Utc; use diesel::prelude::*; use diesel::{r2d2::ConnectionManager, PgConnection}; @@ -21,7 +21,10 @@ pub struct Script<'a> { pub run_start: &'a chrono::NaiveDateTime, } -pub fn save_script_entry(name: &str, either_conn: Either, PgConnection>) -> QueryResult { +pub fn save_script_entry( + name: &str, + either_conn: Either, PgConnection>, +) -> QueryResult { println!("Creating script entry: {:?}", name); let new_script = NewScript { @@ -40,5 +43,4 @@ pub fn save_script_entry(name: &str, either_conn: Either, PgConn .values(&new_script) .execute(right) } - } diff --git a/src/runner/scripts_exec_utils.rs b/src/runner/scripts_exec_utils.rs index 023600c..ed65250 100644 --- a/src/runner/scripts_exec_utils.rs +++ b/src/runner/scripts_exec_utils.rs @@ -31,7 +31,7 @@ fn kill_by_id(id: &str) -> Child { } pub fn exec_scripts(command: &str, projects: Vec) -> Option> { - //todo replace some with result to handle the projects count limitation error + //todo replace some with result to handle the projects count limitation error if projects.len() > 4 || cfg!(target_os = "windows") { return None; }; diff --git a/src/v8/foo.js b/src/v8/foo.js new file mode 100644 index 0000000..d8b0532 --- /dev/null +++ b/src/v8/foo.js @@ -0,0 +1 @@ +1+2; diff --git a/src/v8/js_run.rs b/src/v8/js_run.rs new file mode 100644 index 0000000..ca78f3d --- /dev/null +++ b/src/v8/js_run.rs @@ -0,0 +1,26 @@ + +async fn read_file(path: String) -> Result { + Ok(tokio::fs::read_to_string(path).await?) +} + +pub async fn js_run(path: String) { + let script = read_file(path).await.unwrap(); + let platform = v8::new_default_platform(0, false).make_shared(); + v8::V8::initialize_platform(platform); + v8::V8::initialize(); + + let isolate = &mut v8::Isolate::new(Default::default()); + + let scope = &mut v8::HandleScope::new(isolate); + let context = v8::Context::new(scope); + let scope = &mut v8::ContextScope::new(scope, context); + + let code = v8::String::new(scope, script.as_str()).unwrap(); + println!("javascript code: {}", code.to_rust_string_lossy(scope)); + + let script = v8::Script::compile(scope, code, None).unwrap(); + let result = script.run(scope).unwrap(); + let result = result.to_string(scope).unwrap(); + println!("result: {}", result.to_rust_string_lossy(scope)); +} + diff --git a/src/v8/mod.rs b/src/v8/mod.rs new file mode 100644 index 0000000..ea96461 --- /dev/null +++ b/src/v8/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod js_run; +pub use js_run::*; diff --git a/src/v8/readme.md b/src/v8/readme.md new file mode 100644 index 0000000..19e3b64 --- /dev/null +++ b/src/v8/readme.md @@ -0,0 +1,3 @@ +Experimental!!! + +A try to run scripts on v8 isolate \ No newline at end of file diff --git a/webapp/bun.lockb b/webapp/bun.lockb new file mode 100755 index 0000000..2f534eb Binary files /dev/null and b/webapp/bun.lockb differ