Skip to content

Commit

Permalink
Merge pull request #718 from vrc-get/headers-in-vpm-package-manifest
Browse files Browse the repository at this point in the history
feat(vpm): headers in package.json
  • Loading branch information
anatawa12 committed May 17, 2024
2 parents 9772d13 + 443b908 commit a459e92
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-gui.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added
- `vcc://` support `#978`
- This is enabled by default for macOS and you have to enable manually on Settings page for windows and linux.
- per-package `headers` field support `#718`

### Changed
- Improved project Template selection `#967`
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Per-package `headers` field support `#718`
- Since this is adding support for missing features, I treat this as a bugfix and not bump minor version.

### Changed

Expand Down
17 changes: 15 additions & 2 deletions vrc-get-vpm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,23 @@ impl<T: HttpClient, IO: EnvironmentIo> RemotePackageDownloader for Environment<T
} else {
self.io.create_dir_all(zip_path.parent().unwrap()).await?;

let new_headers = IndexMap::from_iter(
(repository
.headers()
.iter()
.map(|(k, v)| (k.as_ref(), v.as_ref())))
.chain(
package
.headers()
.iter()
.map(|(k, v)| (k.as_ref(), v.as_ref())),
),
);

Ok(download_package_zip(
self.http.as_ref(),
&self.io,
repository.headers(),
&new_headers,
&zip_path,
&sha_path,
&zip_file_name,
Expand Down Expand Up @@ -625,7 +638,7 @@ async fn try_load_package_cache<IO: EnvironmentIo>(
async fn download_package_zip<IO: EnvironmentIo>(
http: Option<&impl HttpClient>,
io: &IO,
headers: &IndexMap<Box<str>, Box<str>>,
headers: &IndexMap<&str, &str>,
zip_path: &Path,
sha_path: &Path,
zip_file_name: &str,
Expand Down
8 changes: 8 additions & 0 deletions vrc-get-vpm/src/package_manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ macro_rules! initialize_from_package_json_like {
legacy_folders: $source.legacy_folders,
legacy_files: $source.legacy_files,
legacy_packages: $source.legacy_packages,
headers: $source.headers,
changelog_url: $source.changelog_url,
vrc_get: VrcGetMeta {
yanked: $source.vrc_get.yanked,
Expand Down Expand Up @@ -78,6 +79,9 @@ macro_rules! package_json_struct {
$(#[$optional])?
$optional_vis legacy_packages: Vec<Box<str>>,

$(#[$optional])?
$optional_vis headers: indexmap::IndexMap<Box<str>, Box<str>>,

$(#[$optional])?
$optional_vis changelog_url: Option<Url>,

Expand Down Expand Up @@ -129,6 +133,9 @@ impl PackageManifest {
pub fn legacy_files(&self) -> &HashMap<Box<str>, Option<Box<str>>> {
&self.legacy_files
}
pub fn headers(&self) -> &IndexMap<Box<str>, Box<str>> {
&self.headers
}

pub fn legacy_packages(&self) -> &[Box<str>] {
self.legacy_packages.as_slice()
Expand Down Expand Up @@ -173,6 +180,7 @@ impl PackageManifest {
legacy_folders: HashMap::new(),
legacy_files: HashMap::new(),
legacy_packages: Vec::new(),
headers: IndexMap::new(),
vrc_get: VrcGetMeta::default(),
zip_sha_256: None,
changelog_url: None,
Expand Down
14 changes: 5 additions & 9 deletions vrc-get-vpm/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait HttpClient: Sync {
fn get(
&self,
url: &Url,
headers: &IndexMap<Box<str>, Box<str>>,
headers: &IndexMap<&str, &str>,
) -> impl Future<Output = io::Result<impl AsyncRead + Send>> + Send;

/// Get resource from the URL with specified headers and etag
Expand All @@ -79,17 +79,13 @@ pub trait HttpClient: Sync {
}

impl HttpClient for reqwest::Client {
async fn get(
&self,
url: &Url,
headers: &IndexMap<Box<str>, Box<str>>,
) -> io::Result<impl AsyncRead> {
async fn get(&self, url: &Url, headers: &IndexMap<&str, &str>) -> io::Result<impl AsyncRead> {
// file not found: err

let mut request = self.get(url.to_owned());

for (name, header) in headers {
request = request.header(name.as_ref(), header.as_ref());
for (&name, &header) in headers {
request = request.header(name, header);
}

Ok(request
Expand Down Expand Up @@ -140,7 +136,7 @@ impl HttpClient for reqwest::Client {
}

impl HttpClient for Infallible {
async fn get(&self, _: &Url, _: &IndexMap<Box<str>, Box<str>>) -> io::Result<impl AsyncRead> {
async fn get(&self, _: &Url, _: &IndexMap<&str, &str>) -> io::Result<impl AsyncRead> {
Ok(io::empty())
}

Expand Down

0 comments on commit a459e92

Please sign in to comment.