Skip to content

Commit

Permalink
feat: Add registry API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed Jul 8, 2024
1 parent 9f69d51 commit 12b00b7
Show file tree
Hide file tree
Showing 6 changed files with 711 additions and 18 deletions.
42 changes: 25 additions & 17 deletions WasmerSDK.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
export * from "./pkg/wasmer_js";
// @ts-ignore
import load, { InitInput, InitOutput, ThreadPoolWorker, setWorkerUrl } from "./pkg/wasmer_js";
import load, { InitInput, InitOutput, ThreadPoolWorker, setWorkerUrl, RegistryConfig } from "./pkg/wasmer_js";

type RegistryInput = {
registry_url: string,
token?: string
}

/**
* Initialize the underlying WebAssembly module.
*/
export const init = async (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory): Promise<InitOutput> => {
if (!module_or_path) {
// This will be replaced by the rollup bundler at the SDK build time
// to point to a valid http location of the SDK using unpkg.com.
let wasmUrl = (globalThis as any)["wasmUrl"];
if (wasmUrl) {
module_or_path = new URL(wasmUrl);
}
}
return load(module_or_path, maybe_memory);
export const init = async (module_or_path?: InitInput | Promise<InitInput>, maybe_memory?: WebAssembly.Memory, maybe_registry?: RegistryInput): Promise<InitOutput> => {
if (!module_or_path) {
// This will be replaced by the rollup bundler at the SDK build time
// to point to a valid http location of the SDK using unpkg.com.
let wasmUrl = (globalThis as any)["wasmUrl"];
if (wasmUrl) {
module_or_path = new URL(wasmUrl);
}
}

(globalThis as any)["__WASMER_REGISTRY__"] = maybe_registry;

return load(module_or_path, maybe_memory);
}

/**
* Set a deafult working Worker Url. Which in this case will be
* an unpkg url that is set up at the SDK build time.
*/
export const setDefaultWorkerUrl = () => {
let workerUrl = (globalThis as any)["workerUrl"];
if (workerUrl) {
setWorkerUrl(workerUrl)
}
let workerUrl = (globalThis as any)["workerUrl"];
if (workerUrl) {
setWorkerUrl(workerUrl)
}
}

// HACK: We save these to the global scope because it's the most reliable way to
Expand All @@ -35,5 +43,5 @@ export const setDefaultWorkerUrl = () => {

// HACK: some bundlers such as webpack uses this on dev mode.
// We add this functions to allow dev mode work in those bundlers.
(globalThis as any).$RefreshReg$ = (globalThis as any).$RefreshReg$ || function () {/**/ };
(globalThis as any).$RefreshSig$ = (globalThis as any).$RefreshSig$ || function () { return function () { } };
(globalThis as any).$RefreshReg$ = (globalThis as any).$RefreshReg$ || function() {/**/ };
(globalThis as any).$RefreshSig$ = (globalThis as any).$RefreshSig$ || function() { return function() { } };
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import terser from "@rollup/plugin-terser";
import pkg from "./package.json" assert { type: "json" };
import pkg from "./package.json" with { type: "json" };
import dts from "rollup-plugin-dts";
import typescript from "@rollup/plugin-typescript";
import replace from "@rollup/plugin-replace";
Expand Down
76 changes: 76 additions & 0 deletions src/registry/app.rs
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)
}
}
73 changes: 73 additions & 0 deletions src/registry/mod.rs
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(
&registry_info,
&JsValue::from(String::from("registry_url")),
)
.ok()
.and_then(|u| u.as_string());
let token =
js_sys::Reflect::get(&registry_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(&registry_input.registry_url)?,
"Wasmer JS SDK",
)?;
if let Some(token) = registry_input.token {
client = client.with_auth_token(token);
}

Ok(client)
})
}
}
Loading

0 comments on commit 12b00b7

Please sign in to comment.