-
Notifications
You must be signed in to change notification settings - Fork 279
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
1 parent
6a5bfc1
commit 15deda7
Showing
11 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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" } |
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,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 = vec!["another@example.com", "coworker@example.com"]; | ||
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("mailbox@anotherexample.com", None).await?; | ||
} else { | ||
message.set_reject("Address not allowed")?; | ||
} | ||
Ok(()) | ||
} |
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,6 @@ | ||
name = "email-worker" | ||
main = "build/worker/shim.mjs" | ||
compatibility_date = "2024-08-13" | ||
|
||
[build] | ||
command = "cargo install -q worker-build && worker-build --release" |
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,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>; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn from(this: &EmailMessage) -> String; | ||
|
||
#[wasm_bindgen(method, getter)] | ||
pub fn to(this: &EmailMessage) -> String; | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[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, | ||
) -> 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>; | ||
|
||
} |
Oops, something went wrong.