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
* @typedef {T extends void ?
* @typedef {[T] extends [void] ?
* (...args: [Error] | []) => void :
* (...args: [Error] | [null, T]) => void
* } CB
@ -421,7 +421,7 @@ function initDB(peer, config) {
/**
* @param {Pick<RecPresent, 'id' | 'msg'>} rec
* @param {(err: Error | null, tangle: DBTangle | null) => void} cb
* @param {CB<DBTangle | null>} cb
*/
function getAccountTangle(rec, cb) {
const accountID = getAccountID(rec)
@ -432,8 +432,7 @@ function initDB(peer, config) {
}
if (!accountTangle.has(accountID)) {
return cb(
Error(`Account tangle "${accountID}" is locally unknown`),
null
Error(`Account tangle "${accountID}" is locally unknown`)
)
}
return cb(null, accountTangle)
@ -483,7 +482,7 @@ function initDB(peer, config) {
}
/**
* @param {CB<void>} cb
* @param {CB<void> | void} cb
*/
function loaded(cb) {
if (cb === void 0) return promisify(loaded)()
@ -499,7 +498,7 @@ function initDB(peer, config) {
*
* @param {Pick<RecPresent, 'id' | 'msg'>} rec
* @param {MsgID} tangleID
* @param {(err: Error | null, val: null) => void} cb
* @param {CB<void>} cb
*/
function verifyRec(rec, tangleID, cb) {
let err
@ -515,34 +514,34 @@ function initDB(peer, config) {
if (
(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:
getAccountTangle(rec, (err, accountTangle) => {
// 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(
accountTangle,
rec.msg.metadata.accountTips,
(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
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 (
(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) {
// Unwrap encrypted inner msg and verify it too
if (typeof rec.msg.data === 'string') {
@ -554,15 +553,15 @@ function initDB(peer, config) {
verifyRec(innerRec, innerMsgID, (err) => {
// 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 {
return cb(null, null)
return cb()
}
} else {
return cb(null, null)
return cb()
}
}
@ -571,7 +570,7 @@ function initDB(peer, config) {
const validAccountTangle = /** @type {Tangle} */ (accountTangle)
validateAccountMsg(rec.msg, validAccountTangle, (err) => {
if (err)
return cb(Error('Invalid account msg', { cause: err }), null)
return cb(Error('Invalid account msg', { cause: err }))
return verifyInner(cb)
})
} else {
@ -677,7 +676,7 @@ function initDB(peer, config) {
/**
* @param {Msg} msg
* @param {Tangle} accountTangle
* @param {(err: Error | null, val: null) => void} cb
* @param {CB<void>} cb
*/
function validateAccountMsg(msg, accountTangle, cb) {
if (!MsgV4.isRoot(msg)) {
@ -690,21 +689,21 @@ function initDB(peer, config) {
public: msg.sigkey,
}
getAccountPowers(accountTangle, keypair, (err, powers) => {
if (err) return cb(err, null)
if (err) return cb(err)
if (!powers.has('add')) {
// 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 {
return cb(null, null)
return cb()
}
// TODO validate 'del'
} else {
return cb(null, null)
return cb()
}
}
@ -800,7 +799,7 @@ function initDB(peer, config) {
* keypair?: KeypairPublicSlice;
* account: string;
* }} opts
* @param {(err: Error | null, has: boolean | null) => void} cb
* @param {CB<boolean>} cb
*/
function accountHas(opts, cb) {
const keypair = opts?.keypair ?? config.global.keypair
@ -811,7 +810,7 @@ function initDB(peer, config) {
pull.asyncMap((msgID, cb) => {
get(msgID, (err, msg) => {
// 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)
/** @type {AccountData} */
@ -826,7 +825,7 @@ function initDB(peer, config) {
}),
pull.collect((err, results) => {
// 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(
null,
@ -897,7 +896,6 @@ function initDB(peer, config) {
})
}
//* @param {(err: Error | null, val: Set<any> | null) => void} cb
/**
* @param {Tangle} accountTangle
* @param {KeypairPublicSlice} keypair
@ -1286,7 +1284,7 @@ function initDB(peer, config) {
/**
* @param {string} accountId
* @param {string} domain
* @param {(err: Error | null, rec: RecPresent | null) => void} cb
* @param {CB<RecPresent | null>} cb
*/
function findMoot(accountId, domain, cb) {
const findAccount = MsgV4.stripAccount(accountId)
@ -1303,7 +1301,7 @@ function initDB(peer, config) {
/**
* @param {MsgID} msgID
* @param {(err: Error | null, rec: RecPresent | null) => void} cb
* @param {CB<RecPresent | null>} cb
*/
function getRecord(msgID, cb) {
// TODO: improve performance of this when getting many messages, the arg
@ -1322,7 +1320,7 @@ function initDB(peer, config) {
/**
* @param {MsgID} msgID
* @param {(err: Error | null, msg?: Msg) => void} cb
* @param {CB<Msg | undefined>} cb
*/
function get(msgID, cb) {
getRecord(msgID, (err, rec) => {
@ -1466,7 +1464,7 @@ function initDB(peer, config) {
/**
* @param {MsgID} tangleID
* @param {(err: Error | null, tangle: DBTangle | null) => void} cb
* @param {CB<DBTangle | null>} cb
*/
function getTangle(tangleID, cb) {
DBTangle.init(tangleID, records(), get).then((tangle) => {