@@ -18,30 +18,37 @@ export const createCircuitWebWorker = async (
1818 )
1919 }
2020
21- const rawWorker = new Worker (
22- configuration . webWorkerUrl ??
23- "https://unpkg.com/@tscircuit/eval-webworker/dist/webworker/index.js" ,
24- { type : "module" } ,
25- )
26- const webWorker = Comlink . wrap < InternalWebWorkerApi > ( rawWorker )
21+ let workerBlobUrl =
22+ configuration . webWorkerBlobUrl ?? configuration . webWorkerUrl
23+
24+ if ( ! workerBlobUrl ) {
25+ const cdnUrl =
26+ "https://cdn.jsdelivr.net/npm/@tscircuit/eval-webworker/dist/webworker/index.js"
27+
28+ const workerBlob = await fetch ( cdnUrl ) . then ( ( res ) => res . blob ( ) )
29+ workerBlobUrl = URL . createObjectURL ( workerBlob )
30+ }
31+
32+ const rawWorker = new Worker ( workerBlobUrl , { type : "module" } )
33+ const comlinkWorker = Comlink . wrap < InternalWebWorkerApi > ( rawWorker )
2734
2835 if ( configuration . snippetsApiBaseUrl ) {
29- await webWorker . setSnippetsApiBaseUrl ( configuration . snippetsApiBaseUrl )
36+ await comlinkWorker . setSnippetsApiBaseUrl ( configuration . snippetsApiBaseUrl )
3037 }
3138
3239 // Create a wrapper that handles events directly through circuit instance
3340 const wrapper : CircuitWebWorker = {
34- clearEventListeners : webWorker . clearEventListeners . bind ( webWorker ) ,
35- execute : webWorker . execute . bind ( webWorker ) ,
36- executeWithFsMap : webWorker . executeWithFsMap . bind ( webWorker ) ,
37- renderUntilSettled : webWorker . renderUntilSettled . bind ( webWorker ) ,
38- getCircuitJson : webWorker . getCircuitJson . bind ( webWorker ) ,
41+ clearEventListeners : comlinkWorker . clearEventListeners . bind ( comlinkWorker ) ,
42+ execute : comlinkWorker . execute . bind ( comlinkWorker ) ,
43+ executeWithFsMap : comlinkWorker . executeWithFsMap . bind ( comlinkWorker ) ,
44+ renderUntilSettled : comlinkWorker . renderUntilSettled . bind ( comlinkWorker ) ,
45+ getCircuitJson : comlinkWorker . getCircuitJson . bind ( comlinkWorker ) ,
3946 on : ( event : string , callback : ( ...args : any [ ] ) => void ) => {
4047 const proxiedCallback = Comlink . proxy ( callback )
41- webWorker . on ( event , proxiedCallback )
48+ comlinkWorker . on ( event , proxiedCallback )
4249 } ,
4350 kill : async ( ) => {
44- webWorker [ Comlink . releaseProxy ] ( )
51+ comlinkWorker [ Comlink . releaseProxy ] ( )
4552 rawWorker . terminate ( )
4653 } ,
4754 }
0 commit comments