Skip to content

Commit cd8d40a

Browse files
authored
chore: robustify playground decode (#17606)
For some reason LLMs like to percent-encode stuff, this makes the download script still run in that case
1 parent 92e6721 commit cd8d40a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

playgrounds/sandbox/scripts/download.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,19 @@ if (is_local) {
635635
} else if (url && url.origin === 'https://svelte.dev' && url.pathname.startsWith('/playground/')) {
636636
// Svelte playground URL handling (existing logic)
637637
if (url.hash.length > 1) {
638-
const decoded = atob(url.hash.slice(1).replaceAll('-', '+').replaceAll('_', '/'));
638+
// Decode percent-encoded characters (e.g., %5F => _), and replace base64 chars.
639+
let decoded;
640+
try {
641+
// First, decode URI components to handle %xx encodings (e.g. %5F -> _) (LLMs calling this script sometimes encode them for some reason)
642+
decoded = url.hash.slice(1);
643+
decoded = decodeURIComponent(decoded);
644+
645+
// Now, restore for base64 (replace -/+, _/ /)
646+
decoded = atob(decoded.replaceAll('-', '+').replaceAll('_', '/'));
647+
} catch (e) {
648+
console.error('Failed to decode URL hash:', e);
649+
process.exit(1);
650+
}
639651
// putting it directly into the blob gives a corrupted file
640652
const u8 = new Uint8Array(decoded.length);
641653
for (let i = 0; i < decoded.length; i++) {

0 commit comments

Comments
 (0)