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

View file

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

View file

@ -11,6 +11,18 @@ require('gun/nts')
// @ts-ignore // @ts-ignore
Gun.log = () => {} 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} * @type {GunT.GUNNode}
*/ */
@ -27,7 +39,7 @@ let user
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
const waitForAuth = async () => { const waitForAuth = async () => {
if (user.is?.pub) { if (user.is && user.is.pub) {
return Promise.resolve() return Promise.resolve()
} }
@ -53,7 +65,10 @@ const handleMsg = msg => {
user.auth(alias, pass, ack => { user.auth(alias, pass, ack => {
/** @type {Smith.GunMsgAuth} */ /** @type {Smith.GunMsgAuth} */
const msg = { const msg = {
ack, ack: {
err: ack.err,
sea: ack.sea
},
type: 'auth' type: 'auth'
} }
// @ts-expect-error // @ts-expect-error