From 4b0118dce1555c61667a678032cdbb6ef8858edf Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 27 Aug 2023 22:36:37 -0700 Subject: [PATCH] Ensure region code includes country. --- src/lib/detect.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 43dac649..86f812bd 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -65,19 +65,27 @@ export async function getLocation(ip, req) { // Cloudflare headers if (req.headers['cf-ipcountry']) { + const country = safeDecodeURIComponent(req.headers['cf-ipcountry']); + const subdivision1 = safeDecodeURIComponent(req.headers['cf-region-code']); + const city = safeDecodeURIComponent(req.headers['cf-ipcity']); + return { - country: safeDecodeURIComponent(req.headers['cf-ipcountry']), - subdivision1: safeDecodeURIComponent(req.headers['cf-region-code']), - city: safeDecodeURIComponent(req.headers['cf-ipcity']), + country, + subdivision1: subdivision1.includes('-') ? subdivision1 : `${country}-${subdivision1}`, + city, }; } // Vercel headers if (req.headers['x-vercel-ip-country']) { + const country = safeDecodeURIComponent(req.headers['x-vercel-ip-country']); + const subdivision1 = safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']); + const city = safeDecodeURIComponent(req.headers['x-vercel-ip-city']); + return { - country: safeDecodeURIComponent(req.headers['x-vercel-ip-country']), - subdivision1: safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']), - city: safeDecodeURIComponent(req.headers['x-vercel-ip-city']), + country, + subdivision1: subdivision1.includes('-') ? subdivision1 : `${country}-${subdivision1}`, + city, }; }