From e36e78f532f9be55130b50a04d85936b92106b32 Mon Sep 17 00:00:00 2001 From: Jesse Gibson Date: Thu, 10 Nov 2016 10:09:18 -0700 Subject: [PATCH] Isolate websocket logic WebSocket logic has it's own folder now, `wsp`. --- lib/server.js | 2 +- lib/{wsp-client.js => wsp/client.js} | 2 +- lib/{wsp.js => wsp/server.js} | 12 ++++++------ lib/{ => wsp}/ws.js | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) rename lib/{wsp-client.js => wsp/client.js} (99%) rename lib/{wsp.js => wsp/server.js} (96%) rename lib/{ => wsp}/ws.js (92%) diff --git a/lib/server.js b/lib/server.js index af0f28b5..a6082d4c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -3,7 +3,7 @@ var Gun = require('../gun'); console.log("TODO: MARK! UPDATE S3 DRIVER BEFORE PUBLISHING!") //require('./s3'); - require('./wsp'); + require('./wsp/server'); require('./file'); module.exports = Gun; }()); diff --git a/lib/wsp-client.js b/lib/wsp/client.js similarity index 99% rename from lib/wsp-client.js rename to lib/wsp/client.js index 8640d917..d39a53df 100644 --- a/lib/wsp-client.js +++ b/lib/wsp/client.js @@ -8,7 +8,7 @@ */ 'use strict'; -var Gun = require('../gun'); +var Gun = require('../../gun'); var WS = require('ws'); var Tab = {}; diff --git a/lib/wsp.js b/lib/wsp/server.js similarity index 96% rename from lib/wsp.js rename to lib/wsp/server.js index 7ac4bbac..2dfdd27c 100644 --- a/lib/wsp.js +++ b/lib/wsp/server.js @@ -1,14 +1,14 @@ /* eslint-disable*/ -var Gun = require('../gun') +var Gun = require('../../gun') , formidable = require('formidable') -, http = require('./http') +, http = require('../http') , url = require('url') , wsp = {} , WS = require('ws') , ws = WS.Server; // Handles server to server sync. -require('./wsp-client.js'); +require('./client.js'); Gun.on('opt', function(at){ var gun = at.gun, opt = at.opt; @@ -67,7 +67,7 @@ Gun.on('opt', function(at){ 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 + 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; } @@ -78,7 +78,7 @@ Gun.on('opt', function(at){ return http(req, res, function(req, res){ if(!req){ return next() } - var stream, cb = res = require('./jsonp')(req, res); + var stream, cb = res = require('../jsonp')(req, res); if(req.headers && (stream = req.headers['gun-sid'])){ stream = (gun.wsp.peers = gun.wsp.peers || {})[stream] = gun.wsp.peers[stream] || {sid: stream}; stream.drain = stream.drain || function(res){ @@ -172,7 +172,7 @@ Gun.on('opt', function(at){ }}); })}); } - gun.wsp.on('network', function(req){ + gun.wsp.on('network', function(rq){ // TODO: MARK! You should move the networking events to here, not in WSS only. }); tran.json = 'application/json'; diff --git a/lib/ws.js b/lib/wsp/ws.js similarity index 92% rename from lib/ws.js rename to lib/wsp/ws.js index 270ea0b4..45dc100a 100644 --- a/lib/ws.js +++ b/lib/wsp/ws.js @@ -1,4 +1,4 @@ -var Gun = require('../gun') +var Gun = require('../../gun') , url = require('url'); module.exports = function(wss, server, opt){ wss.on('connection', function(ws){ @@ -27,7 +27,7 @@ module.exports = function(wss, server, opt){ (reply.headers = reply.headers || {})['ws-rid'] = msg.headers['ws-rid']; } try{ws.send(Gun.text.ify(reply)); - }catch(e){} // juuuust in case. + }catch(e){} // juuuust in case. }); }); ws.off = function(m){