Skip to content

Commit

Permalink
feat: add sync cargo feature (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 22, 2025
1 parent ae318d8 commit 5ed9314
Show file tree
Hide file tree
Showing 34 changed files with 182 additions and 113 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
if: contains(matrix.os, 'ubuntu')
run: cargo clippy

- name: Build (sync)
run: cargo build --features sync

- name: Test
run: cargo test

Expand Down
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018-2024 the Deno authors
Copyright (c) 2018-2025 the Deno authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion auth_tokens.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

interface BearerAuthToken {
type: "bearer";
Expand Down
2 changes: 1 addition & 1 deletion auth_tokens_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { AuthTokens } from "./auth_tokens.ts";
import { assertEquals } from "@std/assert";
Expand Down
2 changes: 1 addition & 1 deletion cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import type { LoadResponse } from "@deno/graph";
import type { CacheSetting, FileFetcher } from "./file_fetcher.ts";
Expand Down
2 changes: 1 addition & 1 deletion cache_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { FetchCacher } from "./cache.ts";
import { DenoDir } from "./deno_dir.ts";
Expand Down
2 changes: 1 addition & 1 deletion deno_dir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { isAbsolute, join, resolve } from "@std/path";
import { DiskCache } from "./disk_cache.ts";
Expand Down
2 changes: 1 addition & 1 deletion deno_dir_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { assertEquals, assertThrows } from "@std/assert";
import { DenoDir } from "./deno_dir.ts";
Expand Down
2 changes: 1 addition & 1 deletion deps_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

export { assertEquals, assertRejects } from "@std/assert";
export { createGraph } from "@deno/graph";
Expand Down
2 changes: 1 addition & 1 deletion disk_cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { ensureDir } from "@std/fs/ensure-dir";
import { dirname, isAbsolute, join } from "@std/path";
Expand Down
2 changes: 1 addition & 1 deletion disk_cache_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { assertEquals, assertRejects } from "@std/assert";
import { DiskCache } from "./disk_cache.ts";
Expand Down
2 changes: 1 addition & 1 deletion file_fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { AuthTokens } from "./auth_tokens.ts";
import { fromFileUrl } from "@std/path";
Expand Down
2 changes: 1 addition & 1 deletion file_fetcher_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { DenoDir } from "./deno_dir.ts";
import { assertRejects, createGraph } from "./deps_test.ts";
Expand Down
2 changes: 1 addition & 1 deletion http_cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

import { isAbsolute } from "@std/path";
import { assert } from "./util.ts";
Expand Down
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

/**
* A module which provides a TypeScript implementation of the Deno CLI's cache
Expand Down
1 change: 1 addition & 0 deletions rs_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ crate-type = ["cdylib", "lib"]
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", "sys_traits/wasm"]
sync = []

[dependencies]
async-trait = { version = "0.1.73", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions rs_lib/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ disallowed-methods = [
{ path = "std::path::Path::exists", reason = "File system operations should be done using DenoCacheEnv trait" },
{ path = "std::time::SystemTime::now", reason = "Getting the time should be done using DenoCacheEnv trait" },
]
disallowed-types = [
{ path = "std::sync::Arc", reason = "use crate::sync::MaybeArc instead" },
]
9 changes: 7 additions & 2 deletions rs_lib/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use deno_error::JsError;
use serde::Deserialize;
Expand All @@ -13,6 +13,8 @@ use url::Url;
use crate::common::base_url_to_filename_parts;
use crate::common::checksum;
use crate::common::HeadersMap;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum GlobalToLocalCopy {
Expand Down Expand Up @@ -153,7 +155,10 @@ pub struct HttpCacheItemKey<'a> {
pub(super) file_path: Option<PathBuf>,
}

pub trait HttpCache: Send + Sync + std::fmt::Debug {
#[allow(clippy::disallowed_types)]
pub type HttpCacheRc = crate::sync::MaybeArc<dyn HttpCache>;

pub trait HttpCache: MaybeSend + MaybeSync + std::fmt::Debug {
/// A pre-computed key for looking up items in the cache.
fn cache_item_key<'a>(
&self,
Expand Down
2 changes: 1 addition & 1 deletion rs_lib/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion rs_lib/src/deno_dir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion rs_lib/src/file_fetcher/auth_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion rs_lib/src/file_fetcher/http_util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::time::Duration;
use std::time::SystemTime;
Expand Down
41 changes: 26 additions & 15 deletions rs_lib/src/file_fetcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use boxed_error::Boxed;
use data_url::DataUrl;
Expand All @@ -23,12 +22,14 @@ use thiserror::Error;
use url::Url;

use self::http_util::CacheSemantics;
use crate::cache::HttpCacheRc;
use crate::common::HeadersMap;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;
use crate::CacheEntry;
use crate::CacheReadFileError;
use crate::Checksum;
use crate::ChecksumIntegrityError;
use crate::HttpCache;

mod auth_tokens;
mod http_util;
Expand Down Expand Up @@ -86,12 +87,16 @@ impl FileOrRedirect {
Ok(FileOrRedirect::File(File {
url: url.clone(),
maybe_headers: Some(cache_entry.metadata.headers),
source: Arc::from(cache_entry.content),
#[allow(clippy::disallowed_types)] // ok for source
source: std::sync::Arc::from(cache_entry.content),
}))
}
}
}

#[allow(clippy::disallowed_types)] // ok for source
type FileSource = std::sync::Arc<[u8]>;

/// A structure representing a source file.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct File {
Expand All @@ -100,7 +105,7 @@ pub struct File {
pub url: Url,
pub maybe_headers: Option<HashMap<String, String>>,
/// The source of the file.
pub source: Arc<[u8]>,
pub source: FileSource,
}

impl File {
Expand All @@ -116,7 +121,10 @@ impl File {
}
}

pub trait MemoryFiles: std::fmt::Debug + Send + Sync {
#[allow(clippy::disallowed_types)]
pub type MemoryFilesRc = crate::sync::MaybeArc<dyn MemoryFiles>;

pub trait MemoryFiles: std::fmt::Debug + MaybeSend + MaybeSync {
fn get(&self, url: &Url) -> Option<File>;
}

Expand Down Expand Up @@ -383,7 +391,7 @@ impl From<FetchCachedNoFollowError> for FetchNoFollowError {
}

#[async_trait::async_trait(?Send)]
pub trait HttpClient: std::fmt::Debug + Send + Sync {
pub trait HttpClient: std::fmt::Debug + MaybeSend + MaybeSync {
/// Send a request getting the response.
///
/// The implementation MUST not follow redirects. Return `SendResponse::Redirect`
Expand Down Expand Up @@ -414,7 +422,7 @@ impl BlobStore for NullBlobStore {
}

#[async_trait::async_trait(?Send)]
pub trait BlobStore: std::fmt::Debug + Send + Sync {
pub trait BlobStore: std::fmt::Debug + MaybeSend + MaybeSync {
async fn get(&self, url: &Url) -> std::io::Result<Option<BlobData>>;
}

Expand Down Expand Up @@ -442,9 +450,9 @@ pub struct FileFetcher<
> {
blob_store: TBlobStore,
sys: TSys,
http_cache: Arc<dyn HttpCache>,
http_cache: HttpCacheRc,
http_client: THttpClient,
memory_files: Arc<dyn MemoryFiles>,
memory_files: MemoryFilesRc,
allow_remote: bool,
cache_setting: CacheSetting,
auth_tokens: AuthTokens,
Expand All @@ -459,9 +467,9 @@ impl<
pub fn new(
blob_store: TBlobStore,
sys: TSys,
http_cache: Arc<dyn HttpCache>,
http_cache: HttpCacheRc,
http_client: THttpClient,
memory_files: Arc<dyn MemoryFiles>,
memory_files: MemoryFilesRc,
options: FileFetcherOptions,
) -> Self {
Self {
Expand Down Expand Up @@ -625,7 +633,8 @@ impl<
Ok(File {
url: url.clone(),
maybe_headers: Some(headers),
source: Arc::from(bytes),
#[allow(clippy::disallowed_types)] // ok for source
source: std::sync::Arc::from(bytes),
})
}

Expand All @@ -651,7 +660,8 @@ impl<
Ok(File {
url: url.clone(),
maybe_headers: Some(headers),
source: Arc::from(blob.bytes),
#[allow(clippy::disallowed_types)] // ok for source
source: std::sync::Arc::from(blob.bytes),
})
}

Expand Down Expand Up @@ -735,7 +745,8 @@ impl<
Ok(FileOrRedirect::File(File {
url: url.clone(),
maybe_headers: Some(headers),
source: Arc::from(bytes),
#[allow(clippy::disallowed_types)] // ok for source
source: std::sync::Arc::from(bytes),
}))
}
}
Expand Down
2 changes: 1 addition & 1 deletion rs_lib/src/global/cache_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::io::ErrorKind;
Expand Down
28 changes: 13 additions & 15 deletions rs_lib/src/global/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2024 the Deno authors. MIT license.
// Copyright 2018-2025 the Deno authors. MIT license.

use std::path::PathBuf;
use std::time::Duration;
Expand Down Expand Up @@ -26,9 +26,14 @@ use crate::cache::CacheReadFileError;
use crate::cache::Checksum;
use crate::cache::SerializedCachedUrlMetadata;
use crate::common::HeadersMap;
use crate::sync::MaybeSend;
use crate::sync::MaybeSync;

mod cache_file;

#[allow(clippy::disallowed_types)]
pub type GlobalHttpCacheRc<TSys> = crate::sync::MaybeArc<GlobalHttpCache<TSys>>;

#[derive(Debug)]
pub struct GlobalHttpCache<
Sys: FsCreateDirAll
Expand Down Expand Up @@ -61,18 +66,11 @@ impl<
Self { path, sys }
}

pub fn get_global_cache_location(&self) -> &PathBuf {
pub fn dir_path(&self) -> &PathBuf {
&self.path
}

pub fn get_global_cache_filepath(
&self,
url: &Url,
) -> std::io::Result<PathBuf> {
Ok(self.path.join(url_to_filename(url)?))
}

fn get_cache_filepath(&self, url: &Url) -> std::io::Result<PathBuf> {
pub fn local_path_for_url(&self, url: &Url) -> std::io::Result<PathBuf> {
Ok(self.path.join(url_to_filename(url)?))
}

Expand All @@ -96,8 +94,8 @@ impl<
+ SystemRandom
+ SystemTimeNow
+ std::fmt::Debug
+ Send
+ Sync
+ MaybeSend
+ MaybeSync
+ Clone,
> HttpCache for GlobalHttpCache<TSys>
{
Expand All @@ -109,12 +107,12 @@ impl<
#[cfg(debug_assertions)]
is_local_key: false,
url,
file_path: Some(self.get_cache_filepath(url)?),
file_path: Some(self.local_path_for_url(url)?),
})
}

fn contains(&self, url: &Url) -> bool {
let Ok(cache_filepath) = self.get_cache_filepath(url) else {
let Ok(cache_filepath) = self.local_path_for_url(url) else {
return false;
};
self.sys.fs_is_file(&cache_filepath).unwrap_or(false)
Expand Down Expand Up @@ -143,7 +141,7 @@ impl<
headers: HeadersMap,
content: &[u8],
) -> std::io::Result<()> {
let cache_filepath = self.get_cache_filepath(url)?;
let cache_filepath = self.local_path_for_url(url)?;
cache_file::write(
&self.sys,
&cache_filepath,
Expand Down
Loading

0 comments on commit 5ed9314

Please sign in to comment.