mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
yson panic chat basic
This commit is contained in:
parent
4e61f11fd2
commit
74714e61e8
8
gun.js
8
gun.js
@ -400,7 +400,6 @@
|
||||
if(!(tmp = ctx.msg)){ return }
|
||||
tmp.out = universe;
|
||||
//console.log("PUT:", +new Date - ctx.START, ctx.lot); ctx.START = null;
|
||||
console.log('out', tmp);
|
||||
root.on('out', tmp);
|
||||
}
|
||||
var ERR = "Error: Invalid graph!";
|
||||
@ -1458,7 +1457,7 @@
|
||||
var tmp = raw[0], msg;
|
||||
if('[' === tmp){
|
||||
parse(raw, function(err, msg){
|
||||
if(err || !msg){ return mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
|
||||
if(err || !msg){ return } //mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
|
||||
console.STAT && console.STAT(+new Date, msg.length, '# on hear batch');
|
||||
var P = opt.puff;
|
||||
(function go(){
|
||||
@ -1474,10 +1473,10 @@
|
||||
raw = ''; //
|
||||
return;
|
||||
}
|
||||
if('{' === tmp || ((raw['#'] || obj_is(raw)) && (msg = raw))){
|
||||
if('{' === tmp || ((raw['#'] || Object.plain(raw)) && (msg = raw))){
|
||||
if(msg){ return hear.one(msg, peer) }
|
||||
parse(raw, function(err, msg){
|
||||
if(err || !msg){ return mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
|
||||
if(err || !msg){ return } //mesh.say({dam: '!', err: "DAM JSON parse error."}, peer) }
|
||||
hear.one(msg, peer);
|
||||
});
|
||||
return;
|
||||
@ -1552,7 +1551,6 @@
|
||||
if(!peer || !peer.id){ message = msg;
|
||||
if(!Object.plain(peer || opt.peers)){ return false }
|
||||
var P = opt.puff, ps = opt.peers, pl = Object.keys(peer || opt.peers || {});
|
||||
console.log('peers', pl);
|
||||
;(function go(){
|
||||
var S = +new Date;
|
||||
//Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
|
||||
|
@ -1,4 +1,5 @@
|
||||
;(function(){
|
||||
require('./yson');
|
||||
var Gun = require('../gun'), u;
|
||||
Gun.serve = require('./serve');
|
||||
//process.env.GUN_ENV = process.env.GUN_ENV || 'debug';
|
||||
|
67
lib/yson.js
67
lib/yson.js
@ -2,16 +2,16 @@
|
||||
// JSON: JavaScript Object Notation
|
||||
// YSON: Yielding javaScript Object Notation
|
||||
var yson = {}, u, sI = setTimeout.turn || (typeof setImmediate != ''+u && setImmediate) || setTimeout;
|
||||
|
||||
yson.parseAsync = function(text, done, revive, M){
|
||||
var ctx = {i: 0, text: text, done: done, o: {}, l: text.length, up: []};
|
||||
ctx.at = ctx.o;
|
||||
var ctx = {i: 0, text: text, done: done, l: text.length, up: []};
|
||||
//M = 1024 * 1024 * 100;
|
||||
//M = M || 1024 * 64;
|
||||
M = M || 1024 * 32;
|
||||
parse();
|
||||
function parse(){
|
||||
//var S = +new Date;
|
||||
var s = ctx.text, o = ctx.o;
|
||||
var s = ctx.text;
|
||||
var i = ctx.i, l = ctx.l, j = 0;
|
||||
var w = ctx.w, b, tmp;
|
||||
while(j++ < M){
|
||||
@ -71,7 +71,7 @@ yson.parseAsync = function(text, done, revive, M){
|
||||
}
|
||||
break;
|
||||
case '{':
|
||||
ctx.up.push(ctx.at);
|
||||
ctx.up.push(ctx.at||(ctx.at = {}));
|
||||
if(ctx.at instanceof Array){
|
||||
ctx.at.push(ctx.at = {});
|
||||
} else
|
||||
@ -99,8 +99,12 @@ yson.parseAsync = function(text, done, revive, M){
|
||||
if(tmp = ctx.s){
|
||||
ctx.up.push(ctx.at);
|
||||
ctx.at[tmp] = ctx.at = [];
|
||||
} else
|
||||
if(!ctx.at){
|
||||
ctx.up.push(ctx.at = []);
|
||||
}
|
||||
ctx.a = true;
|
||||
ctx.ai = i;
|
||||
break;
|
||||
case ']':
|
||||
if(ctx.a){
|
||||
@ -122,11 +126,14 @@ yson.parseAsync = function(text, done, revive, M){
|
||||
}
|
||||
ctx.i = i;
|
||||
ctx.w = w;
|
||||
//console.log("!!!!!!!!", +new Date - S, ctx.i, ctx.l);
|
||||
if(ctx.end){
|
||||
ctx.done(u, ctx.o);
|
||||
tmp = ctx.at;
|
||||
if(u === tmp){
|
||||
try{ tmp = JSON.parse(text)
|
||||
}catch(e){ return ctx.done(e) }
|
||||
}
|
||||
ctx.done(u, tmp);
|
||||
} else {
|
||||
//setTimeout.turn(parse);
|
||||
sI(parse);
|
||||
}
|
||||
}
|
||||
@ -157,39 +164,49 @@ yson.stringifyAsync = function(data, done, replacer, space, ctx){
|
||||
var j = 0;
|
||||
ify();
|
||||
function ify(){
|
||||
var at = ctx.at, data = at.d, tmp;
|
||||
//console.log(at.k, typeof data, data);
|
||||
if(at.i > 0){ ctx.text += ',' }
|
||||
if(u !== (tmp = at.k)){ ctx.text += '"'+tmp+'":' }
|
||||
var at = ctx.at, data = at.d, add = '', tmp;
|
||||
if(at.i && (at.i - at.j) > 0){ add += ',' }
|
||||
if(u !== (tmp = at.k)){ add += '"'+tmp+'":' }
|
||||
switch(typeof data){
|
||||
case 'boolean':
|
||||
ctx.text += ''+data;
|
||||
add += ''+data;
|
||||
break;
|
||||
case 'string':
|
||||
ctx.text += JSON.stringify(data);
|
||||
//ctx.text += '"'+data+'"';//JSON.stringify(data);
|
||||
add += JSON.stringify(data); //ctx.text += '"'+data+'"';//JSON.stringify(data);
|
||||
break;
|
||||
case 'number':
|
||||
ctx.text += data;
|
||||
add += data;
|
||||
break;
|
||||
case 'object':
|
||||
if(!data){
|
||||
ctx.text += 'null';
|
||||
add += 'null';
|
||||
break;
|
||||
}
|
||||
if(data instanceof Array){
|
||||
ctx.text += '[';
|
||||
at = {i: -1, as: data, up: at};
|
||||
add += '[';
|
||||
at = {i: -1, as: data, up: at, j: 0};
|
||||
at.l = data.length;
|
||||
ctx.up.push(ctx.at = at);
|
||||
break;
|
||||
}
|
||||
ctx.text += '{';
|
||||
at = {i: -1, ok: Object.keys(data).sort(), as: data, up: at};
|
||||
at.l = at.ok.length;
|
||||
ctx.up.push(ctx.at = at);
|
||||
break;
|
||||
if('function' != typeof (data||'').toJSON){
|
||||
add += '{';
|
||||
at = {i: -1, ok: Object.keys(data).sort(), as: data, up: at, j: 0};
|
||||
at.l = at.ok.length;
|
||||
ctx.up.push(ctx.at = at);
|
||||
break;
|
||||
}
|
||||
if(tmp = data.toJSON()){
|
||||
add += tmp;
|
||||
break;
|
||||
}
|
||||
// let this & below pass into default case...
|
||||
case 'function':
|
||||
default: // handle wrongly added leading `,` if previous item not JSON-able.
|
||||
add = '';
|
||||
at.j++;
|
||||
}
|
||||
ctx.text += add;
|
||||
while(1+at.i >= at.l){
|
||||
ctx.text += (at.ok? '}' : ']');
|
||||
at = ctx.at = at.up;
|
||||
@ -209,5 +226,9 @@ yson.stringifyAsync = function(data, done, replacer, space, ctx){
|
||||
}
|
||||
if(typeof window != ''+u){ window.YSON = yson }
|
||||
try{ if(typeof module != ''+u){ module.exports = yson } }catch(e){}
|
||||
if(typeof JSON != ''+u){
|
||||
JSON.parseAsync = yson.parseAsync;
|
||||
JSON.stringifyAsync = yson.stringifyAsync;
|
||||
}
|
||||
|
||||
}());
|
@ -15,6 +15,7 @@ describe('Gun', function(){
|
||||
root.Gun = root.Gun;
|
||||
root.Gun.TESTING = true;
|
||||
} else {
|
||||
require('../lib/yson');
|
||||
root.Gun = require('../gun');
|
||||
root.Gun.TESTING = true;
|
||||
Gun.serve = require('../lib/serve');
|
||||
@ -63,7 +64,7 @@ describe('Gun', function(){
|
||||
} );
|
||||
*/
|
||||
|
||||
describe.only('YSON', function(){
|
||||
describe('YSON', function(){
|
||||
it('parse', function(){
|
||||
//var json = require('fs').readFileSync('./radix.json').toString();
|
||||
//var json = require('fs').readFileSync('./data.json').toString();
|
||||
@ -71,12 +72,17 @@ describe('Gun', function(){
|
||||
//var json = require('fs').readFileSync('./stats.json').toString();
|
||||
//var json = require('fs').readFileSync('./video.json').toString();
|
||||
});
|
||||
it('stringify', function(){
|
||||
it('stringify', function(done){
|
||||
function Foo(){}; Foo.prototype.toJSON = function(){};
|
||||
//var obj = {"what\"lol": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}};
|
||||
var obj = {"what": {"a": 1, "b": true, "c": false, "d": null, "wow": [{"z": 9}, true, "hi", 3.3]}};
|
||||
YSON.stringifyAsync(obj, function(err, text){
|
||||
console.log("done!", text);
|
||||
})
|
||||
obj = [{x:"test",a:true,b: new Foo,c:3,y:"yes","get":{"#":"chat"},wow:undefined,foo:[1,function(){}, function(){}, 'go'],blah:{a:5,toJSON:function(){ return 9 }}}];
|
||||
JSON.stringifyAsync(obj, function(err, text){
|
||||
JSON.parseAsync(text, function(err, data){
|
||||
expect(data).to.be.eql([{x:"test",a:true,c:3,y:"yes","get":{"#":"chat"},foo:[1,'go'],blah:9}]);
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user