Open History to see the last commit
1/**
2 * Wrap user code in an async IIFE to support top-level await
3 *
4 * The code is wrapped so that:
5 * 1. Top-level await works
6 * 2. Return values are captured and serialized (for isolate boundary transfer)
7 * 3. Errors propagate correctly
8 */
9export function wrapCode(code: string): string {
10 // Results must be serialized to JSON because objects can't be transferred
11 // directly across the isolate boundary in isolated-vm
12 return `
13(async function() {
14 try {
15 const __userResult = await (async function() {
16${code}
17 })();
18 return JSON.stringify(__userResult);
19 } catch (__error) {
20 throw __error;
21 }
22})()
23`
24}