From 44fa4f325264235c0e215db54f7aac5476b2c201 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 19 Dec 2016 20:30:42 -0800 Subject: [PATCH 01/19] Update gun.js --- gun.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gun.js b/gun.js index cbb3ba57..64408ebf 100644 --- a/gun.js +++ b/gun.js @@ -2352,6 +2352,8 @@ } Client.isSupported = Client.WebSocket !== null; + + if(!Client.isSupported){ return } // TODO: For now, don't do anything in browsers/servers that don't work. Later, use JSONP fallback and merge with server code? // Ensure the protocol is correct. Client.formatURL = function (url) { From 2e7b87c3b9d931b592b0792927b4bccba7d9bbf1 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 19 Dec 2016 20:57:07 -0800 Subject: [PATCH 02/19] Update gun.js --- gun.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gun.js b/gun.js index 64408ebf..fb58dc59 100644 --- a/gun.js +++ b/gun.js @@ -2351,7 +2351,7 @@ null; } - Client.isSupported = Client.WebSocket !== null; + Client.isSupported = !!Client.WebSocket; if(!Client.isSupported){ return } // TODO: For now, don't do anything in browsers/servers that don't work. Later, use JSONP fallback and merge with server code? From e8f840c5487aa695540006c04003c6b39b5a05c8 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 11:21:13 -0800 Subject: [PATCH 03/19] pass gun's options to websocket I recently had to set options given to the websocket client especially rejectUnauthorized {Boolean} Verify or not the server certificate. for connecting to wss which are using self signed certs... could also pass things like 'protocol' to differentiate a Gun websocket connection from some other websocket connection.... I don't know if maybe websock options should be a object in options instead ? To avoid namespace collision? --- lib/wsp/Peer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 493ba8e7..13a472b1 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -134,7 +134,7 @@ API.connect = function () { var url = this.url; // Open a new websocket. - var socket = new WebSocket(url); + var socket = new WebSocket(url, this.options); // Re-use the previous listeners. socket._events = this._events; From f548db293b633b35320007275e0d8e6471ca52d4 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 11:59:28 -0800 Subject: [PATCH 04/19] pull opions from 'wsc' in gun options 'ws' options are used by server 'wsc' options to be used by websock client... ? --- lib/wsp/Peer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 13a472b1..34697a72 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -134,7 +134,7 @@ API.connect = function () { var url = this.url; // Open a new websocket. - var socket = new WebSocket(url, this.options); + var socket = new WebSocket(url, this.options.wsc); // Re-use the previous listeners. socket._events = this._events; From f28b62fd4ce45d1062ef5d0bed97ff970a516851 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 12:09:10 -0800 Subject: [PATCH 05/19] Setup default option if wsc doesn't previosuly exist, create a empty option block. add option 'protocols' for wsc that can specify the optional protocols before the options. Options will also only apply to node client websockets; (package 'ws'), browser websockets only take 1 with 1 optional parameter (being the list of protocols to connect with; so I added that optional param for compatibility. --- lib/wsp/Peer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 34697a72..e8a0b3ae 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,6 +83,7 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; + this.options.wsc = options.wsc || { protocols: null }; // Messages sent before the socket is ready. this.deferredMsgs = []; @@ -134,7 +135,7 @@ API.connect = function () { var url = this.url; // Open a new websocket. - var socket = new WebSocket(url, this.options.wsc); + var socket = new WebSocket(url, this.options.wsc.protocols, this.options.wsc); // Re-use the previous listeners. socket._events = this._events; From 320183fa54df3283ffc43a89d5efa5b4bc37323a Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 12:17:01 -0800 Subject: [PATCH 06/19] Set default options more robustly --- lib/wsp/Peer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index e8a0b3ae..42527e04 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,7 +83,8 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; - this.options.wsc = options.wsc || { protocols: null }; + if( !('wsc" in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; + else if( !("protocols" in this.options.wsc) ) this.options.wsc.protocols = null; // Messages sent before the socket is ready. this.deferredMsgs = []; From 9c411820043db2c82bc570903b3cc3969a48f153 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 12:17:31 -0800 Subject: [PATCH 07/19] Fix quotes --- lib/wsp/Peer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 42527e04..223e21aa 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,8 +83,8 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; - if( !('wsc" in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; - else if( !("protocols" in this.options.wsc) ) this.options.wsc.protocols = null; + if( !('wsc' in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; + else if( !('protocols' in this.options.wsc) ) this.options.wsc.protocols = null; // Messages sent before the socket is ready. this.deferredMsgs = []; From 8e6d827216de9d28f9904e573bf3d9b06736cc88 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 12:17:56 -0800 Subject: [PATCH 08/19] fix indentation --- lib/wsp/Peer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 223e21aa..743c74c1 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,7 +83,7 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; - if( !('wsc' in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; + if( !('wsc' in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; else if( !('protocols' in this.options.wsc) ) this.options.wsc.protocols = null; // Messages sent before the socket is ready. From 8dac8209023529bb0b723bce89fe9cc879e7d70e Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 12:18:39 -0800 Subject: [PATCH 09/19] fix default assignment --- lib/wsp/Peer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index 743c74c1..e64bd63c 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,7 +83,7 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; - if( !('wsc' in this.options ) ) this.options.wsc = options.wsc || { protocols: null }; + if( !('wsc' in this.options ) ) this.options.wsc = { protocols: null }; else if( !('protocols' in this.options.wsc) ) this.options.wsc.protocols = null; // Messages sent before the socket is ready. From 751ac3b2309c1cc628285f57fec5df7749f7e32b Mon Sep 17 00:00:00 2001 From: d3x0r Date: Wed, 21 Dec 2016 15:03:10 -0800 Subject: [PATCH 10/19] pass wsc options to client gun create ( wsc:protocols especially, since that translates to the optional parameter in the standard client websocket constructor ) --- gun.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gun.js b/gun.js index fb58dc59..effa3f6a 100644 --- a/gun.js +++ b/gun.js @@ -2198,9 +2198,9 @@ ); } - function Client (url, options) { + function Client (url, options, wscOptions ) { if (!(this instanceof Client)) { - return new Client(url, options); + return new Client(url, options, wscOptions); } this.url = Client.formatURL(url); @@ -2211,6 +2211,7 @@ this.on = Gun.on; this.options = options || {}; + this.options.wsc = wscOptions; this.resetBackoff(); } @@ -2234,7 +2235,7 @@ connect: function () { var client = this; - var socket = new Client.WebSocket(this.url); + var socket = new Client.WebSocket(this.url, this.options.wsc.protocols, this.options.wsc ); this.socket = socket; // Forward messages into the emitter. @@ -2416,7 +2417,7 @@ return; } - var client = new Client(url, options.backoff); + var client = new Client(url, options.backoff, gun.Back('opt.wsc') || {protocols:null}); // Add it to the pool. Client.pool[url] = client; From 266b18109c423e073a0aa9717a596655729c7c79 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Fri, 23 Dec 2016 00:35:11 -0800 Subject: [PATCH 11/19] Missed a change to get the options.... Somehow my other options test didn't work... had to add something like this. Now I feel this is such a huge hack --- lib/wsp/client.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/wsp/client.js b/lib/wsp/client.js index 80b408f6..62b6e8d3 100644 --- a/lib/wsp/client.js +++ b/lib/wsp/client.js @@ -74,6 +74,7 @@ Gun.on('opt', function (context) { if (sockets[url]) { return; } + if (!options.wsc) options.wsc = gun.Back('opt.wsc') || { protocols:null }; var socket = Socket(url, options); sockets.add(url, socket); From a4425bda24c7c927cd03fd1b113efb85d77d0bea Mon Sep 17 00:00:00 2001 From: d3x0r Date: Fri, 23 Dec 2016 00:43:05 -0800 Subject: [PATCH 12/19] remove redundant option fetch since options have to be fetched higher anyway... Or rather - this options is outside of knowing gun options --- lib/wsp/Peer.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/wsp/Peer.js b/lib/wsp/Peer.js index e64bd63c..11f1069c 100644 --- a/lib/wsp/Peer.js +++ b/lib/wsp/Peer.js @@ -83,8 +83,6 @@ function Peer (url, options) { this.setMaxListeners(Infinity); this.options = options || {}; - if( !('wsc' in this.options ) ) this.options.wsc = { protocols: null }; - else if( !('protocols' in this.options.wsc) ) this.options.wsc.protocols = null; // Messages sent before the socket is ready. this.deferredMsgs = []; From db74549326936010bafb7a13e1392256f41aa54e Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 26 Dec 2016 21:12:21 -0800 Subject: [PATCH 13/19] warning: file.js/data.json is for local development testing only. --- lib/file.js | 120 ++++++++++++++++++++++++++++------------------------ 1 file changed, 64 insertions(+), 56 deletions(-) diff --git a/lib/file.js b/lib/file.js index 26b0bea3..5e63c47e 100644 --- a/lib/file.js +++ b/lib/file.js @@ -6,73 +6,81 @@ var Gun = require('../gun'), fs = require('fs'), file = {}; -function isUsingFileJS (context) { - - // Options passed via .get or .put. - var methodOptions = context.opt || {}; - - // Options set on the gun chain. - var chainOption = context.gun.Back('opt.file'); - - // Favor method options over chain options. - var file = methodOptions.hasOwnProperty('file') - ? methodOptions.file - : chainOption; - - // Return whether the module is disabled. - return file !== false; -} - -// queue writes, adapted from https://github.com/toolness/jsondown/blob/master/jsondown.js -var isWriting = false, queuedWrites = []; -function writeFile(path, disk, at){ - if(isWriting) return queuedWrites.push(at); - isWriting = true; - var contents = JSON.stringify(disk, null, 2); - fs.writeFile(String(path), contents, function(err) { - var batch = queuedWrites.splice(0); - isWriting = false; - at.gun.Back(-1).on('in', {'@': at['#'], err: err, ok: err? false : 1}); - if(!batch.length){ return } - batch.forEach(function(at){ - at.gun.Back(-1).on('in', {'@': at['#'], err: err, ok: err? false : 1}); +Gun.on('put', function(at){ + if(!file.use){ return } + var graph = at.put, Graph = file.gun._.graph, opt = at.opt || {}; + Gun.obj.map(graph, function(node, soul){ + file.disk.graph[soul] = Graph[soul] || graph[soul]; + }); + graph = JSON.stringify(file.disk, null, 2); + fs.writeFile(opt.file || file.file, graph, function(err){ + file.gun.on('in', { + '@': at['#'], + ok: err? undefined : 1, + err: err }); }); +}); + +Gun.on('get', function(at){ + if(!file.use){ return } + var soul = at.get['#']; + if(!soul){ return } + var node = file.disk.graph[soul]; + if(Gun.obj.has(at.get, '.')){ + node = field(node, at.get['.']); + } + file.gun.on('in', { + put: Gun.graph.node(node), + '@': at['#'] + }) +}); + +function field(node, field){ + if(!node){ return } + var tmp = node[field]; + node = {_: node._}; + if(u !== tmp){ + node[field] = tmp; + } + tmp = node._; + if(tmp['>']){ + tmp['>'] = Gun.obj.put({}, field, tmp['>'][field]); + } + return node; } -Gun.on('put', function(at){ - if (isUsingFileJS(at) === false) { - return; - } - var gun = at.gun, graph = at.put, opt = at.opt || {}; - var __ = gun._.root._; - Gun.obj.map(graph, function(node, soul){ - file.disk.graph[soul] = __.graph[soul] || graph[soul]; - }); - writeFile(opt.file || file.file, file.disk, at); -}); -Gun.on('get', function(at){ - if (isUsingFileJS(at) === false) { - return; - } - var gun = at.gun, lex = at.get; - if(!lex){return} - gun.Back(-1).on('in', {'@': at['#'], put: Gun.graph.node(file.disk.graph[lex['#']])}); - //at.cb(null, file.disk.graph[lex['#']]); -}); - Gun.on('opt', function(at){ - var gun = at.gun, opts = at.opt; - if ((opts.file === false) || (opts.s3 && opts.s3.key)) { + var gun = at.gun, opt = at.opt; + if ((opt.file === false) || (opt.s3 && opt.s3.key)) { return; // don't use this plugin if S3 is being used. } Gun.log.once( 'file-warning', 'WARNING! This `file.js` module for gun is ' + - 'intended only for local development testing!' + 'intended for local development testing only!' ); - file.file = opts.file || file.file || 'data.json'; - file.raw = file.raw || (fs.existsSync || require('path').existsSync)(opts.file) ? fs.readFileSync(opts.file).toString() : null; + file.use = true; + file.file = String(opt.file || file.file || 'data.json'); + file.raw = file.raw || (fs.existsSync || require('path').existsSync)(opt.file) ? fs.readFileSync(opt.file).toString() : null; file.disk = file.disk || Gun.obj.ify(file.raw || {graph: {}}); file.disk.graph = file.disk.graph || {}; + file.gun = gun; }); + +function test(){ + var gun = Gun(), i = 2000, expect = 0; + while(--i){ + expect += i; + gun.get('test/' + i).put({ index: i }); + } + setTimeout(function(){ + var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph; + var count = 0; + Gun.obj.map(graph, function(node){ + count += node.index; + }) + console.log(expect === count? "SUCCESS!" : "FAIL!"); + },100); +} +//test(); From 85ae140512932829b735b3093a2c7bc4a9fcbda7 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Mon, 26 Dec 2016 21:49:42 -0800 Subject: [PATCH 14/19] HTTP.js example with external in/out used Connects server websocket by event to gun.... (would have complicated with alternative websocket interfaces and protocol handlings... but left some comments about that) --- examples/http-external-ws.js | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/http-external-ws.js diff --git a/examples/http-external-ws.js b/examples/http-external-ws.js new file mode 100644 index 00000000..22d1d548 --- /dev/null +++ b/examples/http-external-ws.js @@ -0,0 +1,60 @@ +var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8080; + +var Gun = require('../'); +var gun = Gun({ + file: 'data.json', + s3: { + key: '', // AWS Access Key + secret: '', // AWS Secret Token + bucket: '' // The bucket you want to save into + } +}); + +var server = require('http').createServer(function(req, res){ + if(gun.wsp.server(req, res)){ + return; // filters gun requests! + } + require('fs').createReadStream(require('path').join(__dirname, req.url)).on('error',function(){ // static files! + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(require('fs').readFileSync(require('path').join(__dirname, 'index.html'))); // or default to index + }).pipe(res); // stream +}); + +// do not do this to attach server... instead pull websocket provider and use that. +// gun.wsp(server); + +var ws = require( 'ws' ); // default websocket provider gun used... +var WebSocketServer = ws.server; + +var wss = new WebSocketServer( { + server: server, // 'ws' npm + autoAcceptConnections : false // want to handle the request (websocket npm?) + } + +wss.on('connection',acceptConnection ) + +var gunPeers = []; // used as a list of connected clients. + +function acceptConnection( connection ) { + // connection.upgradeReq.headers['sec-websocket-protocol'] === (if present) protocol requested by client + // connection.upgradeReq.url === url request + console.log( "connect?", req.upgradeReq.headers, req.upgradeReq.url ) + gunPeers.push( connection ); + gun.on('out', (msg)=>{ + msg = JSON.stringify({headers:{},body:msg}); + gunPeers.forEach( (peer)=>{ peer.send( msg ) }) + }) + connection.on( 'error',(error)=>{console.log( "WebSocket Error:", error } ); + connection.on( 'message',(msg)=>{gun.on('in',JSON.parse( msg.utf8Data).body)}) + connection.on( 'close', (reason,desc)=>{ + // gunpeers gone. + var i = peers.find( p=>p===connection ); + if( i >= 0 ) + gunPeers.splice( i, 1 ); + + }) +} + +server.listen(port); + +console.log('Server started on port ' + port + ' with '); From 4a5ab4122bb0dfa3593f90e6f7f3fd17fb624947 Mon Sep 17 00:00:00 2001 From: d3x0r Date: Mon, 26 Dec 2016 22:33:46 -0800 Subject: [PATCH 15/19] Update http-external-ws.js use classic functions instead of es6 fix peers.find should be peers.findIndex for splice. --- examples/http-external-ws.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/http-external-ws.js b/examples/http-external-ws.js index 22d1d548..121e3358 100644 --- a/examples/http-external-ws.js +++ b/examples/http-external-ws.js @@ -42,13 +42,13 @@ function acceptConnection( connection ) { gunPeers.push( connection ); gun.on('out', (msg)=>{ msg = JSON.stringify({headers:{},body:msg}); - gunPeers.forEach( (peer)=>{ peer.send( msg ) }) + gunPeers.forEach( function(peer){ peer.send( msg ) }) }) - connection.on( 'error',(error)=>{console.log( "WebSocket Error:", error } ); - connection.on( 'message',(msg)=>{gun.on('in',JSON.parse( msg.utf8Data).body)}) - connection.on( 'close', (reason,desc)=>{ + connection.on( 'error',function(error){console.log( "WebSocket Error:", error } ); + connection.on( 'message',function(msg){gun.on('in',JSON.parse( msg.utf8Data).body)}) + connection.on( 'close', function(reason,desc){ // gunpeers gone. - var i = peers.find( p=>p===connection ); + var i = peers.findIndex( function(p){return p===connection} ); if( i >= 0 ) gunPeers.splice( i, 1 ); From 2e826ca9d8bddcd064f0a87d7cab3e9259e66030 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 26 Dec 2016 22:46:27 -0800 Subject: [PATCH 16/19] fix reads --- lib/file.js | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/file.js b/lib/file.js index 5e63c47e..76b3c648 100644 --- a/lib/file.js +++ b/lib/file.js @@ -13,6 +13,7 @@ Gun.on('put', function(at){ file.disk.graph[soul] = Graph[soul] || graph[soul]; }); graph = JSON.stringify(file.disk, null, 2); + // TODO: Allow for a `fs.writeFile` compatible module, that is more reliable/safe, to be passed in through the options. fs.writeFile(opt.file || file.file, graph, function(err){ file.gun.on('in', { '@': at['#'], @@ -40,7 +41,7 @@ function field(node, field){ if(!node){ return } var tmp = node[field]; node = {_: node._}; - if(u !== tmp){ + if(undefined !== tmp){ node[field] = tmp; } tmp = node._; @@ -62,25 +63,33 @@ Gun.on('opt', function(at){ ); file.use = true; file.file = String(opt.file || file.file || 'data.json'); - file.raw = file.raw || (fs.existsSync || require('path').existsSync)(opt.file) ? fs.readFileSync(opt.file).toString() : null; + file.raw = file.raw || (fs.existsSync || require('path').existsSync)(file.file) ? fs.readFileSync(file.file).toString() : null; file.disk = file.disk || Gun.obj.ify(file.raw || {graph: {}}); file.disk.graph = file.disk.graph || {}; file.gun = gun; }); -function test(){ - var gun = Gun(), i = 2000, expect = 0; - while(--i){ - expect += i; - gun.get('test/' + i).put({ index: i }); - } - setTimeout(function(){ +(function test(){ + try{ var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph; - var count = 0; - Gun.obj.map(graph, function(node){ - count += node.index; - }) - console.log(expect === count? "SUCCESS!" : "FAIL!"); - },100); -} -//test(); + var read; + Gun().get('test/5').path('index').val(function(data){ + read = data; + }); + console.log((5 === read)? "READ SUCCESS" : "FAIL"); + }catch(e){ + var gun = Gun(), i = 100, expect = 0; + while(--i){ + expect += i; + gun.get('test/' + i).put({ index: i, test: true }); + } + setTimeout(function(){ + var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph; + var count = 0; + Gun.obj.map(graph, function(node){ + count += node.index; + }); + console.log((expect && expect === count)? "WRITE SUCCESS! - RUN AGAIN" : "FAIL!"); + },100); + }; +}()); From d5cf9938c1fe868cac60205a7dd1c6193a1f7cbe Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 26 Dec 2016 22:48:59 -0800 Subject: [PATCH 17/19] Update file.js --- lib/file.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/file.js b/lib/file.js index 76b3c648..1aacb12f 100644 --- a/lib/file.js +++ b/lib/file.js @@ -70,6 +70,7 @@ Gun.on('opt', function(at){ }); (function test(){ + return; try{ var graph = Gun.obj.ify(fs.readFileSync('data.json').toString()).graph; var read; From f9a851c73a7cbca2d61f9bc34044b5c6ceacdf4b Mon Sep 17 00:00:00 2001 From: d3x0r Date: Mon, 26 Dec 2016 23:10:58 -0800 Subject: [PATCH 18/19] Apply gun 'out' listener once --- examples/http-external-ws.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/http-external-ws.js b/examples/http-external-ws.js index 121e3358..c95e95d1 100644 --- a/examples/http-external-ws.js +++ b/examples/http-external-ws.js @@ -35,15 +35,15 @@ wss.on('connection',acceptConnection ) var gunPeers = []; // used as a list of connected clients. +gun.on('out', function(msg){ + msg = JSON.stringify({headers:{},body:msg}); + gunPeers.forEach( function(peer){ peer.send( msg ) }) +}) function acceptConnection( connection ) { // connection.upgradeReq.headers['sec-websocket-protocol'] === (if present) protocol requested by client // connection.upgradeReq.url === url request console.log( "connect?", req.upgradeReq.headers, req.upgradeReq.url ) gunPeers.push( connection ); - gun.on('out', (msg)=>{ - msg = JSON.stringify({headers:{},body:msg}); - gunPeers.forEach( function(peer){ peer.send( msg ) }) - }) connection.on( 'error',function(error){console.log( "WebSocket Error:", error } ); connection.on( 'message',function(msg){gun.on('in',JSON.parse( msg.utf8Data).body)}) connection.on( 'close', function(reason,desc){ From 63af6160335898d4d358a76e62f2288d70c46ce7 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 26 Dec 2016 23:13:02 -0800 Subject: [PATCH 19/19] Update http-external-ws.js --- examples/http-external-ws.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/http-external-ws.js b/examples/http-external-ws.js index c95e95d1..2e39d70e 100644 --- a/examples/http-external-ws.js +++ b/examples/http-external-ws.js @@ -35,7 +35,7 @@ wss.on('connection',acceptConnection ) var gunPeers = []; // used as a list of connected clients. -gun.on('out', function(msg){ +Gun.on('out', function(msg){ msg = JSON.stringify({headers:{},body:msg}); gunPeers.forEach( function(peer){ peer.send( msg ) }) })