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
@@ -61,10 +68,10 @@ async function runCode(id, payload) {
6168 self . postMessage ( {
6269 id,
6370 payload : { output, updatedFiles : [ ] } ,
64- } ) ;
71+ } satisfies WorkerResponse [ "runCode" ] ) ;
6572}
6673
67- function runFile ( id , payload ) {
74+ function runFile ( { id, payload } : WorkerRequest [ "runFile" ] ) {
6875 const { name, files } = payload ;
6976 // pyodide worker などと異なり、複数ファイルを読み込んでimportのようなことをするのには対応していません。
7077 try {
@@ -93,16 +100,19 @@ function runFile(id, payload) {
93100 self . postMessage ( {
94101 id,
95102 payload : { output, updatedFiles : [ ] } ,
96- } ) ;
103+ } satisfies WorkerResponse [ "runFile" ] ) ;
97104}
98105
99- async function checkSyntax ( id , payload ) {
106+ async function checkSyntax ( { id, payload } : WorkerRequest [ "checkSyntax" ] ) {
100107 const { code } = payload ;
101108
102109 try {
103110 // Try to create a Function to check syntax
104111 new Function ( code ) ;
105- self . postMessage ( { id, payload : { status : "complete" } } ) ;
112+ self . postMessage ( {
113+ id,
114+ payload : { status : "complete" } ,
115+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
106116 } catch ( e ) {
107117 // Check if it's a syntax error or if more input is expected
108118 if ( e instanceof SyntaxError ) {
@@ -111,17 +121,26 @@ async function checkSyntax(id, payload) {
111121 e . message . includes ( "Unexpected end of input" ) ||
112122 e . message . includes ( "expected expression" )
113123 ) {
114- self . postMessage ( { id, payload : { status : "incomplete" } } ) ;
124+ self . postMessage ( {
125+ id,
126+ payload : { status : "incomplete" } ,
127+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
115128 } else {
116- self . postMessage ( { id, payload : { status : "invalid" } } ) ;
129+ self . postMessage ( {
130+ id,
131+ payload : { status : "invalid" } ,
132+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
117133 }
118134 } else {
119- self . postMessage ( { id, payload : { status : "invalid" } } ) ;
135+ self . postMessage ( {
136+ id,
137+ payload : { status : "invalid" } ,
138+ } satisfies WorkerResponse [ "checkSyntax" ] ) ;
120139 }
121140 }
122141}
123142
124- async function restoreState ( id , payload ) {
143+ async function restoreState ( { id, payload } : WorkerRequest [ "restoreState" ] ) {
125144 // Re-execute all previously successful commands to restore state
126145 const { commands } = payload ;
127146 jsOutput = [ ] ; // Clear output for restoration
@@ -136,29 +155,32 @@ async function restoreState(id, payload) {
136155 }
137156
138157 jsOutput = [ ] ; // Clear any output from restoration
139- self . postMessage ( { id, payload : { } } ) ;
158+ self . postMessage ( {
159+ id,
160+ payload : { } ,
161+ } satisfies WorkerResponse [ "restoreState" ] ) ;
140162}
141163
142- self . onmessage = async ( event ) => {
143- const { id, type, payload } = event . data ;
144- switch ( type ) {
164+ self . onmessage = async ( event : MessageEvent < WorkerRequest [ MessageType ] > ) => {
165+ switch ( event . data . type ) {
145166 case "init" :
146- await init ( id , payload ) ;
167+ await init ( event . data ) ;
147168 return ;
148169 case "runCode" :
149- await runCode ( id , payload ) ;
170+ await runCode ( event . data ) ;
150171 return ;
151172 case "runFile" :
152- runFile ( id , payload ) ;
173+ runFile ( event . data ) ;
153174 return ;
154175 case "checkSyntax" :
155- await checkSyntax ( id , payload ) ;
176+ await checkSyntax ( event . data ) ;
156177 return ;
157178 case "restoreState" :
158- await restoreState ( id , payload ) ;
179+ await restoreState ( event . data ) ;
159180 return ;
160181 default :
161- originalConsole . error ( `Unknown message type: ${ type } ` ) ;
182+ event . data satisfies never ;
183+ originalConsole . error ( `Unknown message: ${ event . data } ` ) ;
162184 return ;
163185 }
164186} ;
0 commit comments