Skip to content

Commit 64835a2

Browse files
Copilotsensslen
andcommitted
Implement URL-safe Base64 encoding and optimize state initialization
Co-authored-by: sensslen <3428860+sensslen@users.noreply.github.com>
1 parent 330d829 commit 64835a2

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/App.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ i18n.use(initReactI18next).init({
2323

2424
const App: React.FC = () => {
2525
// Initialize URL from query parameter at component creation
26-
const getInitialUrl = () => {
26+
// Using lazy initialization to avoid calling getInitialUrl on every render
27+
const [url, setUrl] = useState<string>(() => {
2728
const params = new URLSearchParams(window.location.search);
2829
const urlParam = params.get('url');
2930
if (urlParam) {
@@ -41,9 +42,7 @@ const App: React.FC = () => {
4142
}
4243
}
4344
return '';
44-
};
45-
46-
const [url, setUrl] = useState<string>(getInitialUrl);
45+
});
4746
const [error, setError] = useState<string | null>(null);
4847
const [success, setSuccess] = useState<string | null>(null);
4948
const { t } = useTranslation();

src/utils/urlEncoding.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
/**
2-
* Encodes a URL string to Base64 format for safe URL parameter usage
2+
* Encodes a URL string to URL-safe Base64 format for safe URL parameter usage
3+
* Uses URL-safe Base64 encoding (RFC 4648 §5) by replacing + with -, / with _, and removing padding =
34
* @param url - The URL string to encode
4-
* @returns Base64 encoded string
5+
* @returns URL-safe Base64 encoded string
56
*/
67
export const encodeUrlToBase64 = (url: string): string => {
78
try {
89
// Use btoa for browser-compatible Base64 encoding
910
// First encode to handle UTF-8 characters properly
1011
const utf8Bytes = new TextEncoder().encode(url);
1112
const binaryString = Array.from(utf8Bytes, byte => String.fromCharCode(byte)).join('');
12-
return btoa(binaryString);
13+
const base64 = btoa(binaryString);
14+
// Convert to URL-safe Base64 by replacing + with -, / with _, and removing padding =
15+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
1316
} catch (error) {
1417
console.error('Error encoding URL to Base64:', error);
1518
throw new Error('Failed to encode URL to Base64');
1619
}
1720
};
1821

1922
/**
20-
* Decodes a Base64 encoded URL string
23+
* Decodes a URL-safe Base64 encoded URL string
24+
* Handles both URL-safe and standard Base64 formats
2125
* @param encodedUrl - The Base64 encoded URL string
2226
* @returns Decoded URL string
2327
*/
2428
export const decodeBase64ToUrl = (encodedUrl: string): string => {
2529
try {
30+
// Convert URL-safe Base64 back to standard Base64
31+
let base64 = encodedUrl.replace(/-/g, '+').replace(/_/g, '/');
32+
// Add padding if necessary
33+
while (base64.length % 4 !== 0) {
34+
base64 += '=';
35+
}
2636
// Use atob for browser-compatible Base64 decoding
27-
const binaryString = atob(encodedUrl);
37+
const binaryString = atob(base64);
2838
const bytes = Uint8Array.from(binaryString, char => char.charCodeAt(0));
2939
return new TextDecoder().decode(bytes);
3040
} catch (error) {

0 commit comments

Comments
 (0)