Compare commits

..

No commits in common. "master" and "versions" have entirely different histories.

21 changed files with 12250 additions and 2536 deletions

18
.eslintrc.yml Normal file
View file

@ -0,0 +1,18 @@
env:
es2020: true
worker: true
extends: ['plugin:@typescript-eslint/recommended']
parser: '@typescript-eslint/parser'
parserOptions:
ecmaVersion: 2020
ecmaFeatures:
impliedStrict: true
sourceType: module
root: true
rules:
indent: [error, 2, { SwitchCase: 1 }]
semi: [error, always]
quotes: [error, single, avoid-escape]
no-trailing-spaces: [error]
no-unused-vars: [error, { argsIgnorePattern: ^_ }]
prefer-const: [error, { destructuring: all }]

View file

@ -24,9 +24,8 @@ jobs:
- name: Dependencies
run: npm ci
# TODO(AB): ESLint dependencies are broken. Replace with a better and less complex linter.
# - name: Lint
# run: npm run lint:ci
- name: Lint
run: npm run lint:ci
- name: Format
run: npm run format:ci

View file

@ -7,11 +7,10 @@ on:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v2
- name: wrangler publish
uses: cloudflare/wrangler-action@v3
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
environment: prod

1
.gitignore vendored
View file

@ -1,5 +1,4 @@
dist/
.wrangler/
# Autogenerated by esbuild.
workers-site/index.js
node_modules/

View file

@ -1,5 +1,4 @@
{
"plugins": ["prettier-plugin-toml"],
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,

View file

@ -1,4 +0,0 @@
Alexander Borsuk <me@alex.bio>
Roman Tsisyk <roman@organicmaps.app>
Viktor Havaka <viktor.govako@gmail.com>

View file

@ -1,5 +1,4 @@
Copyright 2021 Alexander Borsuk <me@alex.bio>
Copyright 2024 Organic Maps Contributors (see the CONTRIBUTORS file).
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View file

@ -12,11 +12,9 @@ npm i
## Development
Use `npx wrangler dev` for localhost development and for testing using Cloudflare dev tools.
Use `npx wrangler dev --local` for localhost development.
```
curl -H "X-OM-DataVersion: 241001" -H "X-OM-AppVersion: 2024.10.22-10-Google" -H 'Accept-Language: fr-FR' http://localhost:8787/maps
```
Use `npx wrangler dev` to test local changes using Cloudflare dev tools.
## Update node dependencies to their major versions

13937
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -7,25 +7,30 @@
"scripts": {
"build": "esbuild src/index.ts --bundle --outfile=dist/index.js",
"test": "jest",
"format": "prettier --write '{src,test}/**/*.{ts,tsx,json}' '*.json' '*.toml' '.github/**/*.yml'",
"format:ci": "prettier --check '{src,test}/**/*.{ts,tsx,json}' '*.json' '*.toml' '.github/**/*.yml'",
"lint": "eslint --fix --ext .tsx,.ts src/",
"lint:ci": "eslint --ext .tsx,.ts src/ --max-warnings 0",
"format": "prettier --write '{src,test}/**/*.{ts,tsx}' '*.json' '*.yml' '*.toml' '.github/**/*.yml'",
"format:ci": "prettier --check '{src,test}/**/*.{ts,tsx}' '*.json' '*.yml' '*.toml' '.github/**/*.yml'",
"upgrade": "npx npm-check-updates -u && npm install",
"logs": "npx wrangler tail --env prod --format json"
},
"author": "Alexander Borsuk <me@alex.bio>",
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20241205.0",
"@types/jest": "^29.5.14",
"esbuild": "^0.24.0",
"jest": "^29.7.0",
"jest-environment-miniflare": "^2.14.4",
"miniflare": "^3.20241205.0",
"npm-check-updates": "^17.1.11",
"prettier": "^3.4.2",
"prettier-plugin-toml": "^2.0.1",
"ts-jest": "^29.2.5",
"typescript": "^5.7.2",
"wrangler": "^3.95.0"
"@cloudflare/workers-types": "^4.20221111.1",
"@types/jest": "^29.2.5",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"esbuild": "^0.16.17",
"eslint": "^8.31.0",
"jest": "^29.3.1",
"jest-environment-miniflare": "^2.11.0",
"miniflare": "^2.11.0",
"npm-check-updates": "^16.6.2",
"prettier": "^2.8.2",
"prettier-plugin-toml": "^0.3.1",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4",
"wrangler": "^2.7.1"
}
}

