mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 03:32:19 +00:00
* Show JTWC warnings in Cyclone advisories Cyclone advisories now also read the Joint Typhoon Warning Center (keyless, public domain) for the western North Pacific, North Indian Ocean and Southern Hemisphere, beside NHC and CPHC. - server/providers/jtwc.js reads JTWC's warnings feed, each storm's JMV 3.0 warning (.tcw) and its overlay (.kmz). /api/cyclones merges them with NHC's: one storm per id, NHC's advisory where NHC lists the storm. A failed JTWC read keeps its last storms, marked stale, and never hides NHC's. - Storms carry an agency. A JTWC warning has no cone; its 34-knot wind danger area is drawn as its own dashed area and named for the selected storm. - A JTWC storm's card notes that the regional centre's warning is official and that JTWC winds are 1-minute averages. Source and coverage wording names both agencies. - Each storm is served while its own advisory is usable (12 hours for NHC, 13 for JTWC). * Keep one JTWC failure from affecting other storms or NHC - A storm whose JTWC warning cannot be read or parsed is left out and the others are shown; the read fails only when every listed storm's does. - JTWC is read under its own 8-second deadline. A failed or slow JTWC read keeps JTWC's last good storms and never delays, fails or stales the NHC answer. - Danger-swath longitudes past ±180 (a ring crossing the antimeridian) are wrapped into -180..180 instead of dropping the ring. * Add the changelog entry for JTWC cyclones
275 lines
8.9 KiB
JavaScript
275 lines
8.9 KiB
JavaScript
import { readResponseJsonCapped } from '../../sources/httpBody.js';
|
|
|
|
// Shared with the AI tools, so no i18n import: these errors stay English here
|
|
// and the cyclones layer translates them where it shows them (translateKnown).
|
|
const msg = (text) => text;
|
|
|
|
export const CYCLONE_RESPONSE_LIMIT = 4 * 1024 * 1024;
|
|
const malformed = () => new Error(msg('Malformed cyclone snapshot'));
|
|
const text = (value, max = 160) => {
|
|
if (
|
|
typeof value !== 'string' ||
|
|
!value.trim() ||
|
|
value.length > max ||
|
|
/[\u0000-\u001f<>]/.test(value)
|
|
)
|
|
throw malformed();
|
|
return value;
|
|
};
|
|
const number = (value, low, high) => {
|
|
if (value === null) return null;
|
|
if (!Number.isFinite(value) || value < low || value > high) throw malformed();
|
|
return value;
|
|
};
|
|
const time = (value) => {
|
|
if (
|
|
typeof value !== 'string' ||
|
|
!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(value) ||
|
|
!Number.isFinite(Date.parse(value)) ||
|
|
new Date(value).toISOString() !== value
|
|
)
|
|
throw malformed();
|
|
return value;
|
|
};
|
|
const advisory = (value) => {
|
|
if (typeof value !== 'string' || !/^(?:0|[1-9]\d{0,2})[A-Z]?$/.test(value))
|
|
throw malformed();
|
|
return value;
|
|
};
|
|
const position = (value) => {
|
|
if (!value || value.longitude === null || value.latitude === null)
|
|
throw malformed();
|
|
return {
|
|
longitude: number(value.longitude, -180, 180),
|
|
latitude: number(value.latitude, -90, 90),
|
|
};
|
|
};
|
|
|
|
/** The agencies whose advisories the snapshot carries. */
|
|
export const CYCLONE_AGENCIES = Object.freeze(['nhc', 'jtwc']);
|
|
// Each agency's storm ids, advisory links and outlook links.
|
|
const AGENCY_RULES = Object.freeze({
|
|
nhc: {
|
|
id: /^(?:al|ep|cp)\d{6}$/,
|
|
origin: 'https://www.nhc.noaa.gov',
|
|
advisory: /^\/text\/[A-Z0-9]+\.shtml$/,
|
|
outlook: /^\/gtwo\.php\?basin=(?:atlc|epac|cpac)&fdays=7$/,
|
|
},
|
|
jtwc: {
|
|
id: /^(?:wp|io|sh|ep|cp)\d{6}$/,
|
|
origin: 'https://www.metoc.navy.mil',
|
|
advisory: /^\/jtwc\/products\/[a-z]{2}\d{4}web\.txt$/,
|
|
outlook: /^\/jtwc\/products\/(?:abpw|abio)web\.txt$/,
|
|
},
|
|
});
|
|
|
|
function officialLink(value, rules, outlook = false) {
|
|
if (value === null) return null;
|
|
const url = new URL(text(value, 256));
|
|
if (url.origin !== rules.origin || url.username || url.password || url.hash)
|
|
throw malformed();
|
|
if (
|
|
outlook
|
|
? !rules.outlook.test(url.pathname + url.search)
|
|
: url.search || !rules.advisory.test(url.pathname)
|
|
)
|
|
throw malformed();
|
|
return url.href;
|
|
}
|
|
|
|
function geometry(value, kind, budget) {
|
|
if (value === null) return null;
|
|
const allowed =
|
|
kind === 'track'
|
|
? ['LineString', 'MultiLineString']
|
|
: ['Polygon', 'MultiPolygon'];
|
|
if (!allowed.includes(value?.type)) throw malformed();
|
|
const point = (pair) => {
|
|
if (
|
|
!Array.isArray(pair) ||
|
|
pair.length !== 2 ||
|
|
pair.some((v) => typeof v !== 'number')
|
|
)
|
|
throw malformed();
|
|
position({ longitude: pair[0], latitude: pair[1] });
|
|
if (++budget.coordinates > 25_000) throw malformed();
|
|
return [...pair];
|
|
};
|
|
const list = (items, read, min = 1, max = 128) => {
|
|
if (!Array.isArray(items) || items.length < min || items.length > max)
|
|
throw malformed();
|
|
return items.map(read);
|
|
};
|
|
const line = (pairs) => list(pairs, point, 2, 10_000);
|
|
const ring = (pairs) => {
|
|
const result = list(pairs, point, 4, 10_000);
|
|
if (result[0][0] !== result.at(-1)[0] || result[0][1] !== result.at(-1)[1])
|
|
throw malformed();
|
|
return result;
|
|
};
|
|
const polygon = (rings) => list(rings, ring);
|
|
const coordinates =
|
|
value.type === 'LineString'
|
|
? line(value.coordinates)
|
|
: value.type === 'MultiLineString'
|
|
? list(value.coordinates, line)
|
|
: value.type === 'Polygon'
|
|
? polygon(value.coordinates)
|
|
: list(value.coordinates, polygon);
|
|
return { type: value.type, coordinates };
|
|
}
|
|
|
|
/** Project only the bounded status/geometry contract; never ingest GeoJSON properties or URLs. */
|
|
export function validateCycloneSnapshot(value) {
|
|
if (
|
|
!value ||
|
|
value.schemaVersion !== 1 ||
|
|
typeof value.stale !== 'boolean' ||
|
|
typeof value.unavailable !== 'boolean' ||
|
|
!Array.isArray(value.storms) ||
|
|
value.storms.length > 32
|
|
)
|
|
throw malformed();
|
|
const seen = new Set(),
|
|
budget = { coordinates: 0, points: 0 };
|
|
const storms = value.storms.map((raw) => {
|
|
// A snapshot from before agencies carries NHC storms only.
|
|
const agency = raw?.agency === undefined ? 'nhc' : raw.agency;
|
|
const rules = Object.hasOwn(AGENCY_RULES, agency)
|
|
? AGENCY_RULES[agency]
|
|
: null;
|
|
if (
|
|
!rules ||
|
|
!rules.id.test(raw?.id) ||
|
|
seen.has(raw.id) ||
|
|
!['current', 'pending', 'unavailable'].includes(raw.geometryStatus)
|
|
)
|
|
throw malformed();
|
|
seen.add(raw.id);
|
|
const advisoryNumber = advisory(raw.advisoryNumber);
|
|
const geometryAdvisoryNumber =
|
|
raw.geometryAdvisoryNumber === null
|
|
? null
|
|
: advisory(raw.geometryAdvisoryNumber);
|
|
if (
|
|
raw.geometryStatus === 'current' &&
|
|
geometryAdvisoryNumber !== advisoryNumber
|
|
)
|
|
throw malformed();
|
|
if (
|
|
!Array.isArray(raw.forecastPoints) ||
|
|
(budget.points += raw.forecastPoints.length) > 500
|
|
)
|
|
throw malformed();
|
|
const taus = new Set();
|
|
const forecastPoints = raw.forecastPoints.map((point) => {
|
|
const tauHours = number(point.tauHours, 0, 168);
|
|
if (tauHours === null || taus.has(tauHours)) throw malformed();
|
|
taus.add(tauHours);
|
|
if (++budget.coordinates > 25_000) throw malformed();
|
|
return {
|
|
position: position(point.position),
|
|
tauHours,
|
|
windKt: number(point.windKt, 0, 300),
|
|
gustKt: number(point.gustKt, 0, 350),
|
|
};
|
|
});
|
|
const track = geometry(raw.track, 'track', budget),
|
|
cone = geometry(raw.cone, 'cone', budget),
|
|
dangerArea = geometry(raw.dangerArea ?? null, 'cone', budget);
|
|
// NHC draws a cone of centre-track uncertainty; JTWC publishes none, but
|
|
// a 34-knot wind danger area, which is never a cone.
|
|
const area = agency === 'nhc' ? cone : dangerArea;
|
|
if ((agency === 'nhc' && dangerArea) || (agency === 'jtwc' && cone))
|
|
throw malformed();
|
|
if (
|
|
raw.geometryStatus !== 'current' &&
|
|
(track || area || forecastPoints.length)
|
|
)
|
|
throw malformed();
|
|
if (
|
|
raw.geometryStatus === 'current' &&
|
|
(!track || !area || !forecastPoints.length)
|
|
)
|
|
throw malformed();
|
|
if (raw.basin !== raw.id.slice(0, 2).toUpperCase()) throw malformed();
|
|
return {
|
|
id: raw.id,
|
|
agency,
|
|
name: text(raw.name, 80),
|
|
classification: text(raw.classification, 16),
|
|
basin: raw.basin,
|
|
position: position(raw.position),
|
|
positionAt: time(raw.positionAt),
|
|
advisoryNumber,
|
|
issuedAt: time(raw.issuedAt),
|
|
windKt: number(raw.windKt, 0, 300),
|
|
pressureHpa: number(raw.pressureHpa, 800, 1100),
|
|
movement: {
|
|
directionDegrees: number(raw.movement?.directionDegrees, 0, 360),
|
|
speedKt: number(raw.movement?.speedKt, 0, 200),
|
|
},
|
|
advisoryUrl: officialLink(raw.advisoryUrl, rules),
|
|
outlookUrl: officialLink(raw.outlookUrl, rules, true),
|
|
geometryStatus: raw.geometryStatus,
|
|
geometryAdvisoryNumber,
|
|
forecastPoints,
|
|
track,
|
|
cone,
|
|
dangerArea,
|
|
};
|
|
});
|
|
if (value.unavailable && storms.length) throw malformed();
|
|
return {
|
|
schemaVersion: 1,
|
|
source: text(value.source),
|
|
attribution: text(value.attribution, 240),
|
|
coverage: text(value.coverage, 240),
|
|
fetchedAt: number(value.fetchedAt, 0, Number.MAX_SAFE_INTEGER),
|
|
stale: value.stale,
|
|
unavailable: value.unavailable,
|
|
reason: value.reason === null ? null : text(value.reason, 240),
|
|
storms,
|
|
};
|
|
}
|
|
|
|
/** Lazy same-origin acquisition; source owns only its deadline and cancellation. */
|
|
export function createCycloneSource({
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
timeoutMs = 15_000,
|
|
} = {}) {
|
|
return {
|
|
async getSnapshot({ signal } = {}) {
|
|
const controller = new AbortController();
|
|
const abort = () => controller.abort(signal.reason);
|
|
signal?.addEventListener('abort', abort, { once: true });
|
|
const timer = setTimeout(
|
|
() => controller.abort(new Error(msg('Cyclone request timed out'))),
|
|
timeoutMs,
|
|
);
|
|
try {
|
|
signal?.throwIfAborted();
|
|
const response = await fetchImpl('/api/cyclones', {
|
|
signal: controller.signal,
|
|
cache: 'no-store',
|
|
redirect: 'error',
|
|
});
|
|
if (!response.ok) {
|
|
await response.body?.cancel();
|
|
throw new Error(`Cyclone HTTP ${response.status}`);
|
|
}
|
|
const result = await readResponseJsonCapped(
|
|
response,
|
|
CYCLONE_RESPONSE_LIMIT,
|
|
controller.signal,
|
|
);
|
|
controller.signal.throwIfAborted();
|
|
return validateCycloneSnapshot(result);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
signal?.removeEventListener('abort', abort);
|
|
}
|
|
},
|
|
};
|
|
}
|