Improve cb types

This commit is contained in:
Jacob Karlsson 2024-05-17 17:38:54 +02:00
parent 8e6128f238
commit 1d2470865b
1 changed files with 30 additions and 32 deletions

View File

@ -72,7 +72,7 @@ const { decrypt } = require('./encryption')
/** /**
* @template T * @template T
* @typedef {T extends void ? * @typedef {[T] extends [void] ?
* (...args: [Error] | []) => void : * (...args: [Error] | []) => void :
* (...args: [Error] | [null, T]) => void * (...args: [Error] | [null, T]) => void
* } CB * } CB
@ -421,7 +421,7 @@ function initDB(peer, config) {
/** /**
* @param {Pick<RecPresent, 'id' | 'msg'>} rec * @param {Pick<RecPresent, 'id' | 'msg'>} rec
* @param {(err: Error | null, tangle: DBTangle | null) => void} cb * @param {CB<DBTangle | null>} cb
*/ */
function getAccountTangle(rec, cb) { function getAccountTangle(rec, cb) {
const accountID = getAccountID(rec) const accountID = getAccountID(rec)
@ -432,8 +432,7 @@ function initDB(peer, config) {
} }
if (!accountTangle.has(accountID)) { if (!accountTangle.has(accountID)) {
return cb( return cb(
Error(`Account tangle "${accountID}" is locally unknown`), Error(`Account tangle "${accountID}" is locally unknown`)
null
) )
} }
return cb(null, accountTangle) return cb(null, accountTangle)
@ -483,7 +482,7 @@ function initDB(peer, config) {
} }
/** /**
* @param {CB<void>} cb * @param {CB<void> | void} cb
*/ */
function loaded(cb) { function loaded(cb) {
if (cb === void 0) return promisify(loaded)() if (cb === void 0) return promisify(loaded)()
@ -499,7 +498,7 @@ function initDB(peer, config) {
* *
* @param {Pick<RecPresent, 'id' | 'msg'>} rec * @param {Pick<RecPresent, 'id' | 'msg'>} rec
* @param {MsgID} tangleID * @param {MsgID} tangleID
* @param {(err: Error | null, val: null) => void} cb * @param {CB<void>} cb
*/ */
function verifyRec(rec, tangleID, cb) { function verifyRec(rec, tangleID, cb) {
let err let err
@ -515,34 +514,34 @@ function initDB(peer, config) {
if ( if (
(err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID)) (err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID))
) { ) {
return cb(Error('Invalid msg', { cause: err }), null) return cb(Error('Invalid msg', { cause: err }))
} }
return cb(null, null) return cb()
} }
// Identify the account and its sigkeys: // Identify the account and its sigkeys:
getAccountTangle(rec, (err, accountTangle) => { getAccountTangle(rec, (err, accountTangle) => {
// prettier-ignore // prettier-ignore
if (err) return cb(Error('Unknown account tangle owning this msg', { cause: err }), null) if (err) return cb(Error('Unknown account tangle owning this msg', { cause: err }))
getSigkeysInAccount( getSigkeysInAccount(
accountTangle, accountTangle,
rec.msg.metadata.accountTips, rec.msg.metadata.accountTips,
(err, sigkeys) => { (err, sigkeys) => {
if (err) return cb(err, null) if (err) return cb(err)
// Don't accept ghosts to come back, unless they are trail msgs // Don't accept ghosts to come back, unless they are trail msgs
if (!!rec.msg.data && ghosts.read(tangleID).has(rec.id)) { if (!!rec.msg.data && ghosts.read(tangleID).has(rec.id)) {
return cb(Error('Refusing a ghost msg to come back'), null) return cb(Error('Refusing a ghost msg to come back'))
} }
if ( if (
(err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID)) (err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID))
) { ) {
return cb(Error('Invalid msg', { cause: err }), null) return cb(Error('Invalid msg', { cause: err }))
} }
/** @param {(err: Error | null, val: null) => void} cb */ /** @param {CB<void>} cb */
function verifyInner(cb) { function verifyInner(cb) {
// Unwrap encrypted inner msg and verify it too // Unwrap encrypted inner msg and verify it too
if (typeof rec.msg.data === 'string') { if (typeof rec.msg.data === 'string') {
@ -554,15 +553,15 @@ function initDB(peer, config) {
verifyRec(innerRec, innerMsgID, (err) => { verifyRec(innerRec, innerMsgID, (err) => {
// prettier-ignore // prettier-ignore
if (err) return cb(Error('Failed to verify inner msg', { cause: err }), null) if (err) return cb(Error('Failed to verify inner msg', { cause: err }))
return cb(null, null) return cb()
}) })
} else { } else {
return cb(null, null) return cb()
} }
} else { } else {
return cb(null, null) return cb()
} }
} }
@ -571,7 +570,7 @@ function initDB(peer, config) {
const validAccountTangle = /** @type {Tangle} */ (accountTangle) const validAccountTangle = /** @type {Tangle} */ (accountTangle)
validateAccountMsg(rec.msg, validAccountTangle, (err) => { validateAccountMsg(rec.msg, validAccountTangle, (err) => {
if (err) if (err)
return cb(Error('Invalid account msg', { cause: err }), null) return cb(Error('Invalid account msg', { cause: err }))
return verifyInner(cb) return verifyInner(cb)
}) })
} else { } else {
@ -677,7 +676,7 @@ function initDB(peer, config) {
/** /**
* @param {Msg} msg * @param {Msg} msg
* @param {Tangle} accountTangle * @param {Tangle} accountTangle
* @param {(err: Error | null, val: null) => void} cb * @param {CB<void>} cb
*/ */
function validateAccountMsg(msg, accountTangle, cb) { function validateAccountMsg(msg, accountTangle, cb) {
if (!MsgV4.isRoot(msg)) { if (!MsgV4.isRoot(msg)) {
@ -690,21 +689,21 @@ function initDB(peer, config) {
public: msg.sigkey, public: msg.sigkey,
} }
getAccountPowers(accountTangle, keypair, (err, powers) => { getAccountPowers(accountTangle, keypair, (err, powers) => {
if (err) return cb(err, null) if (err) return cb(err)
if (!powers.has('add')) { if (!powers.has('add')) {
// prettier-ignore // prettier-ignore
return cb(Error(`invalid account msg: sigkey "${msg.sigkey}" does not have "add" power`), null) return cb(Error(`invalid account msg: sigkey "${msg.sigkey}" does not have "add" power`))
} }
return cb(null, null) return cb()
}) })
} else { } else {
return cb(null, null) return cb()
} }
// TODO validate 'del' // TODO validate 'del'
} else { } else {
return cb(null, null) return cb()
} }
} }
@ -800,7 +799,7 @@ function initDB(peer, config) {
* keypair?: KeypairPublicSlice; * keypair?: KeypairPublicSlice;
* account: string; * account: string;
* }} opts * }} opts
* @param {(err: Error | null, has: boolean | null) => void} cb * @param {CB<boolean>} cb
*/ */
function accountHas(opts, cb) { function accountHas(opts, cb) {
const keypair = opts?.keypair ?? config.global.keypair const keypair = opts?.keypair ?? config.global.keypair
@ -811,7 +810,7 @@ function initDB(peer, config) {
pull.asyncMap((msgID, cb) => { pull.asyncMap((msgID, cb) => {
get(msgID, (err, msg) => { get(msgID, (err, msg) => {
// prettier-ignore // prettier-ignore
if (err) return cb(Error("db.account.has() failed to get() account tangle message", { cause: err }), null) if (err) return cb(Error("db.account.has() failed to get() account tangle message", { cause: err }))
if (!msg?.data) return cb(null, false) if (!msg?.data) return cb(null, false)
/** @type {AccountData} */ /** @type {AccountData} */
@ -826,7 +825,7 @@ function initDB(peer, config) {
}), }),
pull.collect((err, results) => { pull.collect((err, results) => {
// prettier-ignore // prettier-ignore
if (err) return cb(Error('db.account.has() failed to calculate', { cause: err }), null) if (err) return cb(Error('db.account.has() failed to calculate', { cause: err }))
return cb( return cb(
null, null,
@ -897,7 +896,6 @@ function initDB(peer, config) {
}) })
} }
//* @param {(err: Error | null, val: Set<any> | null) => void} cb
/** /**
* @param {Tangle} accountTangle * @param {Tangle} accountTangle
* @param {KeypairPublicSlice} keypair * @param {KeypairPublicSlice} keypair
@ -1286,7 +1284,7 @@ function initDB(peer, config) {
/** /**
* @param {string} accountId * @param {string} accountId
* @param {string} domain * @param {string} domain
* @param {(err: Error | null, rec: RecPresent | null) => void} cb * @param {CB<RecPresent | null>} cb
*/ */
function findMoot(accountId, domain, cb) { function findMoot(accountId, domain, cb) {
const findAccount = MsgV4.stripAccount(accountId) const findAccount = MsgV4.stripAccount(accountId)
@ -1303,7 +1301,7 @@ function initDB(peer, config) {
/** /**
* @param {MsgID} msgID * @param {MsgID} msgID
* @param {(err: Error | null, rec: RecPresent | null) => void} cb * @param {CB<RecPresent | null>} cb
*/ */
function getRecord(msgID, cb) { function getRecord(msgID, cb) {
// TODO: improve performance of this when getting many messages, the arg // TODO: improve performance of this when getting many messages, the arg
@ -1322,7 +1320,7 @@ function initDB(peer, config) {
/** /**
* @param {MsgID} msgID * @param {MsgID} msgID
* @param {(err: Error | null, msg?: Msg) => void} cb * @param {CB<Msg | undefined>} cb
*/ */
function get(msgID, cb) { function get(msgID, cb) {
getRecord(msgID, (err, rec) => { getRecord(msgID, (err, rec) => {
@ -1466,7 +1464,7 @@ function initDB(peer, config) {
/** /**
* @param {MsgID} tangleID * @param {MsgID} tangleID
* @param {(err: Error | null, tangle: DBTangle | null) => void} cb * @param {CB<DBTangle | null>} cb
*/ */
function getTangle(tangleID, cb) { function getTangle(tangleID, cb) {
DBTangle.init(tangleID, records(), get).then((tangle) => { DBTangle.init(tangleID, records(), get).then((tangle) => {