22let jsOutput = [ ] ;
33
44// Helper function to capture console output
5- function createConsoleProxy ( ) {
6- return {
7- log : ( ...args ) => {
8- jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
9- } ,
10- error : ( ...args ) => {
11- jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
12- } ,
13- warn : ( ...args ) => {
14- jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
15- } ,
16- info : ( ...args ) => {
17- jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
18- } ,
19- } ;
20- }
5+ const originalConsole = globalThis . console ;
6+ globalThis . console = {
7+ log : ( ...args ) => {
8+ jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
9+ } ,
10+ error : ( ...args ) => {
11+ jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
12+ } ,
13+ warn : ( ...args ) => {
14+ jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
15+ } ,
16+ info : ( ...args ) => {
17+ jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
18+ } ,
19+ } ;
2120
2221async function init ( id ) {
2322 // Initialize the worker
@@ -27,12 +26,9 @@ async function init(id) {
2726async function runJavaScript ( id , payload ) {
2827 const { code } = payload ;
2928 try {
30- // Create a console proxy to capture output
31- const console = createConsoleProxy ( ) ;
32-
3329 // Execute code directly with eval in the worker global scope
3430 // This will preserve variables across calls
35- const result = eval ( code ) ;
31+ const result = globalThis . eval ( code ) ;
3632
3733 if ( result !== undefined ) {
3834 jsOutput . push ( {
@@ -41,8 +37,7 @@ async function runJavaScript(id, payload) {
4137 } ) ;
4238 }
4339 } catch ( e ) {
44- // Use self.console to avoid recursion with our console proxy
45- self . console . log ( e ) ;
40+ originalConsole . log ( e ) ;
4641 if ( e instanceof Error ) {
4742 jsOutput . push ( {
4843 type : "error" ,
@@ -95,12 +90,10 @@ async function restoreState(id, payload) {
9590
9691 for ( const command of commands ) {
9792 try {
98- const console = createConsoleProxy ( ) ;
99- eval ( command ) ;
93+ globalThis . eval ( command ) ;
10094 } catch ( e ) {
10195 // If restoration fails, we still continue with other commands
102- // Use self.console to avoid recursion with our console proxy
103- self . console . error ( "Failed to restore command:" , command , e ) ;
96+ originalConsole . error ( "Failed to restore command:" , command , e ) ;
10497 }
10598 }
10699
@@ -124,7 +117,7 @@ self.onmessage = async (event) => {
124117 await restoreState ( id , payload ) ;
125118 return ;
126119 default :
127- console . error ( `Unknown message type: ${ type } ` ) ;
120+ originalConsole . error ( `Unknown message type: ${ type } ` ) ;
128121 return ;
129122 }
130123} ;
0 commit comments