Improvements

This commit is contained in:
Daniel Lugo 2021-09-11 20:18:16 -04:00
parent e1e0af488c
commit 3ad9f0f5ef
5 changed files with 68 additions and 111 deletions

View file

@ -9,8 +9,6 @@
const uuid = require('uuid').v1 const uuid = require('uuid').v1
const { fork } = require('child_process') const { fork } = require('child_process')
const Config = require('./config')
const logger = require('../../config/log') const logger = require('../../config/log')
/** /**
@ -88,22 +86,13 @@ const auth = (alias, pass) =>
const { ack } = msg const { ack } = msg
if (ack.err) { if (ack.err) {
rej( rej(new Error(ack.err))
new Error(
ack.err
)
)
} else if (ack.sea) { } else if (ack.sea) {
lastAlias = alias; lastAlias = alias
lastPass = pass lastPass = pass
res(ack.sea.pub) res(ack.sea.pub)
} else { } else {
rej( rej(new Error('Auth: ack.sea undefined'))
new Error(
'Auth: ack.sea undefined'
)
)
} }
} }
} }
@ -111,8 +100,6 @@ const auth = (alias, pass) =>
currentGun.send(msg) currentGun.send(msg)
}) })
/** /**
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
@ -346,14 +333,16 @@ function createUserReplica() {
} }
}, },
auth(alias, pass, cb) { auth(alias, pass, cb) {
auth(alias, pass).then((pub) => { auth(alias, pass)
.then(pub => {
cb({ cb({
err: undefined, err: undefined,
sea: { sea: {
pub, pub
} }
}) })
}).catch(e => { })
.catch(e => {
cb({ cb({
err: e.message, err: e.message,
sea: undefined sea: undefined
@ -366,3 +355,20 @@ function createUserReplica() {
return completeReplica return completeReplica
} }
/**
* @param {import('gun/types/options').IGunConstructorOptions} opts
*/
const Gun = opts => {
/** @type {Smith.SmithMsgInit} */
const msg = {
opts,
type: 'init'
}
currentGun.send(msg)
// We should ideally wait for a response but we'd break the constructor's
// signature
return createReplica('$root')
}
module.exports = Gun

View file

