@@ -7,6 +7,10 @@ import ts from 'typescript';
77import { WorkerOutput } from './code-tool-types' ;
88import { Sent , ClientOptions } from '@sentdm/sentdm' ;
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 ;
@@ -276,7 +280,9 @@ const fetch = async (req: Request): Promise<Response> => {
276280
277281 const log_lines : string [ ] = [ ] ;
278282 const err_lines : string [ ] = [ ] ;
279- const console = {
283+ const originalConsole = globalThis . console ;
284+ globalThis . console = {
285+ ...originalConsole ,
280286 log : ( ...args : unknown [ ] ) => {
281287 log_lines . push ( util . format ( ...args ) ) ;
282288 } ,
@@ -286,7 +292,7 @@ const fetch = async (req: Request): Promise<Response> => {
286292 } ;
287293 try {
288294 let run_ = async ( client : any ) => { } ;
289- eval ( `${ code } \nrun_ = run;` ) ;
295+ run_ = ( await tseval ( `${ code } \nexport default run;` ) ) . default ;
290296 const result = await run_ ( makeSdkProxy ( client , { path : [ 'client' ] } ) ) ;
291297 return Response . json ( {
292298 is_error : false ,
@@ -304,6 +310,8 @@ const fetch = async (req: Request): Promise<Response> => {
304310 } satisfies WorkerOutput ,
305311 { status : 400 , statusText : 'Code execution error' } ,
306312 ) ;
313+ } finally {
314+ globalThis . console = originalConsole ;
307315 }
308316} ;
309317
0 commit comments