Skip to content

Commit a21cde8

Browse files
authored
[ty] Fix playground crash for very large files (#20934)
1 parent 64edfb6 commit a21cde8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

playground/ruff/src/Editor/settings.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ export function persistLocal({
7979
settingsSource: string;
8080
pythonSource: string;
8181
}) {
82+
const totalLength = settingsSource.length + pythonSource.length;
83+
84+
// Don't persist large files to local storage because they can exceed the local storage quota
85+
// The number here is picked rarely arbitrarily. Also note, JS uses UTF 16:
86+
// that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character)
87+
if (totalLength > 500_000) {
88+
return;
89+
}
90+
8291
localStorage.setItem(
8392
"source",
8493
JSON.stringify([settingsSource, pythonSource]),

playground/ty/src/Editor/persist.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ export async function restore(): Promise<Workspace | null> {
4040
}
4141

4242
export function persistLocal(workspace: Workspace) {
43+
let totalLength = 0;
44+
for (const fileContent of Object.values(workspace.files)) {
45+
totalLength += fileContent.length;
46+
47+
// Don't persist large files to local storage because they can exceed the local storage quota
48+
// The number here is picked rarely arbitrarily. Also note, JS uses UTF 16:
49+
// that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character)
50+
if (totalLength > 500_000) {
51+
return;
52+
}
53+
}
54+
4355
localStorage.setItem("workspace", JSON.stringify(workspace));
4456
}
4557

0 commit comments

Comments
 (0)