-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I hate how much time supabase wasted, making me figure out that array is stringified before uploading: https://github.com/supabase/supabase-js/issues/780
- Loading branch information
Showing
13 changed files
with
381 additions
and
168 deletions.
There are no files selected for viewing
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,11 @@ | ||
#! /usr/bin/fish | ||
|
||
set -u CARGO_TARGET_DIR | ||
|
||
cargo build --bin worker --target wasm32-unknown-unknown | ||
wasm-bindgen --target no-modules --out-dir ./dist/worker target/wasm32-unknown-unknown/debug/worker.wasm | ||
|
||
cargo build --bin decrypt_attachment_worker --target wasm32-unknown-unknown | ||
wasm-bindgen --target no-modules --out-dir ./dist/decrypt_attachment_worker target/wasm32-unknown-unknown/debug/decrypt_attachment_worker.wasm | ||
|
||
wasm-pack build --target bundler --dev; |
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,9 @@ | ||
use moe::worker::DecryptAttachmentsWorker; | ||
|
||
use gloo::worker::Registrable; | ||
|
||
fn main() { | ||
console_error_panic_hook::set_once(); | ||
|
||
DecryptAttachmentsWorker::registrar().register(); | ||
} |
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,110 @@ | ||
use std::io::Read; | ||
use gloo::console::{console_dbg, log}; | ||
use gloo::worker::{HandlerId, Worker, WorkerBridge, WorkerScope}; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::{JsValue, UnwrapThrowExt}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use gloo::worker::Spawnable; | ||
use gloo::utils::format::JsValueSerdeExt; | ||
use crate::attachments::{AttachmentDecryptor, DecryptorError, MediaEncryptionInfo}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct WorkerInput { | ||
bytes: Vec<u8>, | ||
info: MediaEncryptionInfo, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct WorkerOutput { | ||
bytes: Vec<u8>, | ||
} | ||
|
||
pub struct DecryptAttachmentsWorker {} | ||
|
||
pub enum WorkerMessage {} | ||
|
||
impl Worker for DecryptAttachmentsWorker { | ||
type Message = WorkerMessage; | ||
|
||
type Input = WorkerInput; | ||
|
||
type Output = WorkerOutput; | ||
|
||
fn create(_scope: &WorkerScope<Self>) -> Self { | ||
Self {} | ||
} | ||
|
||
fn update(&mut self, _scope: &WorkerScope<Self>, msg: Self::Message) { | ||
match msg {} | ||
} | ||
|
||
fn received(&mut self, scope: &WorkerScope<Self>, input: Self::Input, who: HandlerId) { | ||
let WorkerInput { bytes, info } = input; | ||
let mut bytes = &bytes[..]; | ||
log!("LOG FROM WORKER -- dec", format!("{:#?}", info), format!("{:?}", bytes)); | ||
let mut decryptor = match AttachmentDecryptor::new(&mut bytes, info) { | ||
Ok(d) => d, | ||
Err(e) => { | ||
log!("Error creating decryptor", e.to_string()); | ||
return; | ||
} | ||
}; | ||
let mut decrypted_data = Vec::new(); | ||
match decryptor.read_to_end(&mut decrypted_data) { | ||
Ok(_) => {} | ||
Err(e) => { | ||
log!("Error decrypting attachment", e.to_string()); | ||
return; | ||
} | ||
} | ||
scope.respond(who, WorkerOutput { | ||
bytes: decrypted_data, | ||
}); | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct JsDecryptAttachmentsWorker { | ||
worker: WorkerBridge<DecryptAttachmentsWorker>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl JsDecryptAttachmentsWorker { | ||
#[wasm_bindgen(js_name = "decryptAttachment")] | ||
pub async fn decrypt_attachment( | ||
&self, | ||
blob: web_sys::Blob, | ||
info: JsValue, | ||
) { | ||
log!("Decrypting attachment"); | ||
let file = gloo::file::Blob::from(blob); | ||
let bytes = gloo::file::futures::read_as_bytes(&file).await.unwrap_throw(); | ||
let info: MediaEncryptionInfo = info.into_serde().unwrap_throw(); | ||
let input = WorkerInput { | ||
bytes, | ||
info, | ||
}; | ||
self.worker.send(input); | ||
} | ||
} | ||
|
||
#[wasm_bindgen(js_name = "newDecryptAttachmentsWorker")] | ||
pub fn new(cb: js_sys::Function) -> JsDecryptAttachmentsWorker { | ||
|
||
console_error_panic_hook::set_once(); | ||
|
||
let bridge = DecryptAttachmentsWorker::spawner() | ||
.callback(move |m| { | ||
let bytes = m.bytes; | ||
let m = js_sys::Uint8Array::from(bytes.as_slice()); | ||
let ret = cb.call1(&JsValue::NULL, &m); | ||
if let Err(e) = ret { | ||
log!("Error calling callback", e); | ||
} | ||
}) | ||
.spawn("/crates/moe/dist/decrypt_attachment_worker/decrypt_attachment_worker.js"); | ||
|
||
JsDecryptAttachmentsWorker { | ||
worker: bridge, | ||
} | ||
} |
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,162 @@ | ||
use std::io::Read; | ||
use gloo::console::log; | ||
use gloo::worker::{HandlerId, Worker, WorkerBridge, WorkerScope}; | ||
use gloo::utils::format::JsValueSerdeExt; | ||
use js_sys::{JsString, Uint8Array}; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::{JsValue, UnwrapThrowExt}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use crate::worker::EncryptedFile; | ||
use gloo::worker::Spawnable; | ||
|
||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct FileToEncrypt { | ||
bytes: Vec<u8>, | ||
name: String, | ||
type_: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct WorkerInput { | ||
ciphertext: String, | ||
files: Vec<FileToEncrypt>, | ||
room_id: String, | ||
uid: String, | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct WorkerOutput { | ||
uid: String, | ||
files: Vec<EncryptedFile>, | ||
room_id: String, | ||
ciphertext: String, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WorkerOutput { | ||
#[wasm_bindgen(getter)] | ||
pub fn uid(&self) -> JsString { | ||
JsString::from(self.uid.as_str()) | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn files(&self) -> js_sys::Array { | ||
js_sys::Array::from_iter(self.files.iter().map(|it| { | ||
JsValue::from_serde(it).unwrap_throw() | ||
})) | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn room_id(&self) -> JsString { | ||
JsString::from(self.room_id.as_str()) | ||
} | ||
#[wasm_bindgen(getter)] | ||
pub fn ciphertext(&self) -> JsString { | ||
JsString::from(self.ciphertext.as_str()) | ||
} | ||
} | ||
|
||
pub struct EncryptedMessagesWorker {} | ||
|
||
pub enum WorkerMessage {} | ||
|
||
impl Worker for EncryptedMessagesWorker { | ||
type Message = WorkerMessage; | ||
|
||
type Input = WorkerInput; | ||
|
||
type Output = WorkerOutput; | ||
|
||
fn create(_scope: &WorkerScope<Self>) -> Self { | ||
Self {} | ||
} | ||
|
||
fn update(&mut self, _scope: &WorkerScope<Self>, msg: Self::Message) { | ||
match msg {} | ||
} | ||
|
||
fn received(&mut self, scope: &WorkerScope<Self>, input: Self::Input, who: HandlerId) { | ||
let WorkerInput { | ||
ciphertext, | ||
files, | ||
room_id, | ||
uid, | ||
} = input; | ||
|
||
let files = files.into_iter().map(|FileToEncrypt { bytes, name, type_ }| { | ||
let mut bytes = &bytes[..]; | ||
let mut encryptor = crate::attachments::AttachmentEncryptor::new(&mut bytes); | ||
let mut encrypted = Vec::new(); | ||
encryptor.read_to_end(&mut encrypted).unwrap(); | ||
let key = encryptor.finish(); | ||
log!("LOG FROM WORKER -- enc", format!("{:#?}", key), format!("{:?}", encrypted)); | ||
EncryptedFile { bytes: encrypted, key, name, type_ } | ||
}); | ||
scope.respond(who, WorkerOutput { | ||
ciphertext, | ||
files: files.collect(), | ||
room_id, | ||
uid, | ||
}) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct JsWorker { | ||
worker: WorkerBridge<EncryptedMessagesWorker>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl JsWorker { | ||
#[wasm_bindgen(js_name = "newMessage")] | ||
pub async fn new_message( | ||
&self, | ||
outbound_session: &mut crate::OutboundSession, | ||
room_id: String, | ||
uid: String, | ||
content: &str, | ||
files: Vec<web_sys::File>, | ||
) { | ||
let mut new_files = Vec::with_capacity(files.len()); | ||
for file in files { | ||
let name = file.name(); | ||
let type_ = file.type_(); | ||
let bytes = { | ||
let file = gloo::file::File::from(file); | ||
gloo::file::futures::read_as_bytes(&file).await.unwrap_throw() | ||
}; | ||
new_files.push(FileToEncrypt { bytes, name, type_ }); | ||
} | ||
|
||
let ciphertext = outbound_session.encrypt(&content); | ||
|
||
let input = WorkerInput { | ||
ciphertext, | ||
files: new_files, | ||
room_id, | ||
uid, | ||
}; | ||
self.worker.send(input); | ||
} | ||
} | ||
|
||
#[wasm_bindgen(js_name = "initAttachmentsWorker")] | ||
pub fn worker_init(cb: js_sys::Function) -> JsWorker { | ||
console_error_panic_hook::set_once(); | ||
|
||
let bridge = EncryptedMessagesWorker::spawner() | ||
.callback(move |m| { | ||
let m = JsValue::from(m); | ||
let ret = cb.call1(&JsValue::NULL, &m); | ||
if let Err(e) = ret { | ||
log!("Error calling callback", e); | ||
} | ||
}) | ||
.spawn("/crates/moe/dist/worker/worker.js"); | ||
|
||
JsWorker { | ||
worker: bridge, | ||
} | ||
} |
Oops, something went wrong.