From d6d09d6f47999af237eac47771d69db68d00e2ed Mon Sep 17 00:00:00 2001 From: jkomyno Date: Wed, 22 Nov 2023 02:08:07 +0100 Subject: [PATCH] feat: remove ducktor --- Cargo.lock | 12 ------ query-engine/driver-adapters/Cargo.toml | 1 - .../src/wasm/async_js_function.rs | 14 +++++++ .../src/wasm/js_object_extern.rs | 10 +++++ query-engine/driver-adapters/src/wasm/mod.rs | 3 ++ .../driver-adapters/src/wasm/proxy.rs | 39 +++++++++++++------ .../driver-adapters/src/wasm/queryable.rs | 11 +++--- .../query-engine-wasm/src/wasm/engine.rs | 5 ++- 8 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 query-engine/driver-adapters/src/wasm/js_object_extern.rs diff --git a/Cargo.lock b/Cargo.lock index 7f79a3e8a5e7..ac4a15e64a61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1097,7 +1097,6 @@ dependencies = [ "async-trait", "bigdecimal", "chrono", - "ducktor", "expect-test", "futures", "js-sys", @@ -1121,17 +1120,6 @@ dependencies = [ "wasm-bindgen-futures", ] -[[package]] -name = "ducktor" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c421abf6328bda65f53e6a76ee9837fd197b23bdfdbcebc4d7917dfaa1cf88ae" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.28", -] - [[package]] name = "either" version = "1.9.0" diff --git a/query-engine/driver-adapters/Cargo.toml b/query-engine/driver-adapters/Cargo.toml index 29697cdc95c4..8fa27edb5aa0 100644 --- a/query-engine/driver-adapters/Cargo.toml +++ b/query-engine/driver-adapters/Cargo.toml @@ -37,5 +37,4 @@ serde-wasm-bindgen.workspace = true wasm-bindgen.workspace = true wasm-bindgen-futures.workspace = true tsify.workspace = true -ducktor = "0.1.0" pin-project = "1" diff --git a/query-engine/driver-adapters/src/wasm/async_js_function.rs b/query-engine/driver-adapters/src/wasm/async_js_function.rs index 5ae0708054c6..29e168e9665e 100644 --- a/query-engine/driver-adapters/src/wasm/async_js_function.rs +++ b/query-engine/driver-adapters/src/wasm/async_js_function.rs @@ -22,6 +22,20 @@ where _phantom_return: PhantomData, } +impl From for AsyncJsFunction +where + T: Serialize, + R: DeserializeOwned, +{ + fn from(js_fn: JsFunction) -> Self { + Self { + threadsafe_fn: js_fn, + _phantom_arg: PhantomData:: {}, + _phantom_return: PhantomData:: {}, + } + } +} + impl AsyncJsFunction where T: Serialize, diff --git a/query-engine/driver-adapters/src/wasm/js_object_extern.rs b/query-engine/driver-adapters/src/wasm/js_object_extern.rs new file mode 100644 index 000000000000..8804e706f67f --- /dev/null +++ b/query-engine/driver-adapters/src/wasm/js_object_extern.rs @@ -0,0 +1,10 @@ +use js_sys::JsString; +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; + +#[wasm_bindgen] +extern "C" { + pub type JsObjectExtern; + + #[wasm_bindgen(method, catch, structural, indexing_getter)] + pub fn get(this: &JsObjectExtern, key: JsString) -> Result; +} diff --git a/query-engine/driver-adapters/src/wasm/mod.rs b/query-engine/driver-adapters/src/wasm/mod.rs index 8636204577c9..5f817569c31c 100644 --- a/query-engine/driver-adapters/src/wasm/mod.rs +++ b/query-engine/driver-adapters/src/wasm/mod.rs @@ -3,8 +3,11 @@ mod async_js_function; mod conversion; mod error; +mod js_object_extern; mod proxy; mod queryable; mod send_future; mod transaction; + +pub use js_object_extern::JsObjectExtern; pub use queryable::{from_wasm, JsQueryable}; diff --git a/query-engine/driver-adapters/src/wasm/proxy.rs b/query-engine/driver-adapters/src/wasm/proxy.rs index e4efc059ce58..75bc8f6347e2 100644 --- a/query-engine/driver-adapters/src/wasm/proxy.rs +++ b/query-engine/driver-adapters/src/wasm/proxy.rs @@ -1,11 +1,12 @@ -use ducktor::FromJsValue as DuckType; use futures::Future; -use js_sys::{Function as JsFunction, Object as JsObject}; +use js_sys::{Function as JsFunction, JsString, Object as JsObject}; +use tsify::Tsify; use super::{async_js_function::AsyncJsFunction, send_future::SendFuture, transaction::JsTransaction}; pub use crate::types::{ColumnType, JSResultSet, Query, TransactionOptions}; +use crate::JsObjectExtern; use metrics::increment_gauge; -use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; +use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue}; type JsResult = core::result::Result; @@ -13,7 +14,7 @@ type JsResult = core::result::Result; /// querying and executing SQL (i.e. a client connector). The Proxy uses Wasm's JsFunction to /// invoke the code within the node runtime that implements the client connector. #[wasm_bindgen(getter_with_clone)] -#[derive(DuckType, Default)] +#[derive(Default)] pub(crate) struct CommonProxy { /// Execute a query given as SQL, interpolating the given parameters. query_raw: AsyncJsFunction, @@ -29,7 +30,6 @@ pub(crate) struct CommonProxy { /// This is a JS proxy for accessing the methods specific to top level /// JS driver objects #[wasm_bindgen(getter_with_clone)] -#[derive(DuckType)] pub(crate) struct DriverProxy { start_transaction: AsyncJsFunction<(), JsTransaction>, } @@ -37,7 +37,7 @@ pub(crate) struct DriverProxy { /// This a JS proxy for accessing the methods, specific /// to JS transaction objects #[wasm_bindgen(getter_with_clone)] -#[derive(DuckType, Default)] +#[derive(Default)] pub(crate) struct TransactionProxy { /// transaction options options: TransactionOptions, @@ -54,8 +54,14 @@ pub(crate) struct TransactionProxy { } impl CommonProxy { - pub fn new(object: &JsObject) -> Self { - CommonProxy::from(&object.into()) + pub fn new(object: &JsObjectExtern) -> JsResult { + let flavour: String = JsString::from(object.get("value".into())?).into(); + + Ok(Self { + query_raw: JsFunction::from(object.get("queryRaw".into())?).into(), + execute_raw: JsFunction::from(object.get("executeRaw".into())?).into(), + flavour, + }) } pub async fn query_raw(&self, params: Query) -> quaint::Result { @@ -68,8 +74,10 @@ impl CommonProxy { } impl DriverProxy { - pub fn new(object: &JsObject) -> Self { - Self::from(&object.into()) + pub fn new(object: &JsObjectExtern) -> JsResult { + Ok(Self { + start_transaction: JsFunction::from(object.get("startTransaction".into())?).into(), + }) } async fn start_transaction_inner(&self) -> quaint::Result> { @@ -91,8 +99,15 @@ impl DriverProxy { } impl TransactionProxy { - pub fn new(object: &JsObject) -> Self { - Self::from(&object.into()) + pub fn new(object: &JsObjectExtern) -> JsResult { + let options = object.get("options".into())?; + + Ok(Self { + options: TransactionOptions::from_js(options).unwrap(), + commit: JsFunction::from(object.get("commit".into())?).into(), + rollback: JsFunction::from(object.get("dispose".into())?).into(), + dispose: object.get("dispose".into())?.into(), + }) } pub fn options(&self) -> &TransactionOptions { diff --git a/query-engine/driver-adapters/src/wasm/queryable.rs b/query-engine/driver-adapters/src/wasm/queryable.rs index abb828a443c0..edb0de4ea493 100644 --- a/query-engine/driver-adapters/src/wasm/queryable.rs +++ b/query-engine/driver-adapters/src/wasm/queryable.rs @@ -1,10 +1,11 @@ +use crate::JsObjectExtern; + use super::{ conversion, proxy::{CommonProxy, DriverProxy, Query}, send_future::SendFuture, }; use async_trait::async_trait; -use ducktor::FromJsValue as DuckType; use futures::Future; use js_sys::Object as JsObject; use psl::datamodel_connector::Flavour; @@ -30,7 +31,7 @@ use wasm_bindgen::prelude::wasm_bindgen; /// into a `quaint::connector::result_set::ResultSet`. A quaint `ResultSet` is basically a vector /// of `quaint::Value` but said type is a tagged enum, with non-unit variants that cannot be converted to javascript as is. #[wasm_bindgen(getter_with_clone)] -#[derive(DuckType, Default)] +#[derive(Default)] pub(crate) struct JsBaseQueryable { pub(crate) proxy: CommonProxy, pub flavour: Flavour, @@ -313,9 +314,9 @@ impl TransactionCapable for JsQueryable { } } -pub fn from_wasm(driver: JsObject) -> JsQueryable { - let common = CommonProxy::new(&driver); - let driver_proxy = DriverProxy::new(&driver); +pub fn from_wasm(driver: JsObjectExtern) -> JsQueryable { + let common = CommonProxy::new(&driver).unwrap(); + let driver_proxy = DriverProxy::new(&driver).unwrap(); JsQueryable { inner: JsBaseQueryable::new(common), diff --git a/query-engine/query-engine-wasm/src/wasm/engine.rs b/query-engine/query-engine-wasm/src/wasm/engine.rs index 20c67ee24bce..7bf67c53c2e7 100644 --- a/query-engine/query-engine-wasm/src/wasm/engine.rs +++ b/query-engine/query-engine-wasm/src/wasm/engine.rs @@ -5,7 +5,8 @@ use crate::{ error::ApiError, logger::{LogCallback, Logger}, }; -use js_sys::{Function as JsFunction, Object as JsObject}; +use driver_adapters::JsObjectExtern; +use js_sys::Function as JsFunction; use request_handlers::ConnectorMode; use serde::{Deserialize, Serialize}; use std::{ @@ -103,7 +104,7 @@ impl QueryEngine { pub fn new( options: ConstructorOptions, callback: JsFunction, - maybe_adapter: Option, + maybe_adapter: Option, ) -> Result { log::info!("Called `QueryEngine::new()`");