Execute JavaScript code locally via Node.js. Args: js_code: JavaScript code to execute. input_spec: Input specification. Returns: Tuple of (stdout, stderr, exit_code).
(
self, js_code: str,
input_spec: ExecutionInput)
| 729 | stdin_input=input_spec.stdin) |
| 730 | |
| 731 | async def _local_execute_javascript( |
| 732 | self, js_code: str, |
| 733 | input_spec: ExecutionInput) -> tuple[str, str, int]: |
| 734 | """ |
| 735 | Execute JavaScript code locally via Node.js. |
| 736 | |
| 737 | Args: |
| 738 | js_code: JavaScript code to execute. |
| 739 | input_spec: Input specification. |
| 740 | |
| 741 | Returns: |
| 742 | Tuple of (stdout, stderr, exit_code). |
| 743 | """ |
| 744 | # Write code to temp file |
| 745 | script_file = self.scripts_dir / f'_temp_{uuid.uuid4().hex[:8]}.js' |
| 746 | try: |
| 747 | # Generate environment setup |
| 748 | env_setup = self._generate_local_js_env_setup(input_spec) |
| 749 | full_code = env_setup + '\n' + js_code |
| 750 | |
| 751 | with open(script_file, 'w', encoding='utf-8') as f: |
| 752 | f.write(full_code) |
| 753 | |
| 754 | # Build command |
| 755 | cmd = [self._get_node_executable(), str(script_file)] |
| 756 | cmd.extend([str(arg) for arg in input_spec.args]) |
| 757 | |
| 758 | # Use working_dir from input_spec for proper resource access |
| 759 | cwd = input_spec.working_dir if input_spec.working_dir else None |
| 760 | |
| 761 | # Keep script in scripts folder for logging/debugging |
| 762 | return self._local_run_subprocess( |
| 763 | cmd, |
| 764 | env=input_spec.env_vars, |
| 765 | cwd=cwd, |
| 766 | stdin_input=input_spec.stdin) |
| 767 | except Exception as e: |
| 768 | logger.error(f'Local JavaScript execution failed: {e}') |
| 769 | raise |
| 770 | |
| 771 | def _generate_local_env_setup(self, input_spec: ExecutionInput) -> str: |
| 772 | """Generate Python code to setup environment for local execution.""" |
no test coverage detected