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

jabis@rig:/home/tests/gun/test$ node index.js
Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!
TAP version 13
# test gun.user().get(key)
writing to ./testdb/hRALTcF7RYPxHcTz.json
wrote {
  _: { '#': 'foo123', '>': { property: 1578907984780 } },
  property: 'value123'
}
{
  ok: 0,
  pub: 'L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0'
}
awaited ack {
  ok: 0,
  pub: 'L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0'
} sleeping
delay ended.  Now attempting to gun.user().get(key).then
auth {
  gun: Gun { _: [Circular] },
  '$': Gun { _: [Circular] },
  root: {
    gun: Gun { _: [Circular] },
    '$': Gun { _: [Circular] },
    opt: {
      peers: {},
      multicast: false,
      file: './testdb/hRALTcF7RYPxHcTz.json',
      web: undefined,
      super: true,
      WebSocket: [Function],
      log: [Function: bound consoleCall],
      gap: 1,
      pack: 419700000,
      mesh: [Function],
      wire: [Function: open],
      uuid: [Function],
      store: [Function],
      until: 9,
      batch: 10000,
      chunk: 10485760,
      code: [Object],
      pid: 'VxgiiuoGe'
    },
    root: [Circular],
    graph: {
      foo123: [Object],
      '~@paul@example.com': [Object],
      '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0': [Object]
    },
    on: [Function: onto] {
      put: [Function],
      get: [Function],
      _: [Object],
      off: [Function]
    },
    ask: [Function: ask],
    dup: {
      s: [Object],
      check: [Function],
      track: [Function],
      to: Timeout {
        _idleTimeout: 9009,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 399,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: true,
        [Symbol(asyncId)]: 14,
        [Symbol(triggerId)]: 0
      }
    },
    sea: { own: {} },
    tag: {
      in: [Object],
      out: [Object],
      node: [Object],
      create: [Object],
      bye: [Object],
      hi: [Object],
      put: [Object],
      get: [Object],
      pJkrX9dNt: [Object],
      ack: [Object],
      v9lSkDcDq: [Object],
      auth: [Object],
      S8vZcVDGA: [Object]
    },
    once: 5,
    next: {
      foo123: [Object],
      '~@paul@example.com': [Object],
      '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0': [Circular]
    },
    mum: undefined,
    stop: null,
    user: User { _: [Circular], is: [Object] },
    now: undefined
  },
  id: 5,
  back: {
    gun: Gun { _: [Circular] },
    '$': Gun { _: [Circular] },
    opt: {
      peers: {},
      multicast: false,
      file: './testdb/hRALTcF7RYPxHcTz.json',
      web: undefined,
      super: true,
      WebSocket: [Function],
      log: [Function: bound consoleCall],
      gap: 1,
      pack: 419700000,
      mesh: [Function],
      wire: [Function: open],
      uuid: [Function],
      store: [Function],
      until: 9,
      batch: 10000,
      chunk: 10485760,
      code: [Object],
      pid: 'VxgiiuoGe'
    },
    root: [Circular],
    graph: {
      foo123: [Object],
      '~@paul@example.com': [Object],
      '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0': [Object]
    },
    on: [Function: onto] {
      put: [Function],
      get: [Function],
      _: [Object],
      off: [Function]
    },
    ask: [Function: ask],
    dup: {
      s: [Object],
      check: [Function],
      track: [Function],
      to: Timeout {
        _idleTimeout: 9009,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 399,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: true,
        [Symbol(asyncId)]: 14,
        [Symbol(triggerId)]: 0
      }
    },
    sea: { own: {} },
    tag: {
      in: [Object],
      out: [Object],
      node: [Object],
      create: [Object],
      bye: [Object],
      hi: [Object],
      put: [Object],
      get: [Object],
      pJkrX9dNt: [Object],
      ack: [Object],
      v9lSkDcDq: [Object],
      auth: [Object],
      S8vZcVDGA: [Object]
    },
    once: 5,
    next: {
      foo123: [Object],
      '~@paul@example.com': [Object],
      '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0': [Circular]
    },
    mum: undefined,
    stop: null,
    user: User { _: [Circular], is: [Object] },
    now: undefined
  },
  on: [Function: onto] {
    put: [Function],
    get: [Function],
    _: { next: [Function: next] },
    off: [Function]
  },
  tag: {
    in: { tag: 'in', to: [Object], last: [Object] },
    out: { tag: 'out', to: [Object], last: [Object] }
  },
  get: '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
  soul: '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
  put: {
    _: {
      '#': '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
      '>': [Object]
    },
    pub: 'L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
    alias: 'paul@example.com',
    epub: 'XTZyI2wWlM4XG8QgJ3Ua3Lcd1CN1wm70s7DmKkJD0Dg.W9_6fWlLaem3dCSqOBxBPnU2waFqJPBAXYvPgRb7Rwo',
    auth: {
      ek: [Object],
      s: '0SarSJB57vABPU47XKQVkb4H554uUz6Ak28TC1yfMIawtG2D5qix74CWDHHatwNg'
    }
  },
  ack: 3,
  opt: { uuid: [Function] },
  sea: {
    pub: 'L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
    epub: 'XTZyI2wWlM4XG8QgJ3Ua3Lcd1CN1wm70s7DmKkJD0Dg.W9_6fWlLaem3dCSqOBxBPnU2waFqJPBAXYvPgRb7Rwo',
    priv: 'VRn5n4jIGwvooER3g7ZyWndNZLZMy1VIvZEOnDvLI7M',
    epriv: '7Zu5HKGmPplTlyfAyQRc6g_r-J9Xxxrz4RHCkK9ybsI'
  }
}
ref User {
  _: {
    '$': [Circular],
    root: {
      gun: [Gun],
      '$': [Gun],
      opt: [Object],
      root: [Circular],
      graph: [Object],
      on: [Function],
      ask: [Function: ask],
      dup: [Object],
      sea: [Object],
      tag: [Object],
      once: 6,
      next: [Object],
      mum: undefined,
      stop: null,
      user: [User],
      now: undefined
    },
    id: 6,
    back: {
      gun: [Gun],
      '$': [Gun],
      root: [Object],
      id: 5,
      back: [Object],
      on: [Function],
      tag: [Object],
      get: '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
      soul: '~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0',
      put: [Object],
      ack: 3,
      opt: [Object],
      sea: [Object],
      next: [Object]
    },
    on: [Function: onto] {
      put: [Function],
      get: [Function],
      _: [Object],
      off: [Function]
    },
    tag: { in: [Object], out: [Object] },
    get: 'foo123',
    has: 'foo123'
  }
}
put ack { '@': 'byumglv64ob', ok: 1, '#': 'nb8Omnl3K' }
val {
  _: {
    '#': 'k5c91r2fFRMicADafeRU~L8Jfrs-Ao_1qF18j69jzOg5NQYTMMRchXMYOodDFBdk.8mhL4iD4bEg19C8haNNpF-n4uPtnh_s9TifVM79COv0.',
    '>': { property: 1578907985989 }
  },
  property: 'value123'
}
ok 1 result2 was what we put in
# force test to end
ok 2 forcing test to end with process.exit

1..2
# tests 2
# pass  2

# ok

@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