diff --git a/as.js b/as.js index 94505c64..f6ac4f37 100644 --- a/as.js +++ b/as.js @@ -80,12 +80,11 @@ }, wait || 200); } } - as.sort = function(sort, el) { - var vs = $(el).find('.sort'); - vs = (vs[0] && u === vs[0].value)? vs.text() : vs.val(); - var id = sort; - var test = id >= vs; - return test ? el : as.sort(sort, el.prev()); + as.sort = function sort(id, li){ + var num = parseFloat(id); + var id = $(li).find('.sort').text() || -Infinity; + var at = num >= parseFloat(id); + return at ? li : sort(id, li.prev()); } $(document).on('keyup', 'input, textarea, [contenteditable]', as.wait(function(){ var el = $(this); diff --git a/examples/social.html b/examples/social.html index 9ff941d8..998126bf 100644 --- a/examples/social.html +++ b/examples/social.html @@ -527,7 +527,7 @@
  • -
    +
  • @@ -553,9 +553,9 @@ $('#speak .draft').text(''); }); S.gun.get('@').time(async (data, key, time) => { - var ref = S.gun.get(data); + var ref = S.gun.get(data), tmp; var said = await ref.then(); - var $li = $($('#'+key)[0] || $('#draft .model .spoke').clone(true,true).attr('id', key).prependTo('#draft ul')); + var $li = $($('#'+key)[0] || $('#draft .model .spoke').clone(true,true).attr('id', key)[(tmp = $.as.sort(time, $('#draft ul').children('li').first()))[0]?'insertBefore':'appendTo'](tmp[0] || '#draft ul')); $li.find('.what').text(said.what); var by = ref.get('by'); by.get('face').get('small').on(data => { @@ -564,6 +564,7 @@ by.get('name').on(data => { data && $li.find('b').text(data); }); + $li.find('.sort').text(time); var time = new Date(time); $li.find('i').text(time.toDateString() + ', ' + time.toLocaleTimeString()); return; diff --git a/gun.js b/gun.js index a0f153d2..0a168502 100644 --- a/gun.js +++ b/gun.js @@ -807,11 +807,11 @@ Gun.on.get = function(msg, gun){ var root = gun._, get = msg.get, soul = get[_soul], node = root.graph[soul], has = get[_has], tmp; var next = root.next || (root.next = {}), at = next[soul]; - if(get['*']){ // TEMPORARY HACK FOR MARTTI, TESTING + if(obj_has(soul, '*')){ // TEMPORARY HACK FOR MARTTI, TESTING var graph = {}; - Gun.obj.map(root.graph, function(node, soul){ - if(Gun.text.match(soul, get)){ - graph[soul] = Gun.obj.copy(node); + Gun.obj.map(root.graph, function(node, s){ + if(Gun.text.match(s, soul)){ + graph[s] = Gun.obj.copy(node); } }); if(!Gun.obj.empty(graph)){ diff --git a/package.json b/package.json index be4cf9d3..5b53f8cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.9.99992", + "version": "0.9.99993", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "main": "index.js", "browser": "gun.min.js", diff --git a/sea.js b/sea.js index d547db36..bcf328b9 100644 --- a/sea.js +++ b/sea.js @@ -384,7 +384,7 @@ var sha256hash = USE('./sha256'); SEA.sign = async (data, pair, cb) => { try { - if(data.slice + if(data && data.slice && 'SEA{' === data.slice(0,4) && '"m":' === data.slice(4,8)){ // TODO: This would prevent pair2 signing pair1's signature. @@ -398,8 +398,8 @@ const jwk = S.jwk(pub, priv) const msg = JSON.stringify(data) const hash = await sha256hash(msg) - const sig = await shim.subtle.importKey('jwk', jwk, S.ecdsa.pair, false, ['sign']) - .then((key) => shim.subtle.sign(S.ecdsa.sign, key, new Uint8Array(hash))) // privateKey scope doesn't leak out from here! + const sig = await (shim.ossl || shim.subtle).importKey('jwk', jwk, S.ecdsa.pair, false, ['sign']) + .then((key) => (shim.ossl || shim.subtle).sign(S.ecdsa.sign, key, new Uint8Array(hash))) // privateKey scope doesn't leak out from here! const r = 'SEA'+JSON.stringify({m: msg, s: shim.Buffer.from(sig, 'binary').toString('utf8')}); if(cb){ try{ cb(r) }catch(e){console.log(e)} } @@ -1475,6 +1475,7 @@ // if there is a request to read data from us, then... var soul = msg.get['#']; if(soul){ // for now, only allow direct IDs to be read. + if(soul !== 'string'){ return to.next(msg) } // do not handle lexical cursors. if('alias' === soul){ // Allow reading the list of usernames/aliases in the system? return to.next(msg); // yes. } else diff --git a/test/panic/holy-grail.js b/test/panic/holy-grail.js index 188d0499..d96dfa2a 100644 --- a/test/panic/holy-grail.js +++ b/test/panic/holy-grail.js @@ -45,7 +45,6 @@ describe("The Holy Grail Test!", function(){ }); it("GUN started!", function(){ - console.log("I must have screwed up my NPM installs, server won't start properly but Holy-Grail passes if I do it manually. Will fix later."); return server.run(function(test){ var env = test.props; test.async(); diff --git a/test/tmp/mitra/client_to_server.js b/test/tmp/mitra/client_to_server.js new file mode 100644 index 00000000..c6adacd3 --- /dev/null +++ b/test/tmp/mitra/client_to_server.js @@ -0,0 +1,6 @@ +process.env.GUN_ENV = "false"; +var Gun=require('gun') +var g = new Gun({peers: ['http://localhost:4246/gun'],localStorage: false}); +g.get("FOOxx").get("BARxx").once(data=>console.log("RCVD: (SHOULD NOT BE UNDEFINED!)", data)); + +console.log('now run ```var g = new Gun({peers: ["http://localhost:4246/gun"],localStorage: false}); g.get("FOOxx").get("BARxx").once(data=>console.log("RCVD:", data))```'); \ No newline at end of file diff --git a/test/tmp/mitra/gun_https2.js b/test/tmp/mitra/gun_https2.js new file mode 100644 index 00000000..b1497f81 --- /dev/null +++ b/test/tmp/mitra/gun_https2.js @@ -0,0 +1,103 @@ +//var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8080; + +const port = 4246; + +const https = require('https'); +const http = require('http'); +const fs = require('fs'); +process.env.GUN_ENV = "false"; +const Gun = require('gun'); +const path = require('path'); + +const usehttps = false; + +const options = + usehttps ? { + key: fs.readFileSync('/etc/letsencrypt/live/dweb.me/privkey.pem'), + cert: fs.readFileSync('/etc/letsencrypt/live/dweb.me/fullchain.pem'), + } : {}; +var h = usehttps ? https : http +//var server = h.createServer(options, (req, res) => { +var server = h.createServer((req, res) => { + if(Gun.serve(req, res)){ return } // filters gun requests! + res.writeHead(200); + res.end('go away - nothing for browsers here\n'); + + /* + fs.createReadStream(path.join(__dirname, req.url)) + .on('error',function(){ // static files! + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(fs.readFileSync(path.join(__dirname, 'index.html'))); // or default to index + }) + .pipe(res); // stream + */ +}); + + +//TODO-GUN put this into a seperate require +function hijack(cb) { + /* Intercept outgoing message and replace result with + result from cb({soul, key, msg, original}) + */ + + Gun.on('opt', function (root) { + console.log("GUN: Hikacking loading trap"); + if (root.once) { + return + } + root.on('out', function (msg) { + console.log("GUN: Hikacking starting outgoing message=", msg); + let to = this.to; + // TODO-GUN - this wont work when running locally in a script ONLY when running in server + if(msg['@'] && !msg.put) { + console.log("GUN: Hikacking outgoing message", msg); + setTimeout(function(){ // TODO-GUN its unclear why this timeout is here, other than for testing + let tmp = root.dup.s[msg['@']]; + let original = tmp && tmp.it && tmp.it.get; + console.log("GUN: Hikacking outgoing message original=", original); + if (original) { + let soul = original['#']; + let key = original['.']; + console.log("GUN.hijack: soul=",soul,"key=", key); + let res; + try { + //TODO - this res now has to be async + res = cb({soul, key, msg, original}); // Note response can be undefined if error + } catch(err) { + console.warn("Gun.hijack callback error",err); + res = undefined; + } + msg.put = { + [soul]: { + _: { + '#': soul, + '>': {[key]: Gun.state()} + }, + [key]: res // Note undefined should (hopefully) be a valid response + } }; + console.log("GUN.hijack updated msg =", msg); + // NOTE: this doesn't necessarily save it back to + // this peers GUN data, (I (Mitra) thinks that may depend on other processes and order of Gun.on) + } + to.next(msg); + }, 100); //Just for testing and note that its async + } else { + to.next(msg); // pass to next middleware + } + }); + this.to.next(root); // This is next for the Gun.on('opt'), not for the root.on('out') + }); +} +hijack(function({soul=undefined, key=undefined, msg=undefined, original=undefined}={}) { + console.log("GUN: hijack testing", soul, key, msg, original); + return ("GUN: This is a test result"); +}); + +var gun = new Gun({ + web: server +}); + + +server.listen(port); + +console.log(usehttps ? "HTTPS" : "HTTP", 'Server started on port ' + port + ' with /gun');