style: format extracted local services

This commit is contained in:
Sameh Khamis committed 2026-09-12 13:01:49 -07:00
1 parent 233285fe12
commit 5223b6df5b
33 files changed
+1785 -830

No files matched your search

@@ -1,4 +1,10 @@
import { MILITARY_INSTALLATION_CACHE_MS, MILITARY_INSTALLATION_ELEMENT_CAP, MILITARY_INSTALLATION_DISK_TTL_MS, MILITARY_INSTALLATION_DISK_DIR, MILITARY_INSTALLATION_MAX_CACHE } from './constants.js';
import {
MILITARY_INSTALLATION_CACHE_MS,
MILITARY_INSTALLATION_ELEMENT_CAP,
MILITARY_INSTALLATION_DISK_TTL_MS,
MILITARY_INSTALLATION_DISK_DIR,
MILITARY_INSTALLATION_MAX_CACHE,
} from './constants.js';
import path from 'node:path';
import { createHash } from 'node:crypto';
import { promises as fsp } from 'node:fs';
@@ -32,10 +38,13 @@ async function resolveMilitaryInstallationTier({
cacheMs = MILITARY_INSTALLATION_CACHE_MS,
}) {
const cached = memoryCache.get(cacheKey);
if (cached && now - cached.cachedAt <= cacheMs) return { source: 'HIT', entry: cached };
if (cached && now - cached.cachedAt <= cacheMs)
return { source: 'HIT', entry: cached };
if (inFlight.has(cacheKey)) return { source: 'UPSTREAM', entry: null };
const disk = await readDisk();
return disk ? { source: 'DISK', entry: disk } : { source: 'UPSTREAM', entry: null };
return disk
? { source: 'DISK', entry: disk }
: { source: 'UPSTREAM', entry: null };
}
/**
@@ -51,8 +60,11 @@ async function resolveMilitaryInstallationTier({
* @returns {?{payload: object, cachedAt: number}}
*/
function migrateMilitaryInstallationEntry(entry) {
if (!entry?.payload || typeof entry.payload.saturated === 'boolean') return entry;
const elements = Array.isArray(entry.payload.elements) ? entry.payload.elements : [];
if (!entry?.payload || typeof entry.payload.saturated === 'boolean')
return entry;
const elements = Array.isArray(entry.payload.elements)
? entry.payload.elements
: [];
return {
...entry,
payload: {
@@ -68,13 +80,24 @@ function militaryInstallationDiskFresh(
maxAgeMs = MILITARY_INSTALLATION_DISK_TTL_MS,
now = Date.now(),
) {
if (!entry || !Number.isFinite(entry.cachedAt) || !Array.isArray(entry.payload?.elements)) return false;
if (
!entry ||
!Number.isFinite(entry.cachedAt) ||
!Array.isArray(entry.payload?.elements)
)
return false;
return now - entry.cachedAt <= maxAgeMs;
}
/** Cache key -> stable disk-cache file path. */
function militaryInstallationDiskPath(cacheKey, dir = MILITARY_INSTALLATION_DISK_DIR) {
return path.join(dir, `${createHash('sha1').update(cacheKey).digest('hex')}.json`);
function militaryInstallationDiskPath(
cacheKey,
dir = MILITARY_INSTALLATION_DISK_DIR,
) {
return path.join(
dir,
`${createHash('sha1').update(cacheKey).digest('hex')}.json`,
);
}
/**
@@ -88,7 +111,9 @@ async function readMilitaryInstallationDisk(
dir = MILITARY_INSTALLATION_DISK_DIR,
) {
try {
const entry = JSON.parse(await fsp.readFile(militaryInstallationDiskPath(cacheKey, dir), 'utf8'));
const entry = JSON.parse(
await fsp.readFile(militaryInstallationDiskPath(cacheKey, dir), 'utf8'),
);
if (!militaryInstallationDiskFresh(entry, maxAgeMs)) return null;
return migrateMilitaryInstallationEntry(entry);
} catch {
@@ -117,7 +142,10 @@ async function writeMilitaryInstallationDisk(
await fsp.rename(temp, target);
return true;
} catch (err) {
console.warn('[Installations Proxy] disk cache write failed:', err?.message || err);
console.warn(
'[Installations Proxy] disk cache write failed:',
err?.message || err,
);
await fsp.rm(temp, { force: true }).catch(() => {});
return false;
}
@@ -131,4 +159,13 @@ function trimMilitaryInstallationCache() {
}
}
export { _militaryInstallationCache, trimMilitaryInstallationCache, writeMilitaryInstallationDisk, resolveMilitaryInstallationTier, readMilitaryInstallationDisk, migrateMilitaryInstallationEntry, militaryInstallationDiskFresh, militaryInstallationDiskPath };
export {
_militaryInstallationCache,
trimMilitaryInstallationCache,
writeMilitaryInstallationDisk,
resolveMilitaryInstallationTier,
readMilitaryInstallationDisk,
migrateMilitaryInstallationEntry,
militaryInstallationDiskFresh,
militaryInstallationDiskPath,
};
@@ -33,7 +33,11 @@ const MILITARY_INSTALLATION_ELEMENT_CAP = 700;
const MILITARY_INSTALLATION_DISK_TTL_MS = 30 * 86_400_000;
/** Disk-cache directory for mapped installation payloads. */
const MILITARY_INSTALLATION_DISK_DIR = path.join(process.cwd(), '.gev-cache', 'military-installations');
const MILITARY_INSTALLATION_DISK_DIR = path.join(
process.cwd(),
'.gev-cache',
'military-installations',
);
/**
* Cache-key grid step in degrees (~5.5 km).
@@ -45,4 +49,13 @@ const MILITARY_INSTALLATION_DISK_DIR = path.join(process.cwd(), '.gev-cache', 'm
*/
const MILITARY_INSTALLATION_BBOX_STEP_DEG = 0.05;
export { MILITARY_INSTALLATION_ELEMENT_CAP, MILITARY_INSTALLATION_MAX_RESPONSE_BYTES, MILITARY_INSTALLATION_DISK_TTL_MS, MILITARY_INSTALLATION_STALE_MS, MILITARY_INSTALLATION_CACHE_MS, MILITARY_INSTALLATION_DISK_DIR, MILITARY_INSTALLATION_MAX_CACHE, MILITARY_INSTALLATION_BBOX_STEP_DEG };
export {
MILITARY_INSTALLATION_ELEMENT_CAP,
MILITARY_INSTALLATION_MAX_RESPONSE_BYTES,
MILITARY_INSTALLATION_DISK_TTL_MS,
MILITARY_INSTALLATION_STALE_MS,
MILITARY_INSTALLATION_CACHE_MS,
MILITARY_INSTALLATION_DISK_DIR,
MILITARY_INSTALLATION_MAX_CACHE,
MILITARY_INSTALLATION_BBOX_STEP_DEG,
};
@@ -7,12 +7,17 @@ import { requiredFiniteQueryNumber } from '../common/query.js';
* @param {number} [stepDeg]
* @returns {{south:number, west:number, north:number, east:number}}
*/
function quantizeMilitaryInstallationBox(box, stepDeg = MILITARY_INSTALLATION_BBOX_STEP_DEG) {
function quantizeMilitaryInstallationBox(
box,
stepDeg = MILITARY_INSTALLATION_BBOX_STEP_DEG,
) {
// Round the ratio first: 29.9999/0.05 lands a hair under an exact grid line
// in binary floating point, which would otherwise snap a whole cell too far.
const snap = (value, grow) => {
const cells = Number((value / stepDeg).toFixed(9));
return Number(((grow > 0 ? Math.ceil(cells) : Math.floor(cells)) * stepDeg).toFixed(6));
return Number(
((grow > 0 ? Math.ceil(cells) : Math.floor(cells)) * stepDeg).toFixed(6),
);
};
return {
south: Math.max(-90, snap(box.south, -1)),
@@ -46,15 +51,35 @@ function validMilitaryInstallationBox(params) {
const north = requiredFiniteQueryNumber(params, 'north');
const east = requiredFiniteQueryNumber(params, 'east');
if (![south, west, north, east].every(Number.isFinite)) return null;
if (south < -90 || north > 90 || west < -180 || east > 180 || south >= north || west >= east) return null;
if (
south < -90 ||
north > 90 ||
west < -180 ||
east > 180 ||
south >= north ||
west >= east
)
return null;
if (north - south > 10 || east - west > 10) return null;
return { south, west, north, east };
}
/** Safe, evidence-based reason for an installation upstream failure. */
function militaryInstallationFailureReason(error) {
if (['rate_limited', 'timeout', 'query_failed'].includes(error?.installationReason)) return error.installationReason;
return ['AbortError', 'TimeoutError'].includes(error?.name) ? 'timeout' : 'unavailable';
if (
['rate_limited', 'timeout', 'query_failed'].includes(
error?.installationReason,
)
)
return error.installationReason;
return ['AbortError', 'TimeoutError'].includes(error?.name)
? 'timeout'
: 'unavailable';
}
export { validMilitaryInstallationBox, quantizeMilitaryInstallationBox, militaryInstallationCacheKey, militaryInstallationFailureReason };
export {
validMilitaryInstallationBox,
quantizeMilitaryInstallationBox,
militaryInstallationCacheKey,
militaryInstallationFailureReason,
};