|
1 | 1 | export function buildIframeSandboxSrcdoc(channelLiteral: string): string { |
2 | 2 | return /* html */ `<!doctype html><html><body><script> |
3 | 3 | const CHANNEL = ${channelLiteral}; |
4 | | -window.addEventListener("message", async (event) => { |
| 4 | +window.addEventListener("message", (event) => { |
5 | 5 | const data = event.data; |
6 | | - if (!data || data.channel !== CHANNEL) { |
| 6 | + if (!data || data.channel !== CHANNEL || data.type !== "init") { |
7 | 7 | return; |
8 | 8 | } |
9 | | - const request = data.request || {}; |
| 9 | + const port = event.ports && event.ports[0]; |
| 10 | + if (!port) { |
| 11 | + return; |
| 12 | + } |
| 13 | +
|
10 | 14 | const safeSend = (payload) => { |
11 | 15 | try { |
12 | | - parent.postMessage({ channel: CHANNEL, ...payload }, "*"); |
| 16 | + port.postMessage({ type: "result", ...payload }); |
13 | 17 | return true; |
14 | 18 | } catch (postError) { |
15 | 19 | try { |
16 | 20 | const postMessageError = |
17 | 21 | postError && typeof postError === "object" && "message" in postError |
18 | 22 | ? String(postError.message) |
19 | 23 | : String(postError); |
20 | | - parent.postMessage( |
21 | | - { |
22 | | - channel: CHANNEL, |
23 | | - ok: false, |
24 | | - error: "Sandbox response is not serializable: " + postMessageError, |
25 | | - }, |
26 | | - "*", |
27 | | - ); |
| 24 | + port.postMessage({ |
| 25 | + type: "result", |
| 26 | + ok: false, |
| 27 | + error: "Sandbox response is not serializable: " + postMessageError, |
| 28 | + }); |
28 | 29 | } catch { |
29 | 30 | // Ignore terminal postMessage failures. |
30 | 31 | } |
31 | 32 | return false; |
32 | 33 | } |
33 | 34 | }; |
34 | | - try { |
35 | | - const moduleUrl = URL.createObjectURL( |
36 | | - new Blob([String(request.code ?? "")], { type: "text/javascript" }), |
37 | | - ); |
| 35 | +
|
| 36 | + port.onmessage = async (portEvent) => { |
| 37 | + const envelope = portEvent.data; |
| 38 | + if (!envelope || envelope.type !== "execute") { |
| 39 | + return; |
| 40 | + } |
| 41 | + const request = envelope.request || {}; |
| 42 | +
|
38 | 43 | try { |
39 | | - const namespace = await import(moduleUrl); |
40 | | - const exportName = |
41 | | - typeof request.exportName === "string" && |
42 | | - request.exportName.trim().length > 0 |
43 | | - ? request.exportName.trim() |
44 | | - : "default"; |
45 | | - const selected = namespace[exportName]; |
46 | | - if (selected === undefined) { |
47 | | - throw new Error( |
48 | | - 'Runtime source export "' + exportName + '" is missing', |
49 | | - ); |
| 44 | + const moduleUrl = URL.createObjectURL( |
| 45 | + new Blob([String(request.code ?? "")], { type: "text/javascript" }), |
| 46 | + ); |
| 47 | + try { |
| 48 | + const namespace = await import(moduleUrl); |
| 49 | + const exportName = |
| 50 | + typeof request.exportName === "string" && |
| 51 | + request.exportName.trim().length > 0 |
| 52 | + ? request.exportName.trim() |
| 53 | + : "default"; |
| 54 | + const selected = namespace[exportName]; |
| 55 | + if (selected === undefined) { |
| 56 | + throw new Error( |
| 57 | + 'Runtime source export "' + exportName + '" is missing', |
| 58 | + ); |
| 59 | + } |
| 60 | + const output = |
| 61 | + typeof selected === "function" |
| 62 | + ? await selected(request.runtimeInput ?? {}) |
| 63 | + : selected; |
| 64 | + safeSend({ ok: true, output }); |
| 65 | + } finally { |
| 66 | + URL.revokeObjectURL(moduleUrl); |
50 | 67 | } |
51 | | - const output = |
52 | | - typeof selected === "function" |
53 | | - ? await selected(request.runtimeInput ?? {}) |
54 | | - : selected; |
55 | | - safeSend({ ok: true, output }); |
56 | | - } finally { |
57 | | - URL.revokeObjectURL(moduleUrl); |
| 68 | + } catch (error) { |
| 69 | + const message = |
| 70 | + error && typeof error === "object" && "message" in error |
| 71 | + ? String(error.message) |
| 72 | + : String(error); |
| 73 | + safeSend({ ok: false, error: message }); |
58 | 74 | } |
59 | | - } catch (error) { |
60 | | - const message = |
61 | | - error && typeof error === "object" && "message" in error |
62 | | - ? String(error.message) |
63 | | - : String(error); |
64 | | - safeSend({ ok: false, error: message }); |
| 75 | + }; |
| 76 | +
|
| 77 | + if (typeof port.start === "function") { |
| 78 | + port.start(); |
65 | 79 | } |
66 | | -}); |
| 80 | + port.postMessage({ type: "ready" }); |
| 81 | +}, { once: true }); |
67 | 82 | </script></body></html>`; |
68 | 83 | } |
0 commit comments