Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Bridge: js-module sender input #1004

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion bridge/svix-bridge/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::path::PathBuf;
use svix_bridge_plugin_queue::config::{
into_receiver_output, QueueConsumerConfig, ReceiverOutputOpts as QueueOutOpts,
};
use svix_bridge_types::{ReceiverInputOpts, ReceiverOutput, SenderInput, TransformationConfig};
use svix_bridge_types::{
ReceiverInputOpts, ReceiverOutput, SenderInput, SenderOutputOpts, TransformationConfig,
};
use tracing::Level;

#[derive(Deserialize)]
Expand Down Expand Up @@ -104,6 +107,7 @@ pub enum SenderConfig {
feature = "sqs"
))]
QueueConsumer(QueueConsumerConfig),
JsModule(JsModuleSenderConfig),
}

impl TryFrom<SenderConfig> for Box<dyn SenderInput> {
Expand All @@ -117,6 +121,7 @@ impl TryFrom<SenderConfig> for Box<dyn SenderInput> {
feature = "sqs"
))]
SenderConfig::QueueConsumer(backend) => backend.into_sender_input(),
SenderConfig::JsModule(inner) => inner.into_sender_input(),
}
}
}
Expand Down Expand Up @@ -154,5 +159,29 @@ impl ReceiverConfig {
}
}

#[derive(Deserialize)]
pub struct JsModuleSenderConfig {
pub name: String,
pub input: JsModuleSenderInputOpts,
#[serde(default)]
pub transformation: Option<TransformationConfig>,
pub output: SenderOutputOpts,
}

impl JsModuleSenderConfig {
fn into_sender_input(self) -> Result<Box<dyn SenderInput>, &'static str> {
// FIXME: need to make it so we can use latest deno for transformations before we can
// connect the new module code.
todo!()
}
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum JsModuleSenderInputOpts {
#[serde(rename = "js-module")]
JsModule { module_path: PathBuf },
}

#[cfg(test)]
mod tests;
45 changes: 44 additions & 1 deletion bridge/svix-bridge/src/config/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::Config;
use crate::config::{LogFormat, LogLevel, SenderConfig};
use crate::config::{
JsModuleSenderConfig, JsModuleSenderInputOpts, LogFormat, LogLevel, SenderConfig,
};
use std::collections::HashMap;
use std::path::PathBuf;
use svix_bridge_plugin_queue::config::{QueueConsumerConfig, RabbitMqInputOpts, SenderInputOpts};
use svix_bridge_types::{SenderOutputOpts, SvixSenderOutputOpts};

Expand Down Expand Up @@ -485,3 +488,43 @@ fn test_variable_substitution_repeated_lookups() {
panic!("sender did not match expected pattern");
}
}

#[test]
fn test_js_module_sender_input_ok() {
let src = r#"
senders:
- name: "js-module-example"
input:
type: "js-module"
# FIXME: custom module loader needed to use yaml keys for src
module_path: "./my-module.js"
transformation: |
function handler(input) {
return {
appId: "xxxxx",
message: {
eventType: "lipsum.word-lengths.changed",
payload: { lengths: input.lengths }
}
};
}
output:
type: "svix"
token: "x"
"#;
let cfg = Config::from_src(src, Some(HashMap::new()).as_ref()).unwrap();

if let SenderConfig::JsModule(JsModuleSenderConfig {
input: JsModuleSenderInputOpts::JsModule { module_path, .. },
transformation,
output: SenderOutputOpts::Svix(SvixSenderOutputOpts { token, .. }),
..
}) = &cfg.senders[0]
{
assert_eq!(module_path, &PathBuf::from("./my-module.js"));
assert!(transformation.is_some());
assert_eq!(token, "x");
} else {
panic!("sender did not match expected pattern");
}
}