diff --git a/examples/admin/data.json b/examples/admin/data.json new file mode 100644 index 00000000..3adc81dc --- /dev/null +++ b/examples/admin/data.json @@ -0,0 +1,11 @@ +{ + "_":{ + "#":"yVbyf7BqlXVQQUOE5cw9rf8h", + ">":{ + "hello":1407328713707, + "from":1407328713707 + } + }, + "hello":"world", + "from":"Mark Nadal" +} \ No newline at end of file diff --git a/examples/admin/index.html b/examples/admin/index.html new file mode 100644 index 00000000..fb47a9ba --- /dev/null +++ b/examples/admin/index.html @@ -0,0 +1,83 @@ + + + + + + + + + + +

Admin Data Editor

+ + + + \ No newline at end of file diff --git a/examples/social/index.html b/examples/social/index.html index af895fa5..8d7ffcc7 100644 --- a/examples/social/index.html +++ b/examples/social/index.html @@ -12,7 +12,7 @@ - + + \ No newline at end of file diff --git a/examples/social/server.js b/examples/social/server.js index 6132c573..e72751b2 100644 --- a/examples/social/server.js +++ b/examples/social/server.js @@ -1,14 +1,14 @@ var fs = require('fs'); var http = require('http'); var qs = require('querystring'); -var sync = require('../../test/shotgun'); +var gun = require('../../test/shotgun'); http.route = function(url){ console.log(url); var path = __dirname + url; if(!url){ return http.route } - if(url === '/gun'){ - sync.server(req, res); + if(gun.server.regex.test(url)){ + return gun; } if(fs.existsSync(path)){ return ((path = require(path)) && path.server)? path : http.route; @@ -21,50 +21,34 @@ http.route = function(url){ http.route.server = function(req, res){ console.log("/ no route found"); } -http.cors = function(req, res){ - var headers = {}; - headers["Access-Control-Allow-Origin"] = "*"; - headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"; - headers["Access-Control-Allow-Credentials"] = false; - headers["Access-Control-Max-Age"] = 1000 * 60 * 60 * 24; - headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept"; - if(res){ - res.writeHead(200, headers); - if(req && req.method === 'OPTIONS'){ - res.end(); - return true; - } - } -} http.createServer(function(req, res){ console.log(req.headers); - console.log(req.method); - if(http.cors(req, res)){ return } - if(req.method == 'POST'){ - var body = {}; - body.length = 0; - body.data = new require('buffer').Buffer(''); - req.on('data', function(buffer){ - if(body.data.length >= body.length + buffer.length){ - buffer.copy(body.data, body.length); - } else { - body.data = Buffer.concat([body.data, buffer]); - } - body.length += buffer.length; - }); - req.on('end', function(x){ - body.text = body.data.toString('utf8'); - try{body.json = JSON.parse(body.text); - }catch(e){} - delete body.data; - req.body = body.json || body.text; - http.route(req.url).server(req, res); - }); - res.on('end', function(data){ - http.cors(null, res); - res.end(JSON.stringify(data)); - }); - } + console.log(req.method, req.url); + var body = {}; + body.length = 0; + body.data = new require('buffer').Buffer(''); + req.on('data', function(buffer){ + if(body.data.length >= body.length + buffer.length){ + buffer.copy(body.data, body.length); + } else { + body.data = Buffer.concat([body.data, buffer]); + } + body.length += buffer.length; + }); + req.on('end', function(x){ + body.text = body.data.toString('utf8'); + try{body.json = JSON.parse(body.text); + }catch(e){} + delete body.data; + req.body = body.json || body.text; + http.route(req.url).server(req, res); + }); + res.on('data', function(data){ + res.write(JSON.stringify(data) + '\n'); + }); + res.on('end', function(data){ + res.end(JSON.stringify(data)); + }); }).listen(8888); console.log("listening"); //process.on("uncaughtException", function(e){console.log('!!!!!!!!!!!!!!!!!!!!!!');console.log(e);console.log('!!!!!!!!!!!!!!!!!!!!!!')}); \ No newline at end of file diff --git a/examples/social/sign.js b/examples/social/sign.js index aad86780..3bf84701 100644 --- a/examples/social/sign.js +++ b/examples/social/sign.js @@ -7,7 +7,7 @@ sign.user.create = function(form, cb, shell){ // TODO: REDO THIS TO MATCH GUN if(err || !user){ return cb(err) } user = {key: user.key, salt: user.salt}; user.account = {email: form.email, registered: new Date().getTime()}; - gun.set(user).index('email/' + user.account.email); + gun.set(user).key('email/' + user.account.email); cb(null, user); }); }; @@ -22,20 +22,20 @@ gun.load('email/mark@accelsor.com') sendEmail("Mark wants to leanr how ot surf, and you are a friend of a friend"); }); */ - -/* -gun.load() - -here is a bunch of node indices -now for each combinatoric pair -find all possible paths between them -*/ sign.server = function(req, res){ - console.log("sign.server", req.body); + console.log("sign.server", req.headers, req.body); + res.emit('data', {ok: 1}); + res.emit('data', {ok: 2}); + res.emit('data', {ok: 3}); + res.emit('data', {ok: 4}); + setTimeout(function(){ + res.emit('end', {ok: 5}); + }, 5000); + return; if(!req.body || !req.body.email){ return res.emit('end', {err: "That email does not exist."}) } var user = gun.load('email/' + req.body.email, function(data){ // this callback is called the magazine, since it holds the clip - console.log("data from index", data); + console.log("data from key", data); if(!req.body.password){ return res.emit('end', {ok: 'sign in'}); } diff --git a/fixtures/network.js b/fixtures/network.js index 5b718ed3..f7b6eaf0 100644 --- a/fixtures/network.js +++ b/fixtures/network.js @@ -5,9 +5,24 @@ var users = require('./users.json').results , chance = new Chance(1234) , b , d = Date.now() - , num = 10000 + , num = 50 ; +users = users.map(function(user){ + user = user.user; + user._ = user._ || {}; + user._['#'] = user.sha1; + user.first = user.name.first; + user.last = user.name.last; + user.title = user.name.title; + delete user.name; + user.zip = user.location.zip; + user.street = user.location.street; + user.city = user.location.city; + user.state = user.location.state; + delete user.location; + return user; +}) users = chance.shuffle(users).slice(0, num); b = chance.shuffle(users.slice(0)); b.forEach(function (user, i) { @@ -15,6 +30,12 @@ b.forEach(function (user, i) { console.log((Date.now() - d) / 1000, i); d = Date.now(); } - user.friends = chance.shuffle(users).slice(chance.integer({ min: 20, max: 120 })); + user.friends = chance.shuffle(users).slice(chance.integer({ min: 20, max: (num < 120)? num : 120 })); }); + +var gun = require('../test/shotgun'); + +gun.set(b[0]); + +console.log(b[1], b[1].friends.length); console.log((Date.now() - d) / 1000, num); diff --git a/gate/redis.js b/gate/redis.js deleted file mode 100644 index eeaab5c1..00000000 --- a/gate/redis.js +++ /dev/null @@ -1,57 +0,0 @@ -module.exports = (function(){ - var r = {} - , fs = require('fs') - , child = require('child_process') - , check = (fs.existsSync||require('path').existsSync) - , install = 'redis-install' - , server = 'redis-server'; - r.redis = require('redis'); - r.client = r.redis.createClient(); - r.refis = require('fakeredis'); - r.clienf = r.refis.createClient(); - process.env[server] = process.env[server] || '/usr/local/bin/redis-server'; - r.client.on('error', function(e){ - // console.log("redis error", e); - if(!(/ECONNREFUSED/).test(e)){ return } - r.start(); - }); - r.client.set('_gun_redis_init_', r.key = Math.random().toString().slice(2)); - r.start = function(){ - if(process.env[install]){ - if(!check(process.env[server])){ - return r.deploy(r.start); - } - } - if(!check(process.env[server])){ - return; // never recover - } - if(process.env.gun_redis_lock){ return } - process.env.gun_redis_lock = process.pid; - console.log('gun', process.pid, 'starting redis'); - require('child_process').spawn(process.env[server]).on('exit',function(){ - if(process.env.gun_redis_lock == process.pid){ - process.env.gun_redis_lock = ''; // 0 and false don't work, cause they are cast to strings! - } - }); - r.client.get('_gun_redis_init_', function(e,r){ - console.log(">>>> BOOM <<<<", e, r); - }); - } - r.deploy = function(done){ - var path = process.env[install]; - if(!path){ return } - if(check(process.env[server])){ - done(process.env[server]); - } else { - child.exec('cd ' + path - + ' && ' + 'curl -O http://download.redis.io/redis-stable.tar.gz' - + ' && ' + 'tar xvzf redis-stable.tar.gz' - + ' && ' + 'cd redis-stable' - + ' && ' + 'make' - , function(e, r){ - done(process.env[server] = path + '/redis-stable/src/redis-server'); - }); - } - } - return r; -})(); \ No newline at end of file diff --git a/gate/s3.js b/gate/s3.js index 0b778125..c9701c3a 100644 --- a/gate/s3.js +++ b/gate/s3.js @@ -61,7 +61,7 @@ if(!a.fns.is(cb)){ return } try{ cb(e,d,t,m,r); }catch(e){ - console.log(e) + console.log(e); } }); s.batch = s.batch || {}; @@ -118,7 +118,7 @@ } this.S3().listObjects(m, function(e,r){ //console.log('list',e); - a.list.each((r||{}).Contents, function(v){console.log(v);}); + a.list.each((r||{}).Contents, function(v){console.log(v)}); //console.log('---end list---'); if(!a.fns.is(cb)) return; cb(e,r); @@ -126,4 +126,4 @@ return this; } return s3; -})(require('../gun2'), {}); \ No newline at end of file +})(require('../gun'), {}); \ No newline at end of file diff --git a/gate/sock.js b/gate/sock.js deleted file mode 100644 index 3bf7efb5..00000000 --- a/gate/sock.js +++ /dev/null @@ -1,27 +0,0 @@ -/* SockJS client, version 0.3.4, http://sockjs.org, MIT License - -Copyright (c) 2011-2012 VMware, Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -// JSON2 by Douglas Crockford (minified). -var JSON;JSON||(JSON={}),function(){function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c1?this._listeners[a]=d.slice(0,e).concat(d.slice(e+1)):delete this._listeners[a];return}return},d.prototype.dispatchEvent=function(a){var b=a.type,c=Array.prototype.slice.call(arguments,0);this["on"+b]&&this["on"+b].apply(this,c);if(this._listeners&&b in this._listeners)for(var d=0;d=3e3&&a<=4999},c.countRTO=function(a){var b;return a>100?b=3*a:b=a+200,b},c.log=function(){b.console&&console.log&&console.log.apply&&console.log.apply(console,arguments)},c.bind=function(a,b){return a.bind?a.bind(b):function(){return a.apply(b,arguments)}},c.flatUrl=function(a){return a.indexOf("?")===-1&&a.indexOf("#")===-1},c.amendUrl=function(b){var d=a.location;if(!b)throw new Error("Wrong url for SockJS");if(!c.flatUrl(b))throw new Error("Only basic urls are supported in SockJS");return b.indexOf("//")===0&&(b=d.protocol+b),b.indexOf("/")===0&&(b=d.protocol+"//"+d.host+b),b=b.replace(/[/]+$/,""),b},c.arrIndexOf=function(a,b){for(var c=0;c=0},c.delay=function(a,b){return typeof a=="function"&&(b=a,a=0),setTimeout(b,a)};var i=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,j={"\0":"\\u0000","\x01":"\\u0001","\x02":"\\u0002","\x03":"\\u0003","\x04":"\\u0004","\x05":"\\u0005","\x06":"\\u0006","\x07":"\\u0007","\b":"\\b","\t":"\\t","\n":"\\n","\x0b":"\\u000b","\f":"\\f","\r":"\\r","\x0e":"\\u000e","\x0f":"\\u000f","\x10":"\\u0010","\x11":"\\u0011","\x12":"\\u0012","\x13":"\\u0013","\x14":"\\u0014","\x15":"\\u0015","\x16":"\\u0016","\x17":"\\u0017","\x18":"\\u0018","\x19":"\\u0019","\x1a":"\\u001a","\x1b":"\\u001b","\x1c":"\\u001c","\x1d":"\\u001d","\x1e":"\\u001e","\x1f":"\\u001f",'"':'\\"',"\\":"\\\\","\x7f":"\\u007f","\x80":"\\u0080","\x81":"\\u0081","\x82":"\\u0082","\x83":"\\u0083","\x84":"\\u0084","\x85":"\\u0085","\x86":"\\u0086","\x87":"\\u0087","\x88":"\\u0088","\x89":"\\u0089","\x8a":"\\u008a","\x8b":"\\u008b","\x8c":"\\u008c","\x8d":"\\u008d","\x8e":"\\u008e","\x8f":"\\u008f","\x90":"\\u0090","\x91":"\\u0091","\x92":"\\u0092","\x93":"\\u0093","\x94":"\\u0094","\x95":"\\u0095","\x96":"\\u0096","\x97":"\\u0097","\x98":"\\u0098","\x99":"\\u0099","\x9a":"\\u009a","\x9b":"\\u009b","\x9c":"\\u009c","\x9d":"\\u009d","\x9e":"\\u009e","\x9f":"\\u009f","\xad":"\\u00ad","\u0600":"\\u0600","\u0601":"\\u0601","\u0602":"\\u0602","\u0603":"\\u0603","\u0604":"\\u0604","\u070f":"\\u070f","\u17b4":"\\u17b4","\u17b5":"\\u17b5","\u200c":"\\u200c","\u200d":"\\u200d","\u200e":"\\u200e","\u200f":"\\u200f","\u2028":"\\u2028","\u2029":"\\u2029","\u202a":"\\u202a","\u202b":"\\u202b","\u202c":"\\u202c","\u202d":"\\u202d","\u202e":"\\u202e","\u202f":"\\u202f","\u2060":"\\u2060","\u2061":"\\u2061","\u2062":"\\u2062","\u2063":"\\u2063","\u2064":"\\u2064","\u2065":"\\u2065","\u2066":"\\u2066","\u2067":"\\u2067","\u2068":"\\u2068","\u2069":"\\u2069","\u206a":"\\u206a","\u206b":"\\u206b","\u206c":"\\u206c","\u206d":"\\u206d","\u206e":"\\u206e","\u206f":"\\u206f","\ufeff":"\\ufeff","\ufff0":"\\ufff0","\ufff1":"\\ufff1","\ufff2":"\\ufff2","\ufff3":"\\ufff3","\ufff4":"\\ufff4","\ufff5":"\\ufff5","\ufff6":"\\ufff6","\ufff7":"\\ufff7","\ufff8":"\\ufff8","\ufff9":"\\ufff9","\ufffa":"\\ufffa","\ufffb":"\\ufffb","\ufffc":"\\ufffc","\ufffd":"\\ufffd","\ufffe":"\\ufffe","\uffff":"\\uffff"},k=/[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0f80-\u0f83\u0f93\u0f9d\u0fa2\u0fa7\u0fac\u0fb9\u1939-\u193a\u1a17\u1b6b\u1cda-\u1cdb\u1dc0-\u1dcf\u1dfc\u1dfe\u1f71\u1f73\u1f75\u1f77\u1f79\u1f7b\u1f7d\u1fbb\u1fbe\u1fc9\u1fcb\u1fd3\u1fdb\u1fe3\u1feb\u1fee-\u1fef\u1ff9\u1ffb\u1ffd\u2000-\u2001\u20d0-\u20d1\u20d4-\u20d7\u20e7-\u20e9\u2126\u212a-\u212b\u2329-\u232a\u2adc\u302b-\u302c\uaab2-\uaab3\uf900-\ufa0d\ufa10\ufa12\ufa15-\ufa1e\ufa20\ufa22\ufa25-\ufa26\ufa2a-\ufa2d\ufa30-\ufa6d\ufa70-\ufad9\ufb1d\ufb1f\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40-\ufb41\ufb43-\ufb44\ufb46-\ufb4e\ufff0-\uffff]/g,l,m=JSON&&JSON.stringify||function(a){return i.lastIndex=0,i.test(a)&&(a=a.replace(i,function(a){return j[a]})),'"'+a+'"'},n=function(a){var b,c={},d=[];for(b=0;b<65536;b++)d.push(String.fromCharCode(b));return a.lastIndex=0,d.join("").replace(a,function(a){return c[a]="\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4),""}),a.lastIndex=0,c};c.quote=function(a){var b=m(a);return k.lastIndex=0,k.test(b)?(l||(l=n(k)),b.replace(k,function(a){return l[a]})):b};var o=["websocket","xdr-streaming","xhr-streaming","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"];c.probeProtocols=function(){var a={};for(var b=0;b0&&h(a)};return c.websocket!==!1&&h(["websocket"]),d["xhr-streaming"]&&!c.null_origin?e.push("xhr-streaming"):d["xdr-streaming"]&&!c.cookie_needed&&!c.null_origin?e.push("xdr-streaming"):h(["iframe-eventsource","iframe-htmlfile"]),d["xhr-polling"]&&!c.null_origin?e.push("xhr-polling"):d["xdr-polling"]&&!c.cookie_needed&&!c.null_origin?e.push("xdr-polling"):h(["iframe-xhr-polling","jsonp-polling"]),e};var p="_sockjs_global";c.createHook=function(){var a="a"+c.random_string(8);if(!(p in b)){var d={};b[p]=function(a){return a in d||(d[a]={id:a,del:function(){delete d[a]}}),d[a]}}return b[p](a)},c.attachMessage=function(a){c.attachEvent("message",a)},c.attachEvent=function(c,d){typeof b.addEventListener!="undefined"?b.addEventListener(c,d,!1):(a.attachEvent("on"+c,d),b.attachEvent("on"+c,d))},c.detachMessage=function(a){c.detachEvent("message",a)},c.detachEvent=function(c,d){typeof b.addEventListener!="undefined"?b.removeEventListener(c,d,!1):(a.detachEvent("on"+c,d),b.detachEvent("on"+c,d))};var q={},r=!1,s=function(){for(var a in q)q[a](),delete q[a]},t=function(){if(r)return;r=!0,s()};c.attachEvent("unload",t),c.unload_add=function(a){var b=c.random_string(8);return q[b]=a,r&&c.delay(s),b},c.unload_del=function(a){a in q&&delete q[a]},c.createIframe=function(b,d){var e=a.createElement("iframe"),f,g,h=function(){clearTimeout(f);try{e.onload=null}catch(a){}e.onerror=null},i=function(){e&&(h(),setTimeout(function(){e&&e.parentNode.removeChild(e),e=null},0),c.unload_del(g))},j=function(a){e&&(i(),d(a))},k=function(a,b){try{e&&e.contentWindow&&e.contentWindow.postMessage(a,b)}catch(c){}};return e.src=b,e.style.display="none",e.style.position="absolute",e.onerror=function(){j("onerror")},e.onload=function(){clearTimeout(f),f=setTimeout(function(){j("onload timeout")},2e3)},a.body.appendChild(e),f=setTimeout(function(){j("timeout")},15e3),g=c.unload_add(i),{post:k,cleanup:i,loaded:h}},c.createHtmlfile=function(a,d){var e=new ActiveXObject("htmlfile"),f,g,i,j=function(){clearTimeout(f)},k=function(){e&&(j(),c.unload_del(g),i.parentNode.removeChild(i),i=e=null,CollectGarbage())},l=function(a){e&&(k(),d(a))},m=function(a,b){try{i&&i.contentWindow&&i.contentWindow.postMessage(a,b)}catch(c){}};e.open(),e.write('