Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download proc-blocks from WAPM #435

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
graphql_client = { version = "0.10.0", features = ["reqwest-blocking"], optional = true }
indexmap = { version = "1.8.1", features = ["serde-1"] }
once_cell = "1.10.0"
queryst = { version = "2.1.0" }
Expand All @@ -34,4 +35,4 @@ tracing-test = "0.2.1"

[features]
default = ["builtins"]
builtins = ["reqwest"]
builtins = ["reqwest", "graphql_client"]
113 changes: 104 additions & 9 deletions crates/compiler/src/asset_loader/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
use std::path::{Component, Path, PathBuf};

use graphql_client::{GraphQLQuery, Response};
use reqwest::blocking::Client;
use uriparse::{Scheme, URI};

use crate::{
asset_loader::{AssetLoader, ReadError, WapmUri},
asset_loader::{
builtin::lookup_package::{
LookupPackageGetPackageVersion,
LookupPackageGetPackageVersionModules,
},
AssetLoader, ReadError, WapmUri,
},
im::Vector,
};

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/asset_loader/schema.graphql",
query_path = "src/asset_loader/queries.graphql",
response_derives = "Debug,Serialize"
)]
pub struct LookupPackage;

const WAPM_REGISTRY: &str = "https://registry.wapm.io/graphql";

/// An [`AssetLoader`] that loads assets using the basic solution.
#[derive(Debug, Clone)]
pub struct DefaultAssetLoader {
Expand Down Expand Up @@ -36,15 +53,16 @@ impl AssetLoader for DefaultAssetLoader {
fn read(&self, uri: &URI<'_>) -> Result<Vector<u8>, ReadError> {
match uri.scheme() {
Scheme::HTTP | Scheme::HTTPS => {
http_file(&self.client, uri).map_err(ReadError::from)
let url = uri.to_string();
http_file(&self.client, &url).map_err(ReadError::from)
},
Scheme::File => local_file(&self.root_directory, uri.path()),
Scheme::Unregistered(u) if u.as_str().is_empty() => {
local_file(&self.root_directory, uri.path())
},
Scheme::Unregistered(u) if u.as_str() == "wapm" => {
let _uri: WapmUri = uri.try_into().map_err(ReadError::other)?;
todo!()
let uri = uri.try_into().map_err(ReadError::other)?;
wapm_file(&self.client, uri).map_err(ReadError::other)
},
other => Err(ReadError::UnsupportedScheme {
scheme: other.as_str().into(),
Expand All @@ -54,15 +72,92 @@ impl AssetLoader for DefaultAssetLoader {
}

#[tracing::instrument(skip_all)]
fn http_file(
fn wapm_file(
client: &Client,
url: &URI<'_>,
) -> Result<Vector<u8>, reqwest::Error> {
uri: WapmUri,
) -> Result<Vector<u8>, WapmDownloadError> {
let WapmUri {
namespace,
package_name,
version,
module,
} = uri;
tracing::debug!(%namespace, %package_name, ?version, "Querying WAPM's GraphQL API");

let variables = lookup_package::Variables {
name: format!("{namespace}/{package_name}"),
version,
};

let Response { data, errors } =
graphql_client::reqwest::post_graphql_blocking::<LookupPackage, _>(
client,
WAPM_REGISTRY,
variables,
)?;

if let Some(mut errors) = errors {
if !errors.is_empty() {
return Err(WapmDownloadError::GraphQL(errors.remove(0)));
}
}

let LookupPackageGetPackageVersion { version, modules } = data
.and_then(|r| r.get_package_version)
.ok_or(WapmDownloadError::PackageNotFound)?;

tracing::debug!(%version, ?modules, "Found a compatible package");

let LookupPackageGetPackageVersionModules { public_url, .. } = match module
{
Some(module) => match modules.iter().find(|v| v.name == module) {
Some(v) => v,
None => {
return Err(WapmDownloadError::ModuleNotFound {
requested: module,
names: modules.iter().map(|v| v.name.clone()).collect(),
})
},
},
None => match modules.as_slice() {
[] => return Err(WapmDownloadError::EmptyPackage),
[v] => v,
[..] => {
return Err(WapmDownloadError::MultipleModules {
names: modules.iter().map(|v| v.name.clone()).collect(),
})
},
},
};

http_file(client, public_url).map_err(WapmDownloadError::Http)
}

#[derive(Debug, thiserror::Error)]
enum WapmDownloadError {
#[error("The request failed")]
Http(#[from] reqwest::Error),
#[error("GraphQL")]
GraphQL(graphql_client::Error),
#[error("The package wasn't found")]
PackageNotFound,
#[error("The package contains multiple modules, but the caller didn't say which to use")]
MultipleModules { names: Vec<String> },
#[error("The package is empty")]
EmptyPackage,
#[error("Couldn't find the \"{requested}\" module in {names:?}")]
ModuleNotFound {
requested: String,
names: Vec<String>,
},
}

#[tracing::instrument(skip_all)]
fn http_file(client: &Client, url: &str) -> Result<Vector<u8>, reqwest::Error> {
tracing::info!(%url, "Downloading");
let response = client.get(url.to_string()).send()?.error_for_status()?;

let response = client.get(url).send()?.error_for_status()?;
let status_code = response.status();

let body = response.bytes()?;
let body = Vector::from(&*body);

Expand Down
9 changes: 9 additions & 0 deletions crates/compiler/src/asset_loader/queries.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query LookupPackage($name: String!, $version: String = "latest") {
getPackageVersion(name: $name, version: $version) {
version
modules {
name
publicUrl
}
}
}
Loading