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
+42 -35
+42 -35
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,45 +145,52 @@ 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;
if (!data.type || data.type === 'execute') { try {
await ensureRuntime(data.packages || []); if (!data.type || data.type === 'execute') {
await execute(id, data.code, data.files); await ensureRuntime(data.packages || []);
return; await execute(id, data.code, data.files);
} return;
await ensureRuntime(); }
switch (data.type) { await ensureRuntime();
case 'fs:upload': switch (data.type) {
upload(data.files, data.dir); case 'fs:upload':
post({ id: id, type: data.type, success: true }); upload(data.files, data.dir);
break; post({ id: id, type: data.type, success: true });
case 'fs:list': break;
post({ id: id, type: data.type, entries: list(data.path) }); case 'fs:list':
break; post({ id: id, type: data.type, entries: list(data.path) });
case 'fs:read': break;
try { case 'fs:read':
const buffer = pyodide.FS.readFile(data.path).buffer; try {
post({ id: id, type: data.type, data: buffer }, [buffer]); const buffer = pyodide.FS.readFile(data.path).buffer;
} catch (error) { post({ id: id, type: data.type, data: buffer }, [buffer]);
post({ id: id, type: data.type, error: error && error.message ? error.message : String(error) }); } catch (error) {
} post({ id: id, type: data.type, error: error && error.message ? error.message : String(error) });
break; }
case 'fs:delete': break;
remove(data.path); case 'fs:delete':
post({ id: id, type: data.type, success: true }); remove(data.path);
break; post({ id: id, type: data.type, success: true });
case 'fs:mkdir': break;
pyodide.FS.mkdirTree(data.path); case 'fs:mkdir':
post({ id: id, type: data.type, success: true }); pyodide.FS.mkdirTree(data.path);
break; post({ id: id, type: data.type, success: true });
case 'fs:sync': break;
post({ id: id, type: data.type, success: true }); case 'fs:sync':
break; 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 { export class PyodideSandboxHost {
onmessage: MessageListener | null = null; onmessage: MessageListener | null = null;