22from pathlib import Path
33import os
44import shutil
5+ import json
56import sys
67import shutil
78from ..backend_base import BackendBase
@@ -53,22 +54,33 @@ def __init__(self, host_work_dir, work_dir, script, async_main, node_binary):
5354 node_binary = shutil_node_binary
5455 self .node_binary = node_binary
5556
57+ def supports_flag_no_experimental_fetch (self ):
58+ probe_cmd = [self .node_binary , "--no-experimental-fetch" , "--version" ]
59+ ret_code = subprocess .call (
60+ probe_cmd , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL
61+ )
62+ return ret_code == 0
63+
5664 def run (self ):
5765 main_name = "node_main.js"
5866 main = Path (THIS_DIR ) / main_name
5967 shutil .copyfile (main , self .host_work_dir / main_name )
6068
61- cmd = [
62- self .node_binary ,
63- "--no-experimental-fetch" ,
64- main_name ,
65- self .work_dir ,
66- self .script ,
67- str (int (self .async_main )),
68- ]
69+ cmd = [self .node_binary ]
70+ if self .supports_flag_no_experimental_fetch ():
71+ cmd .append ("--no-experimental-fetch" )
72+
73+ cmd .extend (
74+ [
75+ main_name ,
76+ self .work_dir ,
77+ self .script ,
78+ str (int (self .async_main )),
79+ self .host_work_dir ,
80+ ]
81+ )
6982
7083 process = subprocess .Popen (cmd , stdout = subprocess .PIPE )
71-
7284 # Poll process.stdout to show stdout live
7385 while True :
7486 output = process .stdout .readline ()
@@ -77,6 +89,16 @@ def run(self):
7789 if output :
7890 print (output .decode ().strip ())
7991 rc = process .poll ()
80- # print("RC", rc)
8192 if process .returncode != 0 :
82- sys .exit (process .returncode )
93+ raise RuntimeError (
94+ f"node return with returncode: { process .returncode } rc { rc } "
95+ )
96+
97+ result_path = self .host_work_dir / "_node_result.json"
98+ if result_path .exists ():
99+ with open (result_path , "r" ) as f :
100+ results = json .load (f )
101+ if results ["return_code" ] != 0 :
102+ raise RuntimeError (results ["error" ])
103+ else :
104+ raise RuntimeError ("internal error in pyjs-code-runner" )
0 commit comments