-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: lockfiles #3231
Merged
Merged
feat: lockfiles #3231
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9453695
First pass at lockfile support
ry ecccf9f
add lock-check tests
ry 0f3bf43
clippy
ry d83e2c1
cleanup
ry 3e6a616
use http for lock_check tests
ry f2c3aa5
cleanup
ry e8745d1
use @hayd's suggestion for flags
ry 72ac938
cleanup
ry e9194ba
docs
ry 4c21065
fmt
ry d060d78
clippy
ry def8edc
cleanup
ry 38e1158
dont write local file:https:// urls to lockfile
ry 13e75de
Merge branch 'master' into lock_file
ry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use ring; | ||
use std::fmt::Write; | ||
|
||
pub fn gen(v: Vec<&[u8]>) -> String { | ||
let mut ctx = ring::digest::Context::new(&ring::digest::SHA256); | ||
for src in v.iter() { | ||
ctx.update(src); | ||
} | ||
let digest = ctx.finish(); | ||
let mut out = String::new(); | ||
// TODO There must be a better way to do this... | ||
for byte in digest.as_ref() { | ||
write!(&mut out, "{:02x}", byte).unwrap(); | ||
} | ||
out | ||
} | ||
|
||
pub fn gen2(s: &str) -> String { | ||
gen(vec![s.as_bytes()]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::compilers::CompiledModule; | ||
use serde_json::json; | ||
pub use serde_json::Value; | ||
use std::collections::HashMap; | ||
use std::io::Result; | ||
|
||
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, m: &CompiledModule) -> Result<bool> { | ||
if m.name.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(&m.name) { | ||
let compiled_checksum = crate::checksum::gen2(&m.code); | ||
lockfile_checksum == &compiled_checksum | ||
} else { | ||
false | ||
}) | ||
} | ||
|
||
// Returns true if module was not already inserted. | ||
pub fn insert(&mut self, m: &CompiledModule) -> bool { | ||
if m.name.starts_with("file:") { | ||
return false; | ||
} | ||
let checksum = crate::checksum::gen2(&m.code); | ||
self.map.insert(m.name.clone(), checksum).is_none() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"http:https://127.0.0.1:4545/cli/tests/subdir/print_hello.ts": "5c93c66125878389f47f4abcac003f4be1276c5223612c26302460d71841e287", | ||
"http:https://127.0.0.1:4545/cli/tests/003_relative_import.ts": "bad" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[WILDCARD]Subresource integrety check failed --lock=lock_check_err.json | ||
http:https://127.0.0.1:4545/cli/tests/003_relative_import.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"http:https://localhost:4545/cli/tests/subdir/mt_text_javascript.j1.js": "3a3e002e2f92dc8f045bd4a7c66b4791453ad0417b038dd2b2d9d0f277c44f18", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_text_typescript.t1.ts": "c320ab0a259760e5c78b9ea840af3cc29697109594a3a5b5cea47128102b3e9d", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_application_x_typescript.t4.ts": "42f66736fea7365ff17d5aa9b9655e8551eb81f360dcfb6b77acdd5c9f699e82", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_video_vdn.t2.ts": "54cc82ff3c3b0387df57c7bb8eda4dcd36cbbf499ea483b04ff22c5365d34744", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js": "3a3e002e2f92dc8f045bd4a7c66b4791453ad0417b038dd2b2d9d0f277c44f18", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_application_ecmascript.j2.js": "3a3e002e2f92dc8f045bd4a7c66b4791453ad0417b038dd2b2d9d0f277c44f18", | ||
"http:https://localhost:4545/cli/tests/subdir/mt_video_mp2t.t3.ts": "ee0b46f757b5f78681a4eead44820c2349daef7a5903fe3c624f29dbc98772e1" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it may be more user friendly to do
deno --lock-write lockfile.json
rather thandeno --lock lockfile.json --lock-write
.I'd be okay with doing this in in a follow-up PR tho.