Add script caching

This commit is contained in:
Peter Stockings
2025-07-26 21:27:56 +10:00
parent c912a3f5ae
commit 4300e08885

View File

@@ -1,4 +1,6 @@
// isolator/worker.ts
const moduleCache = new Map();
const States = Object.freeze({
SUCCESS: "SUCCESS",
NOT_A_FUNCTION: "NOT_A_FUNCTION",
@@ -50,11 +52,15 @@ self.onmessage = async (e) => {
status
);
// Use a data URL to import the user's code as an ES module.
const dataUrl = `data:text/javascript;base64,${btoa(
unescape(encodeURIComponent(code))
)}`;
const userModule = await import(dataUrl);
let userModule = moduleCache.get(code);
if (!userModule) {
// Use a data URL to import the user's code as an ES module.
const dataUrl = `data:text/javascript;base64,${btoa(
unescape(encodeURIComponent(code))
)}`;
userModule = await import(dataUrl);
moduleCache.set(code, userModule);
}
if (typeof userModule.default !== "function") {
throw new Error(