fix server-to-server for @d3x0r

This commit is contained in:
Mark Nadal 2016-07-07 09:56:31 -07:00
parent 6986de3aa1
commit ab60299fe0
5 changed files with 71 additions and 22 deletions

22
gun.js
View File

@ -1173,7 +1173,8 @@
opt = opt || {};
var tab = gun.tab = gun.tab || {};
tab.store = tab.store || Tab.store;
tab.request = tab.request || request;
tab.request = tab.request || Gun.request;
if(!tab.request){ throw new Error("Default GUN driver could not find default network abstraction.") }
tab.request.s = tab.request.s || {};
tab.headers = opt.headers || {};
tab.headers['gun-sid'] = tab.headers['gun-sid'] || Gun.text.random(); // stream id
@ -1299,12 +1300,16 @@
gun.__.opt.wire.put = gun.__.opt.wire.put || tab.put;
gun.__.opt.wire.key = gun.__.opt.wire.key || tab.key;
Tab.request = request;
Tab.request = tab.request;
Gun.Tab = Tab;
});
}.bind(this || module)({}));
;(function(Tab){
var request = (function(){
function r(base, body, cb, opt){
function r(base, body, cb, opt){ opt = opt || {};
var o = base.length? {base: base} : {};
o.base = opt.base || base;
o.body = opt.body || body;
@ -1312,6 +1317,7 @@
o.url = opt.url;
cb = cb || function(){};
if(!o.base){ return }
if(opt.WebSocket){ o.WebSocket = opt.WebSocket }
r.transport(o, cb);
}
r.createServer = function(fn){ r.createServer.s.push(fn) }
@ -1327,7 +1333,7 @@
r.jsonp(opt, cb);
}
r.ws = function(opt, cb){
var ws, WS = window.WebSocket || window.mozWebSocket || window.webkitWebSocket;
var ws, WS = opt.WebSocket || window.WebSocket || window.mozWebSocket || window.webkitWebSocket;
if(!WS){ return }
if(ws = r.ws.peers[opt.base]){
if(!ws.readyState){ return setTimeout(function(){ r.ws(opt, cb) },10), true }
@ -1347,7 +1353,7 @@
try{ws = r.ws.peers[opt.base] = new WS(opt.base.replace('http','ws'));
}catch(e){}
ws.onopen = function(o){ r.back = 2; r.ws(opt, cb) };
ws.onclose = window.onbeforeunload = function(c){
ws.onclose = function(c){
if(!c){ return }
if(ws && ws.close instanceof Function){ ws.close() }
if(1006 === c.code){ // websockets cannot be used
@ -1360,6 +1366,7 @@
r.ws(opt, function(){}); // opt here is a race condition, is it not? Does this matter?
}, r.back *= r.backoff);
};
if(typeof window !== "undefined"){ window.onbeforeunload = ws.onclose; }
ws.onmessage = function(m){
if(!m || !m.data){ return }
var res;
@ -1377,6 +1384,9 @@
r.ws.peers = {};
r.ws.cbs = {};
r.jsonp = function(opt, cb){
if(typeof window === "undefined"){
return cb("JSONP is currently browser only.");
}
//Gun.log("jsonp send", opt);
r.jsonp.ify(opt, function(url){
//Gun.log(url);
@ -1459,4 +1469,6 @@
}
return r;
}());
if(typeof window !== "undefined"){ Gun.request = request }
if(typeof module !== "undefined" && module.exports){ module.exports.request = request }
}.bind(this || module)({}));

View File

@ -2,7 +2,7 @@
console.log("Hello wonderful person! :) I'm mark@gunDB.io, message me for help or with hatemail. I want to hear from you! <3");
var Gun = require('../gun');
require('./s3');
require('./wsp');
require('./file');
require('./wsp');
module.exports = Gun;
}());

View File

@ -219,22 +219,39 @@
wsp(opt.server);
}
setTimeout(function(){ // hack it in :( for now, since 0.4.x will allow us to have multiple drivers.
var proxy = gun.__.opt.wire;
var driver = {
put: function(graph, cb, opt){
proxy.put(graph, cb, opt);
var msg = {
headers: {'Content-Type': 'application/json', id: gun.wsp.msg()},
body: graph
};
gun.wsp.on('network').emit(msg);
},
get: proxy.get
if(gun.wsp.driver){ return }
var driver = gun.wsp.driver = {};
var noop = function(){};
var get = gun.__.opt.wire.get || noop;
var put = gun.__.opt.wire.put || noop;
var driver = {
put: function(graph, cb, opt){
put(graph, cb, opt);
var msg = {
headers: {'Content-Type': 'application/json', id: gun.wsp.msg()},
body: graph
};
gun.wsp.on('network').emit(msg);
},
get: function(lex, cb, opt){
get(lex, cb, opt);
if(!Gun.request){ return console.log("Server could not find default network abstraction.") }
opt = opt || {};
var ropt = {headers:{}, WebSocket: WebSocket};
ropt.headers.id = gun.wsp.msg();
Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){
Gun.request(url, lex, function(err, reply){
reply.body = reply.body || reply.chunk || reply.end || reply.write;
if(err || !reply || (err = reply.body && reply.body.err)){
return cb({err: Gun.log(err || "Get failed.") });
}
cb(null, reply.body);
}, ropt);
});
}
gun.__.opt.wire = driver;
gun.opt({wire: driver}, true);
},1);
}
var WebSocket = require('ws');
gun.__.opt.wire = driver;
gun.opt({wire: driver}, true);
});
}({}));

14
test/server/http.js Normal file
View File

@ -0,0 +1,14 @@
var http = require('http');
var Gun = require('../../index');
var gun = Gun({
file: 'data.json'
});
gun.get('data').put({a: {data: 1}, b: {data: 2}});
var server = http.createServer(function(req, res){});
gun.wsp(server);
server.listen(8081);
console.log('Server started on port ' + 8081 + ' with /gun');

View File

@ -0,0 +1,6 @@
var Gun = require('../../index');
var location = {host:"localhost"};
var gun = Gun( 'ws://' + location.host + ':8081/gun');
var gdb = gun.get( 'data' );
gdb.map(function(val,field){ console.log( field, "=", val ); } );
console.log( "done... wait forever?" );