From d0f23ddd2e4e3d1d8328fb5adeba181275fb24da Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 12 Sep 2021 10:23:52 -0400 Subject: [PATCH] Further improvements --- utils/GunSmith/GunSmith.js | 60 ++++++++++++++++++++++---------------- utils/GunSmith/GunT.ts | 7 +---- utils/GunSmith/gun.js | 19 ++++++++++-- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 99d75dd6..c88709cf 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -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} */ +// 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} + * @returns {Promise} */ 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} + * Returns null if there's no cached credentials. + * @returns {Promise} */ 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 = () => { - currentGun.off('message', handleMsg) - currentGun.kill() - const newGun = fork('./gun') + if (currentGun) { + currentGun.off('message', handleMsg) + currentGun.kill() + } + 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, - _: { - ...baseReplica._, - // TODO - sea: { - epriv: '', - epub: '', - priv: '', - pub: '' + get _() { + return { + ...baseReplica._, + // TODO + 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} */ diff --git a/utils/GunSmith/GunT.ts b/utils/GunSmith/GunT.ts index 282644ec..730527c8 100644 --- a/utils/GunSmith/GunT.ts +++ b/utils/GunSmith/GunT.ts @@ -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 diff --git a/utils/GunSmith/gun.js b/utils/GunSmith/gun.js index f2a5f9ba..7c333e45 100644 --- a/utils/GunSmith/gun.js +++ b/utils/GunSmith/gun.js @@ -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} */ 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