Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jabis/723542616ded82e2d86f389be892d526 to your computer and use it in GitHub Desktop.
Save jabis/723542616ded82e2d86f389be892d526 to your computer and use it in GitHub Desktop.
Gun promises using nodejs and tape
'use strict'
const fs = require('fs');
const tape = require('tape');
const _test = require('tape-promise').default // <---- notice 'default'
const test = _test(tape) // decorate tape
const Gun = require('../gun')
require('../gun/sea')
require('../gun/lib/then.js')
require('../gun/lib/promise.js')
const mkdirp = require('mkdirp');
const { Random } = require('random-js');
const random = new Random();
const _ = require('lodash');
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
test('test gun.user().get(key)', async function(t) {
//console.log(Gun.SEA);
Gun.SEA.throw = true;
//let t = async function(){
const key = 'foo123';
const value = 'value123';
const username = 'paul@example.com';
const password = 'badpassword';
mkdirp.sync('testdb');
// don't use gun's default database, else state is saved across tests, which is an anti-pattern
const dbfilename = "./testdb/"+random.string(16) + '.json';
//fs.closeSync(fs.openSync(dbfilename, 'w'));
console.log('writing to '+dbfilename);
await sleep(250);
let gun = Gun({peers: [], multicast: false, file: dbfilename, web: undefined});
//let peergun1 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
//let peergun2 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
// put some random data to possibly work around abug
let result = await gun.get(key).put({property: value }).then();
console.log("wrote",result);
await sleep(265);
// create user doesn't have a once() so then() doesn't work. So wrap in a promise
const ack = await new Promise((res, rej) => {
gun.user().create(username, password, ack => {
console.log(ack);
gun.user().leave();
res(ack);
//gun.user().leave();
});
});
console.log("awaited ack",ack,"sleeping");
const theSecondDelayResult = await sleep(265);
console.log('delay ended. Now attempting to gun.user().get(key).then');
gun.on('auth',async function(){
let ref = gun.user().get(key);
console.log("ref",ref);
let v = await ref.put({property:value},function(ack){ console.log("put ack",ack); }).then();
console.log("val",v);
//v;
const result2 = await gun.user().get(key).then();
t.equal(result2.property, value, 'result2 was what we put in');
t.end();
});
await gun.user().auth(username,password,function(ack){ console.log("auth",ack) }).then();
//return val?true:false;
});
// gundb doesn't unref its internal timers and also multicast
// so the process to exit.
test('force test to end', async function(t) {
t.ok(true, 'forcing test to end with process.exit');
t.end();
process.exit();
});
@jabis
Copy link
Author

jabis commented Jan 13, 2020

Git diff what I had to change in ../gun/sea/ directory

diff --git a/sea/sea.js b/sea/sea.js
index 75f8b0d..e87ca18 100644
--- a/sea/sea.js
+++ b/sea/sea.js
@@ -48,9 +48,9 @@
     // But all other behavior needs to be equally easy, like opinionated ways of
     // Adding friends (trusted public keys), sending private messages, etc.
     // Cheers! Tell me what you think.
-    var Gun = (SEA.window||{}).Gun || require('./gun', 1);
+    var Gun = (SEA.window||{}).Gun || require('gun', 1);
     Gun.SEA = SEA;
     SEA.GUN = SEA.Gun = Gun;

     module.exports = SEA
-
\ No newline at end of file
+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment