@@ -8,8 +8,62 @@ function wasmRelative(cwd: string, filePath: string): string {
88 return path . resolve ( cwd , filePath ) . replace ( / ^ \/ / , '' ) ;
99}
1010
11+ function resolveNodeModule ( fromPath : string , id : string ) : string | null {
12+ let dir = path . dirname ( '/' + fromPath ) ;
13+
14+ while ( true ) {
15+ const base = wasmRelative ( '/' , path . join ( dir , 'node_modules' , id ) ) ;
16+ const candidates = [ base , base + '.js' , base + '/index.js' ] ;
17+
18+ const pkgJson = base + '/package.json' ;
19+
20+ if ( fs . existsSync ( pkgJson ) ) {
21+ try {
22+ const pkg = JSON . parse ( fs . readFileSync ( pkgJson , 'utf-8' ) as string ) ;
23+ const main = pkg . main || 'index.js' ;
24+ candidates . unshift ( wasmRelative ( '/' , path . join ( dir , 'node_modules' , id , main ) ) ) ;
25+ } catch { }
26+ }
27+
28+ for ( const candidate of candidates ) {
29+ if ( fs . existsSync ( candidate ) ) return candidate ;
30+ }
31+
32+ const parent = path . dirname ( dir ) ;
33+ if ( parent === dir ) return null ;
34+
35+ dir = parent ;
36+ }
37+ }
38+
39+ const makeRequire = ( fromPath : string ) => ( id : string ) => {
40+ let depPath : string ;
41+
42+ if ( ! id . startsWith ( '.' ) && ! id . startsWith ( '/' ) ) {
43+ const resolved = resolveNodeModule ( fromPath , id ) ;
44+
45+ if ( ! resolved ) throw new Error ( `Cannot find module '${ id } '` ) ;
46+
47+ depPath = resolved ;
48+ } else {
49+ const base = wasmRelative ( '/' , path . resolve ( '/' + path . dirname ( fromPath ) , id ) ) ;
50+
51+ depPath = fs . existsSync ( base ) ? base
52+ : fs . existsSync ( base + '.js' ) ? base + '.js'
53+ : base + '/index.js' ;
54+ }
55+
56+ const src = fs . readFileSync ( depPath , 'utf-8' ) as string ;
57+ const m = { exports : { } as any } ;
58+ const fn = new Function ( 'module' , 'exports' , 'require' , '__filename' , '__dirname' , src ) ;
59+
60+ fn ( m , m . exports , makeRequire ( depPath ) , depPath , path . dirname ( depPath ) ) ;
61+
62+ return m . exports ;
63+ } ;
64+
1165const executeFile = task (
12- { name : "executeFile" , compute : "MEDIUM" , ram : "512MB" } ,
66+ { name : "executeFile" , compute : "MEDIUM" , ram : "512MB" , allowedHosts : [ "*" ] } ,
1367 async ( state : State , filePath : string , args : string [ ] ) => {
1468 const capturedOutput : string [ ] = [ ] ;
1569 const relPath = wasmRelative ( state . cwd , filePath ) ;
@@ -28,18 +82,6 @@ const executeFile = task(
2882 try {
2983 const code = fs . readFileSync ( relPath , 'utf-8' ) as string ;
3084 const mod = { exports : { } as any } ;
31-
32- const makeRequire = ( fromPath : string ) => ( id : string ) => {
33- const base = wasmRelative ( '/' , path . resolve ( '/' + path . dirname ( fromPath ) , id ) ) ;
34- const depPath = fs . existsSync ( base ) ? base
35- : fs . existsSync ( base + '.js' ) ? base + '.js'
36- : base + '/index.js' ;
37- const src = fs . readFileSync ( depPath , 'utf-8' ) as string ;
38- const m = { exports : { } as any } ;
39- const fn = new Function ( 'module' , 'exports' , 'require' , '__filename' , '__dirname' , src ) ;
40- fn ( m , m . exports , makeRequire ( depPath ) , depPath , path . dirname ( depPath ) ) ;
41- return m . exports ;
42- } ;
4385 const customRequire = makeRequire ( relPath ) ;
4486
4587 const fn = new Function ( 'module' , 'exports' , 'require' , '__filename' , '__dirname' , code ) ;
@@ -60,7 +102,7 @@ const executeFile = task(
60102
61103
62104const executeCode = task (
63- { name : "executeCode" , compute : "LOW" , ram : "256MB" } ,
105+ { name : "executeCode" , compute : "LOW" , ram : "256MB" , allowedHosts : [ "*" ] } ,
64106 async ( state : State , code : string ) : Promise < unknown > => {
65107 process . chdir ( state . cwd ) ;
66108 const capturedOutput : string [ ] = [ ] ;
@@ -70,14 +112,16 @@ const executeCode = task(
70112 capturedOutput . push ( args . map ( arg => String ( arg ) ) . join ( ' ' ) ) ;
71113 } ;
72114
115+ const require = makeRequire ( wasmRelative ( state . cwd , '.' ) ) ;
116+
73117 try {
74118 let result ;
75119 try {
76120 result = eval ( code ) ;
77121 } catch ( e ) {
78122 if ( e instanceof SyntaxError && e . message . includes ( "return" ) ) {
79- const fn = new Function ( code ) ;
80- result = fn ( ) ;
123+ const fn = new Function ( 'require' , code ) ;
124+ result = fn ( require ) ;
81125 } else {
82126 throw e ;
83127 }
0 commit comments