From eb6674e1067ffca4bdaf7c1450bb8cf9eab2e0f9 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 20 May 2021 00:01:19 +0100 Subject: [PATCH 1/3] Move config validation to Util --- src/api.ts | 106 +---------------------------------------------- src/main.ts | 6 +-- src/util.ts | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 109 deletions(-) diff --git a/src/api.ts b/src/api.ts index ab4f2ca..34ecab8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -30,14 +30,12 @@ import { DynmapTileUpdate, DynmapUpdate, DynmapUpdateResponse, - DynmapUpdates, DynmapUrlConfig, + DynmapUpdates, DynmapWorld, DynmapWorldMap } from "@/dynmap"; import {useStore} from "@/store"; import ChatError from "@/errors/ChatError"; -import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; -import ConfigurationError from "@/errors/ConfigurationError"; const titleColours = /ยง[0-9a-f]/ig; @@ -556,96 +554,6 @@ function buildUpdates(data: Array): DynmapUpdates { return updates; } -const validateLiveAtlasConfiguration = (config: any): Map => { - const check = '\nCheck your server configuration in index.html is correct.', - result = new Map(); - - if (!Object.keys(config).length) { - throw new ConfigurationError(`No servers defined. ${check}`); - } - - for (const server in config) { - if (!Object.hasOwnProperty.call(config, server)) { - continue; - } - - const serverConfig = config[server]; - - if (!serverConfig || serverConfig.constructor !== Object || !Object.keys(serverConfig).length) { - throw new ConfigurationError(`Server '${server}': Configuration missing. ${check}`); - } - - serverConfig.id = server; - serverConfig.type = 'dynmap'; - - if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) { - throw new ConfigurationError(`Server '${server}': Dynmap configuration object missing. ${check}`); - } - - if (!serverConfig.dynmap.configuration) { - throw new ConfigurationError(`Server '${server}': Dynmap configuration URL missing. ${check}`); - } - - if (!serverConfig.dynmap.update) { - throw new ConfigurationError(`Server '${server}': Dynmap update URL missing. ${check}`); - } - - if (!serverConfig.dynmap.markers) { - throw new ConfigurationError(`Server '${server}': Dynmap markers URL missing. ${check}`); - } - - if (!serverConfig.dynmap.tiles) { - throw new ConfigurationError(`Server '${server}': Dynmap tiles URL missing. ${check}`); - } - - if (!serverConfig.dynmap.sendmessage) { - throw new ConfigurationError(`Server '${server}': Dynmap sendmessage URL missing. ${check}`); - } - - result.set(server, serverConfig); - } - - return result; -}; - -const validateDynmapConfiguration = (config: DynmapUrlConfig): Map => { - const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; - - if (!config) { - throw new ConfigurationError(`Dynmap configuration is missing. ${check}`); - } - - if (!config.configuration) { - throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`); - } - - if (!config.update) { - throw new ConfigurationError(`Dynmap update URL is missing. ${check}`); - } - - if (!config.markers) { - throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`); - } - - if (!config.tiles) { - throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`); - } - - if (!config.sendmessage) { - throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`); - } - - const result = new Map(); - result.set('dynmap', { - id: 'dynmap', - label: 'dynmap', - type: 'dynmap', - dynmap: config - }); - - return result; -}; - async function fetchJSON(url: string, signal: AbortSignal) { let response, json; @@ -685,18 +593,6 @@ let configurationAbort: AbortController | undefined = undefined, updateAbort: AbortController | undefined = undefined; export default { - validateConfiguration(): Map { - if (!window.liveAtlasConfig) { - throw new ConfigurationError(`Configuration object is missing`); - } - - if (typeof window.liveAtlasConfig.servers !== 'undefined') { - return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {}); - } - - return validateDynmapConfiguration(window.config?.url || null); - }, - async getConfiguration(): Promise { if(configurationAbort) { configurationAbort.abort(); diff --git a/src/main.ts b/src/main.ts index 91c4da9..ed10357 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,14 +15,14 @@ */ import { createApp } from 'vue' -import App from './App.vue' -import API from './api'; +import App from './App.vue'; import {store} from "@/store"; import 'leaflet/dist/leaflet.css'; import 'normalize-scss/sass/normalize/_import-now.scss'; import '@/scss/style.scss'; import {MutationTypes} from "@/store/mutation-types"; +import {validateConfiguration} from "@/util"; const splash = document.getElementById('splash'), splashSpinner = document.getElementById('splash__spinner'), @@ -86,7 +86,7 @@ window.showSplashError = function(message: string, fatal: boolean, attempts: num console.info(`LiveAtlas version ${store.state.version} - https://github.com/JLyne/LiveAtlas`); try { - const config = API.validateConfiguration(); + const config = validateConfiguration(); store.commit(MutationTypes.SET_SERVERS, config); diff --git a/src/util.ts b/src/util.ts index 8fe7f35..8c1cf90 100644 --- a/src/util.ts +++ b/src/util.ts @@ -16,6 +16,8 @@ import {DynmapPlayer} from "@/dynmap"; import {useStore} from "@/store"; +import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; +import ConfigurationError from "@/errors/ConfigurationError"; interface HeadQueueEntry { cacheKey: string; @@ -201,4 +203,117 @@ export const parseMapSearchParams = (query: URLSearchParams) => { zoom, legacy: true, } -} \ No newline at end of file +} + + +const validateLiveAtlasConfiguration = (config: any): Map => { + const check = '\nCheck your server configuration in index.html is correct.', + result = new Map(); + + if (!Object.keys(config).length) { + throw new ConfigurationError(`No servers defined. ${check}`); + } + + for (const server in config) { + if (!Object.hasOwnProperty.call(config, server)) { + continue; + } + + const serverConfig = config[server]; + + if (!serverConfig || serverConfig.constructor !== Object || !Object.keys(serverConfig).length) { + throw new ConfigurationError(`Server '${server}': Configuration missing. ${check}`); + } + + serverConfig.id = server; + serverConfig.type = serverConfig.type || 'dynmap'; + + switch(serverConfig.type) { + case 'dynmap': + if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) { + throw new ConfigurationError(`Server '${server}': Dynmap configuration object missing. ${check}`); + } + + if (!serverConfig.dynmap.configuration) { + throw new ConfigurationError(`Server '${server}': Dynmap configuration URL missing. ${check}`); + } + + if (!serverConfig.dynmap.update) { + throw new ConfigurationError(`Server '${server}': Dynmap update URL missing. ${check}`); + } + + if (!serverConfig.dynmap.markers) { + throw new ConfigurationError(`Server '${server}': Dynmap markers URL missing. ${check}`); + } + + if (!serverConfig.dynmap.tiles) { + throw new ConfigurationError(`Server '${server}': Dynmap tiles URL missing. ${check}`); + } + + if (!serverConfig.dynmap.sendmessage) { + throw new ConfigurationError(`Server '${server}': Dynmap sendmessage URL missing. ${check}`); + } + break; + + case 'pl3xmap': + case 'plexmap': + if (!serverConfig.plexmap || serverConfig.plexmap.constructor !== Object) { + throw new ConfigurationError(`Server '${server}': Pl3xmap configuration object missing. ${check}`); + } + } + + result.set(server, serverConfig); + } + + return result; +}; + +const validateDynmapConfiguration = (config: DynmapUrlConfig): Map => { + const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; + + if (!config) { + throw new ConfigurationError(`Dynmap configuration is missing. ${check}`); + } + + if (!config.configuration) { + throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`); + } + + if (!config.update) { + throw new ConfigurationError(`Dynmap update URL is missing. ${check}`); + } + + if (!config.markers) { + throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`); + } + + if (!config.tiles) { + throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`); + } + + if (!config.sendmessage) { + throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`); + } + + const result = new Map(); + result.set('dynmap', { + id: 'dynmap', + label: 'dynmap', + type: 'dynmap', + dynmap: config + }); + + return result; +}; + +export const validateConfiguration = (): Map => { + if (!window.liveAtlasConfig) { + throw new ConfigurationError(`Configuration object is missing`); + } + + if (typeof window.liveAtlasConfig.servers !== 'undefined') { + return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {}); + } + + return validateDynmapConfiguration(window.config?.url || null); +}; From 2ab569c18fbba9ddf411f32bda425b0a047d412f Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 20 May 2021 00:06:57 +0100 Subject: [PATCH 2/3] Refactor state.currentServer to contain the server config instead of just the ID. Abstract API away from actions. --- src/App.vue | 11 ++++++++--- src/components/sidebar/ServerListItem.vue | 6 +++--- src/store/actions.ts | 10 +++++----- src/store/getters.ts | 2 +- src/store/mutations.ts | 4 ++-- src/store/state.ts | 2 +- src/util.ts | 13 ++++++++++++- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/App.vue b/src/App.vue index 6414a8a..1191d27 100644 --- a/src/App.vue +++ b/src/App.vue @@ -29,6 +29,7 @@ import {useStore} from "@/store"; import {ActionTypes} from "@/store/action-types"; import {parseUrl} from '@/util'; import {MutationTypes} from "@/store/mutation-types"; +import {LiveAtlasServerDefinition} from "@/index"; export default defineComponent({ name: 'App', @@ -62,7 +63,7 @@ export default defineComponent({ return; } - const error = `Failed to load server configuration for '${store.state.currentServer}'`; + const error = `Failed to load server configuration for '${store.state.currentServer.id}'`; console.error(`${error}:`, e); window.showSplashError(`${error}\n${e}`, false, ++configAttempts.value); setTimeout(() => loadConfiguration(), 1000); @@ -125,16 +126,20 @@ export default defineComponent({ watch(title, (title) => document.title = title); watch(currentUrl, (url) => window.history.replaceState({}, '', url)); - watch(currentServer, (newServer) => { + watch(currentServer, (newServer: LiveAtlasServerDefinition) => { window.showSplash(); stopUpdates(); + if(!newServer) { + return; + } + //Cleanup store.commit(MutationTypes.CLEAR_PLAYERS, undefined); store.commit(MutationTypes.CLEAR_CURRENT_MAP, undefined); store.commit(MutationTypes.CLEAR_PARSED_URL, undefined); - window.history.replaceState({}, '', newServer); + window.history.replaceState({}, '', newServer.id); loadConfiguration(); }); watch(configurationHash, (newHash, oldHash) => { diff --git a/src/components/sidebar/ServerListItem.vue b/src/components/sidebar/ServerListItem.vue index 1388a13..d2a192d 100644 --- a/src/components/sidebar/ServerListItem.vue +++ b/src/components/sidebar/ServerListItem.vue @@ -15,8 +15,8 @@ -->