Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit c98ddc956e79f75dd4236ccc66cbca5af8c8c48a 0 parents
@bmeck bmeck authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
45 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 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 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 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 package.json
@@ -0,0 +1,6 @@
+{
+ "name": "carapace",
+ "dependencies": {
+ "dnode": "0.7.x"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.