mirror of
https://github.com/denoland/deno.git
synced 2024-12-13 02:52:54 -05:00
refactor(emit/cache): move cli version into emit hash (#15348)
This commit is contained in:
parent
ef6968116f
commit
14f74e706e
1 changed files with 9 additions and 10 deletions
19
cli/cache/emit.rs
vendored
19
cli/cache/emit.rs
vendored
|
@ -16,8 +16,6 @@ use super::FastInsecureHasher;
|
||||||
struct EmitMetadata {
|
struct EmitMetadata {
|
||||||
pub source_hash: String,
|
pub source_hash: String,
|
||||||
pub emit_hash: String,
|
pub emit_hash: String,
|
||||||
// purge the cache between cli versions
|
|
||||||
pub cli_version: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The cache that stores previously emitted files.
|
/// The cache that stores previously emitted files.
|
||||||
|
@ -54,9 +52,6 @@ impl EmitCache {
|
||||||
// load and verify the meta data file is for this source and CLI version
|
// load and verify the meta data file is for this source and CLI version
|
||||||
let bytes = self.disk_cache.get(&meta_filename).ok()?;
|
let bytes = self.disk_cache.get(&meta_filename).ok()?;
|
||||||
let meta: EmitMetadata = serde_json::from_slice(&bytes).ok()?;
|
let meta: EmitMetadata = serde_json::from_slice(&bytes).ok()?;
|
||||||
if meta.cli_version != self.cli_version {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
if let Some(expected_source_hash) = expected_source_hash {
|
if let Some(expected_source_hash) = expected_source_hash {
|
||||||
if meta.source_hash != expected_source_hash.to_string() {
|
if meta.source_hash != expected_source_hash.to_string() {
|
||||||
return None;
|
return None;
|
||||||
|
@ -65,7 +60,7 @@ impl EmitCache {
|
||||||
|
|
||||||
// load and verify the emit is for the meta data
|
// load and verify the emit is for the meta data
|
||||||
let emit_bytes = self.disk_cache.get(&emit_filename).ok()?;
|
let emit_bytes = self.disk_cache.get(&emit_filename).ok()?;
|
||||||
if meta.emit_hash != compute_emit_hash(&emit_bytes) {
|
if meta.emit_hash != compute_emit_hash(&emit_bytes, &self.cli_version) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,9 +114,8 @@ impl EmitCache {
|
||||||
|
|
||||||
// save the metadata
|
// save the metadata
|
||||||
let metadata = EmitMetadata {
|
let metadata = EmitMetadata {
|
||||||
cli_version: self.cli_version.to_string(),
|
|
||||||
source_hash: source_hash.to_string(),
|
source_hash: source_hash.to_string(),
|
||||||
emit_hash: compute_emit_hash(code.as_bytes()),
|
emit_hash: compute_emit_hash(code.as_bytes(), &self.cli_version),
|
||||||
};
|
};
|
||||||
self
|
self
|
||||||
.disk_cache
|
.disk_cache
|
||||||
|
@ -146,11 +140,16 @@ impl EmitCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_emit_hash(bytes: &[u8]) -> String {
|
fn compute_emit_hash(bytes: &[u8], cli_version: &str) -> String {
|
||||||
// it's ok to use an insecure hash here because
|
// it's ok to use an insecure hash here because
|
||||||
// if someone can change the emit source then they
|
// if someone can change the emit source then they
|
||||||
// can also change the version hash
|
// can also change the version hash
|
||||||
FastInsecureHasher::new().write(bytes).finish().to_string()
|
FastInsecureHasher::new()
|
||||||
|
.write(bytes)
|
||||||
|
// emit should not be re-used between cli versions
|
||||||
|
.write(cli_version.as_bytes())
|
||||||
|
.finish()
|
||||||
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue