diff options
author | Josh Matthews <josh@joshmatthews.net> | 2020-05-22 14:38:59 -0400 |
---|---|---|
committer | Josh Matthews <josh@joshmatthews.net> | 2020-05-22 14:40:50 -0400 |
commit | 8f4cb28a6d86b7f22dbcc70654d67b57d451fbf4 (patch) | |
tree | a5ddf6fdf362dca3bb0d6875745baf8bf76f9c00 /components/script/dom/htmlscriptelement.rs | |
parent | f6f1648e56c9afddd835097ef8cc0095ec1a69b5 (diff) | |
download | servo-8f4cb28a6d86b7f22dbcc70654d67b57d451fbf4.tar.gz servo-8f4cb28a6d86b7f22dbcc70654d67b57d451fbf4.zip |
Use temporary files instead of pipes for JS unminification.
Diffstat (limited to 'components/script/dom/htmlscriptelement.rs')
-rw-r--r-- | components/script/dom/htmlscriptelement.rs | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs index 03bb0d73a07..f7311ddaca4 100644 --- a/components/script/dom/htmlscriptelement.rs +++ b/components/script/dom/htmlscriptelement.rs @@ -46,9 +46,9 @@ use servo_url::ImmutableOrigin; use servo_url::ServoUrl; use std::cell::Cell; use std::fs::File; -use std::io::{Read, Write}; +use std::io::{Read, Seek, Write}; use std::path::PathBuf; -use std::process::{Command, Stdio}; +use std::process::Command; use std::sync::{Arc, Mutex}; use style::str::{StaticStringVec, HTML_SPACE_CHARACTERS}; use uuid::Uuid; @@ -663,22 +663,31 @@ impl HTMLScriptElement { return; } - match Command::new("js-beautify") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - { - Err(_) => { - warn!("Failed to execute js-beautify. Will store unmodified script"); - }, - Ok(process) => { - let mut script_content = String::from(script.text.clone()); - let _ = process.stdin.unwrap().write_all(script_content.as_bytes()); - script_content.clear(); - let _ = process.stdout.unwrap().read_to_string(&mut script_content); - - script.text = DOMString::from(script_content); - }, + // Write the minified code to a temporary file and pass its path as an argument + // to js-beautify to read from. Meanwhile, redirect the process' stdout into + // another temporary file and read that into a string. This avoids some hangs + // observed on macOS when using direct input/output pipes with very large + // unminified content. + let (input, output) = (tempfile::NamedTempFile::new(), tempfile::tempfile()); + if let (Ok(mut input), Ok(mut output)) = (input, output) { + input.write_all(script.text.as_bytes()).unwrap(); + match Command::new("js-beautify") + .arg(input.path()) + .stdout(output.try_clone().unwrap()) + .status() + { + Ok(status) if status.success() => { + 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; |