View file

@ -1,6 +1,60 @@
export {};
import { getServersList } from './servers';
import { parseDataVersion, parseAppVersion } from './versions';
// TODO: Implement automated version checks from this metaserver script.
// It should check by cron if actual files are really available on all servers.
export const SERVER = {
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, 220912, 221029, 221119, 221216, 230121,
],
},
uk1: {
// Mythic Beasts VPS (London, UK) 200TB/mo.
url: 'https://cdn-uk1.organicmaps.app/',
dataVersions: [221119, 221216, 230121],
},
nl1: {
// // Mythic Beasts VPS (Amsterdam, NL) 200TB/mo.
url: 'https://cdn-nl1.organicmaps.app/',
dataVersions: [221119, 221216, 230121],
},
planet: {
// Hetzner BareMetal (Falkenstein, Germany) unmetered
url: 'https://cdn.organicmaps.app/',
dataVersions: [
220103, 220204, 220314, 220415, 220515, 220613, 220718, 220816, 220912, 221029, 221119, 221216, 230121,
],
},
fi1: {
// Hetzner Cloud (Helsinki, Finland), 20TB/mo
url: 'https://cdn-fi1.organicmaps.app/',
dataVersions: [221216, 230121],
},
de1: {
// Hetzner Cloud (Falkenstein, Germany), 20TB/mo
url: 'https://cdn-eu2.organicmaps.app/',
dataVersions: [221216, 230121],
},
de2: {
// Hetzner Cloud (Falkenstein, Germany), 20TB/mo
url: 'https://cdn-de2.organicmaps.app/',
dataVersions: [221119, 221216, 230121],
},
us2: {
// Hetzner Cloud (Asburn, US East), 20TB/mo
url: 'https://cdn-us2.organicmaps.app/',
dataVersions: [221216, 230121],
},
};
// Exported for tests.
export const DONATE_URL = 'https://organicmaps.app/donate/';
export const DONATE_URL_RU = 'https://organicmaps.app/ru/donate/';
// Main entry point.
addEventListener('fetch', (event) => {
@ -13,8 +67,107 @@ export async function handleRequest(request: Request) {
switch (pathname) {
case '/maps': // Public for map files.
case '/resources': // Public for resources.
case '/servers':
return getServersList(request);
case '/servers': {
// Private for map files.
let servers;
// Starting from 2021-09, our clients have 'X-OM-DataVersion' header with the value
// of their current maps data version, for example, "211022" (October 22, 2021).
// It is lowercased by Cloudflare.
const dataVersion = parseDataVersion(request.headers.get('x-om-dataversion'));
if (dataVersion === null) {
// Older clients download from the archive.
servers = [SERVER.backblaze];
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore workarounds error TS2339: Property 'continent' does not exist on type 'IncomingRequestCfProperties<unknown>'.
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].filter((server) =>
server.dataVersions.includes(dataVersion),
);
break;
default:
// Every other continent + Tor networks.
servers = [SERVER.planet, SERVER.uk1, SERVER.nl1, SERVER.fi1, SERVER.de1, SERVER.de2].filter((server) =>
server.dataVersions.includes(dataVersion),
);
// Only fallback to the archive in the US if nothing was found closer.
if (servers.length == 0 && SERVER.backblaze.dataVersions.includes(dataVersion)) {
servers = [SERVER.backblaze];
}
}
}
// Fallback to the planet with freshly generated/beta data.
if (servers.length == 0) {
servers = [SERVER.planet];
}
servers = servers.map((server) => server.url);
// Header "X-OM-AppVersion: 2022.09.22-3-Google" (lowercased by CF) is supported from August 23, 2022.
const appVersion = parseAppVersion(request.headers.get('x-om-appversion'));
if (!appVersion) {
// Old format for <220823
return new Response(JSON.stringify(servers), {
headers: { 'Content-Type': 'application/json' },
});
}
// New format for >=220823
const response: {
servers: string[];
settings?: {
DonateUrl?: string;
NY?: string;
};
} = {
servers: servers,
};
// Disable donates for reviewers for all app versions AFTER this one.
const lastApprovedAndReleasedGoogleAppVersionCode = 230125;
const lastApprovedAndReleasediOSAppVersionCode = 230125;
let donatesEnabled = true;
if (
appVersion.flavor === 'google' &&
((request.cf?.asOrganization || '').toLowerCase().includes('google') ||
appVersion.code > lastApprovedAndReleasedGoogleAppVersionCode)
) {
donatesEnabled = false;
} else if (appVersion.build === undefined) {
// Disable donates for older iOS versions without donates menu support.
donatesEnabled = false;
} else if (
(appVersion.flavor === 'ios' && (request.cf?.asOrganization || '').toLowerCase().includes('apple')) ||
appVersion.code > lastApprovedAndReleasediOSAppVersionCode
) {
donatesEnabled = false;
}
if (donatesEnabled) {
// To count enabled donations.
console.log('Donates enabled');
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore workarounds error TS2339: Property 'country' does not exist on type 'IncomingRequestCfProperties<unknown>'.
if (request.cf?.country == 'RU') {
response.settings = {
DonateUrl: DONATE_URL_RU,
NY: 'false', // Must be `string` instead of `bool`, otherwise clients will crash
};
} else {
response.settings = {
DonateUrl: DONATE_URL,
NY: 'false', // Must be `string` instead of `bool`, otherwise clients will crash
};
}
}
return new Response(JSON.stringify(response), {
headers: { 'Content-Type': 'application/json' },
});
}
}
return new Response('', { status: 404 });
}

