refactor: compose configurable geocoding and route services

This commit is contained in:
Sameh Khamis committed 2026-09-13 21:23:13 -07:00
1 parent 3b91feffdc
commit 8b36c28b06
25 files changed
+764 -211

No files matched your search

+20 -20
View File
@@ -14,33 +14,33 @@ const REGIONAL_BRIEF_STALE_MS = 60 * 60_000;
const REGIONAL_BRIEF_MAX_CACHE = 120;
const _regionalBriefCache = new Map();
const _regionalBriefInFlight = new Map();
const _regionalBriefRateLimiter = makeRateLimiter({
windowMs: 60_000,
max: 30,
globalMax: 90,
});
function trimRegionalBriefCache() {
while (_regionalBriefCache.size > REGIONAL_BRIEF_MAX_CACHE) {
const oldest = _regionalBriefCache.keys().next().value;
if (oldest === undefined) break;
_regionalBriefCache.delete(oldest);
}
}
/** True when at least one regional source produced usable data. */
function regionalBriefHasAnySource({ place, weather, news } = {}) {
return Boolean(place || weather || (news && news.status !== 'unavailable'));
}
function regionalBriefProxy() {
function regionalBriefProxy({ placeProvider = fetchRegionalPlace } = {}) {
const _regionalBriefCache = new Map();
const _regionalBriefInFlight = new Map();
const _regionalBriefRateLimiter = makeRateLimiter({
windowMs: 60_000,
max: 30,
globalMax: 90,
});
function trimRegionalBriefCache() {
while (_regionalBriefCache.size > REGIONAL_BRIEF_MAX_CACHE) {
const oldest = _regionalBriefCache.keys().next().value;
if (oldest === undefined) break;
_regionalBriefCache.delete(oldest);
}
}
async function refresh(point, key) {
const [placeResult, weatherResult] = await Promise.allSettled([
fetchRegionalPlace(point),
placeProvider(point),
fetchRegionalWeather(point),
]);
const place = placeResult.status === 'fulfilled' ? placeResult.value : null;
+30 -25
View File
@@ -1,37 +1,42 @@
import { fetchRegionalJson } from './http.js';
import { normalizeRegionalPlace } from '../../../src/data/regionalBrief.js';
let _nominatimQueue = Promise.resolve();
/** Construct the serialized Nominatim adapter with a trusted endpoint. */
export function createRegionalPlaceProvider({
endpoint = 'https://nominatim.openstreetmap.org/reverse',
requestJson = fetchRegionalJson,
} = {}) {
let _nominatimQueue = Promise.resolve();
let _nominatimLastRequestAt = 0;
let _nominatimLastRequestAt = 0;
function fetchRegionalPlace(point) {
const task = _nominatimQueue.then(async () => {
const waitMs = Math.max(0, 1100 - (Date.now() - _nominatimLastRequestAt));
if (waitMs) await new Promise((resolve) => setTimeout(resolve, waitMs));
_nominatimLastRequestAt = Date.now();
const params = new URLSearchParams({
format: 'jsonv2',
lat: point.latitude.toFixed(5),
lon: point.longitude.toFixed(5),
zoom: '10',
addressdetails: '1',
'accept-language': 'en',
});
const payload = await fetchRegionalJson(
`https://nominatim.openstreetmap.org/reverse?${params}`,
{
function fetchRegionalPlace(point) {
const task = _nominatimQueue.then(async () => {
const waitMs = Math.max(0, 1100 - (Date.now() - _nominatimLastRequestAt));
if (waitMs) await new Promise((resolve) => setTimeout(resolve, waitMs));
_nominatimLastRequestAt = Date.now();
const params = new URLSearchParams({
format: 'jsonv2',
lat: point.latitude.toFixed(5),
lon: point.longitude.toFixed(5),
zoom: '10',
addressdetails: '1',
'accept-language': 'en',
});
const payload = await requestJson(`${endpoint}?${params}`, {
headers: {
'User-Agent':
'GodsEyeView/0.1 (+https://github.com/bilawalsidhu/gods-eye-view)',
Referer: 'https://github.com/bilawalsidhu/gods-eye-view',
},
},
);
return normalizeRegionalPlace(payload);
});
_nominatimQueue = task.catch(() => null);
return task;
});
return normalizeRegionalPlace(payload);
});
_nominatimQueue = task.catch(() => null);
return task;
}
return fetchRegionalPlace;
}
export { fetchRegionalPlace };
export const fetchRegionalPlace = createRegionalPlaceProvider();