Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c98ddc9
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()}); | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "carapace", | ||
"dependencies": { | ||
"dnode": "0.7.x" | ||
} | ||
} |