Merge pull request #11 from amark/add-verbose-mode

silent `console.log` by default, verbose via `Gun.log.verbose = true;` for debugging.
This commit is contained in:
Mark Nadal 2015-01-24 19:08:37 -07:00
commit 53e027bdf3
8 changed files with 71 additions and 52 deletions

62
gun.js
View File

@ -134,7 +134,7 @@
return {converge: true, incoming: true};
}
}
return {err: "you have not properly handled recursion through your data or filtered it as JSON"};
return {err: console.log("you have not properly handled recursion through your data or filtered it as JSON")};
}
var context = Gun.shot();
context.HAM = {};
@ -153,7 +153,7 @@
var state = HAM(serverState, context.states.delta[field], context.states.current[field], deltaValue, current[field]);
//console.log("HAM:", field, deltaValue, context.states.delta[field], context.states.current[field], 'the', state, (context.states.delta[field] - serverState));
if(state.err){
console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", state.err);
root.console.log(".!HYPOTHETICAL AMNESIA MACHINE ERR!.", state.err); // this error should never happen.
return;
}
if(state.state || state.quarantineState || state.current){
@ -188,14 +188,6 @@
}
return Gun.text.random(l, c);
}
Gun.log = function(a, b, c, d, e, f){
var gun = this;
if(!gun || !gun.__ || !gun.__.opt){
return console.log.apply(console, arguments);
}
if(gun.__.opt.quiet){ return }
return console.log.apply(console, arguments);
}
}(Gun));
;(function(Chain){
Chain.opt = function(opt, stun){ // idempotently update or set options
@ -247,7 +239,7 @@
(gun._.keys = gun._.keys || {})[key] = cb.node? 1 : 0; // set a key marker on it
}
if(!opt.force && cb.node){ // if it was in cache, then...
Gun.log.call(gun, "load via gun");
console.log("load via gun");
gun._.node = cb.node; // assign it to this context
cb.call(gun, null, Gun.obj.copy(gun._.node)); // frozen copy
gun.shot('then').fire();
@ -271,7 +263,7 @@
gun.shot('then').fire();
}, opt);
} else {
Gun.log.call(gun, "Warning! You have no persistence layer to load from!");
cb.call(gun, {err: console.log("Warning! You have no persistence layer to load from!")});
}
});
gun.shot('done').fire(); // because we are loading, we always fire!
@ -299,7 +291,7 @@
return cb.call(gun, null);
});
} else {
Gun.log.call(gun, "Warning! You have no key hook!");
cb.call(gun, {err: console.log("Warning! You have no key hook!")});
}
});
return gun;
@ -396,14 +388,14 @@
val = partial;
} else
if(!Gun.obj.is(val)){
return cb.call(gun, {err: "No field exists to set the " + (typeof val) + " on."});
return cb.call(gun, {err: console.log("No field exists to set the " + (typeof val) + " on.")});
}
// TODO: should be able to handle val being a relation or a gun context or a gun promise.
// TODO: BUG: IF we are setting an object, doing a partial merge, and they are reusing a frozen copy, we need to do a DIFF to update the HAM! Or else we'll get "old" HAM.
val._ = Gun.ify.soul.call(gun, {}, gun._.node || val); // set their souls to be the same that way they will merge correctly for us during the union!
set = Gun.ify.call(gun, val, set);
cb.root = set.root;
if(set.err || !cb.root){ return cb.call(gun, set.err || {err: "No root object!"}) }
if(set.err || !cb.root){ return cb.call(gun, set.err || {err: console.log("No root object!")}) }
set = Gun.ify.state(set.nodes, Gun.time.is()); // set time state on nodes?
if(set.err){ return cb.call(gun, set.err) }
gun.union(set.nodes); // while this maybe should return a list of the nodes that were changed, we want to send the actual delta
@ -420,7 +412,7 @@
return cb.call(gun, null);
});
} else {
Gun.log.call(gun, "Warning! You have no persistence layer to save to!");
cb.call(gun, {err: console.log("Warning! You have no persistence layer to save to!")})
}
});
if(!gun.back){
@ -438,7 +430,7 @@
}).get(function(val){
if(error){ return cb.call(gun, error) } // which in case it is, allows us to fail fast.
var list = {}, soul = Gun.is.soul.on(val);
if(!soul){ return cb.call(gun, {err: "No soul!"}) }
if(!soul){ return cb.call(gun, {err: console.log("No soul!")}) }
list[soul] = val; // other wise, let's then
gun.set(list, cb); // merge with the graph node.
});
@ -471,20 +463,20 @@
cb = cb || function(){}
context.nodes = {};
if(!prime){
context.err = {err: "No data to merge!"};
context.err = {err: console.log("No data to merge!")};
} else
if(Gun.is.soul.on(prime)){
tmp[prime._[Gun._.soul]] = prime;
prime = tmp;
}
if(!gun || context.err){
cb(context.err = context.err || {err: "No gun instance!", corrupt: true}, context);
cb(context.err = context.err || {err: console.log("No gun instance!"), corrupt: true}, context);
return context;
}
if(!Gun.is.graph(prime, function(node, soul){
context.nodes[soul] = node;
})){
cb(context.err = context.err || {err: "Invalid graph!", corrupt: true}, context);
cb(context.err = context.err || {err: console.log("Invalid graph!"), corrupt: true}, context);
return context;
}
if(context.err){ return cb(context.err, context), context } // if any errors happened in the previous steps, then fail.
@ -768,7 +760,7 @@
}({}));
;(function(Serializer){
Gun.ify = function(data, bind){ // TODO: BUG: Modify lists to include HAM state
if(Gun.obj.map(bind, function(){ return true })){ return {err: "Bind must be an empty object."} }
if(Gun.obj.map(bind, function(){ return true })){ return {err: console.log("Bind must be an empty object.")} }
var gun = Gun.is(this)? this : {}
, context = {
nodes: {}
@ -785,16 +777,16 @@
} else
if(Gun.obj.is(data)){
var value = bind || {}, symbol = {}, seen
, err = {err: "Metadata does not support external or circular references at " + sub.path, meta: true};
, err = {err: console.log("Metadata does not support external or circular references at " + sub.path), meta: true};
context.root = context.root || value;
bind = null;
if(seen = ify.seen(context._seen, data)){
//Gun.log.call(gun, "seen in _", sub._, sub.path, data);
//console.log("seen in _", sub._, sub.path, data);
context.err = err;
return;
} else
if(seen = ify.seen(context.seen, data)){
//Gun.log.call(gun, "seen in data", sub._, sub.path, data);
//console.log("seen in data", sub._, sub.path, data);
if(sub._){
context.err = err;
return;
@ -802,7 +794,7 @@
symbol = Gun.ify.soul.call(gun, symbol, seen);
return symbol;
} else {
//Gun.log.call(gun, "seen nowhere", sub._, sub.path, data);
//console.log("seen nowhere", sub._, sub.path, data);
if(sub._){
context.seen.push({data: data, node: value});
} else {
@ -814,7 +806,7 @@
Gun.obj.map(data, function(val, field){
var subs = {path: sub.path + field + '.', _: sub._ || (field == Gun._.meta)? true : false };
val = ify(val, context, subs);
//Gun.log.call(gun, '>>>>', sub.path + field, 'is', val);
//console.log('>>>>', sub.path + field, 'is', val);
if(context.err){ return true }
if(nothing === val){ return }
// TODO: check field validity
@ -827,7 +819,7 @@
} else
if(Gun.list.is(data)){
var unique = {}, edges
, err = {err: "Arrays cause data corruption at " + sub.path, array: true}
, err = {err: console.log("Arrays cause data corruption at " + sub.path), array: true}
edges = Gun.list.map(data, function(val, i, map){
val = ify(val, context, sub);
if(context.err){ return true }
@ -848,7 +840,7 @@
if(context.err){ return }
return edges;
} else {
context.err = {err: "Data type not supported at " + sub.path, invalid: true};
context.err = {err: console.log("Data type not supported at " + sub.path), invalid: true};
}
}
ify.seen = function(seen, data){
@ -866,7 +858,7 @@
context.now = now = (now === 0)? now : now || Gun.time.is();
Gun.obj.map(context.nodes, function(node, soul){
if(!node || !soul || !node._ || !node._[Gun._.soul] || node._[Gun._.soul] !== soul){
return context.err = {err: "There is a corruption of nodes and or their souls", corrupt: true};
return context.err = {err: console.log("There is a corruption of nodes and or their souls"), corrupt: true};
}
var states = node._[Gun._.HAM] = node._[Gun._.HAM] || {};
Gun.obj.map(node, function(val, field){
@ -893,6 +885,9 @@
} else {
module.exports = Gun;
}
var root = this || {}; // safe for window, global, root, and 'use strict'.
root.console = root.console || {log: function(s){ return s }}; // safe for old browsers
var console = {log: Gun.log = function(s){return (Gun.log.verbose && root.console.log.apply(root.console, arguments)), s}};
}({}));
;(function(tab){
@ -928,21 +923,20 @@
cb.peers = true;
});
if(!cb.peers){ // there are no peers! this is a local only instance
Gun.log.call(gun, "Warning! You have no peers to connect to!");
setTimeout(function(){cb({err: "No peers!"})},0);
setTimeout(function(){cb({err: console.log("Warning! You have no peers to connect to!")})},0);
}
}
tab.key = function(key, soul, cb){
var meta = {};
meta[Gun._.soul] = soul = Gun.text.is(soul)? soul : (soul||{})[Gun._.soul];
if(!soul){
return cb({err: "No soul!"});
return cb({err: console.log("No soul!")});
}
Gun.obj.map(gun.__.opt.peers, function(peer, url){
request(url, meta, function(err, reply){
//console.log("gun key done", soul, err, reply);
if(err || !reply){
Gun.log.call(gun, err = err || "Error: Key failed to be made on " + url);
console.log(err = err || "Error: Key failed to be made on " + url);
// tab.key(key, soul, cb); // naive implementation of retry TODO: BUG: need backoff and anti-infinite-loop!
cb(err);
} else {
@ -993,7 +987,7 @@
}
tab.set.defer = {};
request.createServer(function(req, res){
//console.log("client server received request", req);
//.log("client server received request", req);
if(!req.body){ return }
gun.union(req.body);
});

View File

@ -43,7 +43,7 @@
m.Body = a.text.is(o)? o : a.text.ify(o);
}
this.S3().putObject(m, function(e,r){
//console.log('saved', e,r);
//Gun.log('saved', e,r);
if(!cb){ return }
cb(e,r);
});
@ -67,7 +67,7 @@
s.batch = s.batch || {};
if(s.batch[id]){ return s }
s.batch[id] = (s.batch[id] || 0) + 1;
console.log("no batch!");
Gun.log("no batch!");
s.S3().getObject(m, function(e,r){
var d, t, m;
r = r || (this && this.httpResponse);
@ -98,9 +98,9 @@
cb = cb || o;
var m = {}
this.S3().listBuckets(m, function(e,r){
//console.log('dbs',e);
//Gun.log('dbs',e);
a.list.map((r||{}).Contents, function(v){console.log(v);});
//console.log('---end list---');
//Gun.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});
@ -118,9 +118,9 @@
m.Delimiter = upto;
}
this.S3().listObjects(m, function(e,r){
//console.log('list',e);
//Gun.log('list',e);
a.list.each((r||{}).Contents, function(v){console.log(v)});
//console.log('---end list---');
//Gun.log('---end list---');
if(!a.fns.is(cb)) return;
cb(e,r);
});

View File

@ -22,7 +22,7 @@
key = s3.prefix + s3.prekey + key;
}
s3.get(key, function(err, data, text, meta){
console.log('via s3', key, err);
Gun.log('via s3', key, err);
if(meta && meta[Gun._.soul]){
return s3.load(meta, cb);
}
@ -43,10 +43,10 @@
Gun.obj.map(nodes, function(node, soul){
cb.count += 1;
batch[soul] = (batch[soul] || 0) + 1;
//console.log("set listener for", next + ':' + soul, batch[soul], cb.count);
//Gun.log("set listener for", next + ':' + soul, batch[soul], cb.count);
s3.on(next + ':' + soul).event(function(){
cb.count -= 1;
//console.log("transaction", cb.count);
//Gun.log("transaction", cb.count);
if(!cb.count){
s3.on(ack).emit();
this.off(); // MEMORY LEAKS EVERYWHERE!!!!!!!!!!!!!!!! FIX THIS!!!!!!!!!
@ -71,7 +71,7 @@
Gun.obj.map(batch, function put(exists, soul){
var node = gun.__.graph[soul]; // the batch does not actually have the nodes, but what happens when we do cold data? Could this be gone?
s3.put(s3.prefix + s3.prenode + soul, node, function(err, reply){
console.log("s3 put reply", soul, err, reply);
Gun.log("s3 put reply", soul, err, reply);
if(err || !reply){
put(exists, soul); // naive implementation of retry TODO: BUG: need backoff and anti-infinite-loop!
return;
@ -95,7 +95,7 @@
return cb({err: "No soul!"});
}
s3.put(s3.prefix + s3.prekey + key, '', function(err, reply){ // key is 2 bytes??? Should be smaller. Wait HUH? What did I mean by this?
console.log("s3 put reply", soul, err, reply);
Gun.log("s3 put reply", soul, err, reply);
if(err || !reply){
s3.key(key, soul, cb); // naive implementation of retry TODO: BUG: need backoff and anti-infinite-loop!
return;

View File

@ -1,5 +1,5 @@
;(function(){
console.log("Hello wonderful person! :) I'm mark@gunDB.io, message me for help or with hatemail. I want to hear from you! <3");
Gun.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');

View File

@ -7,7 +7,7 @@ module.exports = function(wss, server){
req.url = url.parse(ws.upgradeReq.url||'');
req.method = (ws.upgradeReq.method||'').toLowerCase();
req.headers = ws.upgradeReq.headers || {};
//console.log("wsReq", req);
//Gun.log("wsReq", req);
ws.on('message', function(msg){
msg = Gun.obj.ify(msg);
msg.url = msg.url || {};
@ -28,7 +28,7 @@ module.exports = function(wss, server){
});
});
ws.off = function(m){
console.log("ws.off", m);
Gun.log("ws.off", m);
ws.send = null;
}
ws.on('close', ws.off);

View File

@ -32,7 +32,7 @@
return gun;
}
gun.server = gun.server || function(req, res, next){
//console.log("\n\n GUN SERVER!", req);
//Gun.log("\n\n GUN SERVER!", req);
next = next || function(){};
if(!req || !res){ return next() }
if(!req.url){ return next() }
@ -90,7 +90,7 @@
// except for the ones that are listed in the message as having already been sending to.
// all states, implemented with GET, are replied to the source that asked for it.
function tran(req, cb){
//console.log("gun.server", req);
//Gun.log("gun.server", req);
req.method = req.body? 'post' : 'get'; // post or get is based on whether there is a body or not
req.url.key = req.url.pathname.replace(gun.server.regex,'').replace(/^\//i,'') || '';
if('get' == req.method){ return tran.load(req, cb) }
@ -107,7 +107,7 @@
key = {};
key[Gun._.soul] = req.url.query[Gun._.soul];
}
//console.log("transport.loading key ->", key, gun.__.graph, gun.__.keys);
//Gun.log("transport.loading key ->", key, gun.__.graph, gun.__.keys);
gun.load(key, function(err, node){
//tran.sub.scribe(req.tab, node._[Gun._.soul]);
// TODO: MAJOR BUG!!!! This is crashing server if the socket gets disconnected or something.

View File

@ -1,2 +1,4 @@
console.log("MAKE SURE TO DELETE `data.json` BEFORE RUNNING TESTS!");
var expect = global.expect = require("./expect");
require('./common');
require('./common');

View File

@ -2,6 +2,29 @@ describe('Gun', function(){
var Gun = require('../gun')
, t = {};
describe('Utility', function(){
it('verbose console.log debugging', function(done) {
var gun = Gun();
var log = root.console.log, counter = 1;
root.console.log = function(a,b,c){
--counter;
//log(a,b,c);
}
Gun.log.verbose = true;
gun.set('bar', function(err, yay){ // intentionally trigger an error that will get logged.
expect(counter).to.be(0);
Gun.log.verbose = false;
gun.set('bar', function(err, yay){ // intentionally trigger an error that will get logged.
expect(counter).to.be(0);
root.console.log = log;
done();
});
});
});
describe('Type Check', function(){
it('binary', function(){
expect(Gun.bi.is(false)).to.be(true);