New gun does not expose text.random()

This commit is contained in:
Daniel Lugo 2021-10-05 14:36:25 -04:00
parent 22b77e0b2f
commit beb722fb57
8 changed files with 56 additions and 22 deletions

View file

@ -818,8 +818,7 @@ const createPostNew = async (tags, title, content) => {
const mySecret = require('../Mediator').getMySecret()
await Common.Utils.asyncForEach(content, async c => {
// @ts-expect-error
const uuid = Gun.text.random()
const uuid = Utils.gunID()
newPost.contentItems[uuid] = c
if (
(c.type === 'image/embedded' || c.type === 'video/embedded') &&

View file

@ -212,8 +212,8 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
logger.info(
`onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}`
)
//@ts-expect-error
const ackNode = Gun.text.random()
const ackNode = Utils.gunID()
/** @type {import('shock-common').Schema.OrderResponse} */
const orderResponse = {

View file

@ -233,6 +233,21 @@ const isNodeOnline = async pub => {
)
}
/**
* @returns {string}
*/
const gunID = () => {
// Copied from gun internals
let s = ''
let l = 24 // you are not going to make a 0 length random number, so no need to check type
const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz'
while (l > 0) {
s += c.charAt(Math.floor(Math.random() * c.length))
l--
}
return s
}
module.exports = {
dataHasSoul,
delay,
@ -242,5 +257,6 @@ module.exports = {
promisifyGunNode: require('./promisifygun'),
timeout5,
timeout2,
isNodeOnline
isNodeOnline,
gunID
}

View file

@ -0,0 +1,14 @@
/**
* @format
*/
const expect = require('expect')
const { gunID } = require('./index')
describe('gunID()', () => {
it('generates 24-chars-long unique IDs', () => {
const id = gunID()
expect(id).toBeTruthy()
expect(id.length).toBe(24)
})
})

View file

@ -17,6 +17,7 @@ const {
$$__SHOCKWALLET__ENCRYPTED__
} = require('../Mediator')
const logger = require('../../../config/log')
const Utils = require('../contact-api/utils')
/**
* @typedef {import('../contact-api/SimpleGUN').ValidDataValue} ValidDataValue
* @typedef {import('./types').ValidRPCDataValue} ValidRPCDataValue
@ -266,8 +267,7 @@ async function set(rawPath, value) {
if (Array.isArray(theValue)) {
// we'll create a set of sets
// @ts-expect-error
const uuid = Gun.text.random()
const uuid = Utils.gunID()
// here we are simulating the top-most set()
const subPath = rawPath + PATH_SEPARATOR + uuid
@ -278,8 +278,7 @@ async function set(rawPath, value) {
return uuid
} else if (Schema.isObj(theValue)) {
// @ts-expect-error
const uuid = Gun.text.random() // we'll handle UUID ourselves
const uuid = Utils.gunID() // we'll handle UUID ourselves
// so we can use our own put()

View file

@ -6,7 +6,6 @@
// @ts-check
/// <reference path="Smith.ts" />
/// <reference path="GunT.ts" />
const RealGun = require('gun')
const uuid = require('uuid/v1')
const mapValues = require('lodash/mapValues')
const { fork } = require('child_process')
@ -16,12 +15,15 @@ const logger = require('../../config/log')
const { mergePuts, isPopulated } = require('./misc')
const gunUUID = () => {
const RG = /** @type {any} */ (RealGun)
if (typeof RG.text === 'object' && typeof RG.text.random === 'function') {
return RG.text.random()
// Copied from gun internals
let s = ''
let l = 24 // you are not going to make a 0 length random number, so no need to check type
const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz'
while (l > 0) {
s += c.charAt(Math.floor(Math.random() * c.length))
l--
}
// This probably won't happen
throw new ReferenceError()
return s
}
/**

View file

@ -23,8 +23,8 @@ const instance = Gun({
})
const user = instance.user()
const alias = words()
const pass = words()
const alias = words({ exactly: 2 }).join('')
const pass = words({ exactly: 2 }).join('')
/**
* @param {number} ms

View file

@ -1,7 +1,6 @@
/**
* @format
*/
const Gun = require('gun')
const { asyncFilter } = require('./helpers')
@ -9,10 +8,15 @@ const { asyncFilter } = require('./helpers')
* @returns {string}
*/
const gunUUID = () => {
// @ts-expect-error Not typed
const uuid = Gun.text.random()
return uuid
// Copied from gun internals
let s = ''
let l = 24 // you are not going to make a 0 length random number, so no need to check type
const c = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz'
while (l > 0) {
s += c.charAt(Math.floor(Math.random() * c.length))
l--
}
return s
}
module.exports = {