From fd6752cbd6641c604205f21932935e0d2854086a Mon Sep 17 00:00:00 2001 From: Pavel Diatchenko Date: Mon, 14 Sep 2020 16:06:49 +1200 Subject: [PATCH 01/15] Added browser open utility method. --- test/panic/load.js | 4 ++-- test/panic/util/open.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/panic/util/open.js diff --git a/test/panic/load.js b/test/panic/load.js index 06cde4e7..a1f9f44e 100644 --- a/test/panic/load.js +++ b/test/panic/load.js @@ -105,8 +105,8 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" it(config.browsers +" browser(s) have joined!", function(){ // Okay! Cool. Now we can move on to the next step... - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); - // Which is to manually open up a bunch of browser tabs + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); + // Which is to automatically or manually open up a bunch of browser tabs // and connect to the PANIC server in the same way // the NodeJS servers did. diff --git a/test/panic/util/open.js b/test/panic/util/open.js new file mode 100644 index 00000000..def517a8 --- /dev/null +++ b/test/panic/util/open.js @@ -0,0 +1,34 @@ +module.exports = { + web: (count, url, options) => { + if (typeof count === 'string') { + options = url; + url = count; + count = 1; + } + if (!url || typeof url !== 'string') { + throw new Error('Invalid URL'); + } + try { + require('puppeteer').launch(options).then(browser => { + Array(count).fill(0).forEach((x, i) => { + console.log('Opening browser page ' + i + ' with puppeteer...'); + browser.newPage().then(page => { + page.on('console', msg => { + if (msg.text() === 'JSHandle@object') { + // FIXME: Avoid text comparison + return; + } + console.log(`${i} [${msg.type()}]: ${msg.text()}`); + }); + return page.goto(url); + }).then(() => { + console.log('Browser page ' + i + ' open'); + }); + }); + }); + } catch (err) { + console.log("PLEASE OPEN "+ url +" IN "+ count +" BROWSER(S)!"); + console.warn('Consider installing puppeteer to automate browser management (npm i -g puppeteer && npm link puppeteer)'); + } + } +} \ No newline at end of file From fee7473ccbb98c2279d6967da21943512693a192 Mon Sep 17 00:00:00 2001 From: Pavel Diatchenko Date: Mon, 14 Sep 2020 16:12:50 +1200 Subject: [PATCH 02/15] Use puppeteer if available. --- test/axe/holy-grail.js | 2 +- test/panic/1putackget.js | 2 +- test/panic/2getget.js | 2 +- test/panic/3puts.js | 2 +- test/panic/4dht.js | 2 +- test/panic/b2s2s2b.js | 2 +- test/panic/bulkimport.js | 2 +- test/panic/holy-grail.js | 2 +- test/panic/livestream.js | 2 +- test/panic/no-override.js | 2 +- test/panic/on-recovery.js | 2 +- test/panic/radisk.js | 2 +- test/panic/set.js | 2 +- test/panic/thread.js | 2 +- test/panic/users.js | 2 +- test/panic/who.js | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/axe/holy-grail.js b/test/axe/holy-grail.js index 93ba4bfc..faddaa60 100644 --- a/test/axe/holy-grail.js +++ b/test/axe/holy-grail.js @@ -80,7 +80,7 @@ describe("The Holy Grail AXE Test!", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/1putackget.js b/test/panic/1putackget.js index 8e3d025b..72606f39 100644 --- a/test/panic/1putackget.js +++ b/test/panic/1putackget.js @@ -82,7 +82,7 @@ describe("Put ACK", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/2getget.js b/test/panic/2getget.js index 65bd2920..59e7be0e 100644 --- a/test/panic/2getget.js +++ b/test/panic/2getget.js @@ -83,7 +83,7 @@ describe("Put ACK", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/3puts.js b/test/panic/3puts.js index 734dd901..c64a759c 100644 --- a/test/panic/3puts.js +++ b/test/panic/3puts.js @@ -82,7 +82,7 @@ describe("Put ACK", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/4dht.js b/test/panic/4dht.js index 8ba31eab..1689aed3 100644 --- a/test/panic/4dht.js +++ b/test/panic/4dht.js @@ -123,7 +123,7 @@ describe("Put ACK", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/b2s2s2b.js b/test/panic/b2s2s2b.js index 1b30bcda..e9e0fc3a 100644 --- a/test/panic/b2s2s2b.js +++ b/test/panic/b2s2s2b.js @@ -73,7 +73,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); browsers.atLeast(1).then(function(){ browsers.run(function(test){ var env = test.props; diff --git a/test/panic/bulkimport.js b/test/panic/bulkimport.js index 734dd901..c64a759c 100644 --- a/test/panic/bulkimport.js +++ b/test/panic/bulkimport.js @@ -82,7 +82,7 @@ describe("Put ACK", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/holy-grail.js b/test/panic/holy-grail.js index 0f865095..26ab13e4 100644 --- a/test/panic/holy-grail.js +++ b/test/panic/holy-grail.js @@ -65,7 +65,7 @@ describe("The Holy Grail Test!", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/livestream.js b/test/panic/livestream.js index f9fe1c95..de71545e 100644 --- a/test/panic/livestream.js +++ b/test/panic/livestream.js @@ -82,7 +82,7 @@ describe("Broadcast Video", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/no-override.js b/test/panic/no-override.js index 8f6d9875..8def30d3 100644 --- a/test/panic/no-override.js +++ b/test/panic/no-override.js @@ -75,7 +75,7 @@ describe("No Empty Object on .Once", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/on-recovery.js b/test/panic/on-recovery.js index 9c97a943..df668823 100644 --- a/test/panic/on-recovery.js +++ b/test/panic/on-recovery.js @@ -72,7 +72,7 @@ describe("gun.on should receive updates after crashed relay peer comes back onli }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/radisk.js b/test/panic/radisk.js index 3470bf78..a86b2a0f 100644 --- a/test/panic/radisk.js +++ b/test/panic/radisk.js @@ -74,7 +74,7 @@ describe("Make sure the Radix Storage Engine (RAD) works.", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/set.js b/test/panic/set.js index 53a8adb4..9dc22bb6 100644 --- a/test/panic/set.js +++ b/test/panic/set.js @@ -65,7 +65,7 @@ describe("Make sure SEA syncs correctly", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/thread.js b/test/panic/thread.js index e8e67d54..c72585fd 100644 --- a/test/panic/thread.js +++ b/test/panic/thread.js @@ -64,7 +64,7 @@ describe("Private Message Threading", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/users.js b/test/panic/users.js index 1f77df9e..ed7287e6 100644 --- a/test/panic/users.js +++ b/test/panic/users.js @@ -65,7 +65,7 @@ describe("End-to-End Encryption on User Accounts", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); diff --git a/test/panic/who.js b/test/panic/who.js index 1deaa5d5..f9f2c971 100644 --- a/test/panic/who.js +++ b/test/panic/who.js @@ -65,7 +65,7 @@ describe("Make sure SEA syncs correctly", function(){ }); it(config.browsers +" browser(s) have joined!", function(){ - console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + require('./util/open').web(config.browsers, "http://"+ config.IP +":"+ config.port); return browsers.atLeast(config.browsers); }); From 33b29f4ee6df065618fec2c35a2b719281994b7a Mon Sep 17 00:00:00 2001 From: Pavel Diatchenko Date: Thu, 17 Sep 2020 12:33:04 +1200 Subject: [PATCH 03/15] Added puppeteer cleanup method. --- test/panic/load.js | 2 +- test/panic/util/open.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/panic/load.js b/test/panic/load.js index a1f9f44e..799c34b8 100644 --- a/test/panic/load.js +++ b/test/panic/load.js @@ -249,7 +249,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" after("Everything shut down.", function(){ // which is to shut down all the browsers. - browsers.run(function(){ + require('./util/open').cleanup() || browsers.run(function(){ setTimeout(function(){ location.reload(); }, 15 * 1000); diff --git a/test/panic/util/open.js b/test/panic/util/open.js index def517a8..76c87fa0 100644 --- a/test/panic/util/open.js +++ b/test/panic/util/open.js @@ -1,3 +1,5 @@ +let _browsers = []; + module.exports = { web: (count, url, options) => { if (typeof count === 'string') { @@ -10,6 +12,7 @@ module.exports = { } try { require('puppeteer').launch(options).then(browser => { + _browsers.push(browser); Array(count).fill(0).forEach((x, i) => { console.log('Opening browser page ' + i + ' with puppeteer...'); browser.newPage().then(page => { @@ -30,5 +33,13 @@ module.exports = { console.log("PLEASE OPEN "+ url +" IN "+ count +" BROWSER(S)!"); console.warn('Consider installing puppeteer to automate browser management (npm i -g puppeteer && npm link puppeteer)'); } - } -} \ No newline at end of file + }, + cleanup: () => { + if (_browsers.length === 0) { + return undefined; + } + console.log('Closing all puppeteer browsers...'); + return Promise.all(_browsers.map(b => b.close())) + .then(() => console.log('Closed all puppeteer browsers')); + } +} From a607bdd004c9cf274502cd4c935ad10fcde7f517 Mon Sep 17 00:00:00 2001 From: Pavel Diatchenko Date: Thu, 17 Sep 2020 12:37:47 +1200 Subject: [PATCH 04/15] Cleanup puppeteer on exit. --- test/axe/holy-grail.js | 6 +----- test/panic/1putackget.js | 6 +----- test/panic/2getget.js | 6 +----- test/panic/3puts.js | 6 +----- test/panic/4dht.js | 6 +----- test/panic/b2s2s2b.js | 6 +----- test/panic/bulkimport.js | 6 +----- test/panic/holy-grail.js | 6 +----- test/panic/livestream.js | 6 +----- test/panic/no-override.js | 6 +----- test/panic/on-recovery.js | 2 +- test/panic/radisk.js | 6 +----- test/panic/scale.js | 6 +----- test/panic/set.js | 6 +----- test/panic/speak.js | 6 +----- test/panic/thread.js | 6 +----- test/panic/users.js | 6 +----- test/panic/who.js | 6 +----- 18 files changed, 18 insertions(+), 86 deletions(-) diff --git a/test/axe/holy-grail.js b/test/axe/holy-grail.js index faddaa60..a77dc946 100644 --- a/test/axe/holy-grail.js +++ b/test/axe/holy-grail.js @@ -413,11 +413,7 @@ describe("The Holy Grail AXE Test!", function(){ },1000); }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/1putackget.js b/test/panic/1putackget.js index 72606f39..250e15b9 100644 --- a/test/panic/1putackget.js +++ b/test/panic/1putackget.js @@ -188,11 +188,7 @@ describe("Put ACK", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/2getget.js b/test/panic/2getget.js index 59e7be0e..ed6de48e 100644 --- a/test/panic/2getget.js +++ b/test/panic/2getget.js @@ -153,11 +153,7 @@ describe("Put ACK", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/3puts.js b/test/panic/3puts.js index c64a759c..bbd313c2 100644 --- a/test/panic/3puts.js +++ b/test/panic/3puts.js @@ -125,11 +125,7 @@ describe("Put ACK", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/4dht.js b/test/panic/4dht.js index 1689aed3..78057fd8 100644 --- a/test/panic/4dht.js +++ b/test/panic/4dht.js @@ -179,11 +179,7 @@ describe("Put ACK", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/b2s2s2b.js b/test/panic/b2s2s2b.js index e9e0fc3a..f9271aba 100644 --- a/test/panic/b2s2s2b.js +++ b/test/panic/b2s2s2b.js @@ -166,11 +166,7 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/bulkimport.js b/test/panic/bulkimport.js index c64a759c..bbd313c2 100644 --- a/test/panic/bulkimport.js +++ b/test/panic/bulkimport.js @@ -125,11 +125,7 @@ describe("Put ACK", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/holy-grail.js b/test/panic/holy-grail.js index 26ab13e4..ec9f911c 100644 --- a/test/panic/holy-grail.js +++ b/test/panic/holy-grail.js @@ -267,11 +267,7 @@ describe("The Holy Grail Test!", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/livestream.js b/test/panic/livestream.js index de71545e..6120f369 100644 --- a/test/panic/livestream.js +++ b/test/panic/livestream.js @@ -180,11 +180,7 @@ describe("Broadcast Video", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/no-override.js b/test/panic/no-override.js index 8def30d3..a1e01be5 100644 --- a/test/panic/no-override.js +++ b/test/panic/no-override.js @@ -118,11 +118,7 @@ describe("No Empty Object on .Once", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/on-recovery.js b/test/panic/on-recovery.js index df668823..4fc6ffe6 100644 --- a/test/panic/on-recovery.js +++ b/test/panic/on-recovery.js @@ -217,7 +217,7 @@ describe("gun.on should receive updates after crashed relay peer comes back onli }); after("Everything shut down.", function(){ - return bob.run(function(){ + return require('./util/open').cleanup() || bob.run(function(){ process.exit(); }); }); diff --git a/test/panic/radisk.js b/test/panic/radisk.js index a86b2a0f..cab31467 100644 --- a/test/panic/radisk.js +++ b/test/panic/radisk.js @@ -275,11 +275,7 @@ describe("Make sure the Radix Storage Engine (RAD) works.", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/scale.js b/test/panic/scale.js index ce96e041..33ed3d9d 100644 --- a/test/panic/scale.js +++ b/test/panic/scale.js @@ -223,11 +223,7 @@ describe("Stress test GUN with SEA users causing PANIC!", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/set.js b/test/panic/set.js index 9dc22bb6..d72db6b6 100644 --- a/test/panic/set.js +++ b/test/panic/set.js @@ -188,11 +188,7 @@ describe("Make sure SEA syncs correctly", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/speak.js b/test/panic/speak.js index bea9d3d1..94a7bde5 100644 --- a/test/panic/speak.js +++ b/test/panic/speak.js @@ -310,11 +310,7 @@ describe("Stress test GUN with SEA users causing PANIC!", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/thread.js b/test/panic/thread.js index c72585fd..01148578 100644 --- a/test/panic/thread.js +++ b/test/panic/thread.js @@ -218,11 +218,7 @@ describe("Private Message Threading", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/users.js b/test/panic/users.js index ed7287e6..b08b9bfe 100644 --- a/test/panic/users.js +++ b/test/panic/users.js @@ -253,11 +253,7 @@ describe("End-to-End Encryption on User Accounts", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); diff --git a/test/panic/who.js b/test/panic/who.js index f9f2c971..54a0e6f9 100644 --- a/test/panic/who.js +++ b/test/panic/who.js @@ -233,11 +233,7 @@ describe("Make sure SEA syncs correctly", function(){ }); after("Everything shut down.", function(){ - browsers.run(function(){ - //location.reload(); - //setTimeout(function(){ - //}, 15 * 1000); - }); + require('./util/open').cleanup(); return servers.run(function(){ process.exit(); }); From 811a1b89639b04a697e6ce89af6f136467a16b68 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Mon, 14 Sep 2020 17:24:26 +0300 Subject: [PATCH 05/15] infinite scrolling example, panic test wip --- .../infinite-scroll/InfiniteScrollWindow.js | 113 ++++++++ examples/infinite-scroll/index.html | 28 ++ examples/infinite-scroll/index.js | 164 +++++++++++ examples/infinite-scroll/style.css | 77 ++++++ test/panic/infinite-scroll/index.html | 30 +++ test/panic/infinite-scroll/index.js | 255 ++++++++++++++++++ 6 files changed, 667 insertions(+) create mode 100644 examples/infinite-scroll/InfiniteScrollWindow.js create mode 100644 examples/infinite-scroll/index.html create mode 100644 examples/infinite-scroll/index.js create mode 100644 examples/infinite-scroll/style.css create mode 100644 test/panic/infinite-scroll/index.html create mode 100644 test/panic/infinite-scroll/index.js diff --git a/examples/infinite-scroll/InfiniteScrollWindow.js b/examples/infinite-scroll/InfiniteScrollWindow.js new file mode 100644 index 00000000..54dc9bee --- /dev/null +++ b/examples/infinite-scroll/InfiniteScrollWindow.js @@ -0,0 +1,113 @@ +const DEFAULT_OPTIONS = { + size: 20, + stickTo: 'top', +}; + +class InfiniteScrollWindow { + constructor(gunNode, opts = {}) { + this.opts = Object.assign(DEFAULT_OPTIONS, opts); + this.elements = new Map(); + this.node = gunNode; + this.center = this.opts.startAt; + this.updateListeners(); + } + + updateListeners() { + this.upSubscription && this.upSubscription.off(); + this.downSubscription && this.downSubscription.off(); + if (this.center) { + this.node.get({ '.': { '>': this.center, '<': '\uffff' }}).map().on((val, key, a, eve) => { + this.upSubscription = eve; + this._addElement(key, val); + }); + this.node.get({ '.': { '<': this.center, '>' : '', '-': true }}).map().on((val, key, a, eve) => { + this.downSubscription = eve; + this._addElement(key, val); + }); + } else { + this.node.get({ '.': { '<': '\uffff', '>': '', '-': this.opts.stickTo === 'top' }}).map().on((val, key, a, eve) => { + this.upSubscription = eve; + this._addElement(key, val); + }); + } + } + + _getSortedKeys() { + this.sortedKeys = this.sortedKeys || [...this.elements.keys()].sort(); + return this.sortedKeys; + } + + _upOrDown(n, up) { + this.opts.stickTo = null; + const keys = this._getSortedKeys(); + n = n || (keys.length / 2); + n = up ? n : -n; + const half = Math.floor(keys.length / 2); + const newMiddleIndex = Math.max(Math.min(half + n, keys.length - 1), 0); + if (this.center !== keys[newMiddleIndex]) { + this.center = keys[newMiddleIndex]; + this.updateListeners(); + } + return this.center; + } + + up(n) { + return this._upOrDown(n, true); + } + + down(n) { + return this._upOrDown(n, false); + } + + _topOrBottom(top) { + this.opts.stickTo = top ? 'top' : 'bottom'; + this.center = null; + this.updateListeners(); + } + + top() { + this._topOrBottom(true); + } + + bottom() { + this._topOrBottom(false); + } + + _addElement(key, val) { + if (!val || this.elements.has(key)) return; + const add = () => { + this.elements.set(key, val); + this.sortedKeys = [...this.elements.keys()].sort(); + const sortedElements = this.sortedKeys.map(k => this.elements.get(k)); + this.opts.onChange && this.opts.onChange(sortedElements); + }; + const keys = this._getSortedKeys(); + if (keys.length < this.opts.size) { + add(); + } else { + if (this.opts.stickTo === 'top' && key > keys[0]) { + this.elements.delete(keys[0]); + add(); + } else if (this.opts.stickTo === 'bottom' && key < keys[keys.length - 1]) { + this.elements.delete(keys[keys.length - 1]); + add(); + } else if (this.center) { + if (keys.indexOf(this.center) < (keys.length / 2)) { + if (key < keys[keys.length - 1]) { + this.elements.delete(keys[keys.length - 1]); + add(); + } + } else { + if (key > keys[0]) { + delete this.elements.delete(keys[0]); + add(); + } + } + } + } + } + + getElements() { + return this.elements; + } +} diff --git a/examples/infinite-scroll/index.html b/examples/infinite-scroll/index.html new file mode 100644 index 00000000..0d6d0f49 --- /dev/null +++ b/examples/infinite-scroll/index.html @@ -0,0 +1,28 @@ + + + Infinite scroll example + + + + + + + + +
+
+ + +
+
+ +
+
+ +
+ + + + + + diff --git a/examples/infinite-scroll/index.js b/examples/infinite-scroll/index.js new file mode 100644 index 00000000..c7c4579a --- /dev/null +++ b/examples/infinite-scroll/index.js @@ -0,0 +1,164 @@ +const gun = new Gun(); + +const size = 20; +const gunNode = gun.get('posts'); + +function debounce(func, wait, immediate) { + var timeout; + return function() { + var context = this, args = arguments; + var later = function() { + timeout = null; + if (!immediate) func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; +}; + +let topSentinelPreviousY = 0; +let topSentinelPreviousRatio = 0; +let bottomSentinelPreviousY = 0; +let bottomSentinelPreviousRatio = 0; +let previousUpIndex = previousDownIndex = -1; + +const render = elements => { + const t = new Date(); + elements.reverse().forEach((data, j) => { + var date = new Date(data.date); + $('#date' + j).text(date.toLocaleDateString() + ' ' + date.toLocaleTimeString()); + $('#text' + j).text(data.text); + $('#img' + j).attr('src', _getCatImg(date.getTime())); + $('#post' + j).css({visibility: 'visible'}); + }); + console.log('rendering took', new Date().getTime() - t.getTime(), 'ms'); +}; + +const onChange = debounce(render, 20); + +const scroller = new InfiniteScrollWindow(gunNode, {size, stickTo: 'top', onChange}); + +const initList = () => { + for (var n = 0; n < size; n++) { + var el = $("
").addClass('post').attr('id', 'post' + n).css({visibility: 'hidden'}); + el.append($('').attr('id', 'date' + n)); + el.append($('').attr('id', 'text' + n)); + el.append($('').attr('id', 'img' + n).attr('height', 100).attr('width', 100)); + $('#container').append(el); + } +} + +const _getCatImg = (n) => { + const url = "https://source.unsplash.com/collection/139386/100x100/?sig="; + return url + n % 999999; +}; + +const getNumFromStyle = numStr => Number(numStr.substring(0, numStr.length - 2)); + +const adjustPaddings = isScrollDown => { + const container = document.getElementById("container"); + const currentPaddingTop = getNumFromStyle(container.style.paddingTop); + const currentPaddingBottom = getNumFromStyle(container.style.paddingBottom); + const remPaddingsVal = 198 * (size / 2); // TODO: calculate element heights + if (isScrollDown) { + container.style.paddingTop = currentPaddingTop + remPaddingsVal + "px"; + container.style.paddingBottom = currentPaddingBottom === 0 ? "0px" : currentPaddingBottom - remPaddingsVal + "px"; + console.log(container.style.paddingTop, container.style.paddingBottom); + + } else { + container.style.paddingBottom = currentPaddingBottom + remPaddingsVal + "px"; + container.style.paddingTop = currentPaddingTop === 0 ? "0px" : currentPaddingTop - remPaddingsVal + "px"; + console.log(container.style.paddingTop, container.style.paddingBottom); + } +} + +const topSentCallback = entry => { + const container = document.getElementById("container"); + + const currentY = entry.boundingClientRect.top; + const currentRatio = entry.intersectionRatio; + const isIntersecting = entry.isIntersecting; + + // conditional check for Scrolling up + if ( + currentY > topSentinelPreviousY && + isIntersecting && + currentRatio >= topSentinelPreviousRatio && + scroller.center !== previousUpIndex && // stop if no new results were received + scroller.opts.stickTo !== 'top' + ) { + previousUpIndex = scroller.center; + adjustPaddings(false); // TODO: if top margin 0, increase it + scroller.up(size / 2); + } + topSentinelPreviousY = currentY; + topSentinelPreviousRatio = currentRatio; +} + +const botSentCallback = entry => { + const currentY = entry.boundingClientRect.top; + const currentRatio = entry.intersectionRatio; + const isIntersecting = entry.isIntersecting; + + // conditional check for Scrolling down + if ( + currentY < bottomSentinelPreviousY && + currentRatio > bottomSentinelPreviousRatio && + isIntersecting && + scroller.center !== previousDownIndex && // stop if no new results were received + scroller.opts.stickTo !== 'bottom' + ) { + previousDownIndex = scroller.center; + adjustPaddings(true); + scroller.down(size / 2); + } + bottomSentinelPreviousY = currentY; + bottomSentinelPreviousRatio = currentRatio; +} + +const initIntersectionObserver = () => { + const options = { + //rootMargin: '190px', + } + + const callback = entries => { + entries.forEach(entry => { + if (entry.target.id === 'post0') { + topSentCallback(entry); + } else if (entry.target.id === `post${size - 1}`) { + botSentCallback(entry); + } + }); + } + + var observer = new IntersectionObserver(callback, options); + observer.observe(document.querySelector("#post0")); + observer.observe(document.querySelector(`#post${size - 1}`)); +} + +initList(size); +initIntersectionObserver(); + +$('#top').click(() => { + scroller.top(); + $('#container').css({'padding-top': 0, 'padding-bottom': 0}); + $(document.body).animate({ scrollTop: 0 }, 500); +}); +$('#bottom').click(() => { + scroller.bottom(); + $('#container').css({'padding-top': 0, 'padding-bottom': 0}); + $(document.body).animate({ scrollTop: $("#container").height() }, 500); +}); + +$('#generate').submit(e => { + e.preventDefault(); + const day = 24 * 60 * 60 * 1000; + const year = 365 * day; + const n = Number($('#number').val()); + for (let i = 0; i < n; i++) { + const d = new Date(40 * year + i * day).toISOString(); + gunNode.get(d).put({text: 'Hello world!', date: d}); + } +}); diff --git a/examples/infinite-scroll/style.css b/examples/infinite-scroll/style.css new file mode 100644 index 00000000..66513e89 --- /dev/null +++ b/examples/infinite-scroll/style.css @@ -0,0 +1,77 @@ +html, body { + margin: 0; + padding: 0; +} + +body { + padding-top: 65px; +} + +header { + background: rgba(255,255,255,0.75); + padding: 15px; +} + +header { + position: fixed; + top: 0; left: 0; right: 0; +} + +#bottom { + position: fixed; + right: 15px; + bottom: 15px; +} + +input { + border: 0; + background-color: #efefef; + padding: 15px; + border-radius: 50px; +} + +button { + background-color: #4a4f9d; + border: 0; + padding: 15px; + color: white; + border-radius: 50px; +} + +input, button { + outline: none; + opacity: 1; +} + +input:focus, button:focus, button:hover { + opacity: 0.8; +} + +button:hover { + cursor: pointer; +} + +#top-buttons, form { + display: inline-block; + margin: 0; +} + +#top-buttons { + float: right; +} + +.post { + margin: 15px; + padding: 15px; + background-color: #9de1fe; + border-radius: 5px; +} + +.post b { + margin-right: 5; +} + +.post img { + display: block; + margin: 10px 0; +} diff --git a/test/panic/infinite-scroll/index.html b/test/panic/infinite-scroll/index.html new file mode 100644 index 00000000..b7da61fd --- /dev/null +++ b/test/panic/infinite-scroll/index.html @@ -0,0 +1,30 @@ + + + Infinite scroll example + + + + + + + + + + +
+
+ + +
+
+ +
+
+ +
+ + + + + + diff --git a/test/panic/infinite-scroll/index.js b/test/panic/infinite-scroll/index.js new file mode 100644 index 00000000..a416436a --- /dev/null +++ b/test/panic/infinite-scroll/index.js @@ -0,0 +1,255 @@ +var config = { + IP: require('ip').address(), + port: 8765, + servers: 1, + browsers: 1, + each: 1500, + wait: 1, + route: { + '/': __dirname + '/index.html', + '/scroller.js': __dirname + '/../../../examples/infinite-scroll/scroller.js', + '/gun.js': __dirname + '/../../../gun.js', + '/jquery.js': __dirname + '/../../../examples/jquery.js' + } +} + +/* + Welcome, person! + You have found the test that causes gun to PANIC with load! + Above are options to configure, the only ones useful are: + - browsers // number of browsers you want to load test across. + - each // the number of messages each browser should sync. + This test is less than 200 lines of code (without comments)! + However, if you aren't familiar with PANIC - you are in for a surprise! + I'm Plublious, and I shall be your guide! +*/ + +// First we need to create a PANIC server. +// Each device/browser in the distributed system we are testing connects to it. +// It then coordinates these clients to cause chaos in the distributed system. +// Cool huh? +var panic = require('panic-server'); +panic.server().on('request', function(req, res){ // Static server + config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res); +}).listen(config.port); // Start panic server. + +// In order to tell the clients what to do, +// We need a way to reference all of them. +var clients = panic.clients; + +// Some of the clients may be NodeJS servers on different machines. +// PANIC manager is a nifty tool that lets us remotely spawn them. +var manager = require('panic-manager')(); +manager.start({ + clients: Array(config.servers).fill().map(function(u, i){ // Create a bunch of servers. + return { + type: 'node', + port: config.port + (i + 1) // They'll need unique ports to start their servers on, if we run the test on 1 machine. + } + }), + panic: 'http://' + config.IP + ':' + config.port // Auto-connect to our panic server. +}); + +// Now lets divide our clients into "servers" and "browsers". +var servers = clients.filter('Node.js'); +var browsers = clients.excluding(servers); + +// Sweet! Now we can start the tests. +// PANIC works with Mocha and other testing libraries! +// So it is easy to use PANIC. + +describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" server(s)!", function(){ + + // We'll have to manually launch the browsers, + // So lets up the timeout so we have time to do that. + this.timeout(5 * 60 * 1000); + + it("Servers have joined!", function(){ + // Alright, lets wait until enough gun server peers are connected. + return servers.atLeast(config.servers); + }); + + it("GUN has spawned!", function(){ + // Once they are, we need to actually spin up the gun server. + var tests = [], i = 0; + servers.each(function(client){ + // for each server peer, tell it to run this code: + tests.push(client.run(function(test){ + // NOTE: Despite the fact this LOOKS like we're in a closure... + // it is not! This code is actually getting run + // in a DIFFERENT machine or process! + var env = test.props; + // As a result, we have to manually pass it scope. + test.async(); + // Clean up from previous test. + try{ require('fs').unlinkSync(env.i+'data.json') }catch(e){} + var server = require('http').createServer(function(req, res){ + res.end("I am "+ env.i +"!"); + }); + // Launch the server and start gun! + var Gun = require('gun'); + // Attach the server to gun. + var gun = Gun({file: env.i+'data', web: server, localStorage: false}); + server.listen(env.config.port + env.i, function(){ + // This server peer is now done with the test! + // It has successfully launched. + test.done(); + }); + }, {i: i += 1, config: config})); + }); + // NOW, this is very important: + // Do not proceed to the next test until + // every single server (in different machines/processes) + // have ALL successfully launched. + return Promise.all(tests); + }); + + it(config.browsers +" browser(s) have joined!", function(){ + // Okay! Cool. Now we can move on to the next step... + console.log("PLEASE OPEN http://"+ config.IP +":"+ config.port +" IN "+ config.browsers +" BROWSER(S)!"); + // Which is to manually open up a bunch of browser tabs + // and connect to the PANIC server in the same way + // the NodeJS servers did. + + // However! We're gonna cheat... + browsers.atLeast(1).then(function(){ + // When there is at least one browser opened, tell it to run this code: + browsers.run(function(test){ + // NOTE: This closure is now being run IN THE BROWSER. + // This code is not server side code, despite the fact + // that we've written it on the server. It is not. + // Mind blowing, right? + var env = test.props; + // Like before, we had to manually pass it some scope. + $('body').prepend(""); + // All right, lets cheat by making a button + // that will automatically open all the + // remaining browser tabs for us + // so we don't have to do it manually. + window.allopen = function(i){ + $('#allopen').remove(); + if(env.config.browsers <= i){ return } + i = i || 1; + var win = window.open(location, '_blank'); + win.focus(); + setTimeout(function(){allopen(i+1)},0); + } + }, {config: config}); + }); + // Cool! Once that is done... + // WAIT until all those browser tabs + // have connected to the PANIC server + // THEN move onto the next step + // where we will cause chaos! + return browsers.atLeast(config.browsers); + }); + + it("Data was saved and synced across all browsers!", function(){ + // This is where it gets good! + var tests = [], ids = {}, i = 0; + // Let us create a list of all the browsers IDs connected. + // This will later let each browser check against every other browser. + browsers.each(function(client, id){ + ids[id] = 1; + }); + browsers.each(function(client, id){ + // for every browser, run the following code: + tests.push(client.run(function(test){ + //var audio = new Audio('https://www.nasa.gov/mp3/640170main_Roger%20Roll.mp3');audio.addEventListener('ended', function() {this.currentTime = 0;this.play();}, false);audio.play(); // testing if audio prevents Chrome throttle? + localStorage.clear(); // Clean up anything from before. + var env = test.props; + // Get access to the "outer scope" which has the browser IDs + // as well as other configuration information. + test.async(); + // Now we want to connect to every gun server peer... + var peers = [], i = env.config.servers; + while(i--){ + // For the total number of servers listed in the configuration + // Add their URL into an array. + peers.push('http://'+ env.config.IP + ':' + (env.config.port + (i + 1)) + '/gun'); + } + // Pass all the servers we want to connect to into gun. + //var gun = Gun(); + var gun = Gun(peers); + // Now we want to create a list + // of all the messages that WILL be sent + // according to the expected configuration. + // This is equal to... + var num = 0, total = 0, check = Gun.obj.map(env.ids, function(v,id,t){ + // for each browser ID + // they will be saving X number of messages each. + var i = env.config.each; + while(i--){ + // So add a deterministic key we can check against. + t(id + (i + 1), 1); + // And count up the total number of messages we expect for all. + total += 1; + } + }); + // Note, this `check` hash table now looks something like this: + // {alice1: 1, alice2: 1, alice3: 1, bob1: 1, bob2: 1, bob3: 1} + var report = $("
").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body'); + // Add a nifty UI that tells us how many messages have been verified. + // FINALLY, tell gun to subscribe to every record + // that is is/will be saved to this table. + + var size = 20; + for (var n = 0; n < size; n++) { + var el = $("
").css({margin: 30, padding: 15, 'background-color': '#9de1fe'}).attr('id', 'post' + n).hide(); + $(log).append(el); + } + + + + // But we have to actually tell the browser to save data! + /* + var i = 0, to = setInterval(function go(){ + // Cool, make a recursive function + // that keeps going until we've saved each message. + if(env.config.each <= i){ + clearTimeout(to); + return; + } + //to = setTimeout(go, env.config.wait * Math.random()); // add a little jitter. + i += 1; + var year = 365 * 24 * 60 * 60 * 1000; + var d = new Date(40 * year + Math.floor(Math.random() * 10 * year)).toISOString(); + // And actually save the data with gun, + // as a record added to one big 'test' table. + gun.get('posts').get(d).put({text: 'Hello world, ' + d + '!', date: d, id: env.id + i}); + }, env.config.wait); */ + }, {i: i += 1, id: id, ids: ids, config: config})); + }); + // YAY! We're finally done. + // IF AND ONLY IF + // EVERY SINGLE BROWSER + // HAS VERIFIED + // EVERY OTHER BROWSERS' data. + // If they are ALL done, go to the next step. + return Promise.all(tests); + }); + + after("Everything shut down.", function(){ + // which is to shut down all the browsers. + browsers.run(function(){ + setTimeout(function(){ + location.reload(); + }, 15 * 1000); + }); + // And shut down all the servers. + return servers.run(function(){ + process.exit(); + }); + }); +}) +// THE END! +// Congrats, wasn't that epic? +// Or still confused how a single 200 LOC test file +// Is running correctness verification tests +// across an entire distributed system of devices/browsers? +// Well, jump on https://gitter.im/amark/gun ! + +// Think adding tests like this to your work place would be bomb awesome? +// We totally sell PANIC training, licenses, and support! +// Please reach out to hi@gunDB.io if you are interested +// in purchasing consulting or services for PANIC. From 11bdf2211e1299bce88c266958cd75df1ed910c2 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Thu, 17 Sep 2020 22:03:58 +0300 Subject: [PATCH 06/15] empty img src in between --- examples/infinite-scroll/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/infinite-scroll/index.js b/examples/infinite-scroll/index.js index c7c4579a..da964efc 100644 --- a/examples/infinite-scroll/index.js +++ b/examples/infinite-scroll/index.js @@ -30,6 +30,7 @@ const render = elements => { var date = new Date(data.date); $('#date' + j).text(date.toLocaleDateString() + ' ' + date.toLocaleTimeString()); $('#text' + j).text(data.text); + $('#img' + j).attr('src', ''); $('#img' + j).attr('src', _getCatImg(date.getTime())); $('#post' + j).css({visibility: 'visible'}); }); From 866e8594d6eb0d41635d1bd9bd437b06b8ee5f2e Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Thu, 17 Sep 2020 23:28:19 +0300 Subject: [PATCH 07/15] scrolling up from bottom --- examples/infinite-scroll/index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/infinite-scroll/index.js b/examples/infinite-scroll/index.js index da964efc..34904450 100644 --- a/examples/infinite-scroll/index.js +++ b/examples/infinite-scroll/index.js @@ -62,16 +62,17 @@ const adjustPaddings = isScrollDown => { const container = document.getElementById("container"); const currentPaddingTop = getNumFromStyle(container.style.paddingTop); const currentPaddingBottom = getNumFromStyle(container.style.paddingBottom); - const remPaddingsVal = 198 * (size / 2); // TODO: calculate element heights + const remPaddingsVal = 198 * (size / 2); // TODO: calculate actual element heights if (isScrollDown) { container.style.paddingTop = currentPaddingTop + remPaddingsVal + "px"; container.style.paddingBottom = currentPaddingBottom === 0 ? "0px" : currentPaddingBottom - remPaddingsVal + "px"; - console.log(container.style.paddingTop, container.style.paddingBottom); - } else { container.style.paddingBottom = currentPaddingBottom + remPaddingsVal + "px"; - container.style.paddingTop = currentPaddingTop === 0 ? "0px" : currentPaddingTop - remPaddingsVal + "px"; - console.log(container.style.paddingTop, container.style.paddingBottom); + if (currentPaddingTop === 0) { + $(window).scrollTop($('#post0').offset().top + remPaddingsVal); + } else { + container.style.paddingTop = currentPaddingTop - remPaddingsVal + "px"; + } } } @@ -91,7 +92,7 @@ const topSentCallback = entry => { scroller.opts.stickTo !== 'top' ) { previousUpIndex = scroller.center; - adjustPaddings(false); // TODO: if top margin 0, increase it + adjustPaddings(false); scroller.up(size / 2); } topSentinelPreviousY = currentY; @@ -134,7 +135,7 @@ const initIntersectionObserver = () => { }); } - var observer = new IntersectionObserver(callback, options); + var observer = new IntersectionObserver(callback, options); // TODO: It's possible to quickly scroll past the sentinels without them firing. Top and bottom sentinels should extend to page top & bottom? observer.observe(document.querySelector("#post0")); observer.observe(document.querySelector(`#post${size - 1}`)); } From 773d5cba25b884430ade177b3e9b74cb6efc7fbc Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Fri, 18 Sep 2020 11:07:56 +0300 Subject: [PATCH 08/15] cleanup --- ...nfiniteScrollWindow.js => ScrollWindow.js} | 37 ++++++++++--------- examples/infinite-scroll/index.html | 6 ++- examples/infinite-scroll/index.js | 2 +- test/panic/infinite-scroll/index.html | 2 +- 4 files changed, 26 insertions(+), 21 deletions(-) rename examples/infinite-scroll/{InfiniteScrollWindow.js => ScrollWindow.js} (78%) diff --git a/examples/infinite-scroll/InfiniteScrollWindow.js b/examples/infinite-scroll/ScrollWindow.js similarity index 78% rename from examples/infinite-scroll/InfiniteScrollWindow.js rename to examples/infinite-scroll/ScrollWindow.js index 54dc9bee..4db4fda6 100644 --- a/examples/infinite-scroll/InfiniteScrollWindow.js +++ b/examples/infinite-scroll/ScrollWindow.js @@ -3,32 +3,35 @@ const DEFAULT_OPTIONS = { stickTo: 'top', }; -class InfiniteScrollWindow { +class ScrollWindow { constructor(gunNode, opts = {}) { this.opts = Object.assign(DEFAULT_OPTIONS, opts); this.elements = new Map(); this.node = gunNode; this.center = this.opts.startAt; - this.updateListeners(); + this.updateSubscriptions(); } - updateListeners() { + updateSubscriptions() { this.upSubscription && this.upSubscription.off(); this.downSubscription && this.downSubscription.off(); + + const subscribe = params => { + this.node.get({ '.': params}).map().on((val, key, a, eve) => { + if (params['-']) { + this.downSubscription = eve; + } else { + this.upSubscription = eve; + } + this._addElement(key, val); + }); + }; + if (this.center) { - this.node.get({ '.': { '>': this.center, '<': '\uffff' }}).map().on((val, key, a, eve) => { - this.upSubscription = eve; - this._addElement(key, val); - }); - this.node.get({ '.': { '<': this.center, '>' : '', '-': true }}).map().on((val, key, a, eve) => { - this.downSubscription = eve; - this._addElement(key, val); - }); + subscribe({ '>': this.center, '<': '\uffff' }); + subscribe({'<': this.center, '>' : '', '-': true}); } else { - this.node.get({ '.': { '<': '\uffff', '>': '', '-': this.opts.stickTo === 'top' }}).map().on((val, key, a, eve) => { - this.upSubscription = eve; - this._addElement(key, val); - }); + subscribe({ '<': '\uffff', '>': '', '-': this.opts.stickTo === 'top' }); } } @@ -46,7 +49,7 @@ class InfiniteScrollWindow { const newMiddleIndex = Math.max(Math.min(half + n, keys.length - 1), 0); if (this.center !== keys[newMiddleIndex]) { this.center = keys[newMiddleIndex]; - this.updateListeners(); + this.updateSubscriptions(); } return this.center; } @@ -62,7 +65,7 @@ class InfiniteScrollWindow { _topOrBottom(top) { this.opts.stickTo = top ? 'top' : 'bottom'; this.center = null; - this.updateListeners(); + this.updateSubscriptions(); } top() { diff --git a/examples/infinite-scroll/index.html b/examples/infinite-scroll/index.html index 0d6d0f49..83ba03f7 100644 --- a/examples/infinite-scroll/index.html +++ b/examples/infinite-scroll/index.html @@ -5,7 +5,7 @@ - + @@ -19,7 +19,9 @@
-
+
+
+
diff --git a/examples/infinite-scroll/index.js b/examples/infinite-scroll/index.js index 34904450..b5448572 100644 --- a/examples/infinite-scroll/index.js +++ b/examples/infinite-scroll/index.js @@ -39,7 +39,7 @@ const render = elements => { const onChange = debounce(render, 20); -const scroller = new InfiniteScrollWindow(gunNode, {size, stickTo: 'top', onChange}); +const scroller = new ScrollWindow(gunNode, {size, stickTo: 'top', onChange}); const initList = () => { for (var n = 0; n < size; n++) { diff --git a/test/panic/infinite-scroll/index.html b/test/panic/infinite-scroll/index.html index b7da61fd..d60afc64 100644 --- a/test/panic/infinite-scroll/index.html +++ b/test/panic/infinite-scroll/index.html @@ -5,7 +5,7 @@ - + From fe578137214f8f37b7e7082d3480ee171d48ab72 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Fri, 18 Sep 2020 15:05:23 +0300 Subject: [PATCH 09/15] simple panic test that scrolls through 1500 entries --- examples/infinite-scroll/index.js | 1 + test/panic/infinite-scroll/index.js | 73 +++++++++-------------------- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/examples/infinite-scroll/index.js b/examples/infinite-scroll/index.js index b5448572..6da2b657 100644 --- a/examples/infinite-scroll/index.js +++ b/examples/infinite-scroll/index.js @@ -35,6 +35,7 @@ const render = elements => { $('#post' + j).css({visibility: 'visible'}); }); console.log('rendering took', new Date().getTime() - t.getTime(), 'ms'); + window.onRender && window.onRender(elements); }; const onChange = debounce(render, 20); diff --git a/test/panic/infinite-scroll/index.js b/test/panic/infinite-scroll/index.js index a416436a..99f2a216 100644 --- a/test/panic/infinite-scroll/index.js +++ b/test/panic/infinite-scroll/index.js @@ -7,7 +7,9 @@ var config = { wait: 1, route: { '/': __dirname + '/index.html', - '/scroller.js': __dirname + '/../../../examples/infinite-scroll/scroller.js', + '/ScrollWindow.js': __dirname + '/../../../examples/infinite-scroll/ScrollWindow.js', + '/index.js': __dirname + '/../../../examples/infinite-scroll/index.js', + '/style.css': __dirname + '/../../../examples/infinite-scroll/style.css', '/gun.js': __dirname + '/../../../gun.js', '/jquery.js': __dirname + '/../../../examples/jquery.js' } @@ -120,20 +122,6 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" // that we've written it on the server. It is not. // Mind blowing, right? var env = test.props; - // Like before, we had to manually pass it some scope. - $('body').prepend(""); - // All right, lets cheat by making a button - // that will automatically open all the - // remaining browser tabs for us - // so we don't have to do it manually. - window.allopen = function(i){ - $('#allopen').remove(); - if(env.config.browsers <= i){ return } - i = i || 1; - var win = window.open(location, '_blank'); - win.focus(); - setTimeout(function(){allopen(i+1)},0); - } }, {config: config}); }); // Cool! Once that is done... @@ -175,49 +163,32 @@ describe("Load test "+ config.browsers +" browser(s) across "+ config.servers +" // of all the messages that WILL be sent // according to the expected configuration. // This is equal to... - var num = 0, total = 0, check = Gun.obj.map(env.ids, function(v,id,t){ - // for each browser ID - // they will be saving X number of messages each. - var i = env.config.each; - while(i--){ - // So add a deterministic key we can check against. - t(id + (i + 1), 1); - // And count up the total number of messages we expect for all. - total += 1; - } - }); - // Note, this `check` hash table now looks something like this: - // {alice1: 1, alice2: 1, alice3: 1, bob1: 1, bob2: 1, bob3: 1} + var num = 0, total = env.config.each, check = {}; var report = $("
").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body'); // Add a nifty UI that tells us how many messages have been verified. // FINALLY, tell gun to subscribe to every record // that is is/will be saved to this table. - var size = 20; - for (var n = 0; n < size; n++) { - var el = $("
").css({margin: 30, padding: 15, 'background-color': '#9de1fe'}).attr('id', 'post' + n).hide(); - $(log).append(el); + var countAndScroll = () => { + $('.post b').each(function() { + var t = $(this).text(); + if (check[t]) return; + num += 1; + report.text(num +" / "+ total +" Verified"); + if (num === total) { + test.done(); + } + check[t] = true; + }); + $(window).scrollTop($(window).height()); } + window.onRender = elements => { + countAndScroll(); + }; - - - // But we have to actually tell the browser to save data! - /* - var i = 0, to = setInterval(function go(){ - // Cool, make a recursive function - // that keeps going until we've saved each message. - if(env.config.each <= i){ - clearTimeout(to); - return; - } - //to = setTimeout(go, env.config.wait * Math.random()); // add a little jitter. - i += 1; - var year = 365 * 24 * 60 * 60 * 1000; - var d = new Date(40 * year + Math.floor(Math.random() * 10 * year)).toISOString(); - // And actually save the data with gun, - // as a record added to one big 'test' table. - gun.get('posts').get(d).put({text: 'Hello world, ' + d + '!', date: d, id: env.id + i}); - }, env.config.wait); */ + $('#number').val(env.config.each); + $('#generate button').click(); + countAndScroll(); }, {i: i += 1, id: id, ids: ids, config: config})); }); // YAY! We're finally done. From 69165f69f5133a120c5ba70df9a83a1df2cf418e Mon Sep 17 00:00:00 2001 From: Adriano Rogowski Date: Thu, 24 Sep 2020 21:46:29 -0300 Subject: [PATCH 10/15] Test RAD lexical queries. --- test/panic/lexical.js | 452 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 452 insertions(+) create mode 100644 test/panic/lexical.js diff --git a/test/panic/lexical.js b/test/panic/lexical.js new file mode 100644 index 00000000..fac94fa6 --- /dev/null +++ b/test/panic/lexical.js @@ -0,0 +1,452 @@ +/** + * RAD Lexical search test + * + * What we want here: (1) Superpeer and (n) peers + * - The Superpeer have a graph with `key(timestamp)->value(value)` + * - The peer will run an amount of queries and the total of results are the 'expected results'. + * + * Tip: to run this `mocha test/panic/lexical` + * + */ +var config = { + IP: require('ip').address(), + port: process.env.PORT ? parseInt(process.env.PORT) : 8765, + servers: 1, + browsers: 1, + route: { + '/': __dirname + '/index.html', + '/gun.js': __dirname + '/../../gun.js', + '/gun/lib/radix.js': __dirname + '/../../lib/radix.js', + '/gun/lib/radisk.js': __dirname + '/../../lib/radisk.js', + '/gun/lib/store.js': __dirname + '/../../lib/store.js', + '/gun/lib/rindexed.js': __dirname + '/../../lib/rindexed.js', + '/jquery.js': __dirname + '/../../examples/jquery.js' + }, + wait_map: 700 +} +var panic = require('panic-server'); +panic.server().on('request', function(req, res) { + config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res); +}).listen(config.port); + +var clients = panic.clients; +var manager = require('panic-manager')(); +manager.start({ + clients: Array(config.servers).fill().map(function(u, i) { + return { + type: 'node', + port: config.port + (i + 1) + } + }), + panic: 'http://' + config.IP + ':' + config.port +}); + +var servers = clients.filter('Node.js'); +var server = global.server = servers.pluck(1); +var server2 = servers.excluding(server).pluck(1); +var browsers = clients.excluding(servers); +var alice = browsers.pluck(1); +var bob = browsers.excluding(alice).pluck(1); +var john = browsers.excluding(alice).excluding(bob).pluck(1); + +describe('RAD Lexical search Test! ', function() { + this.timeout(5 * 60 * 1000); + // this.timeout(10 * 60 * 1000); + it('Servers have joined! ', function() { + return servers.atLeast(config.servers); + }); + it('GUN server started! ', function() { + return server.run(function(test) { + var env = test.props; + test.async(); + try { require('fs').unlinkSync('radata_test_lexical_' + env.i) } catch (e) {} + try { require('fs').unlinkSync('radata_test_lexical_' + (env.i + 1)) } catch (e) {} + var port = env.config.port + env.i; + var server = require('http').createServer(function(req, res) { res.end("I am " + env.i + "!"); }); + var Gun = global.Gun = require('gun'); + var gun = global.gun = Gun({ + axe: false, + multicast: false, + web: server, + file: 'radata_test_lexical_' + env.i, + //radisk:false, localStorage:false + }); + server.listen(port, function() { + test.done(); + }); + }, { + i: 1, + config: config + }); + }); + it('Create graph with all days of year 2020', function() { + return server.run(function(test) { + test.async(); + var j = 0; + graph = gun._.graph, timenode = {}, start = new Date('2020-01-01T00:00:00Z'), end = new Date('2020-12-31T23:59:59Z'), startt = start.getTime(), endt = end.getTime(), p = startt; + var ref = global.ref = gun.get('timenode'); + while (p <= endt) { //console.log('.... >>> ', p, ++j, new Date(p).toISOString()); + var t = new Date(p); + var ts = t.toISOString(); + var soul = 'timeline_' + ts; + var d = { msg: ' datetime ' + ts }; + var n = gun.get(soul).put(d); + ref.get(ts).put(n); + + p = p + (24 * 60 * 60 * 1000); //24h + } + setTimeout(function() { /// wait a while for the .put + test.done(); + }, 1000); + }); + }); + it('Query server - Beetween(< item <) ', function() { + var i=0; + return server.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '>': '2020-02-01', '<': '2020-02-26' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 25; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query server - Beetween(< item <): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes.'); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + }); + }); + it('Query server - Higher(>) ', function() { + var i=0; + return server.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '>': '2020-12-01' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 31; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query server - Higher(>): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + }); + }); + it('Query server - Lower(<) ', function() { + var i=0; + return server.run(function(test) { + test.async(); + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '<': '2020-02-06' } }; + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 37; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query server - Lower(<): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + }); + }); + it('Query server - Exact match(=) ', function() { + var i=0; + return server.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '=': '2020-03-01T00:00:00.000Z' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 1; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query server - Exact match(=): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + }); + }); + it('Query server - Prefix match(*) ', function() { + var i=0; + return server.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '*': '2020-10-' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 31; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query server - Prefix match(*): ', results_unique.length, results_unique); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + }); + }); + + it(config.browsers + ' browser(s) have joined! ', function() { + console.log('PLEASE OPEN http://' + config.IP + ':' + config.port + ' IN ' + config.browsers + ' BROWSER(S)!'); + return browsers.atLeast(config.browsers); + }); + it('Browsers initialized gun! ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + test.async(); + localStorage.clear(); + console.log('Clear localStorage!!!'); +// ['/gun/lib/radix.js', '/gun/lib/radisk.js', '/gun/lib/store.js', '/gun/lib/rindexed.js'].map(function(src) { +// var script = document.createElement('script'); +// script.setAttribute('src', src); +// document.head.appendChild(script); +// }); + + var env = test.props; + var opt = { + peers: ['http://' + env.config.IP + ':' + (env.config.port + 1) + '/gun'], + localStorage: false + }; + var pid = location.hash.slice(1); + if (pid) { + opt.pid = pid; + } + Gun.on('opt', function(ctx) { + this.to.next(ctx); + ctx.on('hi', function(opt) { + document.title = 'RAD test PID: ' + this.on.opt.pid; + }); + }); + setTimeout(function() { + var gun = window.gun = Gun(opt); + var ref = window.ref = gun.get('timenode'); + test.done(); + }, 1000); + }, { + i: i += 1, + config: config + })); + }); + return Promise.all(tests); + }); + + it('Query browser - Beetween(< item <) ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.'); }, 6000); + var results = []; + var query = { '%': 100000, '.': { '>': '2020-06-01', '<': '2020-09-01' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 30+31+31; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query browser - Beetween(< item <): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { i: i += 1, config: config })); + }); + return Promise.all(tests); + }); + it('Query browser - Higher(>) ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '>': '2020-11-01' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 61; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query browser - Higher(>): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + })); + }); + return Promise.all(tests); + }); + it('Query browser - Lower(<) ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '<': '2020-02-05' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + var len = 35; /// expected number of results + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query browser - Lower(<): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + })); + }); + return Promise.all(tests); + }); + it('Query browser - Exact match(=) ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '=': '2020-06-01T00:00:00.000Z' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 1; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query browser - Exact match(=): ', results_unique.length, results_unique.join(', ')); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + })); + }); + return Promise.all(tests); + }); + it('Query browser - Prefix match(*) ', function() { + var tests = [], i = 0; + browsers.each(function(client, id) { + tests.push(client.run(function(test) { + var env = test.props; + var t = setTimeout(function() { test.fail('Error: No response.');}, 5000); + var results = []; + var query = { '%': 100000, '.': { '*': '2020-10-' } }; + test.async(); + ref.get(query).map().once(function(v, k) { + if (k && v) { results.push(k); } + }); + var t2 = setTimeout(function() { + var len = 31; /// expected number of results + var results_unique = results.filter(function(v, i, a) { return a.indexOf(v) === i; }).sort(); + clearTimeout(t); + if (results_unique.length === len) { + test.done(); + } else { + console.log('RESULTS Query browser - Prefix match(*): ', results_unique.length, results_unique); + test.fail('Error: get ' + results_unique.length + ' attributes istead of '+len); + } + }, env.config.wait_map); + }, { + i: i += 1, + config: config + })); + }); + return Promise.all(tests); + }); + + //////////////////////////////// + it("Wait...", function(done){ + setTimeout(done, 3000); + }); + it("All finished!", function(done){ + console.log("Done! Cleaning things up..."); + setTimeout(function(){ + done(); + },1000); + }); + after("Everything shut down.", function(){ + browsers.run(function(){ + //location.reload(); + //setTimeout(function(){ + //}, 15 * 1000); + }); + return servers.run(function(){ + process.exit(); + }); + }); +}); From 84097d7e68ecaf7958cfcc2402d5638b81f47824 Mon Sep 17 00:00:00 2001 From: Robin Bron Date: Sun, 27 Sep 2020 01:55:12 +0200 Subject: [PATCH 11/15] Added radless leveldb adapter --- lib/level.js | 96 ++++++++++++++++++++ test/panic/level.js | 216 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 312 insertions(+) create mode 100644 lib/level.js create mode 100644 test/panic/level.js diff --git a/lib/level.js b/lib/level.js new file mode 100644 index 00000000..c7254af6 --- /dev/null +++ b/lib/level.js @@ -0,0 +1,96 @@ +// CAUTION: This adapter does NOT handle encoding. an encoding mechanism like the encoding-down package will need to be included +// Based on localStorage adapter in 05349a5 + +var Gun = ('undefined' !== typeof window) ? window.Gun : require('../gun'); +var debug = false; + +Gun.on('opt', function(ctx) { + var opt = ctx.opt; + var ev = this.to; + + if (debug) debug.emit('create'); + if (ctx.once) return ev.next(ctx); + + // Check if the given 'level' argument implements all the components we need + // Intentionally doesn't check for levelup explicitly, to allow different handlers implementing the same api + if ( + (!opt.level) || + ('object' !== typeof opt.level) || + ('function' !== typeof opt.level.get) || + ('function' !== typeof opt.level.put) + ) { + return; + } + + ctx.on('put', function(msg) { + this.to.next(msg); + + // Extract data from message + var put = msg.put; + var soul = put['#']; + var key = put['.']; + var val = put[':']; + var state = put['>']; + + if (debug) debug.emit('put', soul, val); + + // Fetch previous version + opt.level.get(soul, function(err, data) { + if (err && (err.name === 'NotFoundError')) err = undefined; + if (debug && err) debug.emit('error', err); + if (err) return; + + // Unclear required transformation + data = Gun.state.ify(data, key, state, val, soul); + + // Write into storage + opt.level.put(soul, data, function(err) { + if (err) return; + if (debug) debug.emit('put', soul, val); + + // Bail if message was an ack + if (msg['@']) return; + + // Send ack back + ctx.on('in', { + '@' : msg['@'], + ok : 0, + }); + }); + }); + }); + + ctx.on('get', function(msg) { + this.to.next(msg); + + // Extract soul from message + var lex = msg.get; + if (!lex || !(soul = lex['#'])) return; + var has = lex['.']; + + if (debug) debug.emit('get', soul); + + // Fetch data from storage + opt.level.get(soul, function(err, data) { + if (err) return; + + // Another unclear transformation + if (data && has) { + data = Gun.state.to(data, has); + } + + // Emulate incoming ack + ctx.on('in', { + '@' : msg['#'], + put : Gun.graph.node(data), + }); + }); + }); + +}); + +// Export debug interface +if ('undefined' === typeof window) { + var EventEmitter = require('events').EventEmitter; + module.exports = debug = new EventEmitter(); +} diff --git a/test/panic/level.js b/test/panic/level.js new file mode 100644 index 00000000..f4457327 --- /dev/null +++ b/test/panic/level.js @@ -0,0 +1,216 @@ +const panic = require('panic-server'); +const clients = panic.clients; +const manager = require('panic-manager')(); +const opts = { radisk: false, localStorage: false, file: false }; + +require('events').EventEmitter.defaultMaxListeners = Infinity; + +const config = { + ip : require('ip').address(), + port : 8765, + servers : 3, + route : { + '/' : __dirname + '/index.html', + '/gun.js' : __dirname + '/../../gun.js', + '/jquery.js' : __dirname + '/../../examples/jquery.js' + } +}; + +const srv = panic.server(); +srv.on('request', (req, res) => { + config.route[req.url] && require('fs').createReadStream(config.route[req.url]).pipe(res); +}).listen(config.port); + +manager.start({ + clients: Array(config.servers).fill().map((u,i) => ({ + type: 'node', + port: config.port + i + 1, + })), + panic: `http://${config.ip}:${config.port}` +}); + +const servers = clients.filter('Node.js'); +const server = servers.pluck(1); +const alice = servers.excluding(server).pluck(1); +const bob = servers.excluding(server).excluding(alice).pluck(1); + +describe('Make sure the leveldb storage engine works', function() { + this.timeout(5 * 60 * 60 * 1000); + + it("servers have joined!", function() { + return servers.atLeast(config.servers); + }); + + it("GUN started!", function() { + return server.run(function(test) { + test.async(); + const {config,opts} = test.props; + + const leveldown = require('leveldown'); + const encode = require('encoding-down'); + const levelup = require('levelup'); + + if (require('fs').existsSync('./lvldata')) { + console.error('Please delete previous data first!'); + return; + } + + // Initialize leveldb + // const level = global.level = levelup(leveldown('./lvldata')); + const level = global.level = levelup(encode(leveldown('./lvldata'), { valueEncoding: 'json' })); + + // Load the libraries under test + const Gun = require('../../../index'); + const debug = require('../../../lib/level'); + + // // Add debug message + // debug.on('create', () => console.log('LEVEL CREATE')); + // debug.on('get' , key => console.log('LEVEL GET', key)); + // debug.on('put' , (key, value) => console.log('LEVEL PUT', key, value)); + // // debug.on('list', () => console.log('LEVEL LIST')); + // debug.on('error' , err => console.log('LEVEL ERROR', err)); + + // Track state (so we can wait on put, it's called late by radisk) + global.state = 0; + debug.on('put', () => global.state++); + + // Create server + opts.web = require('http').createServer(function(req, res) { + res.end("Number five is alive!"); + }); + + // Initialize gun & start server + const gun = global.gun = Gun({ ...opts, level }); + opts.web.listen(config.port + 1, function() { + test.done(); + }); + + }, {config,opts}); + }); + + it("Alice saves data", function() { + return alice.run(function(test) { + test.async(); + const {config,opts} = test.props; + const Gun = require('../../../index'); + + // Start gun + const gun = global.gun = Gun({ + ...opts, + peers: 'http://'+ config.ip + ':' + (config.port + 1) + '/gun', + lack : 1000 * 60 * 60, + }); + + // Save data + // Timeout allows callbacks to fire before server read + const ref = gun.get('asdf'); + ref.put({ hello: 'world' }); + setTimeout(() => { + test.done(); + }, 1); + }, {config,opts}); + }); + + it('Server read data', function() { + return server.run(function(test) { + test.async(); + + // Read data (triggers fetch from alice + write to disk) + const ref = gun.get('asdf'); + ref.on(data => { + if (data.hello !== 'world') { + return test.fail('Invalid data returned'); + } + ref.off(); + test.done(); + }); + + }); + }); + + it('Wait for server to store', function() { + return server.run(function(test) { + test.async(); + setTimeout(function awaitState() { + if (global.state < 2) return setTimeout(awaitState, 50); + test.done(); + }, 50); + }); + }); + + it('Close all original running nodes', function() { + clients.pluck(2).run(function() { + if (global.level) { + global.level.close(function() { + process.exit(); + }); + } else { + process.exit(); + } + }); + }); + + it('Start bob', function() { + return bob.run(function(test) { + test.async(); + const {config,opts} = test.props; + + const leveldown = require('leveldown'); + const encode = require('encoding-down'); + const levelup = require('levelup'); + + // Initialize gun opts + const level = global.level = levelup(encode(leveldown('./lvldata'), { valueEncoding: 'json' })); + + // Load the libraries under test + const Gun = require('../../../index'); + const debug = require('../../../lib/level'); + + // // Add debug messages + // debug.on('get', key => console.log('LEVEL GET', key)); + // debug.on('put', (key, value) => console.log('LEVEL PUT', key, value)); + // // debug.on('list', () => console.log('LEVEL LIST')); + // debug.on('error', err => console.log('LEVEL ERROR', err)); + + // Create server + opts.web = require('http').createServer((req, res) => { + res.end("Number five is alive!"); + }); + + // Initialize gun & start server + const gun = global.gun = Gun({ ...opts, level }); + opts.web.listen(config.port + 1, () => { + test.done(); + }); + }, {config,opts}); + }); + + it('Bob read', function() { + return bob.run(function(test) { + test.async(); + + // Read data + const ref = gun.get('asdf'); + ref.on(data => { + if (data.hello !== 'world') { + return test.fail('Invalid data returned'); + } + ref.off(); + test.done(); + }); + + }); + }); + + it('Shutdown bob', function() { + return clients.run(function() { + process.exit() + }); + }); + + it("All finished!", function(done) { + srv.close(); + done(); + }); + +}); From 37726f6e686d69a1779cee918a1f34605a42b185 Mon Sep 17 00:00:00 2001 From: Joe Still Date: Sun, 18 Oct 2020 17:03:48 -0500 Subject: [PATCH 12/15] Fixed issue 935. Detects zero length key --- gun.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gun.js b/gun.js index 3cc08128..6b503e17 100644 --- a/gun.js +++ b/gun.js @@ -1349,6 +1349,10 @@ Gun.chain.get = function(key, cb, as){ var gun, tmp; if(typeof key === 'string'){ + if(key.length == 0) { + (as = this.chain())._.err = {err: Gun.log('Invalid zero length string key!', key)}; + return null + } var back = this, cat = back._; var next = cat.next || empty; if(!(gun = next[key])){ From ca003747b7d92e8694351dd8a40a612381725a2f Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Tue, 3 Nov 2020 17:01:27 +0200 Subject: [PATCH 13/15] use PEERS env variable in http.js --- examples/http.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/http.js b/examples/http.js index 9c011f49..f0edcdf1 100644 --- a/examples/http.js +++ b/examples/http.js @@ -5,9 +5,14 @@ } var fs = require('fs'); - var config = { port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765 }; + var config = { + port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765, + peers: process.env.PEERS && process.env.PEERS.split(',') || [] + }; var Gun = require('../'); // require('gun') + console.log(config.peers); + if(process.env.HTTPS_KEY){ config.key = fs.readFileSync(process.env.HTTPS_KEY); config.cert = fs.readFileSync(process.env.HTTPS_CERT); @@ -16,8 +21,8 @@ config.server = require('http').createServer(Gun.serve(__dirname)); } - var gun = Gun({web: config.server.listen(config.port)}); + var gun = Gun({web: config.server.listen(config.port), peers: config.peers}); console.log('Relay peer started on port ' + config.port + ' with /gun'); module.exports = gun; -}()); \ No newline at end of file +}()); From 2777d87c4de48b9a56570f31c14143ffe5095ab5 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 4 Nov 2020 11:07:23 +0200 Subject: [PATCH 14/15] remove react-native-webiew-crypto dependency which breaks the build --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 27a21a8e..fca2a721 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "buffer": "^5.4.3" }, "peerDependencies": { - "@gooddollar/react-native-webview-crypto": "^0.*", "bufferutil": "^4.0.1", "utf-8-validate": "^5.0.2", "text-encoding": "^0.7.0" From c254f4c6992f92dd7c0b50745ec62516ecc2025b Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Mon, 9 Nov 2020 16:09:34 +0200 Subject: [PATCH 15/15] remove console.log --- examples/http.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/http.js b/examples/http.js index f0edcdf1..c830f6bb 100644 --- a/examples/http.js +++ b/examples/http.js @@ -11,8 +11,6 @@ }; var Gun = require('../'); // require('gun') - console.log(config.peers); - if(process.env.HTTPS_KEY){ config.key = fs.readFileSync(process.env.HTTPS_KEY); config.cert = fs.readFileSync(process.env.HTTPS_CERT);