1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00
denoland-deno/cli/lockfile.rs
Bartek Iwańczuk d062ffc1ba
fix: source maps in inspector (#5223)
This commit fixes problems with source maps in Chrome Devtools
by substituting source map URL generated by TS compiler with
actual file URL pointing to DENO_DIR.

Dummy value of "source_map_url" has been removed from
"ScriptOrigin".

Also fixes lock file which used compiled source code to generate
lock hash; it now uses source code of the file that is
being compiled.
2020-05-11 23:48:36 +02:00

72 lines
1.8 KiB
Rust

use serde_json::json;
pub use serde_json::Value;
use std::collections::HashMap;
use std::io::Result;
use url::Url;
pub struct Lockfile {
need_read: bool,
map: HashMap<String, String>,
pub filename: String,
}
impl Lockfile {
pub fn new(filename: String) -> Lockfile {
Lockfile {
map: HashMap::new(),
filename,
need_read: true,
}
}
pub fn write(&self) -> Result<()> {
let j = json!(self.map);
let s = serde_json::to_string_pretty(&j).unwrap();
let mut f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&self.filename)?;
use std::io::Write;
f.write_all(s.as_bytes())?;
debug!("lockfile write {}", self.filename);
Ok(())
}
pub fn read(&mut self) -> Result<()> {
debug!("lockfile read {}", self.filename);
let s = std::fs::read_to_string(&self.filename)?;
self.map = serde_json::from_str(&s)?;
self.need_read = false;
Ok(())
}
/// Lazily reads the filename, checks the given module is included.
/// Returns Ok(true) if check passed
pub fn check(&mut self, url: &Url, code: Vec<u8>) -> Result<bool> {
let url_str = url.to_string();
if url_str.starts_with("file:") {
return Ok(true);
}
if self.need_read {
self.read()?;
}
assert!(!self.need_read);
Ok(if let Some(lockfile_checksum) = self.map.get(&url_str) {
let compiled_checksum = crate::checksum::gen(vec![&code]);
lockfile_checksum == &compiled_checksum
} else {
false
})
}
// Returns true if module was not already inserted.
pub fn insert(&mut self, url: &Url, code: Vec<u8>) -> bool {
let url_str = url.to_string();
if url_str.starts_with("file:") {
return false;
}
let checksum = crate::checksum::gen(vec![&code]);
self.map.insert(url_str, checksum).is_none()
}
}