1- // JavaScript web worker
2- let jsOutput = [ ] ;
1+ import type { ReplOutput } from "../repl" ;
2+ import type { MessageType , WorkerRequest , WorkerResponse } from "./runtime" ;
3+
4+ let jsOutput : ReplOutput [ ] = [ ] ;
35
46// Helper function to capture console output
57const originalConsole = globalThis . console ;
68globalThis . console = {
7- log : ( ...args ) => {
9+ ...originalConsole ,
10+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11+ log : ( ...args : any [ ] ) => {
812 jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
913 } ,
10- error : ( ...args ) => {
14+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15+ error : ( ...args : any [ ] ) => {
1116 jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
1217 } ,
13- warn : ( ...args ) => {
18+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19+ warn : ( ...args : any [ ] ) => {
1420 jsOutput . push ( { type : "stderr" , message : args . join ( " " ) } ) ;
1521 } ,
16- info : ( ...args ) => {
22+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23+ info : ( ...args : any [ ] ) => {
1724 jsOutput . push ( { type : "stdout" , message : args . join ( " " ) } ) ;
1825 } ,
1926} ;
2027
21- async function init ( id , payload ) {
28+ async function init ( { id } : WorkerRequest [ "init" ] ) {
2229 // Initialize the worker and report capabilities
2330 self . postMessage ( {
2431 id,
2532 payload : { capabilities : { interrupt : "restart" } } ,
26- } ) ;
33+ } satisfies WorkerResponse [ "init" ] ) ;
2734}
2835
29- async function runCode ( id , payload ) {
36+ async function runCode ( { id, payload } : WorkerRequest [ "runCode" ] ) {
3037 const { code } = payload ;
3138 try {
3239 // Execute code directly with eval in the worker global scope
@@ -60,11 +67,11 @@ async function runCode(id, payload) {
6067 self . postMessage ( {
6168 id,
6269 payload : { output, updatedFiles : [ ] } ,
63- } ) ;
70+ } satisfies WorkerResponse [ "runCode" ] ) ;
6471}
6572
66- function runFile ( id , payload ) {
67- const output = [
73+ function runFile ( { id } : WorkerRequest [ "runFile" ] ) {
74+ const output : ReplOutput [ ] = [
6875 {
6976 type : "error" ,
7077 message : "File execution is not supported in this runtime" ,
@@ -73,16 +80,19 @@ function runFile(id, payload) {
7380 self . postMessage ( {
7481 id,
7582 payload : { output, updatedFiles : [ ] } ,
76- } ) ;
83+ } satisfies WorkerResponse [ "runFile" ] ) ;
7784}
7885
79- async function checkSyntax ( id , payload ) {
86+ async function checkSyntax ( { id, payload } : WorkerRequest [ "checkSyntax" ] ) {
8087 const { code } = payload ;
8188
8289 try {
8390 // Try to create a Function to check syntax
8491 new Function ( code ) ;
85- self . postMessage ( { id, payload : { status : "complete" } } ) ;
92+ self . postMessage ( {
93+ id,
94+ payload : { status : "complete" } ,
95+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
8696 } catch ( e ) {
8797 // Check if it's a syntax error or if more input is expected
8898 if ( e instanceof SyntaxError ) {
@@ -91,17 +101,26 @@ async function checkSyntax(id, payload) {
91101 e . message . includes ( "Unexpected end of input" ) ||
92102 e . message . includes ( "expected expression" )
93103 ) {
94- self . postMessage ( { id, payload : { status : "incomplete" } } ) ;
104+ self . postMessage ( {
105+ id,
106+ payload : { status : "incomplete" } ,
107+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
95108 } else {
96- self . postMessage ( { id, payload : { status : "invalid" } } ) ;
109+ self . postMessage ( {
110+ id,
111+ payload : { status : "invalid" } ,
112+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
97113 }
98114 } else {
99- self . postMessage ( { id, payload : { status : "invalid" } } ) ;
115+ self . postMessage ( {
116+ id,
117+ payload : { status : "invalid" } ,
118+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
100119 }
101120 }
102121}
103122
104- async function restoreState ( id , payload ) {
123+ async function restoreState ( { id, payload } : WorkerRequest [ "restoreState" ] ) {
105124 // Re-execute all previously successful commands to restore state
106125 const { commands } = payload ;
107126 jsOutput = [ ] ; // Clear output for restoration
@@ -116,29 +135,32 @@ async function restoreState(id, payload) {
116135 }
117136
118137 jsOutput = [ ] ; // Clear any output from restoration
119- self . postMessage ( { id, payload : { } } ) ;
138+ self . postMessage ( {
139+ id,
140+ payload : { } ,
141+ } satisfies WorkerResponse [ "restoreState" ] ) ;
120142}
121143
122- self . onmessage = async ( event ) => {
123- const { id, type, payload } = event . data ;
124- switch ( type ) {
144+ self . onmessage = async ( event : MessageEvent < WorkerRequest [ MessageType ] > ) => {
145+ switch ( event . data . type ) {
125146 case "init" :
126- await init ( id , payload ) ;
147+ await init ( event . data ) ;
127148 return ;
128149 case "runCode" :
129- await runCode ( id , payload ) ;
150+ await runCode ( event . data ) ;
130151 return ;
131152 case "runFile" :
132- runFile ( id , payload ) ;
153+ runFile ( event . data ) ;
133154 return ;
134155 case "checkSyntax" :
135- await checkSyntax ( id , payload ) ;
156+ await checkSyntax ( event . data ) ;
136157 return ;
137158 case "restoreState" :
138- await restoreState ( id , payload ) ;
159+ await restoreState ( event . data ) ;
139160 return ;
140161 default :
141- originalConsole . error ( `Unknown message type: ${ type } ` ) ;
162+ event . data satisfies never ;
163+ originalConsole . error ( `Unknown message: ${ event . data } ` ) ;
142164 return ;
143165 }
144166} ;
0 commit comments