UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 4.29 kB
import*as i18n from"../../core/i18n/i18n.js";import*as SDK from"../../core/sdk/sdk.js";let lastId=1;export class ProtocolService{mainSessionId;mainFrameId;mainTargetId;targetInfos;parallelConnection;lighthouseWorkerPromise;lighthouseMessageUpdateCallback;removeDialogHandler;configForTesting;async attach(){await SDK.TargetManager.TargetManager.instance().suspendAllTargets();const e=SDK.TargetManager.TargetManager.instance().primaryPageTarget();if(!e)throw new Error("Unable to find main target required for Lighthouse");const s=e.model(SDK.ChildTargetManager.ChildTargetManager);if(!s)throw new Error("Unable to find child target manager required for Lighthouse");const t=e.model(SDK.ResourceTreeModel.ResourceTreeModel);if(!t)throw new Error("Unable to find resource tree model required for Lighthouse");const a=t.mainFrame;if(!a)throw new Error("Unable to find main frame required for Lighthouse");const{connection:r,sessionId:i}=await s.createParallelConnection((e=>{"string"==typeof e&&(e=JSON.parse(e)),this.dispatchProtocolMessage(e)})),o=()=>{e.pageAgent().invoke_handleJavaScriptDialog({accept:!0})};t.addEventListener(SDK.ResourceTreeModel.Events.JavaScriptDialogOpening,o),this.removeDialogHandler=()=>t.removeEventListener(SDK.ResourceTreeModel.Events.JavaScriptDialogOpening,o),this.parallelConnection=r,this.targetInfos=s.targetInfos(),this.mainFrameId=a.id,this.mainTargetId=e.id(),this.mainSessionId=i}getLocales(){return[i18n.DevToolsLocale.DevToolsLocale.instance().locale]}async startTimespan(e){const{inspectedURL:s,categoryIDs:t,flags:a}=e;if(!(this.mainFrameId&&this.mainSessionId&&this.mainTargetId&&this.targetInfos))throw new Error("Unable to get target info required for Lighthouse");await this.sendWithResponse("startTimespan",{url:s,categoryIDs:t,flags:a,config:this.configForTesting,locales:this.getLocales(),mainSessionId:this.mainSessionId,mainFrameId:this.mainFrameId,mainTargetId:this.mainTargetId,targetInfos:this.targetInfos})}async collectLighthouseResults(e){const{inspectedURL:s,categoryIDs:t,flags:a}=e;if(!(this.mainFrameId&&this.mainSessionId&&this.mainTargetId&&this.targetInfos))throw new Error("Unable to get target info required for Lighthouse");let r=a.mode;return"timespan"===r&&(r="endTimespan"),this.sendWithResponse(r,{url:s,categoryIDs:t,flags:a,config:this.configForTesting,locales:this.getLocales(),mainSessionId:this.mainSessionId,mainFrameId:this.mainFrameId,mainTargetId:this.mainTargetId,targetInfos:this.targetInfos})}async detach(){const e=this.lighthouseWorkerPromise,s=this.parallelConnection;this.lighthouseWorkerPromise=void 0,this.parallelConnection=void 0,e&&(await e).terminate(),s&&await s.disconnect(),await SDK.TargetManager.TargetManager.instance().resumeAllTargets(),this.removeDialogHandler?.()}registerStatusCallback(e){this.lighthouseMessageUpdateCallback=e}dispatchProtocolMessage(e){const s=e;(s.sessionId||s.method&&s.method.startsWith("Target"))&&this.send("dispatchProtocolMessage",{message:e})}initWorker(){return this.lighthouseWorkerPromise=new Promise((e=>{const s=new URL("../../entrypoints/lighthouse_worker/lighthouse_worker.js",import.meta.url),t=new URL(self.location.href).searchParams.get("remoteBase");t&&s.searchParams.set("remoteBase",t);const a=new Worker(s,{type:"module"});a.addEventListener("message",(s=>{"workerReady"!==s.data?this.onWorkerMessage(s):e(a)}))})),this.lighthouseWorkerPromise}async ensureWorkerExists(){let e;return e=this.lighthouseWorkerPromise?await this.lighthouseWorkerPromise:await this.initWorker(),e}onWorkerMessage(e){const s=e.data;"statusUpdate"===s.action?this.lighthouseMessageUpdateCallback&&s.args&&"message"in s.args&&this.lighthouseMessageUpdateCallback(s.args.message):"sendProtocolMessage"===s.action&&s.args&&"message"in s.args&&this.sendProtocolMessage(s.args.message)}sendProtocolMessage(e){this.parallelConnection&&this.parallelConnection.sendRawMessage(e)}async send(e,s={}){const t=await this.ensureWorkerExists(),a=lastId++;t.postMessage({id:a,action:e,args:{...s,id:a}})}async sendWithResponse(e,s={}){const t=await this.ensureWorkerExists(),a=lastId++,r=new Promise((e=>{const s=r=>{const i=r.data;i.id===a&&(t.removeEventListener("message",s),e(i.result))};t.addEventListener("message",s)}));return t.postMessage({id:a,action:e,args:{...s,id:a}}),r}}