diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 288185ba..9fd1884d 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -9,8 +9,6 @@ const uuid = require('uuid').v1 const { fork } = require('child_process') -const Config = require('./config') - const logger = require('../../config/log') /** @@ -88,22 +86,13 @@ const auth = (alias, pass) => const { ack } = msg if (ack.err) { - rej( - new Error( - - ack.err - ) - ) + rej(new Error(ack.err)) } else if (ack.sea) { - lastAlias = alias; + lastAlias = alias lastPass = pass res(ack.sea.pub) } else { - rej( - new Error( - 'Auth: ack.sea undefined' - ) - ) + rej(new Error('Auth: ack.sea undefined')) } } } @@ -111,8 +100,6 @@ const auth = (alias, pass) => currentGun.send(msg) }) - - /** * @returns {Promise} */ @@ -346,19 +333,21 @@ function createUserReplica() { } }, auth(alias, pass, cb) { - auth(alias, pass).then((pub) => { - cb({ - err: undefined, - sea: { - pub, - } + auth(alias, pass) + .then(pub => { + cb({ + err: undefined, + sea: { + pub + } + }) }) - }).catch(e => { - cb({ - err: e.message, - sea: undefined + .catch(e => { + cb({ + err: e.message, + sea: undefined + }) }) - }) }, create() {}, leave() {} @@ -366,3 +355,20 @@ function createUserReplica() { 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 diff --git a/utils/GunSmith/GunT.ts b/utils/GunSmith/GunT.ts index a4c6ef32..282644ec 100644 --- a/utils/GunSmith/GunT.ts +++ b/utils/GunSmith/GunT.ts @@ -2,6 +2,7 @@ * @prettier */ namespace GunT { + export type Primitive = boolean | string | number export interface Data { diff --git a/utils/GunSmith/Smith.ts b/utils/GunSmith/Smith.ts index 1988e96a..9a54fb15 100644 --- a/utils/GunSmith/Smith.ts +++ b/utils/GunSmith/Smith.ts @@ -3,13 +3,17 @@ */ /// namespace Smith { - export interface PendingPut { cb: GunT.Callback data: GunT.ValidDataValue id: string } + export interface SmithMsgInit { + opts: Record + type: 'init' + } + export interface SmithMsgAuth { alias: string pass: string @@ -21,12 +25,6 @@ namespace Smith { type: 'on' } - export interface SmithMsgOnce { - id: string - path: string - type: 'once' - } - export interface SmithMsgPut { id: string data: GunT.ValidDataValue @@ -34,7 +32,12 @@ namespace Smith { type: 'put' } - export type SmithMsg = SmithMsgOn | SmithMsgOnce | SmithMsgPut | BatchSmithMsg + export type SmithMsg = + | SmithMsgInit + | SmithMsgAuth + | SmithMsgOn + | SmithMsgPut + | BatchSmithMsg export type BatchSmithMsg = SmithMsg[] @@ -56,5 +59,5 @@ namespace Smith { type: 'put' } - export type GunMsg = GunMsgAuth| GunMsgOn | GunMsgPut + export type GunMsg = GunMsgAuth | GunMsgOn | GunMsgPut } diff --git a/utils/GunSmith/config.js b/utils/GunSmith/config.js deleted file mode 100644 index 44dab137..00000000 --- a/utils/GunSmith/config.js +++ /dev/null @@ -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' diff --git a/utils/GunSmith/gun.js b/utils/GunSmith/gun.js index 112cb72a..839acde8 100644 --- a/utils/GunSmith/gun.js +++ b/utils/GunSmith/gun.js @@ -8,26 +8,20 @@ const Gun = require('gun') // @ts-ignore require('gun/nts') -const Config = require('./config') - // @ts-ignore 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} */ -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} */ const pendingOnces = new Set() @@ -53,6 +47,21 @@ const handleMsg = msg => { msg.forEach(handleMsg) 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') { const [root, ...keys] = msg.path.split('>') @@ -70,7 +79,6 @@ const handleMsg = msg => { /** @type {Smith.GunMsgOn} */ const res = { data, - key, path: msg.path, type: 'on' } @@ -78,31 +86,6 @@ const handleMsg = msg => { 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') { const [root, ...keys] = msg.path.split('>') @@ -116,17 +99,6 @@ const handleMsg = msg => { for (const key of keys) { 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) - }) } }