Created
August 1, 2011 02:31
-
-
Save mjhm/1117479 to your computer and use it in GitHub Desktop.
node.js debug server proxy with automatic launching of a target file named "main.js" as a child.
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
var require; | |
var net = require("net"); | |
var sys = require("sys"); | |
var fs = require("fs"); | |
var child_process = require("child_process"); | |
var encoding = "binary"; | |
var remote; | |
var main; | |
var comLogFd = fs.openSync('/home/mashare/Dropbox/mashare/server/control/comLogFd', 'w'); | |
console.log('comLogFd' + comLogFd); | |
fs.writeSync(comLogFd, 'Debug Proxy Log:'); | |
var server = net.createServer(function (proxyStream) { | |
proxyStream.setEncoding(encoding); | |
proxyStream.setNoDelay(); | |
proxyStream.addListener('connect', function () { | |
console.log('proxy connect request'); | |
console.log('1. main = ' + String(main)); | |
var mainReady = false; | |
if (main) { | |
console.log('killing previous main'); | |
main.removeAllListeners('exit'); | |
main.kill(); | |
} | |
if (remote) { | |
console.log('ending remote connection'); | |
remote.removeAllListeners('end'); | |
remote.end(); | |
} | |
main = child_process.spawn('node', ['--debug', 'main.js']); | |
console.log('2. main = ' + String(main)); | |
main.stdout.setEncoding('utf8'); | |
main.stderr.setEncoding('utf8'); | |
main.stderr.on('data', function (data) { | |
console.log('main: stderr: ' + data); | |
}); | |
main.on('exit', function (code) { | |
console.log('main exited with code ' + code); | |
}); | |
main.stdout.on('data', function (data) { | |
console.log('main: ' + data); | |
if (! mainReady && data.slice(0, 5) === 'ready') { | |
mainReady = true; | |
console.log('connecting to localhost:5858'); | |
remote = net.createConnection(5858, "localhost"); | |
remote.setEncoding(encoding); | |
remote.setTimeout(0); | |
remote.addListener("connect", function () { | |
console.log('remote connect'); | |
}); | |
remote.addListener("data", function (data) { | |
console.log('rem->prxy: ' + String(data).slice(0, 120)); | |
fs.writeSync(comLogFd, '\nrem->prxy:\n'); | |
fs.writeSync(comLogFd, data); | |
if (String(data).length > 120) { | |
console.log(' ...' + String(data).slice(-Math.min(80, String(data).length - 120))); | |
} | |
proxyStream.write(data, encoding); | |
// proxyStream.write(data); | |
}); | |
remote.addListener("end", function () { | |
console.log('remote end event'); | |
proxyStream.end(); | |
remote.end(); | |
}); | |
} | |
}); | |
console.log('finished proxy connect'); | |
}); | |
proxyStream.addListener('data', function (data) { | |
console.log('prxy->rem:' + String(data).slice(0, 120)); | |
fs.writeSync(comLogFd, '\nprxy->rem:\n'); | |
fs.writeSync(comLogFd, data); | |
remote.write(data, encoding); | |
}); | |
proxyStream.addListener('drain', function () { | |
console.log('proxy write drained'); | |
}); | |
proxyStream.addListener('end', function () { | |
console.log('proxy end event'); | |
main.kill(); | |
proxyStream.end(); | |
if (remote) { | |
remote.end(); | |
} | |
}); | |
}); | |
server.listen(7000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment