diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 4893913..80ce0ea 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -12,7 +12,6 @@ */ var Url = require("url"); -var spawn = require("child_process").spawn; var fs = require("fs"); exports.XMLHttpRequest = function() { @@ -381,11 +380,11 @@ exports.XMLHttpRequest = function() { withCredentials: self.withCredentials }; + var done = false; + // Reset error flag errorFlag = false; - // Handle async requests - if (settings.async) { // Use the proper protocol var doRequest = ssl ? https.request : http.request; @@ -447,10 +446,12 @@ exports.XMLHttpRequest = function() { setState(self.DONE); sendFlag = false; } + done = true; }); response.on("error", function(error) { self.handleError(error); + done = true; }); }; @@ -470,58 +471,13 @@ exports.XMLHttpRequest = function() { request.end(); self.dispatchEvent("loadstart"); - } else { // Synchronous - // Create a temporary file for communication with the other Node process - var contentFile = ".node-xmlhttprequest-content-" + process.pid; - var syncFile = ".node-xmlhttprequest-sync-" + process.pid; - fs.writeFileSync(syncFile, "", "utf8"); - // The async request the other Node process executes - var execString = "var http = require('http'), https = require('https'), fs = require('fs');" - + "var doRequest = http" + (ssl ? "s" : "") + ".request;" - + "var options = " + JSON.stringify(options) + ";" - + "var responseText = '';" - + "var req = doRequest(options, function(response) {" - + "response.setEncoding('utf8');" - + "response.on('data', function(chunk) {" - + " responseText += chunk;" - + "});" - + "response.on('end', function() {" - + "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: null, data: {statusCode: response.statusCode, headers: response.headers, text: responseText}}), 'utf8');" - + "fs.unlinkSync('" + syncFile + "');" - + "});" - + "response.on('error', function(error) {" - + "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: error}), 'utf8');" - + "fs.unlinkSync('" + syncFile + "');" - + "});" - + "}).on('error', function(error) {" - + "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: error}), 'utf8');" - + "fs.unlinkSync('" + syncFile + "');" - + "});" - + (data ? "req.write('" + JSON.stringify(data).slice(1,-1).replace(/'/g, "\\'") + "');":"") - + "req.end();"; - // Start the other Node Process, executing this string - var syncProc = spawn(process.argv[0], ["-e", execString]); - while(fs.existsSync(syncFile)) { - // Wait while the sync file is empty - } - var resp = JSON.parse(fs.readFileSync(contentFile, 'utf8')); - // Kill the child process once the file has data - syncProc.stdin.end(); - // Remove the temporary file - fs.unlinkSync(contentFile); - - if (resp.err) { - self.handleError(resp.err); - } else { - response = resp.data; - self.status = resp.data.statusCode; - self.responseText = resp.data.text; - setState(self.DONE); + + while(!done) { + require('deasync').sleep(10); } - } - }; - /** + } + /** * Called when an error is encountered to deal with it. */ this.handleError = function(error) { diff --git a/package.json b/package.json index c14434d..f9c41d1 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,9 @@ "type": "git", "url": "git://github.com/driverdan/node-XMLHttpRequest.git" }, + "dependencies": { + "deasync": ">= 0.1.0" + }, "bugs": "http://github.com/driverdan/node-XMLHttpRequest/issues", "engines": { "node": ">=0.4.0"