Skip to content

Commit

Permalink
Made the registry URL overridable
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed May 19, 2023
1 parent 9c52cca commit e235cd6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
40 changes: 28 additions & 12 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use anyhow::{Context, Result};
use bytes::Bytes;
use clap::Parser;
use tokio::runtime::Handle;
use url::Url;
use virtual_fs::{DeviceFile, FileSystem, PassthruFileSystem, RootFileSystemBuilder};
use wasmer::{Engine, Function, Instance, Memory32, Memory64, Module, RuntimeError, Store, Value};
use wasmer_wasix::{
Expand Down Expand Up @@ -110,6 +111,10 @@ pub struct Wasi {
/// Require WASI modules to only import 1 version of WASI.
#[clap(long = "deny-multiple-wasi-versions")]
pub deny_multiple_wasi_versions: bool,

/// The registry to use.
#[clap(long, env = "WASMER_REGISTRY", value_parser = parse_registry)]
pub registry: Option<Url>,
}

pub struct RunProperties {
Expand Down Expand Up @@ -479,14 +484,10 @@ impl Wasi {
wasmer_dir: &Path,
client: Arc<dyn HttpClient + Send + Sync>,
) -> Result<impl Registry + Send + Sync> {
// FIXME(Michael-F-Bryan): Ideally, all of this would live in some sort
// of from_env() constructor, but we don't want to add wasmer-registry
// as a dependency of wasmer-wasix just yet.
let config =
wasmer_registry::WasmerConfig::from_file(wasmer_dir).map_err(anyhow::Error::msg)?;

let mut registry = MultiSourceRegistry::new();

// Note: This should be first so our "preloaded" sources get a chance to
// override the main registry.
let mut preloaded = InMemorySource::new();
for path in &self.include_webcs {
preloaded
Expand All @@ -495,17 +496,32 @@ impl Wasi {
}
registry.add_source(preloaded);

// Note: This should be last so our "preloaded" sources get a chance to
// override the main registry.
let graphql_endpoint = config.registry.get_graphql_url();
let graphql_endpoint = graphql_endpoint
.parse()
.with_context(|| format!("Unable to parse \"{graphql_endpoint}\" as a URL"))?;
let graphql_endpoint = self.graphql_endpoint(wasmer_dir)?;
registry.add_source(WapmSource::new(graphql_endpoint, Arc::clone(&client)));

let cache_dir = WebSource::default_cache_dir(wasmer_dir);
registry.add_source(WebSource::new(cache_dir, client));

Ok(registry)
}

fn graphql_endpoint(&self, wasmer_dir: &Path) -> Result<Url> {
if let Some(endpoint) = &self.registry {
return Ok(endpoint.clone());
}

let config =
wasmer_registry::WasmerConfig::from_file(wasmer_dir).map_err(anyhow::Error::msg)?;
let graphql_endpoint = config.registry.get_graphql_url();
let graphql_endpoint = graphql_endpoint
.parse()
.with_context(|| format!("Unable to parse \"{graphql_endpoint}\" as a URL"))?;

Ok(graphql_endpoint)
}
}

fn parse_registry(r: &str) -> Result<Url> {
let url = wasmer_registry::format_graphql(r).parse()?;
Ok(url)
}
4 changes: 2 additions & 2 deletions lib/wasi/src/runtime/package_loader/builtin_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl BuiltinPackageLoader {
wasmer_dir.as_ref().join("checkouts")
}

/// Create a new [`BuiltinLoader`] based on `$WASMER_DIR` and the global
/// Wasmer config.
/// Create a new [`BuiltinPackageLoader`] based on `$WASMER_DIR` and the
/// global Wasmer config.
pub fn from_env() -> Result<Self, Error> {
let wasmer_dir = discover_wasmer_dir().context("Unable to determine $WASMER_DIR")?;
let client = crate::http::default_http_client().context("No HTTP client available")?;
Expand Down
4 changes: 2 additions & 2 deletions lib/wasi/src/runtime/resolver/web_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl WebSource {
}

/// Download a package and cache it locally.
#[tracing::instrument(skip(self))]
#[tracing::instrument(skip_all, fields(%url))]
async fn get_locally_cached_file(&self, url: &Url) -> Result<PathBuf, Error> {
// This function is a bit tricky because we go to great lengths to avoid
// unnecessary downloads.
Expand Down Expand Up @@ -227,7 +227,7 @@ impl Source for WebSource {
_ => return Ok(Vec::new()),
};

let local_path = self.get_locally_cached_file(url).await?;
let local_path = self.get_locally_cached_file(url).await.context("Unable to get the locally cached file")?;

// FIXME: this will block
let webc_sha256 = WebcHash::for_file(&local_path)?;
Expand Down

0 comments on commit e235cd6

Please sign in to comment.