@@ -7,6 +7,10 @@ import ts from 'typescript';
77import { WorkerOutput } from './code-tool-types' ;
88import { Writer , ClientOptions } from 'writer-sdk' ;
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 ;
@@ -269,7 +273,9 @@ const fetch = async (req: Request): Promise<Response> => {
269273
270274 const log_lines : string [ ] = [ ] ;
271275 const err_lines : string [ ] = [ ] ;
272- const console = {
276+ const originalConsole = globalThis . console ;
277+ globalThis . console = {
278+ ...originalConsole ,
273279 log : ( ...args : unknown [ ] ) => {
274280 log_lines . push ( util . format ( ...args ) ) ;
275281 } ,
@@ -279,7 +285,7 @@ const fetch = async (req: Request): Promise<Response> => {
279285 } ;
280286 try {
281287 let run_ = async ( client : any ) => { } ;
282- eval ( `${ code } \nrun_ = run;` ) ;
288+ run_ = ( await tseval ( `${ code } \nexport default run;` ) ) . default ;
283289 const result = await run_ ( makeSdkProxy ( client , { path : [ 'client' ] } ) ) ;
284290 return Response . json ( {
285291 is_error : false ,
@@ -297,6 +303,8 @@ const fetch = async (req: Request): Promise<Response> => {
297303 } satisfies WorkerOutput ,
298304 { status : 400 , statusText : 'Code execution error' } ,
299305 ) ;
306+ } finally {
307+ globalThis . console = originalConsole ;
300308 }
301309} ;
302310
0 commit comments