-
Notifications
You must be signed in to change notification settings - Fork 293
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
Add email worker and send email support #624
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "email" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[package.metadata.release] | ||
release = false | ||
|
||
# https://github.com/rustwasm/wasm-pack/issues/1247 | ||
[package.metadata.wasm-pack.profile.release] | ||
wasm-opt = false | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
worker = { workspace=true } | ||
console_error_panic_hook = { version = "0.1.1" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use core::str; | ||
|
||
use worker::*; | ||
|
||
#[event(email)] | ||
async fn email(message: ForwardableEmailMessage, _env: Env, _ctx: Context) -> Result<()> { | ||
console_error_panic_hook::set_once(); | ||
|
||
let allow_list = ["[email protected]", "[email protected]"]; | ||
let from = message.from_envelope(); | ||
|
||
let raw: Vec<u8> = message.raw_bytes().await?; | ||
let raw = str::from_utf8(&raw)?; | ||
console_log!("Raw email: {}", raw); | ||
|
||
if allow_list.contains(&from.as_str()) { | ||
message.forward("[email protected]", None).await?; | ||
} else { | ||
message.set_reject("Address not allowed")?; | ||
} | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = "email-worker" | ||
main = "build/worker/shim.mjs" | ||
compatibility_date = "2024-08-13" | ||
|
||
[build] | ||
command = "cargo install -q worker-build && worker-build --release" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use js_sys::Promise; | ||
use wasm_bindgen::prelude::*; | ||
use web_sys::{Headers, ReadableStream}; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends=js_sys::Object)] | ||
#[derive(Clone, PartialEq, Eq)] | ||
pub type EmailMessage; | ||
|
||
// TODO(lduarte): see if also accepting string is needed | ||
#[wasm_bindgen(constructor, catch)] | ||
pub fn new(from: &str, to: &str, raw: &str) -> Result<EmailMessage, JsValue>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also have a |
||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn from(this: &EmailMessage) -> String; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn to(this: &EmailMessage) -> String; | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add |
||
#[wasm_bindgen(extends=js_sys::Object)] | ||
#[derive(Clone, PartialEq, Eq)] | ||
pub type ForwardableEmailMessage; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn from(this: &ForwardableEmailMessage) -> String; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn to(this: &ForwardableEmailMessage) -> String; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn raw(this: &ForwardableEmailMessage) -> ReadableStream; | ||
|
||
// File size will never pass over 4GB so u32 is enough | ||
#[wasm_bindgen(method, getter, js_name=rawSize)] | ||
pub fn raw_size(this: &ForwardableEmailMessage) -> u32; | ||
|
||
#[wasm_bindgen(method, catch, js_name=setReject)] | ||
pub fn set_reject(this: &ForwardableEmailMessage, reason: &str) -> Result<(), JsValue>; | ||
|
||
#[wasm_bindgen(method, catch)] | ||
pub fn forward( | ||
this: &ForwardableEmailMessage, | ||
rcpt_to: &str, | ||
headers: Headers, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field should probably be optional, or maybe have |
||
) -> Result<Promise, JsValue>; | ||
|
||
#[wasm_bindgen(method, catch)] | ||
pub fn reply(this: &ForwardableEmailMessage, email: EmailMessage) -> Result<Promise, JsValue>; | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends=js_sys::Object)] | ||
#[derive(Clone, PartialEq, Eq)] | ||
pub type SendEmail; | ||
|
||
#[wasm_bindgen(method, catch)] | ||
pub fn send(this: &SendEmail, email: EmailMessage) -> Result<Promise, JsValue>; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ObsidianMinor do you know why email worker wouldn't use the new interface? Will cron triggers have the same issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still haven't figured it out yet - for some reason
env
is still not probably propagated into the rust layer, and therefore I can't seem to use bindings with it 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its also quite difficult to debug because email workers can't be run locally 😅