mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(shared): keywords for MapLibre's template and theme styles
The maplibre_style slot gains two values that name no style document: trek:template, which has MapLibre draw the user's map template the way Leaflet does, and trek:ofm-auto, OpenFreeMap Liberty in the light theme and OpenFreeMap Dark in the dark one. No stored row holds either, so no saved setting changes its meaning, and a client that predates them reads them as a foreign style and draws the default. mapConnectHostAllows reads a host-source entry the way the browser reads the policy (a wildcard never covers its apex, no port means the default one), so the client can ask whether a GL map may fetch a tile before it tries.
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { isMapConnectHost, mapExtraConnectHostsSchema, parseMapConnectHosts } from './map-connect-hosts';
|
||||
import {
|
||||
isMapConnectHost,
|
||||
mapConnectHostAllows,
|
||||
mapExtraConnectHostsSchema,
|
||||
parseMapConnectHosts,
|
||||
} from './map-connect-hosts';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -88,3 +93,26 @@ describe('mapExtraConnectHostsSchema', () => {
|
||||
expect(result.error?.issues[0]?.message).toContain('tiles.example.com');
|
||||
});
|
||||
});
|
||||
|
||||
describe('mapConnectHostAllows', () => {
|
||||
it('MAP-HOSTS-ALLOW-001: a plain entry covers its own host on the default port only', () => {
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com')).toBe(true);
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com:443')).toBe(true);
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'TILES.example.com')).toBe(true);
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com:8443')).toBe(false);
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'a.tiles.example.com')).toBe(false);
|
||||
expect(mapConnectHostAllows('tiles.example.com', 'tiles.example.com.evil.test')).toBe(false);
|
||||
});
|
||||
|
||||
it('MAP-HOSTS-ALLOW-002: a port in the entry must be the URL port', () => {
|
||||
expect(mapConnectHostAllows('tiles.example.com:8443', 'tiles.example.com:8443')).toBe(true);
|
||||
expect(mapConnectHostAllows('tiles.example.com:8443', 'tiles.example.com')).toBe(false);
|
||||
});
|
||||
|
||||
it('MAP-HOSTS-ALLOW-003: a wildcard covers every subdomain and never the apex', () => {
|
||||
expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'a.basemaps.cartocdn.com')).toBe(true);
|
||||
expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'x.y.basemaps.cartocdn.com')).toBe(true);
|
||||
expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'basemaps.cartocdn.com')).toBe(false);
|
||||
expect(mapConnectHostAllows('*.basemaps.cartocdn.com', 'evilbasemaps.cartocdn.com')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,6 +66,21 @@ export function parseMapConnectHosts(raw: string | undefined): { hosts: string[]
|
||||
return { hosts: [...hosts], invalid };
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an https URL's host (`host` or `host:port`, as `URL.host` spells it)
|
||||
* falls under one entry of the grammar above, read the way a CSP host-source
|
||||
* reads it: a wildcard covers every subdomain but never the apex, and an entry
|
||||
* without a port covers only the default one. Lets the client ask the same
|
||||
* question of a tile URL that the browser will ask of the policy.
|
||||
*/
|
||||
export function mapConnectHostAllows(entry: string, urlHost: string): boolean {
|
||||
const [entryHost = '', entryPort = ''] = entry.toLowerCase().split(':');
|
||||
const [host = '', port = ''] = urlHost.toLowerCase().split(':');
|
||||
if (entryPort !== (port === '443' ? '' : port)) return false;
|
||||
if (entryHost.startsWith('*.')) return host.endsWith(entryHost.slice(1));
|
||||
return host === entryHost;
|
||||
}
|
||||
|
||||
/** One entry of the list, as the app config carries it. */
|
||||
export const mapConnectHostSchema = z.string().refine(isMapConnectHost, {
|
||||
message: 'Expected a host name such as tiles.example.com, *.tiles.example.com or tiles.example.com:8443',
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { isMapConnectHost } from './map-connect-hosts';
|
||||
import { OPENFREEMAP_DEFAULT_STYLE, OPENFREEMAP_HOST, RASTER_TILE_HOSTS } from './map-defaults';
|
||||
import {
|
||||
MAPLIBRE_FROM_TEMPLATE,
|
||||
MAPLIBRE_OFM_AUTO,
|
||||
OPENFREEMAP_DEFAULT_STYLE,
|
||||
OPENFREEMAP_HOST,
|
||||
RASTER_TILE_HOSTS,
|
||||
} from './map-defaults';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
@@ -16,4 +22,10 @@ describe('map defaults', () => {
|
||||
expect(OPENFREEMAP_DEFAULT_STYLE).toBe('https://tiles.openfreemap.org/styles/liberty');
|
||||
expect(new URL(OPENFREEMAP_DEFAULT_STYLE).host).toBe(OPENFREEMAP_HOST);
|
||||
});
|
||||
|
||||
it('MAP-DEFAULTS-004: the MapLibre keywords are distinct and can never be read as a style URL', () => {
|
||||
expect(MAPLIBRE_FROM_TEMPLATE).toBe('trek:template');
|
||||
expect(MAPLIBRE_OFM_AUTO).toBe('trek:ofm-auto');
|
||||
for (const keyword of [MAPLIBRE_FROM_TEMPLATE, MAPLIBRE_OFM_AUTO]) expect(keyword).not.toMatch(/^(https?|mapbox):/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,6 +15,19 @@ export const OPENFREEMAP_HOST = 'tiles.openfreemap.org';
|
||||
/** The style a MapLibre map draws when nobody chose one. */
|
||||
export const OPENFREEMAP_DEFAULT_STYLE = `https://${OPENFREEMAP_HOST}/styles/liberty`;
|
||||
|
||||
/**
|
||||
* Values of the `maplibre_style` setting that name no style document. No row
|
||||
* held either before they were introduced, so neither changes what a saved
|
||||
* setting meant, and a client that does not know them reads them as a foreign
|
||||
* style and draws the default.
|
||||
*/
|
||||
|
||||
/** MapLibre draws the user's map template (`map_tile_url`), the one Leaflet draws. */
|
||||
export const MAPLIBRE_FROM_TEMPLATE = 'trek:template';
|
||||
|
||||
/** MapLibre draws OpenFreeMap Liberty in the light theme and OpenFreeMap Dark in the dark one. */
|
||||
export const MAPLIBRE_OFM_AUTO = 'trek:ofm-auto';
|
||||
|
||||
/**
|
||||
* The raster tile hosts TREK ships, in the order the policy lists them.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user