View file

@ -1,44 +0,0 @@
{
"en": {
"placePagePrompt": "Organic Maps app is free for everyone, thanks to your donations. No ads. No trackers. Open source.",
"perMonth": "/month",
"perYear": "/year",
"otherAmount": "Other"
},
"de": {
"placePagePrompt": "Organic Maps ist dank deiner Spenden für alle kostenlos. Keine Werbung. Keine Tracker. Open Source.",
"perMonth": "/Monat",
"perYear": "/Jahr",
"otherAmount": "Andere"
},
"fr": {
"placePagePrompt": "L'application Organic Maps est gratuite pour tout le monde grâce à vos dons. Pas de publicité. Pas de trackers. Open-source.",
"perMonth": "/mois",
"perYear": "/an",
"otherAmount": "Autre"
},
"nl": {
"placePagePrompt": "De Organic Maps app is gratis voor iedereen dankzij jullie donaties. Geen advertenties. Geen trackers. Open-source.",
"perMonth": "/maand",
"perYear": "/jaar",
"otherAmount": "Ander"
},
"it": {
"placePagePrompt": "L'app Organic Maps è gratuita per tutti grazie alle vostre donazioni. Nessuna pubblicità. Nessun tracker. Open-source.",
"perMonth": "/mese",
"perYear": "/anno",
"otherAmount": "Altro"
},
"es": {
"placePagePrompt": "Organic Maps es gratis para todos gracias a sus donaciones. Sin anuncios. Sin rastreadores. Código abierto.",
"perMonth": "/mes",
"perYear": "/año",
"otherAmount": "Otro"
},
"pt": {
"placePagePrompt": "O app Organic Maps é gratuito para todos graças às suas doações. Sem anúncios. Sem rastreadores. Código aberto.",
"perMonth": "/mês",
"perYear": "/ano",
"otherAmount": "Outro"
}
}

