mirror of https://github.com/astral-sh/ruff
[ty] Fix playground crash for very large files (#20934)
This commit is contained in:
parent
64edfb6ef6
commit
a21cde8a5a
|
|
@ -79,6 +79,15 @@ export function persistLocal({
|
||||||
settingsSource: string;
|
settingsSource: string;
|
||||||
pythonSource: 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(
|
localStorage.setItem(
|
||||||
"source",
|
"source",
|
||||||
JSON.stringify([settingsSource, pythonSource]),
|
JSON.stringify([settingsSource, pythonSource]),
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,18 @@ export async function restore(): Promise<Workspace | null> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function persistLocal(workspace: Workspace) {
|
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));
|
localStorage.setItem("workspace", JSON.stringify(workspace));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue