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

feat: add http caching to reqwest #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
489 changes: 485 additions & 4 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -28,9 +28,12 @@ clap = { version = "4", features = ["derive", "string"] }
home = { version = "0.5.5" }
insta = { version = "1.31.0", features = ["yaml", "glob", "walkdir"] }
futures-util = { version = "0.3.28" }
moka = { version = "0.11.3" }
miette = { version = "5.9.0", features = ["fancy"] }
reflink-copy = { version = "0.1.5" }
reqwest = { version = "0.11", default-features = false, features = ["json", "native-tls-vendored"] }
reqwest-middleware = { version = "0.2.3" }
http-cache-reqwest = { version = "0.11.1", default-features = false, features = ["manager-moka"] }
node-semver = { version = "2.1.0" }
pipe-trait = { version = "0.4.0" }
rayon = { version = "1.7.0" }
Expand All @@ -44,7 +47,7 @@ tar = { version = "0.4.38" }
thiserror = { version = "1.0.44" }
tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
walkdir = { version = "2.3.3" }
zune-inflate = { version = "0.2.54" }

Expand Down
21 changes: 12 additions & 9 deletions crates/cli/Cargo.toml
Expand Up @@ -23,15 +23,18 @@ pacquet_registry = { workspace = true }
pacquet_tarball = { workspace = true }
pacquet_diagnostics = { workspace = true }

clap = { workspace = true }
futures-util = { workspace = true }
rayon = { workspace = true }
reflink-copy = { workspace = true }
reqwest = { workspace = true }
node-semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
clap = { workspace = true }
http-cache-reqwest = { workspace = true }
futures-util = { workspace = true }
moka = { workspace = true }
rayon = { workspace = true }
reflink-copy = { workspace = true }
reqwest = { workspace = true }
reqwest-middleware = { workspace = true }
node-semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
Comment on lines -26 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See? This is the huge diff that I talked about.


[dev-dependencies]
insta = { workspace = true }
Expand Down
16 changes: 13 additions & 3 deletions crates/cli/src/package.rs
Expand Up @@ -3,6 +3,7 @@ use crate::package_manager::PackageManagerError;
use pacquet_npmrc::Npmrc;
use pacquet_registry::{Package, PackageVersion};
use pacquet_tarball::download_tarball_to_store;
use reqwest_middleware::ClientWithMiddleware;
use std::path::PathBuf;

