Permalink
Browse files

Request remote scripts in parallel

  • Loading branch information...
1 parent 20e86df commit c0d50722116f938a3eac25e4aaa662864945b9e9 @marijnh committed Nov 5, 2012
Showing with 28 additions and 32 deletions.
  1. +28 −32 serve.js
View
60 serve.js
@@ -61,53 +61,49 @@ function readData(obj, c) {
http.createServer(function(req, resp) {
var question = req.url.indexOf("?");
var query = question == -1 ? {} : parseQuery(req.url.slice(question + 1));
- if (req.method == "POST" && req.headers["content-type"] == "application/x-www-form-urlencoded")
+ if (req.method == "POST" && req.headers["content-type"] == "application/x-www-form-urlencoded") {
readData(req, function(data) {
forEachIn(parseQuery(data), function(name, val) {
var current = query.hasOwnProperty(name) && query[name];
query[name] = current ? current.concat(val) : val;
});
respond(query, resp);
});
- else
+ } else {
respond(query, resp);
+ }
}).listen(8080, "localhost");
function gatherCode(direct, urls, c) {
- var files = [];
- // TODO more parallel
- function iter(i) {
- if (i == urls.length) {
+ var files = [], todo = urls.length + 1;
+ function done() {
+ if (--todo == 0) {
if (direct) files.push({string: direct});
- c(files);
+ c(files.filter(function(x){return x;}));
}
- else {
- var parsed = url.parse(urls[i]);
- if (/^https?:$/.test(parsed.protocol)) {
- var client = http.createClient(parsed.port || 80, parsed.hostname, parsed.protocol == "https:");
- var req = client.request("GET", parsed.pathname + (parsed.search || ""), {"Host": parsed.hostname});
- var chunks = [], redir = 0;
- req.on("response", function(resp) {
- if (resp.statusCode < 300) {
- resp.on("data", function(chunk) {chunks.push(chunk);});
- resp.on("end", function() {
- files.push({string: chunks.join(""), name: urls[i]});
- iter(i + 1);
- });
- resp.on("error", function(e) { iter(i + 1); });
- } else if (resp.statusCode < 400 && redir < 10 && resp.headers.location) {
- redir++;
- urls[i] = resp.headers.location;
- iter(i);
- } else iter(i + 1);
+ }
+ function handle(url, i, redir) {
+ var chunks = [];
+ var req = http.request(urls[i], function(resp) {
+ if (resp.statusCode < 300) {
+ resp.on("data", function(chunk) {chunks.push(chunk);});
+ resp.on("end", function() {
+ files[i] = {string: chunks.join(""), name: urls[i]};
+ done();
});
- req.on("error", function(e) { console.log(e);iter(i + 1); });
- req.end();
- }
- else iter(i + 1);
- }
+ resp.on("error", done);
+ } else if (resp.statusCode < 400 && redir < 10 && resp.headers.location) {
+ self(resp.headers.location, i, redir + 1);
+ } else done();
+ });
+ req.on("error", function(e) { console.log(e); done(); });
+ req.end();
}
- iter(0);
+ urls.forEach(function self(url, i) {
+ if (/^https?:/.test(url)) handle(url, i, 0);
+ else done();
+ });
+ done();
}
function respond(query, resp) {

0 comments on commit c0d5072

Please sign in to comment.