mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Merge upstream/main 73c14ac74
This commit is contained in:
commit
a64b41b3f0
1594 files changed
+155855
-24143
No files matched your search
@@ -47,15 +47,37 @@ function readComponent(text) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Split on a comma, a semicolon, or whitespace — never on anything else. */
|
||||
/**
|
||||
* Split on a comma, a semicolon, or whitespace — never on anything else.
|
||||
* A hemisphere letter stays with the number it names, so `40.7 N 74.0 W`
|
||||
* is two components. Splitting on the space before that letter used to
|
||||
* leave three pieces and the search declined the coordinate.
|
||||
*/
|
||||
function splitComponents(query) {
|
||||
const text = String(query).trim();
|
||||
if (!text) return null;
|
||||
const parts = /[,;]/.test(text)
|
||||
? text.split(/[,;]/)
|
||||
: text.split(/\s+(?=[+-]?[.\d]|[NSEW]\s*[+-]?[.\d])/i);
|
||||
const trimmed = parts.map((part) => part.trim());
|
||||
return trimmed.length === 2 && trimmed.every(Boolean) ? trimmed : null;
|
||||
if (/[,;]/.test(text)) {
|
||||
const trimmed = text.split(/[,;]/).map((part) => part.trim());
|
||||
return trimmed.length === 2 && trimmed.every(Boolean) ? trimmed : null;
|
||||
}
|
||||
const words = text.split(/\s+/);
|
||||
// Each component has at most a number, a degree sign and one hemisphere.
|
||||
if (words.length > 6) return null;
|
||||
// Keep every character: extracting tokens would turn `1e3` or `1.2.3`
|
||||
// into invented coordinates. A lone letter between numbers can name either
|
||||
// component; require an unambiguous split rather than guessing its owner.
|
||||
let result = null;
|
||||
for (let boundary = words.length - 1; boundary > 0; boundary -= 1) {
|
||||
const parts = [
|
||||
words.slice(0, boundary).join(' '),
|
||||
words.slice(boundary).join(' '),
|
||||
];
|
||||
if (parts.every((part) => readComponent(part))) {
|
||||
if (result) return null;
|
||||
result = parts;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Present a parsed pair the way a coordinate is normally written. */
|
||||
|
||||
@@ -52,6 +52,19 @@ test('hemisphere letters fix the axis, in either order', () => {
|
||||
label: '40.0000° N, 74.0000° W',
|
||||
});
|
||||
assert.deepEqual(parseCoordinateQuery('40° N, 74° W')?.lat, 40);
|
||||
// Written the way a coordinate is usually pasted, with no comma.
|
||||
assert.deepEqual(parseCoordinateQuery('40.7128 N 74.0060 W'), {
|
||||
lat: 40.7128,
|
||||
lon: -74.006,
|
||||
label: '40.7128° N, 74.0060° W',
|
||||
});
|
||||
assert.deepEqual(parseCoordinateQuery('40.7128° N 74.0060° W')?.lon, -74.006);
|
||||
assert.deepEqual(parseCoordinateQuery('N 40.7128 W 74.0060'), {
|
||||
lat: 40.7128,
|
||||
lon: -74.006,
|
||||
label: '40.7128° N, 74.0060° W',
|
||||
});
|
||||
assert.deepEqual(parseCoordinateQuery('74.0060 W 40.7128 N')?.lat, 40.7128);
|
||||
// One letter is enough: it fixes its own axis, the other takes what is left.
|
||||
assert.deepEqual(parseCoordinateQuery('40 N, -74')?.lon, -74);
|
||||
assert.deepEqual(parseCoordinateQuery('-74, 40 N'), {
|
||||
@@ -66,6 +79,21 @@ test('malformed and ambiguous coordinates are refused, never guessed', () => {
|
||||
// The reviewer's case: trailing text must not be discarded.
|
||||
'12junk, 34oops',
|
||||
'12junk,34',
|
||||
'12junk 34',
|
||||
'1e3 5',
|
||||
'1e3',
|
||||
'1E3',
|
||||
'1e-3',
|
||||
'1.2.3',
|
||||
'12-34',
|
||||
'40 NW 74',
|
||||
'40 N 74',
|
||||
'40 W 74',
|
||||
'40 E 74',
|
||||
'°40 74',
|
||||
'40°° 74',
|
||||
'40 74°°',
|
||||
'40 26 46 N 79 58 56 W',
|
||||
'43.1731, -79.0384x',
|
||||
'abc, def',
|
||||
'Paris',
|
||||
@@ -196,3 +224,25 @@ test('malformed or unsupported geo URIs are refused without losing their meaning
|
||||
assert.equal(parseCoordinateQuery(query), null, query);
|
||||
}
|
||||
});
|
||||
|
||||
test('whitespace boundaries preserve degree signs and hemisphere ownership', () => {
|
||||
for (const query of ['40 ° 74', '40° 74', '40 N, 74', 'N 40 74']) {
|
||||
assert.equal(parseCoordinateQuery(query)?.lat, 40, query);
|
||||
assert.equal(parseCoordinateQuery(query)?.lon, 74, query);
|
||||
}
|
||||
for (const query of ['40 ° N 74 ° W', 'N 40 ° W 74 °', '40\tN\n74\tW']) {
|
||||
assert.equal(parseCoordinateQuery(query)?.lat, 40, query);
|
||||
assert.equal(parseCoordinateQuery(query)?.lon, -74, query);
|
||||
}
|
||||
});
|
||||
|
||||
test('a signed plain component leaves a spaced letter with the other component', () => {
|
||||
assert.deepEqual(
|
||||
parseCoordinateQuery('-40 N 74'),
|
||||
parseCoordinateQuery('-40, N 74'),
|
||||
);
|
||||
assert.deepEqual(
|
||||
parseCoordinateQuery('40 N -74'),
|
||||
parseCoordinateQuery('40 N, -74'),
|
||||
);
|
||||
});
|
||||
+21
-2
@@ -2,6 +2,7 @@ import { createNominatimProvider } from './nominatim.js';
|
||||
import { createGeospatialServices } from './geospatial.js';
|
||||
import { createHttpGeospatialProvider } from './http.js';
|
||||
import { createPlaceSearch } from './placeSearch.js';
|
||||
import { createNominatimStatus } from '../sources/nominatimStatus.js';
|
||||
import { createGoogleGeocoder } from './google.js';
|
||||
import { createPhotonGeocoder } from '../keylessGeocoder.js';
|
||||
import { createCoordinateGeocoder } from './coordinateGeocoder.js';
|
||||
@@ -28,6 +29,9 @@ export function createDefaultPlaceSearch({
|
||||
} = {}) {
|
||||
if (geocoding && geocoding.provider !== 'nominatim')
|
||||
throw new TypeError('Unsupported geocoding provider');
|
||||
const nominatimConfigured = endpoints.nominatim
|
||||
? null
|
||||
: createNominatimStatus({ fetchImpl });
|
||||
const selected = geocoding
|
||||
? createNominatimProvider({ ...geocoding, fetchImpl })
|
||||
: null;
|
||||
@@ -61,12 +65,27 @@ export function createDefaultPlaceSearch({
|
||||
);
|
||||
},
|
||||
}),
|
||||
createPhotonGeocoder({ fetchImpl, endpoint: endpoints.photon }),
|
||||
// `endpoints.photon === null` is the operator's "no Photon".
|
||||
...(endpoints.photon === null
|
||||
? []
|
||||
: [
|
||||
createPhotonGeocoder({
|
||||
fetchImpl,
|
||||
endpoint: endpoints.photon ?? undefined,
|
||||
}),
|
||||
]),
|
||||
// Last resort: the local Nominatim route, which answers with no key when
|
||||
// neither of the two above did. It speaks the same result shape, so it
|
||||
// rides the existing Google adapter rather than needing its own.
|
||||
createGoogleGeocoder({
|
||||
request(query, { bias, signal }) {
|
||||
async request(query, { bias, signal }) {
|
||||
// The app's own route says up front when Nominatim is off.
|
||||
const capability = nominatimConfigured
|
||||
? await nominatimConfigured()
|
||||
: null;
|
||||
// A search cancelled during the probe starts no request.
|
||||
signal?.throwIfAborted();
|
||||
if (capability === false) return null;
|
||||
const params = new URLSearchParams({ q: query });
|
||||
if (bias) params.set('bounds', bias);
|
||||
return fetchImpl(
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// PHOTON_URL: unset keeps the public default, empty disables, else a URL.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createDefaultPlaceSearch } from './defaults.js';
|
||||
|
||||
async function hosts(endpoints) {
|
||||
const asked = [];
|
||||
const search = createDefaultPlaceSearch({
|
||||
endpoints,
|
||||
fetchImpl: async (url) => {
|
||||
asked.push(new URL(String(url), 'http://local.test').host);
|
||||
return new Response(
|
||||
JSON.stringify({ features: [], status: 'ZERO_RESULTS', results: [] }),
|
||||
{ status: 200 },
|
||||
);
|
||||
},
|
||||
});
|
||||
await search.geocode('nowhere-photon-setting');
|
||||
return asked;
|
||||
}
|
||||
|
||||
test('place search follows the Photon setting', async () => {
|
||||
assert.ok(
|
||||
(await hosts({})).includes('photon.komoot.io'),
|
||||
'default is public Photon',
|
||||
);
|
||||
const own = await hosts({ photon: 'https://photon.example.test/api/' });
|
||||
assert.ok(own.includes('photon.example.test'));
|
||||
assert.ok(!own.includes('photon.komoot.io'));
|
||||
const off = await hosts({ photon: null });
|
||||
assert.ok(!off.some((host) => host.includes('photon')), off.join(','));
|
||||
});
|
||||
|
||||
test('the last-resort route is skipped when the server reports Nominatim unconfigured', async () => {
|
||||
const run = async (configured) => {
|
||||
const asked = [];
|
||||
const search = createDefaultPlaceSearch({
|
||||
endpoints: { photon: null },
|
||||
fetchImpl: async (url) => {
|
||||
const path = new URL(String(url), 'http://local.test').pathname;
|
||||
asked.push(path);
|
||||
if (path === '/api/geocode/status')
|
||||
return Response.json({ configured });
|
||||
return Response.json({ status: 'ZERO_RESULTS', results: [] });
|
||||
},
|
||||
});
|
||||
await search.geocode(`nowhere-nominatim-${configured}`);
|
||||
return asked;
|
||||
};
|
||||
const off = await run(false);
|
||||
assert.ok(off.includes('/api/geocode/status'));
|
||||
assert.ok(!off.includes('/api/geocode'), off.join(','));
|
||||
const on = await run(true);
|
||||
assert.ok(on.includes('/api/geocode'), on.join(','));
|
||||
});
|
||||
|
||||
test('a search cancelled while the Nominatim probe is pending starts no last-resort request', async () => {
|
||||
let release;
|
||||
const held = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const asked = [];
|
||||
const controller = new AbortController();
|
||||
const search = createDefaultPlaceSearch({
|
||||
endpoints: { photon: null },
|
||||
fetchImpl: async (url) => {
|
||||
const path = new URL(String(url), 'http://local.test').pathname;
|
||||
asked.push(path);
|
||||
if (path === '/api/geocode/status') {
|
||||
controller.abort();
|
||||
await held;
|
||||
return Response.json({ configured: true });
|
||||
}
|
||||
return Response.json({ status: 'ZERO_RESULTS', results: [] });
|
||||
},
|
||||
});
|
||||
const pending = search.geocode('nowhere-cancelled', {
|
||||
signal: controller.signal,
|
||||
});
|
||||
release();
|
||||
await pending.catch(() => null);
|
||||
assert.ok(!asked.includes('/api/geocode'), asked.join(','));
|
||||
});
|
||||
Reference in new issue
Block a user