script build update, peers file watching/updating/validating fixes, peers schema improvements

This commit is contained in:
mrfry 2023-04-10 11:01:07 +02:00
parent 1e4f45a76c
commit 0d1abc7d4f
4 changed files with 33 additions and 35 deletions

View file

@ -53,12 +53,7 @@ import {
getAvailableQdbIndexes,
removeCacheFromQuestion,
} from '../../../utils/qdbUtils'
import {
isJsonValidAndLogError,
PeersInfoSchema,
SelfInfoSchema,
} from '../../../types/typeSchemas'
import { paths } from '../../../utils/files'
import { files, paths, readAndValidateFile } from '../../../utils/files'
import { GetResult, get, post } from '../../../utils/networkUtils'
interface MergeResult {
@ -419,15 +414,6 @@ function setup(data: SubmoduleData): Submodule {
let peers: PeerInfo[] = utils.ReadJSON(paths.peersFile)
let selfInfo: PeerInfo = utils.ReadJSON(paths.selfInfoFile)
// TODO: this should be checked by files!
if (!isJsonValidAndLogError(peers, PeersInfoSchema, paths.peersFile)) {
throw new Error('Invalid peers file')
}
if (!isJsonValidAndLogError(selfInfo, SelfInfoSchema, paths.selfInfoFile)) {
throw new Error('Invalid peers file')
}
// self info file is not required to have the publicKey, as it is always added on init
selfInfo.publicKey = publicKey
const filesToWatch = [
@ -435,13 +421,9 @@ function setup(data: SubmoduleData): Submodule {
fname: paths.peersFile,
logMsg: 'Peers file updated',
action: () => {
try {
peers = utils.ReadJSON(paths.peersFile)
} catch (e) {
logger.Log(
`Peers file contents are invalid! Check if syntax is correct for ${paths.peersFile}`,
'redbg'
)
const newVal = readAndValidateFile<PeerInfo[]>(files.peersFile)
if (newVal) {
peers = newVal
}
},
},
@ -449,14 +431,9 @@ function setup(data: SubmoduleData): Submodule {
fname: paths.selfInfoFile,
logMsg: 'P2P self info file changed',
action: () => {
try {
selfInfo = utils.ReadJSON(paths.selfInfoFile)
selfInfo.publicKey = publicKey
} catch (e) {
logger.Log(
`Self info file contents are invalid! Check if syntax is correct for ${paths.selfInfoFile}`,
'redbg'
)
const newVal = readAndValidateFile<PeerInfo>(files.selfInfoFile)
if (newVal) {
selfInfo = newVal
}
},
},
@ -487,7 +464,7 @@ function setup(data: SubmoduleData): Submodule {
function getSelfInfo(includeVerboseInfo?: boolean) {
const result: RemotePeerInfo = {
selfInfo: selfInfo,
selfInfo: { ...selfInfo, publicKey: publicKey },
myPeers: peers,
}
@ -941,7 +918,7 @@ function setup(data: SubmoduleData): Submodule {
// APP SETUP
// ---------------------------------------------------------------------------------------
app.get('/selfInfo', (_req: Request, res: Response<PeerInfo>) => {
res.json(selfInfo)
res.json({ ...selfInfo, publicKey: publicKey })
})
app.get('/p2pinfo', (_req: Request, res: Response<RemotePeerInfo>) => {