Skip to content

Commit

Permalink
Merge pull request #401 from coasys/rust-integration
Browse files Browse the repository at this point in the history
Rust integration with launcher
  • Loading branch information
jdeepee authored Aug 3, 2023
2 parents c8d226a + 79029d2 commit 1c5a57c
Show file tree
Hide file tree
Showing 21 changed files with 210 additions and 144 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

16 changes: 16 additions & 0 deletions executor/src/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ export const init = internalInit
export const path = internalPath
export const os = internalOs

console.log = (...args) => {
UTILS.consoleLog(`${args.reduce((acc, cur) => acc += `${cur} `, "")}`)
};

console.debug = (...args) => {
UTILS.consoleDebug(`${args.reduce((acc, cur) => acc += `${cur} `, "")}`)
};

console.error = (...args) => {
UTILS.consoleError(`${args.reduce((acc, cur) => acc += `${cur} `, "")}`)
};

console.warn = (...args) => {
UTILS.consoleWarn(`${args.reduce((acc, cur) => acc += `${cur} `, "")}`)
};

import "https://deno.land/x/xhr@0.3.0/mod.ts";

import { HTMLElement } from "linkedom"
Expand Down
4 changes: 4 additions & 0 deletions executor/src/utils_extension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ declare global {
interface Utils {
getSigningDNA: () => Uint8Array;
hash: (data: string | buffer) => string;
consoleLog: (...args) => void;
consoleDebug: (...args) => void;
consoleError: (...args) => void;
consoleWarn: (...args) => void;
async loadModule: (path: String) => string;
}

Expand Down
2 changes: 2 additions & 0 deletions rust-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ warp = "0.3.4"
jsonwebtoken = "8.3.0"

holochain = "0.1.3"

tracing = "0.1"
4 changes: 2 additions & 2 deletions rust-executor/src/graphql/mutation_resolvers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_snake_case)]
use juniper::{graphql_object, graphql_value, FieldResult};
use log::debug;
use tracing::debug;

use super::graphql_types::*;
use super::utils::get_capabilies;
Expand Down Expand Up @@ -823,7 +823,7 @@ impl Mutation {
{{ mutations: {}, uuid: "{}", status: {} }},
{{ capabilities: {} }}
))"#,
mutations_json, uuid, status, capabilities
mutations_json, uuid, status, capabilities
);
let result = js.execute(script).await?;
let result: JsResultType<LinkExpressionMutations> = serde_json::from_str(&result)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use holochain::{
Signature, ZomeCallResponse,
},
};
use log::info;
use tracing::info;

use crate::holochain_service::{HolochainService, LocalConductorConfig};

