[ty] Fix playground crash for very large files (#20934)

This commit is contained in:
Micha Reiser 2025-10-17 09:15:33 +02:00 committed by GitHub
parent 64edfb6ef6
commit a21cde8a5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View file

@ -79,6 +79,15 @@ export function persistLocal({
settingsSource: string;
pythonSource: string;
}) {
const totalLength = settingsSource.length + pythonSource.length;
// Don't persist large files to local storage because they can exceed the local storage quota
// The number here is picked rarely arbitrarily. Also note, JS uses UTF 16:
// that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character)
if (totalLength > 500_000) {
return;
}
localStorage.setItem(
"source",
JSON.stringify([settingsSource, pythonSource]),

View file

@ -40,6 +40,18 @@ export async function restore(): Promise<Workspace | null> {
}
export function persistLocal(workspace: Workspace) {
let totalLength = 0;
for (const fileContent of Object.values(workspace.files)) {
totalLength += fileContent.length;
// Don't persist large files to local storage because they can exceed the local storage quota
// The number here is picked rarely arbitrarily. Also note, JS uses UTF 16:
// that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character)
if (totalLength > 500_000) {
return;
}
}
localStorage.setItem("workspace", JSON.stringify(workspace));
}