-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add Gateway object (#1130)
- Loading branch information
1 parent
4a6ede9
commit 3f484ae
Showing
15 changed files
with
828 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,151 @@ | ||
use std::{collections::HashMap, path::PathBuf, str::FromStr}; | ||
|
||
use rattler_conda_types::{Channel, Platform}; | ||
use rattler_repodata_gateway::{fetch::CacheAction, ChannelConfig, Gateway, SourceConfig}; | ||
use serde::Deserialize; | ||
use url::Url; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use crate::JsResult; | ||
|
||
#[wasm_bindgen] | ||
#[repr(transparent)] | ||
#[derive(Clone)] | ||
pub struct JsGateway { | ||
inner: Gateway, | ||
} | ||
|
||
impl From<Gateway> for JsGateway { | ||
fn from(value: Gateway) -> Self { | ||
JsGateway { inner: value } | ||
} | ||
} | ||
|
||
impl From<JsGateway> for Gateway { | ||
fn from(value: JsGateway) -> Self { | ||
value.inner | ||
} | ||
} | ||
|
||
impl AsRef<Gateway> for JsGateway { | ||
fn as_ref(&self) -> &Gateway { | ||
&self.inner | ||
} | ||
} | ||
|
||
#[derive(Default, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct JsGatewayOptions { | ||
max_concurrent_requests: Option<usize>, | ||
|
||
#[serde(default)] | ||
channel_config: JsChannelConfig, | ||
} | ||
|
||
#[derive(Default, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct JsChannelConfig { | ||
#[serde(default)] | ||
default: JsSourceConfig, | ||
#[serde(default)] | ||
per_channel: HashMap<Url, JsSourceConfig>, | ||
} | ||
|
||
impl From<JsChannelConfig> for ChannelConfig { | ||
fn from(value: JsChannelConfig) -> Self { | ||
ChannelConfig { | ||
default: value.default.into(), | ||
per_channel: value | ||
.per_channel | ||
.into_iter() | ||
.map(|(key, value)| (key, value.into())) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
fn yes() -> bool { | ||
true | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct JsSourceConfig { | ||
#[serde(default = "yes")] | ||
zstd_enabled: bool, | ||
|
||
#[serde(default = "yes")] | ||
bz2_enabled: bool, | ||
|
||
#[serde(default = "yes")] | ||
sharded_enabled: bool, | ||
} | ||
|
||
impl Default for JsSourceConfig { | ||
fn default() -> Self { | ||
Self { | ||
zstd_enabled: true, | ||
bz2_enabled: true, | ||
sharded_enabled: true, | ||
} | ||
} | ||
} | ||
|
||
impl From<JsSourceConfig> for SourceConfig { | ||
fn from(value: JsSourceConfig) -> Self { | ||
Self { | ||
jlap_enabled: false, | ||
zstd_enabled: value.zstd_enabled, | ||
bz2_enabled: value.bz2_enabled, | ||
sharded_enabled: value.sharded_enabled, | ||
cache_action: CacheAction::default(), | ||
} | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl JsGateway { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(input: JsValue) -> JsResult<Self> { | ||
let mut builder = Gateway::builder(); | ||
let options: Option<JsGatewayOptions> = serde_wasm_bindgen::from_value(input)?; | ||
if let Some(options) = options { | ||
if let Some(max_concurrent_requests) = options.max_concurrent_requests { | ||
builder.set_max_concurrent_requests(max_concurrent_requests); | ||
} | ||
builder.set_channel_config(options.channel_config.into()); | ||
}; | ||
|
||
Ok(Self { | ||
inner: builder.finish(), | ||
}) | ||
} | ||
|
||
pub async fn names( | ||
&self, | ||
channels: Vec<String>, | ||
platforms: Vec<String>, | ||
) -> Result<Vec<String>, JsError> { | ||
// TODO: Dont hardcode | ||
let channel_config = | ||
rattler_conda_types::ChannelConfig::default_with_root_dir(PathBuf::from("")); | ||
|
||
let channels = channels | ||
.into_iter() | ||
.map(|s| Channel::from_str(&s, &channel_config)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
let platforms = platforms | ||
.into_iter() | ||
.map(|p| Platform::from_str(&p)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
Ok(self | ||
.inner | ||
.names(channels, platforms) | ||
.execute() | ||
.await? | ||
.into_iter() | ||
.map(|name| name.as_source().to_string()) | ||
.collect()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod error; | ||
mod gateway; | ||
pub mod solve; | ||
mod utils; | ||
mod version; | ||
|
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,64 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import { Gateway } from "./Gateway"; | ||
|
||
describe("Gateway", () => { | ||
describe("constructor", () => { | ||
it("works without arguments", () => { | ||
expect(() => new Gateway()).not.toThrowError(); | ||
expect(() => new Gateway(null)).not.toThrowError(); | ||
expect(() => new Gateway(undefined)).not.toThrowError(); | ||
}); | ||
it("throws on invalid arguments", () => { | ||
expect(() => new Gateway(true as any)).toThrowError(); | ||
}); | ||
it("accepts an empty object", () => { | ||
expect(() => new Gateway({})).not.toThrowError(); | ||
}); | ||
it("accepts null for maxConcurrentRequests", () => { | ||
expect( | ||
() => | ||
new Gateway({ | ||
maxConcurrentRequests: null, | ||
}), | ||
).not.toThrowError(); | ||
}); | ||
it("accepts empty channelConfig", () => { | ||
expect( | ||
() => | ||
new Gateway({ | ||
channelConfig: {}, | ||
}), | ||
).not.toThrowError(); | ||
}); | ||
it("accepts perChannel channelConfig", () => { | ||
expect( | ||
() => | ||
new Gateway({ | ||
channelConfig: { | ||
default: {}, | ||
perChannel: { | ||
"https://prefix.dev": { | ||
bz2Enabled: false, | ||
shardedEnabled: false, | ||
zstdEnabled: false, | ||
}, | ||
}, | ||
}, | ||
}), | ||
).not.toThrowError(); | ||
}); | ||
}); | ||
describe("names", () => { | ||
const gateway = new Gateway(); | ||
it("can query prefix.dev", () => { | ||
return gateway | ||
.names( | ||
["https://prefix.dev/emscripten-forge-dev"], | ||
["noarch", "emscripten-wasm32"], | ||
) | ||
.then((names) => { | ||
expect(names.length).toBeGreaterThanOrEqual(177); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.