View file

@ -1,15 +0,0 @@
import LOCALES_JSON from './locales.json';
export interface Locale {
placePagePrompt: string;
perMonth: string;
perYear: string;
otherAmount: string;
}
export interface Locales {
[key: string]: Locale;
}
const LOCALES = LOCALES_JSON as Locales;
export default LOCALES;

View file

@ -1,198 +0,0 @@
{
"FR": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"DE": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"NL": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"IT": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"ES": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"PT": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"BE": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"AT": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"LU": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"MC": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"AD": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"SM": [
{
"title": "4,99€$per_month",
"link": "https://donate.stripe.com/6oEg2912f2c81r200j"
},
{
"title": "34,99€$per_year",
"link": "https://donate.stripe.com/eVa2bjdP19EA0mY6oI"
},
{
"title": "$other",
"link": "https://donate.stripe.com/3csg29aCP0401r2dRj"
}
],
"GB": [
{
"title": "£4.99$per_month",
"link": "https://donate.stripe.com/8wMg29fX98Awd9K28u"
},
{
"title": "£34.99$per_year",
"link": "https://donate.stripe.com/eVabLT9yL2c89Xy7sP"
},
{
"title": "$other",
"link": "https://donate.stripe.com/9AQg29h1dcQMedO3cH"
}
],
"US": [
{
"title": "$5.49$per_month",
"link": "https://donate.stripe.com/00g3fncKXcQMedO5kL"
},
{
"title": "$36.99$per_year",
"link": "https://donate.stripe.com/bIY6rz5ivcQM9XyaF6"
},
{
"title": "$other",
"link": "https://donate.stripe.com/eVa8zH5ivbMIfhS6oS"
}
]
}

View file

@ -1,40 +0,0 @@
import LOCALES from './locales';
import PRODUCTS_JSON from './products.json';
export interface Product {
title: string;
link: string;
}
const PRODUCTS = PRODUCTS_JSON as Record<string, Product[]>;
export interface ProductsConfig {
placePagePrompt: string;
products: Product[];
}
export function getProducts(locale: string | null): ProductsConfig | undefined {
if (!locale) {
return undefined;
}
const parts = locale.split(/[-_]/);
const language = parts[0].toLowerCase();
const country = parts[1] ? parts[1].toUpperCase() : '';
const products = PRODUCTS[country];
const trans = LOCALES[language];
if (products === undefined || trans === undefined) {
return undefined;
}
return {
placePagePrompt: trans.placePagePrompt,
products: products.map((product) => ({
...product,
title: product.title
.replace('$other', trans.otherAmount)
.replace('$per_month', trans.perMonth)
.replace('$per_year', trans.perYear),
})),
};
}

View file

