Skip to content
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
112 changes: 32 additions & 80 deletions lib/jellyproxy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
var http = require('http')
, url = require('url')
, path = require('path')
, paperboy = require('paperboy')
, WEBROOT = path.join(path.dirname(__filename), '../static')
, h = {accept:'application/json', 'content-type':'application/json'}
, tag = "</head>";
, tag = "</head>"
, jellyProxy = require('jelly-proxy')
, send = require("send")

var payload = function(){
var cache = new Date().getTime();
var includes = ('<script type="text/javascript" ' +
'src="/_jellyfish/serv/jquery-1.4.4.min.js?'+cache+'"></script>');
includes += ('<script type="text/javascript"' +
'src="/_jellyfish/serv/nemato.js?'+cache+'"></script>');
return includes
}

var opts = {
tag: tag
, payload : payload
}


exports.jellyproxy = function(_this) {

// Finish the request
var finish = function(req, res, data) {
var dataString = JSON.stringify(data);
Expand All @@ -16,8 +33,9 @@ exports.jellyproxy = function(_this) {
res.write(dataString);
res.end();
};

var server = http.createServer(function (req, res) {


var handleJelly = function(req, res){
var ip = req.connection.remoteAddress;
var uri = url.parse(req.url, true);

Expand Down Expand Up @@ -114,83 +132,17 @@ exports.jellyproxy = function(_this) {
//name and serve it from the static directory
var fname = req.url.split("/");
req.url = req.url.replace(pathname, "/" + fname[fname.length -1]);
send(req, req.url).root(WEBROOT).pipe(res);
}
}

paperboy
.deliver(WEBROOT, req, res)
.otherwise(function() {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('Sorry, no paper this morning!');
res.close();
});
}
else {
// Actual proxying happens here
var c = http.createClient(uri.port, uri.hostname);

// Stop from requesting gzip
req.headers['accept-encoding'] = "text/html";

// allow additional default headers
var headers={},h;

for(h in req.headers)headers[h]=req.headers[h];

if(_this.default_headers)for(h in _this.default_headers)headers[h]=_this.default_headers[h];

var proxyRequest = c.request(req.method, pathname, headers);

var clientError = function (e) {
if (!res._header) {
res.writeHead(502, {})
res.end();
} else {
res.end();
// This should not be debug, it should always display because it's really bad!
_this.log.push(["client error after writeHead was called. "+e.stack]);
}
}

c.on("error", clientError);
proxyRequest.on("error", clientError);

proxyRequest.addListener("response", function (response) {
if (response.headers['content-type'] &&
response.headers['content-type'].indexOf("text/html") != -1) {
delete response.headers['content-length'];
}
res.writeHead(response.statusCode, response.headers);
response.addListener("data", function (chunk) {
// modify the html content
if (response.headers['content-type'] &&
response.headers['content-type'].indexOf("text/html") != -1) {
if (chunk.toString().indexOf(tag)) {
var cache = new Date().getTime();
var includes = '<script type="text/javascript" src="/_jellyfish/serv/jquery-1.4.4.min.js?'+cache+'"></script>';
includes += '<script type="text/javascript" src="/_jellyfish/serv/nemato.js?'+cache+'"></script>';
includes += tag;
chunk = chunk.toString().replace(tag, includes);
}
}
res.write(chunk, 'binary');
})
response.addListener("end", function () {
res.end();
})
})
req.addListener("data", function (chunk) {
proxyRequest.write(chunk, 'binary');
})
req.addListener("end", function () {
proxyRequest.end();
})
}
});
var server = jellyProxy(opts, handleJelly, 80)// TODO don't assume port 80

//needs to be safer
server.listen(_this.port, function() {
_this.port = server.address().port;
});
//needs to be safer
server.listen(_this.port, function() {
_this.port = server.address().port;
});

_this.log.push(['output', 'started server', _this.tid+" on port "+_this.port]);
return server;
_this.log.push(['output', 'started server', _this.tid+" on port "+_this.port]);
return server;
};
17 changes: 11 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ jellyfish = function(b, url, cb) {
if (b) {
_this.start(b, cb);
}


process.on('uncaughtException', function (err) {
console.log(' \x1b[33mCaught exception: \x1b[0m :' + err);
console.log(' \x1b[33mException Stack: \x1b[0m :' + err.stack);
_this.log.push(['Caught exception: ' + err]);
_this.log.push([err.stack]);
});



};
jellyfish.prototype.__proto__ = EventEmitter.prototype;

Expand Down Expand Up @@ -183,9 +194,3 @@ process.on('exit', function () {
}
});

process.on('uncaughtException', function (err) {
console.log(' \x1b[33mCaught exception: \x1b[0m :' + err);
console.log(' \x1b[33mException Stack: \x1b[0m :' + err.stack);
_this.log.push(['Caught exception: ' + err]);
_this.log.push([err.stack]);
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
, "engines" : ["node"]
, "main" : "./lib/main"
, "dependencies" :
{ "paperboy" : "0.0.x"
, "request" : "1.9.x"
{
"request" : "1.9.x"
, "zombie" : "0.x"
, "wd" : "0.0.8"
, "jelly-proxy" : "0.2.0"
, "send" : "0.1.1"
}
, "bin" : { "jellyfish" : "./lib/bin.js" }
, "directories" : { "lib" : "./lib" }
Expand Down