Ejemplo n.º 1
0
function checkUsage (f) {
    var _process = process;
    process = Hash.copy(process);
    var exit = false;
    process.exit = function () { exit = true };
    process.env = Hash.merge(process.env, { _ : 'node' });
    process.argv = [ './usage' ];
    
    var _console = console;
    console = Hash.copy(console);
    var errors = [];
    var logs = [];
    console.error = function (msg) { errors.push(msg) };
    console.log = function (msg) { logs.push(msg) };
    
    var result = f();
    
    process = _process;
    console = _console;
    
    return {
        errors : errors,
        logs : logs,
        exit : exit,
        result : result,
    };
};
Ejemplo n.º 2
0
 self.__proto__.limit = function (user, access) {
     var can = (access ? access : self.access).allowed(user);
     
     var share = Hash.copy(this);
     if (!can.copy) delete share.copy;
     if (!can.spawn) delete share.spawn;
     
     share.processes = Hash(share.processes)
         .map(function (proc) {
             var pcan = proc.access.allowed(user);
             if (pcan) return proc.limit(user);
         })
         .filter(function (x) { return x !== undefined })
         .items
     ;
     
     return share;
 };
Ejemplo n.º 3
0
function Disk (params) {
    if (!(this instanceof Disk)) return new Disk(params);
    var self = this;
    
    // DNode clients can't see or call stuff in __proto__.
    self.__proto__ = Hash.copy(self.__proto__);
    
    self.name = params.name;
    self.processes = {};
    var filename = params.filename;
    self.filename = path.basename(filename); // client sees just the basename
    
    function addProcess (proc) {
        self.processes[proc.address] = proc;
        proc.on('exit', function () {
            delete self.processes[proc.address];
        });
    }
                
    Hash(managers).forEach(function (manager) {
        Hash(manager.processes).forEach(function (proc) {
            if (proc.filename == self.filename) {
                addProcess(proc);
            }
        });
    });
    
    self.__proto__.access = Access(params.rules || {});
    
    self.__proto__.limit = function (user, access) {
        var can = (access ? access : self.access).allowed(user);
        
        var share = Hash.copy(this);
        if (!can.copy) delete share.copy;
        if (!can.spawn) delete share.spawn;
        
        share.processes = Hash(share.processes)
            .map(function (proc) {
                var pcan = proc.access.allowed(user);
                if (pcan) return proc.limit(user);
            })
            .filter(function (x) { return x !== undefined })
            .items
        ;
        
        return share;
    };
    
    self.on('attach', function (tied) {
        tied.tie('processes');
        
        // spawn a new vm process
        tied.spawn = function (engine, cb) {
            managers[engine].spawn(filename, function (proc) {
                proc.name = self.name;
                
                addProcess(proc);
                var tProc = tied.tie(proc);
                
                if (cb) cb(tProc);
                self.emit('spawn', tProc);
            });
        };
        
        tied.copy = function (dstFile) {
            console.log('copy not implemented... yet');
        };
    });
}
Ejemplo n.º 4
0
RemoteEmitter.prototype.attach = function (conn) {
    var self = this;
    if (!self._events) self._events = {};
    var copy = Hash.copy(self);
    if (!self.connections) self.connections = 0;
    if (self.connections == 1) self.emit('_online');
    
    conn.on('end', function () {
        self.connections --;
        if (self.connections == 0) self.emit('_offline');
    });
    
    // Make a copy so we can add prototypes on the fly >:D
    // DNode clients won't see the methods we add.
    copy.__proto__ = Hash.copy(copy.__proto__);
    copy.__proto__.connection = conn;
    copy.__proto__.tie = function (rem) {
        if (typeof rem == 'string') {
            copy[rem] = RemoteEmitter.attach(conn, copy[rem]);
            return copy[rem];
        }
        else {
            return rem.attach(conn);
        }
    };
    copy.__proto__.tieArray = function (rems) {
        return rems.map(function (rem) { return rem.attach(conn) });
    };
    copy.__proto__.tieHash = function (rems) {
        return Hash.map(rems, function (rem) { return rem.attach(conn) });
    };
    
    copy.subscribe = function (cb) {
        var events = {};
        
        var ev = {
            on : function (name, f) {
                if (!(name in events)) events[name] = [];
                events[name].push(f);
                // note: might need to check if the connection is still alive inside
                // the callback
                self.on(name, f);
            },
            off : function (name, f) {
                if (f === undefined) {
                    // remove all listeners with the given name
                    if (events[name]) {
                        events[name].forEach(function (f) {
                            self.removeListener(name, f);
                        });
                        events[name] = [];
                    }
                }
                else {
                    var i = events[name].indexOf(f);
                    if (i >= 0) events[name].splice(i,1);
                    self.removeListener(name, f);
                }
            }
        };
        
        conn.on('end', function () {
            Object.keys(events).forEach(function (name) {
                events[name].forEach(function (f) {
                    ev.off(name, f);
                });
            });
        });
        
        cb(ev);
    };
    
    // emit before so server code can add stuff before it gets to the client
    self.emit('attach', copy);
    
    return copy;
};