Skip to content

Commit

Permalink
Matheusgr/fix loc ipcity (#972)
Browse files Browse the repository at this point in the history
* Fix location misencoded string

* Fix location misencoded string

* Update location.ts
  • Loading branch information
matheusgr authored Jan 21, 2025
1 parent e07bc1e commit a18d645
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions website/matchers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,25 @@ const matchLocation =
result &&= !target.country || target.country === source.country;
return result;
};

function fixEncoding(input: string): string {
// interpret the received ascii string (cf-headers) as UTF-8
try {
const utf8bytes = [...input].map((char) => char.charCodeAt(0));
return new TextDecoder("utf-8").decode(Uint8Array.from(utf8bytes));
} catch (_) {
return input;
}
}

const escaped = (
{ city, country, regionCode, coordinates }: MapLocation,
): MapLocation => {
return {
coordinates,
regionCode,
city: city ? decodeURIComponent(escape(city)) : city,
country: country ? decodeURIComponent(escape(country)) : country,
city: city ? fixEncoding(city) : city,
country: country ? fixEncoding(country) : country,
};
};
/**
Expand Down

0 comments on commit a18d645

Please sign in to comment.