Add script caching
This commit is contained in:
16
worker.ts
16
worker.ts
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user