Expand Down
2 changes: 1 addition & 1 deletion rust-executor/src/holochain_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use holochain::prelude::{
Signature, Timestamp, TransportConfig, ZomeCallResponse, ZomeCallUnsigned,
};
use holochain::test_utils::itertools::Either;
use log::info;
use tracing::info;
use rand::Rng;
use serde::{Deserialize, Serialize};
use tokio::sync::{mpsc, oneshot, Mutex};
Expand Down
4 changes: 2 additions & 2 deletions rust-executor/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::{info, warn};
use tracing::{info, warn};
use semver::{Version, VersionReq};
use std::error::Error;
use std::fs;
Expand All @@ -13,7 +13,7 @@ pub fn init(
network_bootstrap_seed: Option<String>,
) -> Result<(), Box<dyn Error>> {
std::env::set_var("RUST_LOG", "info");
env_logger::init();
env_logger::try_init();

//Get the default data path if none is provided
let app_data_path = match data_path {
Expand Down
6 changes: 3 additions & 3 deletions rust-executor/src/js_core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use deno_core::resolve_url_or_path;
use deno_runtime::worker::MainWorker;
use deno_runtime::{permissions::PermissionsContainer, BootstrapOptions};
use holochain::prelude::{ExternIO, Signal};
use log::{error, info};
use tracing::{error, info};
use once_cell::sync::Lazy;
use std::env::current_dir;
use std::sync::Arc;
Expand Down Expand Up @@ -215,8 +215,8 @@ impl JsCore {
let wrapped_script = format!(
r#"
globalThis.asyncResult = undefined;
(async () => {{
globalThis.asyncResult = ({});
(async () => {{
globalThis.asyncResult = ({});
}})();
"#,
script
Expand Down
2 changes: 1 addition & 1 deletion rust-executor/src/js_core/string_module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use deno_core::ModuleSpecifier;
use deno_core::ModuleType;
use deno_core::ResolutionKind;
use deno_runtime::deno_core::error::AnyError;
use log::info;
use tracing::info;
use std::collections::HashMap;
use std::pin::Pin;

Expand Down
13 changes: 12 additions & 1 deletion rust-executor/src/js_core/utils_extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
},
loadModule: async (path) => {
return core.opAsync("load_module", path);
},
consoleLog: (args) => {
return core.ops.console_log(args);
},
consoleDebug: (args) => {
return core.ops.console_debug(args);
},
consoleError: (args) => {
return core.ops.console_error(args);
},
consoleWarn: (args) => {
return core.ops.console_warn(args);
}
};
})(globalThis);

39 changes: 37 additions & 2 deletions rust-executor/src/js_core/utils_extension.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cid::Cid;
use deno_core::{error::AnyError, include_js_files, op, Extension};
use log::info;
use multibase::Base;
use multihash::{Code, MultihashDigest};
use tracing::{error, info, debug, warn};

use super::JS_CORE_HANDLE;

Expand All @@ -20,6 +20,34 @@ fn hash(data: String) -> Result<String, AnyError> {
Ok(format!("Qm{}", encoded_cid))
}

#[op]
fn console_log(data: String) -> Result<String, AnyError> {
info!("[JSCORE]: {:?}", data);

Ok(String::from("temp"))
}

#[op]
fn console_debug(data: String) -> Result<String, AnyError> {
debug!("[JSCORE]: {:?}", data);

Ok(String::from("temp"))
}

#[op]
fn console_error(data: String) -> Result<String, AnyError> {
error!("[JSCORE]: {:?}", data);

Ok(String::from("temp"))
}

#[op]
fn console_warn(data: String) -> Result<String, AnyError> {
warn!("[JSCORE]: {:?}", data);

Ok(String::from("temp"))
}

#[op]
async fn load_module(path: String) -> Result<String, AnyError> {
info!("Trying to load module: {}", path);
Expand All @@ -37,7 +65,14 @@ async fn load_module(path: String) -> Result<String, AnyError> {
pub fn build() -> Extension {
Extension::builder("utils")
.js(include_js_files!(utils "utils_extension.js",))
.ops(vec![hash::decl(), load_module::decl()])
.ops(vec![
hash::decl(),
load_module::decl(),
console_log::decl(),
console_debug::decl(),
console_error::decl(),
console_warn::decl(),
])
.force_op_registration()
.build()
}
2 changes: 1 addition & 1 deletion rust-executor/src/js_core/wallet_extension.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use base64::{engine::general_purpose as base64engine, Engine as _};
use deno_core::{anyhow::anyhow, error::AnyError, include_js_files, op, Extension};
use did_key::{CoreSign, PatchedKeyPair};
use log::error;
use tracing::error;
use serde::{Deserialize, Serialize};

use crate::wallet::Wallet;
Expand Down
42 changes: 39 additions & 3 deletions rust-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ mod holochain_service;
mod js_core;
mod utils;
mod wallet;
use tokio;


pub mod init;
mod pubsub;

use log::{error, info};
use std::env;
use tracing::{info, error};

//use graphql::start_server;
use js_core::JsCore;
Expand All @@ -19,7 +21,7 @@ pub use config::Ad4mConfig;

/// Runs the GraphQL server and the deno core runtime
pub async fn run(mut config: Ad4mConfig) {
env::set_var("RUST_LOG", "rust_executor=trace,warp::server");
env::set_var("RUST_LOG", "rust_executor=info,warp::server");
let _ = env_logger::try_init();
config.prepare();

Expand All @@ -29,6 +31,7 @@ pub async fn run(mut config: Ad4mConfig) {
info!("js_core initialized.");

info!("Starting GraphQL...");

match graphql::start_server(
js_core_handle,
config.gql_port.expect("Did not get gql port"),
Expand All @@ -43,5 +46,38 @@ pub async fn run(mut config: Ad4mConfig) {
error!("GraphQL server stopped with error: {}", err);
std::process::exit(1);
}
}
};
}

/// Runs the GraphQL server and the deno core runtime
pub async fn run_with_tokio(mut config: Ad4mConfig) {
env::set_var("RUST_LOG", "rust_executor=info,warp::server");
let _ = env_logger::try_init();
config.prepare();

info!("Starting js_core...");
let mut js_core_handle = JsCore::start(config.clone()).await;
js_core_handle.initialized().await;
info!("js_core initialized.");

info!("Starting GraphQL...");

tokio::task::spawn_blocking(move || {
let result = graphql::start_server(
js_core_handle,
config.gql_port.expect("Did not get gql port"),
);
tokio::runtime::Handle::current().block_on(async {
match result.await {
Ok(_) => {
info!("GraphQL server stopped.");
std::process::exit(0);
}
Err(err) => {
error!("GraphQL server stopped with error: {}", err);
std::process::exit(1);
}
}
});
});
}
4 changes: 2 additions & 2 deletions rust-executor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod wallet;
pub mod init;
mod pubsub;

use log::{error, info};
use tracing::{error, info};
use rust_executor::Ad4mConfig;
use std::env;

Expand All @@ -17,7 +17,7 @@ use js_core::JsCore;
#[tokio::main(flavor = "multi_thread")]
async fn main() {
env::set_var("RUST_LOG", "rust_executor=info");
env_logger::init();
env_logger::try_init();

let mut config = Ad4mConfig::default();
config.prepare();
Expand Down
3 changes: 1 addition & 2 deletions rust-executor/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::graphql::graphql_types::GetValue;
use futures::Stream;
use futures::StreamExt;
use juniper::{graphql_value, FieldError, FieldResult};
use log::debug;
use log::error;
use tracing::{debug, error};
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::pin::Pin;
Expand Down
3 changes: 3 additions & 0 deletions ui/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ tauri-plugin-positioner = { version = "1.0", features = ["system-tray"] }
remove_dir_all = "0.7.0"
reqwest = { version = "0.11", features = ["json"] }
ad4m-client = { path = "../../rust-client" }
rust-executor = { path = "../../rust-executor" }
tracing = "0.1"
tracing-subscriber = "0.2"

[features]
# by default Tauri runs in production mode
Expand Down
4 changes: 0 additions & 4 deletions ui/src-tauri/src/commands/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ pub fn open_tray_message(app_handle: tauri::AppHandle) {

#[tauri::command]
pub fn clear_state(app_handle: tauri::AppHandle) {
find_and_kill_processes("ad4m-host");

find_and_kill_processes("holochain");

let _ = remove_dir_all(data_path());

app_handle.restart();
Expand Down
12 changes: 0 additions & 12 deletions ui/src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ pub fn log_path() -> PathBuf {
data_path().join("ad4m.log")
}

pub fn binary_path() -> PathBuf {
data_path().join("binary")
}

pub fn holochain_binary_path() -> PathBuf {
if cfg!(windows) {
binary_path().join("holochain.exe")
} else {
binary_path().join("holochain")
}
}

#[cfg(feature = "custom-protocol")]
pub fn app_url() -> String {
"index.html".to_string()
Expand Down
Loading

0 comments on commit 1c5a57c

Please sign in to comment.