diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..f7c3e62 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,31 @@ +name: Checks + +on: + workflow_dispatch: {} + pull_request: + types: + - opened + - synchronize + +jobs: + checks: + name: Checks + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install Node + uses: actions/setup-node@v2 + with: + node-version: 18 + + - name: Dependencies + run: npm ci + + - name: Lint + run: npm run lint:ci + + - name: Format + run: npm run format:ci diff --git a/package.json b/package.json index 0b82145..ff40085 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,10 @@ "scripts": { "build": "esbuild src/index.ts --bundle --outfile=dist/index.js", "test": "eslint src/**/*.ts && tsc --noEmit", - "format": "prettier --write 'src/**/*.{ts,tsx,json}'" + "lint": "eslint --fix --ext .tsx,.ts src/", + "lint:ci": "eslint --ext .tsx,.ts src/ --max-warnings 0", + "format": "prettier --write 'src/**/*.{ts,tsx}'", + "format:ci": "prettier --check 'src/**/*.{ts,tsx}'" }, "author": "Alexander Borsuk ", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index 678136e..f5744be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export { }; +export {}; // TODO: Implement automated version checks from this metaserver script. // It should check by cron if actual files are really available on all servers. @@ -6,38 +6,41 @@ 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] + 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] + dataVersions: [220613, 220718, 220816], }, nl1: { // // Mythic Beasts VPS (Amsterdam, NL) 200TB/mo. url: 'https://cdn-nl1.organicmaps.app/', - dataVersions: [220613, 220718, 220816] + dataVersions: [220613, 220718, 220816], }, planet: { // Hetzner BareMetal (Falkenstein, Germany) unmetered url: 'https://cdn.organicmaps.app/', - dataVersions: [220103, 220204, 220314, 220415, 220515, 220613, 220718, 220816] + 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] + dataVersions: [220718, 220816], }, de1: { // Hetzner Cloud (Falkenstein, Germany), 20TB/mo url: 'https://cdn-eu2.organicmaps.app/', - dataVersions: [220718, 220816] + dataVersions: [220718, 220816], }, us2: { // Hetzner Cloud (Asburn, US East), 20TB/mo url: 'https://cdn-us2.organicmaps.app/', - dataVersions: [220718, 220816] - } + dataVersions: [220718, 220816], + }, }; // Main entry point. @@ -53,8 +56,9 @@ function extractDataVersion(request: Request): number { const strDataVersion = request.headers.get('x-om-dataversion'); if (strDataVersion) { const dataVersion = parseInt(strDataVersion); - if (!Number.isNaN(dataVersion) && dataVersion >= 210000 && dataVersion <= 500000) + if (!Number.isNaN(dataVersion) && dataVersion >= 210000 && dataVersion <= 500000) { return dataVersion; + } } return 0; } @@ -63,39 +67,42 @@ async function handleRequest(request: Request) { const { pathname } = new URL(request.url); switch (pathname) { - case '/maps': // Public for map files. - case '/resources': // Public for resources. - case '/servers': { // Private for map files. + case '/maps': // Public for map files. + case '/resources': // Public for resources. + case '/servers': { + // Private for map files. let servers; const dataVersion = extractDataVersion(request); 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] - .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] - .filter(server => server.dataVersions.includes(dataVersion)) - if (servers.length == 0) { - if (SERVER.backblaze.dataVersions.includes(dataVersion)) { - servers = [SERVER.backblaze] - } else { - servers = [SERVER.planet] + 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].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].filter((server) => + server.dataVersions.includes(dataVersion), + ); + if (servers.length == 0) { + if (SERVER.backblaze.dataVersions.includes(dataVersion)) { + servers = [SERVER.backblaze]; + } else { + servers = [SERVER.planet]; + } + } + } - let donation - - const response = servers.map(server => server.url) + const response = servers.map((server) => server.url); return new Response(JSON.stringify(response), { headers: { 'Content-Type': 'application/json' }, });