Skip to content

Commit

Permalink
feat: add FileFetcher from CLI (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Dec 13, 2024
1 parent 03c59fe commit 726ca20
Show file tree
Hide file tree
Showing 12 changed files with 2,394 additions and 70 deletions.
560 changes: 510 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"tasks": {
"test": "deno test --allow-read --allow-write --allow-net --allow-env",
"build": "deno task wasmbuild",
"wasmbuild": "deno run -A jsr:@deno/[email protected] --sync --features wasm"
"wasmbuild": "deno run -A jsr:@deno/[email protected] --sync --no-default-features --features wasm"
},
"lint": {
"rules": {
Expand Down
20 changes: 16 additions & 4 deletions rs_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,39 @@ repository = "https://github.com/denoland/deno_cache"
crate-type = ["cdylib", "lib"]

[features]
default = ["file_fetcher"]
file_fetcher = ["async-trait", "base64", "cache_control", "chrono", "data-url", "http"]
wasm = ["console_error_panic_hook", "js-sys", "serde-wasm-bindgen", "wasm-bindgen"]

[dependencies]
async-trait = { version = "0.1.73", optional = true }
base32 = "=0.5.1"
deno_media_type = "0.2.0"
base64 = { version = "0.21.7", optional = true }
boxed_error = "0.2.3"
cache_control = { version = "0.2.0", optional = true }
# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS.
chrono = { version = "0.4", default-features = false, features = ["std"], optional = true }
data-url = { version = "0.3.0", optional = true }
deno_error = { version = "0.5.2", features =["url"] }
deno_media_type = "0.2.2"
deno_path_util = "0.2.2"
http = { version = "1", optional = true }
indexmap = { version = "2.0.0", features = ["serde"] }
log = "0.4.19"
once_cell = "1.18.0"
parking_lot = "0.12.1"
serde = "1.0.183"
serde_json = "1.0.104"
sha2 = "^0.10.0"
sha2 = "0.10.0"
thiserror = "1.0.44"
url = { version = "2.3.1", features = ["serde"] }
url = { version = "2.5.1", features = ["serde"] }

console_error_panic_hook = { version = "0.1.6", optional = true }
js-sys = { version = "=0.3.68", optional = true }
wasm-bindgen = { version = "=0.2.91", optional = true }
serde-wasm-bindgen = { version = "0.6.5", optional = true }
deno_path_util = "0.2.0"

[dev-dependencies]
pretty_assertions = "1.4.0"
tempfile = "3.7.1"
tokio = { version = "1", features = ["rt", "macros"] }
25 changes: 22 additions & 3 deletions rs_lib/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use deno_error::JsError;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
Expand Down Expand Up @@ -32,7 +33,8 @@ impl GlobalToLocalCopy {
}
}

#[derive(Debug, Error)]
#[derive(Debug, Error, JsError)]
#[class(type)]
#[error("Integrity check failed for {}\n\nActual: {}\nExpected: {}", .url, .actual, .expected)]
pub struct ChecksumIntegrityError {
pub url: Url,
Expand All @@ -51,6 +53,23 @@ impl<'a> Checksum<'a> {
pub fn as_str(&self) -> &str {
self.0
}

pub fn check(
&self,
url: &Url,
content: &[u8],
) -> Result<(), Box<ChecksumIntegrityError>> {
let actual = checksum(content);
if self.as_str() != actual {
Err(Box::new(ChecksumIntegrityError {
url: url.clone(),
expected: self.as_str().to_string(),
actual,
}))
} else {
Ok(())
}
}
}

/// Turn provided `url` into a hashed filename.
Expand Down Expand Up @@ -102,7 +121,7 @@ pub enum CacheReadFileError {
ChecksumIntegrity(Box<ChecksumIntegrityError>),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SerializedCachedUrlMetadata {
pub headers: HeadersMap,
pub url: String,
Expand All @@ -111,7 +130,7 @@ pub struct SerializedCachedUrlMetadata {
pub time: Option<u64>,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CacheEntry {
pub metadata: SerializedCachedUrlMetadata,
pub content: Cow<'static, [u8]>,
Expand Down
3 changes: 3 additions & 0 deletions rs_lib/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::collections::HashMap;

use url::Url;

// TODO(ry) HTTP headers are not unique key, value pairs. There may be more than
// one header line with the same key. This should be changed to something like
// Vec<(String, String)>
pub type HeadersMap = HashMap<String, String>;

pub fn base_url_to_filename_parts<'a>(
Expand Down
Loading

0 comments on commit 726ca20

Please sign in to comment.