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

This commit is contained in:
Algorithm5838 authored and GitHub committed 2026-07-01 02:09:19 -05:00
1 parent 2d7fc04bfb
commit a78f0c0302
1 file changed
+9 -2
+9 -2
View File
@@ -17,7 +17,7 @@ const sandboxScript = String.raw`
stdout = null; stdout = null;
stderr = null; stderr = null;
pyodide = await loadPyodide({ pyodide = await loadPyodide({
indexURL: '/pyodide/', indexURL: self.__PYODIDE_INDEX_URL__ || '/pyodide/',
stdout: function (text) { stdout: function (text) {
stdout = stdout ? stdout + text + '\n' : text + '\n'; stdout = stdout ? stdout + text + '\n' : text + '\n';
}, },
@@ -145,6 +145,7 @@ const sandboxScript = String.raw`
if (event.source !== parent) return; if (event.source !== parent) return;
const data = event.data || {}; const data = event.data || {};
const id = data.id; const id = data.id;
try {
if (!data.type || data.type === 'execute') { if (!data.type || data.type === 'execute') {
await ensureRuntime(data.packages || []); await ensureRuntime(data.packages || []);
await execute(id, data.code, data.files); await execute(id, data.code, data.files);
@@ -179,11 +180,17 @@ const sandboxScript = String.raw`
post({ id: id, type: data.type, success: true }); post({ id: id, type: data.type, success: true });
break; 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 { export class PyodideSandboxHost {
onmessage: MessageListener | null = null; onmessage: MessageListener | null = null;