fix: use absolute indexURL for pyodide sandbox (#26625)

This commit is contained in:
Algorithm5838
2026-07-01 02:09:19 -05:00
committed by GitHub
parent 2d7fc04bfb
commit a78f0c0302
+42 -35
View File
@@ -17,7 +17,7 @@ const sandboxScript = String.raw`
stdout = null;
stderr = null;
pyodide = await loadPyodide({
indexURL: '/pyodide/',
indexURL: self.__PYODIDE_INDEX_URL__ || '/pyodide/',
stdout: function (text) {
stdout = stdout ? stdout + text + '\n' : text + '\n';
},
@@ -145,45 +145,52 @@ const sandboxScript = String.raw`
if (event.source !== parent) return;
const data = event.data || {};
const id = data.id;
if (!data.type || data.type === 'execute') {
await ensureRuntime(data.packages || []);
await execute(id, data.code, data.files);
return;
}
await ensureRuntime();
switch (data.type) {
case 'fs:upload':
upload(data.files, data.dir);
post({ id: id, type: data.type, success: true });
break;
case 'fs:list':
post({ id: id, type: data.type, entries: list(data.path) });
break;
case 'fs:read':
try {
const buffer = pyodide.FS.readFile(data.path).buffer;
post({ id: id, type: data.type, data: buffer }, [buffer]);
} catch (error) {
post({ id: id, type: data.type, error: error && error.message ? error.message : String(error) });
}
break;
case 'fs:delete':
remove(data.path);
post({ id: id, type: data.type, success: true });
break;
case 'fs:mkdir':
pyodide.FS.mkdirTree(data.path);
post({ id: id, type: data.type, success: true });
break;
case 'fs:sync':
post({ id: id, type: data.type, success: true });
break;
try {
if (!data.type || data.type === 'execute') {
await ensureRuntime(data.packages || []);
await execute(id, data.code, data.files);
return;
}
await ensureRuntime();
switch (data.type) {
case 'fs:upload':
upload(data.files, data.dir);
post({ id: id, type: data.type, success: true });
break;
case 'fs:list':
post({ id: id, type: data.type, entries: list(data.path) });
break;
case 'fs:read':
try {
const buffer = pyodide.FS.readFile(data.path).buffer;
post({ id: id, type: data.type, data: buffer }, [buffer]);
} catch (error) {
post({ id: id, type: data.type, error: error && error.message ? error.message : String(error) });
}
break;
case 'fs:delete':
remove(data.path);
post({ id: id, type: data.type, success: true });
break;
case 'fs:mkdir':
pyodide.FS.mkdirTree(data.path);
post({ id: id, type: data.type, success: true });
break;
case 'fs:sync':
post({ id: id, type: data.type, success: true });
break;
}
} catch (error) {
post({ id: id, stderr: error && error.message ? error.message : String(error) });
}
});
})();
`;
const sandboxHtml = `<!doctype html><html><head><meta charset="utf-8"></head><body><script src="/pyodide/pyodide.js"></script><script>${sandboxScript}</script></body></html>`;
// indexURL must be absolute because about:srcdoc can't be a base URL
const pyodideIndexURL = `${globalThis.location?.origin ?? ''}/pyodide/`;
const sandboxHtml = `<!doctype html><html><head><meta charset="utf-8"><script>window.__PYODIDE_INDEX_URL__=${JSON.stringify(pyodideIndexURL)}</script></head><body><script src="${pyodideIndexURL}pyodide.js"></script><script>${sandboxScript}</script></body></html>`;
export class PyodideSandboxHost {
onmessage: MessageListener | null = null;