Permalink
Browse files

[api] now have a javascript api to better deal w/ bootstrappers like …

…forever & better example
  • Loading branch information...
1 parent 8b93dc8 commit 47dd9056db40b80f86654a356fbce3a51d82ba05 @bmeck bmeck committed Jul 18, 2011
View
2 .gitignore
@@ -1,3 +1,5 @@
node_modules
+build/
.DS_Store
.lockScript
+.lock-wscript
View
3 .npmignore
@@ -0,0 +1,3 @@
+.lockScript
+.DS_Store
+.gitignore
View
9 addon/.lock-wscript
@@ -0,0 +1,9 @@
+argv = ['/usr/local/bin/node-waf', 'configure', 'build']
+blddir = '/home/bradley/Documents/nodejitsu/carapace/build'
+commands = {'dist': 0, 'configure': True, 'distcheck': 0, 'install': 0, 'build': True, 'clean': 0, 'distclean': 0, 'check': 0, 'uninstall': 0}
+cwd = '/home/bradley/Documents/nodejitsu/carapace'
+environ = {'npm_package_bin_carapace': './bin/carapace', 'npm_config_save': 'false', 'npm_config_color': 'true', 'npm_config_searchopts': '', 'npm_config_registry': 'http://registry.npmjs.org/', 'npm_config_group': '1000', 'npm_config_browser': 'google-chrome', 'npm_config_global': 'false', 'HOME': '/home/bradley', 'DISPLAY': ':0', 'LANG': 'en_US.UTF-8', 'SHELL': '/bin/bash', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share/:/usr/share/', 'MANDATORY_PATH': '/usr/share/gconf/gnome.mandatory.path', 'npm_package_dependencies_dnode': '0.7.x', 'npm_config_pre': 'false', 'WINDOWID': '77594662', 'npm_config_prefix': '/usr/local', 'npm_package_dependencies_daemon': '0.3.x', 'npm_config_searchexclude': 'null', 'npm_config_loglevel': 'warn', 'npm_config_logfd': '2', 'npm_config_tmp': '/tmp', 'npm_package_engines_node': '*', 'npm_config_argv': '"install"', 'npm_package_scripts_preinstall': 'node-waf clean || true; node-waf configure build', 'npm_config_tar': 'tar', 'npm_config_viewer': 'man', 'npm_config_node_version': 'v0.4.8', 'npm_package_main': 'lib/index.js', 'npm_config_init_version': '0.0.0', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-wwrdTe0hFh,guid=d1eaf02c9c9bb489be64c27600000012', 'ORBIT_SOCKETDIR': '/tmp/orbit-bradley', 'DEFAULTS_PATH': '/usr/share/gconf/gnome.default.path', 'npm_lifecycle_event': 'preinstall', 'npm_config_rollback': 'true', 'DESKTOP_SESSION': 'gnome', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'GTK_MODULES': 'canberra-gtk-module', 'npm_config_init_author_name': '', 'npm_config_link': 'false', 'npm_config_globalignorefile': '/usr/local/etc/npmignore', 'npm_config_version': 'false', 'npm_config_usage': 'false', 'npm_config_shell': '/bin/bash', 'npm_config_force': 'false', 'npm_config_username': 'bradleymeck', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:', 'npm_config_ignore': '', 'npm_config_user': '0', 'USERNAME': 'bradley', 'npm_config_globalconfig': '/usr/local/etc/npmrc', 'npm_package_name': 'carapace', 'npm_config_userconfig': '/home/bradley/.npmrc', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'npm_config_parseable': 'false', 'npm_config_dev': 'false', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'npm_config_rebuild_bundle': 'true', 'npm_config_userignorefile': '/home/bradley/.npmignore', 'LOGNAME': 'bradley', 'USER': 'bradley', 'PATH': '/home/bradley/Documents/nodejitsu/carapace/node_modules/.bin:/home/bradley/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games', 'GNOME_KEYRING_CONTROL': '/tmp/keyring-PT0pWG', 'npm_config_gzipbin': 'gzip', 'WINDOWPATH': '7', 'SSH_AGENT_PID': '1157', 'TERM': 'xterm', 'XAUTHORITY': '/var/run/gdm/auth-for-bradley-XzmvHO/database', 'LANGUAGE': 'en_US:en', 'SESSION_MANAGER': 'local/macaroon:@/tmp/.ICE-unix/1123,unix/macaroon:/tmp/.ICE-unix/1123', 'SHLVL': '1', 'npm_config_always_auth': 'false', 'npm_config_init_author_url': '', 'npm_config_bindist': '0.4-ares1.7.4-ev4.4-openssl0.9.8o-v83.1.8.16-linux-2.6.38-10-generic', 'COMPIZ_CONFIG_PROFILE': 'ubuntu', 'npm_config_cache': '/home/bradley/.npm', 'npm_config_email': 'bradley.meck@gmail.com', 'GDM_KEYBOARD_LAYOUT': 'us', 'npm_config_proxy': 'null', 'npm_config_showlevel': '2', 'npm_config_unsafe_perm': 'true', 'npm_config_depth': 'Infinity', 'SSH_AUTH_SOCK': '/tmp/keyring-PT0pWG/ssh', 'GDMSESSION': 'gnome', 'npm_config_long': 'false', 'npm_config_editor': 'vi', 'npm_package_version': '0.0.0', '_': '/usr/local/bin/npm', 'XDG_SESSION_COOKIE': '1b384bc8d6c8a2c7184e00ad00000007-1310566389.798192-878819823', 'npm_config_init_author_email': '', 'npm_config_npat': 'false', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-gnome:/etc/xdg', 'npm_config_onload_script': 'false', 'UBUNTU_MENUPROXY': 'libappmenu.so', 'OLDPWD': '/home/bradley/Documents/nodejitsu/carapace/examples', 'npm_config_outfd': '1', 'npm_config_bin_publish': 'false', 'GDM_LANG': 'en_US', 'npm_lifecycle_script': 'node-waf clean || true; node-waf configure build', 'PWD': '/home/bradley/Documents/nodejitsu/carapace', 'COLORTERM': 'gnome-terminal', 'npm_config_description': 'true', 'npm_config_tag': 'latest', 'npm_config_npaturl': 'http://npat.npmjs.org/'}
+files = []
+hash = 0
+options = {'compile_targets': None, 'force': False, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'destdir': '', 'keep': False, 'zones': '', 'blddir': '', 'prefix': '/usr/local/', 'jobs': 4, 'srcdir': '', 'check_cxx_compiler': 'g++ icpc sunc++'}
+srcdir = '/home/bradley/Documents/nodejitsu/carapace/addon'
View
22 addon/ev.cc
@@ -0,0 +1,22 @@
+#include <node/ev/ev.h>
+#include <node/v8.h>
+
+using namespace v8;
+
+Handle<Value> Unref(const Arguments &args) {
+ HandleScope scope;
+ ev_unref();
+ scope.Close(Undefined());
+}
+Handle<Value> Ref(const Arguments &args) {
+ HandleScope scope;
+ ev_ref();
+ scope.Close(Undefined());
+}
+
+extern "C" void init(Handle<Object> target) {
+ HandleScope scope;
+ target->Set(String::NewSymbol("Unref"),FunctionTemplate::New(Unref)->GetFunction());
+ target->Set(String::NewSymbol("Ref"),FunctionTemplate::New(Ref)->GetFunction());
+ scope.Close(target);
+}
View
67 bin/carapace
@@ -1,65 +1,14 @@
#!/usr/local/bin/node
//
-// Carapace.js
+// Carapace bridge ...
//
-// Setup and bidirectional communication environment for processes
+// bridge - port / path to make bridge server at
+// ... - argv not including the script
//
-var libev = require('../build/default/libev');
-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 = path.resolve(process.argv.splice(2,1)[0]);
-var bridgeServer = net.createServer();
-bridgeServer.listen(bridgePath);
-//
-// ghetto hookio shim until we iron out some domain socket bugs
-//
-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);
-bridgeServer.on('connection', function(conn) {
- libev.Unref();
- conn.on('close',function(){
- libev.Ref();
- })
-});
+var carapace = require('../');
+var path = require('path');
-carapace.on('plugin',function(toRequire, done) {
- 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) {
- if(running) {
- if(done) return done('Cannot spawn a new script, one is already running.');
- return undefined;
- }
- libev.Unref();
- 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
- //
- return process.nextTick(function(){require('module').Module.runMain()});
-});
+var bridgePath = path.resolve(process.argv[2]);
+var argv = process.argv.slice(3);
+carapace.wrap(bridgePath);
View
BIN build/.wafpickle-7
Binary file not shown.
View
2 build/c4che/build.config.py
@@ -0,0 +1,2 @@
+version = 0x105016
+tools = [{'tool': 'ar', 'tooldir': None, 'funs': None}, {'tool': 'cxx', 'tooldir': None, 'funs': None}, {'tool': 'gxx', 'tooldir': None, 'funs': None}, {'tool': 'compiler_cxx', 'tooldir': None, 'funs': None}, {'tool': 'node_addon', 'tooldir': None, 'funs': None}]
View
49 build/c4che/default.cache.py
@@ -0,0 +1,49 @@
+AR = '/usr/bin/ar'
+ARFLAGS = 'rcs'
+CCFLAGS = ['-g']
+CCFLAGS_MACBUNDLE = ['-fPIC']
+CCFLAGS_NODE = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
+CC_VERSION = ('4', '5', '2')
+COMPILER_CXX = 'g++'
+CPP = '/usr/bin/cpp'
+CPPFLAGS_NODE = ['-D_GNU_SOURCE', '-DEV_MULTIPLICITY=0']
+CPPPATH_NODE = '/usr/local/include/node'
+CPPPATH_ST = '-I%s'
+CXX = ['/usr/bin/g++']
+CXXDEFINES_ST = '-D%s'
+CXXFLAGS = ['-g']
+CXXFLAGS_DEBUG = ['-g']
+CXXFLAGS_NODE = ['-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64']
+CXXFLAGS_RELEASE = ['-O2']
+CXXLNK_SRC_F = ''
+CXXLNK_TGT_F = ['-o', '']
+CXX_NAME = 'gcc'
+CXX_SRC_F = ''
+CXX_TGT_F = ['-c', '-o', '']
+DEST_BINFMT = 'elf'
+DEST_CPU = 'x86_64'
+DEST_OS = 'linux'
+FULLSTATIC_MARKER = '-static'
+LIBDIR = '/home/bradley/.node_libraries'
+LIBPATH_NODE = '/usr/local/lib'
+LIBPATH_ST = '-L%s'
+LIB_ST = '-l%s'
+LINKFLAGS_MACBUNDLE = ['-bundle', '-undefined', 'dynamic_lookup']
+LINK_CXX = ['/usr/bin/g++']
+NODE_PATH = '/home/bradley/.node_libraries'
+PREFIX = '/usr/local'
+PREFIX_NODE = '/usr/local'
+RANLIB = '/usr/bin/ranlib'
+RPATH_ST = '-Wl,-rpath,%s'
+SHLIB_MARKER = '-Wl,-Bdynamic'
+SONAME_ST = '-Wl,-h,%s'
+STATICLIBPATH_ST = '-L%s'
+STATICLIB_MARKER = '-Wl,-Bstatic'
+STATICLIB_ST = '-l%s'
+macbundle_PATTERN = '%s.bundle'
+program_PATTERN = '%s'
+shlib_CXXFLAGS = ['-fPIC', '-DPIC']
+shlib_LINKFLAGS = ['-shared']
+shlib_PATTERN = 'lib%s.so'
+staticlib_LINKFLAGS = ['-Wl,-Bstatic']
+staticlib_PATTERN = 'lib%s.a'
View
36 build/config.log
@@ -0,0 +1,36 @@
+# project noname (1.0) configured on Fri Jul 15 14:25:39 2011 by
+# waf 1.5.16 (abi 7, python 20701f0 on linux2)
+# using /usr/local/bin/node-waf configure build
+#
+
+----------------------------------------
+Checking for program g++ or c++
+ find program=['g++', 'c++'] paths=[] var='CXX'
+ -> '/usr/bin/g++'
+
+----------------------------------------
+Checking for program cpp
+ find program=['cpp'] paths=[] var='CPP'
+ -> '/usr/bin/cpp'
+
+----------------------------------------
+Checking for program ar
+ find program=['ar'] paths=[] var='AR'
+ -> '/usr/bin/ar'
+
+----------------------------------------
+Checking for program ranlib
+ find program=['ranlib'] paths=[] var='RANLIB'
+ -> '/usr/bin/ranlib'
+
+----------------------------------------
+Checking for g++
+ok
+
+----------------------------------------
+Checking for node path
+not found
+
+----------------------------------------
+Checking for node prefix
+ok /usr/local
View
BIN build/default/addon/ev_1.o
Binary file not shown.
View
BIN build/default/libev.node
Binary file not shown.
View
14 examples/listener.js
@@ -0,0 +1,14 @@
+var path = require('path');
+var bridgePath = path.resolve(process.argv[2]);
+
+console.log('connecting to ' + bridgePath)
+require('dnode').connect(bridgePath, function(client, conn) {
+ console.log('connected');
+ client.on('heartbeat', function() {
+ console.log('listened to heartbeat')
+ })
+ conn.on('end',function(){
+ console.log('Carapace connection had ended.')
+ });
+});
+
View
42 examples/loader.js
@@ -1,20 +1,28 @@
-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);
- //
- // Connections to Carapace do not keep it open!
- //
- conn.on('end',function(){
- console.log('Carapace connection had ended.')
- });
- client.emit('plugin',carapace.plugins.chroot, function() {
- console.log('plugin done')
- client.emit('chroot:root','..', function() {
- client.emit('run', 'examples/server.js');
+var path = require('path');
+var bridge = process.argv.splice(2,1)[0];
+carapace.wrap(bridge, function() {
+ carapace.emit('plugin',carapace.plugins.heartbeat, function(err) {
+ if(err) {
+ console.error(err.stack);
+ process.exit();
+ }
+ carapace.emit('plugin',carapace.plugins.chroot, function(err) {
+ if(err) {
+ console.error(err.stack);
+ process.exit();
+ }
+ carapace.emit('plugin',carapace.plugins.chdir, function(err) {
+ if(err) {
+ console.error(err.stack);
+ process.exit();
+ }
+ carapace.emit('chroot:root','./tobechrooted', function(err) {
+ carapace.emit('chdir:path','.', function(err) {
+ carapace.emit('run',['./server.js']);
+ });
+ });
+ });
});
- })
+ });
});
View
4 examples/server.js → examples/tobechrooted/server.js
@@ -3,7 +3,3 @@ var server = require('http').createServer(function(req,res) {
});
server.listen(1337);
console.dir('server running on 1337')
-//
-// Close after 5 seconds
-//
-setTimeout(function(){server.close();},5000)
View
61 lib/index.js
@@ -1,6 +1,11 @@
+var libev = require('../build/default/libev');
+var net = require('net');
+var dnode = require('dnode');
var fs = require('fs');
var path = require('path');
-carapace = module.exports;
+var EventEmitter = require('events').EventEmitter;
+module.exports = carapace = new EventEmitter();
+for(var k in carapace) carapace[k] = carapace[k];
//
// Plugins list
//
@@ -10,3 +15,57 @@ var plugins = fs.readdirSync(path.join(__dirname,'../lib/plugins'));
plugins.forEach(function(name){
carapace.plugins[name.replace(/\.js$/,'')] = path.join(__dirname,'../lib/plugins',name);
});
+
+carapace.wrap = function wrap(bridgeServer, done) {
+ if(typeof bridgeServer === 'string' || typeof bridgeServer === 'number') {
+ var bridgePath = bridgeServer
+ bridgeServer = net.createServer();
+ bridgeServer.listen(bridgePath, function() {
+ var bridge = new dnode(carapace);
+ bridge.listen(bridgeServer);
+ bridgeServer.on('connection', function(conn) {
+ libev.Unref();
+ conn.on('close',function(){
+ libev.Ref();
+ });
+ });
+
+ carapace.on('plugin',function(toRequire, done) {
+ try {
+ require(toRequire)(carapace);
+ }
+ catch(e) {
+ if(done) return done(e);
+ }
+ if(done) return done();
+ return undefined;
+ });
+ var running = false;
+ carapace.on('run', function(argv, done) {
+ if(running) {
+ if(done) return done('Cannot spawn a new script, one is already running.');
+ return undefined;
+ }
+ libev.Unref();
+ running = true;
+
+ argv[0] = fs.realpathSync(require.resolve(path.join(process.cwd(),argv[0])));
+ for(var i = 0; i < argv.length;) {
+ var item = argv[i];
+ process.argv[++i] = item;
+ }
+ process.argv.splice(i,i - process.argv.length);
+ require('module').Module._cache = {};
+ //
+ // Next tick to prevent a leak from arguments
+ //
+ process.nextTick(function() {
+ require('module').Module.runMain();
+ });
+ if(done) return done();
+ return undefined;
+ });
+ if(done) done();
+ });
+ }
+}
View
10 lib/plugins/heartbeat.js
@@ -1,5 +1,9 @@
+//
+// Fires off functions at end of a heartbeat event
+//
module.exports = function (carapace) {
- carapace.on('heartbeat',function (done) {
- if(done) done();
- });
+ setInterval(function(){
+ console.log('beating')
+ carapace.emit('heartbeat');
+ },1000);
}
View
14 wscript
@@ -0,0 +1,14 @@
+srcdir = 'addon'
+blddir = 'build'
+
+def set_options(opt):
+ opt.tool_options('compiler_cxx')
+
+def configure(conf):
+ conf.check_tool('compiler_cxx')
+ conf.check_tool('node_addon')
+
+def build(bld):
+ obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
+ obj.target = 'libev'
+ obj.source = 'addon/ev.cc'

0 comments on commit 47dd905

Please sign in to comment.