Files
MyIP/server/globalping-inventory.js
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
common/ had become the backend's library: 30 of its 41 modules were
imported by nothing in the frontend, so the name no longer said what
belonged there. It now holds only what the frontend or the build imports
too; backend-only code moved to server/, flat except for datasets/ (the
dataset engine and its readers). The *-db/ dataset directories stay in
common/: self-hosting docs and Docker volumes mount those paths.

frontend/utils/ keeps its app-wide modules at the root and gains ip/
(IP / ASN shaping and validation), report/ (the report pipeline) and
features/ (the pure layer of a single feature). Paths only — no logic
changes. The Dockerfile copies server/, and the AGENTS.md files say where
a new module goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:10:02 +08:00

36 lines
1.5 KiB
JavaScript

// Pure aggregation for the Globalping probe inventory: turns the raw
// /v1/probes payload (one entry per online probe) into the compact shape
// the frontend country pickers consume — which countries have a probe at
// all, grouped into the five continent buckets the UI displays (NA + SA
// merge into "americas"). Serving this from our backend keeps the multi-
// hundred-KB probe list off the visitor's connection.
// Globalping continent codes → our display buckets.
const CONTINENT_BUCKETS = { AS: 'asia', EU: 'europe', AF: 'africa', NA: 'americas', SA: 'americas', OC: 'oceania' };
const BUCKET_ORDER = ['asia', 'europe', 'africa', 'americas', 'oceania'];
// probes: raw array from Globalping. Returns
// { countries: [cc], continents: [{ key, countries: [cc] }] } with
// continents in display order and country codes sorted alphabetically.
export const buildProbeInventory = (probes) => {
const countries = new Set();
const buckets = new Map();
for (const probe of (Array.isArray(probes) ? probes : [])) {
const cc = probe?.location?.country;
if (!cc) continue;
countries.add(cc);
const bucket = CONTINENT_BUCKETS[probe?.location?.continent];
if (!bucket) continue;
if (!buckets.has(bucket)) buckets.set(bucket, new Set());
buckets.get(bucket).add(cc);
}
return {
countries: [...countries].sort(),
continents: BUCKET_ORDER
.filter((key) => buckets.has(key))
.map((key) => ({ key, countries: [...buckets.get(key)].sort() })),
};
};