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