@@ -7,6 +7,10 @@ import ts from 'typescript';
77import { WorkerOutput } from './code-tool-types' ;
88import { ImageKit , ClientOptions } from '@imagekit/nodejs' ;
99
10+ async function tseval ( code : string ) {
11+ return import ( 'data:application/typescript;charset=utf-8;base64,' + Buffer . from ( code ) . toString ( 'base64' ) ) ;
12+ }
13+
1014function getRunFunctionSource ( code : string ) : {
1115 type : 'declaration' | 'expression' ;
1216 client : string | undefined ;
@@ -285,7 +289,9 @@ const fetch = async (req: Request): Promise<Response> => {
285289
286290 const log_lines : string [ ] = [ ] ;
287291 const err_lines : string [ ] = [ ] ;
288- const console = {
292+ const originalConsole = globalThis . console ;
293+ globalThis . console = {
294+ ...originalConsole ,
289295 log : ( ...args : unknown [ ] ) => {
290296 log_lines . push ( util . format ( ...args ) ) ;
291297 } ,
@@ -295,7 +301,7 @@ const fetch = async (req: Request): Promise<Response> => {
295301 } ;
296302 try {
297303 let run_ = async ( client : any ) => { } ;
298- eval ( `${ code } \nrun_ = run;` ) ;
304+ run_ = ( await tseval ( `${ code } \nexport default run;` ) ) . default ;
299305 const result = await run_ ( makeSdkProxy ( client , { path : [ 'client' ] } ) ) ;
300306 return Response . json ( {
301307 is_error : false ,
@@ -313,6 +319,8 @@ const fetch = async (req: Request): Promise<Response> => {
313319 } satisfies WorkerOutput ,
314320 { status : 400 , statusText : 'Code execution error' } ,
315321 ) ;
322+ } finally {
323+ globalThis . console = originalConsole ;
316324 }
317325} ;
318326
0 commit comments