Created
November 29, 2012 06:59
-
-
Save wscanf/4167276 to your computer and use it in GitHub Desktop.
a simple httpproxy that can beautify JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//a simple httpproxy that can beautify JavaScript | |
//fangyong 2012-11-29 [email protected] | |
//add javascript file names to the config array that you want to beautify | |
//change to your favorite port number | |
var config = [ | |
'ver.js', | |
'mi.Login_new', | |
'login_div.js', | |
'ping.js', | |
'crystal-min.js' | |
]; | |
config.port = 8084; | |
if(typeof exports !== 'undefined'){ | |
exports.config = config; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//$node jsproxy | |
//fangyong 2012-11-29 [email protected] | |
//when debugging a web page written by someone else, these compressed javascript files are impossible to debug.. they are single-lined, and they are often sized more than 6.5k ... | |
//now jsproxy comes to the rescue. configure which javascript files you want to beautify, set your browsers's proxy to jsproxy and you are all set. | |
//reqiures js_beautify in './' | |
//if it does not work for your other http connections(of course it does not, it's just a simple http proxy with 20+ lines of its core...), check out jsproxy.pac | |
//usually, it's safe to disable jsproxy when you have loaded these better javascript files, since they are often cached for a long time by the browser. | |
var config = require('./config').config; | |
var jsbeauty = require('./beautify').js_beautify; | |
var http = require('http'); | |
var url = require("url"); | |
var net = require("net"); | |
var zlib = require('zlib'); | |
var node_proxy = http.createServer(function (req, res) { | |
console.log(req.url); | |
var lo = false; | |
for (var i = 0; i < config.length; i++) { | |
if (req.url.indexOf(config[i]) != -1) { | |
//contains url | |
console.log('read from local'); | |
lo = true; | |
break; | |
}; | |
}; | |
var srvUrl = url.parse(req.url); | |
var proxy = http.createClient(srvUrl.port || 80, req.headers['host']); | |
var proxy_request = proxy.request(req.method,req.url,req.headers); | |
proxy.addListener('error',function(e){ | |
console.log("Got error: " + e.message); | |
}); | |
proxy_request.addListener('response',function(proxy_response){ | |
var data = []; | |
var readlen = 0; | |
if(!lo){ | |
res.writeHead(proxy_response.statusCode, proxy_response.headers); | |
} | |
proxy_response.addListener('data',function(chunk){ | |
if(lo){ | |
//buffer to data | |
data.push(chunk); | |
readlen += chunk.length; | |
}else{ | |
//directly send to client | |
res.write(chunk,'binary'); | |
} | |
}); | |
proxy_response.addListener('end',function(){ | |
if(lo){ | |
var buffer = null; | |
switch(data.length) { | |
case 0: buffer = new Buffer(0); | |
break; | |
case 1: buffer = data[0]; | |
break; | |
default: | |
buffer = new Buffer(readlen); | |
for (var i = 0, pos = 0, l = data.length; i < l; i++) { | |
var chunk = data[i]; | |
chunk.copy(buffer, pos); | |
pos += chunk.length; | |
} | |
break; | |
} | |
//buffer may be compressed by gzip | |
var encoding = proxy_response.headers['content-encoding'] || proxy_response.headers['Content-Encoding']; | |
console.log(encoding); | |
switch(encoding){ | |
case 'gzip': | |
case 'deflate': | |
zlib.unzip(buffer,function(err, resultBuf){ | |
if (!err) { | |
data = jsbeauty(resultBuf.toString()); | |
console.log("result:"+data); | |
zlib.gzip(data,function(err,resToClient){ | |
proxy_response.headers['content-length'] = resToClient.toString('binary').length; | |
res.writeHead(proxy_response.statusCode, proxy_response.headers); | |
res.write(resToClient.toString('binary'),'binary'); | |
res.end(); | |
}); | |
}; | |
}); | |
break; | |
default: | |
data = jsbeauty(buffer.toString()); | |
//console.log(data); | |
proxy_response.headers['content-length'] = data.length; | |
res.writeHead(proxy_response.statusCode, proxy_response.headers); | |
res.write(data); | |
res.end(); | |
break; | |
} | |
}else | |
res.end(); | |
}); | |
}); | |
req.addListener('data',function(chunk){ | |
proxy_request.write(chunk,'binary'); | |
}); | |
req.addListener('end',function(){ | |
console.log('end'); | |
proxy_request.end(); | |
}); | |
}); | |
node_proxy.on('connect', function(req, cltSocket, head) { | |
// connect to an origin server | |
var srvUrl = url.parse('http://' + req.url); | |
var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() { | |
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + | |
'Proxy-agent: Node-Proxy\r\n' + | |
'\r\n'); | |
srvSocket.write(head); | |
srvSocket.pipe(cltSocket); | |
cltSocket.pipe(srvSocket); | |
}); | |
}); | |
node_proxy.listen(config.port, "127.0.0.1"); | |
console.log('proxy server running at http://127.0.0.1:'+config.port+'/'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//fangyong 2012-11-29 [email protected] | |
//when it comes to HTTP 1.1's 'Connection:Keep-Alive', there are some bugs... | |
//it can be fixed by changing all the 'Connection' to 'close'...but it's a dirty hack. | |
//any help will be welcomed with these bugs. | |
//if it really bothers your browser, set browser's proxy to this pac file. | |
//IE: Internet Options -> Connections -> Lan Settings -> Use automatic configuration script -> Address : file://c:/path/to/pac | |
//chrome/firefox are also tested with this pac file. | |
//you may need to change the port and config array as you could have done in config.js | |
function regExpMatch(url, pattern) { | |
try { return new RegExp(pattern).test(url); } catch(ex) { return false; } | |
} | |
function FindProxyForURL(url, host) { | |
var config = [ | |
'ver.js', | |
'mi.Login_new', | |
'login_div.js', | |
'ping.js', | |
'crystal-min.js' | |
]; | |
for (var i = config.length - 1; i >= 0; i--) { | |
if(shExpMatch(url,'*'+config[i]+'*')) | |
return 'PROXY 127.0.0.1:8084'; | |
}; | |
return 'DIRECT'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment