2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::anyhow::anyhow;
|
2022-07-12 18:58:39 -04:00
|
|
|
use deno_core::error::AnyError;
|
2024-08-08 05:41:30 -04:00
|
|
|
use deno_core::unsync::sync::AtomicFlag;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
use super::DiskCache;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
/// The cache that stores previously emitted files.
|
|
|
|
pub struct EmitCache {
|
|
|
|
disk_cache: DiskCache,
|
2024-08-08 05:41:30 -04:00
|
|
|
emit_failed_flag: AtomicFlag,
|
2024-08-26 11:43:57 -04:00
|
|
|
file_serializer: EmitFileSerializer,
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
impl EmitCache {
|
|
|
|
pub fn new(disk_cache: DiskCache) -> Self {
|
|
|
|
Self {
|
|
|
|
disk_cache,
|
2024-08-08 05:41:30 -04:00
|
|
|
emit_failed_flag: Default::default(),
|
2024-08-26 11:43:57 -04:00
|
|
|
file_serializer: EmitFileSerializer {
|
|
|
|
cli_version: crate::version::DENO_VERSION_INFO.deno,
|
|
|
|
},
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the emitted code with embedded sourcemap from the cache.
|
|
|
|
///
|
|
|
|
/// The expected source hash is used in order to verify
|
|
|
|
/// that you're getting a value from the cache that is
|
|
|
|
/// for the provided source.
|
|
|
|
///
|
|
|
|
/// Cached emits from previous CLI releases will not be returned
|
|
|
|
/// or emits that do not match the source.
|
|
|
|
pub fn get_emit_code(
|
2022-07-12 18:58:39 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-11-29 12:43:54 -05:00
|
|
|
expected_source_hash: u64,
|
2024-06-05 11:04:16 -04:00
|
|
|
) -> Option<Vec<u8>> {
|
2022-07-19 11:58:18 -04:00
|
|
|
let emit_filename = self.get_emit_filename(specifier)?;
|
2024-08-26 11:43:57 -04:00
|
|
|
let bytes = self.disk_cache.get(&emit_filename).ok()?;
|
|
|
|
self
|
|
|
|
.file_serializer
|
|
|
|
.deserialize(bytes, expected_source_hash)
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
/// Sets the emit code in the cache.
|
|
|
|
pub fn set_emit_code(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source_hash: u64,
|
2024-06-05 11:04:16 -04:00
|
|
|
code: &[u8],
|
2022-07-19 11:58:18 -04:00
|
|
|
) {
|
|
|
|
if let Err(err) = self.set_emit_code_result(specifier, source_hash, code) {
|
2024-08-08 05:41:30 -04:00
|
|
|
// might error in cases such as a readonly file system
|
|
|
|
log::debug!("Error saving emit data ({}): {}", specifier, err);
|
|
|
|
// assume the cache can't be written to and disable caching to it
|
|
|
|
self.emit_failed_flag.raise();
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
fn set_emit_code_result(
|
2022-07-12 18:58:39 -04:00
|
|
|
&self,
|
2022-07-19 11:58:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source_hash: u64,
|
2024-06-05 11:04:16 -04:00
|
|
|
code: &[u8],
|
2022-07-12 18:58:39 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2024-08-08 05:41:30 -04:00
|
|
|
if self.emit_failed_flag.is_raised() {
|
|
|
|
log::debug!("Skipped emit cache save of {}", specifier);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
let emit_filename = self
|
|
|
|
.get_emit_filename(specifier)
|
|
|
|
.ok_or_else(|| anyhow!("Could not get emit filename."))?;
|
2024-08-26 11:43:57 -04:00
|
|
|
let cache_data = self.file_serializer.serialize(code, source_hash);
|
|
|
|
self.disk_cache.set(&emit_filename, &cache_data)?;
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2024-08-26 11:43:57 -04:00
|
|
|
/// Gets the filepath which stores the emit.
|
|
|
|
pub fn get_emit_filepath(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<PathBuf> {
|
|
|
|
Some(
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.location
|
|
|
|
.join(self.get_emit_filename(specifier)?),
|
|
|
|
)
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_emit_filename(&self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "js")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 11:43:57 -04:00
|
|
|
const LAST_LINE_PREFIX: &str = "\n// denoCacheMetadata=";
|
|
|
|
|
|
|
|
struct EmitFileSerializer {
|
|
|
|
cli_version: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EmitFileSerializer {
|
|
|
|
pub fn deserialize(
|
|
|
|
&self,
|
|
|
|
mut bytes: Vec<u8>,
|
|
|
|
expected_source_hash: u64,
|
|
|
|
) -> Option<Vec<u8>> {
|
|
|
|
let last_newline_index = bytes.iter().rposition(|&b| b == b'\n')?;
|
|
|
|
let (content, last_line) = bytes.split_at(last_newline_index);
|
|
|
|
let hashes = last_line.strip_prefix(LAST_LINE_PREFIX.as_bytes())?;
|
|
|
|
let hashes = String::from_utf8_lossy(hashes);
|
|
|
|
let (source_hash, emit_hash) = hashes.split_once(',')?;
|
|
|
|
|
|
|
|
// verify the meta data file is for this source and CLI version
|
|
|
|
let source_hash = source_hash.parse::<u64>().ok()?;
|
|
|
|
if source_hash != expected_source_hash {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let emit_hash = emit_hash.parse::<u64>().ok()?;
|
|
|
|
// prevent using an emit from a different cli version or emits that were tampered with
|
|
|
|
if emit_hash != self.compute_emit_hash(content) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything looks good, truncate and return it
|
|
|
|
bytes.truncate(content.len());
|
|
|
|
Some(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize(&self, code: &[u8], source_hash: u64) -> Vec<u8> {
|
|
|
|
let source_hash = source_hash.to_string();
|
|
|
|
let emit_hash = self.compute_emit_hash(code).to_string();
|
|
|
|
let capacity = code.len()
|
|
|
|
+ LAST_LINE_PREFIX.len()
|
|
|
|
+ source_hash.len()
|
|
|
|
+ 1
|
|
|
|
+ emit_hash.len();
|
|
|
|
let mut cache_data = Vec::with_capacity(capacity);
|
|
|
|
cache_data.extend(code);
|
|
|
|
cache_data.extend(LAST_LINE_PREFIX.as_bytes());
|
|
|
|
cache_data.extend(source_hash.as_bytes());
|
|
|
|
cache_data.push(b',');
|
|
|
|
cache_data.extend(emit_hash.as_bytes());
|
|
|
|
debug_assert_eq!(cache_data.len(), capacity);
|
|
|
|
cache_data
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_emit_hash(&self, bytes: &[u8]) -> u64 {
|
|
|
|
// it's ok to use an insecure hash here because
|
|
|
|
// if someone can change the emit source then they
|
|
|
|
// can also change the version hash
|
|
|
|
crate::cache::FastInsecureHasher::new_without_deno_version() // use cli_version property instead
|
|
|
|
.write(bytes)
|
|
|
|
// emit should not be re-used between cli versions
|
|
|
|
.write_str(self.cli_version)
|
|
|
|
.finish()
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use test_util::TempDir;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn emit_cache_general_use() {
|
|
|
|
let temp_dir = TempDir::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let disk_cache = DiskCache::new(temp_dir.path().as_path());
|
2022-07-19 11:58:18 -04:00
|
|
|
let cache = EmitCache {
|
|
|
|
disk_cache: disk_cache.clone(),
|
2024-08-26 11:43:57 -04:00
|
|
|
file_serializer: EmitFileSerializer {
|
|
|
|
cli_version: "1.0.0",
|
|
|
|
},
|
2024-08-08 05:41:30 -04:00
|
|
|
emit_failed_flag: Default::default(),
|
2022-07-19 11:58:18 -04:00
|
|
|
};
|
2024-06-05 11:04:16 -04:00
|
|
|
let to_string =
|
|
|
|
|bytes: Vec<u8>| -> String { String::from_utf8(bytes).unwrap() };
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
let specifier1 =
|
|
|
|
ModuleSpecifier::from_file_path(temp_dir.path().join("file1.ts"))
|
|
|
|
.unwrap();
|
|
|
|
let specifier2 =
|
|
|
|
ModuleSpecifier::from_file_path(temp_dir.path().join("file2.ts"))
|
|
|
|
.unwrap();
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 1), None);
|
2022-07-19 11:58:18 -04:00
|
|
|
let emit_code1 = "text1".to_string();
|
|
|
|
let emit_code2 = "text2".to_string();
|
2024-06-05 11:04:16 -04:00
|
|
|
cache.set_emit_code(&specifier1, 10, emit_code1.as_bytes());
|
|
|
|
cache.set_emit_code(&specifier2, 2, emit_code2.as_bytes());
|
2022-07-19 11:58:18 -04:00
|
|
|
// providing the incorrect source hash
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
|
2022-07-19 11:58:18 -04:00
|
|
|
// providing the correct source hash
|
|
|
|
assert_eq!(
|
2024-06-05 11:04:16 -04:00
|
|
|
cache.get_emit_code(&specifier1, 10).map(to_string),
|
2022-07-19 11:58:18 -04:00
|
|
|
Some(emit_code1.clone()),
|
|
|
|
);
|
2024-06-05 11:04:16 -04:00
|
|
|
assert_eq!(
|
|
|
|
cache.get_emit_code(&specifier2, 2).map(to_string),
|
|
|
|
Some(emit_code2)
|
|
|
|
);
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
// try changing the cli version (should not load previous ones)
|
|
|
|
let cache = EmitCache {
|
|
|
|
disk_cache: disk_cache.clone(),
|
2024-08-26 11:43:57 -04:00
|
|
|
file_serializer: EmitFileSerializer {
|
|
|
|
cli_version: "2.0.0",
|
|
|
|
},
|
2024-08-08 05:41:30 -04:00
|
|
|
emit_failed_flag: Default::default(),
|
2022-07-19 11:58:18 -04:00
|
|
|
};
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 10), None);
|
2024-06-05 11:04:16 -04:00
|
|
|
cache.set_emit_code(&specifier1, 5, emit_code1.as_bytes());
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
// recreating the cache should still load the data because the CLI version is the same
|
|
|
|
let cache = EmitCache {
|
|
|
|
disk_cache,
|
2024-08-26 11:43:57 -04:00
|
|
|
file_serializer: EmitFileSerializer {
|
|
|
|
cli_version: "2.0.0",
|
|
|
|
},
|
2024-08-08 05:41:30 -04:00
|
|
|
emit_failed_flag: Default::default(),
|
2022-07-19 11:58:18 -04:00
|
|
|
};
|
2024-06-05 11:04:16 -04:00
|
|
|
assert_eq!(
|
|
|
|
cache.get_emit_code(&specifier1, 5).map(to_string),
|
|
|
|
Some(emit_code1)
|
|
|
|
);
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
// adding when already exists should not cause issue
|
|
|
|
let emit_code3 = "asdf".to_string();
|
2024-06-05 11:04:16 -04:00
|
|
|
cache.set_emit_code(&specifier1, 20, emit_code3.as_bytes());
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
|
2024-06-05 11:04:16 -04:00
|
|
|
assert_eq!(
|
|
|
|
cache.get_emit_code(&specifier1, 20).map(to_string),
|
|
|
|
Some(emit_code3)
|
|
|
|
);
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|