Skip to content

Commit 9dc3d7e

Browse files
committed
fix(evals): use vm context for server eval to match real Node server
Replace parameter-shadowing of browser globals with vm.createContext so that the server eval context genuinely lacks window/document/navigator. This fixes two inaccuracies: - globalThis.window no longer leaks jsdom's window into server code - bare window access throws ReferenceError, so window?.innerWidth correctly fails instead of silently evaluating as undefined
1 parent 704e738 commit 9dc3d7e

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

‎packages/octane-evals/datasets/train/user-apps-v1/strong-repair.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readFileSync } from 'node:fs';
22
import { dirname, join, resolve } from 'node:path';
33
import { fileURLToPath } from 'node:url';
4+
import vm from 'node:vm';
45
import ts from 'typescript';
56
import { expect } from 'vitest';
67
import { compile } from 'octane/compiler';
@@ -45,19 +46,6 @@ export function expectStrongCompile(source: string): void {
4546
expect(strongCompileErrors(source)).toEqual([]);
4647
}
4748

48-
// A real server has none of these. The graders run in jsdom, so the server build
49-
// sees them shadowed as undefined, and `typeof window === 'undefined'` holds.
50-
const BROWSER_GLOBALS = [
51-
'window',
52-
'self',
53-
'document',
54-
'navigator',
55-
'location',
56-
'localStorage',
57-
'sessionStorage',
58-
'matchMedia',
59-
];
60-
6149
/** Evaluate the Strong server build of a submission without browser globals. */
6250
export function serverModule(source: string): Record<string, any> {
6351
const { code } = compile(source, 'App.tsrx', { mode: 'server', strong: true } as any);
@@ -70,11 +58,27 @@ export function serverModule(source: string): Record<string, any> {
7058
if (specifier === 'octane/internal/server') return ServerHelpers;
7159
throw new Error(`Unsupported server-eval import: ${specifier}`);
7260
};
73-
new Function('require', 'module', 'exports', ...BROWSER_GLOBALS, commonJs)(
74-
requireServerRuntime,
61+
// Run in a vm context that has no browser globals. Unlike parameter
62+
// shadowing, this makes bare `window` a ReferenceError and keeps
63+
// `globalThis.window` absent, matching a real Node server.
64+
const ctx = vm.createContext({
65+
require: requireServerRuntime,
7566
module,
76-
module.exports,
77-
);
67+
exports: module.exports,
68+
console,
69+
process,
70+
Buffer,
71+
URL,
72+
URLSearchParams,
73+
TextEncoder,
74+
TextDecoder,
75+
setTimeout,
76+
clearTimeout,
77+
setInterval,
78+
clearInterval,
79+
queueMicrotask,
80+
});
81+
vm.runInContext(commonJs, ctx);
7882
return module.exports;
7983
}
8084

0 commit comments

Comments
 (0)