Use temporary files instead of pipes for JS unminification.

This commit is contained in:
Josh Matthews 2020-05-22 14:38:59 -04:00
parent f6f1648e56
commit 8f4cb28a6d
3 changed files with 29 additions and 18 deletions

1
Cargo.lock generated
View file

@ -4726,6 +4726,7 @@ dependencies = [
"style", "style",
"style_traits", "style_traits",
"swapper", "swapper",
"tempfile",
"tendril", "tendril",
"time", "time",
"unicode-bidi", "unicode-bidi",

View file

@ -106,6 +106,7 @@ sparkle = "0.1"
style = { path = "../style", features = ["servo"] } style = { path = "../style", features = ["servo"] }
style_traits = { path = "../style_traits" } style_traits = { path = "../style_traits" }
swapper = "0.1" swapper = "0.1"
tempfile = "3"
tendril = { version = "0.4.1", features = ["encoding_rs"] } tendril = { version = "0.4.1", features = ["encoding_rs"] }
time = "0.1.12" time = "0.1.12"
unicode-bidi = "0.3.4" unicode-bidi = "0.3.4"

View file

@ -46,9 +46,9 @@ use servo_url::ImmutableOrigin;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use std::cell::Cell; use std::cell::Cell;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Seek, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::Command;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use style::str::{StaticStringVec, HTML_SPACE_CHARACTERS}; use style::str::{StaticStringVec, HTML_SPACE_CHARACTERS};
use uuid::Uuid; use uuid::Uuid;
@ -663,22 +663,31 @@ impl HTMLScriptElement {
return; return;
} }
match Command::new("js-beautify") // Write the minified code to a temporary file and pass its path as an argument
.stdin(Stdio::piped()) // to js-beautify to read from. Meanwhile, redirect the process' stdout into
.stdout(Stdio::piped()) // another temporary file and read that into a string. This avoids some hangs
.spawn() // observed on macOS when using direct input/output pipes with very large
{ // unminified content.
Err(_) => { let (input, output) = (tempfile::NamedTempFile::new(), tempfile::tempfile());
warn!("Failed to execute js-beautify. Will store unmodified script"); if let (Ok(mut input), Ok(mut output)) = (input, output) {
}, input.write_all(script.text.as_bytes()).unwrap();
Ok(process) => { match Command::new("js-beautify")
let mut script_content = String::from(script.text.clone()); .arg(input.path())
let _ = process.stdin.unwrap().write_all(script_content.as_bytes()); .stdout(output.try_clone().unwrap())
script_content.clear(); .status()
let _ = process.stdout.unwrap().read_to_string(&mut script_content); {
Ok(status) if status.success() => {
script.text = DOMString::from(script_content); let mut script_content = String::new();
}, output.seek(std::io::SeekFrom::Start(0)).unwrap();
output.read_to_string(&mut script_content).unwrap();
script.text = DOMString::from(script_content);
},
_ => {
warn!("Failed to execute js-beautify. Will store unmodified script");
},
}
} else {
warn!("Error creating input and output files for unminify");
} }
let path; let path;