import{assert}from"./assert.js";import{helper}from"./helper.js";export class Tracing{constructor(e){this._recording=!1,this._path="",this._client=e}async start(e={}){assert(!this._recording,"Cannot start recording trace while already recording trace.");const t=["-*","devtools.timeline","v8.execute","disabled-by-default-devtools.timeline","disabled-by-default-devtools.timeline.frame","toplevel","blink.console","blink.user_timing","latencyInfo","disabled-by-default-devtools.timeline.stack","disabled-by-default-v8.cpu_profiler","disabled-by-default-v8.cpu_profiler.hires"],{path:s=null,screenshots:i=!1,categories:r=t}=e;i&&r.push("disabled-by-default-devtools.screenshot"),this._path=s,this._recording=!0,await this._client.send("Tracing.start",{transferMode:"ReturnAsStream",categories:r.join(",")})}async stop(){let e,t;const s=new Promise(((s,i)=>{e=s,t=i}));return this._client.once("Tracing.tracingComplete",(s=>{helper.readProtocolStream(this._client,s.stream,this._path).then(e,t)})),await this._client.send("Tracing.end"),this._recording=!1,s}}