1- const global = self as any
2- global . importScripts ( 'https://cdn.jsdelivr.net/pyodide/v0.22.1/full/pyodide.js' )
1+ declare function importScripts ( ...urls : string [ ] ) : void
2+
3+ importScripts ( 'https://cdn.jsdelivr.net/pyodide/v0.22.1/full/pyodide.js' )
4+
5+ const scope = self as any
36
47async function loadPyodideAndPackages ( ) : Promise < any [ ] > {
5- global . pyodide = await global . loadPyodide ( )
6- await global . pyodide . loadPackage ( 'micropip' )
8+ scope . pyodide = await scope . loadPyodide ( )
9+
10+ await scope . pyodide . loadPackage ( 'micropip' )
11+
12+ const micropip = scope . pyodide . pyimport ( 'micropip' )
713
8- const micropip = global . pyodide . pyimport ( 'micropip' )
914 await micropip . install ( 'sqlglot' )
15+
1016 const file = await (
1117 await fetch ( new URL ( './sqlglot.py' , import . meta. url ) )
1218 ) . text ( )
1319
14- global . postMessage ( { topic : 'init' } )
20+ scope . postMessage ( { topic : 'init' } )
1521
16- return Array . from ( global . pyodide . runPython ( file ) )
22+ return Array . from ( scope . pyodide . runPython ( file ) )
1723}
1824
1925const pyodideReadyPromise = loadPyodideAndPackages ( )
2026
21- global . onmessage = async ( e : MessageEvent ) => {
27+ scope . onmessage = async ( e : MessageEvent ) => {
2228 const [ parse , get_dialect , dialects ] = await pyodideReadyPromise
2329
2430 if ( e . data . topic === 'parse' ) {
@@ -33,7 +39,7 @@ global.onmessage = async (e: MessageEvent) => {
3339 }
3440 }
3541
36- global . postMessage ( {
42+ scope . postMessage ( {
3743 topic : 'parse' ,
3844 payload,
3945 } )
@@ -44,7 +50,7 @@ global.onmessage = async (e: MessageEvent) => {
4450 get_dialect ( e . data . payload ) ,
4551 )
4652
47- global . postMessage ( {
53+ scope . postMessage ( {
4854 topic : 'dialect' ,
4955 payload : {
5056 types : `${ types } ` . toLowerCase ( ) ,
@@ -54,7 +60,7 @@ global.onmessage = async (e: MessageEvent) => {
5460 }
5561
5662 if ( e . data . topic === 'dialects' ) {
57- global . postMessage ( {
63+ scope . postMessage ( {
5864 topic : 'dialects' ,
5965 payload : {
6066 dialects : JSON . parse ( dialects ) ,
0 commit comments