-
Notifications
You must be signed in to change notification settings - Fork 82
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
Showing
6 changed files
with
711 additions
and
18 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
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,76 @@ | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use wasmer_api::types::{DeployAppVersion, PublishDeployAppVars}; | ||
use wasmer_config::app::AppConfigV1; | ||
|
||
use crate::{ | ||
utils::{self, Error}, | ||
Wasmer, | ||
}; | ||
|
||
#[wasm_bindgen(getter_with_clone)] | ||
#[derive(Debug, Clone)] | ||
pub struct DeployedApp { | ||
pub id: String, | ||
pub created_at: String, | ||
pub version: String, | ||
pub description: Option<String>, | ||
pub yaml_config: String, | ||
pub user_yaml_config: String, | ||
pub config: String, | ||
pub json_config: String, | ||
pub url: String, | ||
} | ||
|
||
impl From<DeployAppVersion> for DeployedApp { | ||
fn from(value: DeployAppVersion) -> Self { | ||
Self { | ||
id: value.id.inner().to_string(), | ||
created_at: value.created_at.0, | ||
version: value.version, | ||
description: value.description, | ||
yaml_config: value.yaml_config, | ||
user_yaml_config: value.user_yaml_config, | ||
config: value.config, | ||
json_config: value.json_config, | ||
url: value.url, | ||
} | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl Wasmer { | ||
/// Deploy an app to the registry. | ||
#[wasm_bindgen(js_name = "deployApp")] | ||
#[allow(non_snake_case)] | ||
pub async fn deploy_app(appConfig: wasm_bindgen::JsValue) -> Result<DeployedApp, Error> { | ||
let default = js_sys::Reflect::get(&appConfig, &(String::from("default").into())) | ||
.map_err(utils::js_error)? | ||
.as_bool(); | ||
let app_config = serde_wasm_bindgen::from_value(appConfig) | ||
.map_err(|e| anyhow::anyhow!(e.to_string()))?; | ||
Wasmer::deploy_app_inner(app_config, default).await | ||
} | ||
} | ||
|
||
impl Wasmer { | ||
async fn deploy_app_inner( | ||
app_config: AppConfigV1, | ||
make_default: Option<bool>, | ||
) -> Result<DeployedApp, Error> { | ||
let client = Wasmer::get_client()?; | ||
let config = app_config.clone().to_yaml()?; | ||
|
||
wasmer_api::query::publish_deploy_app( | ||
client, | ||
PublishDeployAppVars { | ||
config, | ||
name: app_config.name.into(), | ||
owner: app_config.owner.map(Into::into), | ||
make_default, | ||
}, | ||
) | ||
.await | ||
.map(|v| v.into()) | ||
.map_err(utils::Error::Rust) | ||
} | ||
} |
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,73 @@ | ||
pub mod app; | ||
pub mod package; | ||
|
||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; | ||
use wasmer_api::WasmerClient; | ||
|
||
use crate::{utils::Error, Wasmer}; | ||
|
||
static WASMER_CLIENT: std::sync::OnceLock<WasmerClient> = std::sync::OnceLock::new(); | ||
|
||
#[wasm_bindgen(getter_with_clone)] | ||
pub struct RegistryConfig { | ||
pub registry_url: String, | ||
pub token: Option<String>, | ||
} | ||
|
||
impl Default for RegistryConfig { | ||
fn default() -> Self { | ||
Self { | ||
registry_url: String::from("https://registry.wasmer.io/graphql"), | ||
token: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Wasmer { | ||
pub fn get_client() -> Result<&'static WasmerClient, Error> { | ||
WASMER_CLIENT.get_or_try_init(|| { | ||
let registry_input = if let Some(registry_info) = | ||
web_sys::window().and_then(|w| w.get("__WASMER_REGISTRY__")) | ||
{ | ||
if registry_info.is_undefined() { | ||
RegistryConfig::default() | ||
} else { | ||
let registry_url = js_sys::Reflect::get( | ||
®istry_info, | ||
&JsValue::from(String::from("registry_url")), | ||
) | ||
.ok() | ||
.and_then(|u| u.as_string()); | ||
let token = | ||
js_sys::Reflect::get(®istry_info, &JsValue::from(String::from("token"))) | ||
.ok() | ||
.and_then(|u| u.as_string()); | ||
|
||
if let Some(registry_url) = registry_url { | ||
RegistryConfig { | ||
registry_url, | ||
token, | ||
} | ||
} else { | ||
RegistryConfig { | ||
token, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
} else { | ||
RegistryConfig::default() | ||
}; | ||
|
||
let mut client = wasmer_api::WasmerClient::new( | ||
url::Url::parse(®istry_input.registry_url)?, | ||
"Wasmer JS SDK", | ||
)?; | ||
if let Some(token) = registry_input.token { | ||
client = client.with_auth_token(token); | ||
} | ||
|
||
Ok(client) | ||
}) | ||
} | ||
} |
Oops, something went wrong.