From 46544dd96a0be596fbfd5a2b681ef235eef453b7 Mon Sep 17 00:00:00 2001 From: Roman Tsisyk Date: Thu, 1 Sep 2022 15:53:44 +0300 Subject: [PATCH] Handle old versions properly --- src/index.ts | 77 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/src/index.ts b/src/index.ts index d592bc0..678136e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,17 +2,42 @@ export { }; // TODO: Implement automated version checks from this metaserver script. // It should check by cron if actual files are really available on all servers. -const LAST_AVAILABLE_VERSION = 220103; -const BEFORE_LAST_AVAILABLE_VERSION = 220204; - const SERVER = { - backblaze: 'https://cdn-us1.organicmaps.app/', // BackBlaze + CloudFlare (US-West) unmetered. - uk1: 'https://cdn-uk1.organicmaps.app/', // Mythic Beasts VPS (London, UK) 200TB/mo. - nl1: 'https://cdn-nl1.organicmaps.app/', // Mythic Beasts VPS (Amsterdam, NL) 200TB/mo. - planet: 'https://cdn.organicmaps.app/', // Hetzner BareMetal (Falkenstein, Germany) unmetered - fi1: 'https://cdn-fi1.organicmaps.app/', // Hetzner Cloud (Helsinki, Finland), 20TB/mo - de1: 'https://cdn-eu2.organicmaps.app/', // Hetzner Cloud (Falkenstein, Germany), 20TB/mo - us2: 'https://cdn-us2.organicmaps.app/', // Hetzner Cloud (Asburn, US East), 20TB/mo + backblaze: { + // BackBlaze + CloudFlare (US-West) unmetered. + url: 'https://cdn-us1.organicmaps.app/', + dataVersions: [210529, 210703, 210729, 210825, 211002, 211022, 211122, 220103, 220204, 220314, 220415, 220515, 220613, 220718, 220816] + }, + uk1: { + // Mythic Beasts VPS (London, UK) 200TB/mo. + url: 'https://cdn-uk1.organicmaps.app/', + dataVersions: [220613, 220718, 220816] + }, + nl1: { + // // Mythic Beasts VPS (Amsterdam, NL) 200TB/mo. + url: 'https://cdn-nl1.organicmaps.app/', + dataVersions: [220613, 220718, 220816] + }, + planet: { + // Hetzner BareMetal (Falkenstein, Germany) unmetered + url: 'https://cdn.organicmaps.app/', + dataVersions: [220103, 220204, 220314, 220415, 220515, 220613, 220718, 220816] + }, + fi1: { + // Hetzner Cloud (Helsinki, Finland), 20TB/mo + url: 'https://cdn-fi1.organicmaps.app/', + dataVersions: [220718, 220816] + }, + de1: { + // Hetzner Cloud (Falkenstein, Germany), 20TB/mo + url: 'https://cdn-eu2.organicmaps.app/', + dataVersions: [220718, 220816] + }, + us2: { + // Hetzner Cloud (Asburn, US East), 20TB/mo + url: 'https://cdn-us2.organicmaps.app/', + dataVersions: [220718, 220816] + } }; // Main entry point. @@ -24,7 +49,7 @@ addEventListener('fetch', (event) => { // of their current maps data version, for example, "211022" (October 22, 2021). // It is lowercased by Cloudflare. // Returns 0 if data version is absent or invalid, or a valid integer version. -function extractDataVersion(request: Request): Number { +function extractDataVersion(request: Request): number { const strDataVersion = request.headers.get('x-om-dataversion'); if (strDataVersion) { const dataVersion = parseInt(strDataVersion); @@ -43,23 +68,35 @@ async function handleRequest(request: Request) { case '/servers': { // Private for map files. let servers; const dataVersion = extractDataVersion(request); - if (dataVersion < LAST_AVAILABLE_VERSION) { - // Old maps versions are available only on BackBlaze. - servers = [SERVER.backblaze]; - } else if (dataVersion < BEFORE_LAST_AVAILABLE_VERSION) { - // Some servers have one additional version available. - servers = [SERVER.uk1, SERVER.nl1, SERVER.backblaze]; + if (dataVersion == 0) { + servers = [SERVER.backblaze] } else switch (request.cf?.continent) { // See https://developers.cloudflare.com/firewall/cf-firewall-language/fields for a list of all continents. case 'NA': // North America case 'SA': // South America case 'OC': // Oceania - servers = [SERVER.backblaze, SERVER.us2, SERVER.uk1, SERVER.nl1, SERVER.planet]; + servers = [SERVER.backblaze, SERVER.us2, SERVER.uk1, SERVER.nl1, SERVER.planet] + .filter(server => server.dataVersions.includes(dataVersion)) + if (servers.length == 0) { + servers = [SERVER.planet] + } break; default: // Every other continent + Tor networks. - servers = [SERVER.planet, SERVER.uk1, SERVER.nl1, SERVER.fi1, SERVER.de1]; + servers = [SERVER.planet, SERVER.uk1, SERVER.nl1, SERVER.fi1, SERVER.de1] + .filter(server => server.dataVersions.includes(dataVersion)) + if (servers.length == 0) { + if (SERVER.backblaze.dataVersions.includes(dataVersion)) { + servers = [SERVER.backblaze] + } else { + servers = [SERVER.planet] + } + } } - return new Response(JSON.stringify(servers), { + + let donation + + const response = servers.map(server => server.url) + return new Response(JSON.stringify(response), { headers: { 'Content-Type': 'application/json' }, }); }