Get rid of yew completely

This commit is contained in:
Edward Shen 2021-10-24 12:14:55 -07:00
parent 95e15b81b7
commit 23b90ebfe4
Signed by: edward
GPG key ID: 19182661E818369F
4 changed files with 218 additions and 515 deletions

217
Cargo.lock generated
View file

@ -47,18 +47,6 @@ version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1"
[[package]]
name = "anymap"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344"
[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.51" version = "0.1.51"
@ -176,12 +164,6 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "boolinator"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfa8873f51c92e232f9bac4065cddef41b714152812bfc5f7672ba16d6ef8cd9"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.7.1" version = "3.7.1"
@ -248,12 +230,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cfg-match"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8100e46ff92eb85bf6dc2930c73f2a4f7176393c84a9446b3d501e1b354e7b34"
[[package]] [[package]]
name = "chacha20" name = "chacha20"
version = "0.8.1" version = "0.8.1"
@ -350,16 +326,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.1" version = "0.2.1"
@ -617,18 +583,6 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "gloo"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ce6f2dfa9f57f15b848efa2aade5e1850dc72986b87a2b0752d44ca08f4967"
dependencies = [
"gloo-console-timer",
"gloo-events",
"gloo-file",
"gloo-timers",
]
[[package]] [[package]]
name = "gloo-console" name = "gloo-console"
version = "0.1.0" version = "0.1.0"
@ -641,48 +595,6 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "gloo-console-timer"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b48675544b29ac03402c6dffc31a912f716e38d19f7e74b78b7e900ec3c941ea"
dependencies = [
"web-sys",
]
[[package]]
name = "gloo-events"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "088514ec8ef284891c762c88a66b639b3a730134714692ee31829765c5bc814f"
dependencies = [
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "gloo-file"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f9fecfe46b5dc3cc46f58e98ba580cc714f2c93860796d002eb3527a465ef49"
dependencies = [
"gloo-events",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "gloo-timers"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f"
dependencies = [
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.6" version = "0.3.6"
@ -915,19 +827,6 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "lexical-core"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe"
dependencies = [
"arrayvec",
"bitflags",
"cfg-if",
"ryu",
"static_assertions",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.103" version = "0.2.103"
@ -1048,17 +947,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "nom"
version = "5.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
dependencies = [
"lexical-core",
"memchr",
"version_check",
]
[[package]] [[package]]
name = "nom" name = "nom"
version = "6.1.2" version = "6.1.2"
@ -1207,9 +1095,6 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
"yew",
"yew-router",
"yewtil",
] ]
[[package]] [[package]]
@ -1711,12 +1596,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -2286,102 +2165,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214"
[[package]]
name = "yew"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4d5154faef86dddd2eb333d4755ea5643787d20aca683e58759b0e53351409f"
dependencies = [
"anyhow",
"anymap",
"bincode",
"cfg-if",
"cfg-match",
"console_error_panic_hook",
"gloo",
"http",
"indexmap",
"js-sys",
"log",
"ryu",
"serde",
"serde_json",
"slab",
"thiserror",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"yew-macro",
]
[[package]]
name = "yew-macro"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6e23bfe3dc3933fbe9592d149c9985f3047d08c637a884b9344c21e56e092ef"
dependencies = [
"boolinator",
"lazy_static",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "yew-router"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27666236d9597eac9be560e841e415e20ba67020bc8cd081076be178e159c8bc"
dependencies = [
"cfg-if",
"cfg-match",
"gloo",
"js-sys",
"log",
"nom 5.1.2",
"serde",
"serde_json",
"wasm-bindgen",
"web-sys",
"yew",
"yew-router-macro",
"yew-router-route-parser",
]
[[package]]
name = "yew-router-macro"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c0ace2924b7a175e2d1c0e62ee7022a5ad840040dcd52414ce5f410ab322dba"
dependencies = [
"proc-macro2",
"quote",
"syn",
"yew-router-route-parser",
]
[[package]]
name = "yew-router-route-parser"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de4a67208fb46b900af18a7397938b01f379dfc18da34799cfa8347eec715697"
dependencies = [
"nom 5.1.2",
]
[[package]]
name = "yewtil"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8543663ac49cd613df079282a1d8bdbdebdad6e02bac229f870fd4237b5d9aaa"
dependencies = [
"log",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"yew",
]
[[package]] [[package]]
name = "zeroize" name = "zeroize"
version = "1.4.2" version = "1.4.2"

View file

@ -20,9 +20,6 @@ reqwasm = "0.2"
tree_magic_mini = { version = "3", features = ["with-gpl-data"] } tree_magic_mini = { version = "3", features = ["with-gpl-data"] }
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
yew = { version = "0.18", features = ["wasm-bindgen-futures"] }
yew-router = "0.15"
yewtil = "0.4"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3" version = "0.3"
@ -38,5 +35,7 @@ features = [
"IdbIndex", "IdbIndex",
"IdbIndexParameters", "IdbIndexParameters",
"Event", "Event",
"EventTarget" "EventTarget",
"Window",
"Location",
] ]

View file

@ -35,28 +35,28 @@ pub fn decrypt(
.map_err(|_| PasteCompleteConstructionError::StageTwoFailure)?; .map_err(|_| PasteCompleteConstructionError::StageTwoFailure)?;
log!("stage 2 decryption end"); log!("stage 2 decryption end");
if let Ok(decrypted) = std::str::from_utf8(&container) { if let Ok(decrypted) = std::str::from_utf8(container) {
Ok(DecryptedData::String(Arc::new(decrypted.to_owned()))) Ok(DecryptedData::String(Arc::new(decrypted.to_owned())))
} else { } else {
log!("blob conversion start"); log!("blob conversion start");
let blob_chunks = Array::new_with_length(container.chunks(65536).len().try_into().unwrap()); let blob_chunks = Array::new_with_length(container.chunks(65536).len().try_into().unwrap());
for (i, chunk) in container.chunks(65536).enumerate() { for (i, chunk) in container.chunks(65536).enumerate() {
let array = Uint8Array::new_with_length(chunk.len().try_into().unwrap()); let array = Uint8Array::new_with_length(chunk.len().try_into().unwrap());
array.copy_from(&chunk); array.copy_from(chunk);
blob_chunks.set(i.try_into().unwrap(), array.dyn_into().unwrap()); blob_chunks.set(i.try_into().unwrap(), array.dyn_into().unwrap());
} }
let blob = let blob =
Arc::new(Blob::new_with_u8_array_sequence(blob_chunks.dyn_ref().unwrap()).unwrap()); Arc::new(Blob::new_with_u8_array_sequence(blob_chunks.dyn_ref().unwrap()).unwrap());
log!("blob conversion end"); log!("blob conversion end");
if let Ok(image) = image::load_from_memory(&container) { if let Ok(image) = image::load_from_memory(container) {
Ok(DecryptedData::Image( Ok(DecryptedData::Image(
blob, blob,
image.dimensions(), image.dimensions(),
container.len(), container.len(),
)) ))
} else { } else {
let mime_type = tree_magic_mini::from_u8(&container); let mime_type = tree_magic_mini::from_u8(container);
log!(mime_type); log!(mime_type);
if mime_type.starts_with("audio/") { if mime_type.starts_with("audio/") {
Ok(DecryptedData::Audio(blob)) Ok(DecryptedData::Audio(blob))

View file

@ -1,8 +1,6 @@
#![warn(clippy::nursery, clippy::pedantic)] #![warn(clippy::nursery, clippy::pedantic)]
use std::fmt::Debug;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc;
use byte_unit::Byte; use byte_unit::Byte;
use decrypt::DecryptedData; use decrypt::DecryptedData;
@ -15,87 +13,22 @@ use omegaupload_common::{Expiration, PartialParsedUrl};
use reqwasm::http::Request; use reqwasm::http::Request;
use wasm_bindgen::prelude::{wasm_bindgen, Closure}; use wasm_bindgen::prelude::{wasm_bindgen, Closure};
use wasm_bindgen::{JsCast, JsValue}; use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture; use wasm_bindgen_futures::{spawn_local, JsFuture};
use web_sys::{Blob, Event, IdbDatabase, IdbObjectStore, IdbOpenDbRequest, IdbTransactionMode}; use web_sys::{window, Event, IdbDatabase, IdbObjectStore, IdbOpenDbRequest, IdbTransactionMode};
use yew::utils::window;
use yew::{html, Component, ComponentLink, Html, ShouldRender};
use yew_router::router::Router;
use yew_router::Switch;
use yewtil::future::LinkFuture;
use crate::decrypt::decrypt; use crate::decrypt::decrypt;
mod decrypt; mod decrypt;
fn main() {
yew::start_app::<App>();
}
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
fn loadFromDb(); fn loadFromDb();
fn createNotFoundUi(); fn createNotFoundUi();
} }
struct App; fn main() {
impl Component for App { let window = window().unwrap();
type Message = (); let url = String::from(window.location().to_string());
type Properties = ();
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
Self
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
<Router<Route> render={Router::render(render_route)} />
}
}
}
#[derive(Clone, Debug, Switch)]
enum Route {
#[to = "/!"]
Index,
#[rest]
Path(String),
}
#[allow(clippy::needless_pass_by_value)]
fn render_route(route: Route) -> Html {
match route {
Route::Index => html! {
<main>
<p>{ "Hello world" }</p>
</main>
},
Route::Path(_) => html! {
<>
<Paste/>
<section class="hljs fullscreen centered">
<p>{"Loading"}</p>
</section>
</>
},
}
}
pub struct Paste;
impl Component for Paste {
type Message = ();
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
let url = String::from(window().location().to_string());
let request_uri = { let request_uri = {
let mut uri_parts = url.parse::<Uri>().unwrap().into_parts(); let mut uri_parts = url.parse::<Uri>().unwrap().into_parts();
if let Some(parts) = uri_parts.path_and_query.as_mut() { if let Some(parts) = uri_parts.path_and_query.as_mut() {
@ -104,7 +37,14 @@ impl Component for Paste {
Uri::from_parts(uri_parts).unwrap() Uri::from_parts(uri_parts).unwrap()
}; };
link.send_future(async move { if window.location().pathname().unwrap() == "/" {
} else {
spawn_local(a(request_uri, url));
}
}
#[allow(clippy::future_not_send)]
async fn a(request_uri: Uri, url: String) {
match Request::get(&request_uri.to_string()).send().await { match Request::get(&request_uri.to_string()).send().await {
Ok(resp) if resp.status() == StatusCode::OK => { Ok(resp) if resp.status() == StatusCode::OK => {
let expires = resp let expires = resp
@ -115,8 +55,10 @@ impl Component for Paste {
.as_deref() .as_deref()
.and_then(|v| Expiration::try_from(v).ok()) .and_then(|v| Expiration::try_from(v).ok())
.as_ref() .as_ref()
.map(Expiration::to_string) .map_or_else(
.unwrap_or_else(|| "This item does not expire.".to_string()); || "This item does not expire.".to_string(),
Expiration::to_string,
);
let data = { let data = {
Uint8Array::new( Uint8Array::new(
@ -146,6 +88,7 @@ impl Component for Paste {
}; };
let db_open_req = window() let db_open_req = window()
.unwrap()
.indexed_db() .indexed_db()
.unwrap() .unwrap()
.unwrap() .unwrap()
@ -157,10 +100,7 @@ impl Component for Paste {
let target: IdbOpenDbRequest = event.target().unwrap().dyn_into().unwrap(); let target: IdbOpenDbRequest = event.target().unwrap().dyn_into().unwrap();
let db: IdbDatabase = target.result().unwrap().dyn_into().unwrap(); let db: IdbDatabase = target.result().unwrap().dyn_into().unwrap();
let transaction: IdbObjectStore = db let transaction: IdbObjectStore = db
.transaction_with_str_and_mode( .transaction_with_str_and_mode("decrypted data", IdbTransactionMode::Readwrite)
"decrypted data",
IdbTransactionMode::Readwrite,
)
.unwrap() .unwrap()
.object_store("decrypted data") .object_store("decrypted data")
.unwrap(); .unwrap();
@ -170,7 +110,7 @@ impl Component for Paste {
DecryptedData::String(s) => { DecryptedData::String(s) => {
let entry = Array::new(); let entry = Array::new();
entry.push(&JsString::from("data")); entry.push(&JsString::from("data"));
entry.push(&JsValue::from_str(&s)); entry.push(&JsValue::from_str(s));
decrypted_object.push(&entry); decrypted_object.push(&entry);
let entry = Array::new(); let entry = Array::new();
@ -273,21 +213,19 @@ impl Component for Paste {
transaction transaction
.put_with_key( .put_with_key(
&db_entry, &db_entry,
&JsString::from(window().location().pathname().unwrap()), &JsString::from(window().unwrap().location().pathname().unwrap()),
) )
.unwrap() .unwrap()
.set_onsuccess(Some( .set_onsuccess(Some(
Closure::once(Box::new(|| { Closure::once(Box::new(|| {
log!("success"); log!("success");
loadFromDb(); loadFromDb();
}) }) as Box<dyn FnOnce()>)
as Box<dyn FnOnce()>)
.into_js_value() .into_js_value()
.dyn_ref() .dyn_ref()
.unwrap(), .unwrap(),
)); ));
}) }) as Box<dyn FnOnce(Event)>);
as Box<dyn FnOnce(Event)>);
db_open_req.set_onsuccess(Some(on_success.into_js_value().dyn_ref().unwrap())); db_open_req.set_onsuccess(Some(on_success.into_js_value().dyn_ref().unwrap()));
@ -298,8 +236,7 @@ impl Component for Paste {
let _obj_store = db.create_object_store("decrypted data").unwrap(); let _obj_store = db.create_object_store("decrypted data").unwrap();
}) as Box<dyn FnMut(Event)>); }) as Box<dyn FnMut(Event)>);
db_open_req db_open_req.set_onupgradeneeded(Some(on_upgrade.into_js_value().dyn_ref().unwrap()));
.set_onupgradeneeded(Some(on_upgrade.into_js_value().dyn_ref().unwrap()));
} }
Ok(resp) if resp.status() == StatusCode::NOT_FOUND => { Ok(resp) if resp.status() == StatusCode::NOT_FOUND => {
createNotFoundUi(); createNotFoundUi();
@ -308,20 +245,4 @@ impl Component for Paste {
Ok(err) => {} Ok(err) => {}
Err(err) => {} Err(err) => {}
}; };
});
Self
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
false
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {}
}
} }