diff --git a/scripts/setup.sh b/scripts/setup.sh index c40336a..bf7cb7f 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -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 diff --git a/src/modules/api/submodules/p2p.ts b/src/modules/api/submodules/p2p.ts index 371f130..f3bcee2 100644 --- a/src/modules/api/submodules/p2p.ts +++ b/src/modules/api/submodules/p2p.ts @@ -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>) => { diff --git a/src/types/typeSchemas.ts b/src/types/typeSchemas.ts index 51cfe1d..4b07b67 100644 --- a/src/types/typeSchemas.ts +++ b/src/types/typeSchemas.ts @@ -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 = { diff --git a/src/utils/files.ts b/src/utils/files.ts index 90eae6a..52cce06 100644 --- a/src/utils/files.ts +++ b/src/utils/files.ts @@ -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