@ -2,6 +2,7 @@
* @prettier * @prettier
*/ */
namespace GunT { namespace GunT {
export type Primitive = boolean | string | number export type Primitive = boolean | string | number
export interface Data { export interface Data {

View file

@ -3,13 +3,17 @@
*/ */
/// <reference path="GunT.ts" /> /// <reference path="GunT.ts" />
namespace Smith { namespace Smith {
export interface PendingPut { export interface PendingPut {
cb: GunT.Callback cb: GunT.Callback
data: GunT.ValidDataValue data: GunT.ValidDataValue
id: string id: string
} }
export interface SmithMsgInit {
opts: Record<string, any>
type: 'init'
}
export interface SmithMsgAuth { export interface SmithMsgAuth {
alias: string alias: string
pass: string pass: string
@ -21,12 +25,6 @@ namespace Smith {
type: 'on' type: 'on'
} }
export interface SmithMsgOnce {
id: string
path: string
type: 'once'
}
export interface SmithMsgPut { export interface SmithMsgPut {
id: string id: string
data: GunT.ValidDataValue data: GunT.ValidDataValue
@ -34,7 +32,12 @@ namespace Smith {
type: 'put' type: 'put'
} }
export type SmithMsg = SmithMsgOn | SmithMsgOnce | SmithMsgPut | BatchSmithMsg export type SmithMsg =
| SmithMsgInit
| SmithMsgAuth
| SmithMsgOn
| SmithMsgPut
| BatchSmithMsg
export type BatchSmithMsg = SmithMsg[] export type BatchSmithMsg = SmithMsg[]
@ -56,5 +59,5 @@ namespace Smith {
type: 'put' type: 'put'
} }
export type GunMsg = GunMsgAuth| GunMsgOn | GunMsgPut export type GunMsg = GunMsgAuth | GunMsgOn | GunMsgPut
} }

View file

@ -1,25 +0,0 @@
/**
* @format
*/
/* eslint-disable no-process-env */
const dotenv = require('dotenv')
const defaults = require('../../config/defaults')(false)
dotenv.config()
// @ts-ignore Let it crash if undefined
exports.DATA_FILE_NAME = process.env.DATA_FILE_NAME || defaults.dataFileName
/**
* @type {string[]}
*/
exports.PEERS = process.env.PEERS
? JSON.parse(process.env.PEERS)
: defaults.peers
exports.MS_TO_TOKEN_EXPIRATION = Number(
process.env.MS_TO_TOKEN_EXPIRATION || defaults.tokenExpirationMS
)
exports.SHOW_LOG = process.env.SHOW_GUN_DB_LOG === 'true'

View file

@ -8,26 +8,20 @@ const Gun = require('gun')
// @ts-ignore // @ts-ignore
require('gun/nts') require('gun/nts')
const Config = require('./config')
// @ts-ignore // @ts-ignore
Gun.log = () => {} Gun.log = () => {}
/**
* This var is just to please typescript's casting rules.
*/
const _gun = /** @type {any} */ (new Gun({
axe: false,
multicast: false,
peers: Config.PEERS
}))
/** /**
* @type {GunT.GUNNode} * @type {GunT.GUNNode}
*/ */
const gun = _gun // eslint-disable-next-line init-declarations
let gun
const user = gun.user() /**
* @type {GunT.UserGUNNode}
*/
// eslint-disable-next-line init-declarations
let user
/** @type {Set<string>} */ /** @type {Set<string>} */
const pendingOnces = new Set() const pendingOnces = new Set()
@ -53,6 +47,21 @@ const handleMsg = msg => {
msg.forEach(handleMsg) msg.forEach(handleMsg)
return return
} }
if (msg.type === 'init') {
gun = /** @type {any} */ (new Gun(msg.opts))
}
if (msg.type === 'auth') {
const { alias, pass } = msg
user.auth(alias, pass, ack => {
/** @type {Smith.GunMsgAuth} */
const msg = {
ack,
type: 'auth'
}
// @ts-expect-error
process.send(msg)
})
}
if (msg.type === 'on') { if (msg.type === 'on') {
const [root, ...keys] = msg.path.split('>') const [root, ...keys] = msg.path.split('>')
@ -70,7 +79,6 @@ const handleMsg = msg => {
/** @type {Smith.GunMsgOn} */ /** @type {Smith.GunMsgOn} */
const res = { const res = {
data, data,
key,
path: msg.path, path: msg.path,
type: 'on' type: 'on'
} }
@ -78,31 +86,6 @@ const handleMsg = msg => {
process.send(res) process.send(res)
}) })
} }
if (msg.type === 'once') {
const [root, ...keys] = msg.path.split('>')
/** @type {GunT.GUNNode} */
let node =
{
$root: gun,
$user: user
}[root] || gun.user(root)
for (const key of keys) {
node = node.get(key)
}
node.once((data, key) => {
/** @type {Smith.GunMsgOnce} */
const res = {
data,
id: msg.id,
key,
type: 'once'
}
// @ts-expect-error
process.send(res)
})
}
if (msg.type === 'put') { if (msg.type === 'put') {
const [root, ...keys] = msg.path.split('>') const [root, ...keys] = msg.path.split('>')
@ -116,17 +99,6 @@ const handleMsg = msg => {
for (const key of keys) { for (const key of keys) {
node = node.get(key) node = node.get(key)
} }
node.on((data, key) => {
/** @type {Smith.GunMsgOn} */
const res = {
data,
key,
path: msg.path,
type: 'on'
}
// @ts-expect-error
process.send(res)
})
} }
} }