Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Jul 14, 2011
0 parents commit c98ddc9
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
45 changes: 45 additions & 0 deletions lib/carapace.js
@@ -0,0 +1,45 @@
#!/usr/local/bin/node
//
// Carapace.js
//
// Setup and bidirectional communication environment for processes
//

var path = require('path');
var fs = require('fs');
var net = require('net');
var EventEmitter = require('events').EventEmitter;
var dnode = require('dnode');
//
// Carapace arguments are for the rooting environment
// * carapace.bridge
//
var bridgePath = process.argv.splice(2,1)[0];
var bridgeServer = net.createServer();
bridgeServer.listen(bridgePath);
//
// ghetto hookio shim
//
var carapace = new EventEmitter();
var bridge = new dnode({
emit: function() {
//
// DO NOT STIP OFF THE ASYNC CALLBACK ON THE END
//
carapace.emit.apply(carapace,arguments);
}
});
bridge.listen(bridgeServer);

carapace.on('plugin',function(toRequire, done) {
require(toRequire)(carapace);
});

carapace.on('run', function(script, done) {
process.argv.splice(1,1,fs.realpathSync(require.resolve(path.join(process.cwd(),carapace.config.script))));
require('module').Module._cache = {};
//
// Next tick to prevent a leak from arguments
//
process.nextTick(function(){require('module').Module.runMain()});
})
8 changes: 8 additions & 0 deletions lib/plugins/chdir.js
@@ -0,0 +1,8 @@
var path = require('path');
module.exports = function chdirPlugin(carapace) {
carapace.on('chdir:path',function (value, done) {
process.chdir(path.resolve(value));
console.log(process.cwd());
done();
});
}
8 changes: 8 additions & 0 deletions lib/plugins/chroot.js
@@ -0,0 +1,8 @@
var daemon = require('daemon');
var path = require('path');
module.exports = function (carapace) {
carapace.on('chroot:root',function (value, done) {
daemon.chroot(path.resolve(value));
done();
});
}
54 changes: 54 additions & 0 deletions lib/plugins/proxy.js
@@ -0,0 +1,54 @@
function toPort(x) {
return (x = Number(x)) >= 0 ? x : false;
}

module.exports = function (carapace,done) {
var net = require('net');
var netListen = net.Server.prototype._doListen;
var binding = process.binding('net');
var bindingBind = binding.bind;

function registerPort(desiredPort, actualPort) {
carapace.emit('proxy:map',desiredPort,actualPort);
}
//
// Bind clobber
// fd, port | unix, addr?
//
// Used to prevent a socket being bound to a port and instead use a different port
//
binding.bind = function bind() {
var fd = arguments[0];
var port = arguments[1];
port = toPort(port);
if(!port) {
return bindingBind.apply(this,arguments);
}
var desiredPort = port;
arguments[1] = undefined;
var result = bindingBind.apply(this,arguments);
var actualPort = binding.getsockname(fd).port;
registerPort(desiredPort,actualPort);
return result;
}

//
// Server _doListen clobber
//
// This needs to be done because listen uses a cached bind
// Listening on a port should be deferred to any port and a port mapping should be emitted
//
net.Server.prototype._doListen = function _doListen() {
var port = arguments[0];
port = toPort(port);
if(!port) {
return netListen.apply(this,arguments);
}
var desiredPort = port;
var result = netListen.apply(this,arguments);
var actualPort = this.address().port;
registerPort(desiredPort,actualPort);
return result;
}

}
6 changes: 6 additions & 0 deletions package.json
@@ -0,0 +1,6 @@
{
"name": "carapace",
"dependencies": {
"dnode": "0.7.x"
}
}

0 comments on commit c98ddc9

Please sign in to comment.