Skip to content

Commit

Permalink
feat: separate core and core_wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdward162 committed Mar 20, 2024
1 parent 4269834 commit 5dd2107
Show file tree
Hide file tree
Showing 23 changed files with 683 additions and 833 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ ${CORE_DIST}: ${WASI_SDK_FOLDER} ${CORE_JS_ASSETS_MAP_STD} ${CORE_JS_ASSETS_PROF
touch ${CORE_DIST}

${CORE_WASM}: ${CORE_DIST}
cd core; cargo build --package oneclient_core --target wasm32-wasi ${CARGO_FLAGS}
cd core; cargo build --package oneclient_core_wasm --target wasm32-wasi ${CARGO_FLAGS}
@echo 'Optimizing wasm...'
wasm-opt -Oz ${WASM_OPT_FLAGS} core/target/wasm32-wasi/${CARGO_PROFILE}/oneclient_core.wasm --output ${CORE_WASM}
wasm-opt -Oz ${WASM_OPT_FLAGS} core/target/wasm32-wasi/${CARGO_PROFILE}/oneclient_core_wasm.wasm --output ${CORE_WASM}

${TEST_CORE_WASM}: ${CORE_DIST}
cd core; cargo build --package oneclient_core --target wasm32-wasi --features "core_mock" ${CARGO_FLAGS}
cp core/target/wasm32-wasi/${CARGO_PROFILE}/oneclient_core.wasm ${TEST_CORE_WASM}
cd core; cargo build --package oneclient_core_wasm --target wasm32-wasi --features "core_mock" ${CARGO_FLAGS}
cp core/target/wasm32-wasi/${CARGO_PROFILE}/oneclient_core_wasm.wasm ${TEST_CORE_WASM}

${CORE_ASYNCIFY_WASM}: ${CORE_WASM}
@echo 'Running asyncify...'
Expand Down
9 changes: 9 additions & 0 deletions core/Cargo.lock

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

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["comlink", "comlink_wasm", "core", "core_to_map_std", "host_to_core_std", "interpreter_js", "json_schemas", "wasm_abi"]
members = ["comlink", "comlink_wasm", "core", "core_wasm", "core_to_map_std", "host_to_core_std", "interpreter_js", "json_schemas", "wasm_abi"]

[workspace.dependencies]
base64 = { version = "0.21" }
Expand Down
5 changes: 0 additions & 5 deletions core/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ name = "oneclient_core"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[features]
default = ["asyncify"]
asyncify = []
core_mock = []

[dependencies]
Expand Down
46 changes: 29 additions & 17 deletions core/core/src/sf_core/cache.rs → core/core/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::{
collections::HashMap,
io::Read,
time::{Duration, Instant},
time::{Duration, Instant}, ops::Deref,
};

use url::Url;

use sf_std::{
unstable::{http::HttpCallError, provider::ProviderJson},
HeaderName, HeadersMultiMap,
unstable::{http::{HttpCallError, HttpRequest}, provider::ProviderJson, fs::read_in},
HeaderName, HeadersMultiMap, abi::{MessageExchange, StreamExchange},
};

use super::{digest, Fs, HttpRequest};
use super::digest;

#[derive(Debug, thiserror::Error)]
pub enum ProfileCacheEntryError {
Expand Down Expand Up @@ -104,29 +104,39 @@ impl<E: std::fmt::Debug> std::fmt::Debug for DocumentCacheEntry<E> {
}
}

pub struct DocumentCache<E> {
pub struct DocumentCache<E, Me: MessageExchange, Se: StreamExchange> {
message_exchange: Me,
stream_exchange: Se,
map: HashMap<String, DocumentCacheEntry<E>>,
cache_duration: Duration,
registry_url: Url,
user_agent: Option<String>,
}
impl<E> DocumentCache<E> {
impl<E, Me: MessageExchange, Se: StreamExchange> DocumentCache<E, Me, Se> {
const FILE_URL_PREFIX: &'static str = "file://";
const HTTP_URL_PREFIX: &'static str = "http://";
const HTTPS_URL_PREFIX: &'static str = "https://";
const BASE64_URL_PREFIX: &'static str = "data:;base64,";

pub fn new(cache_duration: Duration, registry_url: Url, user_agent: Option<String>) -> Self {
pub fn new(
cache_duration: Duration,
registry_url: Url,
user_agent: Option<String>,
message_exchange: Me,
stream_exchange: Se
) -> Self {
Self {
message_exchange,
stream_exchange,
map: HashMap::new(),
cache_duration,
registry_url,
user_agent,
}
}

pub fn get(&self, url: &str) -> Option<&E> {
self.map.get(url).map(|e| &e.data)
pub fn get_key_value(&self, url: &str) -> Option<(&str, &E)> {
self.map.get_key_value(url).map(|(k, e)| (k.deref(), &e.data))
}

pub fn cache<PostProcessError: std::error::Error>(
Expand All @@ -149,12 +159,12 @@ impl<E> DocumentCache<E> {
}

let data = match url {
url if url.starts_with(Self::FILE_URL_PREFIX) => Self::cache_file(url),
url if url.starts_with(Self::FILE_URL_PREFIX) => self.cache_file(url),
url if url.starts_with("data:;base64,") => Self::cache_base64(url),
url => {
if url.starts_with(Self::HTTP_URL_PREFIX) || url.starts_with(Self::HTTPS_URL_PREFIX)
{
Self::cache_http(url, self.user_agent.as_deref())
self.cache_http(url, self.user_agent.as_deref())
} else {
let file = format!("{}.js", url);
let full_url = self.registry_url.join(&file).map_err(|_e| {
Expand All @@ -164,7 +174,7 @@ impl<E> DocumentCache<E> {
)
})?;

Self::cache_http(full_url.as_str(), self.user_agent.as_deref())
self.cache_http(full_url.as_str(), self.user_agent.as_deref())
}
}
}?;
Expand All @@ -187,28 +197,30 @@ impl<E> DocumentCache<E> {
}

fn cache_file<PostProcessError: std::error::Error>(
url: &str,
&self,
url: &str
) -> Result<Vec<u8>, DocumentCacheError<PostProcessError>> {
match url.strip_prefix(Self::FILE_URL_PREFIX) {
None => Err(DocumentCacheError::FileLoadFailed(
url.to_string(),
std::io::ErrorKind::NotFound.into(),
)),
Some(path) => Fs::read(path)
Some(path) => read_in(path, &self.message_exchange, &self.stream_exchange)
.map_err(|err| DocumentCacheError::FileLoadFailed(path.to_string(), err)),
}
}

fn cache_http<PostProcessError: std::error::Error>(
&self,
url: &str,
user_agent: Option<&str>,
user_agent: Option<&str>
) -> Result<Vec<u8>, DocumentCacheError<PostProcessError>> {
let mut headers = HeadersMultiMap::new();
if let Some(user_agent) = user_agent {
headers.insert(HeaderName::from("user-agent"), vec![user_agent.to_string()]);
}

let mut response = HttpRequest::fetch("GET", url, &headers, &Default::default(), None)
let mut response = HttpRequest::fetch_in("GET", url, &headers, &Default::default(), None, &self.message_exchange, &self.stream_exchange)
.and_then(|v| v.into_response())
.map_err(|err| DocumentCacheError::HttpLoadFailed(url.to_string(), err))?;

Expand All @@ -235,7 +247,7 @@ impl<E> DocumentCache<E> {
}
}
}
impl<E: std::fmt::Debug> std::fmt::Debug for DocumentCache<E> {
impl<E: std::fmt::Debug, Me: MessageExchange, Se: StreamExchange> std::fmt::Debug for DocumentCache<E, Me, Se> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DocumentCache")
.field("map", &self.map)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5dd2107

Please sign in to comment.