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 tokio-rustls-tls feature #55

Merged
merged 4 commits into from
Apr 8, 2024
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 .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: ilammy/setup-nasm@v1

- name: Install Rust Stable
uses: actions-rs/toolchain@v1
with:
Expand Down
60 changes: 42 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ license = "Apache-2.0"
documentation = "https://docs.rs/hf-hub"
repository = "https://github.com/huggingface/hf-hub"
readme = "README.md"
keywords = [
"huggingface",
"hf",
"hub",
"machine-learning"
]
keywords = ["huggingface", "hf", "hub", "machine-learning"]
description = """
This crates aims ease the interaction with [huggingface](https://huggingface.co/)
It aims to be compatible with [huggingface_hub](https://github.com/huggingface/huggingface_hub/) python package, but only implements a smaller subset of functions.
Expand All @@ -22,26 +17,55 @@ It aims to be compatible with [huggingface_hub](https://github.com/huggingface/h
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
futures = { version = "0.3.28", optional = true }
dirs = "5.0.1"
http = { version = "1.0.0", optional = true }
indicatif = { version = "0.17.5", optional = true }
log = "0.4.19"
num_cpus = { version = "1.15.0", optional = true }
rand = { version = "0.8.5", optional = true }
reqwest = { version = "0.11.18", optional = true, features = ["json"] }
reqwest = { version = "0.12.2", optional = true, default-features = false, features = [
"json",
] }
rustls = { version = "0.23.4", optional = true }
serde = { version = "1.0.171", features = ["derive"], optional = true }
serde_json = { version = "1.0.103", optional = true }
indicatif = { version = "0.17.5", optional = true }
num_cpus = { version = "1.15.0", optional = true }
tokio = { version = "1.29.1", optional = true, features = ["fs", "macros"] }
futures = { version = "0.3.28", optional = true }
thiserror = { version = "1.0.43", optional = true }
ureq = { version = "2.8.0", optional = true, features = ["native-tls", "json", "socks-proxy"] }
native-tls = { version = "0.2.11", optional = true }
log = "0.4.19"
tokio = { version = "1.29.1", optional = true, features = ["fs", "macros"] }
ureq = { version = "2.8.0", optional = true, features = [
"json",
"socks-proxy",
] }

[features]
default = ["online"]
online = ["ureq", "tokio"]
ureq = ["dep:ureq", "dep:native-tls", "dep:rand", "dep:serde", "dep:serde_json", "dep:indicatif", "dep:thiserror", "dep:http"]
tokio = ["dep:reqwest", "dep:tokio", "tokio/rt-multi-thread", "dep:futures", "dep:rand", "dep:serde", "dep:serde_json", "dep:indicatif", "dep:num_cpus", "dep:thiserror"]
default = ["default-tls", "tokio", "ureq"]
# These features are only relevant when used with the `tokio` feature, but this might change in the future.
default-tls = ["dep:reqwest", "reqwest/default"]
rustls-tls = ["dep:reqwest", "dep:rustls", "reqwest/rustls-tls"]
tokio = [
"dep:futures",
"dep:indicatif",
"dep:num_cpus",
"dep:rand",
"dep:reqwest",
"reqwest/charset",
"reqwest/http2",
"reqwest/macos-system-configuration",
"dep:serde",
"dep:serde_json",
"dep:thiserror",
"dep:tokio",
"tokio/rt-multi-thread",
]
ureq = [
"dep:http",
"dep:indicatif",
"dep:rand",
"dep:serde",
"dep:serde_json",
"dep:thiserror",
"dep:ureq",
]

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ let _filename = repo.get("config.json").unwrap();

// filename is now the local location within hf cache of the config.json file
```

# SSL/TLS

When using the [`ureq`](https://github.com/algesten/ureq) feature, you will always use its default TLS backend which is [rustls](https://github.com/rustls/rustls).

When using [`tokio`](https://github.com/tokio-rs/tokio), by default `default-tls` will be enabled, which means OpenSSL. If you want/need to use rustls, disable the default features and use `rustls-tls` in conjunction with `tokio`.
1 change: 0 additions & 1 deletion src/api/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ impl ApiRepo {
mod tests {
use super::*;
use crate::api::Siblings;
use crate::RepoType;
use hex_literal::hex;
use rand::{distributions::Alphanumeric, Rng};
use serde_json::{json, Value};
Expand Down
3 changes: 1 addition & 2 deletions src/api/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,8 @@ impl ApiRepo {
mod tests {
use super::*;
use crate::api::Siblings;
use crate::RepoType;
use hex_literal::hex;
use rand::{distributions::Alphanumeric, Rng};
use rand::distributions::Alphanumeric;
use serde_json::{json, Value};
use sha2::{Digest, Sha256};

Expand Down
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![deny(missing_docs)]
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
use rand::{distributions::Alphanumeric, Rng};
use std::io::Write;
use std::path::PathBuf;

/// The actual Api to interact with the hub.
#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub mod api;

/// The type of repo to interact with
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Cache {
self.repo(Repo::new(model_id, RepoType::Space))
}

#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub(crate) fn temp_path(&self) -> PathBuf {
let mut path = self.path().clone();
path.push("tmp");
Expand Down Expand Up @@ -175,8 +175,7 @@ impl CacheRepo {
Ok(())
}


#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub(crate) fn blob_path(&self, etag: &str) -> PathBuf {
let mut blob_path = self.path();
blob_path.push("blobs");
Expand Down Expand Up @@ -262,7 +261,7 @@ impl Repo {
}

/// The actual URL part of the repo
#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub fn url(&self) -> String {
match self.repo_type {
RepoType::Model => self.repo_id.to_string(),
Expand All @@ -276,13 +275,13 @@ impl Repo {
}

/// Revision needs to be url escaped before being used in a URL
#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub fn url_revision(&self) -> String {
self.revision.replace('/', "%2F")
}

/// Used to compute the repo's url part when accessing the metadata of the repo
#[cfg(any(feature="tokio", feature="ureq"))]
#[cfg(any(feature = "tokio", feature = "ureq"))]
pub fn api_url(&self) -> String {
let prefix = match self.repo_type {
RepoType::Model => "models",
Expand Down
Loading