"use strict";(self.webpackChunk_JUPYTERLAB_CORE_OUTPUT=self.webpackChunk_JUPYTERLAB_CORE_OUTPUT||[]).push([[2720,339],{63019:(e,t,s)=>{s.r(t),s.d(t,{IServiceWorkerManager:()=>n,SERVICE_WORKER_BROADCAST_CHANNEL_ID:()=>l,ServiceWorkerManager:()=>h,WORKER_NAME:()=>o});var r=s(82780);const i=s.p+"service-worker.js",n=new r.Token("@jupyterlite/server:IServiceWorkerManager"),o=`${i}`.split("/").slice(-1)[0];var a=s(18091),c=s(76988),g=s(50713);const l="/sw-api.v1",d=a.PageConfig.getOption("appVersion");class h{constructor(e){var t;this._onBroadcastMessage=async e=>{const{data:t,browsingContextId:s,pathname:r}=e.data;s===this._browsingContextId&&(r.includes("/api/stdin/")?this._onStdinMessage(r,t):this._onDriveMessage(t))},this._onDriveMessage=async e=>{const t=await this._driveContentsProcessor.processDriveRequest(e);this._broadcastChannel.postMessage({response:t,browsingContextId:this._browsingContextId})},this._onStdinMessage=async(e,t)=>{const s=e.slice(e.lastIndexOf("/")+1),r=this._stdinHandlers.get(s);if(void 0!==r){const e=await r(t);this._broadcastChannel.postMessage({response:e,browsingContextId:this._browsingContextId})}else console.warn(`No stdin handler registered for '${e}'`)},this._registration=null,this._registrationChanged=new g.Signal(this),this._ready=new r.PromiseDelegate,this._stdinHandlers=new Map;const s=null!==(t=e.workerUrl)&&void 0!==t?t:a.URLExt.join(a.PageConfig.getBaseUrl(),o),i=new URL(s,window.location.href),n=a.PageConfig.getOption("enableServiceWorkerCache")||"false";i.searchParams.set("enableCache",n),this._browsingContextId=r.UUID.uuid4(),this._contents=e.contents,this._broadcastChannel=new BroadcastChannel(l),this._broadcastChannel.addEventListener("message",this._onBroadcastMessage),this._driveContentsProcessor=new c.DriveContentsProcessor({contentsManager:this._contents}),this._initialize(i.href).catch(console.warn)}get registrationChanged(){return this._registrationChanged}get enabled(){return null!==this._registration}get browsingContextId(){return this._browsingContextId}get ready(){return this._ready.promise}registerStdinHandler(e,t){this._stdinHandlers.set(e,t)}async _initialize(e){const{serviceWorker:t}=navigator;let s=null;if(t){if(t.controller){const e=t.controller.scriptURL;await this._unregisterOldServiceWorkers(e),s=await t.getRegistration(e)||null,console.info("JupyterLite ServiceWorker was already registered")}if(!s&&t)try{console.info("Registering new JupyterLite ServiceWorker",e),s=await t.register(e),console.info("JupyterLite ServiceWorker was sucessfully registered")}catch(e){console.warn(e),console.warn(`JupyterLite ServiceWorker registration unexpectedly failed: ${e}`)}this._setRegistration(s),s?(this._ready.resolve(void 0),setTimeout(this._pingServiceWorker,2e4)):this._ready.reject(void 0)}else console.warn("ServiceWorkers not supported in this browser")}async _unregisterOldServiceWorkers(e){const t=`${e}-version`,s=localStorage.getItem(t);if(s&&s!==d||!s){console.info("New version, unregistering existing service workers.");const e=await navigator.serviceWorker.getRegistrations();await Promise.all(e.map((e=>e.unregister()))),console.info("All existing service workers have been unregistered.")}localStorage.setItem(t,d)}async _pingServiceWorker(){const e=await fetch("/api/service-worker-heartbeat");"ok"===await e.text()&&setTimeout(this._pingServiceWorker,2e4)}_setRegistration(e){this._registration=e,this._registrationChanged.emit(this._registration)}}}}]);