-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
3,620 additions
and
403 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "dozer-ingestion-javascript" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
deno_ast = "0.29.5" | ||
deno_runtime = "0.129.0" | ||
dozer-ingestion-connector = { path = "../connector" } | ||
|
||
[dev-dependencies] | ||
camino = "1.1.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(async () => { | ||
const url = 'https://api.github.com/repos/getdozer/dozer/commits'; | ||
const response = await fetch(url); | ||
|
||
const commits = await response.json(); | ||
|
||
const snapshot_msg = { typ: "SnapshottingDone", old_val: null, new_val: null }; | ||
await Deno[Deno.internal].core.ops.ingest(snapshot_msg); | ||
|
||
for (const commit of commits) { | ||
const msg = { | ||
typ: "Insert", | ||
old_val: null, | ||
new_val: { commit: commit.sha }, | ||
}; | ||
await Deno[Deno.internal].core.ops.ingest(msg); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
use std::{future::Future, sync::Arc}; | ||
|
||
use deno_runtime::{ | ||
deno_core::{self, anyhow::Error, extension, op2, ModuleSpecifier}, | ||
permissions::PermissionsContainer, | ||
worker::{MainWorker, WorkerOptions}, | ||
}; | ||
use dozer_ingestion_connector::{ | ||
dozer_types::{ | ||
errors::{internal::BoxedError, types::DeserializationError}, | ||
json_types::serde_json_to_json_value, | ||
models::ingestion_types::IngestionMessage, | ||
serde::{Deserialize, Serialize}, | ||
serde_json, | ||
thiserror::{self, Error}, | ||
types::{Field, Operation, Record}, | ||
}, | ||
tokio::{runtime::Runtime, task::LocalSet}, | ||
Ingestor, | ||
}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(crate = "dozer_ingestion_connector::dozer_types::serde")] | ||
pub enum MsgType { | ||
SnapshottingStarted, | ||
SnapshottingDone, | ||
Insert, | ||
Delete, | ||
Update, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(crate = "dozer_ingestion_connector::dozer_types::serde")] | ||
pub struct JsMessage { | ||
typ: MsgType, | ||
old_val: serde_json::Value, | ||
new_val: serde_json::Value, | ||
} | ||
|
||
#[op2(async)] | ||
fn ingest( | ||
#[state] ingestor: &Ingestor, | ||
#[serde] val: JsMessage, | ||
) -> impl Future<Output = Result<(), Error>> { | ||
send(ingestor.clone(), val) | ||
} | ||
|
||
extension!( | ||
dozer_extension, | ||
ops = [ingest], | ||
options = { ingestor: Ingestor }, | ||
state = |state, options| { | ||
state.put(options.ingestor); | ||
}, | ||
); | ||
|
||
pub struct JsExtension { | ||
runtime: Arc<Runtime>, | ||
ingestor: Ingestor, | ||
module_specifier: ModuleSpecifier, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum JsExtensionError { | ||
#[error("Failed to canonicalize path {0}: {1}")] | ||
CanonicalizePath(String, #[source] std::io::Error), | ||
} | ||
|
||
impl JsExtension { | ||
pub fn new( | ||
runtime: Arc<Runtime>, | ||
ingestor: Ingestor, | ||
js_path: String, | ||
) -> Result<Self, JsExtensionError> { | ||
let path = std::fs::canonicalize(js_path.clone()) | ||
.map_err(|e| JsExtensionError::CanonicalizePath(js_path, e))?; | ||
let module_specifier = | ||
ModuleSpecifier::from_file_path(path).expect("we just canonicalized it"); | ||
Ok(Self { | ||
runtime, | ||
ingestor, | ||
module_specifier, | ||
}) | ||
} | ||
|
||
pub async fn run(self) -> Result<(), BoxedError> { | ||
let runtime = self.runtime.clone(); | ||
runtime | ||
.spawn_blocking(move || { | ||
let local_set = LocalSet::new(); | ||
local_set.block_on(&self.runtime, async move { | ||
let mut worker = MainWorker::bootstrap_from_options( | ||
self.module_specifier.clone(), | ||
PermissionsContainer::allow_all(), | ||
WorkerOptions { | ||
module_loader: std::rc::Rc::new( | ||
ts_module_loader::TypescriptModuleLoader::with_no_source_map(), | ||
), | ||
extensions: vec![dozer_extension::init_ops(self.ingestor)], | ||
..Default::default() | ||
}, | ||
); | ||
|
||
worker.execute_main_module(&self.module_specifier).await?; | ||
worker.run_event_loop(false).await | ||
}) | ||
}) | ||
.await | ||
.unwrap() // Propagate panics. | ||
.map_err(Into::into) | ||
} | ||
} | ||
|
||
async fn send(ingestor: Ingestor, val: JsMessage) -> Result<(), Error> { | ||
let msg = match val.typ { | ||
MsgType::SnapshottingStarted => IngestionMessage::SnapshottingStarted, | ||
MsgType::SnapshottingDone => IngestionMessage::SnapshottingDone, | ||
MsgType::Insert | MsgType::Delete | MsgType::Update => { | ||
let op = map_operation(val)?; | ||
IngestionMessage::OperationEvent { | ||
table_index: 0, | ||
op, | ||
id: None, | ||
} | ||
} | ||
}; | ||
|
||
// Ignore if the receiver is closed. | ||
let _ = ingestor.handle_message(msg).await; | ||
Ok(()) | ||
} | ||
|
||
fn map_operation(msg: JsMessage) -> Result<Operation, DeserializationError> { | ||
Ok(match msg.typ { | ||
MsgType::Insert => Operation::Insert { | ||
new: Record { | ||
values: vec![Field::Json(serde_json_to_json_value(msg.new_val)?)], | ||
lifetime: None, | ||
}, | ||
}, | ||
MsgType::Delete => Operation::Delete { | ||
old: Record { | ||
values: vec![Field::Json(serde_json_to_json_value(msg.old_val)?)], | ||
lifetime: None, | ||
}, | ||
}, | ||
MsgType::Update => Operation::Update { | ||
old: Record { | ||
values: vec![Field::Json(serde_json_to_json_value(msg.old_val)?)], | ||
lifetime: None, | ||
}, | ||
new: Record { | ||
values: vec![Field::Json(serde_json_to_json_value(msg.new_val)?)], | ||
lifetime: None, | ||
}, | ||
}, | ||
_ => unreachable!(), | ||
}) | ||
} | ||
|
||
mod ts_module_loader; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::time::Duration; | ||
|
||
use camino::Utf8Path; | ||
use dozer_ingestion_connector::{test_util::create_test_runtime, IngestionConfig, Ingestor}; | ||
|
||
use super::JsExtension; | ||
|
||
#[test] | ||
fn test_deno() { | ||
let js_path = Utf8Path::new(env!("CARGO_MANIFEST_DIR")).join("./src/js_extension/ingest.js"); | ||
|
||
let (ingestor, mut iterator) = Ingestor::initialize_channel(IngestionConfig::default()); | ||
|
||
let runtime = create_test_runtime(); | ||
let ext = JsExtension::new(runtime.clone(), ingestor, js_path.into()).unwrap(); | ||
|
||
runtime.spawn(ext.run()); | ||
|
||
runtime.block_on(async move { | ||
let mut count = 0; | ||
loop { | ||
let msg = iterator.next_timeout(Duration::from_secs(5)).await.unwrap(); | ||
count += 1; | ||
println!("i: {:?}, msg: {:?}", count, msg); | ||
if count > 3 { | ||
break; | ||
} | ||
} | ||
}); | ||
} |
Oops, something went wrong.