Files
gods-eye-view/src/layers/rail/source.js
T
Denis Groshev 830c080803 feat(rail): add a Rail layer with keyless live trains
Live trains from 30 keyless operator feeds: GPS positions from 29 and
Irish Rail's station reports, shown as the operators publish them. 28
work out of the box; Renfe long and medium distance and KTMB appear once
their pack is built, because their vehicles name no route.

Packs are opt-in (scripts/build-rail-paths.mjs): the operator's own
route lines, stop names, next stops and trip-update delays for live
trains. Nothing downloads when the app starts, no train is placed from a
timetable, and no request carries a deployer's key.
2026-11-11 16:06:22 +01:00

69 lines
2.4 KiB
JavaScript

/** Request rail snapshots, path packs, timetables and trip updates through caller-owned transport. */
export function createRailSource({
fetchImpl = (...args) => fetch(...args),
} = {}) {
/**
* GET one per-feed JSON resource and hand back a response-like object whose
* body read still honours the caller's abort.
* @param {string} route `trains`, `paths`, `schedule` or `updates`.
* @param {string} feedId
* @param {{signal?: AbortSignal}} [options]
*/
function requestFeedJson(route, feedId, { signal } = {}) {
signal?.throwIfAborted();
if (typeof feedId !== 'string' || !feedId || feedId.length > 160)
throw new TypeError('A rail feed identifier is required');
return Promise.resolve(
fetchImpl(`/api/rail/${route}/${encodeURIComponent(feedId)}`, {
signal,
headers: { Accept: 'application/json' },
}),
).then((response) => {
signal?.throwIfAborted();
return {
ok: response.ok,
status: response.status,
headers: response.headers,
async json() {
const body = await response.json();
signal?.throwIfAborted();
return body;
},
};
});
}
return {
/** Read the public catalog of enabled feeds. */
async getFeeds({ signal } = {}) {
signal?.throwIfAborted();
const response = await fetchImpl('/api/rail/feeds', {
signal,
headers: { Accept: 'application/json' },
});
if (!response.ok) throw new Error('Rail feeds HTTP ' + response.status);
const body = await response.json();
signal?.throwIfAborted();
if (!Array.isArray(body?.feeds))
throw new Error('Malformed rail feed catalog');
return body.feeds;
},
/** The feed's current train-only snapshot. */
requestSnapshot(feedId, options) {
return requestFeedJson('trains', feedId, options);
},
/** The feed's path pack (route shapes); 404 when none is published. */
requestPaths(feedId, options) {
return requestFeedJson('paths', feedId, options);
},
/** The feed's timetable pack; 404 when none is published. */
requestSchedule(feedId, options) {
return requestFeedJson('schedule', feedId, options);
},
/** The feed's live trip updates (delays); 404 for a feed without them. */
requestUpdates(feedId, options) {
return requestFeedJson('updates', feedId, options);
},
};
}