Skip to content

Commit

Permalink
helpers moved to fastn-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 22, 2024
1 parent 00ba15b commit cf205ee
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 132 deletions.
196 changes: 94 additions & 102 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ enum-iterator = "0.6"
enum-iterator-derive = "0.6"
fastn-ds.path = "fastn-ds"
fastn-update.path = "fastn-update"
fastn-wasm.path = "v0.5/fastn-wasm"
fastn-builtins.path = "fastn-builtins"
fastn-resolved = { path = "fastn-resolved" }
fastn-core.path = "fastn-core"
Expand Down Expand Up @@ -128,7 +129,7 @@ tracing = "0.1"
scc = "2"
url = "2"
walkdir = "2"
wasmtime = "27"
wasmtime = "28"
zip = "2"
prettify-js = "0.1.0"
indexmap = { version = "2", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions fastn-ds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ homepage.workspace = true

[dependencies]
fastn-utils.workspace = true
fastn-wasm.workspace = true
tokio.workspace = true
thiserror.workspace = true
actix-web.workspace = true
Expand Down
8 changes: 4 additions & 4 deletions fastn-ds/src/wasm/exports/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ pub async fn encrypt(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let input = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let input = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
let secret_key = std::env::var("FASTN_SECRET_KEY").unwrap();
let mc_obj = magic_crypt::new_magic_crypt!(secret_key, 256);
let o = mc_obj.encrypt_to_base64(input.as_str()).as_str().to_owned();
fastn_ds::wasm::helpers::send_bytes(&o.into_bytes(), &mut caller).await
fastn_wasm::helpers::send_bytes(&o.into_bytes(), &mut caller).await
}

pub async fn decrypt(
mut caller: wasmtime::Caller<'_, fastn_ds::wasm::Store>,
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let input = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let input = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
let secret_key = std::env::var("FASTN_SECRET_KEY").unwrap();
let mc_obj = magic_crypt::new_magic_crypt!(secret_key, 256);
let o = mc_obj
.decrypt_base64_to_string(input)
.map_err(|e| ft_sys_shared::DecryptionError::Generic(format!("{e:?}")));
fastn_ds::wasm::helpers::send_json(o, &mut caller).await
fastn_wasm::helpers::send_json(o, &mut caller).await
}
10 changes: 5 additions & 5 deletions fastn-ds/src/wasm/exports/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub async fn print(
) -> wasmtime::Result<()> {
println!(
"wasm: {}",
fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?
fastn_wasm::helpers::get_str(ptr, len, &mut caller)?
);

Ok(())
Expand All @@ -16,18 +16,18 @@ pub async fn var(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let key = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let key = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
let value = std::env::var(key).ok();

fastn_ds::wasm::helpers::send_json(value, &mut caller).await
fastn_wasm::helpers::send_json(value, &mut caller).await
}

pub async fn now(mut caller: wasmtime::Caller<'_, fastn_ds::wasm::Store>) -> wasmtime::Result<i32> {
fastn_ds::wasm::helpers::send_json(chrono::Utc::now(), &mut caller).await
fastn_wasm::helpers::send_json(chrono::Utc::now(), &mut caller).await
}

pub async fn random(
mut caller: wasmtime::Caller<'_, fastn_ds::wasm::Store>,
) -> wasmtime::Result<i32> {
fastn_ds::wasm::helpers::send_json(rand::random::<f64>(), &mut caller).await
fastn_wasm::helpers::send_json(rand::random::<f64>(), &mut caller).await
}
2 changes: 1 addition & 1 deletion fastn-ds/src/wasm/exports/http/get_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub async fn get_request(
mut caller: wasmtime::Caller<'_, fastn_ds::wasm::Store>,
) -> wasmtime::Result<i32> {
let req = caller.data().to_http();
fastn_ds::wasm::helpers::send_json(req, &mut caller).await
fastn_wasm::helpers::send_json(req, &mut caller).await
}

impl fastn_ds::wasm::Store {
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/http/send_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub async fn send_request(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let r: ft_sys_shared::Request = fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
let r: ft_sys_shared::Request = fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;

let mut headers = reqwest::header::HeaderMap::new();
for (header_name, header_value) in r.headers {
Expand All @@ -27,5 +27,5 @@ pub async fn send_request(
}
let response = response.body(reqwest_response.bytes().await?)?;

fastn_ds::wasm::helpers::send_json(ft_sys_shared::Request::from(response), &mut caller).await
fastn_wasm::helpers::send_json(ft_sys_shared::Request::from(response), &mut caller).await
}
2 changes: 1 addition & 1 deletion fastn-ds/src/wasm/exports/http/send_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub async fn send_response(
ptr: i32,
len: i32,
) -> wasmtime::Result<()> {
let r = fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
let r = fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;
caller.data_mut().store_response(r);
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/pg/batch_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub async fn batch_execute(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let q = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let q = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
let res = caller.data_mut().pg_batch_execute(conn, q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

impl fastn_ds::wasm::Store {
Expand Down
2 changes: 1 addition & 1 deletion fastn-ds/src/wasm/exports/pg/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub async fn connect(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let db_url = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let db_url = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
caller.data_mut().pg_connect(db_url.as_str()).await
}

Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/pg/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub async fn execute(
len: i32,
) -> wasmtime::Result<i32> {
let q: fastn_ds::wasm::exports::pg::Query =
fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;
let res = caller.data_mut().pg_execute(conn, q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

impl fastn_ds::wasm::Store {
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/pg/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub async fn query(
len: i32,
) -> wasmtime::Result<i32> {
let q: fastn_ds::wasm::exports::pg::Query =
fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;
let res = caller.data_mut().pg_query(conn, q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

#[derive(serde::Serialize, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/sqlite/batch_execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub async fn batch_execute(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let q = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let q = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
let res = caller.data_mut().sqlite_batch_execute(q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

impl fastn_ds::wasm::Store {
Expand Down
2 changes: 1 addition & 1 deletion fastn-ds/src/wasm/exports/sqlite/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub async fn connect(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let db_url = fastn_ds::wasm::helpers::get_str(ptr, len, &mut caller)?;
let db_url = fastn_wasm::helpers::get_str(ptr, len, &mut caller)?;
println!("sqlite_connect: {db_url}");
caller.data_mut().sqlite_connect(db_url.as_str()).await
}
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/sqlite/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub async fn execute(
len: i32,
) -> wasmtime::Result<i32> {
let q: fastn_ds::wasm::exports::sqlite::Query =
fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;
let res = caller.data_mut().sqlite_execute(q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

impl fastn_ds::wasm::Store {
Expand Down
4 changes: 2 additions & 2 deletions fastn-ds/src/wasm/exports/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub async fn query(
ptr: i32,
len: i32,
) -> wasmtime::Result<i32> {
let q: Query = fastn_ds::wasm::helpers::get_json(ptr, len, &mut caller)?;
let q: Query = fastn_wasm::helpers::get_json(ptr, len, &mut caller)?;
let res = caller.data_mut().sqlite_query(q).await?;
fastn_ds::wasm::helpers::send_json(res, &mut caller).await
fastn_wasm::helpers::send_json(res, &mut caller).await
}

#[derive(serde::Deserialize, Debug)]
Expand Down
1 change: 0 additions & 1 deletion fastn-ds/src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod exports;
pub mod helpers;
pub mod macros;
mod store;

Expand Down
4 changes: 4 additions & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions v0.5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"fastn-static",
"fastn-unresolved",
"fastn-update",
"fastn-wasm",
]
exclude = []
resolver = "2"
Expand Down Expand Up @@ -59,4 +60,5 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
string-interner = "0.18"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "fs"] }
wasmtime = "28"

14 changes: 14 additions & 0 deletions v0.5/fastn-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "fastn-wasm"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
description.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true

[dependencies]
wasmtime.workspace = true
serde_json.workspace = true
serde.workspace = true
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub async fn str<S: Send>(str: &str, caller: &mut wasmtime::Caller<'_, S>) -> wasmtime::Result<i32> {
pub async fn str<S: Send>(
str: &str,
caller: &mut wasmtime::Caller<'_, S>,
) -> wasmtime::Result<i32> {
send_bytes(str.as_bytes(), caller).await
}

Expand Down
7 changes: 7 additions & 0 deletions v0.5/fastn-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(clippy::derive_partial_eq_without_eq, clippy::get_first)]
#![deny(unused_crate_dependencies)]
#![warn(clippy::used_underscore_binding)]

extern crate self as fastn_wasm;

pub mod helpers;

0 comments on commit cf205ee

Please sign in to comment.