Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ doctest = true

[dependencies]
anyhow = "=1.0.93"
async-compression = { version = "=0.4.18", default-features = false, features = ["gzip", "tokio"] }
async-trait = "=0.1.83"
aws-credential-types = { version = "=1.2.1", features = ["hardcoded-credentials"] }
aws-ip-ranges = "=0.926.0"
Expand Down Expand Up @@ -87,7 +88,7 @@ indexmap = { version = "=2.6.0", features = ["serde"] }
indicatif = "=0.17.9"
ipnetwork = "=0.20.0"
json-subscriber = "=0.2.3"
tikv-jemallocator = { version = "=0.6.0", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
krata-tokio-tar = "=0.4.2"
lettre = { version = "=0.11.10", default-features = false, features = ["file-transport", "smtp-transport", "hostname", "builder", "tokio1", "tokio1-native-tls"] }
minijinja = "=2.5.0"
mockall = "=0.13.1"
Expand All @@ -112,6 +113,7 @@ spdx = "=0.10.7"
tar = "=0.4.43"
tempfile = "=3.14.0"
thiserror = "=2.0.3"
tikv-jemallocator = { version = "=0.6.0", features = ['unprefixed_malloc_on_supported_platforms', 'profiling'] }
tokio = { version = "=1.41.1", features = ["net", "signal", "io-std", "io-util", "rt-multi-thread", "macros", "process"]}
tokio-postgres = "=0.7.12"
tokio-util = "=0.7.12"
Expand Down
112 changes: 62 additions & 50 deletions src/bin/crates-admin/render_readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
models::Version,
schema::{crates, readme_renderings, versions},
};
use std::path::PathBuf;
use std::{io::Read, path::Path, sync::Arc};
use futures_util::{StreamExt, TryStreamExt};
use std::path::{Path, PathBuf};
use std::{future, sync::Arc};
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio_util::io::StreamReader;

use async_compression::tokio::bufread::GzipDecoder;
use chrono::{NaiveDateTime, Utc};
use crates_io::storage::Storage;
use crates_io::tasks::spawn_blocking;
Expand All @@ -15,10 +19,9 @@
use diesel::prelude::*;
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use flate2::read::GzDecoder;
use reqwest::{header, Client};
use std::str::FromStr;
use tar::{self, Archive};
use tokio_tar::{self, Archive};

const USER_AGENT: &str = "crates-admin";

Expand Down Expand Up @@ -168,22 +171,25 @@
));
}

let body = response.bytes().await?;

spawn_blocking(move || {
let reader = GzDecoder::new(&*body);
let archive = Archive::new(reader);
render_pkg_readme(archive, &pkg_name)
})
.await?
let reader = response
.bytes_stream()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e));
let reader = StreamReader::new(reader);
let reader = GzipDecoder::new(reader);
let archive = Archive::new(reader);
render_pkg_readme(archive, &pkg_name).await

Check warning on line 180 in src/bin/crates-admin/render_readmes.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/crates-admin/render_readmes.rs#L174-L180

Added lines #L174 - L180 were not covered by tests
}

