ytdlp_ejs/builtin/
node.rs1use crate::provider::JsChallengeError;
4use std::io::Write;
5use std::process::{Command, Stdio};
6
7pub struct NodeJCP {
9 code: String,
10}
11
12impl NodeJCP {
13 pub fn new(code: &str) -> Self {
14 Self {
15 code: code.to_string(),
16 }
17 }
18
19 pub fn solve(&self, func_name: &str, challenge: &str) -> Result<String, JsChallengeError> {
20 let escaped = challenge.replace('\\', "\\\\").replace('"', "\\\"");
21 let script = format!(
22 "const _result = {{}};\n{}\nconsole.log(_result.{}(\"{}\"));",
23 self.code, func_name, escaped
24 );
25
26 let mut child = Command::new("node")
27 .arg("-")
28 .stdin(Stdio::piped())
29 .stdout(Stdio::piped())
30 .stderr(Stdio::piped())
31 .spawn()?;
32
33 if let Some(mut stdin) = child.stdin.take() {
34 stdin.write_all(script.as_bytes())?;
35 }
36
37 let output = child.wait_with_output()?;
38
39 if !output.status.success() {
40 let stderr = String::from_utf8_lossy(&output.stderr);
41 return Err(JsChallengeError::Runtime(format!(
42 "Node execution failed: {}",
43 stderr.trim()
44 )));
45 }
46
47 Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
48 }
49}