Jelajahi Sumber

update gun db to 0.2020.421

Nikolay Suslov 4 tahun lalu
induk
melakukan
b3fade6b63
5 mengubah file dengan 67 tambahan dan 48 penghapusan
  1. 21 21
      package-lock.json
  2. 1 1
      package.json
  3. 26 16
      public/lib/gundb/gun.js
  4. 1 1
      public/lib/gundb/lib/ws.js
  5. 18 9
      public/lib/gundb/sea.js

+ 21 - 21
package-lock.json

@@ -5,9 +5,9 @@
   "requires": true,
   "dependencies": {
     "@peculiar/asn1-schema": {
-      "version": "1.0.5",
-      "resolved": "https://registry.npmjs.org/@peculiar/asn1-schema/-/asn1-schema-1.0.5.tgz",
-      "integrity": "sha512-rzzorGYnQNmVHleLvC8gJSbbdNYtg+EB9s075dHvwpxs10teXHYnRmTWhCVuWjbSVSofwdm7IYPtMTWTbcNUWA==",
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/@peculiar/asn1-schema/-/asn1-schema-1.1.2.tgz",
+      "integrity": "sha512-ntQ4UnUFgdjs0tfWR6YmEQm/x0glV4OFus/RjxLkaJUKfu/R7VilefBntyUO3MoKWdlCgib30KN+JpCY1HqU2A==",
       "optional": true,
       "requires": {
         "asn1js": "^2.0.26",
@@ -24,16 +24,16 @@
       }
     },
     "@peculiar/webcrypto": {
-      "version": "1.0.27",
-      "resolved": "https://registry.npmjs.org/@peculiar/webcrypto/-/webcrypto-1.0.27.tgz",
-      "integrity": "sha512-sERMakD19gNhwBVXGGoJjBfc28bDbd2YWaio7/x8jKtvwMKNuljM7ANQ6LzEkEvqFAyjf3bhBZktJ6UXy/0Plg==",
+      "version": "1.0.29",
+      "resolved": "https://registry.npmjs.org/@peculiar/webcrypto/-/webcrypto-1.0.29.tgz",
+      "integrity": "sha512-3e6CIpjZuj5WxPymGm0kCZA05XA9BSXwBEb/mU9KjBHAmEwMokT0yVIhxqwspCHSynlrTfUB+9KTNpseyB077A==",
       "optional": true,
       "requires": {
-        "@peculiar/asn1-schema": "^1.0.5",
+        "@peculiar/asn1-schema": "^1.1.2",
         "@peculiar/json-schema": "^1.1.10",
         "pvtsutils": "^1.0.10",
         "tslib": "^1.11.1",
-        "webcrypto-core": "^1.0.19-next.0"
+        "webcrypto-core": "^1.0.20"
       }
     },
     "@types/color-name": {
@@ -244,9 +244,9 @@
       }
     },
     "buffer": {
-      "version": "5.5.0",
-      "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.5.0.tgz",
-      "integrity": "sha512-9FTEDjLjwoAkEwyMGDjYJQN2gfRgOKBKRfiglhvibGbpeeU/pQn1bJxQqm32OD/AIeEuHxU9roxXxg34Byp/Ww==",
+      "version": "5.6.0",
+      "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz",
+      "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==",
       "requires": {
         "base64-js": "^1.0.2",
         "ieee754": "^1.1.4"
@@ -685,9 +685,9 @@
       "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="
     },
     "gun": {
-      "version": "0.2020.401",
-      "resolved": "https://registry.npmjs.org/gun/-/gun-0.2020.401.tgz",
-      "integrity": "sha512-CiNZGqEgMjoXXs/HYs0g3hnX+cetME1WbHzRrZlCiQpdVQsAOPsKff+nnyJVTuiM01BN8P86g6dBUtlh63cekA==",
+      "version": "0.2020.421",
+      "resolved": "https://registry.npmjs.org/gun/-/gun-0.2020.421.tgz",
+      "integrity": "sha512-ys7BOwq+O8/BkQHgqYlkSPlrUK9VTu+9d45XHoIGTZT9X6WaJzF3D3Rtxr7PMMYR7AGgxaHAjt1n3VT4rW1mdg==",
       "requires": {
         "buffer": "^5.4.3",
         "bufferutil": "^4.0.1",
@@ -1488,12 +1488,12 @@
       "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
     },
     "webcrypto-core": {
-      "version": "1.0.19",
-      "resolved": "https://registry.npmjs.org/webcrypto-core/-/webcrypto-core-1.0.19.tgz",
-      "integrity": "sha512-6XHExtfMJrpkFDh9MiJ/y7ptX0dfZi0ogxFyelqxMu1eFowxivHfIp6DKzT+ZjU66xTuNfJkfkUk1bIB3tEOgA==",
+      "version": "1.0.21",
+      "resolved": "https://registry.npmjs.org/webcrypto-core/-/webcrypto-core-1.0.21.tgz",
+      "integrity": "sha512-PcSD8Ga3u344LXEcI/1UbGv3dv5rZ8pzkeJIrMf/JOnEq8H5hSMmdTrRIp2WFYYnMNdKQbP564v560ZmERT2HQ==",
       "optional": true,
       "requires": {
-        "@peculiar/asn1-schema": "^1.0.5",
+        "@peculiar/asn1-schema": "^1.1.2",
         "@peculiar/json-schema": "^1.1.10",
         "asn1js": "^2.0.26",
         "pvtsutils": "^1.0.10",
@@ -1522,9 +1522,9 @@
       }
     },
     "ws": {
-      "version": "7.2.3",
-      "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.3.tgz",
-      "integrity": "sha512-HTDl9G9hbkNDk98naoR/cHDws7+EyYMOdL1BmjsZXRUjf7d+MficC4B7HLUPlSiho0vg+CWKrGIt/VJBd1xunQ=="
+      "version": "7.2.5",
+      "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.5.tgz",
+      "integrity": "sha512-C34cIU4+DB2vMyAbmEKossWq2ZQDr6QEyuuCzWrM9zfw1sGc0mYiJ0UnG9zzNykt49C2Fi34hvr2vssFQRS6EA=="
     },
     "xmlhttprequest-ssl": {
       "version": "1.5.5",

+ 1 - 1
package.json

@@ -20,7 +20,7 @@
     "yargs": "15.3.1",
     "socket.io": "2.2.0",
     "lcs-reflector": "0.3",
-    "gun": "0.2020.401"
+    "gun": "0.2020.421"
   },
   "scripts": {
     "start": "node index.js",

+ 26 - 16
public/lib/gundb/gun.js

@@ -795,7 +795,7 @@
 				if((tmp = ctx.out) && (tmp = tmp.put)){
 					tmp[soul] = state_ify(tmp[soul], key, state, val, soul); // TODO: Hacky, fix & come back later, for actual pushing messages.
 				}
-				if(!(--ctx.lot.more)){ fire(ctx) }
+				if(!(--ctx.lot.more)){ fire(ctx) } // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
 				eve.to.next(msg);
 			}
 			function chain(ctx, soul, key,val, state){
@@ -987,7 +987,7 @@
 		if(typeof window !== "undefined"){ (window.GUN = window.Gun = Gun).window = window }
 		try{ if(typeof MODULE !== "undefined"){ MODULE.exports = Gun } }catch(e){}
 		module.exports = Gun;
-		
+
 		(Gun.window||'').console = (Gun.window||'').console || {log: function(){}};
 		(C = console).only = function(i, s){ return (C.only.i && i === C.only.i && C.only.i++) && (C.log.apply(C, arguments) || s) };
 
@@ -1499,7 +1499,8 @@
 					if(as.res){ as.res() }
 					return gun;
 				}
-				as.soul = as.soul || (as.not = Gun.node.soul(as.data) || (as.via.back('opt.uuid') /*|| Gun.text.random*/)());
+				as.soul = as.soul || (as.not = Gun.node.soul(as.data) || (as.via.back('opt.uuid') || Gun.text.random)());
+				as.via._.stun = {};
 				if(!as.soul){ // polyfill async uuid for SEA
 					as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
 						if(err){ return Gun.log(err) } // TODO: Handle error!
@@ -1512,9 +1513,11 @@
 				ify(as);
 				return gun;
 			}
+			as.via._.stun = {};
 			if(Gun.is(data)){
 				data.get(function(soul, o, msg){
 					if(!soul){
+						delete as.via._.stun;
 						return Gun.log("The reference you are saving is a", typeof msg.put, '"'+ msg.put +'", not a node (object)!');
 					}
 					gun.put(Gun.val.link.ify(soul), cb, as);
@@ -1578,6 +1581,7 @@
 			if(!as.graph || !obj_empty(as.stun)){ return }
 			as.res = as.res || function(cb){ if(cb){ cb() } };
 			as.res(function(){
+				delete as.via._.stun;
 				var cat = (as.$.back(-1)._), ask = cat.ask(function(ack){
 					cat.root.on('ack', ack);
 					if(ack.err){ Gun.log(ack) }
@@ -1598,6 +1602,7 @@
 				});
 				cat.root.mum = mum? obj.to(mum, cat.root.mum) : mum;
 				cat.root.now = tmp;
+				as.via._.on('res', {}); delete as.via._.tag.res; // emitting causes mem leak?
 			}, as);
 			if(as.res){ as.res() }
 		} function no(v,k){ if(v){ return true } }
@@ -1620,17 +1625,19 @@
 					return;
 				}
 				(as.stun = as.stun || {})[path] = 1;
-				ref.get(soul, true, {as: {at: at, as: as, p:path}});
+				ref.get(soul, true, {as: {at: at, as: as, p:path, ref: ref}});
 			}, {as: as, at: at});
 			//if(is){ return {} }
 		}
 		var G = String.fromCharCode(31);
 		function soul(id, as, msg, eve){
-			var as = as.as, path = as.p, cat = as.at; as = as.as;
+			var as = as.as, path = as.p, ref = as.ref, cat = as.at; as = as.as;
+			var sat = ref.back(function(at){ return sat = at.soul || at.link || at.dub });
+			var pat = [sat || as.soul].concat(ref._.has || ref._.get || path)
 			var at = ((msg || {}).$ || {})._ || {};
-			id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || (as.via.back('opt.uuid') || function(){
+			id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || pat.join('/') /* || (function(){
 				return (as.soul+'.')+Gun.text.hash(path.join(G)).toString(32);
-			})(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? What about copy operations?
+			})(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? What about copy operations? */
 			if(eve){ eve.stun = true }
 			if(!id){ // polyfill async uuid for SEA
 				as.via.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback
@@ -1662,6 +1669,7 @@
 			if(as.ref !== as.$){
 				tmp = (as.$._).get || at.get;
 				if(!tmp){ // TODO: Handle
+					delete as.via._.stun;
 					Gun.log("Please report this as an issue! Put.no.get"); // TODO: BUG!??
 					return;
 				}
@@ -1669,7 +1677,7 @@
 				tmp = null;
 			}
 			if(u === data){
-				if(!at.get){ return } // TODO: Handle
+				if(!at.get){ delete as.via._.stun; return } // TODO: Handle
 				if(!soul){
 					tmp = at.$.back(function(at){
 						if(at.link || at.soul){ return at.link || at.soul }
@@ -1684,17 +1692,17 @@
 			}
 			if(!as.not && !(as.soul = as.soul || soul)){
 				if(as.path && obj_is(as.data)){
-					as.soul = (opt.uuid || as.via.back('opt.uuid') /*|| Gun.text.random*/)();
+					as.soul = (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
 				} else {
 					//as.data = obj_put({}, as.$._.get, as.data);
 					if(node_ == at.get){
 						as.soul = (at.put||empty)['#'] || at.dub;
 					}
-					as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') /*|| Gun.text.random*/)();
+					as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
 				}
 				if(!as.soul){ // polyfill async uuid for SEA
 					as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
-						if(err){ return Gun.log(err) } // Handle error.
+						if(err){ delete as.via._.stun; return Gun.log(err) } // Handle error.
 						as.ref.put(as.data, as.soul = soul, as);
 					});
 					return;
@@ -1903,12 +1911,14 @@
 			opt = opt || {}; opt.item = opt.item || item;
 			if(soul = Gun.node.soul(item)){ item = Gun.obj.put({}, soul, Gun.val.link.ify(soul)) }
 			if(!Gun.is(item)){
-				if(Gun.obj.is(item)){;
-					item = gun.back(-1).get(soul = soul || Gun.node.soul(item) || (gun.back('opt.uuid') || uuid)()).put(item);
+				if(Gun.obj.is(item)){
+					//item = gun.back(-1).get(soul = soul || Gun.node.soul(item) || (gun.back('opt.uuid') || uuid)()).put(item);
+					soul = soul || Gun.node.soul(item) || uuid(); // this just key now, not a soul.
 				}
 				return gun.get(soul || uuid()).put(item, cb, opt);
 			}
 			item.get(function(soul, o, msg){
+				if(!soul && item._.stun){ item._.on('res', function(){ this.off(); gun.set(item, cb, opt) }); return }
 				if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}) }
 				gun.put(Gun.obj.put({}, soul, Gun.val.link.ify(soul)), cb, opt);
 			},true);
@@ -2148,7 +2158,7 @@
 				var message, loop;
 				function each(peer){ mesh.say(message, peer) }
 				var say = mesh.say = function(msg, peer){
-					if(this.to){ this.to.next(msg) } // compatible with middleware adapters.
+					if(this && this.to){ this.to.next(msg) } // compatible with middleware adapters.
 					if(!msg){ return false }
 					var id, hash, tmp, raw;
 					var DBG = msg.DBG, S; if(!peer){ S = +new Date ; DBG && (DBG.y = S) }
@@ -2314,7 +2324,7 @@
 			});
 
 			root.on('bye', function(peer, tmp){
-				peer = opt.peers[peer.id || peer] || peer; 
+				peer = opt.peers[peer.id || peer] || peer;
 				this.to.next(peer);
 				peer.bye? peer.bye() : (tmp = peer.wire) && tmp.close && tmp.close();
 				Type.obj.del(opt.peers, peer.id);
@@ -2429,4 +2439,4 @@
 		var noop = function(){};
 	})(USE, './adapters/websocket');
 
-}());
+}());

+ 1 - 1
public/lib/gundb/lib/ws.js

@@ -19,7 +19,7 @@ Gun.on('opt', function mount(ctx){
 		ws.server = ws.server || opt.web;
 		ws.path = ws.path || '/gun';
 
-		ws.web = new WebSocket.Server(ws);
+		if (!ws.web) ws.web = new WebSocket.Server(ws);
 
 		ws.web.on('connection', function(wire){
 			wire.upgradeReq = wire.upgradeReq || {};

+ 18 - 9
public/lib/gundb/sea.js

@@ -734,13 +734,13 @@
       if(user = root.back('user')){ return user }
       var root = (root._), at = root, uuid = at.opt.uuid || Gun.state.lex;
       (at = (user = at.user = gun.chain(new User))._).opt = {};
-      /*at.opt.uuid = function(cb){
+      at.opt.uuid = function(cb){
         var id = uuid(), pub = root.user;
         if(!pub || !(pub = pub.is) || !(pub = pub.pub)){ return id }
-        id = id + '~' + pub + '.';
+        id = id + '~' + pub + '/';
         if(cb && cb.call){ cb(null, id) }
         return id;
-      }*/
+      }
       return user;
     }
     Gun.User = User;
@@ -1160,9 +1160,14 @@
       var no = function(why){ at.on('in', {'@': id, err: why}) };
       //var no = function(why){ msg.ack(why) };
       (msg._||'').DBG && ((msg._||'').DBG.c = +new Date);
-      if(0 <= soul.indexOf('#')){ // special case for content addressing immutable hashed data.
-        check.hash(eve, msg, val, key, soul, at, no); return;
-      } 
+      if(0 <= soul.indexOf('<?')){ // special case for "do not sync data X old"
+        // 'a~pub.key/b<?9'
+        tmp = parseFloat(soul.split('<?')[1]||'');
+        if(tmp && (state < (Gun.state() - (tmp * 1000)))){ // sec to ms
+          (tmp = msg._) && (tmp = tmp.lot) && (tmp.more--); // THIS IS BAD CODE! It assumes GUN internals do something that will probably change in future, but hacking in now.
+          return; // omit!
+        }
+      }
       if('~@' === soul){  // special case for shared system data, the list of aliases.
         check.alias(eve, msg, val, key, soul, at, no); return;
       }
@@ -1173,6 +1178,9 @@
       if(tmp = SEA.opt.pub(soul)){ // special case, account data for a public key.
         check.pub(eve, msg, val, key, soul, at, no, at.user||'', tmp); return;
       }
+      if(0 <= soul.indexOf('#')){ // special case for content addressing immutable hashed data.
+        check.hash(eve, msg, val, key, soul, at, no); return;
+      } 
       check.any(eve, msg, val, key, soul, at, no, at.user||''); return;
       eve.to.next(msg); // not handled
     }
@@ -1380,13 +1388,14 @@
       }
       to.next(msg); // pass forward any data we do not know how to handle or process (this allows custom security protocols).
     }
+    var pubcut = /[^\w_-]/; // anything not alphanumeric or _ -
     SEA.opt.pub = function(s){
       if(!s){ return }
       s = s.split('~');
       if(!s || !(s = s[1])){ return }
-      s = s.split('.');
-      if(!s || 2 > s.length){ return }
-      if('@' === (s[0]||'')[0]){ return } // TODO: Should check ~X.Y. are alphanumeric, not just not @.
+      s = s.split(pubcut).slice(0,2);
+      if(!s || 2 != s.length){ return }
+      if('@' === (s[0]||'')[0]){ return }
       s = s.slice(0,2).join('.');
       return s;
     }