Skip to content
Permalink
Browse files

add: standalone gun for publicdb (#49)

* add: standalone gun for publicdb

* fix: verification jest mocks unit tests

* fix: force sendemail unit test

* fix: try to fix stack overflow on travis

* fix: gun callstack error

* fix: try to fix travis tests

* fix: unit tests. revert gun version

* refactor: remove console.log

* fix: unit test, restore await
  • Loading branch information...
sirpy committed Aug 11, 2019
1 parent 983f508 commit 623e4bad742f434aeb3cf9b6a28038409dfa2969
@@ -1,5 +1,6 @@
NODE_ENV=development
GUNDB_PASS=password
GUNDB_PEERS="https://goodgun-dev.herokuapp.com/gun"
JWT_PASS=G00DDAPP
MNEMONIC="myth like bonus scare over problem client lizard pioneer submit female collect"
INFURA_API=
@@ -1,6 +1,9 @@
##
NODE_ENV=development
GUNDB_PASS=password
## superpeers to connect with
GUNDB_PEERS="https://goodgun-dev.herokuapp.com/gun"
GUNDB_SERVERMODE=false
JWT_PASS=G00DDAPP
MNEMONIC=choice congress hobby buddy dutch busy army eager empty solution start grunt
INFURA_API=

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,13 +1,14 @@
/**
* @jest-environment node
*/
import { GunDB } from '../gun-middleware'
import { GunDB, GunDBPrivate } from '../gun-middleware'
import SEA from 'gun/sea'

const storage = new GunDB()
describe('GunDB', () => {
beforeAll(async () => {
await storage.init(null, 'test', 'testdb')
await GunDBPrivate.ready
})

it('Should init correctly without s3', async () => {
@@ -47,4 +48,9 @@ describe('GunDB', () => {
let res = await storage.recordSanitize({ _: { '#': 'soul' } })
expect(res).toEqual({})
})

it('should set private user in database', async () => {
const res = GunDBPrivate.usersCol.get('testuser').putAck({ username: 'test' })
expect(res).resolves.toBeDefined()
})
})
@@ -73,6 +73,20 @@ const setup = (app: Router) => {
* Can be instantiated with a private or a public gundb and should be used to access gun accross the API server
*/
class GunDB implements StorageAPI {
constructor(serverMode: boolean, peers: Array<string> | void = undefined) {
log.info({ serverMode, peers })
this.serverMode = serverMode
this.peers = peers
if (serverMode === false && peers === undefined) {
if (conf.env === 'production') throw new Error('Atleast one peer required for client mode')
else log.warn('Atleast one peer required for client mode')
}
}

serverMode: boolean

peers: Array<string>

gun: Gun

user: Gun
@@ -89,20 +103,35 @@ class GunDB implements StorageAPI {
* @param {string} name folder to store gundb
* @param {S3Conf} [s3] optional S3 settings instead of local file storage
*/
init(server: typeof express | null, password: string, name: string, s3?: S3Conf): Promise<boolean> {
init(server: typeof express | Array<string> | null, password: string, name: string, s3?: S3Conf): Promise<boolean> {
//gun lib/les.js settings
const gc_delay = conf.gunGCInterval || 1 * 60 * 1000 /*1min*/
const memory = conf.gunGCMaxMemoryMB || 512
//log connected peers information
Gun.on('opt', ctx => {
console.log('Starting interval')
setInterval(() => log.info({ GunServer: ctx.opt.name, Peers: Object.keys(ctx.opt.peers).length }), gc_delay)
})
if (s3 && s3.secret) {
if (this.serverMode) {
Gun.on('opt', ctx => {
setInterval(() => log.info({ GunServer: ctx.opt.name, Peers: Object.keys(ctx.opt.peers).length }), gc_delay)
})
}
if (this.serverMode === false) {
log.info('Starting gun as client:', { peers: this.peers })
this.gun = Gun({ file: name, peers: this.peers, axe: true, multicast: false })
} else if (s3 && s3.secret) {
log.info('Starting gun with S3:', { gc_delay, memory })
this.gun = Gun({ web: server, file: name, s3, gc_delay, memory, name, chunk: 1024 * 32, batch: 10 })
this.gun = Gun({
web: server,
file: name,
s3,
gc_delay,
memory,
name,
chunk: 1024 * 32,
batch: 10,
axe: true,
multicast: false
})
} else {
this.gun = Gun({ web: server, file: name, gc_delay, memory, name })
this.gun = Gun({ web: server, file: name, gc_delay, memory, name, axe: true, multicast: false })
log.info('Starting gun with radisk:', { gc_delay, memory })
if (conf.env === 'production') log.error('Started production without S3')
}
@@ -274,7 +303,7 @@ class GunDB implements StorageAPI {
}
}

const GunDBPublic = new GunDB()
const GunDBPublic = new GunDB(conf.gundbServerMode, conf.gundbPeers)
const GunDBPrivate = new GunDB()

GunDBPrivate.init(null, conf.gundbPassword, 'privatedb', conf.gunPrivateS3).catch(e => {
@@ -52,9 +52,12 @@ describe('sendAPÏ', () => {
test('/verify/sendemail with creds', async () => {
const token = await getToken(server)
//make sure fullname is set for user which is required for sending the recovery email
const user = await GunDBPrivate.usersCol
const user = GunDBPrivate.usersCol
.get('0x7ac080f6607405705aed79675789701a48c76f55')
.putAck({ fullName: 'full name', mauticId: 3461 })

expect(user).resolves.toBeDefined()
await user
await request(server)
.post('/send/recoveryinstructions')
.send({
@@ -37,6 +37,18 @@ const conf = convict({
default: '',
env: 'GUNDB_PASS'
},
gundbPeers: {
doc: 'superpeer to connect with for public db',
format: Array,
default: undefined,
env: 'GUNDB_PEERS'
},
gundbServerMode: {
doc: 'should we start as a superpeer',
format: Boolean,
default: false,
env: 'GUNDB_SERVERMODE'
},
jwtPassword: {
doc: 'The password to sign the JWT token with',
format: '*',
@@ -37,7 +37,6 @@ function lightLogs(fn: Function) {
* @param next
*/
const onlyInEnv = (...environments: Array<string>) => {
console.log('checking envs..current env:', conf.env, ' supplied envs: ', { environments })
return function(req: $Request, res: $Response, next: NextFunction) {
if (environments.includes(conf.env)) {
next()
@@ -5,7 +5,9 @@ import delay from 'delay'
import makeServer from '../../server-test'
import { getToken } from '../../__util__/'
import { GunDBPrivate } from '../../gun/gun-middleware'
import Config from '../../server.config'

Config.skipEmailVerification = false
describe('verificationAPI', () => {
let server
beforeAll(done => {
@@ -18,7 +20,6 @@ describe('verificationAPI', () => {
afterAll(done => {
console.log('afterAll')
server.close(err => {
console.log({ err })
done()
})
})

0 comments on commit 623e4ba

Please sign in to comment.
You can’t perform that action at this time.