1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(jsr): ensure proper storage of ahead of time module infos (#21918)

This was incorrectly being stored so the AOT cache for JSR specifiers
wasn't being used. I added a wrapper type to help prevent the API being
used incorrectly.
This commit is contained in:
David Sherret 2024-01-13 20:56:56 -05:00 committed by GitHub
parent 248fb9c946
commit 3298ca06f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 14 deletions

5
cli/cache/mod.rs vendored
View file

@ -96,6 +96,8 @@ pub type LocalLspHttpCache =
deno_cache_dir::LocalLspHttpCache<RealDenoCacheEnv>; deno_cache_dir::LocalLspHttpCache<RealDenoCacheEnv>;
pub use deno_cache_dir::HttpCache; pub use deno_cache_dir::HttpCache;
use self::module_info::ModuleInfoCacheSourceHash;
/// A "wrapper" for the FileFetcher and DiskCache for the Deno CLI that provides /// A "wrapper" for the FileFetcher and DiskCache for the Deno CLI that provides
/// a concise interface to the DENO_DIR when building module graphs. /// a concise interface to the DENO_DIR when building module graphs.
pub struct FetchCacher { pub struct FetchCacher {
@ -280,10 +282,11 @@ impl Loader for FetchCacher {
source: &str, source: &str,
module_info: &deno_graph::ModuleInfo, module_info: &deno_graph::ModuleInfo,
) { ) {
let source_hash = ModuleInfoCacheSourceHash::from_source(source.as_bytes());
let result = self.module_info_cache.set_module_info( let result = self.module_info_cache.set_module_info(
specifier, specifier,
MediaType::from_specifier(specifier), MediaType::from_specifier(specifier),
source, &source_hash,
module_info, module_info,
); );
if let Err(err) = result { if let Err(err) = result {

View file

@ -39,6 +39,22 @@ pub static MODULE_INFO_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration {
on_failure: CacheFailure::InMemory, on_failure: CacheFailure::InMemory,
}; };
pub struct ModuleInfoCacheSourceHash(String);
impl ModuleInfoCacheSourceHash {
pub fn new(hash: u64) -> Self {
Self(hash.to_string())
}
pub fn from_source(source: &[u8]) -> Self {
Self::new(FastInsecureHasher::hash(source))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
/// A cache of `deno_graph::ModuleInfo` objects. Using this leads to a considerable /// A cache of `deno_graph::ModuleInfo` objects. Using this leads to a considerable
/// performance improvement because when it exists we can skip parsing a module for /// performance improvement because when it exists we can skip parsing a module for
/// deno_graph. /// deno_graph.
@ -68,7 +84,7 @@ impl ModuleInfoCache {
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
media_type: MediaType, media_type: MediaType,
expected_source_hash: &str, expected_source_hash: &ModuleInfoCacheSourceHash,
) -> Result<Option<ModuleInfo>, AnyError> { ) -> Result<Option<ModuleInfo>, AnyError> {
let query = SELECT_MODULE_INFO; let query = SELECT_MODULE_INFO;
let res = self.conn.query_row( let res = self.conn.query_row(
@ -76,7 +92,7 @@ impl ModuleInfoCache {
params![ params![
&specifier.as_str(), &specifier.as_str(),
serialize_media_type(media_type), serialize_media_type(media_type),
&expected_source_hash, expected_source_hash.as_str(),
], ],
|row| { |row| {
let module_info: String = row.get(0)?; let module_info: String = row.get(0)?;
@ -91,7 +107,7 @@ impl ModuleInfoCache {
&self, &self,
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
media_type: MediaType, media_type: MediaType,
source_hash: &str, source_hash: &ModuleInfoCacheSourceHash,
module_info: &ModuleInfo, module_info: &ModuleInfo,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let sql = " let sql = "
@ -104,7 +120,7 @@ impl ModuleInfoCache {
params![ params![
specifier.as_str(), specifier.as_str(),
serialize_media_type(media_type), serialize_media_type(media_type),
&source_hash, source_hash.as_str(),
&serde_json::to_string(&module_info)?, &serde_json::to_string(&module_info)?,
], ],
)?; )?;
@ -135,7 +151,7 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> {
media_type: MediaType, media_type: MediaType,
) -> Result<ModuleInfo, deno_ast::Diagnostic> { ) -> Result<ModuleInfo, deno_ast::Diagnostic> {
// attempt to load from the cache // attempt to load from the cache
let source_hash = FastInsecureHasher::hash(source.as_bytes()).to_string(); let source_hash = ModuleInfoCacheSourceHash::from_source(source.as_bytes());
match self.module_info_cache.get_module_info( match self.module_info_cache.get_module_info(
specifier, specifier,
media_type, media_type,
@ -214,7 +230,11 @@ mod test {
ModuleSpecifier::parse("https://localhost/mod2.ts").unwrap(); ModuleSpecifier::parse("https://localhost/mod2.ts").unwrap();
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::JavaScript, "1") .get_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
None None
); );
@ -234,31 +254,52 @@ mod test {
text: "test".to_string(), text: "test".to_string(),
}); });
cache cache
.set_module_info(&specifier1, MediaType::JavaScript, "1", &module_info) .set_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1),
&module_info,
)
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::JavaScript, "1") .get_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
Some(module_info.clone()) Some(module_info.clone())
); );
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier2, MediaType::JavaScript, "1") .get_module_info(
&specifier2,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
None, None,
); );
// different media type // different media type
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::TypeScript, "1") .get_module_info(
&specifier1,
MediaType::TypeScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
None, None,
); );
// different source hash // different source hash
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::JavaScript, "2") .get_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(2)
)
.unwrap(), .unwrap(),
None, None,
); );
@ -269,7 +310,11 @@ mod test {
// should get it // should get it
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::JavaScript, "1") .get_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
Some(module_info) Some(module_info)
); );
@ -280,7 +325,11 @@ mod test {
// should no longer exist // should no longer exist
assert_eq!( assert_eq!(
cache cache
.get_module_info(&specifier1, MediaType::JavaScript, "1") .get_module_info(
&specifier1,
MediaType::JavaScript,
&ModuleInfoCacheSourceHash::new(1)
)
.unwrap(), .unwrap(),
None, None,
); );