-
Notifications
You must be signed in to change notification settings - Fork 40
fix JS and TS executors to await Promise in context execution #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0fbf830
0fd5dc1
43d9b86
e1c3e86
2b7481a
e4aa98a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cloudflare/sandbox": patch | ||
| --- | ||
|
|
||
| fix JS and TS executors to await Promise in context execution |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import { fileURLToPath } from 'node:url'; | |
| import * as util from 'node:util'; | ||
| import * as vm from 'node:vm'; | ||
| import type { RichOutput } from '../../process-pool'; | ||
| import { transformForAsyncExecution } from '../shared/code-transformer'; | ||
| import { isThenable } from '../shared/thenable'; | ||
|
|
||
| // Create CommonJS-like globals for the sandbox | ||
| const __filename = fileURLToPath(import.meta.url); | ||
|
|
@@ -83,7 +85,14 @@ rl.on('line', async (line: string) => { | |
| options.timeout = timeout; | ||
| } | ||
|
|
||
| result = vm.runInContext(code, context, options); | ||
| // Transform code to support top-level await and capture last expression | ||
| const wrappedCode = transformForAsyncExecution(code); | ||
| result = vm.runInContext(wrappedCode, context, options); | ||
|
|
||
| // Result is always a Promise from async IIFE - await it | ||
| if (isThenable(result)) { | ||
| result = await result; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical timeout bypass: Wrap in Promise.race: if (isThenable(result)) {
if (timeout !== undefined) {
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Execution timeout')), timeout)
);
result = await Promise.race([result, timeoutPromise]);
} else {
result = await result;
}
} |
||
| } catch (error: unknown) { | ||
| const err = error as Error; | ||
| stderr += err.stack || err.toString(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.