mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
refactor(lockfile): migrate from v1 to v2 in-memory (#16385)
<!-- Before submitting a PR, please read http://deno.land/manual/contributing 1. Give the PR a descriptive title. Examples of good title: - fix(std/http): Fix race condition in server - docs(console): Update docstrings - feat(doc): Handle nested reexports Examples of bad title: - fix #7123 - update docs - fix bugs 2. Ensure there is a related issue and it is referenced in the PR text. 3. Ensure there are tests that cover the changes. 4. Ensure `cargo test` passes. 5. Ensure `./tools/format.js` passes without changing files. 6. Ensure `./tools/lint.js` passes. -->
This commit is contained in:
parent
bcfe279fba
commit
b3ddd0cea2
1 changed files with 22 additions and 59 deletions
|
@ -8,7 +8,6 @@ use deno_core::parking_lot::Mutex;
|
|||
use deno_core::serde::Deserialize;
|
||||
use deno_core::serde::Serialize;
|
||||
use deno_core::serde_json;
|
||||
use deno_core::serde_json::json;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use log::debug;
|
||||
use std::cell::RefCell;
|
||||
|
@ -21,21 +20,12 @@ use std::sync::Arc;
|
|||
use crate::tools::fmt::format_json;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LockfileV2Content {
|
||||
pub struct LockfileContent {
|
||||
version: String,
|
||||
// Mapping between URLs and their checksums
|
||||
remote: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct LockfileV1Content(BTreeMap<String, String>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum LockfileContent {
|
||||
V1(LockfileV1Content),
|
||||
V2(LockfileV2Content),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Lockfile {
|
||||
write: bool,
|
||||
|
@ -47,10 +37,10 @@ impl Lockfile {
|
|||
pub fn new(filename: PathBuf, write: bool) -> Result<Lockfile, AnyError> {
|
||||
// Writing a lock file always uses the new format.
|
||||
let content = if write {
|
||||
LockfileContent::V2(LockfileV2Content {
|
||||
LockfileContent {
|
||||
version: "2".to_string(),
|
||||
remote: BTreeMap::new(),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let s = std::fs::read_to_string(&filename).with_context(|| {
|
||||
format!("Unable to read lockfile: {}", filename.display())
|
||||
|
@ -59,13 +49,18 @@ impl Lockfile {
|
|||
.context("Unable to parse contents of the lockfile")?;
|
||||
let version = value.get("version").and_then(|v| v.as_str());
|
||||
if version == Some("2") {
|
||||
let content: LockfileV2Content =
|
||||
serde_json::from_value(value).context("Unable to parse lockfile")?;
|
||||
LockfileContent::V2(content)
|
||||
serde_json::from_value::<LockfileContent>(value)
|
||||
.context("Unable to parse lockfile")?
|
||||
} else {
|
||||
let content: LockfileV1Content =
|
||||
// If there's no version field, we assume that user is using the old
|
||||
// version of the lockfile. We'll migrate it in-place into v2 and it
|
||||
// will be writte in v2 if user uses `--lock-write` flag.
|
||||
let remote: BTreeMap<String, String> =
|
||||
serde_json::from_value(value).context("Unable to parse lockfile")?;
|
||||
LockfileContent::V1(content)
|
||||
LockfileContent {
|
||||
version: "2".to_string(),
|
||||
remote,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -82,14 +77,7 @@ impl Lockfile {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let json_string = match &self.content {
|
||||
LockfileContent::V1(c) => {
|
||||
let j = json!(&c.0);
|
||||
serde_json::to_string(&j).unwrap()
|
||||
}
|
||||
LockfileContent::V2(c) => serde_json::to_string(&c).unwrap(),
|
||||
};
|
||||
|
||||
let json_string = serde_json::to_string(&self.content).unwrap();
|
||||
let format_s = format_json(&json_string, &Default::default())
|
||||
.ok()
|
||||
.flatten()
|
||||
|
@ -120,23 +108,11 @@ impl Lockfile {
|
|||
if specifier.starts_with("file:") {
|
||||
return true;
|
||||
}
|
||||
match &self.content {
|
||||
LockfileContent::V1(c) => {
|
||||
if let Some(lockfile_checksum) = c.0.get(specifier) {
|
||||
let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||
lockfile_checksum == &compiled_checksum
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
LockfileContent::V2(c) => {
|
||||
if let Some(lockfile_checksum) = c.remote.get(specifier) {
|
||||
let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||
lockfile_checksum == &compiled_checksum
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
if let Some(lockfile_checksum) = self.content.remote.get(specifier) {
|
||||
let compiled_checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||
lockfile_checksum == &compiled_checksum
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -145,14 +121,7 @@ impl Lockfile {
|
|||
return;
|
||||
}
|
||||
let checksum = crate::checksum::gen(&[code.as_bytes()]);
|
||||
match &mut self.content {
|
||||
LockfileContent::V1(c) => {
|
||||
c.0.insert(specifier.to_string(), checksum);
|
||||
}
|
||||
LockfileContent::V2(c) => {
|
||||
c.remote.insert(specifier.to_string(), checksum);
|
||||
}
|
||||
};
|
||||
self.content.remote.insert(specifier.to_string(), checksum);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,10 +202,7 @@ mod tests {
|
|||
|
||||
let result = Lockfile::new(file_path, false).unwrap();
|
||||
|
||||
let remote = match result.content {
|
||||
LockfileContent::V2(c) => c.remote,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let remote = result.content.remote;
|
||||
let keys: Vec<String> = remote.keys().cloned().collect();
|
||||
let expected_keys = vec![
|
||||
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
||||
|
@ -259,10 +225,7 @@ mod tests {
|
|||
"Here is some source code",
|
||||
);
|
||||
|
||||
let remote = match lockfile.content {
|
||||
LockfileContent::V2(c) => c.remote,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let remote = lockfile.content.remote;
|
||||
let keys: Vec<String> = remote.keys().cloned().collect();
|
||||
let expected_keys = vec![
|
||||
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
||||
|
|
Loading…
Reference in a new issue