xmlhttprequest
Advanced tools
Comparing version 1.2.2 to 1.3.0
{ "name" : "xmlhttprequest" | ||
, "description" : "XMLHttpRequest for Node" | ||
, "version" : "1.2.2" | ||
, "version" : "1.3.0" | ||
, "author" : "Dan DeFelippi" | ||
@@ -5,0 +5,0 @@ , "licenses" : |
# node-XMLHttpRequest # | ||
node-XMLHttpRequest is arapper for the built-in http client to emulate the browser XMLHttpRequest object. | ||
node-XMLHttpRequest is a wrapper for the built-in http client to emulate the browser XMLHttpRequest object. | ||
@@ -13,4 +13,12 @@ This can be used with JS designed for browsers to improve reuse of code and allow the use of existing libraries. | ||
Refer to W3C specs for XHR methods. | ||
Refer to [W3C specs](http://www.w3.org/TR/XMLHttpRequest/) for XHR methods. | ||
## Supports ## | ||
* Async and synchronous requests | ||
* GET, POST, and PUT requests | ||
* All native methods (open, send, abort, getRequestHeader, | ||
getAllRequestHeaders) | ||
* Requests to all domains | ||
## TODO ## | ||
@@ -21,1 +29,3 @@ | ||
* Possibly move from http to tcp for more flexibility | ||
* DELETE requests aren't working | ||
* XML parsing |
var sys = require("util") | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,xhr = new XMLHttpRequest(); | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,xhr = new XMLHttpRequest(); | ||
@@ -6,0 +6,0 @@ // Test constant values |
var sys = require("util") | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,xhr = new XMLHttpRequest() | ||
,http = require("http"); | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,xhr = new XMLHttpRequest() | ||
,http = require("http"); | ||
// Test server | ||
var server = http.createServer(function (req, res) { | ||
// Test setRequestHeader | ||
assert.equal("Foobar", req.headers["x-test"]); | ||
var body = "Hello World"; | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
"Content-Length": Buffer.byteLength(body) | ||
}); | ||
res.write("Hello World"); | ||
res.end(); | ||
this.close(); | ||
// Test setRequestHeader | ||
assert.equal("Foobar", req.headers["x-test"]); | ||
var body = "Hello World"; | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
"Content-Length": Buffer.byteLength(body) | ||
}); | ||
res.write("Hello World"); | ||
res.end(); | ||
this.close(); | ||
}).listen(8000); | ||
xhr.onreadystatechange = function() { | ||
if (this.readyState == 4) { | ||
// Test getAllResponseHeaders() | ||
var headers = "content-type: text/plain\r\ncontent-length: 11\r\nconnection: close"; | ||
assert.equal(headers, this.getAllResponseHeaders()); | ||
sys.puts("done"); | ||
} | ||
if (this.readyState == 4) { | ||
// Test getAllResponseHeaders() | ||
var headers = "content-type: text/plain\r\ncontent-length: 11\r\nconnection: close"; | ||
assert.equal(headers, this.getAllResponseHeaders()); | ||
// Test aborted getAllResponseHeaders | ||
this.abort(); | ||
assert.equal("", this.getAllResponseHeaders()); | ||
assert.equal(null, this.getResponseHeader("Connection")); | ||
sys.puts("done"); | ||
} | ||
}; | ||
assert.equal(null, xhr.getResponseHeader("Content-Type")); | ||
xhr.open("GET", "http://localhost:8000/"); | ||
xhr.setRequestHeader("X-Test", "Foobar"); | ||
xhr.send(); |
var sys = require("util") | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,http = require("http") | ||
,xhr; | ||
,assert = require("assert") | ||
,XMLHttpRequest = require("../XMLHttpRequest").XMLHttpRequest | ||
,http = require("http") | ||
,xhr; | ||
// Test server | ||
var server = http.createServer(function (req, res) { | ||
// Check request method and URL | ||
assert.equal(methods[curMethod], req.method); | ||
assert.equal("/" + methods[curMethod], req.url); | ||
// Check request method and URL | ||
assert.equal(methods[curMethod], req.method); | ||
assert.equal("/" + methods[curMethod], req.url); | ||
var body = (req.method != "HEAD" ? "Hello World" : ""); | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
"Content-Length": Buffer.byteLength(body) | ||
}); | ||
// HEAD has no body | ||
if (req.method != "HEAD") { | ||
res.write(body); | ||
} | ||
res.end(); | ||
if (curMethod == methods.length - 1) { | ||
this.close(); | ||
sys.puts("done"); | ||
} | ||
var body = (req.method != "HEAD" ? "Hello World" : ""); | ||
res.writeHead(200, { | ||
"Content-Type": "text/plain", | ||
"Content-Length": Buffer.byteLength(body) | ||
}); | ||
// HEAD has no body | ||
if (req.method != "HEAD") { | ||
res.write(body); | ||
} | ||
res.end(); | ||
if (curMethod == methods.length - 1) { | ||
this.close(); | ||
sys.puts("done"); | ||
} | ||
}).listen(8000); | ||
@@ -35,29 +35,29 @@ | ||
function start(method) { | ||
// Reset each time | ||
xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function() { | ||
if (this.readyState == 4) { | ||
if (method == "HEAD") { | ||
assert.equal("", this.responseText); | ||
} else { | ||
assert.equal("Hello World", this.responseText); | ||
} | ||
curMethod++; | ||
if (curMethod < methods.length) { | ||
start(methods[curMethod]); | ||
} | ||
} | ||
}; | ||
var url = "http://localhost:8000/" + method; | ||
xhr.open(method, url); | ||
xhr.send(); | ||
// Reset each time | ||
xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = function() { | ||
if (this.readyState == 4) { | ||
if (method == "HEAD") { | ||
assert.equal("", this.responseText); | ||
} else { | ||
assert.equal("Hello World", this.responseText); | ||
} | ||
curMethod++; | ||
if (curMethod < methods.length) { | ||
start(methods[curMethod]); | ||
} | ||
} | ||
}; | ||
var url = "http://localhost:8000/" + method; | ||
xhr.open(method, url); | ||
xhr.send(); | ||
} | ||
for (var curMethod in methods) { | ||
sys.puts("Testing " + methods[curMethod]); | ||
start(methods[curMethod]); | ||
sys.puts("Testing " + methods[curMethod]); | ||
start(methods[curMethod]); | ||
} |
@@ -16,295 +16,349 @@ /** | ||
var Url = require("url"), | ||
spawn = require("child_process").spawn, | ||
fs = require('fs'); | ||
spawn = require("child_process").spawn, | ||
fs = require('fs'); | ||
exports.XMLHttpRequest = function() { | ||
/** | ||
* Private variables | ||
*/ | ||
var self = this; | ||
var http = require('http'); | ||
var https = require('https'); | ||
/** | ||
* Private variables | ||
*/ | ||
var self = this; | ||
var http = require('http'); | ||
var https = require('https'); | ||
// Holds http.js objects | ||
var client; | ||
var request; | ||
var response; | ||
// Request settings | ||
var settings = {}; | ||
// Set some default headers | ||
var defaultHeaders = { | ||
"User-Agent": "node.js", | ||
"Accept": "*/*", | ||
}; | ||
var headers = defaultHeaders; | ||
/** | ||
* Constants | ||
*/ | ||
this.UNSENT = 0; | ||
this.OPENED = 1; | ||
this.HEADERS_RECEIVED = 2; | ||
this.LOADING = 3; | ||
this.DONE = 4; | ||
// Holds http.js objects | ||
var client; | ||
var request; | ||
var response; | ||
/** | ||
* Public vars | ||
*/ | ||
// Current state | ||
this.readyState = this.UNSENT; | ||
// Request settings | ||
var settings = {}; | ||
// default ready state change handler in case one is not set or is set late | ||
this.onreadystatechange = null; | ||
// Set some default headers | ||
var defaultHeaders = { | ||
"User-Agent": "node.js", | ||
"Accept": "*/*", | ||
}; | ||
// Result & response | ||
this.responseText = ""; | ||
this.responseXML = ""; | ||
this.status = null; | ||
this.statusText = null; | ||
/** | ||
* Open the connection. Currently supports local server requests. | ||
* | ||
* @param string method Connection method (eg GET, POST) | ||
* @param string url URL for the connection. | ||
* @param boolean async Asynchronous connection. Default is true. | ||
* @param string user Username for basic authentication (optional) | ||
* @param string password Password for basic authentication (optional) | ||
*/ | ||
this.open = function(method, url, async, user, password) { | ||
settings = { | ||
"method": method, | ||
"url": url, | ||
"async": async || null, | ||
"user": user || null, | ||
"password": password || null | ||
}; | ||
this.abort(); | ||
// Send flag | ||
var sendFlag = false; | ||
// Error flag, used when errors occur or abort is called | ||
var errorFlag = false; | ||
setState(this.OPENED); | ||
}; | ||
/** | ||
* Sets a header for the request. | ||
* | ||
* @param string header Header name | ||
* @param string value Header value | ||
*/ | ||
this.setRequestHeader = function(header, value) { | ||
headers[header] = value; | ||
}; | ||
/** | ||
* Gets a header from the server response. | ||
* | ||
* @param string header Name of header to get. | ||
* @return string Text of the header or null if it doesn't exist. | ||
*/ | ||
this.getResponseHeader = function(header) { | ||
if (this.readyState > this.OPENED && response.headers[header]) { | ||
return header + ": " + response.headers[header]; | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Gets all the response headers. | ||
* | ||
* @return string | ||
*/ | ||
this.getAllResponseHeaders = function() { | ||
if (this.readyState < this.HEADERS_RECEIVED) { | ||
throw "INVALID_STATE_ERR: Headers have not been received."; | ||
} | ||
var result = ""; | ||
for (var i in response.headers) { | ||
result += i + ": " + response.headers[i] + "\r\n"; | ||
} | ||
return result.substr(0, result.length - 2); | ||
}; | ||
var headers = defaultHeaders; | ||
/** | ||
* Sends the request to the server. | ||
* | ||
* @param string data Optional data to send as request body. | ||
*/ | ||
this.send = function(data) { | ||
if (this.readyState != this.OPENED) { | ||
throw "INVALID_STATE_ERR: connection must be opened before send() is called"; | ||
} | ||
var ssl = false; | ||
var url = Url.parse(settings.url); | ||
// Determine the server | ||
switch (url.protocol) { | ||
case 'https:': | ||
ssl = true; | ||
// SSL & non-SSL both need host, no break here. | ||
case 'http:': | ||
var host = url.hostname; | ||
break; | ||
case undefined: | ||
case '': | ||
var host = "localhost"; | ||
break; | ||
default: | ||
throw "Protocol not supported."; | ||
} | ||
/** | ||
* Constants | ||
*/ | ||
this.UNSENT = 0; | ||
this.OPENED = 1; | ||
this.HEADERS_RECEIVED = 2; | ||
this.LOADING = 3; | ||
this.DONE = 4; | ||
// Default to port 80. If accessing localhost on another port be sure | ||
// to use http://localhost:port/path | ||
var port = url.port || (ssl ? 443 : 80); | ||
// Add query string if one is used | ||
var uri = url.pathname + (url.search ? url.search : ''); | ||
// Set the Host header or the server may reject the request | ||
this.setRequestHeader("Host", host); | ||
/** | ||
* Public vars | ||
*/ | ||
// Current state | ||
this.readyState = this.UNSENT; | ||
// Set Basic Auth if necessary | ||
if (settings.user) { | ||
if (typeof settings.password == "undefined") { | ||
settings.password = ""; | ||
} | ||
var authBuf = new Buffer(settings.user + ":" + settings.password); | ||
headers["Authorization"] = "Basic " + authBuf.toString("base64"); | ||
} | ||
// Set content length header | ||
if (settings.method == "GET" || settings.method == "HEAD") { | ||
data = null; | ||
} else if (data) { | ||
this.setRequestHeader("Content-Length", Buffer.byteLength(data)); | ||
if (!headers["Content-Type"]) { | ||
this.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); | ||
} | ||
} | ||
var options = { | ||
host: host, | ||
port: port, | ||
path: uri, | ||
method: settings.method, | ||
headers: headers | ||
}; | ||
// default ready state change handler in case one is not set or is set late | ||
this.onreadystatechange = null; | ||
if(!settings.hasOwnProperty("async") || settings.async) { //Normal async path | ||
// Use the proper protocol | ||
var doRequest = ssl ? https.request : http.request; | ||
var req = doRequest(options, function(response) { | ||
response.setEncoding("utf8"); | ||
setState(self.HEADERS_RECEIVED); | ||
self.status = response.statusCode; | ||
response.on('data', function(chunk) { | ||
// Make sure there's some data | ||
if (chunk) { | ||
self.responseText += chunk; | ||
} | ||
setState(self.LOADING); | ||
}); | ||
response.on('end', function() { | ||
setState(self.DONE); | ||
}); | ||
response.on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
}).on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
// Node 0.4 and later won't accept empty data. Make sure it's needed. | ||
if (data) { | ||
req.write(data); | ||
} | ||
req.end(); | ||
} else { // Synchronous | ||
// Create a temporary file for communication with the other Node process | ||
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('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');" | ||
+ "});" | ||
+ "response.on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ "}).on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"") | ||
+ "req.end();"; | ||
// Start the other Node Process, executing this string | ||
syncProc = spawn(process.argv[0], ["-e", execString]); | ||
while((self.responseText = fs.readFileSync(syncFile, 'utf8')) == "") { | ||
// Wait while the file is empty | ||
} | ||
// Kill the child process once the file has data | ||
syncProc.stdin.end(); | ||
// Remove the temporary file | ||
fs.unlinkSync(syncFile); | ||
if(self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) { | ||
// If the file returned an error, handle it | ||
var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, ""); | ||
self.handleError(errorObj); | ||
} else { | ||
// If the file returned okay, parse its data and move to the DONE state | ||
self.status = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/, "$1"); | ||
self.responseText = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/, "$1"); | ||
setState(self.DONE); | ||
} | ||
} | ||
}; | ||
// Result & response | ||
this.responseText = ""; | ||
this.responseXML = ""; | ||
this.status = null; | ||
this.statusText = null; | ||
this.handleError = function(error) { | ||
this.status = 503; | ||
this.statusText = error; | ||
this.responseText = error.stack; | ||
setState(this.DONE); | ||
}; | ||
/** | ||
* Open the connection. Currently supports local server requests. | ||
* | ||
* @param string method Connection method (eg GET, POST) | ||
* @param string url URL for the connection. | ||
* @param boolean async Asynchronous connection. Default is true. | ||
* @param string user Username for basic authentication (optional) | ||
* @param string password Password for basic authentication (optional) | ||
*/ | ||
this.open = function(method, url, async, user, password) { | ||
settings = { | ||
"method": method, | ||
"url": url.toString(), | ||
"async": (typeof async !== "boolean" ? true : async), | ||
"user": user || null, | ||
"password": password || null | ||
}; | ||
/** | ||
* Aborts a request. | ||
*/ | ||
this.abort = function() { | ||
headers = defaultHeaders; | ||
this.readyState = this.UNSENT; | ||
this.responseText = ""; | ||
this.responseXML = ""; | ||
}; | ||
/** | ||
* Changes readyState and calls onreadystatechange. | ||
* | ||
* @param int state New state | ||
*/ | ||
var setState = function(state) { | ||
self.readyState = state; | ||
if (typeof self.onreadystatechange === "function") { | ||
self.onreadystatechange(); | ||
} | ||
} | ||
this.abort(); | ||
setState(this.OPENED); | ||
}; | ||
/** | ||
* Sets a header for the request. | ||
* | ||
* @param string header Header name | ||
* @param string value Header value | ||
*/ | ||
this.setRequestHeader = function(header, value) { | ||
if (this.readyState != this.OPENED) { | ||
throw "INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN"; | ||
} | ||
if (sendFlag) { | ||
throw "INVALID_STATE_ERR: send flag is true"; | ||
} | ||
headers[header] = value; | ||
}; | ||
/** | ||
* Gets a header from the server response. | ||
* | ||
* @param string header Name of header to get. | ||
* @return string Text of the header or null if it doesn't exist. | ||
*/ | ||
this.getResponseHeader = function(header) { | ||
if (this.readyState > this.OPENED | ||
&& response.headers[header] | ||
&& !errorFlag | ||
) { | ||
return response.headers[header]; | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Gets all the response headers. | ||
* | ||
* @return string | ||
*/ | ||
this.getAllResponseHeaders = function() { | ||
if (this.readyState < this.HEADERS_RECEIVED || errorFlag) { | ||
return ""; | ||
} | ||
var result = ""; | ||
for (var i in response.headers) { | ||
result += i + ": " + response.headers[i] + "\r\n"; | ||
} | ||
return result.substr(0, result.length - 2); | ||
}; | ||
/** | ||
* Sends the request to the server. | ||
* | ||
* @param string data Optional data to send as request body. | ||
*/ | ||
this.send = function(data) { | ||
if (this.readyState != this.OPENED) { | ||
throw "INVALID_STATE_ERR: connection must be opened before send() is called"; | ||
} | ||
if (sendFlag) { | ||
throw "INVALID_STATE_ERR: send has already been called"; | ||
} | ||
var ssl = false; | ||
var url = Url.parse(settings.url); | ||
// Determine the server | ||
switch (url.protocol) { | ||
case 'https:': | ||
ssl = true; | ||
// SSL & non-SSL both need host, no break here. | ||
case 'http:': | ||
var host = url.hostname; | ||
break; | ||
case undefined: | ||
case '': | ||
var host = "localhost"; | ||
break; | ||
default: | ||
throw "Protocol not supported."; | ||
} | ||
// Default to port 80. If accessing localhost on another port be sure | ||
// to use http://localhost:port/path | ||
var port = url.port || (ssl ? 443 : 80); | ||
// Add query string if one is used | ||
var uri = url.pathname + (url.search ? url.search : ''); | ||
// Set the Host header or the server may reject the request | ||
this.setRequestHeader("Host", host); | ||
// Set Basic Auth if necessary | ||
if (settings.user) { | ||
if (typeof settings.password == "undefined") { | ||
settings.password = ""; | ||
} | ||
var authBuf = new Buffer(settings.user + ":" + settings.password); | ||
headers["Authorization"] = "Basic " + authBuf.toString("base64"); | ||
} | ||
// Set content length header | ||
if (settings.method == "GET" || settings.method == "HEAD") { | ||
data = null; | ||
} else if (data) { | ||
this.setRequestHeader("Content-Length", Buffer.byteLength(data)); | ||
if (!headers["Content-Type"]) { | ||
this.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); | ||
} | ||
} | ||
var options = { | ||
host: host, | ||
port: port, | ||
path: uri, | ||
method: settings.method, | ||
headers: headers | ||
}; | ||
// Reset error flag | ||
errorFlag = false; | ||
// Handle async requests | ||
if(!settings.hasOwnProperty("async") || settings.async) { | ||
// Use the proper protocol | ||
var doRequest = ssl ? https.request : http.request; | ||
// Request is being sent, set send flag | ||
sendFlag = true; | ||
// As per spec, this is called here for historical reasons. | ||
if (typeof self.onreadystatechange === "function") { | ||
self.onreadystatechange(); | ||
} | ||
// Create the request | ||
request = doRequest(options, function(resp) { | ||
response = resp; | ||
response.setEncoding("utf8"); | ||
setState(self.HEADERS_RECEIVED); | ||
self.status = response.statusCode; | ||
response.on('data', function(chunk) { | ||
// Make sure there's some data | ||
if (chunk) { | ||
self.responseText += chunk; | ||
} | ||
// Don't emit state changes if the connection has been aborted. | ||
if (sendFlag) { | ||
setState(self.LOADING); | ||
} | ||
}); | ||
response.on('end', function() { | ||
if (sendFlag) { | ||
// Discard the 'end' event if the connection has been aborted | ||
setState(self.DONE); | ||
sendFlag = false; | ||
} | ||
}); | ||
response.on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
}).on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
// Node 0.4 and later won't accept empty data. Make sure it's needed. | ||
if (data) { | ||
request.write(data); | ||
} | ||
request.end(); | ||
} else { // Synchronous | ||
// Create a temporary file for communication with the other Node process | ||
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('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');" | ||
+ "});" | ||
+ "response.on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ "}).on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"") | ||
+ "req.end();"; | ||
// Start the other Node Process, executing this string | ||
syncProc = spawn(process.argv[0], ["-e", execString]); | ||
while((self.responseText = fs.readFileSync(syncFile, 'utf8')) == "") { | ||
// Wait while the file is empty | ||
} | ||
// Kill the child process once the file has data | ||
syncProc.stdin.end(); | ||
// Remove the temporary file | ||
fs.unlinkSync(syncFile); | ||
if(self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) { | ||
// If the file returned an error, handle it | ||
var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, ""); | ||
self.handleError(errorObj); | ||
} else { | ||
// If the file returned okay, parse its data and move to the DONE state | ||
self.status = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/, "$1"); | ||
self.responseText = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/, "$1"); | ||
setState(self.DONE); | ||
} | ||
} | ||
}; | ||
this.handleError = function(error) { | ||
this.status = 503; | ||
this.statusText = error; | ||
this.responseText = error.stack; | ||
errorFlag = true; | ||
setState(this.DONE); | ||
}; | ||
/** | ||
* Aborts a request. | ||
*/ | ||
this.abort = function() { | ||
if (request) { | ||
request.abort(); | ||
request = null; | ||
} | ||
headers = defaultHeaders; | ||
this.responseText = ""; | ||
this.responseXML = ""; | ||
errorFlag = true; | ||
if (this.readyState !== this.UNSENT | ||
&& (this.readyState !== this.OPENED || sendFlag) | ||
&& this.readyState !== this.DONE) { | ||
sendFlag = false; | ||
setState(this.DONE); | ||
} | ||
this.readyState = this.UNSENT; | ||
}; | ||
/** | ||
* Changes readyState and calls onreadystatechange. | ||
* | ||
* @param int state New state | ||
*/ | ||
var setState = function(state) { | ||
self.readyState = state; | ||
if (typeof self.onreadystatechange === "function") { | ||
self.onreadystatechange(); | ||
} | ||
}; | ||
}; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
15049
423
30
0
6