@appetize/cli 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @appetize/cli
2
2
 
3
+ ## 0.20.0
4
+
5
+ ### Minor Changes
6
+
7
+ - feat: `--json` errors list their hints
8
+ - feat(session): session start --force replaces a session with the same id
9
+ - feat(session): session stop waits for the daemon to exit, unless --no-wait
10
+
11
+ ### Patch Changes
12
+
13
+ - fix: multiple active sessions error points at --help
14
+ - chore(license): declare the proprietary license
15
+ - docs(skill): drop guidance duplicated by `--help` and command output
16
+
3
17
  ## 0.19.0
4
18
 
5
19
  ### Minor Changes
package/LICENSE ADDED
@@ -0,0 +1 @@
1
+ Proprietary license. See your commercial agreement with Appetize.io for details.
package/cli.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
- import Ue from"sade";import{d as X,D as je,p as v,a as m,f as L,U as c,b as V,c as ze,i as Fe,e as Ce,r as H,g as k,h as Me,j as Be,s as qe,k as J,l as Ge,m as Ke,n as Ze,S as Y,o as Q,q as We,t as Xe,u as ee,v as Ve,w as He,x as Je,y as Ye,z as Qe,A as et}from"./config-D0ukqfLN.js";import{existsSync as te,openAsBlob as tt,openSync as nt,closeSync as ot}from"node:fs";import{basename as D,extname as it,resolve as ne,dirname as rt,join as S,relative as st}from"node:path";import{writeFile as oe,stat as at,rm as ct,cp as pt}from"node:fs/promises";import{spawn as dt}from"node:child_process";import{fileURLToPath as ie}from"node:url";import{humanId as lt}from"human-id";import{homedir as ut}from"node:os";import ft from"debug";import mt from"yocto-spinner";import ht from"cli-table3";import"stream";import"module";import"node:net";import"json-rpc-2.0";const re=["back","menu","unlock","volumeDown","volumeUp"],se=["home","lock"],ae=[...se,...re],N=["down","left","right","up"];async function wt(e,t={}){const n=await X(e,{signal:t.signal});return new je(n,{...t,onClose:()=>{n.close(),t.onClose?.()}})}const ce=[{header:"Target",key:"id"},{header:"App",key:"appId"},{header:"Platform",key:"platform"},{header:"Version",key:"versionName"},{header:"Build",key:"buildNumber"},{header:"Uploaded",key:"created"}];function _({appId:e,buildNumber:t,created:n,id:o,platform:i,versionName:r}){return{appId:e,buildNumber:t,created:n,id:o,platform:i,versionName:r}}const gt=["app"],yt=10,vt=[{fields:ce,key:"builds"},{header:"Total",key:"total"},{header:"Next page",key:"nextPage"}];async function St({options:e,signal:t}){const n=v(e.app,"--app"),o=m(e.limit,"--limit")??yt,i=m(e.page,"--page"),r={limit:String(o)};n!==void 0&&(r.appId=n),i!==void 0&&(r.page=String(i));const s=await L("/api/v2/builds",{commandOptions:e,query:r,signal:t});return{builds:s.builds.map(_),nextPage:s.nextPage,total:s.total}}const bt=["note","tags"],pe=12e4,xt=2e3,R=[".apk",".apks",".zip",".tar.gz",".tgz"],Et={".apk":"android",".apks":"android",".tar.gz":"ios",".tgz":"ios",".zip":"ios"},Ot=ce;async function kt({options:e,positionals:[t],progress:n,signal:o}){const i=$t(t),r=D(t);n.status(`Uploading ${r}`);const s=await At(t,i,e,o);if(e.wait!==!0)return n.status(`Uploaded ${r}`),_(s);n.step(`Uploaded ${r}`),n.status("Waiting for build metadata");const a=await Pt(s.id,e,o);return n.status(`Processed ${r}`),_(a)}async function Pt(e,t,n){const o=m(t.timeout,"--timeout")??pe,i=Date.now()+o;for(;;){const r=await L(`/api/v2/builds/${e}`,{commandOptions:t,signal:n});if(r.appId!==void 0)return r;if(Date.now()>=i)throw new Error(`Build ${e} is still being processed after ${String(o)}ms; find it with 'appetize build list'`);await V(xt,n)}}async function It(e){if(!te(e))throw new Error(`No build file at ${e}`);return await tt(e)}function $t(e){const t=e.toLowerCase(),n=R.find(o=>t.endsWith(o));if(n===void 0)throw new c(`Cannot tell the platform of ${D(e)}; upload a ${R.join(", ")} file`);return Et[n]}async function At(e,t,n,o){const i=new FormData;i.set("platform",t);const r=v(n.note,"--note");r!==void 0&&i.set("note",r);const s=v(n.tags,"--tags");return s!==void 0&&i.set("tags",s),i.set("file",await It(e),D(e)),await L("/api/v2/builds",{body:i,commandOptions:n,method:"POST",signal:o})}const Tt=["platform"],Lt=[{fields:[{header:"ID",key:"id"},{header:"Name",key:"name"},{header:"Platform",key:"platform"},{header:"OS versions",key:"osVersions"}],key:"devices"}];async function Dt({options:e,signal:t}){const n=Nt(e.platform);return{devices:(await ze(e,t)).filter(i=>n===void 0||i.platform===n).map(_t)}}function Nt(e){const t=v(e,"--platform");if(t!==void 0){if(!Fe(t))throw new c(`--platform expects one of: ${Ce.join(", ")}`);return t}}function _t({id:e,name:t,osVersions:n,platform:o}){return{id:e,name:t,osVersions:n,platform:o}}const Rt="APPETIZE_OUTPUT";function Ut(e,t,n){return{format:jt(e,t),progress:zt(e,n)}}function U(e,{extension:t,strict:n=!1}){const o=it(e);if(o==="")return`${e}${t}`;if(n&&o!==t)throw new c(`The output must be a path ending in ${t}`);return e}function jt(e,t){if(e.json===!0)return"json";if(e.json===!1)return"text";const n=process.env[Rt]?.trim();return n==="json"||n==="text"?n:t.isTTY===!0?"text":"json"}function zt(e,t){return e.quiet===!0?"silent":t.isTTY===!0&&process.env.TERM!=="dumb"&&!("CI"in process.env)?"interactive":"silent"}const b=["text","testId"],h="select",E={testId:{help:"Test id to match by exact string",suffix:"test-id"},text:{help:'Text to match by exact string or regex (e.g., "Sign in" or "/sign in/i")',suffix:"text"}},j="index",P="position",z="x",F="y";function de(e,{prefix:t=h}={}){return b.reduce((n,o)=>{const{help:i,suffix:r}=E[o];return n.option(`--${p(t,r)}`,i)},e).option(`--${p(t,j)}`,"Which match when multiple (0-based)")}function C(e,t={}){const{prefix:n=h}=t;return de(e,t).option(`--${p(n,P)}`,"Screen position to use as x,y (0-1), e.g., 0.5,0.75").option(`--${p(n,z)}`,"Horizontal position within the element (0-1, outside allowed)").option(`--${p(n,F)}`,"Vertical position within the element (0-1, outside allowed)")}function le(e,{prefix:t=h}={}){const n=B(e,t);if(n.length===0)throw new c(`A selector is required: pass one of ${$(b,t)}`);if(n.length>1)throw new c(`Only one selector may be given, got ${$(n,t)}`);const[o]=n,i=ge(e,p(t,E[o].suffix)),r=m(e[p(t,j)],`--${p(t,j)}`);switch(o){case"testId":return{by:"testId",index:r,testId:i};case"text":return{by:"text",index:r,text:Mt(i)}}}function ue(e){const t=m(e.timeout,"--timeout");return t===void 0?void 0:{timeout:t}}function Ft(e,{prefix:t=h}={}){return he(e,t)?le(e,{prefix:t}):void 0}function I(e,{prefix:t=h}={}){return he(e,t)?fe(e,{prefix:t}):void 0}function fe(e,{prefix:t=h}={}){const n=p(t,P),o=e[n],i=we(e,p(t,z)),r=we(e,p(t,F));if(o===void 0){if(B(e,t).length===0)throw new c(`A selector is required: pass one of ${O({prefix:t})}`);return{...le(e,{prefix:t}),x:i,y:r}}if(B(e,t).length>0)throw new c(`--${n} may not be combined with ${$(b,t)}`);if(i!==void 0||r!==void 0)throw new c(`--${n} may not be combined with --${p(t,z)} or --${p(t,F)}`);return Ct(ge(e,n),n)}function O(e={}){const{prefix:t=h}=e;return`${$(b,t)}, --${p(t,P)}`}function M(e={}){const{prefix:t=h}=e;return[...me(e),p(t,P)]}function me({prefix:e=h}={}){return b.map(t=>p(e,E[t].suffix))}function B(e,t){return b.filter(n=>e[p(t,E[n].suffix)]!==void 0)}function p(...e){return e.filter(Boolean).join("-")}function $(e,t){return e.map(n=>`--${p(t,E[n].suffix)}`).join(", ")}function he(e,t){return Object.entries(e).some(([n,o])=>o!==void 0&&n.startsWith(`${t}-`))}function we(e,t){return m(e[t],`--${t}`)}function Ct(e,t){const n=e.split(",");if(n.length!==2)throw new c(`--${t} expects x,y (0-1), e.g., 0.5,0.75`);const[o,i]=n.map(r=>Number(r.trim()));return{by:"position",x:H(o,`--${t}`),y:H(i,`--${t}`)}}function Mt(e){const t=/^\/(.*)\/([a-z]*)$/s.exec(e);return t?new RegExp(t[1],t[2]):e}function ge(e,t){return k(e[t],`--${t}`)}const Bt=me(),qt=async({device:e,options:t,positionals:[n]})=>{const o=Gt(n),i=Ft(t),r=await e.inspect(i,ue(t)),s=Kt(r,t.pretty===!0);return await oe(o,s),{bytes:Buffer.byteLength(s),nodes:ye(r.root),output:o}};function ye(e){return e.children.reduce((t,n)=>t+ye(n),1)}function Gt(e="inspect"){return U(e,{extension:".json"})}function Kt(e,t){return`${JSON.stringify(e,null,t?2:void 0)}
3
- `}const Zt=async({device:e})=>{await e.clearKeychain()},Wt=async({device:e,positionals:[t]})=>{await e.open(Xt(t))};function Xt(e){if(typeof e=="string")try{return new URL(e),e}catch{throw new c("<url> expects a URL, e.g., myapp://orders/42")}throw new c("<url> expects a URL, e.g., myapp://orders/42")}const Vt=new Set(ae),Ht=async({device:e,positionals:[t]})=>{if(!Jt(t))throw new c(`Button must be one of: ${ae.join(", ")}`);await e.press(t)};function Jt(e){return typeof e=="string"&&Vt.has(e)}const Yt=async({control:e,options:t,positionals:[n]})=>{const{path:o}=await e.recordStart({force:t.force===!0,path:en(n)});return{path:o}},Qt=async({control:e})=>{const{path:t}=await e.recordStop();return{path:t}};function en(e){const t=ne(U(e,{extension:".mp4",strict:!0})),n=rt(t);if(!te(n))throw new c(`No such directory: ${n}`);return t}const ve={landscapeLeft:!0,landscapeRight:!0,portrait:!0,upsideDown:!0},tn=async({device:e,positionals:[t]})=>{if(!nn(t))throw new c(`Orientation must be one of: ${Object.keys(ve).join(", ")}`);await e.rotate(t)};function nn(e){return typeof e=="string"&&Object.hasOwn(ve,e)}const on=async({device:e,positionals:[t]})=>{const n=await e.screenshot(),o=sn(n.mimeType,t);return await oe(o,n.data),{bytes:n.data.byteLength,output:o}};function rn(e){const t=e.split("/").at(1);return`.${t==="jpeg"?"jpg":t??"png"}`}function sn(e,t="screenshot"){return U(t,{extension:rn(e)})}function an(e){const t=ie(new URL("./daemon.js",import.meta.url)),n=nt(Me(e.sessionId),"a"),o=dt(process.execPath,[t],{detached:!0,env:{...process.env,[Be]:qe(e),DEBUG:cn(process.env.DEBUG)},stdio:["ignore",n,n]});if(ot(n),o.unref(),o.pid===void 0)throw new Error("Failed to spawn the session daemon");return o.pid}function cn(e){return e?`${e},appetize:daemon`:"appetize:daemon"}const pn=/^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$/i;function dn(){return lt({capitalize:!1,separator:"-"})}function ln(e){if(!pn.test(e))throw new c('--session-id must use letters, digits, ".", "-" or "_"');return e}const un=["device-os-version","open","proxy"],fn=150,mn={downloadingApp:"Downloading the app",installingApp:"Installing the app",launchingApp:"Launching the app",queued:"Queued",ready:"Device ready",requesting:"Requesting a device",starting:"Starting the device"},hn=[{header:"Session ID",key:"sessionId"},{header:"Web view",key:"viewerUrl"},{format:e=>`adb connect ${e}`,header:"adb",key:"adbSerial"},{header:"Host",key:"baseUrl"},{fields:[{header:"Device",key:"device"},{header:"Daemon",key:"daemon"},{header:"Network",key:"network"}],header:"Logs",key:"logs"},{hidden:!0,key:"controlSocket"}];async function wn(e,{onPhase:t,signal:n}){let o;for(;;){const i=J(e),r=i?.state?.phase;if(r&&r!==o&&(o=r,t?.(r)),i&&r==="ready"&&i.controlSocket!==void 0)return{...i,controlSocket:i.controlSocket};if(r==="error")throw new Error(i?.endedReason??"The session failed to start");if(r==="ended")throw new Error(i?.endedReason??"The session ended");await V(fn,n)}}function gn(e){const t=v(e,"--open");if(t!==void 0){try{new URL(t)}catch{throw new c("--open expects a URL, e.g., myapp://orders/42")}return t}}function yn(e){if(!(e===void 0||e===!1)){if(e===""||e==="intercept")return"intercept";if(typeof e!="string"||!Sn(e))throw new c("--proxy expects 'intercept' or an http:// url, e.g., http://proxy:8080");return e}}async function vn({options:e,positionals:[t,n],progress:o,signal:i}){const r=v(e["device-os-version"],"--device-os-version"),s=v(e["session-id"],"--session-id"),a=s===void 0?dn():ln(s);if(J(a)!==void 0)throw new Error(`Session ${a} already exists`);const d={device:r===void 0?{id:t}:{id:t,osVersion:r},endpoint:Ke(e),featureFlags:Ge(),open:gn(e.open),proxy:yn(e.proxy),sessionId:a,target:n};an(d),o.step(`Session ${a}`);const l=Ze(a,d.proxy);if(e.wait===!1)return{logs:l,sessionId:a};const u=await wn(a,{onPhase:f=>{bn(f)&&o.status(mn[f]??"Starting the session")},signal:i}).catch(f=>{throw Y.wrap(f,Q(a))});return{adbSerial:u.adbSerial,baseUrl:u.baseUrl,controlSocket:u.controlSocket,logs:l,sessionId:a,viewerUrl:u.viewerUrl}}function Sn(e){return e.startsWith("http://")}function bn(e){return e!=="ended"&&e!=="error"}const xn=async({record:{pid:e,sessionId:t}})=>(We(e)&&process.kill(e,"SIGTERM"),{sessionId:t}),Se="appetize",En=ie(new URL(`./skills/${Se}/`,import.meta.url)),q=["agents","claude","codex","copilot","cursor"],be=["project","user"],On={agents:".agents",claude:".claude",codex:".codex",copilot:".copilot",cursor:".cursor"},kn=S(".agents","skills"),Pn=["claude","agents"];function In(e){return q.some(t=>t===e)}function $n(e){return be.some(t=>t===e)}async function An(e){const t=e.agents??await Tn(e),n=await Promise.all(t.map(o=>xe(o,e)));return[...new Set(n.flat())]}async function xe(e,t){const n=Ee(e,t);if(e==="claude")return[S(n,"skills")];const o=S(t.root,kn);return t.scope==="project"?[o]:await Oe(n)?[o,S(n,"skills")]:[o]}function Ee(e,t){if(e==="claude"&&t.scope==="user"){const n=process.env.CLAUDE_CONFIG_DIR?.trim();if(n)return ne(n)}return S(t.root,On[e])}async function Tn(e){const n=(await Promise.all(q.map(async o=>await Ln(o,e)?o:void 0))).filter(o=>o!==void 0);return n.length>0?n:Pn}async function Ln(e,t){const n=[Ee(e,t),...await xe(e,t)];return(await Promise.all(n.map(i=>Oe(i)))).includes(!0)}async function Oe(e){try{return(await at(e)).isDirectory()}catch{return!1}}const Dn=["agent","scope"];async function Nn({options:e,signal:t}){const n=Un(e.scope),o=Rn(e.agent),i=n==="user"?ut():process.cwd(),r=await An({agents:o,root:i,scope:n}),s=[];for(const a of r){t.throwIfAborted();const d=S(a,Se);await ct(d,{force:!0,recursive:!0}),await pt(En,d,{recursive:!0}),s.push(n==="user"?d:st(i,d))}return{installed:s}}function _n(e){const t=k(e,"--agent");if(!In(t))throw new c(`--agent expects one of ${q.join(", ")}`);return t}function Rn(e){return e===void 0?void 0:(Array.isArray(e)?e:[e]).map(n=>_n(n))}function Un(e){if(e===void 0)return"project";const t=k(e,"--scope");if(!$n(t))throw new c(`--scope expects one of ${be.join(", ")}`);return t}const w="from",g="to",jn=[...M({prefix:w}),...M({prefix:g})],zn=async({device:e,options:t})=>{await e.swipe(Bn(t),Gn(t))};function Fn(e){return N.some(t=>t===e)}function Cn(e){if(e===void 0)return;const t=k(e,"--direction");if(!Fn(t))throw new c(`--direction expects one of ${N.join(", ")}`);return t}function Mn(e,t){const n=Xe(e.distance,"--distance");if(I(e,{prefix:g})!==void 0)throw new c(`--direction may not be combined with --${g}-*`);return{direction:t,distance:n,from:I(e,{prefix:w})}}function Bn(e){const t=Cn(e.direction);return t===void 0?qn(e):Mn(e,t)}function qn(e){if(e.distance!==void 0)throw new c("--distance is only valid with --direction");return{from:ke(I(e,{prefix:w}),w),to:ke(I(e,{prefix:g}),g)}}function Gn(e){const t=m(e.duration,"--duration"),n=m(e.timeout,"--timeout");return t===void 0&&n===void 0?void 0:{duration:t,timeout:n}}function ke(e,t){if(e===void 0)throw new c(`A swipe needs --direction, or both endpoints: pass one of ${O({prefix:t})}`);return e}const Kn=M(),Zn=async({device:e,options:t})=>{const n=fe(t);await e.tap(n,ue(t))},Wn=async({device:e,options:t,positionals:[n]})=>{await e.type(n,Xn(t))};function Xn(e){const t=m(e.delay,"--delay");return t===void 0?void 0:{delay:t}}const G=ft("appetize:cli"),Vn={fail:()=>{},status:()=>{},step:()=>{},succeed:()=>{}};function Hn(e,t,n){return e==="interactive"?Jn(t,n):Vn}function Jn(e,t){const n=mt({handleSignals:!1,stream:e});let o=0;const i=()=>{n.stop()};t.addEventListener("abort",i,{once:!0});const r=()=>(process.off("exit",i),n.isSpinning);return{fail:()=>{r()&&n.stop()},status:s=>{if(!t.aborted){if(n.isSpinning){n.text=s;return}o=Date.now(),process.once("exit",i),n.start(s)}},step:s=>{e.write(`\u25B8 ${s}
4
- `)},succeed:()=>{if(!r())return;const s=Math.round((Date.now()-o)/1e3);n.success(`${n.text} (${String(s)}s)`)}}}const Yn=new Set(["adb","api","apk","id","os","url"]),Qn={bottom:"","bottom-left":"","bottom-mid":"","bottom-right":"",left:"","left-mid":"",mid:"","mid-mid":"",middle:" ",right:"","right-mid":"",top:"","top-left":"","top-mid":"","top-right":""},Pe=2,eo=10;function to(e){return e.split(/(?=[A-Z])/).map((t,n)=>{const o=t.toLowerCase();return Yn.has(o)?o.toUpperCase():n===0?o.charAt(0).toUpperCase()+o.slice(1):o}).join(" ")}function no(e,t,n){return Z(e)?De(e,t,n):A(e,{fields:t,key:""},n)}function oo(e,t){const n=[];for(let o=0;o<e.length;o+=t)n.push(e.slice(o,o+t));return n.length===0?[e]:n}function Ie(e){return new ht({chars:Qn,head:e,style:{border:[],head:[],"padding-left":0,"padding-right":Pe}})}function $e(e,t){return e.find(n=>n.key===t)}function Ae(e,t){return t?.header??to(e)}function K(e){return e!=null}function Z(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Te(e){return["boolean","number","string"].includes(typeof e)}function Le(e,t){const n=new Set(t),o=e.map(r=>r.key).filter(r=>n.has(r)),i=new Set(e.filter(r=>r.hidden).map(r=>r.key));return[...new Set([...o,...t])].filter(r=>!i.has(r))}function De(e,t,n){const o=new Map(Object.entries(e)),i=Le(t,[...o.keys()]).filter(u=>K(o.get(u))),r=i.map(u=>$e(t,u)),s=i.map((u,f)=>Ae(u,r[f])),a=Math.max(0,...s.map(u=>u.length))+Pe,d=n===void 0?void 0:n-a-1,l=Ie();return l.push(...i.map((u,f)=>[s[f],A(o.get(u),r[f],d)])),Ne(l.toString())}function io(e,t){const n=e.map(s=>new Map(Object.entries(s))),o=Le(t,n.flatMap(s=>[...s.keys()])),i=o.map(s=>$e(t,s)),r=Ie(o.map((s,a)=>Ae(s,i[a])));for(const s of n)r.push(o.map((a,d)=>{const l=s.get(a);return K(l)?A(l,i[d]):""}));return Ne(r.toString())}function A(e,t,n){if(t?.format)return T(t.format(e),n);if(Te(e))return T(ee(e),n);if(Array.isArray(e)){const o=e.filter(K);return o.every(Te)?T(ee(o),n):o.every(Z)?io(o,t?.fields??[]):o.map(i=>A(i,t,n)).join(`
5
- `)}return Z(e)?De(e,t?.fields??[],n):T(String(e),n)}function Ne(e){return e.split(`
2
+ import Be from"sade";import{d as Y,D as qe,p as v,a as f,f as N,U as c,b as _,c as Ge,i as Ke,e as Ze,r as Q,g as $,h as We,j as Xe,s as Ve,k as He,l as ee,m as te,S as R,n as Je,o as Ye,q as ne,t as Qe,u as et,v as tt,w as nt,x as oe,y as ot,z as it,A as st,B as rt,C as at}from"./config-DfwUvfKT.js";import{existsSync as ie,openAsBlob as ct,openSync as pt,closeSync as dt}from"node:fs";import{basename as U,extname as lt,resolve as se,dirname as ut,join as S,relative as ft}from"node:path";import{writeFile as re,stat as mt,rm as ht,cp as wt}from"node:fs/promises";import{spawn as gt}from"node:child_process";import{fileURLToPath as ae}from"node:url";import{humanId as yt}from"human-id";import{homedir as vt}from"node:os";import St from"debug";import xt from"yocto-spinner";import bt from"cli-table3";import"stream";import"module";import"node:net";import"json-rpc-2.0";const ce=["back","menu","unlock","volumeDown","volumeUp"],pe=["home","lock"],de=[...pe,...ce],j=["down","left","right","up"];async function Et(e,t={}){const n=await Y(e,{signal:t.signal});return new qe(n,{...t,onClose:()=>{n.close(),t.onClose?.()}})}const le=[{header:"Target",key:"id"},{header:"App",key:"appId"},{header:"Platform",key:"platform"},{header:"Version",key:"versionName"},{header:"Build",key:"buildNumber"},{header:"Uploaded",key:"created"}];function z({appId:e,buildNumber:t,created:n,id:o,platform:i,versionName:s}){return{appId:e,buildNumber:t,created:n,id:o,platform:i,versionName:s}}const Ot=["app"],It=10,kt=[{fields:le,key:"builds"},{header:"Total",key:"total"},{header:"Next page",key:"nextPage"}];async function $t({options:e,signal:t}){const n=v(e.app,"--app"),o=f(e.limit,"--limit")??It,i=f(e.page,"--page"),s={limit:String(o)};n!==void 0&&(s.appId=n),i!==void 0&&(s.page=String(i));const r=await N("/api/v2/builds",{commandOptions:e,query:s,signal:t});return{builds:r.builds.map(z),nextPage:r.nextPage,total:r.total}}const At=["note","tags"],ue=12e4,Pt=2e3,F=[".apk",".apks",".zip",".tar.gz",".tgz"],Tt={".apk":"android",".apks":"android",".tar.gz":"ios",".tgz":"ios",".zip":"ios"},Dt=le;async function Lt({options:e,positionals:[t],progress:n,signal:o}){const i=Rt(t),s=U(t);n.status(`Uploading ${s}`);const r=await Ut(t,i,e,o);if(e.wait!==!0)return n.status(`Uploaded ${s}`),z(r);n.step(`Uploaded ${s}`),n.status("Waiting for build metadata");const d=await Nt(r.id,e,o);return n.status(`Processed ${s}`),z(d)}async function Nt(e,t,n){const o=f(t.timeout,"--timeout")??ue,i=Date.now()+o;for(;;){const s=await N(`/api/v2/builds/${e}`,{commandOptions:t,signal:n});if(s.appId!==void 0)return s;if(Date.now()>=i)throw new Error(`Build ${e} is still being processed after ${String(o)}ms; find it with 'appetize build list'`);await _(Pt,n)}}async function _t(e){if(!ie(e))throw new Error(`No build file at ${e}`);return await ct(e)}function Rt(e){const t=e.toLowerCase(),n=F.find(o=>t.endsWith(o));if(n===void 0)throw new c(`Cannot tell the platform of ${U(e)}; upload a ${F.join(", ")} file`);return Tt[n]}async function Ut(e,t,n,o){const i=new FormData;i.set("platform",t);const s=v(n.note,"--note");s!==void 0&&i.set("note",s);const r=v(n.tags,"--tags");return r!==void 0&&i.set("tags",r),i.set("file",await _t(e),U(e)),await N("/api/v2/builds",{body:i,commandOptions:n,method:"POST",signal:o})}const jt=["platform"],zt=[{fields:[{header:"ID",key:"id"},{header:"Name",key:"name"},{header:"Platform",key:"platform"},{header:"OS versions",key:"osVersions"}],key:"devices"}];async function Ft({options:e,signal:t}){const n=Ct(e.platform);return{devices:(await Ge(e,t)).filter(i=>n===void 0||i.platform===n).map(Mt)}}function Ct(e){const t=v(e,"--platform");if(t!==void 0){if(!Ke(t))throw new c(`--platform expects one of: ${Ze.join(", ")}`);return t}}function Mt({id:e,name:t,osVersions:n,platform:o}){return{id:e,name:t,osVersions:n,platform:o}}const Bt="APPETIZE_OUTPUT";function qt(e,t,n){return{format:Gt(e,t),progress:Kt(e,n)}}function C(e,{extension:t,strict:n=!1}){const o=lt(e);if(o==="")return`${e}${t}`;if(n&&o!==t)throw new c(`The output must be a path ending in ${t}`);return e}function Gt(e,t){if(e.json===!0)return"json";if(e.json===!1)return"text";const n=process.env[Bt]?.trim();return n==="json"||n==="text"?n:t.isTTY===!0?"text":"json"}function Kt(e,t){return e.quiet===!0?"silent":t.isTTY===!0&&process.env.TERM!=="dumb"&&!("CI"in process.env)?"interactive":"silent"}const x=["text","testId"],m="select",E={testId:{help:"Test id to match by exact string",suffix:"test-id"},text:{help:'Text to match by exact string or regex (e.g., "Sign in" or "/sign in/i")',suffix:"text"}},M="index",A="position",B="x",q="y";function fe(e,{prefix:t=m}={}){return x.reduce((n,o)=>{const{help:i,suffix:s}=E[o];return n.option(`--${p(t,s)}`,i)},e).option(`--${p(t,M)}`,"Which match when multiple (0-based)")}function G(e,t={}){const{prefix:n=m}=t;return fe(e,t).option(`--${p(n,A)}`,"Screen position to use as x,y (0-1), e.g., 0.5,0.75").option(`--${p(n,B)}`,"Horizontal position within the element (0-1, outside allowed)").option(`--${p(n,q)}`,"Vertical position within the element (0-1, outside allowed)")}function me(e,{prefix:t=m}={}){const n=Z(e,t);if(n.length===0)throw new c(`A selector is required: pass one of ${T(x,t)}`);if(n.length>1)throw new c(`Only one selector may be given, got ${T(n,t)}`);const[o]=n,i=Se(e,p(t,E[o].suffix)),s=f(e[p(t,M)],`--${p(t,M)}`);switch(o){case"testId":return{by:"testId",index:s,testId:i};case"text":return{by:"text",index:s,text:Xt(i)}}}function he(e){const t=f(e.timeout,"--timeout");return t===void 0?void 0:{timeout:t}}function Zt(e,{prefix:t=m}={}){return ye(e,t)?me(e,{prefix:t}):void 0}function P(e,{prefix:t=m}={}){return ye(e,t)?we(e,{prefix:t}):void 0}function we(e,{prefix:t=m}={}){const n=p(t,A),o=e[n],i=ve(e,p(t,B)),s=ve(e,p(t,q));if(o===void 0){if(Z(e,t).length===0)throw new c(`A selector is required: pass one of ${O({prefix:t})}`);return{...me(e,{prefix:t}),x:i,y:s}}if(Z(e,t).length>0)throw new c(`--${n} may not be combined with ${T(x,t)}`);if(i!==void 0||s!==void 0)throw new c(`--${n} may not be combined with --${p(t,B)} or --${p(t,q)}`);return Wt(Se(e,n),n)}function O(e={}){const{prefix:t=m}=e;return`${T(x,t)}, --${p(t,A)}`}function K(e={}){const{prefix:t=m}=e;return[...ge(e),p(t,A)]}function ge({prefix:e=m}={}){return x.map(t=>p(e,E[t].suffix))}function Z(e,t){return x.filter(n=>e[p(t,E[n].suffix)]!==void 0)}function p(...e){return e.filter(Boolean).join("-")}function T(e,t){return e.map(n=>`--${p(t,E[n].suffix)}`).join(", ")}function ye(e,t){return Object.entries(e).some(([n,o])=>o!==void 0&&n.startsWith(`${t}-`))}function ve(e,t){return f(e[t],`--${t}`)}function Wt(e,t){const n=e.split(",");if(n.length!==2)throw new c(`--${t} expects x,y (0-1), e.g., 0.5,0.75`);const[o,i]=n.map(s=>Number(s.trim()));return{by:"position",x:Q(o,`--${t}`),y:Q(i,`--${t}`)}}function Xt(e){const t=/^\/(.*)\/([a-z]*)$/s.exec(e);return t?new RegExp(t[1],t[2]):e}function Se(e,t){return $(e[t],`--${t}`)}const Vt=ge(),Ht=async({device:e,options:t,positionals:[n]})=>{const o=Jt(n),i=Zt(t),s=await e.inspect(i,he(t)),r=Yt(s,t.pretty===!0);return await re(o,r),{bytes:Buffer.byteLength(r),nodes:xe(s.root),output:o}};function xe(e){return e.children.reduce((t,n)=>t+xe(n),1)}function Jt(e="inspect"){return C(e,{extension:".json"})}function Yt(e,t){return`${JSON.stringify(e,null,t?2:void 0)}
3
+ `}const Qt=async({device:e})=>{await e.clearKeychain()},en=async({device:e,positionals:[t]})=>{await e.open(tn(t))};function tn(e){if(typeof e=="string")try{return new URL(e),e}catch{throw new c("<url> expects a URL, e.g., myapp://orders/42")}throw new c("<url> expects a URL, e.g., myapp://orders/42")}const nn=new Set(de),on=async({device:e,positionals:[t]})=>{if(!sn(t))throw new c(`Button must be one of: ${de.join(", ")}`);await e.press(t)};function sn(e){return typeof e=="string"&&nn.has(e)}const rn=async({control:e,options:t,positionals:[n]})=>{const{path:o}=await e.recordStart({force:t.force===!0,path:cn(n)});return{path:o}},an=async({control:e})=>{const{path:t}=await e.recordStop();return{path:t}};function cn(e){const t=se(C(e,{extension:".mp4",strict:!0})),n=ut(t);if(!ie(n))throw new c(`No such directory: ${n}`);return t}const be={landscapeLeft:!0,landscapeRight:!0,portrait:!0,upsideDown:!0},pn=async({device:e,positionals:[t]})=>{if(!dn(t))throw new c(`Orientation must be one of: ${Object.keys(be).join(", ")}`);await e.rotate(t)};function dn(e){return typeof e=="string"&&Object.hasOwn(be,e)}const ln=async({device:e,positionals:[t]})=>{const n=await e.screenshot(),o=fn(n.mimeType,t);return await re(o,n.data),{bytes:n.data.byteLength,output:o}};function un(e){const t=e.split("/").at(1);return`.${t==="jpeg"?"jpg":t??"png"}`}function fn(e,t="screenshot"){return C(t,{extension:un(e)})}function mn(e){const t=ae(new URL("./daemon.js",import.meta.url)),n=pt(We(e.sessionId),"a"),o=gt(process.execPath,[t],{detached:!0,env:{...process.env,[Xe]:Ve(e),DEBUG:hn(process.env.DEBUG)},stdio:["ignore",n,n]});if(dt(n),o.unref(),o.pid===void 0)throw new Error("Failed to spawn the session daemon");return o.pid}function hn(e){return e?`${e},appetize:daemon`:"appetize:daemon"}const wn=/^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$/i;function gn(){return yt({capitalize:!1,separator:"-"})}function yn(e){if(!wn.test(e))throw new c('--session-id must use letters, digits, ".", "-" or "_"');return e}const Ee=1e4;function Oe(e){if(!He(e))return!1;try{process.kill(e.pid,"SIGTERM")}catch{}return!0}async function Ie(e,t){(Oe(e)||Sn(e))&&await vn(e,t)}async function vn({pid:e,sessionId:t},n){const o=te(e);try{await _(Ee,AbortSignal.any([n,o]))}catch(i){if(o.aborted||!ee(e))return;throw i}throw new R(`Session ${t} did not stop in time`,{sessionId:t})}function Sn({endedAt:e,pid:t}){return e!==void 0&&Date.now()-Date.parse(e)<Ee&&ee(t)}const xn=["device-os-version","open","proxy"],bn=150,En={downloadingApp:"Downloading the app",installingApp:"Installing the app",launchingApp:"Launching the app",queued:"Queued",ready:"Device ready",requesting:"Requesting a device",starting:"Starting the device"},On=[{header:"Session ID",key:"sessionId"},{header:"Web view",key:"viewerUrl"},{format:e=>`adb connect ${e}`,header:"adb",key:"adbSerial"},{header:"Host",key:"baseUrl"},{fields:[{header:"Device",key:"device"},{header:"Daemon",key:"daemon"},{header:"Network",key:"network"}],header:"Logs",key:"logs"},{hidden:!0,key:"controlSocket"}];async function In(e,{onPhase:t,signal:n}){let o;for(;;){const i=ne(e),s=i?.state?.phase;if(s&&s!==o&&(o=s,t?.(s)),i&&s==="ready"&&i.controlSocket!==void 0)return{...i,controlSocket:i.controlSocket};if(s==="error")throw new Error(i?.endedReason??"The session failed to start");if(s==="ended")throw new Error(i?.endedReason??"The session ended");await _(bn,n)}}function kn(e){const t=v(e,"--open");if(t!==void 0){try{new URL(t)}catch{throw new c("--open expects a URL, e.g., myapp://orders/42")}return t}}function $n(e){if(!(e===void 0||e===!1)){if(e===""||e==="intercept")return"intercept";if(typeof e!="string"||!Pn(e))throw new c("--proxy expects 'intercept' or an http:// url, e.g., http://proxy:8080");return e}}async function An({options:e,positionals:[t,n],progress:o,signal:i}){const s=v(e["device-os-version"],"--device-os-version"),r=v(e["session-id"],"--session-id"),d=e.force===!0;if(d&&r===void 0)throw new c("--force needs --session-id");const a=r===void 0?gn():yn(r),l={device:s===void 0?{id:t}:{id:t,osVersion:s},endpoint:Ye(e),featureFlags:Je(),open:kn(e.open),proxy:$n(e.proxy),sessionId:a,target:n},u=ne(a);if(u!==void 0){if(!d)throw new Qe(a);o.status(`Replacing session ${a}`),await Ie(u,i),et(a)}mn(l),o.step(`Session ${a}`);const y=tt(a,l.proxy);if(e.wait===!1)return{logs:y,sessionId:a};const I=await In(a,{onPhase:k=>{Tn(k)&&o.status(En[k]??"Starting the session")},signal:i}).catch(k=>{throw R.wrap(k,a)});return{adbSerial:I.adbSerial,baseUrl:I.baseUrl,controlSocket:I.controlSocket,logs:y,sessionId:a,viewerUrl:I.viewerUrl}}function Pn(e){return e.startsWith("http://")}function Tn(e){return e!=="ended"&&e!=="error"}const Dn=async({options:e,progress:t,record:n,signal:o})=>(e.wait===!1?Oe(n):(t.status(`Stopping session ${n.sessionId}`),await Ie(n,o)),{sessionId:n.sessionId}),ke="appetize",Ln=ae(new URL(`./skills/${ke}/`,import.meta.url)),W=["agents","claude","codex","copilot","cursor"],$e=["project","user"],Nn={agents:".agents",claude:".claude",codex:".codex",copilot:".copilot",cursor:".cursor"},_n=S(".agents","skills"),Rn=["claude","agents"];function Un(e){return W.some(t=>t===e)}function jn(e){return $e.some(t=>t===e)}async function zn(e){const t=e.agents??await Fn(e),n=await Promise.all(t.map(o=>Ae(o,e)));return[...new Set(n.flat())]}async function Ae(e,t){const n=Pe(e,t);if(e==="claude")return[S(n,"skills")];const o=S(t.root,_n);return t.scope==="project"?[o]:await Te(n)?[o,S(n,"skills")]:[o]}function Pe(e,t){if(e==="claude"&&t.scope==="user"){const n=process.env.CLAUDE_CONFIG_DIR?.trim();if(n)return se(n)}return S(t.root,Nn[e])}async function Fn(e){const n=(await Promise.all(W.map(async o=>await Cn(o,e)?o:void 0))).filter(o=>o!==void 0);return n.length>0?n:Rn}async function Cn(e,t){const n=[Pe(e,t),...await Ae(e,t)];return(await Promise.all(n.map(i=>Te(i)))).includes(!0)}async function Te(e){try{return(await mt(e)).isDirectory()}catch{return!1}}const Mn=["agent","scope"];async function Bn({options:e,signal:t}){const n=Kn(e.scope),o=Gn(e.agent),i=n==="user"?vt():process.cwd(),s=await zn({agents:o,root:i,scope:n}),r=[];for(const d of s){t.throwIfAborted();const a=S(d,ke);await ht(a,{force:!0,recursive:!0}),await wt(Ln,a,{recursive:!0}),r.push(n==="user"?a:ft(i,a))}return{installed:r}}function qn(e){const t=$(e,"--agent");if(!Un(t))throw new c(`--agent expects one of ${W.join(", ")}`);return t}function Gn(e){return e===void 0?void 0:(Array.isArray(e)?e:[e]).map(n=>qn(n))}function Kn(e){if(e===void 0)return"project";const t=$(e,"--scope");if(!jn(t))throw new c(`--scope expects one of ${$e.join(", ")}`);return t}const h="from",w="to",Zn=[...K({prefix:h}),...K({prefix:w})],Wn=async({device:e,options:t})=>{await e.swipe(Jn(t),Qn(t))};function Xn(e){return j.some(t=>t===e)}function Vn(e){if(e===void 0)return;const t=$(e,"--direction");if(!Xn(t))throw new c(`--direction expects one of ${j.join(", ")}`);return t}function Hn(e,t){const n=nt(e.distance,"--distance");if(P(e,{prefix:w})!==void 0)throw new c(`--direction may not be combined with --${w}-*`);return{direction:t,distance:n,from:P(e,{prefix:h})}}function Jn(e){const t=Vn(e.direction);return t===void 0?Yn(e):Hn(e,t)}function Yn(e){if(e.distance!==void 0)throw new c("--distance is only valid with --direction");return{from:De(P(e,{prefix:h}),h),to:De(P(e,{prefix:w}),w)}}function Qn(e){const t=f(e.duration,"--duration"),n=f(e.timeout,"--timeout");return t===void 0&&n===void 0?void 0:{duration:t,timeout:n}}function De(e,t){if(e===void 0)throw new c(`A swipe needs --direction, or both endpoints: pass one of ${O({prefix:t})}`);return e}const eo=K(),to=async({device:e,options:t})=>{const n=we(t);await e.tap(n,he(t))},no=async({device:e,options:t,positionals:[n]})=>{await e.type(n,oo(t))};function oo(e){const t=f(e.delay,"--delay");return t===void 0?void 0:{delay:t}}const X=St("appetize:cli"),io={fail:()=>{},status:()=>{},step:()=>{},succeed:()=>{}};function so(e,t,n){return e==="interactive"?ro(t,n):io}function ro(e,t){const n=xt({handleSignals:!1,stream:e});let o=0;const i=()=>{n.stop()};t.addEventListener("abort",i,{once:!0});const s=()=>(process.off("exit",i),n.isSpinning);return{fail:()=>{s()&&n.stop()},status:r=>{if(!t.aborted){if(n.isSpinning){n.text=r;return}o=Date.now(),process.once("exit",i),n.start(r)}},step:r=>{e.write(`\u25B8 ${r}
4
+ `)},succeed:()=>{if(!s())return;const r=Math.round((Date.now()-o)/1e3);n.success(`${n.text} (${String(r)}s)`)}}}const ao=new Set(["adb","api","apk","id","os","url"]),co={bottom:"","bottom-left":"","bottom-mid":"","bottom-right":"",left:"","left-mid":"",mid:"","mid-mid":"",middle:" ",right:"","right-mid":"",top:"","top-left":"","top-mid":"","top-right":""},Le=2,po=10;function lo(e){return e.split(/(?=[A-Z])/).map((t,n)=>{const o=t.toLowerCase();return ao.has(o)?o.toUpperCase():n===0?o.charAt(0).toUpperCase()+o.slice(1):o}).join(" ")}function uo(e,t,n){return H(e)?ze(e,t,n):D(e,{fields:t,key:""},n)}function fo(e,t){const n=[];for(let o=0;o<e.length;o+=t)n.push(e.slice(o,o+t));return n.length===0?[e]:n}function Ne(e){return new bt({chars:co,head:e,style:{border:[],head:[],"padding-left":0,"padding-right":Le}})}function _e(e,t){return e.find(n=>n.key===t)}function Re(e,t){return t?.header??lo(e)}function V(e){return e!=null}function H(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Ue(e){return["boolean","number","string"].includes(typeof e)}function je(e,t){const n=new Set(t),o=e.map(s=>s.key).filter(s=>n.has(s)),i=new Set(e.filter(s=>s.hidden).map(s=>s.key));return[...new Set([...o,...t])].filter(s=>!i.has(s))}function ze(e,t,n){const o=new Map(Object.entries(e)),i=je(t,[...o.keys()]).filter(u=>V(o.get(u))),s=i.map(u=>_e(t,u)),r=i.map((u,y)=>Re(u,s[y])),d=Math.max(0,...r.map(u=>u.length))+Le,a=n===void 0?void 0:n-d-1,l=Ne();return l.push(...i.map((u,y)=>[r[y],D(o.get(u),s[y],a)])),Fe(l.toString())}function mo(e,t){const n=e.map(r=>new Map(Object.entries(r))),o=je(t,n.flatMap(r=>[...r.keys()])),i=o.map(r=>_e(t,r)),s=Ne(o.map((r,d)=>Re(r,i[d])));for(const r of n)s.push(o.map((d,a)=>{const l=r.get(d);return V(l)?D(l,i[a]):""}));return Fe(s.toString())}function D(e,t,n){if(t?.format)return L(t.format(e),n);if(Ue(e))return L(oe(e),n);if(Array.isArray(e)){const o=e.filter(V);return o.every(Ue)?L(oe(o),n):o.every(H)?mo(o,t?.fields??[]):o.map(i=>D(i,t,n)).join(`
5
+ `)}return H(e)?ze(e,t?.fields??[],n):L(String(e),n)}function Fe(e){return e.split(`
6
6
  `).map(t=>t.trimEnd()).join(`
7
- `)}function T(e,t){return t===void 0||t<eo?e:e.split(`
8
- `).flatMap(n=>ro(n,t)).join(`
9
- `)}function ro(e,t){const n=[];let o="";for(const i of e.split(" ").flatMap(r=>oo(r,t)))o===""?o=i:o.length+1+i.length<=t?o=`${o} ${i}`:(n.push(o),o=i);return n.push(o),n}function so(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");return wt(e.controlSocket,{baseUrl:e.baseUrl,signal:t})}async function ao(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");const n=await X(e.controlSocket,{signal:t}),{client:o}=Ve(n,{});return{close:()=>{n.close()},recordStart:async i=>await o.request("appetizeio.cli.record.start",i),recordStop:async()=>await o.request("appetizeio.cli.record.stop")}}function co(e){return async({options:t,positionals:n,progress:o,signal:i})=>{const r=t["session-id"],s=He(typeof r=="string"?r:void 0);try{return await e({options:t,positionals:n,progress:o,record:s,signal:AbortSignal.any([i,Je(s.pid)])})}catch(a){throw Y.wrap(a,Q(s.sessionId))}}}function po(e){return async({options:t,positionals:n,progress:o,record:i,signal:r})=>{const s=await so(i,r);try{return await e({device:s,options:t,positionals:n,progress:o,signal:r})}finally{s.close()}}}function lo(e){return async({options:t,positionals:n,progress:o,record:i,signal:r})=>{const s=await ao(i,r);try{return await e({control:s,options:t,positionals:n,progress:o,signal:r})}finally{s.close()}}}const uo=["session-id",...gt,...bt,...Tt,...Bt,...Dn,...un,...jn,...Kn],fo=["json","quiet"];function mo(e){const t=Ue("appetize").version(e).option("--json","Print the result as JSON").option("--quiet","Hide progress");return x(t,"session start <device-id> <target>",n=>n.describe(["Start a device session and wait until it is ready","","Environment variables:","APPETIZE_API_TOKEN: Authorizes private APIs and session targets","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--device-os-version","Device OS version (e.g., 17 for iOS, 14 for Android)").option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--no-wait","Return without waiting for the device session to be ready").option("--open","URL opened once the session is ready").option("--proxy","Route traffic through a proxy: 'intercept' to capture it, or an http url").option("--session-id","Session id; defaults to a generated 3-word id").example("session start iphone15pro app_a1b2c3").example("session start pixel7 app_a1b2c3 --device-os-version 14").example("session start iphone15pro app_a1b2c3 --open myapp://orders/42"),vn,{fields:hn}),W(t,"session stop",n=>n.describe("Stop a running device session"),xn),x(t,"build list",n=>n.describe(["List uploaded builds, printing session start targets","","Environment variables:","APPETIZE_API_TOKEN: Authorizes the listing request","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--app","App id to filter by (bundle or package id)").option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--limit","Builds per page (default 10, max 200)").option("--page","Page of results to fetch (default 1)").example("build list").example("build list --app com.example.app").example("build list --app com.example.app --page 2"),St,{fields:vt}),x(t,"build upload <file>",n=>n.describe([`Upload a build (${R.join(", ")}) as a session start target`,"","Environment variables:","APPETIZE_API_TOKEN: Authorizes the upload","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--note","Note to store with the build").option("--tags","Comma separated tags to store with the build").option("--timeout",`Milliseconds to wait with --wait (default ${String(pe)})`).option("--wait","Wait for the app id and version metadata to resolve").example("build upload ./app.apk").example("build upload ./app.zip").example("build upload ./app.apk --wait").example('build upload ./app.apk --tags beta,latest --note "PR 42"'),kt,{fields:Ot}),x(t,"device list",n=>n.describe(["List device models and their supported OS versions","","Environment variables:","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--platform","Platform to filter by: ios or android").example("device list").example("device list --platform ios"),Dt,{fields:Lt}),y(t,"press <button>",n=>n.describe(`Press a device button: ${se.join(", ")}; Android only: ${re.join(", ")}`).example("press home").example("press volumeUp"),Ht),y(t,"rotate <orientation>",n=>n.describe("Rotate the device to an orientation: portrait, landscapeLeft, landscapeRight or upsideDown").example("rotate portrait").example("rotate landscapeLeft"),tn),y(t,"open <url>",n=>n.describe("Open a URL or deep link on the device").example("open myapp://orders/42").example("open https://example.com"),Wt),y(t,"keychain clear",n=>n.describe("Clear the entire device keychain (iOS only)").example("keychain clear"),Zt),y(t,"screenshot [output]",n=>n.describe("Take a screenshot of the current device screen").example("screenshot").example("screenshot home-screen").example("screenshot ./shots/checkout.png"),on),y(t,"tap",n=>C(n.describe(["Tap an element matched by a selector, or a screen position","",`Pick one of: ${O()}`])).option("--timeout","Milliseconds to wait for the element").example("tap --select-text 'Log in'").example("tap --select-test-id save").example("tap --select-text '/^Item \\d+/i' --select-index 2").example("tap --select-position 0.5,0.75").example("tap --select-test-id slider --select-x 0.9").example("tap --select-test-id dialog --select-x=-0.1").example("tap --select-test-id row --select-y=1.2"),Zn),y(t,"swipe",n=>C(C(n.describe(["Swipe in a direction, or along a path between two points","",`Pick a --direction with an optional --${w} starting point,`,`or both a --${w}-* and a --${g}-* point to form a path`,"",`Pick one per point: ${O({prefix:w})}`,` ${O({prefix:g})}`]).option("--direction",`Direction to travel: ${N.join(", ")}`).option("--distance","Travel as a fraction of the screen (0-1)"),{prefix:w}),{prefix:g}).option("--duration","Milliseconds the gesture travels for").option("--timeout",`Milliseconds to wait for any --${w}/--${g} elements`).example("swipe --direction up").example("swipe --direction up --distance 0.25").example("swipe --direction left --from-test-id card --from-index 1").example("swipe --from-position 0.5,0.75 --to-position 0.5,0.3").example("swipe --from-text 'Drag me' --from-y 0.9 --to-test-id drop-zone"),zn),y(t,"inspect [output]",n=>de(n.describe("Capture the view hierarchy of the screen, or of an element matched by a selector")).option("--timeout","Milliseconds to wait for the element").option("--pretty","Pretty-print the JSON",!1).example("inspect").example("inspect --select-text 'Log in'").example("inspect hierarchy.json --pretty"),qt),y(t,"type <text>",n=>n.describe(`Type text; {...} presses a named key: ${et.join(", ")}`).option("--delay","Milliseconds to wait between steps").example("type 'hello{Enter}'").example("type '{Backspace>16/}'").example("type 'user@example.com{Tab}hunter2{Enter}' --delay 50"),Wn),_e(t,"recording start <output>",n=>n.describe("Start recording the device video to an mp4 file").option("--force","Overwrite the file if it already exists",!1).example("recording start login-flow").example("recording start ./captures/checkout.mp4").example("recording start login-flow --force"),Yt),_e(t,"recording stop",n=>n.describe("Stop recording the device video and close the file").example("recording stop"),Qt),x(t,"skill install",n=>n.describe("Install the Appetize agent skill for coding agents").option("--agent","Agent to install for, repeatable; defaults to the agents detected").option("--scope","Install for the project (default) or the user").example("skill install").example("skill install --scope user").example("skill install --agent claude --agent cursor"),Nn),t}function x(e,t,n,o,{fields:i}={}){const r=t.split(" ").filter(s=>!/^[<[]/.test(s)).join(" ");return n(e.command(t)).action(go({action:o,fields:i,name:r}))}function W(e,t,n,o){return x(e,t,i=>n(i).option("--session-id","Target session id; defaults to the sole active session"),co(o))}function y(e,t,n,o){return W(e,t,n,po(o))}function _e(e,t,n,o){return W(e,t,n,lo(o))}function Re(e){return`${String(Date.now()-e)}ms`}function ho(e,t,n){if(e==="json")return JSON.stringify(t===void 0?{}:t,null,2);const o=t===void 0?"":no(t,n,process.stdout.columns);return o===""?void 0:o}async function wo({action:e,fields:t=[],name:n},o){const i=Date.now();G(`invoked: ${process.argv.slice(2).join(" ")}`);const r=Ye(o),{format:s,progress:a}=Ut(r.options,process.stdout,process.stderr),d=Hn(a,process.stderr,r.signal);try{const l=ho(s,await e({...r,progress:d}),t);d.succeed(),l!==void 0&&process.stdout.write(`${l}
10
- `),G(`ok in ${Re(i)}`)}catch(l){d.fail(),process.stderr.write(`${Qe(s,l,{command:n})}
11
- `),G(`failed after ${Re(i)}:`,l),process.exitCode=1}}function go(e){return(...t)=>{wo(e,t)}}mo("0.19.0").parse(process.argv,{boolean:fo,string:uo});
7
+ `)}function L(e,t){return t===void 0||t<po?e:e.split(`
8
+ `).flatMap(n=>ho(n,t)).join(`
9
+ `)}function ho(e,t){const n=[];let o="";for(const i of e.split(" ").flatMap(s=>fo(s,t)))o===""?o=i:o.length+1+i.length<=t?o=`${o} ${i}`:(n.push(o),o=i);return n.push(o),n}function wo(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");return Et(e.controlSocket,{baseUrl:e.baseUrl,signal:t})}async function go(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");const n=await Y(e.controlSocket,{signal:t}),{client:o}=ot(n,{});return{close:()=>{n.close()},recordStart:async i=>await o.request("appetizeio.cli.record.start",i),recordStop:async()=>await o.request("appetizeio.cli.record.stop")}}function yo(e){return async({options:t,positionals:n,progress:o,signal:i})=>{const s=t["session-id"],r=it(typeof s=="string"?s:void 0);try{return await e({options:t,positionals:n,progress:o,record:r,signal:AbortSignal.any([i,te(r.pid)])})}catch(d){throw R.wrap(d,r.sessionId)}}}function vo(e){return async({options:t,positionals:n,progress:o,record:i,signal:s})=>{const r=await wo(i,s);try{return await e({device:r,options:t,positionals:n,progress:o,signal:s})}finally{r.close()}}}function So(e){return async({options:t,positionals:n,progress:o,record:i,signal:s})=>{const r=await go(i,s);try{return await e({control:r,options:t,positionals:n,progress:o,signal:s})}finally{r.close()}}}const xo=["session-id",...Ot,...At,...jt,...Vt,...Mn,...xn,...Zn,...eo],bo=["json","quiet"];function Eo(e){const t=Be("appetize").version(e).option("--json","Print the result as JSON").option("--quiet","Hide progress");return b(t,"session start <device-id> <target>",n=>n.describe(["Start a device session and wait until it is ready","","Environment variables:","APPETIZE_API_TOKEN: Authorizes private APIs and session targets","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--device-os-version","Device OS version (e.g., 17 for iOS, 14 for Android)").option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--force","Replace a session with the same id",!1).option("--no-wait","Return without waiting for the device session to be ready").option("--open","URL opened once the session is ready").option("--proxy","Route traffic through a proxy: 'intercept' to capture it, or an http url").option("--session-id","Session id; defaults to a generated 3-word id").example("session start iphone15pro app_a1b2c3").example("session start pixel7 app_a1b2c3 --device-os-version 14").example("session start iphone15pro app_a1b2c3 --open myapp://orders/42").example("session start iphone15pro app_a1b2c3 --session-id demo --force"),An,{fields:On}),J(t,"session stop",n=>n.describe("Stop a device session and await its exit").option("--no-wait","Return without waiting for the session to exit").example("session stop --no-wait"),Dn),b(t,"build list",n=>n.describe(["List uploaded builds, printing session start targets","","Environment variables:","APPETIZE_API_TOKEN: Authorizes the listing request","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--app","App id to filter by (bundle or package id)").option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--limit","Builds per page (default 10, max 200)").option("--page","Page of results to fetch (default 1)").example("build list").example("build list --app com.example.app").example("build list --app com.example.app --page 2"),$t,{fields:kt}),b(t,"build upload <file>",n=>n.describe([`Upload a build (${F.join(", ")}) as a session start target`,"","Environment variables:","APPETIZE_API_TOKEN: Authorizes the upload","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--note","Note to store with the build").option("--tags","Comma separated tags to store with the build").option("--timeout",`Milliseconds to wait with --wait (default ${String(ue)})`).option("--wait","Wait for the app id and version metadata to resolve").example("build upload ./app.apk").example("build upload ./app.zip").example("build upload ./app.apk --wait").example('build upload ./app.apk --tags beta,latest --note "PR 42"'),Lt,{fields:Dt}),b(t,"device list",n=>n.describe(["List device models and their supported OS versions","","Environment variables:","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--platform","Platform to filter by: ios or android").example("device list").example("device list --platform ios"),Ft,{fields:zt}),g(t,"press <button>",n=>n.describe(`Press a device button: ${pe.join(", ")}; Android only: ${ce.join(", ")}`).example("press home").example("press volumeUp"),on),g(t,"rotate <orientation>",n=>n.describe("Rotate the device to an orientation: portrait, landscapeLeft, landscapeRight or upsideDown").example("rotate portrait").example("rotate landscapeLeft"),pn),g(t,"open <url>",n=>n.describe("Open a URL or deep link on the device").example("open myapp://orders/42").example("open https://example.com"),en),g(t,"keychain clear",n=>n.describe("Clear the entire device keychain (iOS only)").example("keychain clear"),Qt),g(t,"screenshot [output]",n=>n.describe("Take a screenshot of the current device screen").example("screenshot").example("screenshot home-screen").example("screenshot ./shots/checkout.png"),ln),g(t,"tap",n=>G(n.describe(["Tap an element matched by a selector, or a screen position","",`Pick one of: ${O()}`])).option("--timeout","Milliseconds to wait for the element").example("tap --select-text 'Log in'").example("tap --select-test-id save").example("tap --select-text '/^Item \\d+/i' --select-index 2").example("tap --select-position 0.5,0.75").example("tap --select-test-id slider --select-x 0.9").example("tap --select-test-id dialog --select-x=-0.1").example("tap --select-test-id row --select-y=1.2"),to),g(t,"swipe",n=>G(G(n.describe(["Swipe in a direction, or along a path between two points","",`Pick a --direction with an optional --${h} starting point,`,`or both a --${h}-* and a --${w}-* point to form a path`,"",`Pick one per point: ${O({prefix:h})}`,` ${O({prefix:w})}`]).option("--direction",`Direction to travel: ${j.join(", ")}`).option("--distance","Travel as a fraction of the screen (0-1)"),{prefix:h}),{prefix:w}).option("--duration","Milliseconds the gesture travels for").option("--timeout",`Milliseconds to wait for any --${h}/--${w} elements`).example("swipe --direction up").example("swipe --direction up --distance 0.25").example("swipe --direction left --from-test-id card --from-index 1").example("swipe --from-position 0.5,0.75 --to-position 0.5,0.3").example("swipe --from-text 'Drag me' --from-y 0.9 --to-test-id drop-zone"),Wn),g(t,"inspect [output]",n=>fe(n.describe("Capture the view hierarchy of the screen, or of an element matched by a selector")).option("--timeout","Milliseconds to wait for the element").option("--pretty","Pretty-print the JSON",!1).example("inspect").example("inspect --select-text 'Log in'").example("inspect hierarchy.json --pretty"),Ht),g(t,"type <text>",n=>n.describe(`Type text; {...} presses a named key: ${at.join(", ")}`).option("--delay","Milliseconds to wait between steps").example("type 'hello{Enter}'").example("type '{Backspace>16/}'").example("type 'user@example.com{Tab}hunter2{Enter}' --delay 50"),no),Ce(t,"recording start <output>",n=>n.describe("Start recording the device video to an mp4 file").option("--force","Overwrite the file if it already exists",!1).example("recording start login-flow").example("recording start ./captures/checkout.mp4").example("recording start login-flow --force"),rn),Ce(t,"recording stop",n=>n.describe("Stop recording the device video and close the file").example("recording stop"),an),b(t,"skill install",n=>n.describe("Install the Appetize agent skill for coding agents").option("--agent","Agent to install for, repeatable; defaults to the agents detected").option("--scope","Install for the project (default) or the user").example("skill install").example("skill install --scope user").example("skill install --agent claude --agent cursor"),Bn),t}function b(e,t,n,o,{fields:i}={}){const s=t.split(" ").filter(r=>!/^[<[]/.test(r)).join(" ");return n(e.command(t)).action(ko({action:o,fields:i,name:s}))}function J(e,t,n,o){return b(e,t,i=>n(i).option("--session-id","Target session id; defaults to the sole active session"),yo(o))}function g(e,t,n,o){return J(e,t,n,vo(o))}function Ce(e,t,n,o){return J(e,t,n,So(o))}function Me(e){return`${String(Date.now()-e)}ms`}function Oo(e,t,n){if(e==="json")return JSON.stringify(t===void 0?{}:t,null,2);const o=t===void 0?"":uo(t,n,process.stdout.columns);return o===""?void 0:o}async function Io({action:e,fields:t=[],name:n},o){const i=Date.now();X(`invoked: ${process.argv.slice(2).join(" ")}`);const s=st(o),{format:r,progress:d}=qt(s.options,process.stdout,process.stderr),a=so(d,process.stderr,s.signal);try{const l=Oo(r,await e({...s,progress:a}),t);a.succeed(),l!==void 0&&process.stdout.write(`${l}
10
+ `),X(`ok in ${Me(i)}`)}catch(l){a.fail(),process.stderr.write(`${rt(r,l,{command:n})}
11
+ `),X(`failed after ${Me(i)}:`,l),process.exitCode=1}}function ko(e){return(...t)=>{Io(e,t)}}Eo("0.20.0").parse(process.argv,{boolean:bo,string:xo});
@@ -0,0 +1,3 @@
1
+ import{Transform as cr}from"stream";import{createRequire as lr}from"module";import{connect as fr}from"node:net";import ze from"debug";import{JSONRPCServerAndClient as ur,JSONRPCServer as dr,JSONRPCClient as hr,JSONRPC as pr}from"json-rpc-2.0";import{writeFileSync as yr,renameSync as wr,mkdirSync as gt,readFileSync as mr,rmSync as br,readdirSync as gr}from"node:fs";import{sep as Er,join as te}from"node:path";import{homedir as Et}from"node:os";let je;try{je=new TextDecoder}catch{}let p,V,l=0;const Ve=[],Sr=105,vr=57342,Ar=57343,St=57337,vt=6,ue={};let me=11281e4,Y=1681e4,re=Ve,be=0,k={},_,ge,Ee=0,se=0,D,q,N=[],Ke=[],K,j,Se,At={useRecords:!1,mapsAsObjects:!0},ve=!1,Ot=2;try{new Function("")}catch{Ot=1/0}class pe{constructor(e){if(e&&((e.keyMap||e._keyMap)&&!e.useRecords&&(e.useRecords=!1,e.mapsAsObjects=!0),e.useRecords===!1&&e.mapsAsObjects===void 0&&(e.mapsAsObjects=!0),e.getStructures&&(e.getShared=e.getStructures),e.getShared&&!e.structures&&((e.structures=[]).uninitialized=!0),e.keyMap)){this.mapKey=new Map;for(let[r,s]of Object.entries(e.keyMap))this.mapKey.set(s,r)}Object.assign(this,e)}decodeKey(e){return this.keyMap&&this.mapKey.get(e)||e}encodeKey(e){return this.keyMap&&this.keyMap.hasOwnProperty(e)?this.keyMap[e]:e}encodeKeys(e){if(!this._keyMap)return e;let r=new Map;for(let[s,n]of Object.entries(e))r.set(this._keyMap.hasOwnProperty(s)?this._keyMap[s]:s,n);return r}decodeKeys(e){if(!this._keyMap||e.constructor.name!="Map")return e;if(!this._mapKey){this._mapKey=new Map;for(let[s,n]of Object.entries(this._keyMap))this._mapKey.set(n,s)}let r={};return e.forEach((s,n)=>r[J(this._mapKey.has(n)?this._mapKey.get(n):n)]=s),r}mapDecode(e,r){let s=this.decode(e);return this._keyMap&&s.constructor.name==="Array"?s.map(n=>this.decodeKeys(n)):s}decode(e,r){if(p)return Pt(()=>(We(),this?this.decode(e,r):pe.prototype.decode.call(At,e,r)));V=r>-1?r:e.length,l=0,be=0,se=0,ge=null,re=Ve,D=null,p=e;try{j=e.dataView||(e.dataView=new DataView(e.buffer,e.byteOffset,e.byteLength))}catch(s){throw p=null,e instanceof Uint8Array?s:new Error("Source must be a Uint8Array or Buffer but was a "+(e&&typeof e=="object"?e.constructor.name:typeof e))}if(this instanceof pe){if(k=this,K=this.sharedValues&&(this.pack?new Array(this.maxPrivatePackedValues||16).concat(this.sharedValues):this.sharedValues),this.structures)return _=this.structures,xe();(!_||_.length>0)&&(_=[])}else k=At,(!_||_.length>0)&&(_=[]),K=null;return xe()}decodeMultiple(e,r){let s,n=0;try{let i=e.length;ve=!0;let f=this?this.decode(e,i):He.decode(e,i);if(r){if(r(f)===!1)return;for(;l<i;)if(n=l,r(xe())===!1)return}else{for(s=[f];l<i;)n=l,s.push(xe());return s}}catch(i){throw i.lastPosition=n,i.values=s,i}finally{ve=!1,We()}}}function xe(){try{let t=I();if(D){if(l>=D.postBundlePosition){let e=new Error("Unexpected bundle position");throw e.incomplete=!0,e}l=D.postBundlePosition,D=null}if(l==V)_=null,p=null,q&&(q=null);else if(l>V){let e=new Error("Unexpected end of CBOR data");throw e.incomplete=!0,e}else if(!ve)throw new Error("Data read, but end of buffer not reached");return t}catch(t){throw We(),(t instanceof RangeError||t.message.startsWith("Unexpected end of buffer"))&&(t.incomplete=!0),t}}function ne(){let t=new Error("Unexpected end of CBOR data");return t.incomplete=!0,t}function I(){if(!(l<V))throw ne();let t=p[l++],e=t>>5;if(t=t&31,t>23)switch(t){case 24:if(l>=V)throw ne();t=p[l++];break;case 25:if(e==7)return xr();t=j.getUint16(l),l+=2;break;case 26:if(e==7){let r=j.getFloat32(l);if(k.useFloat32>2){let s=Ge[(p[l]&127)<<1|p[l+1]>>7];return l+=4,(s*r+(r>0?.5:-.5)>>0)/s}return l+=4,r}if(t=j.getUint32(l),l+=4,e===1)return-1-t;break;case 27:if(e==7){let r=j.getFloat64(l);return l+=8,r}if(e>1){if(j.getUint32(l)>0)throw new Error("JavaScript does not support arrays, maps, or strings with length over 4294967295");t=j.getUint32(l+4)}else k.int64AsNumber?(t=j.getUint32(l)*4294967296,t+=j.getUint32(l+4)):t=j.getBigUint64(l);l+=8;break;case 31:switch(e){case 2:case 3:throw new Error("Indefinite length not supported for byte or text strings");case 4:let r=[],s,n=0;for(;(s=I())!=ue;){if(n>=me)throw new Error(`Array length exceeds ${me}`);r[n++]=s}return e==4?r:e==3?r.join(""):Buffer.concat(r);case 5:let i;if(k.mapsAsObjects){let f={},m=0;if(k.keyMap)for(;(i=I())!=ue;){if(m++>=Y)throw new Error(`Property count exceeds ${Y}`);f[J(k.decodeKey(i))]=I()}else for(;(i=I())!=ue;){if(m++>=Y)throw new Error(`Property count exceeds ${Y}`);f[J(i)]=I()}return f}else{Se&&(k.mapsAsObjects=!0,Se=!1);let f=new Map;if(k.keyMap){let m=0;for(;(i=I())!=ue;){if(m++>=Y)throw new Error(`Map size exceeds ${Y}`);f.set(k.decodeKey(i),I())}}else{let m=0;for(;(i=I())!=ue;){if(m++>=Y)throw new Error(`Map size exceeds ${Y}`);f.set(i,I())}}return f}case 7:return ue;default:throw new Error("Invalid major type for indefinite length "+e)}default:throw new Error("Unknown token "+t)}switch(e){case 0:return t;case 1:return~t;case 2:return Ir(t);case 3:if(se>=l)return ge.slice(l-Ee,(l+=t)-Ee);if(se==0&&V<140&&t<32){let n=t<16?xt(t):kr(t);if(n!=null)return n}return It(t);case 4:if(t>=me)throw new Error(`Array length exceeds ${me}`);if(t>V-l)throw ne();let r=new Array(t);for(let n=0;n<t;n++)r[n]=I();return r;case 5:if(t>=Y)throw new Error(`Map size exceeds ${me}`);if(t>(V-l)/2)throw ne();if(k.mapsAsObjects){let n={};if(k.keyMap)for(let i=0;i<t;i++)n[J(k.decodeKey(I()))]=I();else for(let i=0;i<t;i++)n[J(I())]=I();return n}else{Se&&(k.mapsAsObjects=!0,Se=!1);let n=new Map;if(k.keyMap)for(let i=0;i<t;i++)n.set(k.decodeKey(I()),I());else for(let i=0;i<t;i++)n.set(I(),I());return n}case 6:if(t>=St){let n=_[t&8191];if(n)return n.read||(n.read=$e(n)),n.read();if(t<65536){if(t==Ar){let i=he(),f=I(),m=I();qe(f,m);let y={};if(k.keyMap)for(let v=2;v<i;v++){let g=k.decodeKey(m[v-2]);y[J(g)]=I()}else for(let v=2;v<i;v++){let g=m[v-2];y[J(g)]=I()}return y}else if(t==vr){let i=he(),f=I();for(let m=2;m<i;m++)qe(f++,I());return I()}else if(t==St)return Ur();if(k.getShared&&(Je(),n=_[t&8191],n))return n.read||(n.read=$e(n)),n.read()}}let s=N[t];if(s)return s.handlesRead?s(I):s(I());{let n=I();for(let i=0;i<Ke.length;i++){let f=Ke[i](t,n);if(f!==void 0)return f}return new ie(n,t)}case 7:switch(t){case 20:return!1;case 21:return!0;case 22:return null;case 23:return;default:let n=(K||oe())[t];if(n!==void 0)return n;throw new Error("Unknown token "+t)}default:throw isNaN(t)?ne():new Error("Unknown CBOR token "+t)}}const kt=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;function $e(t){if(!t)throw new Error("Structure is required in record definition");function e(){let r=p[l++];if(r=r&31,r>23)switch(r){case 24:r=p[l++];break;case 25:r=j.getUint16(l),l+=2;break;case 26:r=j.getUint32(l),l+=4;break;default:throw new Error("Expected array header, but got "+p[l-1])}let s=this.compiledReader;for(;s;){if(s.propertyCount===r)return s(I);s=s.next}if(this.slowReads++>=Ot){let i=this.length==r?this:this.slice(0,r);return s=k.keyMap?new Function("r","return {"+i.map(f=>k.decodeKey(f)).map(f=>kt.test(f)?J(f)+":r()":"["+JSON.stringify(f)+"]:r()").join(",")+"}"):new Function("r","return {"+i.map(f=>kt.test(f)?J(f)+":r()":"["+JSON.stringify(f)+"]:r()").join(",")+"}"),this.compiledReader&&(s.next=this.compiledReader),s.propertyCount=r,this.compiledReader=s,s(I)}let n={};if(k.keyMap)for(let i=0;i<r;i++)n[J(k.decodeKey(this[i]))]=I();else for(let i=0;i<r;i++)n[J(this[i])]=I();return n}return t.slowReads=0,e}function J(t){if(typeof t=="string")return t==="__proto__"?"__proto_":t;if(typeof t=="number"||typeof t=="boolean"||typeof t=="bigint")return t.toString();if(t==null)return t+"";throw new Error("Invalid property name type "+typeof t)}let It=Me;function Or(t){It=e();function e(r){return function(n){let i=re[be++];if(i==null){if(D)return Me(n);let m=t(l,V,n,p);if(typeof m=="string")i=m,re=Ve;else if(re=m,be=1,se=1,i=re[0],i===void 0)throw new Error("Unexpected end of buffer")}let f=i.length;return f<=n?(l+=n,i):(ge=i,Ee=l,se=l+f,l+=n,i.slice(0,n))}}}function Me(t){let e;if(t<16&&(e=xt(t)))return e;if(t>64&&je)return je.decode(p.subarray(l,l+=t));const r=l+t,s=[];for(e="";l<r;){const n=p[l++];if((n&128)===0)s.push(n);else if((n&224)===192)if(n<194||l>=r||(p[l]&192)!==128)s.push(65533);else{const i=p[l++]&63;s.push((n&31)<<6|i)}else if((n&240)===224){const i=l<r?p[l]:0;if(l>=r||(i&192)!==128||n===224&&i<160||n===237&&i>=160)s.push(65533);else if(l++,l>=r||(p[l]&192)!==128)s.push(65533);else{const f=p[l++]&63;s.push((n&31)<<12|(i&63)<<6|f)}}else if((n&248)===240){const i=l<r?p[l]:0;if(n>244||l>=r||(i&192)!==128||n===240&&i<144||n===244&&i>=144)s.push(65533);else if(l++,l>=r||(p[l]&192)!==128)s.push(65533);else{const f=p[l++]&63;if(l>=r||(p[l]&192)!==128)s.push(65533);else{const m=p[l++]&63;let y=(n&7)<<18|(i&63)<<12|f<<6|m;y-=65536,s.push(y>>>10&1023|55296),s.push(56320|y&1023)}}}else s.push(65533);s.length>=4096&&(e+=F.apply(String,s),s.length=0)}return s.length>0&&(e+=F.apply(String,s)),e}let F=String.fromCharCode;function kr(t){let e=l,r=new Array(t);for(let s=0;s<t;s++){const n=p[l++];if((n&128)>0){l=e;return}r[s]=n}return F.apply(String,r)}function xt(t){if(t<4)if(t<2){if(t===0)return"";{let e=p[l++];if((e&128)>1){l-=1;return}return F(e)}}else{let e=p[l++],r=p[l++];if((e&128)>0||(r&128)>0){l-=2;return}if(t<3)return F(e,r);let s=p[l++];if((s&128)>0){l-=3;return}return F(e,r,s)}else{let e=p[l++],r=p[l++],s=p[l++],n=p[l++];if((e&128)>0||(r&128)>0||(s&128)>0||(n&128)>0){l-=4;return}if(t<6){if(t===4)return F(e,r,s,n);{let i=p[l++];if((i&128)>0){l-=5;return}return F(e,r,s,n,i)}}else if(t<8){let i=p[l++],f=p[l++];if((i&128)>0||(f&128)>0){l-=6;return}if(t<7)return F(e,r,s,n,i,f);let m=p[l++];if((m&128)>0){l-=7;return}return F(e,r,s,n,i,f,m)}else{let i=p[l++],f=p[l++],m=p[l++],y=p[l++];if((i&128)>0||(f&128)>0||(m&128)>0||(y&128)>0){l-=8;return}if(t<10){if(t===8)return F(e,r,s,n,i,f,m,y);{let v=p[l++];if((v&128)>0){l-=9;return}return F(e,r,s,n,i,f,m,y,v)}}else if(t<12){let v=p[l++],g=p[l++];if((v&128)>0||(g&128)>0){l-=10;return}if(t<11)return F(e,r,s,n,i,f,m,y,v,g);let M=p[l++];if((M&128)>0){l-=11;return}return F(e,r,s,n,i,f,m,y,v,g,M)}else{let v=p[l++],g=p[l++],M=p[l++],R=p[l++];if((v&128)>0||(g&128)>0||(M&128)>0||(R&128)>0){l-=12;return}if(t<14){if(t===12)return F(e,r,s,n,i,f,m,y,v,g,M,R);{let L=p[l++];if((L&128)>0){l-=13;return}return F(e,r,s,n,i,f,m,y,v,g,M,R,L)}}else{let L=p[l++],U=p[l++];if((L&128)>0||(U&128)>0){l-=14;return}if(t<15)return F(e,r,s,n,i,f,m,y,v,g,M,R,L,U);let C=p[l++];if((C&128)>0){l-=15;return}return F(e,r,s,n,i,f,m,y,v,g,M,R,L,U,C)}}}}}function Ir(t){return k.copyBuffers?Uint8Array.prototype.slice.call(p,l,l+=t):p.subarray(l,l+=t)}let Mt=new Float32Array(1),Re=new Uint8Array(Mt.buffer,0,4);function xr(){let t=p[l++],e=p[l++],r=(t&127)>>2;if(r===31)return e||t&3?NaN:t&128?-1/0:1/0;if(r===0){let s=((t&3)<<8|e)/16777216;return t&128?-s:s}return Re[3]=t&128|(r>>1)+56,Re[2]=(t&7)<<5|e>>3,Re[1]=e<<5,Re[0]=0,Mt[0]}new Array(4096);class ie{constructor(e,r){this.value=e,this.tag=r}}N[0]=t=>new Date(t),N[1]=t=>new Date(Math.round(t*1e3)),N[2]=t=>{let e=BigInt(0);for(let r=0,s=t.byteLength;r<s;r++)e=BigInt(t[r])+(e<<BigInt(8));return e},N[3]=t=>BigInt(-1)-N[2](t),N[4]=t=>+(t[1]+"e"+t[0]),N[5]=t=>t[1]*Math.exp(t[0]*Math.log(2));const qe=(t,e)=>{t=t-57344;let r=_[t];r&&r.isShared&&((_.restoreStructures||(_.restoreStructures=[]))[t]=r),_[t]=e,e.read=$e(e)};N[Sr]=t=>{let e=t.length,r=t[1];qe(t[0],r);let s={};for(let n=2;n<e;n++){let i=r[n-2];s[J(i)]=t[n]}return s},N[14]=t=>D?D[0].slice(D.position0,D.position0+=t):new ie(t,14),N[15]=t=>D?D[1].slice(D.position1,D.position1+=t):new ie(t,15);let Mr={Error,RegExp};N[27]=t=>(Mr[t[0]]||Error)(t[1],t[2]);const Rt=t=>{if(p[l++]!=132){let r=new Error("Packed values structure must be followed by a 4 element array");throw p.length<l&&(r.incomplete=!0),r}let e=t();if(!e||!e.length){let r=new Error("Packed values structure must be followed by a 4 element array");throw r.incomplete=!0,r}return K=K?e.concat(K.slice(e.length)):e,K.prefixes=t(),K.suffixes=t(),t()};Rt.handlesRead=!0,N[51]=Rt,N[vt]=t=>{if(!K)if(k.getShared)Je();else return new ie(t,vt);if(typeof t=="number")return K[16+(t>=0?2*t:-2*t-1)];let e=new Error("No support for non-integer packed references yet");throw t===void 0&&(e.incomplete=!0),e},N[28]=t=>{q||(q=new Map,q.id=0);let e=q.id++,r=l,s=p[l],n;s>>5==4?n=[]:n={};let i={target:n};q.set(e,i);let f=t();return i.used?(Object.getPrototypeOf(n)!==Object.getPrototypeOf(f)&&(l=r,n=f,q.set(e,{target:n}),f=t()),Object.assign(n,f)):(i.target=f,f)},N[28].handlesRead=!0,N[29]=t=>{let e=q.get(t);return e.used=!0,e.target},N[258]=t=>new Set(t),(N[259]=t=>(k.mapsAsObjects&&(k.mapsAsObjects=!1,Se=!0),t())).handlesRead=!0;function de(t,e){return typeof t=="string"?t+e:t instanceof Array?t.concat(e):Object.assign({},t,e)}function oe(){if(!K)if(k.getShared)Je();else throw new Error("No packed values available");return K}const Rr=1399353956;Ke.push((t,e)=>{if(t>=225&&t<=255)return de(oe().prefixes[t-224],e);if(t>=28704&&t<=32767)return de(oe().prefixes[t-28672],e);if(t>=1879052288&&t<=2147483647)return de(oe().prefixes[t-1879048192],e);if(t>=216&&t<=223)return de(e,oe().suffixes[t-216]);if(t>=27647&&t<=28671)return de(e,oe().suffixes[t-27639]);if(t>=1811940352&&t<=1879048191)return de(e,oe().suffixes[t-1811939328]);if(t==Rr)return{packedValues:K,structures:_.slice(0),version:e};if(t==55799)return e});const Tr=new Uint8Array(new Uint16Array([1]).buffer)[0]==1,Tt=[Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?{name:"BigUint64Array"}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?{name:"BigInt64Array"}:BigInt64Array,Float32Array,Float64Array],Pr=[64,68,69,70,71,72,77,78,79,85,86];for(let t=0;t<Tt.length;t++)Cr(Tt[t],Pr[t]);function Cr(t,e){let r="get"+t.name.slice(0,-5),s;typeof t=="function"?s=t.BYTES_PER_ELEMENT:t=null;for(let n=0;n<2;n++){if(!n&&s==1)continue;let i=s==2?1:s==4?2:s==8?3:0;N[n?e:e-4]=s==1||n==Tr?f=>{if(!t)throw new Error("Could not find typed array for code "+e);return!k.copyBuffers&&(s===1||s===2&&!(f.byteOffset&1)||s===4&&!(f.byteOffset&3)||s===8&&!(f.byteOffset&7))?new t(f.buffer,f.byteOffset,f.byteLength>>i):new t(Uint8Array.prototype.slice.call(f,0).buffer)}:f=>{if(!t)throw new Error("Could not find typed array for code "+e);let m=new DataView(f.buffer,f.byteOffset,f.byteLength),y=f.length>>i,v=new t(y),g=m[r];for(let M=0;M<y;M++)v[M]=g.call(m,M<<i,n);return v}}}function Ur(){let t=he(),e=l+I();for(let s=2;s<t;s++){let n=he();l+=n}let r=l;return l=e,D=[Me(he()),Me(he())],D.position0=0,D.position1=0,D.postBundlePosition=l,l=r,I()}function he(){if(!(l<V))throw ne();let t=p[l++]&31;if(t>23)switch(t){case 24:if(l>=V)throw ne();t=p[l++];break;case 25:t=j.getUint16(l),l+=2;break;case 26:t=j.getUint32(l),l+=4;break}return t}function Je(){if(k.getShared){let t=Pt(()=>(p=null,k.getShared()))||{},e=t.structures||[];k.sharedVersion=t.version,K=k.sharedValues=t.packedValues,_===!0?k.structures=_=e:_.splice.apply(_,[0,e.length].concat(e))}}function Pt(t){let e=V,r=l,s=be,n=Ee,i=se,f=ge,m=re,y=q,v=D,g=new Uint8Array(p.slice(0,V)),M=_,R=k,L=ve,U=t();return V=e,l=r,be=s,Ee=n,se=i,ge=f,re=m,q=y,D=v,p=g,ve=L,_=M,k=R,j=new DataView(p.buffer,p.byteOffset,p.byteLength),U}function We(){p=null,q=null,_=null}const Ge=new Array(147);for(let t=0;t<256;t++)Ge[t]=+("1e"+Math.floor(45.15-t*.30103));let He=new pe({useRecords:!1});const Lr=He.decode;He.decodeMultiple;let Te;try{Te=new TextEncoder}catch{}let Ze,Ct;const Pe=typeof globalThis=="object"&&globalThis.Buffer,Ae=typeof Pe<"u",Ye=Ae?Pe.allocUnsafeSlow:Uint8Array,Ut=Ae?Pe:Uint8Array,Lt=256,_t=Ae?4294967296:2144337920;let Xe,c,P,o=0,Q,B=null;const _r=61440,Dr=/[\u0080-\uFFFF]/,$=Symbol("record-id");class Dt extends pe{constructor(e){super(e),this.offset=0;let r,s,n,i,f;e=e||{};let m=Ut.prototype.utf8Write?function(a,w){return c.utf8Write(a,w,c.byteLength-w)}:Te&&Te.encodeInto?function(a,w){return Te.encodeInto(a,c.subarray(w)).written}:!1,y=this,v=e.structures||e.saveStructures,g=e.maxSharedStructures;if(g==null&&(g=v?128:0),g>8190)throw new Error("Maximum maxSharedStructure is 8190");let M=e.sequential;M&&(g=0),this.structures||(this.structures=[]),this.saveStructures&&(this.saveShared=this.saveStructures);let R,L,U=e.sharedValues,C;if(U){C=Object.create(null);for(let a=0,w=U.length;a<w;a++)C[U[a]]=a}let W=[],ye=0,G=0;this.mapEncode=function(a,w){return this._keyMap&&!this._mapped&&a.constructor.name==="Array"&&(a=a.map(d=>this.encodeKeys(d))),this.encode(a,w)},this.encode=function(a,w){if(c||(c=new Ye(8192),P=new DataView(c.buffer,0,8192),o=0),Q=c.length-10,Q-o<2048?(c=new Ye(c.length),P=new DataView(c.buffer,0,c.length),Q=c.length-10,o=0):w===zt&&(o=o+7&2147483640),r=o,y.useSelfDescribedHeader&&(P.setUint32(o,3654940416),o+=3),f=y.structuredClone?new Map:null,y.bundleStrings&&typeof a!="string"?(B=[],B.size=1/0):B=null,s=y.structures,s){if(s.uninitialized){let h=y.getShared()||{};y.structures=s=h.structures||[],y.sharedVersion=h.version;let u=y.sharedValues=h.packedValues;if(u){C={};for(let b=0,E=u.length;b<E;b++)C[u[b]]=b}}let d=s.length;if(d>g&&!M&&(d=g),!s.transitions){s.transitions=Object.create(null);for(let h=0;h<d;h++){let u=s[h];if(!u)continue;let b,E=s.transitions;for(let S=0,A=u.length;S<A;S++){E[$]===void 0&&(E[$]=h);let x=u[S];b=E[x],b||(b=E[x]=Object.create(null)),E=b}E[$]=h|1048576}}M||(s.nextId=d)}if(n&&(n=!1),i=s||[],L=C,e.pack){let d=new Map;if(d.values=[],d.encoder=y,d.maxValues=e.maxPrivatePackedValues||(C?16:1/0),d.objectMap=C||!1,d.samplingPackedValues=R,Ce(a,d),d.values.length>0){c[o++]=216,c[o++]=51,H(4);let h=d.values;O(h),H(0),H(0),L=Object.create(C||null);for(let u=0,b=h.length;u<b;u++)L[h[u]]=u}}Xe=w&rt;try{if(Xe)return;if(O(a),B&&Ft(r,O),y.offset=o,f&&f.idsToInsert){o+=f.idsToInsert.length*2,o>Q&&le(o),y.offset=o;let d=Fr(c.subarray(r,o),f.idsToInsert);return f=null,d}return w&zt?(c.start=r,c.end=o,c):c.subarray(r,o)}finally{if(s){if(G<10&&G++,s.length>g&&(s.length=g),ye>1e4)s.transitions=null,G=0,ye=0,W.length>0&&(W=[]);else if(W.length>0&&!M){for(let d=0,h=W.length;d<h;d++)W[d][$]=void 0;W=[]}}if(n&&y.saveShared){y.structures.length>g&&(y.structures=y.structures.slice(0,g));let d=c.subarray(r,o);return y.updateSharedData()===!1?y.encode(a):d}w&jr&&(o=r)}},this.findCommonStringsToPack=()=>(R=new Map,C||(C=Object.create(null)),a=>{let w=a&&a.threshold||4,d=this.pack?a.maxPrivatePackedValues||16:0;U||(U=this.sharedValues=[]);for(let[h,u]of R)u.count>w&&(C[h]=d++,U.push(h),n=!0);for(;this.saveShared&&this.updateSharedData()===!1;);R=null});const O=a=>{o>Q&&(c=le(o));var w=typeof a,d;if(w==="string"){if(L){let E=L[a];if(E>=0){E<16?c[o++]=E+224:(c[o++]=198,E&1?O(15-E>>1):O(E-16>>1));return}else if(R&&!e.pack){let S=R.get(a);S?S.count++:R.set(a,{count:1})}}let h=a.length;if(B&&h>=4&&h<1024){if((B.size+=h)>_r){let S,A=(B[0]?B[0].length*3+B[1].length:0)+10;o+A>Q&&(c=le(o+A)),c[o++]=217,c[o++]=223,c[o++]=249,c[o++]=B.position?132:130,c[o++]=26,S=o-r,o+=4,B.position&&Ft(r,O),B=["",""],B.size=0,B.position=S}let E=Dr.test(a);B[E?0:1]+=a,c[o++]=E?206:207,O(h);return}let u;h<32?u=1:h<256?u=2:h<65536?u=3:u=5;let b=h*3;if(o+b>Q&&(c=le(o+b)),h<64||!m){let E,S,A,x=o+u;for(E=0;E<h;E++)S=a.charCodeAt(E),S<128?c[x++]=S:S<2048?(c[x++]=S>>6|192,c[x++]=S&63|128):(S&64512)===55296&&((A=a.charCodeAt(E+1))&64512)===56320?(S=65536+((S&1023)<<10)+(A&1023),E++,c[x++]=S>>18|240,c[x++]=S>>12&63|128,c[x++]=S>>6&63|128,c[x++]=S&63|128):(c[x++]=S>>12|224,c[x++]=S>>6&63|128,c[x++]=S&63|128);d=x-o-u}else d=m(a,o+u,b);d<24?c[o++]=96|d:d<256?(u<2&&c.copyWithin(o+2,o+1,o+1+d),c[o++]=120,c[o++]=d):d<65536?(u<3&&c.copyWithin(o+3,o+2,o+2+d),c[o++]=121,c[o++]=d>>8,c[o++]=d&255):(u<5&&c.copyWithin(o+5,o+3,o+3+d),c[o++]=122,P.setUint32(o,d),o+=4),o+=d}else if(w==="number")if(!this.alwaysUseFloat&&a>>>0===a)a<24?c[o++]=a:a<256?(c[o++]=24,c[o++]=a):a<65536?(c[o++]=25,c[o++]=a>>8,c[o++]=a&255):(c[o++]=26,P.setUint32(o,a),o+=4);else if(!this.alwaysUseFloat&&a>>0===a)a>=-24?c[o++]=31-a:a>=-256?(c[o++]=56,c[o++]=~a):a>=-65536?(c[o++]=57,P.setUint16(o,~a),o+=2):(c[o++]=58,P.setUint32(o,~a),o+=4);else if(!this.alwaysUseFloat&&a<0&&a>=-4294967296&&Math.floor(a)===a)c[o++]=58,P.setUint32(o,-1-a),o+=4;else{let h;if((h=this.useFloat32)>0&&a<4294967296&&a>=-2147483648){c[o++]=250,P.setFloat32(o,a);let u;if(h<4||(u=a*Ge[(c[o]&127)<<1|c[o+1]>>7])>>0===u){o+=4;return}else o--}c[o++]=251,P.setFloat64(o,a),o+=8}else if(w==="object")if(!a)c[o++]=246;else{if(f){let u=f.get(a);if(u){if(c[o++]=216,c[o++]=29,c[o++]=25,!u.references){let b=f.idsToInsert||(f.idsToInsert=[]);u.references=[],b.push(u)}u.references.push(o-r),o+=2;return}else f.set(a,{offset:o-r})}let h=a.constructor;if(h===Object)this.skipFunction===!0&&(a=Object.fromEntries([...Object.keys(a).filter(u=>typeof a[u]!="function").map(u=>[u,a[u]])])),z(a);else if(h===Array){d=a.length,d<24?c[o++]=128|d:H(d);for(let u=0;u<d;u++)O(a[u])}else if(h===Map)if((this.mapsAsObjects?this.useTag259ForMaps!==!1:this.useTag259ForMaps)&&(c[o++]=217,c[o++]=1,c[o++]=3),d=a.size,d<24?c[o++]=160|d:d<256?(c[o++]=184,c[o++]=d):d<65536?(c[o++]=185,c[o++]=d>>8,c[o++]=d&255):(c[o++]=186,P.setUint32(o,d),o+=4),y.keyMap)for(let[u,b]of a)O(y.encodeKey(u)),O(b);else for(let[u,b]of a)O(u),O(b);else{for(let u=0,b=Ze.length;u<b;u++){let E=Ct[u];if(a instanceof E){let S=Ze[u],A=S.tag;A==null&&(A=S.getTag&&S.getTag.call(this,a)),A<24?c[o++]=192|A:A<256?(c[o++]=216,c[o++]=A):A<65536?(c[o++]=217,c[o++]=A>>8,c[o++]=A&255):A>-1&&(c[o++]=218,P.setUint32(o,A),o+=4),S.encode.call(this,a,O,le);return}}if(a[Symbol.iterator]){if(Xe){let u=new Error("Iterable should be serialized as iterator");throw u.iteratorNotHandled=!0,u}c[o++]=159;for(let u of a)O(u);c[o++]=255;return}if(a[Symbol.asyncIterator]||Qe(a)){let u=new Error("Iterable/blob should be serialized as iterator");throw u.iteratorNotHandled=!0,u}if(this.useToJSON&&a.toJSON){const u=a.toJSON();if(u!==a)return O(u)}z(a)}}else if(w==="boolean")c[o++]=a?245:244;else if(w==="bigint"){if(a<BigInt(1)<<BigInt(64)&&a>=0)c[o++]=27,P.setBigUint64(o,a);else if(a>-(BigInt(1)<<BigInt(64))&&a<0)c[o++]=59,P.setBigUint64(o,-a-BigInt(1));else if(this.largeBigIntToFloat)c[o++]=251,P.setFloat64(o,Number(a));else{a>=BigInt(0)?c[o++]=194:(c[o++]=195,a=BigInt(-1)-a);let h=[];for(;a;)h.push(Number(a&BigInt(255))),a>>=BigInt(8);et(new Uint8Array(h.reverse()),le);return}o+=8}else if(w==="undefined")c[o++]=247;else throw new Error("Unknown type: "+w)},z=this.useRecords===!1?this.variableMapSize?a=>{let w=Object.keys(a),d=Object.values(a),h=w.length;if(h<24?c[o++]=160|h:h<256?(c[o++]=184,c[o++]=h):h<65536?(c[o++]=185,c[o++]=h>>8,c[o++]=h&255):(c[o++]=186,P.setUint32(o,h),o+=4),y.keyMap)for(let u=0;u<h;u++)O(y.encodeKey(w[u])),O(d[u]);else for(let u=0;u<h;u++)O(w[u]),O(d[u])}:a=>{c[o++]=185;let w=o-r;o+=2;let d=0;if(y.keyMap)for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(O(y.encodeKey(h)),O(a[h]),d++);else for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(O(h),O(a[h]),d++);c[w+++r]=d>>8,c[w+r]=d&255}:(a,w)=>{let d,h=i.transitions||(i.transitions=Object.create(null)),u=0,b=0,E,S;if(this.keyMap){S=Object.keys(a).map(x=>this.encodeKey(x)),b=S.length;for(let x=0;x<b;x++){let bt=S[x];d=h[bt],d||(d=h[bt]=Object.create(null),u++),h=d}}else for(let x in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(x))&&(d=h[x],d||(h[$]&1048576&&(E=h[$]&65535),d=h[x]=Object.create(null),u++),h=d,b++);let A=h[$];if(A!==void 0)A&=65535,c[o++]=217,c[o++]=A>>8|224,c[o++]=A&255;else if(S||(S=h.__keys__||(h.__keys__=Object.keys(a))),E===void 0?(A=i.nextId++,A||(A=0,i.nextId=1),A>=Lt&&(i.nextId=(A=g)+1)):A=E,i[A]=S,A<g){c[o++]=217,c[o++]=A>>8|224,c[o++]=A&255,h=i.transitions;for(let x=0;x<b;x++)(h[$]===void 0||h[$]&1048576)&&(h[$]=A),h=h[S[x]];h[$]=A|1048576,n=!0}else{if(h[$]=A,P.setUint32(o,3655335680),o+=3,u&&(ye+=G*u),W.length>=Lt-g&&(W.shift()[$]=void 0),W.push(h),H(b+2),O(57344+A),O(S),w)return;for(let x in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(x))&&O(a[x]);return}if(b<24?c[o++]=128|b:H(b),!w)for(let x in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(x))&&O(a[x])},le=a=>{let w;if(a>16777216){if(a-r>_t)throw new Error("Encoded buffer would be larger than maximum buffer size");w=Math.min(_t,Math.round(Math.max((a-r)*(a>67108864?1.25:2),4194304)/4096)*4096)}else w=(Math.max(a-r<<2,c.length-1)>>12)+1<<12;let d=new Ye(w);return P=new DataView(d.buffer,0,w),c.copy?c.copy(d,0,r,a):d.set(c.slice(r,a)),o-=r,r=0,Q=d.length-10,c=d};let ee=100,yt=1e3;this.encodeAsIterable=function(a,w){return wt(a,w,fe)},this.encodeAsAsyncIterable=function(a,w){return wt(a,w,mt)};function*fe(a,w,d){let h=a.constructor;if(h===Object){let u=y.useRecords!==!1;u?z(a,!0):Nt(Object.keys(a).length,160);for(let b in a){let E=a[b];u||O(b),E&&typeof E=="object"?w[b]?yield*fe(E,w[b]):yield*Fe(E,w,b):O(E)}}else if(h===Array){let u=a.length;H(u);for(let b=0;b<u;b++){let E=a[b];E&&(typeof E=="object"||o-r>ee)?w.element?yield*fe(E,w.element):yield*Fe(E,w,"element"):O(E)}}else if(a[Symbol.iterator]&&!a.buffer){c[o++]=159;for(let u of a)u&&(typeof u=="object"||o-r>ee)?w.element?yield*fe(u,w.element):yield*Fe(u,w,"element"):O(u);c[o++]=255}else Qe(a)?(Nt(a.size,64),yield c.subarray(r,o),yield a,we()):a[Symbol.asyncIterator]?(c[o++]=159,yield c.subarray(r,o),yield a,we(),c[o++]=255):O(a);d&&o>r?yield c.subarray(r,o):o-r>ee&&(yield c.subarray(r,o),we())}function*Fe(a,w,d){let h=o-r;try{O(a),o-r>ee&&(yield c.subarray(r,o),we())}catch(u){if(u.iteratorNotHandled)w[d]={},o=r+h,yield*fe.call(this,a,w[d]);else throw u}}function we(){ee=yt,y.encode(null,rt)}function wt(a,w,d){return w&&w.chunkThreshold?ee=yt=w.chunkThreshold:ee=100,a&&typeof a=="object"?(y.encode(null,rt),d(a,y.iterateProperties||(y.iterateProperties={}),!0)):[y.encode(a)]}async function*mt(a,w){for(let d of fe(a,w,!0)){let h=d.constructor;if(h===Ut||h===Uint8Array)yield d;else if(Qe(d)){let u=d.stream().getReader(),b;for(;!(b=await u.read()).done;)yield b.value}else if(d[Symbol.asyncIterator])for await(let u of d)we(),u?yield*mt(u,w.async||(w.async={})):yield y.encode(u);else yield d}}}useBuffer(e){c=e,P=new DataView(c.buffer,c.byteOffset,c.byteLength),o=0}clearSharedData(){this.structures&&(this.structures=[]),this.sharedValues&&(this.sharedValues=void 0)}updateSharedData(){let e=this.sharedVersion||0;this.sharedVersion=e+1;let r=this.structures.slice(0),s=new Bt(r,this.sharedValues,this.sharedVersion),n=this.saveShared(s,i=>(i&&i.version||0)==e);return n===!1?(s=this.getShared()||{},this.structures=s.structures||[],this.sharedValues=s.packedValues,this.sharedVersion=s.version,this.structures.nextId=this.structures.length):r.forEach((i,f)=>this.structures[f]=i),n}}function Nt(t,e){t<24?c[o++]=e|t:t<256?(c[o++]=e|24,c[o++]=t):t<65536?(c[o++]=e|25,c[o++]=t>>8,c[o++]=t&255):(c[o++]=e|26,P.setUint32(o,t),o+=4)}class Bt{constructor(e,r,s){this.structures=e,this.packedValues=r,this.version=s}}function H(t){t<24?c[o++]=128|t:t<256?(c[o++]=152,c[o++]=t):t<65536?(c[o++]=153,c[o++]=t>>8,c[o++]=t&255):(c[o++]=154,P.setUint32(o,t),o+=4)}const Nr=typeof Blob>"u"?function(){}:Blob;function Qe(t){if(t instanceof Nr)return!0;let e=t[Symbol.toStringTag];return e==="Blob"||e==="File"}function Ce(t,e){switch(typeof t){case"string":if(t.length>3){if(e.objectMap[t]>-1||e.values.length>=e.maxValues)return;let s=e.get(t);if(s)++s.count==2&&e.values.push(t);else if(e.set(t,{count:1}),e.samplingPackedValues){let n=e.samplingPackedValues.get(t);n?n.count++:e.samplingPackedValues.set(t,{count:1})}}break;case"object":if(t)if(t instanceof Array)for(let s=0,n=t.length;s<n;s++)Ce(t[s],e);else{let s=!e.encoder.useRecords;for(var r in t)t.hasOwnProperty(r)&&(s&&Ce(r,e),Ce(t[r],e))}break;case"function":console.log(t)}}const Br=new Uint8Array(new Uint16Array([1]).buffer)[0]==1;Ct=[Date,Set,Error,RegExp,ie,ArrayBuffer,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?function(){}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?function(){}:BigInt64Array,Float32Array,Float64Array,Bt],Ze=[{tag:1,encode(t,e){let r=t.getTime()/1e3;(this.useTimestamp32||t.getMilliseconds()===0)&&r>=0&&r<4294967296?(c[o++]=26,P.setUint32(o,r),o+=4):(c[o++]=251,P.setFloat64(o,r),o+=8)}},{tag:258,encode(t,e){let r=Array.from(t);e(r)}},{tag:27,encode(t,e){e([t.name,t.message])}},{tag:27,encode(t,e){e(["RegExp",t.source,t.flags])}},{getTag(t){return t.tag},encode(t,e){e(t.value)}},{encode(t,e,r){et(t,r)}},{getTag(t){if(t.constructor===Uint8Array&&(this.tagUint8Array||Ae&&this.tagUint8Array!==!1))return 64},encode(t,e,r){et(t,r)}},Z(68,1),Z(69,2),Z(70,4),Z(71,8),Z(72,1),Z(77,2),Z(78,4),Z(79,8),Z(85,4),Z(86,8),{encode(t,e){let r=t.packedValues||[],s=t.structures||[];if(r.values.length>0){c[o++]=216,c[o++]=51,H(4);let n=r.values;e(n),H(0),H(0),packedObjectMap=Object.create(sharedPackedObjectMap||null);for(let i=0,f=n.length;i<f;i++)packedObjectMap[n[i]]=i}if(s){P.setUint32(o,3655335424),o+=3;let n=s.slice(0);n.unshift(57344),n.push(new ie(t.version,1399353956)),e(n)}else e(new ie(t.version,1399353956))}}];function Z(t,e){return!Br&&e>1&&(t-=4),{tag:t,encode:function(s,n){let i=s.byteLength,f=s.byteOffset||0,m=s.buffer||s;n(Ae?Pe.from(m,f,i):new Uint8Array(m,f,i))}}}function et(t,e){let r=t.byteLength;r<24?c[o++]=64+r:r<256?(c[o++]=88,c[o++]=r):r<65536?(c[o++]=89,c[o++]=r>>8,c[o++]=r&255):(c[o++]=90,P.setUint32(o,r),o+=4),o+r>=c.length&&e(o+r),c.set(t.buffer?t:new Uint8Array(t),o),o+=r}function Fr(t,e){let r,s=e.length*2,n=t.length-s;e.sort((i,f)=>i.offset>f.offset?1:-1);for(let i=0;i<e.length;i++){let f=e[i];f.id=i;for(let m of f.references)t[m++]=i>>8,t[m]=i&255}for(;r=e.pop();){let i=r.offset;t.copyWithin(i+s,i,n),s-=2;let f=i+s;t[f++]=216,t[f++]=28,n=i}return t}function Ft(t,e){P.setUint32(B.position+t,o-B.position-t+1);let r=B;B=null,e(r[0]),e(r[1])}let tt=new Dt({useRecords:!1});const zr=tt.encode;tt.encodeAsIterable,tt.encodeAsAsyncIterable;const zt=512,jr=1024,rt=2048;class Vr extends cr{constructor(e){e||(e={}),e.objectMode=!0,super(e),e.structures=[],this.decoder=e.decoder||new pe(e)}_transform(e,r,s){this.incompleteBuffer&&(e=Buffer.concat([this.incompleteBuffer,e]),this.incompleteBuffer=null);let n;try{n=this.decoder.decodeMultiple(e)}catch(i){if(i.incomplete)this.incompleteBuffer=e.slice(i.lastPosition),n=i.values;else return s(i)}finally{for(let i of n||[])i===null&&(i=this.getNullValue()),this.push(i)}s()}getNullValue(){return Symbol.for(null)}}const Kr=process.env.CBOR_NATIVE_ACCELERATION_DISABLED!==void 0&&process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase()==="true";if(!Kr){let t;try{typeof require=="function"?t=require("cbor-extract"):t=lr(import.meta.url)("cbor-extract"),t&&Or(t.extractStrings)}catch{}}class $r extends Event{code;reason;wasClean;constructor(e,r={}){super(e,r),this.code=r.code??0,this.reason=r.reason??"",this.wasClean=r.wasClean??!1}}function qr(t,e){const r="CloseEvent"in globalThis?globalThis.CloseEvent:$r;return new r(t,e)}class Jr{#e=new EventTarget;addEventListener(e,r,s){return this.#e.addEventListener(e,r,s),()=>{this.#e.removeEventListener(e,r,s)}}removeEventListener(e,r,s){this.#e.removeEventListener(e,r,s)}dispatchEvent(e){return this.#e.dispatchEvent(e)}}const T=Symbol.for("@appetizeio/rpc:rpcMethods");class Wr extends Jr{send;constructor(e){super(),this.send=e}dispatchClose(e){return this.dispatchEvent(qr("close",e))}dispatchMessage(e){return this.dispatchEvent(new CustomEvent("message",{detail:e}))}}function st(t){return!!t&&typeof t=="object"&&"jsonrpc"in t}function Gr(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}class Oe{closed=!1;[T];#e;constructor(e,{signal:r}={}){this[T]=new Wr(e),this.#e=r,this.#e?.addEventListener("abort",this.#r)}close(e={}){this.closed||(this.closed=!0,this.handleClose(e))}handleClose(e){this.#e?.removeEventListener("abort",this.#r),this[T].dispatchClose(e)}#r=()=>{this.close({code:1e3,reason:String(this.#e?.reason)})}}const jt={useRecords:!1},Hr=new Dt(jt);class Vt extends Oe{#e=[];#r;constructor(e,{signal:r}={}){super(async f=>{this.#r.write(Hr.encode(f))},{signal:r}),this.#r=e;const s=new Vr(jt);s.on("data",f=>{st(f)&&this[T].dispatchMessage(f)}),s.on("error",()=>{this.close()});const n=f=>{s.write(f)},i=()=>{this.close()};e.on("data",n),e.once("close",i),e.once("error",i),this.#e.push(()=>{e.off("data",n),e.off("close",i),e.off("error",i),s.destroy()})}handleClose(e){for(const r of this.#e.splice(0))r();super.handleClose(e)}}function Zr(t,e){const{promise:r,reject:s,resolve:n}=Gr(),i=fr(t),f=e?.signal,m=()=>{i.off("connect",g),i.off("error",M),f?.removeEventListener("abort",v)},y=R=>{m(),i.destroy(),s(R)},v=()=>{y(new Error("The connection was aborted"))},g=()=>{m();const R=new Vt(i,e);R[T].addEventListener("close",()=>{i.destroy()},{once:!0}),n(R)},M=R=>{y(R)};return f?.aborted?(y(new Error("The connection was aborted")),r):(i.once("connect",g),i.once("error",M),f?.addEventListener("abort",v,{once:!0}),r)}const Kt=ze("appetize.io:error:rpc");function nt(t,e){const r=new ur(new dr({errorListener:Kt}),Yr(t),{errorListener:Kt}),s=t[T].addEventListener("message",({detail:i})=>{r.receiveAndSend(i)}),n=t[T].addEventListener("close",()=>{s(),r.rejectAllPendingRequests("Transport closed")},{once:!0});for(const i in e){const f=e[i];f&&r.addMethod(i,f.bind(e))}return{rpc:r,unsub:()=>{s(),n()}}}function $t(t,e){const{rpc:r,unsub:s}=nt(t,e);return{client:r.client,unsub:s}}function Ue(t,e){const r=t[T].addEventListener("message",({detail:n})=>{if("method"in n)try{e({method:n.method,param:n.params})}catch{}}),s=t[T].addEventListener("close",r,{once:!0});return()=>{r(),s()}}function Yr(t){return new hr(t[T].send.bind(t[T]),()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)+1)}function it(t,e,{leftEffects:r=()=>({}),notifiesLeft:s,notifiesRight:n,rightEffects:i=()=>({})}={}){const f=nt(t,{}),m=nt(e,{}),y=[()=>{f.unsub(),m.unsub()}],v=()=>{for(const U of y)try{U()}catch{}},g=m.rpc.client,M=f.rpc.client;y.push(t[T].addEventListener("close",()=>{v()},{once:!0}),e[T].addEventListener("close",()=>{v()},{once:!0}));const R=r(g,M);for(const U in R){const C=R[U];C&&f.rpc.addMethod(U,C)}const L=i(M,g);for(const U in L){const C=L[U];C&&m.rpc.addMethod(U,C)}return qt(f.rpc,m.rpc,{notifies:s}),qt(m.rpc,f.rpc,{notifies:n}),v}function qt(t,e,{notifies:r}={}){t.applyServerMiddleware(async(s,n)=>{const i=r?.();if(i){const{id:m,...y}=n;i[T].send(y)}if(t.hasMethod(n.method))return await s(n);if(!n.id)return e.notify(n.method,n.params),null;const f=await e.request(n.method,n.params);return{id:n.id,jsonrpc:pr,result:f}})}function X(t,...e){t.notify(e[0],e[1],e[2])}const Xr=(...t)=>$t(...t),ot=["ArrowDown","ArrowLeft","ArrowRight","ArrowUp","Backspace","Enter","Tab"];function at(){const t=new Oe(async r=>{e[T].dispatchMessage(r)}),e=new Oe(async r=>{t[T].dispatchMessage(r)});return t[T].addEventListener("close",()=>{e.close()},{once:!0}),e[T].addEventListener("close",()=>{t.close()},{once:!0}),[t,e]}function Jt(t,e){const r=[],s=()=>{for(const n of r)n()};r.push(t[T].addEventListener("message",n=>e[T].send(n.detail))),r.push(t[T].addEventListener("close",s,{once:!0})),r.push(e[T].addEventListener("message",n=>t[T].send(n.detail))),r.push(e[T].addEventListener("close",s,{once:!0}))}const ae=ze("appetize.io:trace:transport:iframe");class Wt extends Oe{onmessage;#e;#r;constructor(e,{origin:r="*",signal:s}={}){super(async n=>{e.postMessage(n,this.#e)},{signal:s}),this.#r=e,this.#e=r,window.addEventListener("message",this.#t)}postMessage(e,r,s){ae("postMessage(legacy)",{message:e,targetOrigin:r,transfer:s}),this.#r.postMessage(e,r??this.#e,s)}handleClose(e){ae("close()"),window.removeEventListener("message",this.#t),super.handleClose(e)}#t=async e=>{if(ae("handleMessage()",e),e.source!==this.#r||(ae("handleMessage() -> source ok"),this.#e!=="*"&&e.origin!==this.#e))return;ae("handleMessage() -> origin ok");const r=e.data;if(!st(r)){ae("handleMessage() -> legacy"),this.onmessage?.(e);return}ae("handleMessage() -> jsonrpc"),this[T].dispatchMessage(r)}}function Le(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}const Qr=2*1024*1024,Gt=ze("appetize:client:acked-writable-stream");class es{unacked=0;#e;#r;#t;#s;#n=[];#i;constructor(e){this.#t=e,this.#i=e.window??Qr}abort(e){this.#a(e),this.#t.onAbort?.(e)}async close(){await this.#c(()=>this.unacked===0),this.#t.onClose?.()}fail(e){this.#a(e)&&this.#t.onError?.(e)}start(e){this.#e=e}write(e){const r=e.byteLength;return this.unacked+=r,this.#t.send(e).then(()=>{this.unacked-=r,this.#s&&(this.#s.acked+=r),this.#o()},s=>{this.fail(s)}),this.unacked>=this.#i&&!this.#s&&(this.#s={acked:0,at:performance.now()},Gt("window full: %d bytes unacked, writes stalled",this.unacked)),this.#c(()=>this.unacked<this.#i)}#o(){if(this.#s&&this.unacked<=this.#i/2){const e=performance.now()-this.#s.at;Gt("pressure relieved after %dms: %d bytes acked, %s MB/s",Math.round(e),this.#s.acked,(this.#s.acked/Math.max(e,1)/1e3).toFixed(2)),this.#s=void 0}this.#n=this.#n.filter(e=>e.until()?(e.resolve(void 0),!1):!0)}#a(e){if(this.#r)return!1;this.#r={reason:e},this.#e.error(e);const r=this.#n;this.#n=[];for(const s of r)s.reject(e);return!0}#c(e){if(e())return Promise.resolve();const r={...Le(),until:e};return this.#n.push(r),r.promise}}class ts extends WritableStream{get unacked(){return this.#e.unacked}#e;constructor(e){const r=new es(e);super({abort:s=>{r.abort(s)},close:()=>r.close(),start:s=>{r.start(s)},write:s=>r.write(s)}),this.#e=r}fail(e){this.#e.fail(e)}}const rs=1024*1024;class ss{#e;#r=!1;#t;#s;constructor(e){this.#t=e}cancel(e){this.#n(),this.#t?.(e)}close(){this.#n()&&this.#e.close()}error(e){this.#n()&&this.#e.error(e)}pull(){const e=this.#s;this.#s=void 0,e?.resolve(void 0)}async push(e){if(this.#r)return;this.#s??=Le();const{promise:r}=this.#s;this.#e.enqueue(e),await r}start(e){this.#e=e}#n(){return this.#r?!1:(this.#r=!0,this.pull(),!0)}}class ns extends ReadableStream{#e;constructor({highWaterMark:e=rs,onCancel:r}={}){const s=new ss(r);super({cancel:n=>{s.cancel(n)},pull:()=>{s.pull()},start:n=>{s.start(n)}},new ByteLengthQueuingStrategy({highWaterMark:e})),this.#e=s}close(){this.#e.close()}error(e){this.#e.error(e)}push(e){return this.#e.push(e)}}class is{closed;readable;writable;#e=[];#r;#t;#s;#n;#i;#o="starting";constructor(e,{onClose:r,receiveBuffer:s,sendWindow:n,signal:i}){if(this.#i=e,this.#r=Le(),this.closed=this.#r.promise,this.closed.catch(os),this.#s=new ns({highWaterMark:s,onCancel:()=>{this.#a(void 0)}}),this.readable=this.#s,this.#n=new ts({onAbort:()=>{this.#a(void 0)},onClose:()=>{this.#a(void 0)},onError:f=>{this.#a(f)},send:f=>e.request("appetizeio.device.server.debug.adbTunnel.data",{data:f}),window:n}),this.writable=this.#n,i){const f=()=>{this.#a(i.reason)};i.addEventListener("abort",f,{once:!0}),this.#e.push(()=>{i.removeEventListener("abort",f)})}this.#e.push(()=>{r(this)})}close=()=>{this.#a(void 0)};detach(){this.#a(void 0,{notifyServer:!1})}async handleData(e){this.#o==="open"&&await this.#s.push(e)}handleStopped({error:e}){this.#o==="open"&&this.#a(e?new Error(e):void 0)}async start(){try{await this.#i.request("appetizeio.device.server.debug.adbTunnel.start",void 0)}catch(e){throw this.#a(e),e}if(this.#o==="closed")throw this.#t?this.#t:new Error("The adb tunnel closed before it started");this.#o="open"}async[Symbol.asyncDispose](){this.close();try{await this.closed}catch{}}#a(e,{notifyServer:r=!0}={}){if(this.#o!=="closed"){this.#o="closed",this.#t=e,r&&X(this.#i,"appetizeio.device.server.debug.adbTunnel.stop",void 0),e?(this.#s.error(e),this.#n.fail(e)):(this.#s.close(),this.#n.fail(new Error("The adb tunnel has stopped")));for(const s of this.#e)s();e?this.#r.reject(e):this.#r.resolve(void 0)}}}function os(){}class as{readable;ready;#e=[];#r=!1;#t;#s;#n;constructor({label:e,onClose:r,signal:s}){this.#s=e,this.ready=new Promise((i,f)=>{this.#n={reject:f,resolve:i}});let n;if(this.readable=new ReadableStream({cancel:()=>{this.#o()},start:i=>{n=i}}),!n)throw new Error("ReadableStream controller was not initialized");if(this.#t=n,s){const i=()=>{this.fail(s.reason)};s.addEventListener("abort",i,{once:!0}),this.#e.push(()=>{s.removeEventListener("abort",i)})}this.#e.push(()=>{r(this)})}close=()=>{this.#i()};fail(e){this.#i(e)}handleItem(e){this.#t.enqueue(e)}markReady(){if(!this.#n)return;const{resolve:e}=this.#n;this.#n=void 0,e(this)}async*[Symbol.asyncIterator](){const e=this.readable.getReader();try{for(let r=await e.read();!r.done;r=await e.read())yield r.value}finally{await e.cancel()}}#i(e){this.#r||(this.#t.close(),this.#o(e))}#o(e){this.#r=!0;for(const r of this.#e)r();this.#n?.reject(e??new Error(`${this.#s} closed before it started`)),this.#n=void 0}}class cs{get subscribers(){return this.#t}#e;#r;#t=new Set;constructor(e){this.#e=e}close(){this.#r=void 0;for(const e of[...this.#t])e.close();this.#t.clear()}add(e){this.#t.add(e)}ensureStarted(){return this.#r||(this.#r=(async()=>{try{return await this.#e.start()}catch(e){throw this.#r=void 0,e}})()),this.#r}release(e){this.#t.delete(e),!(this.#t.size>0||!this.#r)&&(this.#r=void 0,this.#e.stop())}}class Ht extends cs{#e;constructor(e){super({start:e.start,stop:e.stop}),this.#e=e.label}async createStream({signal:e}={}){e?.throwIfAborted();const r=new as({label:this.#e,onClose:s=>{this.release(s)},signal:e});this.add(r);try{await this.ensureStarted(),r.markReady()}catch(s){r.fail(s)}return await r.ready}handleNotification(e){for(const r of this.subscribers)r.handleItem(e)}}class ls{#e;#r;#t;#s;constructor(e){this.#s=e,this.#r=new Ht({label:"Log stream",start:()=>e.request("appetizeio.device.server.debug.log.start"),stop:()=>{X(e,"appetizeio.device.server.debug.log.stop",void 0)}}),this.#t=new Ht({label:"Proxy intercept stream",start:()=>e.request("appetizeio.device.server.debug.proxyIntercept.start"),stop:()=>{X(e,"appetizeio.device.server.debug.proxyIntercept.stop",void 0)}})}close(){this.#e?.detach(),this.#r.close(),this.#t.close()}async createAdbTunnel({signal:e}={}){e?.throwIfAborted(),this.#e?.close();const r=new is(this.#s,{onClose:()=>{this.#e=void 0},signal:e});return this.#e=r,await r.start(),r}async createLogStream(e){return await this.#r.createStream(e)}async createProxyInterceptStream(e){return await this.#t.createStream(e)}async handleAdbTunnelData({data:e}){await this.#e?.handleData(e)}handleAdbTunnelStopped(e){this.#e?.handleStopped(e)}handleLogEntry(e){this.#r.handleNotification(e)}handleProxyInterceptEvent(e){this.#t.handleNotification(e)}}function ct(t){return{...fs(t),index:t.index,within:t.within?ct(t.within):void 0}}function fs(t){switch(t.by){case"testId":return{by:"testId",testId:t.testId};case"text":return{by:"text",text:us(t.text)}}}function us(t){return typeof t=="string"?{kind:"exact",value:t}:{flags:t.flags,kind:"regex",source:t.source}}function ds(t){return{press:e=>{X(t,"appetizeio.device.server.keyboard.press",{key:e})}}}function hs({onRequest:t,parent:e,proxyInlet:r,signal:s}){const n=new Wt(e,{signal:s}),i=[()=>{n.close()}];return Jt(n,r),t&&i.push(Ue(n,f=>{t(f,{source:"parent"})})),{cleanup:()=>{for(const f of i)f()},notifies:()=>e===globalThis.window?void 0:n}}const ps="@appetizeio/rpc:playwrightAdapter",_e=Symbol.for(ps);function ys(t){window[_e]&&console.warn("playwright exposed more than once, overwriting previous adapter");const e={send(r){t[T].send(r)},subscribe(r){return t[T].addEventListener("message",s=>{r(s.detail)})}};return window[_e]=e,()=>{window[_e]===e&&delete window[_e]}}function ws({onRequest:t,proxyInlet:e}){const[r,s]=at(),i=[ys(s),()=>{r.close()}];return Jt(r,e),t&&i.push(Ue(r,f=>{t(f,{source:"parent"})})),{cleanup:()=>{for(const f of i)f()},notifies:()=>r}}function De(t){return t.by==="position"?{by:"position",x:t.x,y:t.y}:{...ct(t),x:t.x,y:t.y}}function ms(t){return"to"in t?{from:De(t.from),kind:"path",to:De(t.to)}:{direction:t.direction,distance:t.distance,from:t.from?De(t.from):void 0,kind:"direction"}}function bs(t){return{down:(e,r)=>{X(t,"appetizeio.device.server.touch.down",{x:e,y:r})},move:(e,r)=>{X(t,"appetizeio.device.server.touch.move",{x:e,y:r})},up:(e,r)=>{X(t,"appetizeio.device.server.touch.up",{x:e,y:r})}}}const gs=new Set(ot),Es=/^(?<key>[A-Za-z]+)>(?<repeat>\d+)\/$/u,Ss=/(\{\{|\{[^{}]*\})/u;function vs(t){const e=[];let r="";function s(){r.length>0&&(e.push({kind:"text",text:r}),r="")}for(const[n,i]of t.split(Ss).entries())if(n%2===0){if(i.includes("{"))throw new Error(`Unclosed "{" in ${JSON.stringify(t)}`);r+=i}else i==="{{"?r+="{":(s(),e.push(Os(i.slice(1,-1))));return s(),e}function As(t,e){if(!gs.has(t))throw e.endsWith(">")?new Error(`Holding a key is not supported: {${e}}`):e.startsWith("/")?new Error(`Releasing a key is not supported: {${e}}`):new Error(`{${e}} does not name a supported key. Supported: ${ot.join(", ")}. Type characters as text, and escape a literal "{" as "{{".`)}function Os(t){const e=Es.exec(t)?.groups,r=e?.key??t;if(As(r,t),!e)return{key:r,kind:"press"};const s=Number(e.repeat);if(s<1)throw new Error(`Repeat for {${t}} must be at least 1`);return{key:r,kind:"press",repeat:s}}const ks=Symbol("@appetizeio/client:v1Compat");function Zt(t){return t?.[ks]}class Is{readable;ready;get dimensions(){return this.#s}#e=[];#r=!1;#t;#s;#n;#i;constructor({onClose:e,requestKeyFrame:r,signal:s}){this.#n=r,this.ready=new Promise((i,f)=>{this.#i={reject:f,resolve:i}});let n;if(this.readable=new ReadableStream({cancel:()=>{this.#a()},start:i=>{n=i}}),!n)throw new Error("ReadableStream controller was not initialized");if(this.#t=n,s){const i=()=>{this.fail(s.reason)};s.addEventListener("abort",i,{once:!0}),this.#e.push(()=>{s.removeEventListener("abort",i)})}this.#e.push(()=>{e(this)})}close=()=>{this.#o()};fail(e){this.#o(e)}handleChunk(e){if(this.#i){if(e.type!=="key")return;const{resolve:r}=this.#i;this.#i=void 0,this.#s={height:e.height,width:e.width},r(this)}else e.type==="key"&&(this.#s={height:e.height,width:e.width});this.#t.enqueue(e)}requestKeyFrame=()=>{this.#r||this.#n()};async*[Symbol.asyncIterator](){const e=this.readable.getReader();try{for(let r=await e.read();!r.done;r=await e.read())yield r.value}finally{await e.cancel()}}#o(e){this.#r||(this.#t.close(),this.#a(e))}#a(e){this.#r=!0;for(const r of this.#e)r();this.#i?.reject(e??new Error("Video stream closed before the first key chunk")),this.#i=void 0}}class xs{#e=!1;#r;#t;#s=new Set;constructor(e){this.#r=e}close(){this.#t=void 0;for(const e of[...this.#s])e.close()}async createStream({signal:e}={}){e?.throwIfAborted();const r=new Is({onClose:s=>{this.#i(s)},requestKeyFrame:()=>{this.#o()},signal:e});this.#s.add(r);try{await this.#n()}catch(s){r.fail(s)}return await r.ready}handleChunk(e){e.type==="key"&&(this.#e=!1);for(const r of this.#s)r.handleChunk(e)}#n(){return this.#t?(this.#o(),this.#t):(this.#e=!0,this.#t=(async()=>{try{await this.#r.request("appetizeio.device.server.video.start")}catch(e){throw this.#t=void 0,e}})(),this.#t)}#i(e){this.#s.delete(e),!(this.#s.size>0||!this.#t)&&(this.#t=void 0,X(this.#r,"appetizeio.device.server.video.stop",void 0))}#o(){this.#e||(this.#e=!0,X(this.#r,"appetizeio.device.server.video.requestKeyFrame",void 0))}}class Ms{debug;keyboard;touch;video;#e;#r=[];#t;constructor(e,r={}){const s=Zt(r)??{},{baseUrl:n,onClose:i,signal:f}=r;this.#e=n?Rs(n):void 0;const{onRequest:m,parent:y,proxyRequest:v}=s,g=Ts(m);f?.addEventListener("abort",this.close);const[M,R]=at(),[L,U]=at();this.#r.push(()=>{M.close(),R.close(),L.close(),U.close()}),this.#r.push(it(U,e,{leftEffects:v}));const C=y==="playwright"?ws({onRequest:g,proxyInlet:L}):y?hs({onRequest:g,parent:y,proxyInlet:L,signal:f}):void 0;C&&this.#r.push(C.cleanup),this.#r.push(it(R,L,{notifiesLeft:C?.notifies}));const{client:W,unsub:ye}=Xr(M,{"appetizeio.device.client.debug.adbTunnel.data":z=>G.handleAdbTunnelData(z),"appetizeio.device.client.debug.adbTunnel.stopped":async z=>{G.handleAdbTunnelStopped(z)},"appetizeio.device.client.debug.log.entry":async z=>{G.handleLogEntry(z)},"appetizeio.device.client.debug.proxyIntercept.event":async z=>{G.handleProxyInterceptEvent(z)},"appetizeio.device.client.video.chunk":async z=>{O.handleChunk(z)}});this.#t=W,this.#r.push(ye);const G=new ls(this.#t);this.#r.push(()=>{G.close()});const O=new xs(this.#t);this.#r.push(()=>{O.close()}),this.debug=G,this.keyboard=ds(this.#t),this.touch=bs(this.#t),this.video=O,g&&(this.#r.push(Ue(R,z=>{g(z,{source:"self"})})),e instanceof Wt&&this.#r.push(Ue(e,z=>{g(z,{source:"iframe"})}))),this.#r.push(()=>i?.())}async clearKeychain(){await this.#t.request("appetizeio.device.server.clearKeychain")}close=()=>{for(const e of this.#r)try{e()}catch(r){console.error("Device cleanup error:",r)}};async inspect(e,r){return await this.#t.request("appetizeio.device.server.inspect",{options:r,selector:e?ct(e):void 0})}async open(e){new URL(e),await this.#t.request("appetizeio.device.server.open",{url:e})}async press(e){await this.#t.request("appetizeio.device.server.press",{action:e})}async rotate(e){return await this.#t.request("appetizeio.device.server.rotate",{orientation:e})}async screenshot(){const e=await this.#t.request("appetizeio.device.server.screenshot"),r=this.#e?new URL(e.url,this.#e).href:e.url,s=await fetch(r);if(!s.ok)throw new Error(`Screenshot download failed: ${String(s.status)} ${s.statusText}`);const n=s.headers.get("content-type")??"application/octet-stream";return{data:new Uint8Array(await s.arrayBuffer()),mimeType:n}}async swipe(e,r){await this.#t.request("appetizeio.device.server.swipe",{gesture:ms(e),options:r})}async tap(e,r){await this.#t.request("appetizeio.device.server.tap",{options:r,selector:De(e)})}async type(e,r){await this.#t.request("appetizeio.device.server.type",{options:r,steps:vs(e)})}}function Rs(t){const e=new URL(t);return e.protocol==="ws:"?e.protocol="http:":e.protocol==="wss:"&&(e.protocol="https:"),e.href}const Ts=t=>{if(t)return(e,r)=>{e.method.startsWith("appetizeio.device.server.")&&t(e,r)}},Ps={dateStyle:"medium",timeStyle:"short"},Cs=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;function Ne(t){return Array.isArray(t)?t.map(Ne).join(", "):typeof t=="boolean"?t?"yes":"no":typeof t=="number"?t.toLocaleString():Us(t)}function Us(t){if(Cs.test(t)){const r=new Date(t);if(!Number.isNaN(r.getTime()))return r.toLocaleString(void 0,Ps)}const e=Et();return t===e?"~":t.startsWith(`${e}${Er}`)?`~${t.slice(e.length)}`:t}function Yt(){return process.env.APPETIZE_CLI_HOME??te(Et(),".appetize","cli","sessions")}function lt(t){return te(Yt(),t)}class Be extends Error{details;constructor(e,r={},s){super(e,s===void 0?void 0:{cause:s}),this.name="CommandError",this.details=r}hints(e){return[]}}class Ie extends Be{sessionDir;constructor(e,{cause:r,details:s={},sessionId:n}){const i=lt(n);super(e,Object.assign({sessionDir:i},s),r),this.name="SessionError",this.sessionDir=i}static wrap(e,r){if(e instanceof Ie||e instanceof ce)return e;const s=e instanceof Error?e.message:String(e),n=e instanceof Be?e.details:{};return new Ie(s,{cause:e,details:n,sessionId:r})}hints(){return[`Check the session logs for details: ${Ne(this.sessionDir)}`]}}class Ls extends Ie{constructor(e){super(`Session ${e} already exists`,{sessionId:e}),this.name="SessionAlreadyExistsError"}hints(){return[`Pass --force to replace it, deleting ${Ne(this.sessionDir)}`]}}class ce extends Be{#e;constructor(e,r=[]){super(e),this.name="UsageError",this.#e=r}hints({command:e}){return[...this.#e,`Run 'appetize ${e} --help' for usage`]}}function _s(t,e,r){const s=e instanceof Error?e.message:String(e);if(!(e instanceof Be))return t==="json"?JSON.stringify({error:{message:s}},null,2):`\u2716 ${s}`;const n=e.hints(r);if(t==="json"){const i=n.length>0?{hints:n}:{};return JSON.stringify({error:{message:s,...e.details,...i}},null,2)}return[`\u2716 ${s}`,...n.map(i=>` ${i}`)].join(`
2
+ `)}function ft(t){try{return process.kill(t,0),!0}catch{return!1}}const Ds=["SIGINT","SIGTERM"],Ns=150;function Bs(t){const e=new AbortController,r=setInterval(()=>{ft(t)||(clearInterval(r),e.abort(new Error("The device session exited")))},Ns);return r.unref(),e.signal}function Xt(){const t=new AbortController;for(const e of Ds)process.once(e,()=>{t.abort(new Error(`Received ${e}`))});return t.signal}function Fs(t,e){return new Promise((r,s)=>{if(e.aborted){s(Qt(e.reason));return}const n=()=>{clearTimeout(i),s(Qt(e.reason))},i=setTimeout(()=>{e.removeEventListener("abort",n),r()},t);e.addEventListener("abort",n,{once:!0})})}function Qt(t){return t instanceof Error?t:new Error(String(t))}function zs(t){const e=t.at(-1);return{options:Ks(e)?e:{},positionals:t.slice(0,-1).filter(r=>typeof r=="string"),signal:Xt()}}function js(t,e){const r=er(t,e);return r===void 0?void 0:tr(r,e)}function er(t,e){if(t===void 0)return;const r=typeof t=="boolean"?Number.NaN:Number(t);if(!Number.isFinite(r))throw new ce(`${e} expects a number, e.g., ${e}=-0.1`);return r}function ut(t,e){if(t!==void 0){if(typeof t!="string"||t==="")throw new ce(`${e} needs a value`);return t}}function tr(t,e){if(!(t>=0&&t<=1))throw new ce(`${e} expects a number between 0 and 1`);return t}function Vs(t,e){const r=ut(t,e);if(r===void 0)throw new ce(`${e} needs a value`);return r}function Ks(t){return typeof t=="object"&&t!==null}const $s="APPETIZEIO_FEATURE_FLAGS";function qs(){const t={};for(const e of(process.env[$s]??"").split(",")){const r=e.trim();r!==""&&(t[r]=!0)}return t}function Js(){const t=process.env.APPETIZE_API_TOKEN;if(!t)throw new Error("APPETIZE_API_TOKEN must be set");return t}function rr(t){return ut(t.endpoint,"--endpoint")??Ws()}function Ws(){const t=process.env.APPETIZE_ENDPOINT?.trim();return t===""?void 0:t}const Gs="https://appetize.io";async function sr(t,{authorize:e=!0,commandOptions:r={},query:s={},...n}={}){const i=new URL(t,rr(r)??Gs);for(const[y,v]of Object.entries(s))i.searchParams.set(y,v);const f=new Headers(n.headers);e&&f.set("x-api-key",Js());const m=await fetch(i,{...n,headers:f});if(!m.ok)throw new Error(`The webserver rejected the request (${String(m.status)})`);return await m.json()}const nr=["android","ios"];function ir(t={},e){return sr("/api/v2/service/devices",{authorize:!1,commandOptions:t,signal:e})}function Hs(t){return nr.includes(t)}async function Zs(t,e={}){return(await ir(e)).find(s=>s.id===t)?.platform}function Ys(t){let e={...t,updatedAt:new Date().toISOString()};return{path:ht(e.sessionId),save:r=>(e={...e,...r,updatedAt:new Date().toISOString()},on(e),e)}}function or(t){const e=t.state?.phase;return e==="ended"||e==="error"?!1:ft(t.pid)}function Xs(){const t=Yt();return gt(t,{recursive:!0}),gr(t).map(e=>dt(e)).filter(e=>e!==void 0)}function Qs(t){if(t!==void 0){const r=dt(t);if(!r)throw new Error(`No session found for id ${t}`);return r}const e=Xs().filter(or);if(e.length===0)throw new Error("No active sessions");if(e.length>1)throw new ce("There are multiple active sessions",["Specify --session-id to pick one"]);return e[0]}function dt(t){try{return JSON.parse(mr(ht(t),"utf8"))}catch{return}}function en(t){br(lt(t),{force:!0,recursive:!0})}function tn(t){return te(ke(t),"control.sock")}function ar(t){return te(ke(t),"daemon.log")}function rn(t){return te(ke(t),"device.log.jsonl")}function sn(t,e){return{daemon:ar(t),device:rn(t),...e==="intercept"?{network:nn(t)}:void 0}}function nn(t){return te(ke(t),"network.jsonl")}function ke(t){const e=lt(t);return gt(e,{recursive:!0}),e}function ht(t){return te(ke(t),"state.json")}function on(t){const e=ht(t.sessionId),r=`${e}.tmp`;yr(r,`${JSON.stringify(t,null,2)}
3
+ `),wr(r,e)}const pt="APPETIZE_DAEMON_CONFIG";function an(){const t=process.env[pt];if(!t)throw new Error(`${pt} is not set`);return JSON.parse(t)}function cn(t){return JSON.stringify(t)}export{zs as A,_s as B,ot as C,Ms as D,zr as E,T as F,Lr as G,st as H,Zt as I,Le as J,an as K,Ys as L,Zs as M,Vt as N,it as O,tn as P,Xt as Q,Ie as S,Oe as T,ce as U,er as a,Fs as b,ir as c,Zr as d,nr as e,sr as f,Vs as g,ar as h,Hs as i,pt as j,or as k,ft as l,Bs as m,qs as n,rr as o,ut as p,dt as q,tr as r,cn as s,Ls as t,en as u,sn as v,js as w,Ne as x,$t as y,Qs as z};
package/daemon.js CHANGED
@@ -1,2 +1,2 @@
1
- import{T as M,B as L,C as O,E as b,F as _,D as z,G as F,H as V,I as N,n as j,J as H,K as J,L as Q,M as k,N as G,O as K}from"./config-D0ukqfLN.js";import S from"debug";import{createServer as A}from"node:net";import{pipeline as $}from"node:stream";import{rm as B}from"node:fs/promises";import{readFileSync as Z,createWriteStream as X,existsSync as Y,rmSync as ee}from"node:fs";import{createServer as te}from"node:http";import{WebSocketServer as ne}from"ws";import{pipeline as re}from"node:stream/promises";import{EncodedVideoPacketSource as oe,Output as se,FilePathTarget as ie,Mp4OutputFormat as ae,EncodedPacket as ce}from"mediabunny";import"stream";import"module";import"json-rpc-2.0";import"node:os";import"node:path";const de=S("appetize.io:transport:ws-ping"),ue=3e4;function le(e){let t=!0;const n=()=>{t=!0};e.on("pong",n);const r=setInterval(()=>{if(!t){clearInterval(r),de("liveness ping went unanswered, terminating"),e.terminate();return}e.readyState===e.OPEN&&(t=!1,e.ping())},ue);return{destroy(){clearInterval(r),e.off("pong",n)},pong:n}}const v=S("appetize.io:trace:transport:websocket");class E extends M{constructor(t,{signal:n}={}){super(async o=>{this.#e.send(L(o))},{signal:n}),this.wsOrUrl=t,typeof t=="string"?(this.#e=new WebSocket(t),this.#e.binaryType="arraybuffer"):this.#e=t;const r=this.#e;if(r instanceof WebSocket?(r.addEventListener("open",this.#s),r.addEventListener("message",this.#o),r.addEventListener("error",this.#r),r.addEventListener("close",this.#n),this.#t.push(()=>{r.removeEventListener("open",this.#s),r.removeEventListener("message",this.#o),r.removeEventListener("error",this.#r),r.removeEventListener("close",this.#n)})):(r.addEventListener("open",this.#s),r.addEventListener("message",this.#o),r.addEventListener("error",this.#r),r.addEventListener("close",this.#n),this.#t.push(()=>{r.removeEventListener("open",this.#s),r.removeEventListener("message",this.#o),r.removeEventListener("error",this.#r),r.removeEventListener("close",this.#n)})),this.send=r.send.bind(r),"ping"in r){const{destroy:o,pong:s}=le(r);this.#c=s,this.#t.push(o)}}wsOrUrl;onclose;onerror;onmessage;onopen;send;get bufferedAmount(){return this.#e.bufferedAmount}get readyState(){return this.#e.readyState}#t=[];#i=!1;#a=!1;#c;#e;close({destroyWs:t=!1,...n}={}){this.closed||(this.#i=t,super.close(n))}handleClose(t){v("close()");for(const n of this.#t.splice(0))n();if((typeof this.wsOrUrl=="string"||this.#i)&&(this.#e.close(t.code,t.reason),"terminate"in this.#e)){const n=this.#e;setTimeout(()=>{n.terminate()},100)}this.#d(),super.handleClose(t)}#d(t){this.#a||(this.#a=!0,this.onclose?.(t))}#n=t=>{v("handleWebSocketClose()",t);try{this.#d(t)}finally{this.close()}};#r=t=>{v("handleWebSocketError()",t),this.onerror?.(t)};#o=async t=>{v("handleWebSocketMessage()",t),this.#c?.();const n=this.#u(t);if(!n){v("handleWebSocketMessage() -> legacy"),this.onmessage?.(t);return}v("handleWebSocketMessage() -> jsonrpc",n),this[O].dispatchMessage(n)};#s=t=>{v("handleWebSocketOpen()",t),this.onopen?.(t)};#u(t){try{const n=b(new Uint8Array(t.data));return _(n)?n:null}catch{return null}}}function y(e){return typeof e=="object"&&e!==null}function R(e){try{const t=b(new Uint8Array(e.data));if(y(t)&&typeof t.name=="string")return{name:t.name,payload:"payload"in t?t.payload:void 0}}catch{}}async function fe(e,t={}){const n=t.baseUrl??(typeof e=="string"?new URL(e).origin:void 0),r=typeof e=="string"?new E(e,{signal:t.signal}):e,o=new z(r,{...t,baseUrl:n});return typeof e=="string"&&await pe(r,o,t),o}function pe(e,t,n){const r=F(n)?.onLegacyMessage;return new Promise((o,s)=>{e.onopen=()=>{o()},e.onclose=()=>{t.close(),n.signal?.aborted?s(n.signal.reason):s(new Error("The websocket was closed before the device connection was established"))},e.onerror=s,e.onmessage=i=>{const c=R(i);c&&r?.(c)}})}async function he(e,{device:t,onStateChange:n,signal:r}){r?.throwIfAborted(),n?.({device:t,phase:"starting"}),await new Promise((o,s)=>{const i=()=>{c(),s(r?.reason)},c=()=>{e.onmessage=void 0,e.onclose=void 0,r?.removeEventListener("abort",i)};e.onmessage=a=>{const d=we(R(a),t);d&&(n?.(d),d.phase==="ready"&&(c(),o()))},e.onclose=()=>{c(),s(new Error("The device closed before it was ready"))},r?.addEventListener("abort",i,{once:!0})})}function we(e,t){if(!e)return;if(e.name==="ready")return{device:t,phase:"ready"};if(e.name!=="launchStatus")return;switch(y(e.payload)&&typeof e.payload.status=="string"?e.payload.status:void 0){case"downloadingApp":return{device:t,phase:"downloadingApp"};case"installingApp":return{device:t,phase:"installingApp"};case"launchingApp":return{device:t,phase:"launchingApp"};default:return}}function ve(){return typeof process>"u"?void 0:process}function ge(e){return{deviceType:e.id,osVersion:e.osVersion}}function ye(e){return e==="intercept"?"interceptOnDemand":e}class g extends Error{code;constructor(t,n){super(n),this.name="SessionRequestError",this.code=t}}function me(e,{onClose:t,onEvent:n,signal:r}={}){return new Promise((o,s)=>{const i=new WebSocket(e);i.binaryType="arraybuffer",i.onopen=()=>{P(i,{capabilities:{},t:"handshake"})},i.onmessage=c=>{be(i,c,{onEvent:n,onHandshake:a=>{o({close:()=>{i.close()},id:a})}})},i.onclose=()=>{s(new g("disconnected",`The connection to ${e} closed`)),t?.()},r?.addEventListener("abort",()=>{s(r.reason),i.close()},{once:!0})})}function be(e,t,n){const r=b(new Uint8Array(t.data));y(r)&&(r.t==="handshake"&&typeof r.id=="string"?n.onHandshake(r.id):r.name==="ping"?P(e,{name:"pong"}):typeof r.name=="string"&&n.onEvent?.(r.name,r.payload))}function P(e,t){e.send(L(t))}async function Se(e,t,{apiToken:n,onQueue:r,signal:o}={}){const s=V();ke(s.reject,o);const i=await me(Ae(e),{onClose:()=>{s.reject(Ee())},onEvent:(c,a)=>{Te(c,a,s,r)},signal:o});return Le(e,{apiToken:n,data:t,socketid:i.id},o).catch(s.reject),await s.promise.finally(()=>{i.close()})}function Ee(){return new g("disconnected","The webserver connection closed before a session was granted")}function C(e,t){return y(e)?e[t]:void 0}function Te(e,t,n,r){if(e==="queue"||e==="concurrentQueue"){const o=e==="queue"?Re(t):Pe(t);o&&r?.(o)}else e==="newSession"?$e(n,t):e==="userError"&&n.reject(new g("denied",Ce(t)))}function I(e,t){const n=C(e,t);return typeof n=="number"?n:void 0}function m(e,t){const n=C(e,t);return typeof n=="string"?n:void 0}async function Le(e,t,n){const r=await fetch(new URL("/api/session/request",e),{body:JSON.stringify(t),headers:{"content-type":"application/json"},method:"POST",signal:n});if(!r.ok)throw new g("denied",`The webserver rejected the session request (${String(r.status)})`)}function ke(e,t){t?.addEventListener("abort",()=>{e(t.reason)},{once:!0})}function Ae(e){const t=new URL("/client",e);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.href}function $e(e,t){const n=m(t,"path"),r=m(t,"sessionToken");n!==void 0&&r!==void 0?e.resolve({path:n,sessionToken:r}):e.reject(new g("denied","The granted session is missing its address"))}function Re(e){const t=I(e,"position");return t===void 0?void 0:{position:t,reason:"capacity"}}function Pe(e){const t=I(e,"position");if(t!==void 0)return{limit:m(e,"name")??"Queue",position:t,reason:"concurrency"}}function Ce(e){return m(e,"message")??"The session request was denied"}async function Ie({apiToken:e=Ue(),codec:t="h264",device:n,endpoint:r="https://appetize.io",onStateChange:o,open:s,proxy:i,signal:c,target:a}){if(c?.throwIfAborted(),!a)throw new TypeError("target is required");if(!n.id)throw new TypeError("device.id is required");o?.({phase:"requesting"});const d=await Se(r,{codec:t,launchUrl:s,noVideo:!0,proxy:ye(i),publicKey:a,...ge(n)},{apiToken:e,onQueue:p=>{o?.({phase:"queued",queue:p})},signal:c});return{baseUrl:new URL(d.path).origin,url:We(d)}}function Ue(){return ve()?.env.APPETIZE_API_TOKEN}function We(e){const t=new URL("/client",e.path);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.searchParams.set("sessionToken",e.sessionToken),t.href}const De=S("appetize:daemon");function f(e){De(e)}function qe(e,{message:t="The operation timed out"}={}){const n=new AbortController,r=setTimeout(()=>{n.abort(new Error(t))},e),o=()=>{clearTimeout(r)};return{cancel:o,signal:n.signal,[Symbol.dispose]:o}}var xe=(e,t)=>(t=Symbol[e])?t:Symbol.for("Symbol."+e),U=e=>{throw TypeError(e)},Me=(e,t,n)=>{if(t!=null){typeof t!="object"&&typeof t!="function"&&U("Object expected");var r,o;r===void 0&&(r=t[xe("dispose")]),typeof r!="function"&&U("Object not disposable"),o&&(r=function(){try{o.call(this)}catch(s){return Promise.reject(s)}}),e.push([n,r,t])}return t},Oe=(e,t,n)=>{var r=typeof SuppressedError=="function"?SuppressedError:function(i,c,a,d){return d=Error(a),d.name="SuppressedError",d.error=i,d.suppressed=c,d},o=i=>t=n?new r(i,t,"An error was suppressed during disposal"):(n=!0,i),s=i=>{for(;i=e.pop();)try{var c=i[1]&&i[1].call(i[2]);if(i[0])return Promise.resolve(c).then(s,a=>(o(a),s()))}catch(a){o(a)}if(n)throw t};return s()};const _e=1e4;async function ze(e,t,{onClose:n,signal:r}={}){var o=[];try{const c=Me(o,qe(_e,{message:"Timed out waiting for session request state"})),a=AbortSignal.any([r,c.signal].filter(w=>!!w)),d=w=>{t.save({state:w}),f(w.phase),w.phase!=="requesting"&&c.cancel()},{baseUrl:p,url:u}=await Ie({device:e.device,endpoint:e.endpoint,onStateChange:d,open:e.open,proxy:e.proxy,signal:a,target:e.target});t.save({baseUrl:p});const l=new E(u,{signal:a});return await he(l,{device:e.device,onStateChange:d,signal:a}),l.onclose=()=>{n?.("The device connection closed")},{device:await fe(l,{baseUrl:p,signal:a}),transport:l}}catch(c){var s=c,i=!0}finally{Oe(o,s,i)}}function Fe(){const e=N(),t=j(e.sessionId,e.proxy),n=H({device:e.device,endpoint:e.endpoint,open:e.open,pid:process.pid,proxy:e.proxy,sessionId:e.sessionId,startedAt:new Date().toISOString(),target:e.target});return n.save({logs:t}),{config:e,logs:t,store:n}}async function Ve(e,{host:t="127.0.0.1",onError:n,port:r=0,signal:o}={}){o?.throwIfAborted();let s;const i=()=>{const u=s;s=void 0,u&&(u.tunnel?.close(),u.socket.destroy())},c=async u=>{const{socket:l}=u;try{const h=await e.debug.createAdbTunnel();if(l.destroyed){h.close();return}u.tunnel=h,h.closed.catch(w=>{n?.(w)}),Ne(l,h)}catch(h){l.destroyed||n?.(h),l.destroy()}},a=A(u=>{i();const l={socket:u};s=l,u.once("close",()=>{s===l&&(s=void 0)}),c(l)}),d=()=>{o?.removeEventListener("abort",d),i(),a.close()};o?.addEventListener("abort",d,{once:!0});const p=await je(a,t,r);return{close:d,url:new URL(`adb://${p.address}:${String(p.port)}`)}}function Ne(e,t){$(t.readable,e,W),$(e,async n=>{const r=t.writable.getWriter();try{for await(const o of n)await r.write(o);await r.close()}catch(o){throw await r.abort(o),o}},W)}function W(){}function je(e,t,n){return new Promise((r,o)=>{e.once("error",o),e.listen({host:t,port:n},()=>{e.off("error",o);const s=e.address();if(s===null||typeof s=="string"){o(new Error("The listener has no tcp address"));return}r(s)})})}async function He(e,t,n,{signal:r}={}){try{if(!await Je(t))return;const o=await Ve(e,{onError:s=>{f(`adb endpoint error: ${D(s)}`)},signal:r});n.save({adbSerial:o.url.host}),f(`adb endpoint listening at ${o.url.host}`)}catch(o){f(`adb endpoint unavailable: ${D(o)}`)}}async function Je(e){return await J(e.device.id,{endpoint:e.endpoint})==="android"}function D(e){return e instanceof Error?e.message:String(e)}async function Qe(e,t,{methods:n={},signal:r}={}){await B(t,{force:!0});const o=new Set,s=A(i=>{f("control client connected"),o.add(i),i.once("close",()=>{o.delete(i)});const c=new Q(i,{signal:r});k(c,e,{leftEffects:()=>n})});r?.addEventListener("abort",()=>{for(const i of o)i.destroy()},{once:!0}),await new Promise((i,c)=>{s.once("error",c),s.listen({path:t,signal:r},()=>{s.off("error",c),i()})})}async function Ge(e,t,n,{recorder:r,signal:o}){const s=G(n);return await Qe(e,s,{methods:{"appetizeio.cli.record.start":async i=>await r.start(i),"appetizeio.cli.record.stop":async()=>await r.stop()},signal:o}),t.save({controlSocket:s}),s}const q="127.0.0.1";async function Ke(e,t,n={}){try{const r=await Be(e,n);return t.save({viewerUrl:r.url.href}),f(`web endpoint listening at ${r.url.href}`),r}catch(r){f(`web endpoint unavailable: ${tt(r)}`);return}}async function Be(e,{port:t=0,signal:n,viewerHtml:r=Z(new URL("./viewer.html",import.meta.url),"utf8")}){n?.throwIfAborted();const o=te((u,l)=>{Xe(u,l,r)}),s=new Set,i=new ne({noServer:!0,perMessageDeflate:!1});let c;i.on("connection",u=>{if(s.size>0){u.close(1013,"Viewer already connected");return}s.add(u);const l=new E(u);k(l,e),u.once("close",()=>{s.delete(u)})}),o.on("upgrade",(u,l,h)=>{Ye(i,u,l,h)});const a=()=>(c??=new Promise((u,l)=>{n?.removeEventListener("abort",d);for(const h of s)Ze(h);i.close(),o.close(h=>{h?l(h):u()})}),c),d=()=>{a()};await et(o,t),n?.addEventListener("abort",d,{once:!0});const p=o.address();if(!p||typeof p=="string")throw await a(),new Error("Web address unavailable");return{close:a,url:new URL(`http://${q}:${String(p.port)}/`)}}function Ze(e){e.close(1001)}function Xe(e,t,n){if(e.method==="GET"&&e.url==="/"){t.writeHead(200,{"Cache-Control":"no-store","Content-Type":"text/html; charset=utf-8"}),t.end(n);return}t.writeHead(404).end()}function Ye(e,t,n,r){if(t.url!=="/ws"){n.destroy();return}e.handleUpgrade(t,n,r,o=>{e.emit("connection",o,t)})}function et(e,t){return new Promise((n,r)=>{const o=s=>{r(s)};e.once("error",o),e.listen(t,q,()=>{e.removeListener("error",o),n()})})}function tt(e){return e instanceof Error?e.message:String(e)}function nt(e){const t=new AbortController,n=rt(e,t),r=K();return r.addEventListener("abort",()=>{const{reason:o}=r;n(o instanceof Error?o.message:String(o))},{once:!0}),{shutdown:n,signal:t.signal}}function rt(e,t){let n=!1;return r=>{n||(n=!0,e.save({endedAt:new Date().toISOString(),endedReason:r,state:{phase:"ended"}}),f(r),t.abort())}}async function x(e,t,{label:n,signal:r}){try{const o=await e();await re(o.readable.pipeThrough(ot()),X(t,{flags:"a"}))}catch(o){if(r?.aborted)return;const s=o instanceof Error?o.message:String(o);f(`${n} ended with an error: ${s}`)}}function ot(){return new TransformStream({transform:(e,t)=>{t.enqueue(`${JSON.stringify(e)}
1
+ import{T as M,E as L,F as O,G as b,H as _,D as z,I as F,J as V,K as N,v as j,L as H,M as Q,N as J,O as k,P as G,Q as K}from"./config-DfwUvfKT.js";import S from"debug";import{createServer as A}from"node:net";import{pipeline as $}from"node:stream";import{rm as Z}from"node:fs/promises";import{readFileSync as B,createWriteStream as X,existsSync as Y,rmSync as ee}from"node:fs";import{createServer as te}from"node:http";import{WebSocketServer as ne}from"ws";import{pipeline as re}from"node:stream/promises";import{EncodedVideoPacketSource as oe,Output as se,FilePathTarget as ie,Mp4OutputFormat as ae,EncodedPacket as ce}from"mediabunny";import"stream";import"module";import"json-rpc-2.0";import"node:path";import"node:os";const de=S("appetize.io:transport:ws-ping"),ue=3e4;function le(e){let t=!0;const n=()=>{t=!0};e.on("pong",n);const r=setInterval(()=>{if(!t){clearInterval(r),de("liveness ping went unanswered, terminating"),e.terminate();return}e.readyState===e.OPEN&&(t=!1,e.ping())},ue);return{destroy(){clearInterval(r),e.off("pong",n)},pong:n}}const v=S("appetize.io:trace:transport:websocket");class E extends M{constructor(t,{signal:n}={}){super(async o=>{this.#e.send(L(o))},{signal:n}),this.wsOrUrl=t,typeof t=="string"?(this.#e=new WebSocket(t),this.#e.binaryType="arraybuffer"):this.#e=t;const r=this.#e;if(r instanceof WebSocket?(r.addEventListener("open",this.#s),r.addEventListener("message",this.#o),r.addEventListener("error",this.#r),r.addEventListener("close",this.#n),this.#t.push(()=>{r.removeEventListener("open",this.#s),r.removeEventListener("message",this.#o),r.removeEventListener("error",this.#r),r.removeEventListener("close",this.#n)})):(r.addEventListener("open",this.#s),r.addEventListener("message",this.#o),r.addEventListener("error",this.#r),r.addEventListener("close",this.#n),this.#t.push(()=>{r.removeEventListener("open",this.#s),r.removeEventListener("message",this.#o),r.removeEventListener("error",this.#r),r.removeEventListener("close",this.#n)})),this.send=r.send.bind(r),"ping"in r){const{destroy:o,pong:s}=le(r);this.#c=s,this.#t.push(o)}}wsOrUrl;onclose;onerror;onmessage;onopen;send;get bufferedAmount(){return this.#e.bufferedAmount}get readyState(){return this.#e.readyState}#t=[];#i=!1;#a=!1;#c;#e;close({destroyWs:t=!1,...n}={}){this.closed||(this.#i=t,super.close(n))}handleClose(t){v("close()");for(const n of this.#t.splice(0))n();if((typeof this.wsOrUrl=="string"||this.#i)&&(this.#e.close(t.code,t.reason),"terminate"in this.#e)){const n=this.#e;setTimeout(()=>{n.terminate()},100)}this.#d(),super.handleClose(t)}#d(t){this.#a||(this.#a=!0,this.onclose?.(t))}#n=t=>{v("handleWebSocketClose()",t);try{this.#d(t)}finally{this.close()}};#r=t=>{v("handleWebSocketError()",t),this.onerror?.(t)};#o=async t=>{v("handleWebSocketMessage()",t),this.#c?.();const n=this.#u(t);if(!n){v("handleWebSocketMessage() -> legacy"),this.onmessage?.(t);return}v("handleWebSocketMessage() -> jsonrpc",n),this[O].dispatchMessage(n)};#s=t=>{v("handleWebSocketOpen()",t),this.onopen?.(t)};#u(t){try{const n=b(new Uint8Array(t.data));return _(n)?n:null}catch{return null}}}function y(e){return typeof e=="object"&&e!==null}function R(e){try{const t=b(new Uint8Array(e.data));if(y(t)&&typeof t.name=="string")return{name:t.name,payload:"payload"in t?t.payload:void 0}}catch{}}async function fe(e,t={}){const n=t.baseUrl??(typeof e=="string"?new URL(e).origin:void 0),r=typeof e=="string"?new E(e,{signal:t.signal}):e,o=new z(r,{...t,baseUrl:n});return typeof e=="string"&&await pe(r,o,t),o}function pe(e,t,n){const r=F(n)?.onLegacyMessage;return new Promise((o,s)=>{e.onopen=()=>{o()},e.onclose=()=>{t.close(),n.signal?.aborted?s(n.signal.reason):s(new Error("The websocket was closed before the device connection was established"))},e.onerror=s,e.onmessage=i=>{const c=R(i);c&&r?.(c)}})}async function he(e,{device:t,onStateChange:n,signal:r}){r?.throwIfAborted(),n?.({device:t,phase:"starting"}),await new Promise((o,s)=>{const i=()=>{c(),s(r?.reason)},c=()=>{e.onmessage=void 0,e.onclose=void 0,r?.removeEventListener("abort",i)};e.onmessage=a=>{const d=we(R(a),t);d&&(n?.(d),d.phase==="ready"&&(c(),o()))},e.onclose=()=>{c(),s(new Error("The device closed before it was ready"))},r?.addEventListener("abort",i,{once:!0})})}function we(e,t){if(!e)return;if(e.name==="ready")return{device:t,phase:"ready"};if(e.name!=="launchStatus")return;switch(y(e.payload)&&typeof e.payload.status=="string"?e.payload.status:void 0){case"downloadingApp":return{device:t,phase:"downloadingApp"};case"installingApp":return{device:t,phase:"installingApp"};case"launchingApp":return{device:t,phase:"launchingApp"};default:return}}function ve(){return typeof process>"u"?void 0:process}function ge(e){return{deviceType:e.id,osVersion:e.osVersion}}function ye(e){return e==="intercept"?"interceptOnDemand":e}class g extends Error{code;constructor(t,n){super(n),this.name="SessionRequestError",this.code=t}}function me(e,{onClose:t,onEvent:n,signal:r}={}){return new Promise((o,s)=>{const i=new WebSocket(e);i.binaryType="arraybuffer",i.onopen=()=>{P(i,{capabilities:{},t:"handshake"})},i.onmessage=c=>{be(i,c,{onEvent:n,onHandshake:a=>{o({close:()=>{i.close()},id:a})}})},i.onclose=()=>{s(new g("disconnected",`The connection to ${e} closed`)),t?.()},r?.addEventListener("abort",()=>{s(r.reason),i.close()},{once:!0})})}function be(e,t,n){const r=b(new Uint8Array(t.data));y(r)&&(r.t==="handshake"&&typeof r.id=="string"?n.onHandshake(r.id):r.name==="ping"?P(e,{name:"pong"}):typeof r.name=="string"&&n.onEvent?.(r.name,r.payload))}function P(e,t){e.send(L(t))}async function Se(e,t,{apiToken:n,onQueue:r,signal:o}={}){const s=V();ke(s.reject,o);const i=await me(Ae(e),{onClose:()=>{s.reject(Ee())},onEvent:(c,a)=>{Te(c,a,s,r)},signal:o});return Le(e,{apiToken:n,data:t,socketid:i.id},o).catch(s.reject),await s.promise.finally(()=>{i.close()})}function Ee(){return new g("disconnected","The webserver connection closed before a session was granted")}function I(e,t){return y(e)?e[t]:void 0}function Te(e,t,n,r){if(e==="queue"||e==="concurrentQueue"){const o=e==="queue"?Re(t):Pe(t);o&&r?.(o)}else e==="newSession"?$e(n,t):e==="userError"&&n.reject(new g("denied",Ie(t)))}function U(e,t){const n=I(e,t);return typeof n=="number"?n:void 0}function m(e,t){const n=I(e,t);return typeof n=="string"?n:void 0}async function Le(e,t,n){const r=await fetch(new URL("/api/session/request",e),{body:JSON.stringify(t),headers:{"content-type":"application/json"},method:"POST",signal:n});if(!r.ok)throw new g("denied",`The webserver rejected the session request (${String(r.status)})`)}function ke(e,t){t?.addEventListener("abort",()=>{e(t.reason)},{once:!0})}function Ae(e){const t=new URL("/client",e);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.href}function $e(e,t){const n=m(t,"path"),r=m(t,"sessionToken");n!==void 0&&r!==void 0?e.resolve({path:n,sessionToken:r}):e.reject(new g("denied","The granted session is missing its address"))}function Re(e){const t=U(e,"position");return t===void 0?void 0:{position:t,reason:"capacity"}}function Pe(e){const t=U(e,"position");if(t!==void 0)return{limit:m(e,"name")??"Queue",position:t,reason:"concurrency"}}function Ie(e){return m(e,"message")??"The session request was denied"}async function Ue({apiToken:e=Ce(),codec:t="h264",device:n,endpoint:r="https://appetize.io",onStateChange:o,open:s,proxy:i,signal:c,target:a}){if(c?.throwIfAborted(),!a)throw new TypeError("target is required");if(!n.id)throw new TypeError("device.id is required");o?.({phase:"requesting"});const d=await Se(r,{codec:t,launchUrl:s,noVideo:!0,proxy:ye(i),publicKey:a,...ge(n)},{apiToken:e,onQueue:p=>{o?.({phase:"queued",queue:p})},signal:c});return{baseUrl:new URL(d.path).origin,url:We(d)}}function Ce(){return ve()?.env.APPETIZE_API_TOKEN}function We(e){const t=new URL("/client",e.path);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.searchParams.set("sessionToken",e.sessionToken),t.href}const De=S("appetize:daemon");function f(e){De(e)}function qe(e,{message:t="The operation timed out"}={}){const n=new AbortController,r=setTimeout(()=>{n.abort(new Error(t))},e),o=()=>{clearTimeout(r)};return{cancel:o,signal:n.signal,[Symbol.dispose]:o}}var xe=(e,t)=>(t=Symbol[e])?t:Symbol.for("Symbol."+e),C=e=>{throw TypeError(e)},Me=(e,t,n)=>{if(t!=null){typeof t!="object"&&typeof t!="function"&&C("Object expected");var r,o;r===void 0&&(r=t[xe("dispose")]),typeof r!="function"&&C("Object not disposable"),o&&(r=function(){try{o.call(this)}catch(s){return Promise.reject(s)}}),e.push([n,r,t])}return t},Oe=(e,t,n)=>{var r=typeof SuppressedError=="function"?SuppressedError:function(i,c,a,d){return d=Error(a),d.name="SuppressedError",d.error=i,d.suppressed=c,d},o=i=>t=n?new r(i,t,"An error was suppressed during disposal"):(n=!0,i),s=i=>{for(;i=e.pop();)try{var c=i[1]&&i[1].call(i[2]);if(i[0])return Promise.resolve(c).then(s,a=>(o(a),s()))}catch(a){o(a)}if(n)throw t};return s()};const _e=1e4;async function ze(e,t,{onClose:n,signal:r}={}){var o=[];try{const c=Me(o,qe(_e,{message:"Timed out waiting for session request state"})),a=AbortSignal.any([r,c.signal].filter(w=>!!w)),d=w=>{t.save({state:w}),f(w.phase),w.phase!=="requesting"&&c.cancel()},{baseUrl:p,url:u}=await Ue({device:e.device,endpoint:e.endpoint,onStateChange:d,open:e.open,proxy:e.proxy,signal:a,target:e.target});t.save({baseUrl:p});const l=new E(u,{signal:a});return await he(l,{device:e.device,onStateChange:d,signal:a}),l.onclose=()=>{n?.("The device connection closed")},{device:await fe(l,{baseUrl:p,signal:a}),transport:l}}catch(c){var s=c,i=!0}finally{Oe(o,s,i)}}function Fe(){const e=N(),t=j(e.sessionId,e.proxy),n=H({device:e.device,endpoint:e.endpoint,open:e.open,pid:process.pid,proxy:e.proxy,sessionId:e.sessionId,startedAt:new Date().toISOString(),target:e.target});return n.save({logs:t}),{config:e,logs:t,store:n}}async function Ve(e,{host:t="127.0.0.1",onError:n,port:r=0,signal:o}={}){o?.throwIfAborted();let s;const i=()=>{const u=s;s=void 0,u&&(u.tunnel?.close(),u.socket.destroy())},c=async u=>{const{socket:l}=u;try{const h=await e.debug.createAdbTunnel();if(l.destroyed){h.close();return}u.tunnel=h,h.closed.catch(w=>{n?.(w)}),Ne(l,h)}catch(h){l.destroyed||n?.(h),l.destroy()}},a=A(u=>{i();const l={socket:u};s=l,u.once("close",()=>{s===l&&(s=void 0)}),c(l)}),d=()=>{o?.removeEventListener("abort",d),i(),a.close()};o?.addEventListener("abort",d,{once:!0});const p=await je(a,t,r);return{close:d,url:new URL(`adb://${p.address}:${String(p.port)}`)}}function Ne(e,t){$(t.readable,e,W),$(e,async n=>{const r=t.writable.getWriter();try{for await(const o of n)await r.write(o);await r.close()}catch(o){throw await r.abort(o),o}},W)}function W(){}function je(e,t,n){return new Promise((r,o)=>{e.once("error",o),e.listen({host:t,port:n},()=>{e.off("error",o);const s=e.address();if(s===null||typeof s=="string"){o(new Error("The listener has no tcp address"));return}r(s)})})}async function He(e,t,n,{signal:r}={}){try{if(!await Qe(t))return;const o=await Ve(e,{onError:s=>{f(`adb endpoint error: ${D(s)}`)},signal:r});n.save({adbSerial:o.url.host}),f(`adb endpoint listening at ${o.url.host}`)}catch(o){f(`adb endpoint unavailable: ${D(o)}`)}}async function Qe(e){return await Q(e.device.id,{endpoint:e.endpoint})==="android"}function D(e){return e instanceof Error?e.message:String(e)}async function Je(e,t,{methods:n={},signal:r}={}){await Z(t,{force:!0});const o=new Set,s=A(i=>{f("control client connected"),o.add(i),i.once("close",()=>{o.delete(i)});const c=new J(i,{signal:r});k(c,e,{leftEffects:()=>n})});r?.addEventListener("abort",()=>{for(const i of o)i.destroy()},{once:!0}),await new Promise((i,c)=>{s.once("error",c),s.listen({path:t,signal:r},()=>{s.off("error",c),i()})})}async function Ge(e,t,n,{recorder:r,signal:o}){const s=G(n);return await Je(e,s,{methods:{"appetizeio.cli.record.start":async i=>await r.start(i),"appetizeio.cli.record.stop":async()=>await r.stop()},signal:o}),t.save({controlSocket:s}),s}const q="127.0.0.1";async function Ke(e,t,n={}){try{const r=await Ze(e,n);return t.save({viewerUrl:r.url.href}),f(`web endpoint listening at ${r.url.href}`),r}catch(r){f(`web endpoint unavailable: ${tt(r)}`);return}}async function Ze(e,{port:t=0,signal:n,viewerHtml:r=B(new URL("./viewer.html",import.meta.url),"utf8")}){n?.throwIfAborted();const o=te((u,l)=>{Xe(u,l,r)}),s=new Set,i=new ne({noServer:!0,perMessageDeflate:!1});let c;i.on("connection",u=>{if(s.size>0){u.close(1013,"Viewer already connected");return}s.add(u);const l=new E(u);k(l,e),u.once("close",()=>{s.delete(u)})}),o.on("upgrade",(u,l,h)=>{Ye(i,u,l,h)});const a=()=>(c??=new Promise((u,l)=>{n?.removeEventListener("abort",d);for(const h of s)Be(h);i.close(),o.close(h=>{h?l(h):u()})}),c),d=()=>{a()};await et(o,t),n?.addEventListener("abort",d,{once:!0});const p=o.address();if(!p||typeof p=="string")throw await a(),new Error("Web address unavailable");return{close:a,url:new URL(`http://${q}:${String(p.port)}/`)}}function Be(e){e.close(1001)}function Xe(e,t,n){if(e.method==="GET"&&e.url==="/"){t.writeHead(200,{"Cache-Control":"no-store","Content-Type":"text/html; charset=utf-8"}),t.end(n);return}t.writeHead(404).end()}function Ye(e,t,n,r){if(t.url!=="/ws"){n.destroy();return}e.handleUpgrade(t,n,r,o=>{e.emit("connection",o,t)})}function et(e,t){return new Promise((n,r)=>{const o=s=>{r(s)};e.once("error",o),e.listen(t,q,()=>{e.removeListener("error",o),n()})})}function tt(e){return e instanceof Error?e.message:String(e)}function nt(e){const t=new AbortController,n=rt(e,t),r=K();return r.addEventListener("abort",()=>{const{reason:o}=r;n(o instanceof Error?o.message:String(o))},{once:!0}),{shutdown:n,signal:t.signal}}function rt(e,t){let n=!1;return r=>{n||(n=!0,e.save({endedAt:new Date().toISOString(),endedReason:r,state:{phase:"ended"}}),f(r),t.abort())}}async function x(e,t,{label:n,signal:r}){try{const o=await e();await re(o.readable.pipeThrough(ot()),X(t,{flags:"a"}))}catch(o){if(r?.aborted)return;const s=o instanceof Error?o.message:String(o);f(`${n} ended with an error: ${s}`)}}function ot(){return new TransformStream({transform:(e,t)=>{t.enqueue(`${JSON.stringify(e)}
2
2
  `)}})}async function st(e,t,{signal:n}={}){await x(async()=>await e.debug.createLogStream({signal:n}),t,{label:"log capture",signal:n})}async function it(e,t,{signal:n}={}){await x(async()=>await e.debug.createProxyInterceptStream({signal:n}),t,{label:"network capture",signal:n})}const T=30;async function at(e,t){await e.pipeTo(dt(t))}function ct(e){return{decoderConfig:{codec:"avc1.640028",codedHeight:e.height,codedWidth:e.width}}}function dt(e){let t;const n=async()=>{await t?.output.finalize()};return new WritableStream({abort:n,close:n,write:async r=>{r.data.byteLength!==0&&(t??=await ut(ft(r),e),lt(t,r),await pt(t,r))}})}async function ut(e,t){const n=new oe("avc"),r=new se({format:new ae({fastStart:"fragmented"}),target:new ie(t,{chunked:!1})});return r.addVideoTrack(n,{frameRate:T}),await r.start(),{frame:0,output:r,seen:{height:e.height,width:e.width},size:{height:e.height,width:e.width},source:n}}function lt(e,t){if(t.type!=="key")return;const{seen:n,size:r}=e;t.width===n.width&&t.height===n.height||(e.seen={height:t.height,width:t.width},f(`video resized to ${String(t.width)}x${String(t.height)}; keeping ${String(r.width)}x${String(r.height)}`))}function ft(e){if(e.type!=="key")throw new Error("Video stream started without a key chunk");return e}async function pt(e,t){const n=new ce(t.data,t.type,e.frame/T,1/T);e.frame+=1,await e.source.add(n,ct(e.size))}function ht(e,t,{signal:n}={}){let r,o;const s=async(a,{force:d=!1}={})=>{if(Y(a)){if(!d)throw new Error(`A recording already exists at ${a}; pass --force to overwrite`);ee(a)}const p=await e.video.createStream({signal:n});return r={done:wt(p,a),path:a,stream:p},t.save({video:{path:a,recording:!0}}),f(`recording video to ${a}`),{path:a}},i=async a=>{a.stream.close();try{await a.done,t.save({video:{path:a.path,recording:!1}})}finally{r=void 0,o=void 0}},c=async()=>{if(!r)return;const{path:a}=r;await i(r),f(`closed the video recording: ${a}`)};return n?.addEventListener("abort",()=>{c()},{once:!0}),{start:async({force:a=!1,path:d})=>{const p=r?.path??o;if(p!==void 0)throw new Error(`Already recording to ${p}`);o=d;try{return await s(d,{force:a})}finally{o=void 0}},stop:async()=>{if(!r)throw new Error("No video recording in progress");const{path:a}=r;return await i(r),{path:a}}}}async function wt(e,t){try{await at(e.readable,t)}catch(n){f(`video capture ended with an error: ${String(n)}`)}}async function vt(){const{config:e,logs:t,store:n}=Fe();f(`recording to ${n.path}`);const{shutdown:r,signal:o}=nt(n);try{const{device:s,transport:i}=await ze(e,n,{onClose:r,signal:o}),c=ht(s,n,{signal:o});await He(s,e,n,{signal:o}),await Ke(i,n,{signal:o});const a=await Ge(i,n,e.sessionId,{recorder:c,signal:o});f(`control socket listening at ${a}`),st(s,t.device,{signal:o}),f(`recording device logs to ${t.device}`),t.network&&(it(s,t.network,{signal:o}),f(`recording network events to ${t.network}`))}catch(s){if(o.aborted)return;const i=s instanceof Error?s.message:String(s);n.save({endedAt:new Date().toISOString(),endedReason:i,state:{message:i,phase:"error"}}),f(`error: ${i}`),process.exitCode=1}}vt();
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@appetize/cli",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Run, inspect and control iOS and Android apps on Appetize cloud devices",
5
- "license": "UNLICENSED",
5
+ "license": "SEE LICENSE IN LICENSE",
6
6
  "homepage": "https://appetize.io",
7
7
  "bugs": {
8
8
  "url": "https://appetize.io/support-request"
@@ -32,7 +32,7 @@
32
32
  "debug": "^4.4.3",
33
33
  "human-id": "^4.2.1",
34
34
  "json-rpc-2.0": "^1.8.1",
35
- "mediabunny": "^1.58.1",
35
+ "mediabunny": "^1.59.0",
36
36
  "sade": "^1.8.1",
37
37
  "ws": "^8.21.3",
38
38
  "yocto-spinner": "^1.2.2"