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;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
use super::DiskCache;
|
|
|
|
use super::FastInsecureHasher;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
struct EmitMetadata {
|
2022-07-12 18:58:39 -04:00
|
|
|
pub source_hash: String,
|
2022-07-19 11:58:18 -04:00
|
|
|
pub emit_hash: String,
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
/// The cache that stores previously emitted files.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct EmitCache {
|
|
|
|
disk_cache: DiskCache,
|
2023-03-23 18:27:58 -04:00
|
|
|
cli_version: &'static str,
|
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,
|
|
|
|
cli_version: crate::version::deno(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2022-07-19 11:58:18 -04:00
|
|
|
) -> Option<String> {
|
|
|
|
let meta_filename = self.get_meta_filename(specifier)?;
|
|
|
|
let emit_filename = self.get_emit_filename(specifier)?;
|
|
|
|
|
|
|
|
// load and verify the meta data file is for this source and CLI version
|
|
|
|
let bytes = self.disk_cache.get(&meta_filename).ok()?;
|
|
|
|
let meta: EmitMetadata = serde_json::from_slice(&bytes).ok()?;
|
2022-11-29 12:43:54 -05:00
|
|
|
if meta.source_hash != expected_source_hash.to_string() {
|
|
|
|
return None;
|
2022-07-20 16:30:57 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
// load and verify the emit is for the meta data
|
|
|
|
let emit_bytes = self.disk_cache.get(&emit_filename).ok()?;
|
2023-03-23 18:27:58 -04:00
|
|
|
if meta.emit_hash != compute_emit_hash(&emit_bytes, self.cli_version) {
|
2022-07-19 11:58:18 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything looks good, return it
|
|
|
|
let emit_text = String::from_utf8(emit_bytes).ok()?;
|
|
|
|
Some(emit_text)
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:58:18 -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-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,
|
|
|
|
code: &str,
|
|
|
|
) {
|
|
|
|
if let Err(err) = self.set_emit_code_result(specifier, source_hash, code) {
|
|
|
|
// should never error here, but if it ever does don't fail
|
|
|
|
if cfg!(debug_assertions) {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Error saving emit data ({specifier}): {err}");
|
2022-07-19 11:58:18 -04:00
|
|
|
} else {
|
|
|
|
log::debug!("Error saving emit data({}): {}", specifier, err);
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
code: &str,
|
2022-07-12 18:58:39 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-07-19 11:58:18 -04:00
|
|
|
let meta_filename = self
|
|
|
|
.get_meta_filename(specifier)
|
|
|
|
.ok_or_else(|| anyhow!("Could not get meta filename."))?;
|
|
|
|
let emit_filename = self
|
|
|
|
.get_emit_filename(specifier)
|
|
|
|
.ok_or_else(|| anyhow!("Could not get emit filename."))?;
|
|
|
|
|
|
|
|
// save the metadata
|
|
|
|
let metadata = EmitMetadata {
|
|
|
|
source_hash: source_hash.to_string(),
|
2023-03-23 18:27:58 -04:00
|
|
|
emit_hash: compute_emit_hash(code.as_bytes(), self.cli_version),
|
2022-07-19 11:58:18 -04:00
|
|
|
};
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.set(&meta_filename, &serde_json::to_vec(&metadata)?)?;
|
|
|
|
|
|
|
|
// save the emit source
|
|
|
|
self.disk_cache.set(&emit_filename, code.as_bytes())?;
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
fn get_meta_filename(&self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "meta")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_emit_filename(&self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "js")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:09:12 -04:00
|
|
|
fn compute_emit_hash(bytes: &[u8], cli_version: &str) -> String {
|
2022-07-19 11:58:18 -04:00
|
|
|
// 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
|
2022-07-30 12:09:12 -04:00
|
|
|
FastInsecureHasher::new()
|
|
|
|
.write(bytes)
|
|
|
|
// emit should not be re-used between cli versions
|
|
|
|
.write(cli_version.as_bytes())
|
|
|
|
.finish()
|
|
|
|
.to_string()
|
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(),
|
2023-03-23 18:27:58 -04:00
|
|
|
cli_version: "1.0.0",
|
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();
|
|
|
|
cache.set_emit_code(&specifier1, 10, &emit_code1);
|
|
|
|
cache.set_emit_code(&specifier2, 2, &emit_code2);
|
|
|
|
// 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!(
|
2022-11-29 12:43:54 -05:00
|
|
|
cache.get_emit_code(&specifier1, 10),
|
2022-07-19 11:58:18 -04:00
|
|
|
Some(emit_code1.clone()),
|
|
|
|
);
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier2, 2), 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(),
|
2023-03-23 18:27:58 -04:00
|
|
|
cli_version: "2.0.0",
|
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);
|
2022-07-19 11:58:18 -04:00
|
|
|
cache.set_emit_code(&specifier1, 5, &emit_code1);
|
|
|
|
|
|
|
|
// recreating the cache should still load the data because the CLI version is the same
|
|
|
|
let cache = EmitCache {
|
|
|
|
disk_cache,
|
2023-03-23 18:27:58 -04:00
|
|
|
cli_version: "2.0.0",
|
2022-07-19 11:58:18 -04:00
|
|
|
};
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 5), 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();
|
|
|
|
cache.set_emit_code(&specifier1, 20, &emit_code3);
|
2022-11-29 12:43:54 -05:00
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
|
|
|
|
assert_eq!(cache.get_emit_code(&specifier1, 20), Some(emit_code3));
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
2022-07-12 18:58:39 -04:00
|
|
|
}
|