/// This function execute the following and returns the package
Expand All @@ -15,7 +16,7 @@ use std::path::PathBuf;
/// `node_modules/.pacquet/fastify@1.0.0/node_modules`.
pub async fn find_package_version_from_registry<P: Into<PathBuf>>(
config: &Npmrc,
http_client: &reqwest::Client,
http_client: &ClientWithMiddleware,
name: &str,
version: &str,
symlink_path: P,
Expand All @@ -28,7 +29,7 @@ pub async fn find_package_version_from_registry<P: Into<PathBuf>>(

pub async fn fetch_package_version_directly<P: Into<PathBuf>>(
config: &Npmrc,
http_client: &reqwest::Client,
http_client: &ClientWithMiddleware,
name: &str,
version: &str,
symlink_path: P,
Expand Down Expand Up @@ -67,8 +68,11 @@ async fn internal_fetch<P: Into<PathBuf>>(
#[cfg(test)]
mod tests {
use crate::package::find_package_version_from_registry;
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions, MokaManager};
use node_semver::Version;
use pacquet_npmrc::Npmrc;
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
use std::fs;
use std::path::Path;
use tempfile::tempdir;
Expand Down Expand Up @@ -103,7 +107,13 @@ mod tests {
let modules_dir = tempdir().unwrap();
let virtual_store_dir = tempdir().unwrap();
let config = get_config(store_dir.path(), modules_dir.path(), virtual_store_dir.path());
let http_client = reqwest::Client::new();
let http_client = ClientBuilder::new(Client::new())
.with(Cache(HttpCache {
mode: CacheMode::ForceCache,
manager: MokaManager::default(),
options: HttpCacheOptions::default(),
}))
.build();
let symlink_path = tempdir().unwrap();
let package = find_package_version_from_registry(
&config,
Expand Down
15 changes: 13 additions & 2 deletions crates/cli/src/package_manager.rs
@@ -1,3 +1,6 @@
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions, MokaManager};
use reqwest::Client;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use std::path::PathBuf;

use pacquet_diagnostics::{
Expand Down Expand Up @@ -30,15 +33,23 @@ pub enum PackageManagerError {
pub struct PackageManager {
pub config: Box<Npmrc>,
pub package_json: Box<PackageJson>,
pub http_client: Box<reqwest::Client>,
pub http_client: Box<ClientWithMiddleware>,
}

impl PackageManager {
pub fn new<P: Into<PathBuf>>(package_json_path: P) -> Result<Self, PackageManagerError> {
Ok(PackageManager {
config: Box::new(get_current_npmrc()),
package_json: Box::new(PackageJson::create_if_needed(package_json_path.into())?),
http_client: Box::new(reqwest::Client::new()),
http_client: Box::new(
ClientBuilder::new(Client::new())
.with(Cache(HttpCache {
mode: CacheMode::ForceCache,
manager: MokaManager::default(),
options: HttpCacheOptions::default(),
}))
.build(),
),
})
}
}
13 changes: 7 additions & 6 deletions crates/registry/Cargo.toml
Expand Up @@ -13,12 +13,13 @@ repository.workspace = true
[dependencies]
pacquet_diagnostics = { workspace = true }

reqwest = { workspace = true }
node-semver = { workspace = true }
pipe-trait = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
reqwest = { workspace = true }
reqwest-middleware = { workspace = true }
node-semver = { workspace = true }
pipe-trait = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
4 changes: 4 additions & 0 deletions crates/registry/src/lib.rs
Expand Up @@ -26,6 +26,10 @@ pub enum RegistryError {
#[diagnostic(code(pacquet_registry::network_error))]
Network(#[from] reqwest::Error),

#[error(transparent)]
#[diagnostic(code(pacquet_registry::network_middleware_error))]
NetworkMiddleware(#[from] reqwest_middleware::Error),

#[error(transparent)]
#[diagnostic(code(pacquet_registry::io_error))]
Io(#[from] std::io::Error),
Expand Down
3 changes: 2 additions & 1 deletion crates/registry/src/package.rs
Expand Up @@ -4,6 +4,7 @@ use std::{
};

use pipe_trait::Pipe;
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};

use crate::{package_version::PackageVersion, RegistryError};
Expand All @@ -28,7 +29,7 @@ impl PartialEq for Package {
impl Package {
pub async fn fetch_from_registry(
name: &str,
http_client: &reqwest::Client,
http_client: &ClientWithMiddleware,
registry: &str,
) -> Result<Self, RegistryError> {
http_client
Expand Down
3 changes: 2 additions & 1 deletion crates/registry/src/package_version.rs
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use pipe_trait::Pipe;
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};

use crate::package_distribution::PackageDistribution;
Expand All @@ -27,7 +28,7 @@ impl PackageVersion {
pub async fn fetch_from_registry(
name: &str,
version: &str,
http_client: &reqwest::Client,
http_client: &ClientWithMiddleware,
registry: &str,
) -> Result<Self, RegistryError> {
http_client
Expand Down
11 changes: 6 additions & 5 deletions crates/tarball/Cargo.toml
Expand Up @@ -14,11 +14,12 @@ repository.workspace = true
pacquet_cafs = { workspace = true }
pacquet_diagnostics = { workspace = true }

reqwest = { workspace = true }
ssri = { workspace = true }
tar = { workspace = true }
tokio = { workspace = true }
zune-inflate = { workspace = true }
reqwest = { workspace = true }
reqwest-middleware = { workspace = true }
ssri = { workspace = true }
tar = { workspace = true }
tokio = { workspace = true }
zune-inflate = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }