Run an arbitrary JS program under Node and return its last JSON line.
(program: str)
| 149 | |
| 150 | |
| 151 | def run_js(program: str): |
| 152 | """Run an arbitrary JS program under Node and return its last JSON line.""" |
| 153 | result = subprocess.run( |
| 154 | ["node", "-"], |
| 155 | input=program, |
| 156 | capture_output=True, |
| 157 | text=True, |
| 158 | encoding="utf-8", |
| 159 | timeout=30, |
| 160 | ) |
| 161 | |
| 162 | if result.returncode != 0: |
| 163 | raise RuntimeError(f"Node.js exited {result.returncode}:\n{result.stderr}") |
| 164 | |
| 165 | last_line = result.stdout.strip().splitlines()[-1] |
| 166 | return json.loads(last_line) |