Further improvements

This commit is contained in:
Daniel Lugo 2021-09-12 10:23:52 -04:00
parent f94646d67b
commit d0f23ddd2e
3 changed files with 53 additions and 33 deletions

View file

@ -31,8 +31,8 @@ const handleMsg = msg => {
const { data, path } = msg
// eslint-disable-next-line no-multi-assign
const listeners = (pathToListeners[path] =
pathToListeners[path] || new Set())
const listeners =
pathToListeners[path] || (pathToListeners[path] = new Set())
for (const l of listeners) {
l(data, path.split('>')[path.split('>').length - 1])
@ -60,17 +60,22 @@ const handleMsg = msg => {
}
}
let currentGun = fork('./gun')
/** @type {ReturnType<typeof fork>} */
// eslint-disable-next-line init-declarations
let currentGun
let lastAlias = ''
let lastPass = ''
/** @type {GunT.UserPair} */
// eslint-disable-next-line init-declarations
let lastPair
/** @type {import('gun/types/options').IGunConstructorOptions} */
let lastOpts = {}
/**
* @param {string} alias
* @param {string} pass
* @returns {Promise<string>}
* @returns {Promise<GunT.UserPair>}
*/
const auth = (alias, pass) =>
new Promise((res, rej) => {
@ -80,6 +85,7 @@ const auth = (alias, pass) =>
pass,
type: 'auth'
}
/** @param {Smith.GunMsg} msg */
const _cb = msg => {
if (msg.type === 'auth') {
@ -92,7 +98,9 @@ const auth = (alias, pass) =>
} else if (ack.sea) {
lastAlias = alias
lastPass = pass
res(ack.sea.pub)
lastPair = ack.sea
processPendingPutsFromLastGun(currentGun)
res(ack.sea)
} else {
rej(new Error('Auth: ack.sea undefined'))
}
@ -103,11 +111,12 @@ const auth = (alias, pass) =>
})
/**
* @returns {Promise<string>}
* Returns null if there's no cached credentials.
* @returns {Promise<GunT.UserPair|null>}
*/
const autoAuth = () => {
if (!lastAlias || !lastPass) {
return Promise.resolve('')
return Promise.resolve(null)
}
return auth(lastAlias, lastPass)
}
@ -117,11 +126,18 @@ const processPendingPutsFromLastGun = async (forGun, pps = pendingPuts) => {
}
const forge = () => {
if (currentGun) {
currentGun.off('message', handleMsg)
currentGun.kill()
const newGun = fork('./gun')
}
const newGun = fork('utils/GunSmith/gun.js')
currentGun = newGun
// currentGun.on('', e => {
// console.log('event from subp')
// console.log(e)
// })
/** @type {Smith.SmithMsgInit} */
const initMsg = {
opts: lastOpts,
@ -141,9 +157,7 @@ const forge = () => {
})
currentGun.send(lastGunListeners)
autoAuth().then(() => {
processPendingPutsFromLastGun(newGun)
})
autoAuth()
}
/**
@ -334,24 +348,19 @@ function createUserReplica() {
/** @type {GunT.UserGUNNode} */
const completeReplica = {
...baseReplica,
_: {
get _() {
return {
...baseReplica._,
// TODO
sea: {
epriv: '',
epub: '',
priv: '',
pub: ''
sea: lastPair
}
},
auth(alias, pass, cb) {
auth(alias, pass)
.then(pub => {
.then(pair => {
cb({
err: undefined,
sea: {
pub
}
sea: pair
})
})
.catch(e => {
@ -372,6 +381,7 @@ function createUserReplica() {
* @param {import('gun/types/options').IGunConstructorOptions} opts
*/
const Gun = opts => {
forge()
lastOpts = opts
/** @type {Smith.SmithMsgInit} */

View file

@ -2,7 +2,6 @@
* @prettier
*/
namespace GunT {
export type Primitive = boolean | string | number
export interface Data {
@ -93,11 +92,7 @@ namespace GunT {
export interface AuthAck {
err: string | undefined
sea:
| {
pub: string
}
| undefined
sea: UserPair | undefined
}
export type AuthCB = (ack: AuthAck) => void

View file

@ -11,6 +11,18 @@ require('gun/nts')
// @ts-ignore
Gun.log = () => {}
console.log('subprocess invoked')
process.on('uncaughtException', e => {
console.log('Uncaught exception inside Gun subprocess:')
console.log(e)
})
process.on('unhandledRejection', e => {
console.log('Unhandled rejection inside Gun subprocess:')
console.log(e)
})
/**
* @type {GunT.GUNNode}
*/
@ -27,7 +39,7 @@ let user
* @returns {Promise<void>}
*/
const waitForAuth = async () => {
if (user.is?.pub) {
if (user.is && user.is.pub) {
return Promise.resolve()
}
@ -53,7 +65,10 @@ const handleMsg = msg => {
user.auth(alias, pass, ack => {
/** @type {Smith.GunMsgAuth} */
const msg = {
ack,
ack: {
err: ack.err,
sea: ack.sea
},
type: 'auth'
}
// @ts-expect-error