Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sync version work on large messages, improve sync version using deasync #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 9 additions & 53 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

var Url = require("url");
var spawn = require("child_process").spawn;
var fs = require("fs");

exports.XMLHttpRequest = function() {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -447,10 +446,12 @@ exports.XMLHttpRequest = function() {
setState(self.DONE);
sendFlag = false;
}
done = true;
});

response.on("error", function(error) {
self.handleError(error);
done = true;
});
};

Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down