Skip to content

Commit

Permalink
Merge pull request #12 from formula1/master
Browse files Browse the repository at this point in the history
tunnel and api
  • Loading branch information
zodern committed Dec 7, 2014
2 parents b472568 + d8b7e81 commit 6787289
Show file tree
Hide file tree
Showing 29 changed files with 1,674 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
npm-debug.log
roadmap
*.orig
*.*~
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "apps/tunnelclient"]
path = apps/tunnelclient
url = https://github.com/formula1/Silk-Tunnel-Client
2 changes: 1 addition & 1 deletion apps/app-defaults/settings/app-defaults.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{"text/plain":{"default":"","available":[]},"*":{"default":"","available":[]},"undefined":{"default":"Text Editor","available":[]},"text/html":{"default":"Text Editor","available":[]}}
1 change: 1 addition & 0 deletions apps/tunnelclient
Submodule tunnelclient added at b8f76f
7 changes: 6 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"jschannel": "*",
"vue": "~0.11.0",
"jquery": "~2.1.1",
"fontawesome": "*"
"fontawesome": "*",
"rsvp": "*",
"eventEmitter": "*"
},
"resolutions": {
"jquery": ">=1.9.1"
}
}
31 changes: 28 additions & 3 deletions core/bower_static.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
var fs = require("fs");
var url = require("url");
var p = require("path");
module.exports = function(req,res,next){
var path = url.parse(req.originalUrl).pathname;
if(!/^\/?bc\/.*/.test(path)) return next();
if(/.*\/\.\.?\/.*/.test(path)) return next();
if(/.*\/\/.*/.test(path)) return next();
path = __root+"/bower_components/"+path.split("/").slice(2).join("/");
raw(path,function(file){
if(file) return res.sendFile(file)
next();
})
};

function raw(path,next){
path = path.split("/").slice(2);
var wp = path;
path = __root+"/bower_components/"+path[0]+"/";
fs.readFile(path+"/bower.json",function(err,file){
if(err) return next();
file = JSON.parse(file);
if(!("main" in file)) return next();
res.sendFile(path+"/"+file.main);
if(!Array.isArray(file.main))
return next(p.resolve(path,file.main));

if(file.main.length == 1 && wp.length == 1)
return next(p.resolve(path,file.main[0]))

wp.shift();
var r = p.resolve(path,wp.join("/"));
while(file.main.length > 0){
var f = p.resolve(path,file.main.pop());
if(f == r)
return next(r);
}
return next();
})
};
}

module.exports.raw = raw;
37 changes: 37 additions & 0 deletions core/client_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var __api = __dirname+"/client_api";
var bowerstatic = require(__dirname+"/bower_static.js");
var async = require("async");
var fs = require("fs");

module.exports = function(req,res,next){
res.setHeader('content-type', 'application/javascript');
bowerstatic.raw("/bc/eventEmitter",function(ee){
console.log(ee);
bowerstatic.raw("/bc/rsvp",function(rsvp){
console.log(rsvp);
async.eachSeries([
ee,
rsvp,
__api+"/abstract/StreamPromise.js",
__api+"/abstract/MessageRouter.js",
__api+"/abstract/MessageWriter.js",
__api+"/abstract/MessageDuplex.js",
__api+"/window/Window2Server_com.js",
__api+"/window/WindowAbstract.js",
__api+"/window/FrameContext.js",
__api+"/window/WindowManager.js",
__api+"/window/Window2Server_com.js",
__api+"/network/NetworkHost.js",
__api+"/network/NetworkUser.js",
],function(file, next){
var temp = fs.createReadStream(file, {encoding:"utf-8"});
temp.on('data',res.write.bind(res));
temp.on('end',next);
temp.on('error',next);
},function(err){
if(err) return next(err);
res.end();
});
});
});
}
120 changes: 120 additions & 0 deletions core/client_api/abstract/DependentArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
function DependentArray(){
this.q = {};
this.d = {};
this.u = {};
}

DependentArray.prototype.dormant = function (value,deps,name){
if(typeof deps == "undefined")
deps = [];
if(typeof deps == "string")
deps = [deps];
if(typeof name == "undefined"){
if(typeof value == "string"){
name = value.split("/").pop();
name = name.substring(0, name.indexOf("."));
}else
throw new Error("When value isn't a string, name is required")
}
var type = "d";
var chil = [];
if(name in this.u){
for(var i=0;i<this.u[name].length;i++)
if(this.u[name][i] in this.q)
type="q";
chil = this.u[name];
delete this.u[name];
}
this[type][name] = {
deps:deps,
value:value,
chil:chil
};
this.handleDeps(name, deps, (type=="d"));
return this;
}

DependentArray.prototype.queue = function (value,deps,name){
if(typeof deps == "undefined")
deps = [];
if(typeof deps == "string")
deps = [deps];
if(typeof name == "undefined"){
if(typeof value == "string"){
name = value.split("/").pop();
name = name.substring(0, name.indexOf("."));
}else
throw new Error("When value isn't a string, name is required")
}
var chil = [];
if(name in this.u){
chil = this.u[name];
delete this.u[name];
}
this.q[name] = {
deps:deps,
value:value,
chil:chil
}
this.handleDeps(name, deps)
return this;
}

