@@ -452,6 +452,7 @@ exports.XMLHttpRequest = function() {
452452 self . dispatchEvent ( "loadstart" ) ;
453453 } else { // Synchronous
454454 // Create a temporary file for communication with the other Node process
455+ var contentFile = ".node-xmlhttprequest-content-" + process . pid ;
455456 var syncFile = ".node-xmlhttprequest-sync-" + process . pid ;
456457 fs . writeFileSync ( syncFile , "" , "utf8" ) ;
457458 // The async request the other Node process executes
@@ -462,28 +463,33 @@ exports.XMLHttpRequest = function() {
462463 + "var req = doRequest(options, function(response) {"
463464 + "response.setEncoding('utf8');"
464465 + "response.on('data', function(chunk) {"
465- + "responseText += chunk;"
466+ + " responseText += chunk;"
466467 + "});"
467468 + "response.on('end', function() {"
468- + "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
469+ + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
470+ + "fs.unlinkSync('" + syncFile + "');"
469471 + "});"
470472 + "response.on('error', function(error) {"
471- + "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
473+ + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
474+ + "fs.unlinkSync('" + syncFile + "');"
472475 + "});"
473476 + "}).on('error', function(error) {"
474- + "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
477+ + "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
478+ + "fs.unlinkSync('" + syncFile + "');"
475479 + "});"
476480 + ( data ? "req.write('" + data . replace ( / ' / g, "\\'" ) + "');" :"" )
477481 + "req.end();" ;
478482 // Start the other Node Process, executing this string
479483 var syncProc = spawn ( process . argv [ 0 ] , [ "-e" , execString ] ) ;
480- while ( ( self . responseText = fs . readFileSync ( syncFile , 'utf8' ) ) == "" ) {
481- // Wait while the file is empty
484+ var statusText ;
485+ while ( fs . existsSync ( syncFile ) ) {
486+ // Wait while the sync file is empty
482487 }
488+ self . responseText = fs . readFileSync ( contentFile , 'utf8' ) ;
483489 // Kill the child process once the file has data
484490 syncProc . stdin . end ( ) ;
485491 // Remove the temporary file
486- fs . unlinkSync ( syncFile ) ;
492+ fs . unlinkSync ( contentFile ) ;
487493 if ( self . responseText . match ( / ^ N O D E - X M L H T T P R E Q U E S T - E R R O R : / ) ) {
488494 // If the file returned an error, handle it
489495 var errorObj = self . responseText . replace ( / ^ N O D E - X M L H T T P R E Q U E S T - E R R O R : / , "" ) ;
0 commit comments