@@ -4,52 +4,71 @@ import { err, ok, type Result } from './result';
44import { staticNodeModulesForVM } from './static-node-modules-for-vm' ;
55
66export const createContext = ( filename : string ) : vm . Context => {
7- return {
8- ...global ,
9- console,
10- Buffer,
11- AbortSignal,
12- Event,
13- EventTarget,
14- TextDecoder,
15- Request,
16- Response,
17- TextDecoderStream,
18- SyntaxError,
19- Error,
20- TextEncoder,
21- TextEncoderStream,
22- ReadableStream,
23- URL ,
24- URLSearchParams,
25- Headers,
26- module : {
27- exports : { } ,
28- } ,
29- __filename : filename ,
30- __dirname : path . dirname ( filename ) ,
31- require : ( specifiedModule : string ) => {
32- let m = specifiedModule ;
33- if ( specifiedModule . startsWith ( 'node:' ) ) {
34- m = m . split ( ':' ) [ 1 ] ! ;
35- }
7+ return new Proxy (
8+ {
9+ module : {
10+ exports : { } ,
11+ } ,
12+ __filename : filename ,
13+ __dirname : path . dirname ( filename ) ,
14+ require : ( specifiedModule : string ) => {
15+ let m = specifiedModule ;
16+ if ( specifiedModule . startsWith ( 'node:' ) ) {
17+ m = m . split ( ':' ) [ 1 ] ! ;
18+ }
19+
20+ if ( m in staticNodeModulesForVM ) {
21+ return staticNodeModulesForVM [ m ] ;
22+ }
3623
37- if ( m in staticNodeModulesForVM ) {
38- return staticNodeModulesForVM [ m ] ;
39- }
24+ return require ( `${ specifiedModule } ` ) as unknown ;
25+ // this string templating was necessary to not have
26+ // webpack warnings like:
27+ //
28+ // Import trace for requested module:
29+ // ./src/utils/get-email-component.tsx
30+ // ./src/app/page.tsx
31+ // ⚠ ./src/utils/get-email-component.tsx
32+ // Critical dependency: the request of a dependency is an expression
33+ } ,
34+ } ,
35+ {
36+ get ( target , property : string ) {
37+ if ( property in target ) {
38+ return target [ property ] ;
39+ }
4040
41- return require ( `${ specifiedModule } ` ) as unknown ;
42- // this string templating was necessary to not have
43- // webpack warnings like:
44- //
45- // Import trace for requested module:
46- // ./src/utils/get-email-component.tsx
47- // ./src/app/page.tsx
48- // ⚠ ./src/utils/get-email-component.tsx
49- // Critical dependency: the request of a dependency is an expression
41+ return globalThis [ property as keyof typeof globalThis ] ;
42+ } ,
43+ has ( target , property : string ) {
44+ return property in target || property in globalThis ;
45+ } ,
46+ set ( target , property , value ) {
47+ target [ property ] = value ;
48+ return true ;
49+ } ,
50+ getOwnPropertyDescriptor ( target , property ) {
51+ return (
52+ Object . getOwnPropertyDescriptor ( target , property ) ??
53+ Object . getOwnPropertyDescriptor ( globalThis , property )
54+ ) ;
55+ } ,
56+ ownKeys ( target ) {
57+ const keys = new Set ( [
58+ ...Reflect . ownKeys ( globalThis ) ,
59+ ...Reflect . ownKeys ( target ) ,
60+ ] ) ;
61+ return Array . from ( keys ) ;
62+ } ,
63+ defineProperty ( target , property , descriptor ) {
64+ Object . defineProperty ( target , property , descriptor ) ;
65+ return true ;
66+ } ,
67+ deleteProperty ( target , property ) {
68+ return delete target [ property ] ;
69+ } ,
5070 } ,
51- process,
52- } ;
71+ ) ;
5372} ;
5473
5574export const runBundledCode = (
0 commit comments