mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Merge remote-tracking branch 'upstream/main' into fix/api-method-guard
main renamed /api/opensky to /api/flights and /api/adsblol/mil to /api/military, and each mount now also matches its /track route, handing it on with next(). The GET check therefore sits after that hand-off, so a /track request is never answered with the snapshot route's 405; a new test pins that, and fails if the check is moved above the hand-off. Tests and notes use the new paths.
This commit is contained in:
commit
46717548c4
739 files changed
+112475
-4274
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import { findNaturalRegion, pointInRing } from '../data/naturalEarthRegions.js';
|
||||
|
||||
/**
|
||||
* The unqualified Alps ask names the European range, not a same-named peak
|
||||
* or local POI. Keep this correction exact and Alps-only: other ranges and
|
||||
* geographic qualifiers retain the caller's configured geocoder semantics.
|
||||
*/
|
||||
export function createAlpsGeocoder({ lookupRegion = findNaturalRegion } = {}) {
|
||||
return {
|
||||
async geocode(query, { signal } = {}) {
|
||||
signal?.throwIfAborted();
|
||||
const name = String(query ?? '')
|
||||
.trim()
|
||||
.replace(/\s+/g, ' ');
|
||||
if (!/^(?:the )?alps(?: mountains)?$/i.test(name))
|
||||
return { place: null, answered: true };
|
||||
|
||||
const region = await lookupRegion('Alps');
|
||||
signal?.throwIfAborted();
|
||||
if (
|
||||
region?.name !== 'Alps' ||
|
||||
region.kind !== 'natural' ||
|
||||
!Array.isArray(region.polygons) ||
|
||||
region.polygons.length === 0 ||
|
||||
!region.polygons.every(
|
||||
(ring) =>
|
||||
Array.isArray(ring) &&
|
||||
ring.length >= 3 &&
|
||||
ring.every(
|
||||
(point) =>
|
||||
Array.isArray(point) &&
|
||||
point.length === 2 &&
|
||||
point.every(Number.isFinite) &&
|
||||
Math.abs(point[0]) <= 180 &&
|
||||
Math.abs(point[1]) <= 90,
|
||||
),
|
||||
)
|
||||
)
|
||||
throw new Error('Bundled Alps geometry unavailable');
|
||||
const vertices = region.polygons.flat();
|
||||
const west = Math.min(...vertices.map(([lon]) => lon));
|
||||
const east = Math.max(...vertices.map(([lon]) => lon));
|
||||
const south = Math.min(...vertices.map(([, lat]) => lat));
|
||||
const north = Math.max(...vertices.map(([, lat]) => lat));
|
||||
const lat = (south + north) / 2;
|
||||
const lng = (west + east) / 2;
|
||||
if (
|
||||
![west, east, south, north, lat, lng].every(Number.isFinite) ||
|
||||
!region.polygons.some((ring) => pointInRing(ring, lat, lng))
|
||||
) {
|
||||
// A broken pack is not permission to cache an unrelated network peak
|
||||
// as the canonical range. Existing search error/retry handling owns it.
|
||||
throw new Error('Bundled Alps geometry unavailable');
|
||||
}
|
||||
return {
|
||||
answered: true,
|
||||
place: {
|
||||
lat,
|
||||
lng,
|
||||
name: region.name,
|
||||
label: region.name,
|
||||
types: ['natural_feature'],
|
||||
exact: true,
|
||||
viewport: {
|
||||
southwest: { lat: south, lng: west },
|
||||
northeast: { lat: north, lng: east },
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { parseCoordinateQuery } from './coordinateParser.js';
|
||||
test('search refuses omitted components and repeated separators', () => {
|
||||
for (const q of ['30,, -97', ',30,-97', '30,-97,', '30;; -97', '30,; -97'])
|
||||
assert.equal(parseCoordinateQuery(q), null, q);
|
||||
for (const q of ['30,-97', '30; -97', '30 -97', 'N 30, W 97'])
|
||||
assert.deepEqual(
|
||||
[parseCoordinateQuery(q).lat, parseCoordinateQuery(q).lon],
|
||||
[30, -97],
|
||||
);
|
||||
});
|
||||
@@ -49,8 +49,8 @@ function splitComponents(query) {
|
||||
const parts = /[,;]/.test(text)
|
||||
? text.split(/[,;]/)
|
||||
: text.split(/\s+(?=[+-]?[.\d]|[NSEW]\s*[+-]?[.\d])/i);
|
||||
const trimmed = parts.map((part) => part.trim()).filter(Boolean);
|
||||
return trimmed.length === 2 ? trimmed : null;
|
||||
const trimmed = parts.map((part) => part.trim());
|
||||
return trimmed.length === 2 && trimmed.every(Boolean) ? trimmed : null;
|
||||
}
|
||||
|
||||
/** Present a parsed pair the way a coordinate is normally written. */
|
||||
|
||||
+17
-10
@@ -6,9 +6,10 @@ import { createGoogleGeocoder } from './google.js';
|
||||
import { createPhotonGeocoder } from '../keylessGeocoder.js';
|
||||
import { createCoordinateGeocoder } from './coordinateGeocoder.js';
|
||||
import { createPresetGeocoder } from './presetGeocoder.js';
|
||||
import { createAlpsGeocoder } from './alpsGeocoder.js';
|
||||
|
||||
/**
|
||||
* Coordinates and bundled names first — both answer offline and with no key —
|
||||
* Coordinates, bundled names and the unqualified Alps first — all offline —
|
||||
* then Google when configured, then keyless Photon, then the local Nominatim
|
||||
* route as a last resort. Transport stays local to setup.
|
||||
*
|
||||
@@ -37,21 +38,27 @@ export function createDefaultPlaceSearch({
|
||||
providers: providers.geocode || [
|
||||
createCoordinateGeocoder(),
|
||||
...(presets ? [createPresetGeocoder({ presets })] : []),
|
||||
createAlpsGeocoder(),
|
||||
...(selected
|
||||
? [selected]
|
||||
: [
|
||||
// Google answers through the local proxy, which holds the server
|
||||
// key: its Geocoding web service refuses referrer-restricted keys,
|
||||
// so calling it from here would force the bundled browser key to be
|
||||
// left unrestricted (#363). That key still gates the provider,
|
||||
// because a keyless session must not spend a request to learn it
|
||||
// has no Google: the proxy's "not configured" reads as an
|
||||
// unanswered provider, which stops the chain caching the negative
|
||||
// Photon or Nominatim then produces.
|
||||
createGoogleGeocoder({
|
||||
request(query, { bias, signal }) {
|
||||
const key = resolveApiKey?.();
|
||||
if (!key) return null;
|
||||
const url = new URL(
|
||||
endpoints.geocode ||
|
||||
'https://maps.googleapis.com/maps/api/geocode/json',
|
||||
if (!resolveApiKey?.()) return null;
|
||||
const params = new URLSearchParams({ address: query });
|
||||
if (bias) params.set('bounds', bias);
|
||||
return fetchImpl(
|
||||
`${endpoints.geocode || '/api/google/geocode'}?${params}`,
|
||||
{ signal },
|
||||
);
|
||||
url.searchParams.set('address', query);
|
||||
url.searchParams.set('key', key);
|
||||
if (bias) url.searchParams.set('bounds', bias);
|
||||
return fetchImpl(url.toString(), { signal });
|
||||
},
|
||||
}),
|
||||
createPhotonGeocoder({ fetchImpl, endpoint: endpoints.photon }),
|
||||
|
||||
+7
-8
@@ -53,7 +53,9 @@ export function createHttpGeospatialProvider({
|
||||
endpoints = {},
|
||||
} = {}) {
|
||||
const urls = {
|
||||
reverse: 'https://maps.googleapis.com/maps/api/geocode/json',
|
||||
// Same-origin like its siblings below: Google's Geocoding web service
|
||||
// refuses referrer-restricted keys, so the key stays on the server (#363).
|
||||
reverse: '/api/google/reverse-geocode',
|
||||
textSearch: '/api/google/text-search',
|
||||
nearby: '/api/google/nearby-places',
|
||||
route: '/api/route',
|
||||
@@ -88,14 +90,11 @@ export function createHttpGeospatialProvider({
|
||||
},
|
||||
routeProfiles: ['foot', 'car', 'bike'],
|
||||
async reverseGeocode(latitude, longitude, options) {
|
||||
const key = resolveApiKey?.();
|
||||
if (resolveApiKey && !key) return null;
|
||||
// The browser key gates the provider without travelling with the
|
||||
// request: the proxy supplies the key Google actually sees.
|
||||
if (resolveApiKey && !resolveApiKey()) return null;
|
||||
return normalizeGoogleReverse(
|
||||
await json(
|
||||
urls.reverse,
|
||||
{ latlng: `${latitude},${longitude}`, ...(key ? { key } : {}) },
|
||||
options,
|
||||
),
|
||||
await json(urls.reverse, { lat: latitude, lon: longitude }, options),
|
||||
);
|
||||
},
|
||||
async textSearch(query, { latitude, longitude, radiusM = 6000 }, options) {
|
||||
|
||||
@@ -20,7 +20,7 @@ test('standalone geocoding falls back after Google connection, JSON and refusal
|
||||
for (const fail of [() => { throw new Error('offline'); }, () => new Response('invalid json'), () => Response.json({ status: 'REQUEST_DENIED' })]) {
|
||||
const urls = [];
|
||||
const service = createStandalonePlaceSearch({ resolveApiKey: () => 'fixture', fetchImpl: async (url) => {
|
||||
urls.push(url); return url.includes('maps.googleapis.com') ? fail() : hit();
|
||||
urls.push(url); return url.includes('/api/google/geocode') ? fail() : hit();
|
||||
} });
|
||||
const result = await service.geocode('Hanoi');
|
||||
assert.equal(result.place.name, 'Hà Nội');
|
||||
|
||||
Reference in new issue
Block a user