2022-03-23 14:41:12 +00:00
|
|
|
import { safeRun } from "../util";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import sandboxHtml from "bundle-text:./iframe_sandbox.html";
|
2022-03-23 14:41:12 +00:00
|
|
|
import { Sandbox } from "../sandbox";
|
|
|
|
import { WorkerLike } from "./worker";
|
2022-03-25 11:03:06 +00:00
|
|
|
import { Plug } from "../plug";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
class IFrameWrapper implements WorkerLike {
|
|
|
|
private iframe: HTMLIFrameElement;
|
|
|
|
onMessage?: (message: any) => Promise<void>;
|
2022-03-21 14:21:34 +00:00
|
|
|
ready: Promise<void>;
|
2022-03-24 09:48:56 +00:00
|
|
|
private messageListener: (evt: any) => void;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
const iframe = document.createElement("iframe", {});
|
|
|
|
this.iframe = iframe;
|
|
|
|
iframe.style.display = "none";
|
|
|
|
// Let's lock this down significantly
|
|
|
|
iframe.setAttribute("sandbox", "allow-scripts");
|
|
|
|
iframe.srcdoc = sandboxHtml;
|
2022-03-24 09:48:56 +00:00
|
|
|
this.messageListener = (evt: any) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
if (evt.source !== iframe.contentWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let data = evt.data;
|
|
|
|
if (!data) return;
|
|
|
|
safeRun(async () => {
|
|
|
|
await this.onMessage!(data);
|
|
|
|
});
|
2022-03-24 09:48:56 +00:00
|
|
|
};
|
|
|
|
window.addEventListener("message", this.messageListener);
|
2022-03-20 08:56:28 +00:00
|
|
|
document.body.appendChild(iframe);
|
2022-03-21 14:21:34 +00:00
|
|
|
this.ready = new Promise((resolve) => {
|
|
|
|
iframe.onload = () => {
|
|
|
|
resolve();
|
|
|
|
iframe.onload = null;
|
|
|
|
};
|
|
|
|
});
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
postMessage(message: any): void {
|
|
|
|
this.iframe.contentWindow!.postMessage(message, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate() {
|
2022-03-31 15:25:34 +00:00
|
|
|
console.log("Terminating iframe sandbox");
|
2022-03-24 09:48:56 +00:00
|
|
|
window.removeEventListener("message", this.messageListener);
|
2022-03-20 08:56:28 +00:00
|
|
|
return this.iframe.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 11:03:06 +00:00
|
|
|
export function createSandbox(plug: Plug<any>) {
|
|
|
|
return new Sandbox(plug, new IFrameWrapper());
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|