From 6f8c99ccf1d611239919539737aa97d533cce984 Mon Sep 17 00:00:00 2001 From: Jesse Gibson Date: Tue, 18 Oct 2016 13:54:59 -0600 Subject: [PATCH] Fix /gun.js route The `wsp.server` logic was never making it to the /gun.js route. If the browser is just sending a GET request for a the js file, it won't set the upgrade property, and the server logic wouldn't let the request pass if it didn't have that header. I've simply moved the check below the file serving logic. --- lib/wsp.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/wsp.js b/lib/wsp.js index 09ea2a02..7768fa86 100644 --- a/lib/wsp.js +++ b/lib/wsp.js @@ -34,7 +34,7 @@ if(msg && msg.headers){ delete msg.headers['ws-rid'] } // TODO: BUG? ^ What if other peers want to ack? Do they use the ws-rid or a gun declared id? try{ws.send(Gun.text.ify(msg)); - }catch(e){} // juuuust in case. + }catch(e){} // juuuust in case. }); gun.wsp.wire(req, res); }, {headers: {'ws-rid': 1, 'gun-sid': 1}}); @@ -67,18 +67,18 @@ gun.wsp.server = gun.wsp.server || function(req, res, next){ // http next = next || function(){}; if(!req || !res){ return next(), false } - if(!req.upgrade){ return next(), false } - if(!req.url){ return next(), false } - if(!req.method){ return next(), false } - var msg = {}; - msg.url = url.parse(req.url, true); - if(!gun.wsp.regex.test(msg.url.pathname)){ return next(), false } // TODO: BUG! If the option isn't a regex then this will fail! - if(msg.url.pathname.replace(gun.wsp.regex,'').slice(0,3).toLowerCase() === '.js'){ - res.writeHead(200, {'Content-Type': 'text/javascript'}); - res.end(gun.wsp.js = gun.wsp.js || require('fs').readFileSync(__dirname + '/../gun.js')); // gun server is caching the gun library for the client - return true; - } - return http(req, res, function(req, res){ + if(!req.url){ return next(), false } + if(!req.method){ return next(), false } + var msg = {}; + msg.url = url.parse(req.url, true); + if(!gun.wsp.regex.test(msg.url.pathname)){ return next(), false } // TODO: BUG! If the option isn't a regex then this will fail! + if(msg.url.pathname.replace(gun.wsp.regex,'').slice(0,3).toLowerCase() === '.js'){ + res.writeHead(200, {'Content-Type': 'text/javascript'}); + res.end(gun.wsp.js = gun.wsp.js || require('fs').readFileSync(__dirname + '/../gun.js')); // gun server is caching the gun library for the client + return true; + } + if(!req.upgrade){ return next(), false } + return http(req, res, function(req, res){ if(!req){ return next() } var stream, cb = res = require('./jsonp')(req, res); if(req.headers && (stream = req.headers['gun-sid'])){ @@ -121,7 +121,7 @@ Gun.obj.del(gun.wsp.msg.debounce, id); }); },500); - if(id = gun.wsp.msg.debounce[id]){ + if(id = gun.wsp.msg.debounce[id]){ return gun.wsp.msg.debounce[id] = Gun.time.is(), id; } gun.wsp.msg.debounce[id] = Gun.time.is();