@ -1,239 +0,0 @@
import { parseDataVersion, parseAppVersion } from './versions';
import { getProducts, ProductsConfig } from './products';
export const DATA_VERSIONS = [
210529, //
210703,
210729,
210825,
211002,
211022,
211122,
220103,
220204,
220314,
220415,
220515,
220613,
220718,
220816,
220912,
221029,
221119,
221216,
230121,
230210,
230227,
230329,
230503,
230602,
230710,
230814,
230920,
231113,
231213,
240105,
240202,
240228,
240326,
240429,
240528,
240613,
240702,
240723,
240810,
240904,
241001,
241017,
241107,
241122,
250121,
250213,
250227,
];
const kUnlimited = 99999;
// TODO: Implement automated version checks from this metaserver script.
// It should check by cron if actual files are really available on all servers.
export const SERVER = {
backblaze: {
// BackBlaze + CloudFlare (US-West) unmetered.
url: 'https://cdn-us1.organicmaps.app/',
dataVersions: kUnlimited,
},
uk1: {
// Mythic Beasts VPS (London, UK) 200TB/mo.
url: 'https://cdn-uk1.organicmaps.app/',
dataVersions: 3,
},
nl1: {
// // Mythic Beasts VPS (Amsterdam, NL) 200TB/mo.
url: 'https://cdn-nl1.organicmaps.app/',
dataVersions: 3,
},
planet: {
// Hetzner BareMetal (Helsinki, Finland) unmetered
url: 'https://cdn.organicmaps.app/',
dataVersions: kUnlimited,
},
beta: {
// Alias of the planet above that is proxied via CF and with enabled /maps/ *.mwm caching,
// to speed-up downloads for beta testers.
url: 'https://cdn-beta.organicmaps.app/',
// Can have any non-publicly available maps data version.
dataVersions: [],
},
fi1: {
// Hetzner Cloud (Helsinki, Finland), 20TB/mo
url: 'https://cdn-fi1.organicmaps.app/',
dataVersions: 2,
},
de1: {
// Hetzner Cloud (Falkenstein, Germany), 20TB/mo
url: 'https://cdn-eu2.organicmaps.app/',
dataVersions: 2,
},
de2: {
// Hetzner Cloud (Falkenstein, Germany), 20TB/mo
url: 'https://cdn-de2.organicmaps.app/',
dataVersions: 3,
},
de3: {
// Hetzner Cloud (Nuremberg, Germany), 20TB/mo
url: 'https://cdn-de3.organicmaps.app/',
dataVersions: 4,
},
us_east1: {
// Hetzner Cloud (Ashburn, US East), 20TB/mo
url: 'https://cdn-us-east1.organicmaps.app/',
dataVersions: 3,
},
us_west1: {
// Hetzner Cloud (Hillsdate, US West), 20TB/mo
url: 'https://cdn-us-west1.organicmaps.app/',
dataVersions: 2,
},
vi1: {
// Vietnam, unlimited 1Gbps bandwidth
url: 'https://cdn-vi1.organicmaps.app/',
dataVersions: 5,
},
};
// Exported for tests.
export const DONATE_URL = 'https://organicmaps.app/donate/';
export async function getServersList(request: Request) {
// Private for map files.
let servers;
// Starting from 2021-09, our clients have 'X-OM-DataVersion' header with the value
// of their current maps data version, for example, "211022" (October 22, 2021).
// It is lowercased by Cloudflare.
const dataVersion = parseDataVersion(request.headers.get('x-om-dataversion'));
const abusedVersions = ['1.8.6-4-ios', '1.8.7-1-ios', '1.8.8-1-ios'];
if (dataVersion === null) {
// Older clients download from the archive.
servers = [SERVER.backblaze];
} else if (dataVersion == 240702 && abusedVersions.includes(request.headers.get('x-om-appversion') || 'unknown')) {
// Redirect https://apps.apple.com/us/app/mapxplorer-navigation-radar/id6463052823
// who abuses our servers to a slow download "trap" node.
return new Response('["https://cdn-fi2.organicmaps.app/"]', {
headers: { 'Content-Type': 'application/json' },
});
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore workarounds error TS2339: Property 'continent' does not exist on type 'IncomingRequestCfProperties<unknown>'.
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
servers = [SERVER.us_east1, SERVER.us_west1, SERVER.uk1, SERVER.nl1, SERVER.planet].filter((server) =>
DATA_VERSIONS.slice(-server.dataVersions).includes(dataVersion),
);
break;
case 'OC': // Oceania
servers = [SERVER.vi1, SERVER.us_east1, SERVER.us_west1, SERVER.planet].filter((server) =>
DATA_VERSIONS.slice(-server.dataVersions).includes(dataVersion),
);
break;
case 'AS': // Asia
servers = [SERVER.vi1, SERVER.uk1, SERVER.nl1, SERVER.planet].filter((server) =>
DATA_VERSIONS.slice(-server.dataVersions).includes(dataVersion),
);
break;
default:
// Every other continent + Tor networks.
servers = [SERVER.planet, SERVER.uk1, SERVER.nl1, SERVER.fi1, SERVER.de1, SERVER.de2, SERVER.de3].filter(
(server) => DATA_VERSIONS.slice(-server.dataVersions).includes(dataVersion),
);
// Only fallback to the archive in the US if nothing was found closer.
if (servers.length == 0 && DATA_VERSIONS.slice(-SERVER.backblaze.dataVersions).includes(dataVersion)) {
servers = [SERVER.backblaze];
}
}
}
// Fallback to the planet with freshly generated/beta data.
if (servers.length == 0) {
servers = [SERVER.beta, SERVER.planet];
}
servers = servers.map((server) => server.url);
// Header "X-OM-AppVersion: 2022.09.22-3-Google" (lowercased by CF) is supported from August 23, 2022.
const appVersion = parseAppVersion(request.headers.get('x-om-appversion'));
if (!appVersion) {
// Old format for <220823
return new Response(JSON.stringify(servers), {
headers: { 'Content-Type': 'application/json' },
});
}
// New format for >=220823
const response: {
servers: string[];
settings?: {
DonateUrl?: string;
NY?: string;
};
productsConfig?: ProductsConfig;
} = {
servers: servers,
};
// Disable donates for reviewers for all app versions AFTER this one.
const lastApprovedAndReleasedGoogleAppVersionCode = 250217;
const lastApprovedAndReleasediOSAppVersionCode = 250303;
let donatesEnabled = true;
if (
appVersion.flavor === 'google' &&
((typeof request.cf?.asOrganization === 'string' && request.cf?.asOrganization.toLowerCase().includes('google')) ||
appVersion.code > lastApprovedAndReleasedGoogleAppVersionCode)
) {
donatesEnabled = false;
} else if (appVersion.build === undefined) {
// Disable donates for older iOS versions without donates menu support.
donatesEnabled = false;
} else if (
appVersion.flavor === 'ios' &&
((typeof request.cf?.asOrganization === 'string' && request.cf?.asOrganization.toLowerCase().includes('apple')) ||
appVersion.code > lastApprovedAndReleasediOSAppVersionCode)
) {
donatesEnabled = false;
}
if (donatesEnabled) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
response.settings = {
DonateUrl: DONATE_URL,
NY: 'false', // Must be `string` instead of `bool`, otherwise clients will crash
};
if (appVersion.code >= 241022) {
const locale = request.headers.get('accept-language');
response.productsConfig = getProducts(locale);
}
}
return new Response(JSON.stringify(response), {
headers: { 'Content-Type': 'application/json' },
});
}