DependentArray.prototype.handleDeps = function(name,deps,isdormant){
for(var i=0;i<deps.length;i++){
if(deps[i] in this.d){
if(isdormant){
if(this.d[deps[i]].deps.indexOf(name) != -1)
throw new Error("Circular Depndencied detected: ["+deps[i]+", "+name+"]");
this.d[deps[i]].chil.push(name);
continue;
}else
this.q[deps[i]] = this.d[deps[i]];
delete this.d[deps[i]];
}
if(deps[i] in this.q){
if(this.q[deps[i]].deps.indexOf(name) != -1)
throw new Error("Circular Depndencied detected: ["+deps[i]+", "+name+"]");
this.q[deps[i]].chil.push(name);
}else if(deps[i] in this.u)
this.u[deps[i]].push(name);
else
this.u[deps[i]] = [name];
}
}

DependentArray.prototype.resolve = function (){
this.ret = [];

if(Object.keys(this.u).length > 0)
console.log("We have some unknowns");
if(Object.keys(this.d).length > 0)
console.log("We have some dormants");

for(var i in this.q){
this.resolveRecurs(i);
}
if(Object.keys(this.q).length > 0)
throw new Error("Unresolved queue items: "+JSON.stringify(Object.keys(this.q)));

console.log("done");
return this.ret;
}

DependentArray.prototype.resolveRecurs = function(i){
if(!(i in this.q))
throw new Error("Unresolved dependency: "+i)
while(this.q[i].deps.length > 0){
this.resolveRecurs(this.q[i].deps[0]);
}
this.ret.push(this.q[i].value);
var chil = this.q[i].chil;
for(var j=0;j<chil.length;j++)
this.q[chil[j]].deps.splice(this.q[chil[j]].deps.indexOf(i), 1);
delete this.q[i];
}


if(typeof module != "undefined" && module.exports){
module.exports = DependentArray;
}
67 changes: 67 additions & 0 deletions core/client_api/abstract/MessageDuplex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var doAsync;
if(typeof module != "undefined" && module.exports){
var MessageRouter = require(___dirname+"/MessageRouter.js");
var MessageWriter = require(___dirname+"/MessageWriter.js");
doAsync = process.nextTick.bind(process);
}else{
doAsync = function(fn){
setTimeout(fn,1);
}
}


/**
An io listener that can also write messages.
@constructor
@interface
@augments MessageRouter
@augments MessageWriter
@param {function} writwFn - Function that will be called when a MessageDuplex is ready to write a message
@param {function} [readFn=writeFn] - Function that will be called when a MessageDuplex is reading a message
*/

function MessageDuplex(wSendFn, rSendFn){
console.log("mesdup")
if(!rSendFn) rSendFn = wSendFn;
if(typeof wSendFn == "undefined") throw new Error("Need at least 1 function");
var _writeFn = wSendFn;
this.originator = Date.now()+"|"+Math.random();
var that = this;
wSendFn = function(message){
if(message.originator){
if(!Array.isArray(message.originator)){
throw new Error("something went wrong in the originator chain");
}
message.originator.push(that.originator);
}else{
message.originator = [that.originator]
}
_writeFn(message);
};
console.log("b4rout")
MessageRouter.call(this, rSendFn);
console.log("b4writ")
MessageWriter.call(this, wSendFn);
};
MessageDuplex.prototype = Object.create(MessageWriter.prototype);
MessageDuplex.prototype.rSendFn = MessageRouter.prototype.rSendFn;
MessageDuplex.prototype.add = MessageRouter.prototype.add;
MessageDuplex.prototype.routeMessage = MessageRouter.prototype.routeMessage;
MessageDuplex.prototype.processMessage = MessageRouter.prototype.processMessage;

/**
The method to call after you have processed the message the io has recieved.
@memberof MessageDuplex
@param {object} message - An object containing important message information
@param {object} user - the user you want to recieve in the {@link MessageRouter#rSendFn}
*/
MessageDuplex.prototype.handleMessage = function(message,user){
if(message.originator && message.originator.indexOf(this.originator) != -1){
console.log("return");
this.returnMessage(message);
}else{
console.log("route");
this.routeMessage(message,user)
}
};
MessageDuplex.prototype.constructor = MessageDuplex;
31 changes: 31 additions & 0 deletions core/client_api/abstract/MessageObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function MessageObject(message,fn,next){
this.id = message.id;
this.user = message.user;
this.name = message.name;
this.data = message.data;
this.next = function(err,result){
var message = {
id: this.id,
user: this.user.id,
error: (err)?err.stack:null,
data: (err)?null:result
};
next(message);
}.bind(this)
this.exec(fn);
}

MessageObject.prototype.exec = function(fn){
try{
var result = fn(this.data,this,this.next);
}catch(e){
return this.next(e);
}
if(typeof result != "undefined")
this.next(void(0),result);
}


if(typeof module != "undefined" && module.exports){
module.exports = MessageObject;
}
Loading

0 comments on commit 6787289

Please sign in to comment.