Skip to content

Commit

Permalink
Update car.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pakastin committed Jun 10, 2024
1 parent c1c04d5 commit 4660adb
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions car.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,31 @@

const host = await fastestPing(
[
{ host: "https://car-hel1.pakastin.fi", city: "Helsinki, Finland" },
{ host: "https://car-nbg1.pakastin.fi", city: "Nuremberg, Germany" },
{ host: "https://car-fsn1.pakastin.fi", city: "Falkenstein, Germany" },
{ host: "https://car-hil1.pakastin.fi", city: "Hillsboro, Oregon" },
{ host: "https://car-ash1.pakastin.fi", city: "Ashburn, Virginia" },
{
host: "https://car-hel1.pakastin.fi",
city: "Helsinki, Finland",
continent: "Europe",
},
{
host: "https://car-nbg1.pakastin.fi",
city: "Nuremberg, Germany",
continent: "Europe",
},
{
host: "https://car-fsn1.pakastin.fi",
city: "Falkenstein, Germany",
continent: "Europe",
},
{
host: "https://car-hil1.pakastin.fi",
city: "Hillsboro, Oregon",
continent: "America",
},
{
host: "https://car-ash1.pakastin.fi",
city: "Ashburn, Virginia",
continent: "America",
},
],
{ host: "https://car.pakastin.fi", city: "Cloudflare (fallback)" }
);
Expand Down Expand Up @@ -546,6 +566,24 @@
async function fastestPing(hosts, fallback) {
let result;

try {
const continent = Intl.DateTimeFormat()
.resolvedOptions()
.timeZone.split("/")[0];

hosts.sort((a, b) => {
if (continent === a.continent && continent !== b.continent) {
return -1;
} else if (continent === b.continent && continent !== a.continent) {
return 1;
} else {
return 0;
}
});
} catch (err) {
console.error(err);
}

for (const host of hosts) {
const abortController = new AbortController();
const abortTimeout = setTimeout(() => abortController.abort(), 5000);
Expand Down

0 comments on commit 4660adb

Please sign in to comment.