View file

@ -16,10 +16,7 @@ export function parseDataVersion(strDataVersion: string | null): number | null {
// 2022.11.20 for iOS versions released before November 21 (without donate menu)
// 2022.11.24-4-ios for newer iOS versions (with donate menu)
// 2022.12.24-10-Google for Android
// 2022.12.24-3-3f4ca43-Linux or 2022.12.24-3-3f4ca43-dirty-Linux for Linux
// 2022.12.24-3-3f4ca43-Darwin for Mac
const VERSION_RE =
/(?<year>\d{4})\.(?<month>\d{1,2})\.(?<day>\d{1,2})(?:$|-(?<build>[0-9]+)(?:-[0-9a-f]+)?(?:-dirty)?-(?<flavor>[A-Za-z3264]+))/;
const VERSION_RE = /(\d{4}).(\d{1,2}).(\d{1,2})(?:$|-(\d{1,9})(?:-([^-]+))?)/;
// Returns code like 221224 for both platforms, build and flavor for Android and newer iOS versions.
export function parseAppVersion(
versionName: string | null,
@ -29,32 +26,32 @@ export function parseAppVersion(
}
const m = versionName.match(VERSION_RE);
if (m === null || m.length < 4 || !m.groups) {
if (m === null || m.length < 4) {
return null;
}
const yyyy = parseInt(m.groups.year);
const yyyy = parseInt(m[1]);
if (Number.isNaN(yyyy) || yyyy > 2099 || yyyy < 2022) {
return null;
}
const mm = parseInt(m.groups.month);
const mm = parseInt(m[2]);
if (Number.isNaN(mm) || mm > 12 || mm < 1) {
return null;
}
const dd = parseInt(m.groups.day);
const dd = parseInt(m[3]);
if (Number.isNaN(dd) || dd > 31 || dd < 1) {
return null;
}
const code = parseInt(String(yyyy % 100) + String(mm).padStart(2, '0') + String(dd).padStart(2, '0'));
// Older iOS versions without donate button.
if (!m.groups.build) {
if (m[4] === undefined) {
return { code: code };
}
const buildNumber = parseInt(m.groups.build);
const buildNumber = parseInt(m[4]);
const build = Number.isNaN(buildNumber) ? 0 : buildNumber;
// 'ios' for iOS devices.
const flavor = (m.groups.flavor !== undefined && m.groups.flavor.toLowerCase()) || undefined;
const flavor = (m[5] !== undefined && m[5].toLowerCase()) || undefined;
return {
code: code,

View file

@ -1,14 +1,14 @@
import { describe, expect, test } from '@jest/globals';
import { getServersList, SERVER, DATA_VERSIONS, DONATE_URL } from '../src/servers';
import { handleRequest, SERVER, DONATE_URL, DONATE_URL_RU } from '../src/index';
const URL = 'https://worker/servers';
const LAST_DATA_VERSION = DATA_VERSIONS[DATA_VERSIONS.length - 1];
const LAST_DATA_VERSION = SERVER.planet.dataVersions[SERVER.planet.dataVersions.length - 1];
// Note: CF lowercases all headers.
describe('X-OM-DataVersion', () => {
test('no X-OM-DataVersion', async () => {
const req = new Request(URL);
const result = await getServersList(req);
const result = await handleRequest(req);
expect(result.status).toBe(200);
expect(JSON.parse(await result.text())).toEqual([SERVER.backblaze.url]);
});
@ -17,10 +17,10 @@ describe('X-OM-DataVersion', () => {
const server = SERVER.fi1;
let req = new Request(URL, {
headers: {
'X-OM-DataVersion': String(LAST_DATA_VERSION),
'X-OM-DataVersion': String(server.dataVersions[0]),
},
});
const result = await getServersList(req);
const result = await handleRequest(req);
expect(result.status).toBe(200);
expect(JSON.parse(await result.text())).toContain(server.url);
});
@ -31,16 +31,16 @@ describe('X-OM-DataVersion', () => {
'X-OM-DataVersion': '210000', // this version doesn't exist on servers
},
});
const result = await getServersList(req);
const result = await handleRequest(req);
expect(result.status).toBe(200);
expect(JSON.parse(await result.text())).toEqual([SERVER.beta.url, SERVER.planet.url]);
expect(JSON.parse(await result.text())).toEqual([SERVER.planet.url]);
});
});
describe('X-OM-AppVersion DonateUrl', () => {
test('Old versions without X-OM-AppVersion and old metaserver JSON format', async () => {
const req = new Request(URL);
const response = await getServersList(req);
const response = await handleRequest(req);
expect(response.status).toBe(200);
expect(JSON.parse(await response.text())).toEqual([SERVER.backblaze.url]);
});
@ -51,10 +51,10 @@ describe('X-OM-AppVersion DonateUrl', () => {
let req = new Request(URL, {
headers: {
'X-OM-AppVersion': '2022.08.23-1-Google',
'X-OM-DataVersion': String(LAST_DATA_VERSION),
'X-OM-DataVersion': String(server.dataVersions[0]),
},
});
const response = await getServersList(req);
const response = await handleRequest(req);
expect(response.status).toBe(200);
const result = JSON.parse(await response.text());
expect(result.servers).toBeDefined();
@ -69,25 +69,26 @@ describe('X-OM-AppVersion DonateUrl', () => {
let req = new Request(URL, {
headers: {
'X-OM-AppVersion': '2022.08.23-1-Google',
'X-OM-DataVersion': String(LAST_DATA_VERSION),
'X-OM-DataVersion': String(server.dataVersions[0]),
},
//@ts-ignore
cf: { country: 'RU' },
});
const response = await getServersList(req);
const response = await handleRequest(req);
expect(response.status).toBe(200);
const result = JSON.parse(await response.text());
expect(result.settings.DonateUrl).toBeDefined();
expect(result.settings.DonateUrl).toEqual(DONATE_URL_RU);
});
test('Older iOS versions with X-OM-AppVersion but without donates', async () => {
let req = new Request(URL, {
headers: {
'X-OM-AppVersion': '2022.11.20',
'X-OM-DataVersion': String(LAST_DATA_VERSION),
'X-OM-DataVersion': String(server.dataVersions[0]),
},
});
const response = await getServersList(req);
const response = await handleRequest(req);
expect(response.status).toBe(200);
const result = JSON.parse(await response.text());
expect(result.settings).not.toBeDefined();
@ -97,10 +98,10 @@ describe('X-OM-AppVersion DonateUrl', () => {
let req = new Request(URL, {
headers: {
'X-OM-AppVersion': '2022.11.20-4-ios',
'X-OM-DataVersion': String(LAST_DATA_VERSION),
'X-OM-DataVersion': String(server.dataVersions[0]),
},
});
const response = await getServersList(req);
const response = await handleRequest(req);
expect(response.status).toBe(200);
const result = JSON.parse(await response.text());
expect(result.settings.DonateUrl).toBeDefined();

View file

@ -1,14 +0,0 @@
import { describe, expect, test } from '@jest/globals';
import { getProducts } from '../src/products';
describe('getProducts', () => {
test('fr-FR', () => {
const fr_FR = getProducts('fr-FR');
expect(fr_FR).toBeDefined();
if (!fr_FR) return;
expect(fr_FR.placePagePrompt).toBe(
"L'application Organic Maps est gratuite pour tout le monde grâce à vos dons. Pas de publicité. Pas de trackers. Open-source.",
);
expect(fr_FR.products[fr_FR.products.length - 1].title).toEqual('Autre');
});
});

View file

@ -24,19 +24,12 @@ describe('parseAppVersion', () => {
'2022.08.01': { code: 220801 },
// Newer iOS releases with donate menu
'2022.11.25-5-ios': { code: 221125, build: 5, flavor: 'ios' },
// There were no such versions in production.
'2022.08.01-1': null,
'2022.08.01-1': { code: 220801, build: 1 },
'2022.08.01-1-Google': { code: 220801, build: 1, flavor: 'google' },
// -debug is ignored
'2022.08.01-1-Google-debug': { code: 220801, build: 1, flavor: 'google' },
// TODO: Fix regexp. Not it should not happen in production.
//'2022.08.01-1-fd-debug': { code: 220801, build: 1, flavor: 'fd' },
'2022.1.1-0': null,
'2099.12.31-999999999': null,
'2023.03.22-1-4fac32de-Linux': { code: 230322, build: 1, flavor: 'linux' },
'2023.03.22-1-4fac32de-dirty-Linux': { code: 230322, build: 1, flavor: 'linux' },
'2023.03.22-1-4fac32de-Darwin': { code: 230322, build: 1, flavor: 'darwin' },
'2023.03.22-1-4fac32de-dirty-Darwin': { code: 230322, build: 1, flavor: 'darwin' },
'2022.1.1-0': { code: 220101, build: 0 },
'2099.12.31-999999999': { code: 991231, build: 999999999 },
'2021.01.31-1': null,
'2100.01.31-1': null,
'2022.00.31-1': null,

View file

@ -11,7 +11,6 @@
"preserveConstEnums": true,
"sourceMap": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"types": ["@cloudflare/workers-types"]
},
"include": ["src/"],