From a177f44027a041c31407e2b5fa5ae6a0512da991 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Sun, 21 Sep 2014 06:15:16 -0700 Subject: [PATCH] made chunk actually do chunk, not body --- gun.js | 15 +++++++-------- shots.js | 17 ++++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/gun.js b/gun.js index 3df61c75..3ce41e7e 100644 --- a/gun.js +++ b/gun.js @@ -129,7 +129,6 @@ Gun.chain.get = function(cb){ var gun = this; gun._.events.on(gun._.events.trace += 1).event(function(node){ - console.log("shabam", node); if(gun._.field){ return cb((node||{})[gun._.field]); } @@ -636,7 +635,7 @@ tab.server = tab.server || function(req, res, next){ } - //window.tab = tab; //window.XMLHttpRequest = null; // for debugging purposes + window.tab = tab; //window.XMLHttpRequest = null; // for debugging purposes tab.load = tab.load || function(key, cb, opt){ cb = cb || function(){}; opt = opt || {}; @@ -705,8 +704,8 @@ Gun.obj.map(gun.__.opt.peers, function(peer, url){ tab.ajax(url + query, null, function(err, reply){ //console.log("poll", err, reply); + if(!reply || !reply.body){ return } // not interested in any null/0/''/undefined values tab.subscribe.poll(); - if(!reply){ return } // do anything? if(reply.headers){ tab.subscribe.sub = reply.headers['gun-sub'] || tab.subscribe.sub; } @@ -819,11 +818,11 @@ } opt.onload = opt.onload || function(reply){ if(!reply){ return } - if( reply.headers - && ("application/json" === reply.headers["content-type"]) - && (ajax.string(reply.body)) - ){ - reply.body = (reply.body === String(u))? u : JSON.parse(reply.body); + if( reply.headers && ("application/json" === reply.headers["content-type"])){ + var body; + try{body = JSON.parse(reply.body); + }catch(e){body = reply.body} + reply.body = body; } if(cb){ cb(null, reply); diff --git a/shots.js b/shots.js index 236bf3b5..0c987e46 100644 --- a/shots.js +++ b/shots.js @@ -38,10 +38,10 @@ } meta.CORS(req, res); // add option to disable this if(reply.chunk){ - res.write(Gun.text.ify(reply.chunk)); + res.write(Gun.text.ify(reply.chunk) || ''); } if(reply.body){ - res.end(Gun.text.ify(reply.body)); + res.end(Gun.text.ify(reply.body) || ''); } }); } @@ -240,7 +240,10 @@ return; } var next = tran.post.s[req.sub]; - if(!next){ return tran.post.s[req.sub] = cb } // was there a previous POST? If not, we become the previous POST. + if(!next){ // was there a previous POST? If not, we become the previous POST. + //cb({chunk: ''}); // because on some services (heroku) you need to reply starting a stream to keep the connection open. + return tran.post.s[req.sub] = cb; + } next.count = (next.count || 1) + 1; // start counting how many we accumulate //console.log("Counting up", next.count); next.body = next.body || {}; // this becomes the polyfill for all the posts @@ -291,6 +294,7 @@ } cb({ body: req.tab.queue.shift() }); } else { + cb({chunk: ''}); // same thing as the defer code, initialize a stream to support some services (heroku). req.tab.reply = cb; console.log("_____ STANDING BY, WAITING FOR DATA ______", req.sub); } @@ -330,7 +334,7 @@ if(res.chunk){ cb({ headers: reply.headers - ,body: Gun.text.ify(res.body) + '\n' + ,chunk: Gun.text.ify(res.chunk) + '\n' }) } if(res.body){ @@ -364,10 +368,9 @@ (reply.chunks = reply.chunks || []).push(res.chunk); } if(res.body){ - reply.body = res.body; - reply.body = ';'+ cb.jsonp + '(' + Gun.text.ify(reply) + ');'; + reply.body = res.body; // self-reference yourself so on the client we can get the headers and body. + reply.body = ';'+ cb.jsonp + '(' + Gun.text.ify(reply) + ');'; // javascriptify it! can't believe the client trusts us. cb(reply); - console.log("replied with jsonp!!!!!!"); } } }