Permalink
Browse files

plugin example made, readme started

  • Loading branch information...
1 parent c98ddc9 commit dbfffd8e0f56238fdfa1be832808cacebdff387b @bmeck bmeck committed Jul 15, 2011
Showing with 122 additions and 64 deletions.
  1. +19 −0 LICENSE
  2. +40 −0 Readme.md
  3. +19 −6 lib/carapace.js → bin/carapace
  4. +14 −0 examples/loader.js
  5. +4 −0 examples/server.js
  6. +12 −0 lib/index.js
  7. +1 −2 lib/plugins/chdir.js
  8. +1 −1 lib/plugins/chroot.js
  9. +5 −0 lib/plugins/heartbeat.js
  10. +0 −54 lib/plugins/proxy.js
  11. +7 −1 package.json
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 Nodejitsu Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -0,0 +1,40 @@
+# Carapace
+
+## What is carapace
+
+Carapace is an in process wrapper for Node.js applications.
+It provides a bridge for setting up applications and extended communications.
+It also provides a plugin system to ease development of applications that need to be private.
+
+## Example workflow
+
+On terminal one: open up a carapace.
+```
+carapace ./test
+```
+
+On terminal two: chroot the process.
+Then tell carapace to run a script *server.js* from where we were chrooted.
+```
+//
+// Connect to the carapace
+//
+var carapace = require('carapace');
+require('dnode').connect(__dirname + '/test', function(client, conn) {
+ //
+ // Tell the carapace we have a new plugin to be loaded
+ //
+ client.emit('plugin',carapace.plugins.chroot, function() {
+ //
+ // Tell carapace we have a new directory to use as root
+ //
+ client.emit('chroot:root','..', function() {
+ //
+ // Tell carapace to run a script
+ //
+ client.emit('run', 'server.js');
+ });
+ })
+});
+
+```
View
25 lib/carapace.js → bin/carapace 100644 → 100755
@@ -18,9 +18,10 @@ var bridgePath = process.argv.splice(2,1)[0];
var bridgeServer = net.createServer();
bridgeServer.listen(bridgePath);
//
-// ghetto hookio shim
+// ghetto hookio shim until we iron out some domain socket bugs
//
var carapace = new EventEmitter();
+
var bridge = new dnode({
emit: function() {
//
@@ -32,14 +33,26 @@ var bridge = new dnode({
bridge.listen(bridgeServer);
carapace.on('plugin',function(toRequire, done) {
- require(toRequire)(carapace);
+ try {
+ require(toRequire)(carapace);
+ }
+ catch(e) {
+ if(done) return done(e);
+ }
+ if(done) return done();
+ return undefined;
});
-
+var running = false;
carapace.on('run', function(script, done) {
- process.argv.splice(1,1,fs.realpathSync(require.resolve(path.join(process.cwd(),carapace.config.script))));
+ if(running) {
+ if(done) return done('Cannot spawn a new script, one is already running.');
+ return undefined;
+ }
+ running = true;
+ process.argv.splice(1,1,fs.realpathSync(require.resolve(path.join(process.cwd(),script))));
require('module').Module._cache = {};
//
// Next tick to prevent a leak from arguments
//
- process.nextTick(function(){require('module').Module.runMain()});
-})
+ return process.nextTick(function(){require('module').Module.runMain()});
+});
View
@@ -0,0 +1,14 @@
+var path = require('path');
+var carapace = require('../');
+var bridgePath = path.resolve(process.argv[2]);
+console.log('connecting to ' + bridgePath)
+require('dnode').connect(bridgePath, function(client, conn) {
+ console.log('loading plugin ' + carapace.plugins.chroot);
+ client.emit('plugin',carapace.plugins.chroot, function() {
+ console.log('plugin done')
+ console.dir(arguments)
+ client.emit('chroot:root','..', function() {
+ client.emit('run', 'examples/server.js');
+ });
+ })
+});
View
@@ -0,0 +1,4 @@
+require('http').createServer(function(req,res) {
+ res.end('CWD: ' + process.cwd());
+}).listen(1337);
+console.dir('server running on 1337')
View
@@ -0,0 +1,12 @@
+var fs = require('fs');
+var path = require('path');
+carapace = module.exports;
+//
+// Plugins list
+//
+carapace.plugins = {};
+
+var plugins = fs.readdirSync(path.join(__dirname,'../lib/plugins'));
+plugins.forEach(function(name){
+ carapace.plugins[name.replace(/\.js$/,'')] = path.join(__dirname,'../lib/plugins',name);
+});
View
@@ -2,7 +2,6 @@ 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();
+ if(done) done();
});
}
View
@@ -3,6 +3,6 @@ var path = require('path');
module.exports = function (carapace) {
carapace.on('chroot:root',function (value, done) {
daemon.chroot(path.resolve(value));
- done();
+ if(done) done();
});
}
View
@@ -0,0 +1,5 @@
+module.exports = function (carapace) {
+ carapace.on('heartbeat',function (done) {
+ if(done) done();
+ });
+}
View
@@ -1,54 +0,0 @@
-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;
- }
-
-}
View
@@ -1,6 +1,12 @@
{
"name": "carapace",
+ "version": "0.0.0",
+ "main": "lib/index.js",
"dependencies": {
- "dnode": "0.7.x"
+ "dnode": "0.7.x",
+ "daemon": "0.3.x"
+ },
+ "bin": {
+ "carapace": "./bin/carapace"
}
}

0 comments on commit dbfffd8

Please sign in to comment.