@@ -7,6 +7,10 @@ import ts from 'typescript';
77import { WorkerOutput } from './code-tool-types' ;
88import { CasParser , ClientOptions } from 'cas-parser-node' ;
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 ;
@@ -257,7 +261,9 @@ const fetch = async (req: Request): Promise<Response> => {
257261
258262 const log_lines : string [ ] = [ ] ;
259263 const err_lines : string [ ] = [ ] ;
260- const console = {
264+ const originalConsole = globalThis . console ;
265+ globalThis . console = {
266+ ...originalConsole ,
261267 log : ( ...args : unknown [ ] ) => {
262268 log_lines . push ( util . format ( ...args ) ) ;
263269 } ,
@@ -267,7 +273,7 @@ const fetch = async (req: Request): Promise<Response> => {
267273 } ;
268274 try {
269275 let run_ = async ( client : any ) => { } ;
270- eval ( `${ code } \nrun_ = run;` ) ;
276+ run_ = ( await tseval ( `${ code } \nexport default run;` ) ) . default ;
271277 const result = await run_ ( makeSdkProxy ( client , { path : [ 'client' ] } ) ) ;
272278 return Response . json ( {
273279 is_error : false ,
@@ -285,6 +291,8 @@ const fetch = async (req: Request): Promise<Response> => {
285291 } satisfies WorkerOutput ,
286292 { status : 400 , statusText : 'Code execution error' } ,
287293 ) ;
294+ } finally {
295+ globalThis . console = originalConsole ;
288296 }
289297} ;
290298
0 commit comments