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