Skip to content

Commit

Permalink
Update deno_core to 0.280.0 in harmonizer and router-bridge (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored May 15, 2024
1 parent 6df51ee commit 6371556
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 398 deletions.
711 changes: 400 additions & 311 deletions federation-2/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions federation-2/harmonizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ include = [
apollo-federation-types = { version = "0.11.0", path = "../../apollo-federation-types", default-features = false, features = [
"build",
] }
deno_core = "0.200.0"
deno_core = "0.280.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
insta = "1.34.0"

[build-dependencies]
deno_core = "0.200.0"
deno_core = "0.280.0"
semver = "1"
serde_json = "1"
toml_edit = "0.19"
Expand Down
7 changes: 2 additions & 5 deletions federation-2/harmonizer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,13 @@ fn create_snapshot(out_dir: &Path) -> Result<(), Box<dyn Error>> {
// functions for interacting with it.
let runtime_str = read_to_string("bundled/runtime.js").unwrap();
runtime
.execute_script("<init>", deno_core::FastString::Owned(runtime_str.into()))
.execute_script("<init>", runtime_str)
.expect("unable to initialize router bridge runtime environment");

// Load the composition library.
let bridge_str = read_to_string("bundled/composition_bridge.js").unwrap();
runtime
.execute_script(
"composition_bridge.js",
deno_core::FastString::Owned(bridge_str.into()),
)
.execute_script("composition_bridge.js", bridge_str)
.expect("unable to evaluate bridge module");

// Create our base query snapshot which will be included in
Expand Down
27 changes: 9 additions & 18 deletions federation-2/harmonizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ composition implementation while we work toward something else.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, future_incompatible, unreachable_pub, rust_2018_idioms)]
use deno_core::{JsRuntime, RuntimeOptions, Snapshot};
use deno_core::{JsRuntime, RuntimeOptions};

mod js_types;

Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn harmonize_limit(

// Use our snapshot to provision our new runtime
let options = RuntimeOptions {
startup_snapshot: Some(Snapshot::Static(buffer)),
startup_snapshot: Some(buffer),
..Default::default()
};
let mut runtime = JsRuntime::new(options);
Expand All @@ -71,33 +71,24 @@ pub fn harmonize_limit(

// store the subgraph definition JSON in the `serviceList` variable
runtime
.execute_script(
"<set_service_list>",
deno_core::FastString::Owned(service_list_javascript.into()),
)
.execute_script("<set_service_list>", service_list_javascript)
.expect("unable to evaluate service list in JavaScript runtime");

// store the nodes_limit variable in the nodesLimit variable
runtime
.execute_script(
"<set_nodes_limit>",
deno_core::FastString::Owned(
format!(
"nodesLimit = {}",
nodes_limit
.map(|n| n.to_string())
.unwrap_or("null".to_string())
)
.into(),
format!(
"nodesLimit = {}",
nodes_limit
.map(|n| n.to_string())
.unwrap_or("null".to_string())
),
)
.expect("unable to evaluate nodes limit in JavaScript runtime");

// run the unmodified do_compose.js file, which expects `serviceList` to be set
match runtime.execute_script(
"do_compose",
deno_core::FastString::Static(include_str!("../bundled/do_compose.js")),
) {
match runtime.execute_script("do_compose", include_str!("../bundled/do_compose.js")) {
Ok(execute_result) => {
let scope = &mut runtime.handle_scope();
let local = deno_core::v8::Local::new(scope, execute_result);
Expand Down
24 changes: 12 additions & 12 deletions federation-2/router-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ include = [
[dependencies]
anyhow = "1.0.79"
async-channel = "1.9.0"
deno_console = "0.115.0"
deno_core = "0.200.0"
deno_crypto = "0.129.0"
deno_url = "0.115.0"
deno_web = "0.146.0"
deno_webidl = "0.115.0"
deno_console = "0.151.0"
deno_core = "0.280.0"
deno_crypto = "0.165.0"
deno_url = "0.151.0"
deno_web = "0.182.0"
deno_webidl = "0.151.0"
rand = "0.8.5"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = { version = "1.0.111", features = ["preserve_order"] }
Expand All @@ -46,12 +46,12 @@ tracing-test = "0.2.1"
criterion = { version = "0.4", features = ["async_tokio", "async_futures"] }

[build-dependencies]
deno_console = "0.115.0"
deno_core = "0.200.0"
deno_crypto = "0.129.0"
deno_url = "0.115.0"
deno_web = "0.146.0"
deno_webidl = "0.115.0"
deno_console = "0.151.0"
deno_core = "0.280.0"
deno_crypto = "0.165.0"
deno_url = "0.151.0"
deno_web = "0.182.0"
deno_webidl = "0.151.0"
which = "4.4.2"

[features]
Expand Down
8 changes: 2 additions & 6 deletions federation-2/router-bridge/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ fn create_snapshot(out_dir: &Path) {
// functions for interacting with it.
let runtime_str = read_to_string("bundled/runtime.js").unwrap();
runtime
.execute_script("<init>", deno_core::FastString::Owned(runtime_str.into()))
.execute_script("<init>", runtime_str)
.expect("unable to initialize router bridge runtime environment");

// Load the composition library.
let bridge_str = read_to_string("bundled/bridge.js").unwrap();
runtime
.execute_script("bridge.js", deno_core::FastString::Owned(bridge_str.into()))
.execute_script("bridge.js", bridge_str)
.expect("unable to evaluate bridge module");

// Create our base query snapshot which will be included in
Expand All @@ -118,8 +118,4 @@ impl deno_web::TimersPermission for Permissions {
fn allow_hrtime(&mut self) -> bool {
unreachable!("snapshotting!")
}

fn check_unstable(&self, _state: &deno_core::OpState, _api_name: &'static str) {
unreachable!("snapshotting!")
}
}
9 changes: 4 additions & 5 deletions federation-2/router-bridge/js-src/test_logger_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ type CommandResult = {
payload: boolean;
};

const send = async (result: CommandResult): Promise<void> => {
await Deno.core.opAsync("send", result);
};
const receive = async (): Promise<Command> =>
await Deno.core.opAsync("receive");
const send = async (result: CommandResult): Promise<void> =>
await Deno.core.ops.send(result);

const receive = async (): Promise<Command> => await Deno.core.ops.receive();

async function run() {
while (true) {
Expand Down
19 changes: 6 additions & 13 deletions federation-2/router-bridge/src/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::Error;
/// Wraps creating the Deno Js runtime collecting parameters and executing a script.
use deno_core::{Extension, JsRuntime, RuntimeOptions, Snapshot};
use deno_core::{Extension, JsRuntime, RuntimeOptions};
use serde::de::DeserializeOwned;
use serde::Serialize;

Expand Down Expand Up @@ -51,14 +51,11 @@ impl Js {

for parameter in self.parameters.iter() {
runtime
.execute_script(
parameter.0,
deno_core::FastString::Owned(parameter.1.clone().into()),
)
.execute_script(parameter.0, parameter.1.clone())
.expect("unable to evaluate service list in JavaScript runtime");
}

match runtime.execute_script(name, deno_core::FastString::Static(source)) {
match runtime.execute_script(name, source) {
Ok(execute_result) => {
let scope = &mut runtime.handle_scope();
let local = deno_core::v8::Local::new(scope, execute_result);
Expand Down Expand Up @@ -114,10 +111,6 @@ impl Js {
// not needed in the planner
false
}

fn check_unstable(&self, _state: &deno_core::OpState, _api_name: &'static str) {
unreachable!("not needed in the planner")
}
}

#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
Expand All @@ -130,7 +123,7 @@ impl Js {
deno_crypto::deno_crypto::init_ops(None),
my_ext,
],
startup_snapshot: Some(Snapshot::Static(buffer)),
startup_snapshot: Some(buffer),
..Default::default()
});

Expand All @@ -155,13 +148,13 @@ impl Js {
// functions for interacting with it.
let runtime_str = include_str!("../bundled/runtime.js");
js_runtime
.execute_script("<init>", deno_core::FastString::Owned(runtime_str.into()))
.execute_script("<init>", runtime_str)
.expect("unable to initialize router bridge runtime environment");

// Load the composition library.
let bridge_str = include_str!("../bundled/bridge.js");
js_runtime
.execute_script("bridge.js", deno_core::FastString::Owned(bridge_str.into()))
.execute_script("bridge.js", bridge_str)
.expect("unable to evaluate bridge module");
js_runtime
};
Expand Down
54 changes: 28 additions & 26 deletions federation-2/router-bridge/src/worker.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::error::Error;
use async_channel::{bounded, Receiver, Sender};
use deno_core::Op;
use deno_core::{op, Extension, OpState};
use deno_core::{op2, Extension, OpState};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::hash::Hasher;
use std::rc::Rc;
Expand Down Expand Up @@ -68,14 +66,14 @@ impl JsWorker {
let handle = std::thread::spawn(move || {
let my_ext = Extension {
name: concat!(env!("CARGO_PKG_NAME"), "_worker"),
ops: Cow::Borrowed(&[
send::DECL,
receive::DECL,
log_trace::DECL,
log_debug::DECL,
log_info::DECL,
log_warn::DECL,
log_error::DECL,
ops: Cow::Owned(vec![
send(),
receive(),
log_trace(),
log_debug(),
log_info(),
log_warn(),
log_error(),
]),
op_state_fn: Some(Box::new(move |state| {
state.put(response_sender.clone());
Expand All @@ -94,9 +92,9 @@ impl JsWorker {

let future = async move {
js_runtime
.execute_script_static("worker.js", worker_source_code)
.execute_script("worker.js", worker_source_code)
.unwrap();
js_runtime.run_event_loop(false).await
js_runtime.run_event_loop(Default::default()).await
};
runtime.block_on(future).unwrap();
});
Expand Down Expand Up @@ -221,38 +219,41 @@ impl Drop for JsWorker {
}

// Logging capabilities
#[op]
fn log_trace(_: &mut OpState, message: String) -> Result<(), anyhow::Error> {
#[op2(fast)]
fn log_trace(_: &mut OpState, #[string] message: String) -> Result<(), anyhow::Error> {
tracing::trace!("{message}");
Ok(())
}

#[op]
fn log_debug(_: &mut OpState, message: String) -> Result<(), anyhow::Error> {
#[op2(fast)]
fn log_debug(_: &mut OpState, #[string] message: String) -> Result<(), anyhow::Error> {
tracing::debug!("{message}");
Ok(())
}

#[op]
fn log_info(_: &mut OpState, message: String) -> Result<(), anyhow::Error> {
#[op2(fast)]
fn log_info(_: &mut OpState, #[string] message: String) -> Result<(), anyhow::Error> {
tracing::info!("{message}");
Ok(())
}

#[op]
fn log_warn(_: &mut OpState, message: String) -> Result<(), anyhow::Error> {
#[op2(fast)]
fn log_warn(_: &mut OpState, #[string] message: String) -> Result<(), anyhow::Error> {
tracing::warn!("{message}");
Ok(())
}

#[op]
fn log_error(_: &mut OpState, message: String) -> Result<(), anyhow::Error> {
#[op2(fast)]
fn log_error(_: &mut OpState, #[string] message: String) -> Result<(), anyhow::Error> {
tracing::error!("{message}");
Ok(())
}

#[op]
async fn send(state: Rc<RefCell<OpState>>, payload: JsonPayload) -> Result<(), anyhow::Error> {
#[op2(async)]
async fn send(
state: Rc<RefCell<OpState>>,
#[serde] payload: JsonPayload,
) -> Result<(), anyhow::Error> {
let sender = {
let state = state.borrow();
// we're cloning here because we don't wanna keep the borrow across an await point
Expand All @@ -265,7 +266,8 @@ async fn send(state: Rc<RefCell<OpState>>, payload: JsonPayload) -> Result<(), a
.map_err(|e| anyhow::anyhow!("couldn't send response {e}"))
}

#[op]
#[op2(async)]
#[serde]
async fn receive(state: Rc<RefCell<OpState>>) -> Result<JsonPayload, anyhow::Error> {
let receiver = {
let state = state.borrow();
Expand Down

0 comments on commit 6371556

Please sign in to comment.