Skip to content

Commit 9755ea7

Browse files
authored
fix: regex, LazyLock, etc. (#82)
1 parent a427c52 commit 9755ea7

5 files changed

Lines changed: 45 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ bytes = "1.11"
2121
figment = { version = "0.10", features = ["toml"] }
2222
futures = "0.3"
2323
futures-util = "0.3"
24-
lazy_static = "1.4"
2524
percent-encoding = "2.1"
2625
prometheus = "0.14"
2726
regex = "1"

src/artifacts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ enum UploadPayload {
2929
}
3030

3131
impl UploadPayload {
32-
fn content_length(&self) -> u64 {
32+
const fn content_length(&self) -> u64 {
3333
match self {
3434
Self::Memory(bytes) => bytes.len() as u64,
3535
Self::File { content_length, .. } => *content_length,
@@ -40,11 +40,11 @@ impl UploadPayload {
4040
/// Global unique file id counter.
4141
static FILE_ID: AtomicUsize = AtomicUsize::new(0);
4242

43-
fn max_download_size(config: &Config) -> u64 {
43+
const fn max_download_size(config: &Config) -> u64 {
4444
config.ignore_threshold_mb.saturating_mul(BYTES_PER_MB)
4545
}
4646

47-
fn file_threshold(config: &Config) -> u64 {
47+
const fn file_threshold(config: &Config) -> u64 {
4848
config.file_threshold_mb.saturating_mul(BYTES_PER_MB)
4949
}
5050

src/repos.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use actix_web::http::{Method, Uri};
22
use actix_web::{HttpResponse, Route, guard, web};
3-
use lazy_static::lazy_static;
43
use regex::Regex;
4+
use std::sync::LazyLock;
55

66
use crate::error::Result;
77
use crate::intel_path::IntelPath;
@@ -100,15 +100,23 @@ pub fn rust_static_allow(_config: &Config, path: &str) -> bool {
100100
true
101101
}
102102

103-
pub fn wheels_allow(_config: &Config, path: &str) -> bool {
103+
pub fn wheel_allow_legacy(_config: &Config, path: &str) -> bool {
104104
path.ends_with(".whl") || path.ends_with(".html")
105105
}
106106

107+
pub fn wheels_allow(_config: &Config, path: &str) -> bool {
108+
if let Some((wheel_path, sha256)) = path.split_once("#sha256=") {
109+
return wheel_path.ends_with(".whl")
110+
&& sha256.len() == 64
111+
&& sha256.chars().all(|c| c.is_ascii_hexdigit());
112+
}
113+
114+
wheel_allow_legacy(_config, path)
115+
}
116+
107117
pub fn github_release_allow(config: &Config, path: &str) -> bool {
108-
lazy_static! {
109-
static ref REGEX: Regex =
110-
Regex::new("^[^/]*/[^/]*/releases/download/[^/]*/[^/]*$").unwrap();
111-
};
118+
static REGEX: LazyLock<Regex> =
119+
LazyLock::new(|| Regex::new("^[^/]+/[^/]+/releases/download/[^/]+/[^/]+$").unwrap());
112120

113121
if !config
114122
.github_release
@@ -123,10 +131,9 @@ pub fn github_release_allow(config: &Config, path: &str) -> bool {
123131
}
124132

125133
pub fn sjtug_internal_allow(_config: &Config, path: &str) -> bool {
126-
lazy_static! {
127-
static ref REGEX: Regex =
128-
Regex::new("^[^/]*/releases/download/[^/]*/[^/]*.(tar.gz|zip)$").unwrap();
129-
};
134+
static REGEX: LazyLock<Regex> = LazyLock::new(|| {
135+
Regex::new("^[^/]*/releases/download/[^/]*/[^/]*[.](tar[.]gz|zip)$").unwrap()
136+
});
130137

131138
REGEX.is_match(path)
132139
}
@@ -738,6 +745,28 @@ mod tests {
738745
assert!(!flutter_allow(&config, "flutter/coverage/lcov.info"));
739746
}
740747

748+
#[test]
749+
fn test_wheels_allow() {
750+
let config = Config::default();
751+
assert!(wheels_allow(&config, "torch_stable.html"));
752+
assert!(wheels_allow(
753+
&config,
754+
"torch-2.0.0-cp311-cp311-manylinux.whl"
755+
));
756+
assert!(!wheels_allow(
757+
&config,
758+
"torch-2.0.0-cp311-cp311-manylinux.whl#sha256=0123456789abcdef",
759+
));
760+
assert!(wheels_allow(
761+
&config,
762+
"cpu/torch-2.11.0%2Bcpu-cp314-cp314t-manylinux_2_28_x86_64.whl",
763+
));
764+
assert!(wheels_allow(
765+
&config,
766+
"cu130/torch-2.11.0%2Bcu130-cp314-cp314t-manylinux_2_28_x86_64.whl",
767+
));
768+
}
769+
741770
#[test]
742771
fn test_task_override() {
743772
let mut task = Task {

src/storage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn s3_region(s3_config: &S3Config) -> Region {
1515
/// Creates an authenticated S3 client.
1616
///
1717
/// The default credential provider is used.
18-
async fn get_s3_client(s3_config: &S3Config) -> S3Client {
18+
fn get_s3_client(s3_config: &S3Config) -> S3Client {
1919
let s3_builder = aws_sdk_s3::Config::builder()
2020
.region(s3_region(s3_config))
2121
.endpoint_url(s3_config.endpoint.clone())
@@ -47,7 +47,7 @@ pub async fn stream_to_s3(
4747
stream: aws_sdk_s3::primitives::ByteStream,
4848
s3_config: &S3Config,
4949
) -> Result<aws_sdk_s3::operation::put_object::PutObjectOutput> {
50-
let s3_client = get_s3_client(s3_config).await;
50+
let s3_client = get_s3_client(s3_config);
5151

5252
Ok(s3_client
5353
.put_object()
@@ -62,7 +62,7 @@ pub async fn stream_to_s3(
6262
/// Check whether authenticated S3 storage is available.
6363
pub async fn check_s3(s3_config: &S3Config) -> Result<()> {
6464
timeout(Duration::from_secs(1), async move {
65-
let s3_client = get_s3_client(s3_config).await;
65+
let s3_client = get_s3_client(s3_config);
6666

6767
// s3_client
6868
// .list_objects()

0 commit comments

Comments
 (0)