Fix parseUrl issues

This commit is contained in:
James Lyne 2021-09-12 20:37:59 +01:00
parent bfd2b4083f
commit 48b94f6bfb

View File

@ -107,89 +107,65 @@ export const parseUrl = (url: URL) => {
const query = new URLSearchParams(url.search), const query = new URLSearchParams(url.search),
hash = url.hash.replace('#', ''); hash = url.hash.replace('#', '');
if(hash) { return hash ? parseMapHash(hash) : parseMapSearchParams(query);
try {
return parseMapHash(hash);
} catch (e) {
console.warn('Ignoring invalid url ' + e);
}
}
try {
return parseMapSearchParams(query);
} catch(e) {
console.warn('Ignoring invalid legacy url ' + e);
}
return null;
} }
export const parseMapHash = (hash: string) => { export const parseMapHash = (hash: string) => {
const parts = hash.replace('#', '').split(';'); const parts = hash.replace('#', '').split(';');
const world = parts[0] || undefined, const world = parts[0] || undefined,
map = parts[1] || undefined; map = parts[1] || undefined,
location = (parts[2] || '').split(',')
let location = (parts[2] || '').split(',')
.map(item => parseFloat(item)) .map(item => parseFloat(item))
.filter(item => !isNaN(item) && isFinite(item)), .filter(item => !isNaN(item) && isFinite(item)),
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined; zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
if(location.length && location.length !== 3) { return validateParsedUrl({
location = [];
console.warn('Ignoring invalid URL location');
}
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
zoom = undefined;
console.warn('Ignoring invalid URL value for zoom');
}
return {
world, world,
map, map,
location: location.length ? { location: location.length === 3 ? {
x: location[0], x: location[0],
y: location[1], y: location[1],
z: location[2], z: location[2],
} : undefined, } : undefined,
zoom, zoom,
legacy: false, legacy: false,
} });
} }
export const parseMapSearchParams = (query: URLSearchParams) => { export const parseMapSearchParams = (query: URLSearchParams) => {
const world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined, const world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined,
map = query.get('mapname') || undefined; map = query.has('worldname') ? query.get('mapname') || undefined : undefined, //Dynmap only
location = [
let location = [
query.get('x') || '', query.get('x') || '',
query.get('y') || '64', query.get('y') || '64',
query.get('z') || '' query.get('z') || ''
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)), ].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)),
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined; zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
if(location.length && location.length !== 3) { return validateParsedUrl({
location = [];
console.warn('Ignoring invalid URL location');
}
if(typeof zoom !== 'undefined' && (isNaN(zoom) || zoom < 0 || !isFinite(zoom))) {
zoom = undefined;
console.warn('Ignoring invalid URL value for zoom');
}
return {
world, world,
map, map,
location: location.length ? { location: location.length === 3 ? {
x: location[0], x: location[0],
y: location[1], y: location[1],
z: location[2], z: location[2],
} : undefined, } : undefined,
zoom, zoom,
legacy: true, legacy: true,
});
}
const validateParsedUrl = (parsed: any) => {
if(typeof parsed.zoom !== 'undefined' && (isNaN(parsed.zoom) || parsed.zoom < 0 || !isFinite(parsed.zoom))) {
parsed.zoom = undefined;
} }
if(!parsed.world) {
return null;
}
return parsed;
} }
export const getUrlForLocation = (map: LiveAtlasMapDefinition, location: { export const getUrlForLocation = (map: LiveAtlasMapDefinition, location: {