fn render_pkg_readme<R: Read>(mut archive: Archive<R>, pkg_name: &str) -> anyhow::Result<String> {
async fn render_pkg_readme<R: AsyncRead + Unpin>(
mut archive: Archive<R>,
pkg_name: &str,
) -> anyhow::Result<String> {
let mut entries = archive.entries().context("Invalid tar archive entries")?;

let manifest: Manifest = {
let path = format!("{pkg_name}/Cargo.toml");
let contents = find_file_by_path(&mut entries, Path::new(&path))
.await

Check warning on line 192 in src/bin/crates-admin/render_readmes.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/crates-admin/render_readmes.rs#L192

Added line #L192 was not covered by tests
.context("Failed to read Cargo.toml file")?;

Manifest::from_str(&contents).context("Failed to parse manifest file")?
Expand All @@ -207,39 +213,42 @@

let path = Path::new(pkg_name).join(&readme_path);
let contents = find_file_by_path(&mut entries, Path::new(&path))
.await

Check warning on line 216 in src/bin/crates-admin/render_readmes.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/crates-admin/render_readmes.rs#L216

Added line #L216 was not covered by tests
.with_context(|| format!("Failed to read {} file", readme_path.display()))?;

// pkg_path_in_vcs Unsupported from admin::render_readmes. See #4095
// Would need access to cargo_vcs_info
let pkg_path_in_vcs = None;

let repository = manifest
.package
.as_ref()
.and_then(|p| p.repository.as_ref())
.and_then(|r| r.as_ref().as_local())
.map(|s| s.as_str());

text_to_html(&contents, &readme_path, repository, pkg_path_in_vcs)
spawn_blocking(move || {
let repository = manifest
.package
.as_ref()
.and_then(|p| p.repository.as_ref())
.and_then(|r| r.as_ref().as_local())
.map(|s| s.as_str());
text_to_html(&contents, &readme_path, repository, pkg_path_in_vcs)
})
.await?
};
Ok(rendered)
}

/// Search an entry by its path in a Tar archive.
fn find_file_by_path<R: Read>(
entries: &mut tar::Entries<'_, R>,
async fn find_file_by_path<R: AsyncRead + Unpin>(
entries: &mut tokio_tar::Entries<R>,
path: &Path,
) -> anyhow::Result<String> {
let mut file = entries
.filter_map(|entry| entry.ok())
.find(|file| match file.path() {
Ok(p) => p == path,
Err(_) => false,
})
.filter_map(|entry| future::ready(entry.ok()))
.filter(|entry| future::ready(entry.path().is_ok_and(|p| p == path)))
.next()
.await

Check warning on line 246 in src/bin/crates-admin/render_readmes.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/crates-admin/render_readmes.rs#L246

Added line #L246 was not covered by tests
.ok_or_else(|| anyhow!("Failed to find tarball entry: {}", path.display()))?;

let mut contents = String::new();
file.read_to_string(&mut contents)
.await

Check warning on line 251 in src/bin/crates-admin/render_readmes.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/crates-admin/render_readmes.rs#L251

Added line #L251 was not covered by tests
.context("Failed to read file contents")?;

Ok(contents)
Expand All @@ -252,8 +261,8 @@

use super::render_pkg_readme;

#[test]
fn test_render_pkg_readme() {
#[tokio::test]
async fn test_render_pkg_readme() {
let serialized_archive = TarballBuilder::new()
.add_file(
"foo-0.0.1/Cargo.toml",
Expand All @@ -267,13 +276,14 @@
.add_file("foo-0.0.1/README.md", b"readme")
.build_unzipped();

let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
let result = render_pkg_readme(tokio_tar::Archive::new(&*serialized_archive), "foo-0.0.1")
.await
.unwrap();
assert!(result.contains("readme"))
}

#[test]
fn test_render_pkg_no_readme() {
#[tokio::test]
async fn test_render_pkg_no_readme() {
let serialized_archive = TarballBuilder::new()
.add_file(
"foo-0.0.1/Cargo.toml",
Expand All @@ -283,14 +293,13 @@
)
.build_unzipped();

assert_err!(render_pkg_readme(
tar::Archive::new(&*serialized_archive),
"foo-0.0.1"
));
assert_err!(
render_pkg_readme(tokio_tar::Archive::new(&*serialized_archive), "foo-0.0.1").await
);
}

#[test]
fn test_render_pkg_implicit_readme() {
#[tokio::test]
async fn test_render_pkg_implicit_readme() {
let serialized_archive = TarballBuilder::new()
.add_file(
"foo-0.0.1/Cargo.toml",
Expand All @@ -303,13 +312,14 @@
.add_file("foo-0.0.1/README.md", b"readme")
.build_unzipped();

let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
let result = render_pkg_readme(tokio_tar::Archive::new(&*serialized_archive), "foo-0.0.1")
.await
.unwrap();
assert!(result.contains("readme"))
}

#[test]
fn test_render_pkg_readme_w_link() {
#[tokio::test]
async fn test_render_pkg_readme_w_link() {
let serialized_archive = TarballBuilder::new()
.add_file(
"foo-0.0.1/Cargo.toml",
Expand All @@ -324,13 +334,14 @@
.add_file("foo-0.0.1/README.md", b"readme [link](./Other.md)")
.build_unzipped();

let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
let result = render_pkg_readme(tokio_tar::Archive::new(&*serialized_archive), "foo-0.0.1")
.await
.unwrap();
assert!(result.contains("\"https://github.com/foo/foo/blob/HEAD/./Other.md\""))
}

#[test]
fn test_render_pkg_readme_not_at_root() {
#[tokio::test]
async fn test_render_pkg_readme_not_at_root() {
let serialized_archive = TarballBuilder::new()
.add_file(
"foo-0.0.1/Cargo.toml",
Expand All @@ -348,8 +359,9 @@
)
.build_unzipped();

let result =
render_pkg_readme(tar::Archive::new(&*serialized_archive), "foo-0.0.1").unwrap();
let result = render_pkg_readme(tokio_tar::Archive::new(&*serialized_archive), "foo-0.0.1")
.await
.unwrap();
assert!(result.contains("docs/readme"));
assert!(result.contains("\"https://github.com/foo/foo/blob/HEAD/docs/./Other.md\""))
}
Expand Down