Skip to content

Commit 06e43de

Browse files
committed
fix(native-sidecar): parse node input type before eval
1 parent 886b69e commit 06e43de

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

crates/native-sidecar/src/execution/launch.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,14 +1698,36 @@ pub(super) fn resolve_special_node_cli_invocation(
16981698
args: &[String],
16991699
env: &mut BTreeMap<String, String>,
17001700
) -> Option<(String, Vec<String>)> {
1701-
let first = args.first()?;
1701+
let mut index = 0;
1702+
while let Some(arg) = args.get(index) {
1703+
match arg.as_str() {
1704+
"--input-type" => {
1705+
if !apply_node_input_type(env, args.get(index + 1)?) {
1706+
return None;
1707+
}
1708+
index += 2;
1709+
}
1710+
value if value.starts_with("--input-type=") => {
1711+
if !apply_node_input_type(env, &value["--input-type=".len()..]) {
1712+
return None;
1713+
}
1714+
index += 1;
1715+
}
1716+
_ => break,
1717+
}
1718+
}
1719+
1720+
let first = args.get(index)?;
17021721
match first.as_str() {
17031722
"-e" | "--eval" => {
17041723
env.insert(
17051724
String::from("AGENTOS_NODE_EVAL"),
1706-
args.get(1).cloned().unwrap_or_default(),
1725+
args.get(index + 1).cloned().unwrap_or_default(),
17071726
);
1708-
Some((first.clone(), args.iter().skip(2).cloned().collect()))
1727+
Some((
1728+
String::from("-e"),
1729+
args.iter().skip(index + 2).cloned().collect(),
1730+
))
17091731
}
17101732
"-v" | "--version" => {
17111733
env.insert(
@@ -1760,12 +1782,32 @@ if (summary.failed > 0) process.exitCode = 1;
17601782
"#,
17611783
),
17621784
);
1763-
Some((String::from("-e"), args.iter().skip(1).cloned().collect()))
1785+
Some((
1786+
String::from("-e"),
1787+
args.iter().skip(index + 1).cloned().collect(),
1788+
))
17641789
}
17651790
_ => None,
17661791
}
17671792
}
17681793

1794+
fn apply_node_input_type(env: &mut BTreeMap<String, String>, value: &str) -> bool {
1795+
match value {
1796+
"module" => {
1797+
env.insert(
1798+
String::from("AGENTOS_GUEST_ENTRYPOINT_MODULE_MODE"),
1799+
String::from("1"),
1800+
);
1801+
true
1802+
}
1803+
"commonjs" => {
1804+
env.remove("AGENTOS_GUEST_ENTRYPOINT_MODULE_MODE");
1805+
true
1806+
}
1807+
_ => false,
1808+
}
1809+
}
1810+
17691811
fn node_runtime_command_name(command: &str) -> Option<&str> {
17701812
let name = Path::new(command)
17711813
.file_name()

crates/native-sidecar/tests/service.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14987,7 +14987,7 @@ if (child.status !== 0) {
1498714987
);
1498814988
}
1498914989

14990-
fn command_resolution_executes_node_eval_command() {
14990+
fn command_resolution_executes_node_module_eval_command() {
1499114991
let mut sidecar = create_test_sidecar();
1499214992
let (connection_id, session_id) =
1499314993
authenticate_and_open_session(&mut sidecar).expect("authenticate and open session");
@@ -15009,8 +15009,11 @@ if (child.status !== 0) {
1500915009
runtime: None,
1501015010
entrypoint: None,
1501115011
args: vec![
15012+
String::from("--input-type=module"),
1501215013
String::from("-e"),
15013-
String::from("process.stdout.write('node-eval-ok\\n')"),
15014+
String::from(
15015+
"process.stdout.write(await Promise.resolve('node-module-eval-ok\\n'))",
15016+
),
1501415017
],
1501515018
env: std::collections::HashMap::new(),
1501615019
cwd: None,
@@ -15030,7 +15033,7 @@ if (child.status !== 0) {
1503015033
drain_process_output(&mut sidecar, &vm_id, "proc-command-node-eval");
1503115034

1503215035
assert_eq!(exit_code, Some(0), "stderr: {stderr}");
15033-
assert!(stdout.contains("node-eval-ok"), "stdout: {stdout}");
15036+
assert!(stdout.contains("node-module-eval-ok"), "stdout: {stdout}");
1503415037
}
1503515038
fn command_resolution_rejects_unknown_command() {
1503615039
let mut sidecar = create_test_sidecar();
@@ -25652,7 +25655,7 @@ try {
2565225655
bindings_javascript_child_process_rejects_invalid_json_file_input_before_dispatch();
2565325656
bindings_javascript_child_process_accepts_valid_json_input();
2565425657
command_resolution_executes_javascript_path_command_with_sidecar_mappings();
25655-
command_resolution_executes_node_eval_command();
25658+
command_resolution_executes_node_module_eval_command();
2565625659
command_resolution_rejects_unknown_command();
2565725660
python_vfs_rpc_requests_proxy_into_the_vm_kernel_filesystem();
2565825661
javascript_sync_rpc_requests_proxy_into_the_vm_kernel_filesystem();

0 commit comments

Comments
 (0)