From 5ed93144d35e561313013ba3f07428bcf73bbc0d Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 21 Jan 2025 19:18:15 -0500 Subject: [PATCH] feat: add sync cargo feature (#72) --- .github/workflows/ci.yml | 3 ++ LICENCE | 2 +- auth_tokens.ts | 2 +- auth_tokens_test.ts | 2 +- cache.ts | 2 +- cache_test.ts | 2 +- deno_dir.ts | 2 +- deno_dir_test.ts | 2 +- deps_test.ts | 2 +- disk_cache.ts | 2 +- disk_cache_test.ts | 2 +- file_fetcher.ts | 2 +- file_fetcher_test.ts | 2 +- http_cache.ts | 2 +- mod.ts | 2 +- rs_lib/Cargo.toml | 1 + rs_lib/clippy.toml | 3 ++ rs_lib/src/cache.rs | 9 +++- rs_lib/src/common.rs | 2 +- rs_lib/src/deno_dir.rs | 2 +- rs_lib/src/file_fetcher/auth_tokens.rs | 2 +- rs_lib/src/file_fetcher/http_util.rs | 2 +- rs_lib/src/file_fetcher/mod.rs | 41 ++++++++++------ rs_lib/src/global/cache_file.rs | 2 +- rs_lib/src/global/mod.rs | 28 +++++------ rs_lib/src/lib.rs | 10 ++-- rs_lib/src/local.rs | 67 +++++++++++++++----------- rs_lib/src/memory.rs | 40 ++++++++------- rs_lib/src/npm.rs | 2 +- rs_lib/src/sync.rs | 29 +++++++++++ rs_lib/tests/file_fetcher_test.rs | 8 +-- rs_lib/tests/integration_test.rs | 12 ++--- test.ts | 2 +- util.ts | 2 +- 34 files changed, 182 insertions(+), 113 deletions(-) create mode 100644 rs_lib/src/sync.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3454d02..a3826b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/LICENCE b/LICENCE index edf73ca..2429503 100644 --- a/LICENCE +++ b/LICENCE @@ -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 diff --git a/auth_tokens.ts b/auth_tokens.ts index b7f9aad..6a4293e 100644 --- a/auth_tokens.ts +++ b/auth_tokens.ts @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. interface BearerAuthToken { type: "bearer"; diff --git a/auth_tokens_test.ts b/auth_tokens_test.ts index ba6c601..1fb2b8d 100644 --- a/auth_tokens_test.ts +++ b/auth_tokens_test.ts @@ -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"; diff --git a/cache.ts b/cache.ts index 4bdc082..9fe4769 100644 --- a/cache.ts +++ b/cache.ts @@ -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"; diff --git a/cache_test.ts b/cache_test.ts index e57715d..0b337bb 100644 --- a/cache_test.ts +++ b/cache_test.ts @@ -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"; diff --git a/deno_dir.ts b/deno_dir.ts index 9715ea1..a893d66 100644 --- a/deno_dir.ts +++ b/deno_dir.ts @@ -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"; diff --git a/deno_dir_test.ts b/deno_dir_test.ts index b01f9e6..1d13c51 100644 --- a/deno_dir_test.ts +++ b/deno_dir_test.ts @@ -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"; diff --git a/deps_test.ts b/deps_test.ts index fcbf2bb..b280bae 100644 --- a/deps_test.ts +++ b/deps_test.ts @@ -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"; diff --git a/disk_cache.ts b/disk_cache.ts index 22970a4..e969e5e 100644 --- a/disk_cache.ts +++ b/disk_cache.ts @@ -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"; diff --git a/disk_cache_test.ts b/disk_cache_test.ts index 25bc96b..3d4ea94 100644 --- a/disk_cache_test.ts +++ b/disk_cache_test.ts @@ -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"; diff --git a/file_fetcher.ts b/file_fetcher.ts index 21edcec..7a51758 100644 --- a/file_fetcher.ts +++ b/file_fetcher.ts @@ -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"; diff --git a/file_fetcher_test.ts b/file_fetcher_test.ts index ced44c0..2b8effa 100644 --- a/file_fetcher_test.ts +++ b/file_fetcher_test.ts @@ -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"; diff --git a/http_cache.ts b/http_cache.ts index 35ab3a9..bb9ac56 100644 --- a/http_cache.ts +++ b/http_cache.ts @@ -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"; diff --git a/mod.ts b/mod.ts index 7f7c649..d7f2ea0 100644 --- a/mod.ts +++ b/mod.ts @@ -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 diff --git a/rs_lib/Cargo.toml b/rs_lib/Cargo.toml index 7089e6f..91a8636 100644 --- a/rs_lib/Cargo.toml +++ b/rs_lib/Cargo.toml @@ -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 } diff --git a/rs_lib/clippy.toml b/rs_lib/clippy.toml index 5a10568..c5ad63d 100644 --- a/rs_lib/clippy.toml +++ b/rs_lib/clippy.toml @@ -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" }, +] diff --git a/rs_lib/src/cache.rs b/rs_lib/src/cache.rs index 4bbfd9a..2ace3fe 100644 --- a/rs_lib/src/cache.rs +++ b/rs_lib/src/cache.rs @@ -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; @@ -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 { @@ -153,7 +155,10 @@ pub struct HttpCacheItemKey<'a> { pub(super) file_path: Option, } -pub trait HttpCache: Send + Sync + std::fmt::Debug { +#[allow(clippy::disallowed_types)] +pub type HttpCacheRc = crate::sync::MaybeArc; + +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, diff --git a/rs_lib/src/common.rs b/rs_lib/src/common.rs index a9f3aaf..bcffd16 100644 --- a/rs_lib/src/common.rs +++ b/rs_lib/src/common.rs @@ -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; diff --git a/rs_lib/src/deno_dir.rs b/rs_lib/src/deno_dir.rs index eed62b9..ce8db6a 100644 --- a/rs_lib/src/deno_dir.rs +++ b/rs_lib/src/deno_dir.rs @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. use std::path::PathBuf; diff --git a/rs_lib/src/file_fetcher/auth_tokens.rs b/rs_lib/src/file_fetcher/auth_tokens.rs index cf3ab5e..6f2a8ed 100644 --- a/rs_lib/src/file_fetcher/auth_tokens.rs +++ b/rs_lib/src/file_fetcher/auth_tokens.rs @@ -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; diff --git a/rs_lib/src/file_fetcher/http_util.rs b/rs_lib/src/file_fetcher/http_util.rs index 9a955b4..91df768 100644 --- a/rs_lib/src/file_fetcher/http_util.rs +++ b/rs_lib/src/file_fetcher/http_util.rs @@ -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; diff --git a/rs_lib/src/file_fetcher/mod.rs b/rs_lib/src/file_fetcher/mod.rs index f7f968c..43dec22 100644 --- a/rs_lib/src/file_fetcher/mod.rs +++ b/rs_lib/src/file_fetcher/mod.rs @@ -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; @@ -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; @@ -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 { @@ -100,7 +105,7 @@ pub struct File { pub url: Url, pub maybe_headers: Option>, /// The source of the file. - pub source: Arc<[u8]>, + pub source: FileSource, } impl File { @@ -116,7 +121,10 @@ impl File { } } -pub trait MemoryFiles: std::fmt::Debug + Send + Sync { +#[allow(clippy::disallowed_types)] +pub type MemoryFilesRc = crate::sync::MaybeArc; + +pub trait MemoryFiles: std::fmt::Debug + MaybeSend + MaybeSync { fn get(&self, url: &Url) -> Option; } @@ -383,7 +391,7 @@ impl From 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` @@ -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>; } @@ -442,9 +450,9 @@ pub struct FileFetcher< > { blob_store: TBlobStore, sys: TSys, - http_cache: Arc, + http_cache: HttpCacheRc, http_client: THttpClient, - memory_files: Arc, + memory_files: MemoryFilesRc, allow_remote: bool, cache_setting: CacheSetting, auth_tokens: AuthTokens, @@ -459,9 +467,9 @@ impl< pub fn new( blob_store: TBlobStore, sys: TSys, - http_cache: Arc, + http_cache: HttpCacheRc, http_client: THttpClient, - memory_files: Arc, + memory_files: MemoryFilesRc, options: FileFetcherOptions, ) -> Self { Self { @@ -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), }) } @@ -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), }) } @@ -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), })) } } diff --git a/rs_lib/src/global/cache_file.rs b/rs_lib/src/global/cache_file.rs index e562068..8672050 100644 --- a/rs_lib/src/global/cache_file.rs +++ b/rs_lib/src/global/cache_file.rs @@ -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; diff --git a/rs_lib/src/global/mod.rs b/rs_lib/src/global/mod.rs index 607093e..5e35420 100644 --- a/rs_lib/src/global/mod.rs +++ b/rs_lib/src/global/mod.rs @@ -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; @@ -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 = crate::sync::MaybeArc>; + #[derive(Debug)] pub struct GlobalHttpCache< Sys: FsCreateDirAll @@ -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 { - Ok(self.path.join(url_to_filename(url)?)) - } - - fn get_cache_filepath(&self, url: &Url) -> std::io::Result { + pub fn local_path_for_url(&self, url: &Url) -> std::io::Result { Ok(self.path.join(url_to_filename(url)?)) } @@ -96,8 +94,8 @@ impl< + SystemRandom + SystemTimeNow + std::fmt::Debug - + Send - + Sync + + MaybeSend + + MaybeSync + Clone, > HttpCache for GlobalHttpCache { @@ -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) @@ -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, diff --git a/rs_lib/src/lib.rs b/rs_lib/src/lib.rs index cc5a1ca..45b152b 100644 --- a/rs_lib/src/lib.rs +++ b/rs_lib/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. mod cache; mod common; @@ -9,6 +9,7 @@ mod global; mod local; pub mod memory; pub mod npm; +mod sync; /// Permissions used to save a file in the disk caches. pub const CACHE_PERM: u32 = 0o644; @@ -21,12 +22,15 @@ pub use cache::ChecksumIntegrityError; pub use cache::GlobalToLocalCopy; pub use cache::HttpCache; pub use cache::HttpCacheItemKey; +pub use cache::HttpCacheRc; pub use cache::SerializedCachedUrlMetadata; pub use common::HeadersMap; pub use deno_dir::resolve_deno_dir; pub use deno_dir::DenoDirResolutionError; pub use global::GlobalHttpCache; +pub use global::GlobalHttpCacheRc; pub use local::LocalHttpCache; +pub use local::LocalHttpCacheRc; pub use local::LocalLspHttpCache; #[cfg(feature = "wasm")] @@ -34,7 +38,6 @@ pub mod wasm { use std::collections::HashMap; use std::io::ErrorKind; use std::path::PathBuf; - use std::sync::Arc; use js_sys::Object; use js_sys::Reflect; @@ -49,6 +52,7 @@ pub mod wasm { use crate::cache::GlobalToLocalCopy; use crate::common::HeadersMap; use crate::deno_dir; + use crate::sync::new_rc; use crate::CacheReadFileError; use crate::Checksum; use crate::HttpCache; @@ -128,7 +132,7 @@ pub mod wasm { crate::GlobalHttpCache::new(RealSys, wasm_string_to_path(global_path)); let local = crate::LocalHttpCache::new( wasm_string_to_path(local_path), - Arc::new(global), + new_rc(global), if allow_global_to_local_copy { GlobalToLocalCopy::Allow } else { diff --git a/rs_lib/src/local.rs b/rs_lib/src/local.rs index e418d62..74a2a27 100644 --- a/rs_lib/src/local.rs +++ b/rs_lib/src/local.rs @@ -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::BTreeMap; @@ -7,7 +7,6 @@ use std::collections::HashSet; use std::io::ErrorKind; use std::path::Path; use std::path::PathBuf; -use std::sync::Arc; use std::time::SystemTime; use deno_media_type::MediaType; @@ -29,13 +28,15 @@ use url::Url; use crate::cache::CacheEntry; use crate::cache::CacheReadFileError; use crate::cache::GlobalToLocalCopy; +use crate::global::GlobalHttpCacheRc; +use crate::sync::MaybeSend; +use crate::sync::MaybeSync; use crate::SerializedCachedUrlMetadata; use crate::CACHE_PERM; use super::common::base_url_to_filename_parts; use super::common::checksum; use super::common::HeadersMap; -use super::global::GlobalHttpCache; use super::Checksum; use super::HttpCache; use super::HttpCacheItemKey; @@ -53,10 +54,10 @@ pub struct LocalLspHttpCache< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > { cache: LocalHttpCache, } @@ -71,13 +72,13 @@ impl< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > LocalLspHttpCache { - pub fn new(path: PathBuf, global_cache: Arc>) -> Self { + pub fn new(path: PathBuf, global_cache: GlobalHttpCacheRc) -> Self { #[cfg(not(feature = "wasm"))] assert!(path.is_absolute()); let manifest = LocalCacheManifest::new_for_lsp( @@ -194,10 +195,10 @@ impl< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > HttpCache for LocalLspHttpCache { fn cache_item_key<'a>( @@ -250,6 +251,9 @@ impl< } } +#[allow(clippy::disallowed_types)] +pub type LocalHttpCacheRc = crate::sync::MaybeArc>; + #[derive(Debug)] pub struct LocalHttpCache< TSys: FsCreateDirAll @@ -261,14 +265,14 @@ pub struct LocalHttpCache< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > { path: PathBuf, manifest: LocalCacheManifest, - global_cache: Arc>, + global_cache: GlobalHttpCacheRc, allow_global_to_local: GlobalToLocalCopy, } @@ -282,15 +286,15 @@ impl< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > LocalHttpCache { pub fn new( path: PathBuf, - global_cache: Arc>, + global_cache: GlobalHttpCacheRc, allow_global_to_local: GlobalToLocalCopy, ) -> Self { #[cfg(not(feature = "wasm"))] @@ -322,7 +326,7 @@ impl< let local_path = url_to_local_sub_path(url, None)?; if self .env() - .fs_is_file_no_err(&local_path.as_path_from_root(&self.path)) + .fs_is_file_no_err(local_path.as_path_from_root(&self.path)) { return Ok(Some(Default::default())); } @@ -361,10 +365,10 @@ impl< + ThreadSleep + SystemRandom + SystemTimeNow - + Clone + + MaybeSend + + MaybeSync + std::fmt::Debug - + Send - + Sync, + + Clone, > HttpCache for LocalHttpCache { fn cache_item_key<'a>( @@ -399,7 +403,7 @@ impl< url_to_local_sub_path(key.url, headers_content_type(&headers))?; if let Ok(metadata) = self .env() - .fs_metadata(&local_path.as_path_from_root(&self.path)) + .fs_metadata(local_path.as_path_from_root(&self.path)) { if let Ok(modified_time) = metadata.modified() { return Ok(Some(modified_time)); @@ -723,7 +727,10 @@ struct LocalCacheManifest< + FsRemoveFile + FsRename + ThreadSleep - + SystemRandom, + + SystemRandom + + MaybeSend + + MaybeSync + + std::fmt::Debug, > { sys: Sys, file_path: PathBuf, @@ -738,7 +745,11 @@ impl< + FsRemoveFile + FsRename + ThreadSleep - + SystemRandom, + + SystemRandom + + MaybeSend + + MaybeSync + + std::fmt::Debug + + Clone, > LocalCacheManifest { pub fn new(file_path: PathBuf, sys: Sys) -> Self { diff --git a/rs_lib/src/memory.rs b/rs_lib/src/memory.rs index ee3c7d0..6a00a12 100644 --- a/rs_lib/src/memory.rs +++ b/rs_lib/src/memory.rs @@ -1,14 +1,15 @@ -// 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::sync::Arc; -use std::time::SystemTime; use std::time::UNIX_EPOCH; use parking_lot::Mutex; +use sys_traits::SystemTimeNow; use url::Url; +use crate::sync::MaybeSend; +use crate::sync::MaybeSync; use crate::CacheEntry; use crate::CacheReadFileError; use crate::Checksum; @@ -17,35 +18,36 @@ use crate::HttpCache; use crate::HttpCacheItemKey; use crate::SerializedCachedUrlMetadata; -pub trait MemoryHttpCacheClock: std::fmt::Debug + Send + Sync { - fn time_now(&self) -> SystemTime; -} - +#[cfg(not(target_arch = "wasm32"))] #[derive(Debug)] pub struct MemoryHttpCacheSystemTimeClock; -impl MemoryHttpCacheClock for MemoryHttpCacheSystemTimeClock { - fn time_now(&self) -> SystemTime { +#[cfg(not(target_arch = "wasm32"))] +impl sys_traits::SystemTimeNow for MemoryHttpCacheSystemTimeClock { + fn sys_time_now(&self) -> std::time::SystemTime { #[allow(clippy::disallowed_methods)] - SystemTime::now() + std::time::SystemTime::now() } } /// A simple in-memory cache mostly useful for testing. #[derive(Debug)] -pub struct MemoryHttpCache { +pub struct MemoryHttpCache { cache: Mutex>, - clock: Arc, + clock: TSys, } -impl Default for MemoryHttpCache { +#[cfg(not(target_arch = "wasm32"))] +impl Default for MemoryHttpCache { fn default() -> Self { - Self::new(Arc::new(MemoryHttpCacheSystemTimeClock)) + Self::new(MemoryHttpCacheSystemTimeClock) } } -impl MemoryHttpCache { - pub fn new(clock: Arc) -> Self { +impl + MemoryHttpCache +{ + pub fn new(clock: TSys) -> Self { Self { cache: Mutex::new(HashMap::new()), clock, @@ -53,7 +55,9 @@ impl MemoryHttpCache { } } -impl HttpCache for MemoryHttpCache { +impl HttpCache + for MemoryHttpCache +{ fn cache_item_key<'a>( &self, url: &'a Url, @@ -85,7 +89,7 @@ impl HttpCache for MemoryHttpCache { time: Some( self .clock - .time_now() + .sys_time_now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs(), diff --git a/rs_lib/src/npm.rs b/rs_lib/src/npm.rs index 3d9eed4..70d5803 100644 --- a/rs_lib/src/npm.rs +++ b/rs_lib/src/npm.rs @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. use std::io::ErrorKind; use std::path::Path; diff --git a/rs_lib/src/sync.rs b/rs_lib/src/sync.rs new file mode 100644 index 0000000..18d4c96 --- /dev/null +++ b/rs_lib/src/sync.rs @@ -0,0 +1,29 @@ +// Copyright 2018-2025 the Deno authors. MIT license. + +pub use inner::*; + +#[cfg(feature = "sync")] +mod inner { + #![allow(clippy::disallowed_types)] + + pub use core::marker::Send as MaybeSend; + pub use core::marker::Sync as MaybeSync; + pub use std::sync::Arc as MaybeArc; +} + +#[cfg(not(feature = "sync"))] +mod inner { + pub trait MaybeSync {} + impl MaybeSync for T where T: ?Sized {} + pub trait MaybeSend {} + impl MaybeSend for T where T: ?Sized {} + + pub use std::rc::Rc as MaybeArc; +} + +#[allow(clippy::disallowed_types)] +#[allow(dead_code)] // not used for all features +#[inline] +pub fn new_rc(value: T) -> MaybeArc { + MaybeArc::new(value) +} diff --git a/rs_lib/tests/file_fetcher_test.rs b/rs_lib/tests/file_fetcher_test.rs index 9a297f3..ec60c5a 100644 --- a/rs_lib/tests/file_fetcher_test.rs +++ b/rs_lib/tests/file_fetcher_test.rs @@ -1,8 +1,8 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. #![allow(clippy::disallowed_methods)] -use std::sync::Arc; +use std::rc::Rc; use deno_cache_dir::file_fetcher::AuthTokens; use deno_cache_dir::file_fetcher::CacheSetting; @@ -58,9 +58,9 @@ fn create_file_fetcher( FileFetcher::new( NullBlobStore, InMemorySys::default(), - Arc::new(MemoryHttpCache::default()), + Rc::new(MemoryHttpCache::default()), client, - Arc::new(NullMemoryFiles), + Rc::new(NullMemoryFiles), FileFetcherOptions { allow_remote: true, cache_setting: CacheSetting::Use, diff --git a/rs_lib/tests/integration_test.rs b/rs_lib/tests/integration_test.rs index 1989f8e..ee90a6b 100644 --- a/rs_lib/tests/integration_test.rs +++ b/rs_lib/tests/integration_test.rs @@ -1,10 +1,10 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. #![allow(clippy::disallowed_methods)] use std::collections::HashMap; use std::path::Path; -use std::sync::Arc; +use std::rc::Rc; use deno_cache_dir::CacheReadFileError; use deno_cache_dir::Checksum; @@ -33,11 +33,11 @@ fn test_global_create_cache() { // https://github.com/denoland/deno/issues/5688 let sys = RealSys; let cache = GlobalHttpCache::new(sys, cache_path.clone()); - assert!(!cache.get_global_cache_location().exists()); + assert!(!cache.dir_path().exists()); let url = Url::parse("http://example.com/foo/bar.js").unwrap(); cache.set(&url, Default::default(), b"hello world").unwrap(); assert!(cache_path.is_dir()); - assert!(cache.get_global_cache_filepath(&url).unwrap().is_file()); + assert!(cache.local_path_for_url(&url).unwrap().is_file()); } #[test] @@ -105,7 +105,7 @@ fn test_local_global_cache() { let local_cache_path = temp_dir.path().join("local"); let sys = RealSys; let global_cache = - Arc::new(GlobalHttpCache::new(sys, global_cache_path.clone())); + Rc::new(GlobalHttpCache::new(sys, global_cache_path.clone())); let local_cache = LocalHttpCache::new( local_cache_path.clone(), global_cache.clone(), @@ -525,7 +525,7 @@ fn test_lsp_local_cache() { let local_cache_path = temp_dir.path().join("local"); let sys = RealSys; let global_cache = - Arc::new(GlobalHttpCache::new(sys, global_cache_path.to_path_buf())); + Rc::new(GlobalHttpCache::new(sys, global_cache_path.to_path_buf())); let local_cache = LocalHttpCache::new( local_cache_path.to_path_buf(), global_cache.clone(), diff --git a/test.ts b/test.ts index 95b9b6f..7d4cd99 100644 --- a/test.ts +++ b/test.ts @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. import { assertEquals } from "@std/assert"; import { createGraph } from "@deno/graph"; diff --git a/util.ts b/util.ts index 28fc046..993492f 100644 --- a/util.ts +++ b/util.ts @@ -1,4 +1,4 @@ -// Copyright 2018-2024 the Deno authors. MIT license. +// Copyright 2018-2025 the Deno authors. MIT license. export const CACHE_PERM = 0o644;