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

@ -88,6 +88,9 @@ log "Making moodle test userscript"
checkFile "$PWD/submodules/moodle-test-userscript/stable.user.js"
mkdir -pv "$PWD/publicDirs/qminingPublic/moodle-test-userscript"
ln -sfv "$PWD/submodules/moodle-test-userscript/stable.user.js" "$PWD/publicDirs/qminingPublic/moodle-test-userscript/"
cd "./submodules/moodle-test-userscript/" || exit
./make.sh || exit
cd "${baseDir}" || exit
# ------------------------------------------------------------------------------------
# DB-s

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>) => {

View file

@ -42,14 +42,12 @@ const PeerInfoSchemaBase = {
name: { type: 'string' },
host: { type: 'string' },
port: { type: 'number' },
publicKey: { type: 'string' },
contact: { type: 'string' },
lastSync: { type: 'number' },
note: { type: 'string' },
pw: { type: 'string' },
http: { type: 'boolean' },
},
required: ['name', 'host', 'port', 'contact', 'pw'],
additionalProperties: false,
}
export const SelfInfoSchema: Schema = {
@ -59,6 +57,12 @@ export const SelfInfoSchema: Schema = {
export const PeerInfoSchema: Schema = {
...PeerInfoSchemaBase,
properties: {
...PeerInfoSchemaBase.properties,
publicKey: { type: 'string' },
pw: { type: 'string' },
},
required: ['name', 'host', 'port', 'contact', 'pw'],
}
export const PeersInfoSchema: Schema = {

View file

@ -51,6 +51,20 @@ export const validateFiles = (): boolean => {
return everythingValid
}
export const readAndValidateFile = <T>(file: FileDescriptor): T => {
if (!file.schema) return null
const fileExists = utils.FileExists(file.path)
if (!fileExists) return null
const content = utils.ReadFile(file.path)
const parsedContent: T = JSON.parse(content)
if (!isJsonValidAndLogError(parsedContent, file.schema, file.path))
return null
return parsedContent
}
export const files = {
// --------------------------------------------------------------------------------
// server / modules files