@appetize/cli 0.22.0 → 0.24.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 +18 -0
- package/cli.js +10 -9
- package/daemon.js +2 -3
- package/package.json +3 -3
- package/skills/appetize/SKILL.md +4 -3
- package/skills/appetize/references/commands.md +9 -2
- package/stream-BowDfEJS.js +4 -0
- package/stream-CdilRAka.js +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @appetize/cli
|
|
2
2
|
|
|
3
|
+
## 0.24.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- feat(session): `session start` sets the device's orientation, locale, timezone, location, appearance, font size, keyboards, autocorrect, soft keyboard and adb shell command
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- fix(session): `session start` errors include the webserver's reason
|
|
12
|
+
- fix(session): `session start` rejects invalid options before spawning its daemon
|
|
13
|
+
|
|
14
|
+
## 0.23.0
|
|
15
|
+
|
|
16
|
+
### Minor Changes
|
|
17
|
+
|
|
18
|
+
- feat(inspect): reports `matches` when a selector is given
|
|
19
|
+
- feat(inspect)!: writes `{ platform, roots }`, one root per match
|
|
20
|
+
|
|
3
21
|
## 0.22.0
|
|
4
22
|
|
|
5
23
|
### Minor Changes
|
package/cli.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
`}const
|
|
4
|
-
`)},
|
|
5
|
-
`)}return
|
|
2
|
+
import He from"sade";import{p as u,a as h,f as _,U as a,d as R,b as Ye,i as Je,c as Qe,e as et,r as te,g as E,h as T,j as tt,D as ot,s as nt,k as it,l as oe,m as ne,S as U,n as ie,o as re,q as se,v as rt,t as st,u as ae,w as at,x as ce,y as ct,z as pt,A as dt,B as lt,C as pe,E as de,F as ut,G as ft,H as mt,I as ht,J as gt,K as wt}from"./stream-BowDfEJS.js";import{existsSync as le,openAsBlob as yt,openSync as vt,closeSync as xt}from"node:fs";import{basename as z,extname as St,resolve as ue,dirname as bt,join as x,relative as Et}from"node:path";import{writeFile as fe,stat as At,rm as Ot,cp as It}from"node:fs/promises";import{spawn as Pt}from"node:child_process";import{fileURLToPath as me}from"node:url";import{humanId as Tt}from"human-id";import{homedir as $t}from"node:os";import kt from"debug";import Lt from"yocto-spinner";import Dt from"cli-table3";import"json-rpc-2.0";import"zod/mini";import"stream";import"module";import"node:net";const j=["closed","fullyOpen","partiallyOpen"],he=["back","menu","unlock","volumeDown","volumeUp"],ge=["home","lock"],we=[...ge,...he],F=["down","left","right","up"],ye=[{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 C({appId:e,buildNumber:t,created:o,id:n,platform:i,versionName:r}){return{appId:e,buildNumber:t,created:o,id:n,platform:i,versionName:r}}const Nt=["app"],_t=10,Rt=[{fields:ye,key:"builds"},{header:"Total",key:"total"},{header:"Next page",key:"nextPage"}];async function Ut({options:e,signal:t}){const o=u(e.app,"--app"),n=h(e.limit,"--limit")??_t,i=h(e.page,"--page"),r={limit:String(n)};o!==void 0&&(r.appId=o),i!==void 0&&(r.page=String(i));const s=await _("/api/v2/builds",{commandOptions:e,query:r,signal:t});return{builds:s.builds.map(C),nextPage:s.nextPage,total:s.total}}const zt=["note","tags"],ve=12e4,jt=2e3,M=[".apk",".apks",".zip",".tar.gz",".tgz"],Ft={".apk":"android",".apks":"android",".tar.gz":"ios",".tgz":"ios",".zip":"ios"},Ct=ye;async function Mt({options:e,positionals:[t],progress:o,signal:n}){const i=Gt(t),r=z(t);o.status(`Uploading ${r}`);const s=await Kt(t,i,e,n);if(e.wait!==!0)return o.status(`Uploaded ${r}`),C(s);o.step(`Uploaded ${r}`),o.status("Waiting for build metadata");const p=await Bt(s.id,e,n);return o.status(`Processed ${r}`),C(p)}async function Bt(e,t,o){const n=h(t.timeout,"--timeout")??ve,i=Date.now()+n;for(;;){const r=await _(`/api/v2/builds/${e}`,{commandOptions:t,signal:o});if(r.appId!==void 0)return r;if(Date.now()>=i)throw new Error(`Build ${e} is still being processed after ${String(n)}ms; find it with 'appetize build list'`);await R(jt,o)}}async function qt(e){if(!le(e))throw new Error(`No build file at ${e}`);return await yt(e)}function Gt(e){const t=e.toLowerCase(),o=M.find(n=>t.endsWith(n));if(o===void 0)throw new a(`Cannot tell the platform of ${z(e)}; upload a ${M.join(", ")} file`);return Ft[o]}async function Kt(e,t,o,n){const i=new FormData;i.set("platform",t);const r=u(o.note,"--note");r!==void 0&&i.set("note",r);const s=u(o.tags,"--tags");return s!==void 0&&i.set("tags",s),i.set("file",await qt(e),z(e)),await _("/api/v2/builds",{body:i,commandOptions:o,method:"POST",signal:n})}const Zt=["platform"],Vt=[{fields:[{header:"ID",key:"id"},{header:"Name",key:"name"},{header:"Platform",key:"platform"},{header:"OS versions",key:"osVersions"}],key:"devices"}];async function Wt({options:e,signal:t}){const o=Xt(e.platform);return{devices:(await Ye(e,t)).filter(i=>o===void 0||i.platform===o).map(Ht)}}function Xt(e){const t=u(e,"--platform");if(t!==void 0){if(!Je(t))throw new a(`--platform expects one of: ${Qe.join(", ")}`);return t}}function Ht({id:e,name:t,osVersions:o,platform:n}){return{id:e,name:t,osVersions:o,platform:n}}const Yt="cli-fold",Jt=async({device:e,positionals:[t]})=>{if(!Qt(t))throw new a(`Hinge must be one of: ${j.join(", ")}`);await e.fold(t)};function Qt(e){return j.some(t=>t===e)}const eo=async({device:e,options:t,positionals:[o]})=>{await e.gesture(oo(o),to(t))};function to(e){const t=h(e.timeout,"--timeout");return t===void 0?void 0:{timeout:t}}function oo(e){let t;try{t=et(e)}catch(o){throw o instanceof SyntaxError?new a(o.message):o}if(t.length===0)throw new a("Expected at least one gesture step");return t}const no="APPETIZE_OUTPUT";function io(e,t,o){return{format:ro(e,t),progress:so(e,o)}}function B(e,{extension:t,strict:o=!1}){const n=St(e);if(n==="")return`${e}${t}`;if(o&&n!==t)throw new a(`The output must be a path ending in ${t}`);return e}function ro(e,t){if(e.json===!0)return"json";if(e.json===!1)return"text";const o=process.env[no]?.trim();return o==="json"||o==="text"?o:t.isTTY===!0?"text":"json"}function so(e,t){return e.quiet===!0?"silent":t.isTTY===!0&&process.env.TERM!=="dumb"&&!("CI"in process.env)?"interactive":"silent"}const S=["text","testId"],g="select",A={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"}},q="index",$="position",G="x",K="y";function xe(e,{prefix:t=g}={}){return S.reduce((o,n)=>{const{help:i,suffix:r}=A[n];return o.option(`--${d(t,r)}`,i)},e).option(`--${d(t,q)}`,"Which match when multiple (0-based)")}function Z(e,t={}){const{prefix:o=g}=t;return xe(e,t).option(`--${d(o,$)}`,"Screen position to use as x,y (0-1), e.g., 0.5,0.75").option(`--${d(o,G)}`,"Horizontal position within the element (0-1, outside allowed)").option(`--${d(o,K)}`,"Vertical position within the element (0-1, outside allowed)")}function Se(e,{prefix:t=g}={}){const o=W(e,t);if(o.length===0)throw new a(`A selector is required: pass one of ${L(S,t)}`);if(o.length>1)throw new a(`Only one selector may be given, got ${L(o,t)}`);const[n]=o,i=Pe(e,d(t,A[n].suffix)),r=h(e[d(t,q)],`--${d(t,q)}`);switch(n){case"testId":return{by:"testId",index:r,testId:i};case"text":return{by:"text",index:r,text:po(i)}}}function be(e){const t=h(e.timeout,"--timeout");return t===void 0?void 0:{timeout:t}}function ao(e,{prefix:t=g}={}){return Oe(e,t)?Se(e,{prefix:t}):void 0}function k(e,{prefix:t=g}={}){return Oe(e,t)?Ee(e,{prefix:t}):void 0}function Ee(e,{prefix:t=g}={}){const o=d(t,$),n=e[o],i=Ie(e,d(t,G)),r=Ie(e,d(t,K));if(n===void 0){if(W(e,t).length===0)throw new a(`A selector is required: pass one of ${O({prefix:t})}`);return{...Se(e,{prefix:t}),x:i,y:r}}if(W(e,t).length>0)throw new a(`--${o} may not be combined with ${L(S,t)}`);if(i!==void 0||r!==void 0)throw new a(`--${o} may not be combined with --${d(t,G)} or --${d(t,K)}`);return co(Pe(e,o),o)}function O(e={}){const{prefix:t=g}=e;return`${L(S,t)}, --${d(t,$)}`}function V(e={}){const{prefix:t=g}=e;return[...Ae(e),d(t,$)]}function Ae({prefix:e=g}={}){return S.map(t=>d(e,A[t].suffix))}function W(e,t){return S.filter(o=>e[d(t,A[o].suffix)]!==void 0)}function d(...e){return e.filter(Boolean).join("-")}function L(e,t){return e.map(o=>`--${d(t,A[o].suffix)}`).join(", ")}function Oe(e,t){return Object.entries(e).some(([o,n])=>n!==void 0&&o.startsWith(`${t}-`))}function Ie(e,t){return h(e[t],`--${t}`)}function co(e,t){const o=e.split(",");if(o.length!==2)throw new a(`--${t} expects x,y (0-1), e.g., 0.5,0.75`);const[n,i]=o.map(r=>Number(r.trim()));return{by:"position",x:te(n,`--${t}`),y:te(i,`--${t}`)}}function po(e){const t=/^\/(.*)\/([a-z]*)$/s.exec(e);return t?new RegExp(t[1],t[2]):e}function Pe(e,t){return E(e[t],`--${t}`)}const lo=Ae(),uo=async({device:e,options:t,positionals:[o]})=>{const n=fo(o),i=ao(t),r=await e.inspect(i,be(t)),s=mo(r,t.pretty===!0);await fe(n,s);const p=Buffer.byteLength(s),c=Te(r.roots);return i?{bytes:p,matches:r.roots.length,nodes:c,output:n}:{bytes:p,nodes:c,output:n}};function Te(e){return e.reduce((t,o)=>t+1+Te(o.children),0)}function fo(e="inspect"){return B(e,{extension:".json"})}function mo(e,t){return`${JSON.stringify(e,null,t?2:void 0)}
|
|
3
|
+
`}const ho=async({device:e})=>{await e.clearKeychain()},go=async({device:e,positionals:[t]})=>{await e.open(wo(t))};function wo(e){if(typeof e=="string")try{return new URL(e),e}catch{throw new a("<url> expects a URL, e.g., myapp://orders/42")}throw new a("<url> expects a URL, e.g., myapp://orders/42")}const yo=new Set(we),vo=async({device:e,positionals:[t]})=>{if(!xo(t))throw new a(`Button must be one of: ${we.join(", ")}`);await e.press(t)};function xo(e){return typeof e=="string"&&yo.has(e)}const So=async({control:e,options:t,positionals:[o]})=>{const{path:n}=await e.recordStart({force:t.force===!0,path:Eo(o)});return{path:n}},bo=async({control:e})=>{const{path:t}=await e.recordStop();return{path:t}};function Eo(e){const t=ue(B(e,{extension:".mp4",strict:!0})),o=bt(t);if(!le(o))throw new a(`No such directory: ${o}`);return t}const Ao=async({device:e,positionals:[t]})=>{if(!Oo(t))throw new a(`Orientation must be one of: ${T.join(", ")}`);await e.rotate(t)};function Oo(e){return T.some(t=>t===e)}const Io=async({device:e,positionals:[t]})=>{const o=await e.screenshot(),n=To(o.mimeType,t);return await fe(n,o.data),{bytes:o.data.byteLength,output:n}};function Po(e){const t=e.split("/").at(1);return`.${t==="jpeg"?"jpg":t??"png"}`}function To(e,t="screenshot"){return B(t,{extension:Po(e)})}function $o(e){const t=me(new URL("./daemon.js",import.meta.url)),o=vt(tt(e.sessionId),"a"),n=Pt(process.execPath,[t],{detached:!0,env:{...process.env,[ot]:nt(e),DEBUG:ko(process.env.DEBUG)},stdio:["ignore",o,o]});if(xt(o),n.unref(),n.pid===void 0)throw new Error("Failed to spawn the session daemon");return n.pid}function ko(e){return e?`${e},appetize:daemon`:"appetize:daemon"}const Lo=/^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$/i;function Do(){return Tt({capitalize:!1,separator:"-"})}function No(e){if(!Lo.test(e))throw new a('--session-id must use letters, digits, ".", "-" or "_"');return e}const $e=1e4;function ke(e){if(!it(e))return!1;try{process.kill(e.pid,"SIGTERM")}catch{}return!0}async function Le(e,t){(ke(e)||Ro(e))&&await _o(e,t)}async function _o({pid:e,sessionId:t},o){const n=ne(e);try{await R($e,AbortSignal.any([o,n]))}catch(i){if(n.aborted||!oe(e))return;throw i}throw new U(`Session ${t} did not stop in time`,{sessionId:t})}function Ro({endedAt:e,pid:t}){return e!==void 0&&Date.now()-Date.parse(e)<$e&&oe(t)}const Uo=["device-android-shell","device-appearance","device-font-size","device-ios-keyboards","device-locale","device-location","device-orientation","device-os-version","device-timezone"];function zo(e,t){const o=De({shell:u(e["device-android-shell"],"--device-android-shell"),softKeyboard:ie(e["device-android-soft-keyboard"],{defaultValue:!0})}),n=De({autocorrect:ie(e["device-ios-autocorrect"],{defaultValue:!0}),keyboards:Co(e["device-ios-keyboards"],"--device-ios-keyboards")});return{android:o,appearance:X(e["device-appearance"],se,"--device-appearance"),fontSize:X(e["device-font-size"],re,"--device-font-size"),id:t,ios:n,locale:u(e["device-locale"],"--device-locale"),location:Fo(e["device-location"]),orientation:X(e["device-orientation"],T,"--device-orientation"),osVersion:u(e["device-os-version"],"--device-os-version"),timezone:u(e["device-timezone"],"--device-timezone")}}function jo(e){return e!==""&&Number.isFinite(Number(e))}function De(e){return Object.values(e).some(t=>t!==void 0)?e:void 0}function X(e,t,o){const n=u(e,o);if(n===void 0)return;const i=t.find(r=>r===n);if(i===void 0)throw new a(`${o} expects one of ${t.join(", ")}`);return i}function Fo(e){const t=u(e,"--device-location");if(t===void 0)return;const o=t.split(",").map(r=>r.trim());if(o.length!==2||!o.every(jo))throw new a("--device-location expects <lat>,<lng>, e.g., 43.65,-79.38");const[n,i]=o.map(Number);return{latitude:n,longitude:i}}function Co(e,t){return e===void 0?void 0:(Array.isArray(e)?e:[e]).map(n=>E(n,t))}const Ne={"device.id":"<device>","device.location.latitude":"--device-location latitude","device.location.longitude":"--device-location longitude","launch.url":"--open"};function Mo(e,{endpointFromEnv:t=!1}={}){const o=t?{...Ne,endpoint:"APPETIZE_ENDPOINT"}:Ne;try{rt(e)}catch(n){throw n instanceof st?new a(n.issues.map(i=>Bo(i,o)).join(`
|
|
4
|
+
`)):n}}function Bo({code:e,message:t,path:o},n){const i=qo(o,n);switch(e){case"invalidValue":return`${i} ${t.replace(/^expected /,"expects ")}`;case"needsLaunchApp":return`${i} has no effect without --target, or with --no-launch-app`;case"needsTarget":return`${i} has no effect without --target`}}function qo(e,t){return t[e]??`--${e.split(".").filter(o=>!/^\d+$/.test(o)).map(o=>o.replaceAll(/([A-Z])/g,"-$1").toLowerCase()).join("-")}`}const Go=[...Uo,"open","proxy","target"],Ko=150,Zo={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"},Vo=[{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 Wo(e,{onPhase:t,signal:o}){let n;for(;;){const i=ce(e),r=i?.state?.phase;if(r&&r!==n&&(n=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 R(Ko,o)}}function Xo(e){const t=u(e,"--open");if(t!==void 0){try{new URL(t)}catch{throw new a("--open expects a URL, e.g., myapp://orders/42")}return t}}function Ho(e){if(!(e===void 0||e===!1)){if(e===""||e==="intercept")return"intercept";if(typeof e!="string"||!Jo(e))throw new a("--proxy expects 'intercept' or an http:// url, e.g., http://proxy:8080");return e}}async function Yo({options:e,positionals:t,progress:o,signal:n}){const[i]=t,r=u(e.target,"--target")??en(e);if(r===void 0&&!process.env.APPETIZE_API_TOKEN)throw new a("APPETIZE_API_TOKEN must be set without a target",["Set APPETIZE_API_TOKEN, or pass --target"]);const s=u(e["session-id"],"--session-id"),p=e.force===!0;if(p&&s===void 0)throw new a("--force needs --session-id");const c=s===void 0?Do():No(s),l={device:zo(e,i),endpoint:at(e),featureFlags:ae(),launch:tn(Xo(e.open)),proxy:Ho(e.proxy),sessionId:c,target:r};Mo(l,{endpointFromEnv:e.endpoint===void 0});const f=ce(c);if(f!==void 0){if(!p)throw new ct(c);o.status(`Replacing session ${c}`),await Le(f,n),pt(c)}$o(l),o.step(`Session ${c}`);const v=dt(c,l.proxy);if(e.wait===!1)return{logs:v,sessionId:c};const I=await Wo(c,{onPhase:P=>{Qo(P)&&o.status(Zo[P]??"Starting the session")},signal:n}).catch(P=>{throw U.wrap(P,c)});return{adbSerial:I.adbSerial,baseUrl:I.baseUrl,controlSocket:I.controlSocket,logs:v,sessionId:c,viewerUrl:I.viewerUrl}}function Jo(e){return e.startsWith("http://")}function Qo(e){return e!=="ended"&&e!=="error"}function en(e){const t=e._;return Array.isArray(t)&&typeof t[0]=="string"?t[0]:void 0}function tn(e){return e===void 0?void 0:{url:e}}const on=async({options:e,progress:t,record:o,signal:n})=>(e.wait===!1?ke(o):(t.status(`Stopping session ${o.sessionId}`),await Le(o,n)),{sessionId:o.sessionId}),_e="appetize",nn=me(new URL(`./skills/${_e}/`,import.meta.url)),H=["agents","claude","codex","copilot","cursor"],Re=["project","user"],rn={agents:".agents",claude:".claude",codex:".codex",copilot:".copilot",cursor:".cursor"},sn=x(".agents","skills"),an=["claude","agents"];function cn(e){return H.some(t=>t===e)}function pn(e){return Re.some(t=>t===e)}async function dn(e){const t=e.agents??await ln(e),o=await Promise.all(t.map(n=>Ue(n,e)));return[...new Set(o.flat())]}async function Ue(e,t){const o=ze(e,t);if(e==="claude")return[x(o,"skills")];const n=x(t.root,sn);return t.scope==="project"?[n]:await je(o)?[n,x(o,"skills")]:[n]}function ze(e,t){if(e==="claude"&&t.scope==="user"){const o=process.env.CLAUDE_CONFIG_DIR?.trim();if(o)return ue(o)}return x(t.root,rn[e])}async function ln(e){const o=(await Promise.all(H.map(async n=>await un(n,e)?n:void 0))).filter(n=>n!==void 0);return o.length>0?o:an}async function un(e,t){const o=[ze(e,t),...await Ue(e,t)];return(await Promise.all(o.map(i=>je(i)))).includes(!0)}async function je(e){try{return(await At(e)).isDirectory()}catch{return!1}}const fn=["agent","scope"];async function mn({options:e,signal:t}){const o=wn(e.scope),n=gn(e.agent),i=o==="user"?$t():process.cwd(),r=await dn({agents:n,root:i,scope:o}),s=[];for(const p of r){t.throwIfAborted();const c=x(p,_e);await Ot(c,{force:!0,recursive:!0}),await It(nn,c,{recursive:!0}),s.push(o==="user"?c:Et(i,c))}return{installed:s}}function hn(e){const t=E(e,"--agent");if(!cn(t))throw new a(`--agent expects one of ${H.join(", ")}`);return t}function gn(e){return e===void 0?void 0:(Array.isArray(e)?e:[e]).map(o=>hn(o))}function wn(e){if(e===void 0)return"project";const t=E(e,"--scope");if(!pn(t))throw new a(`--scope expects one of ${Re.join(", ")}`);return t}const w="from",y="to",yn=[...V({prefix:w}),...V({prefix:y})],vn=async({device:e,options:t})=>{await e.swipe(En(t),On(t))};function xn(e){return F.some(t=>t===e)}function Sn(e){if(e===void 0)return;const t=E(e,"--direction");if(!xn(t))throw new a(`--direction expects one of ${F.join(", ")}`);return t}function bn(e,t){const o=lt(e.distance,"--distance");if(k(e,{prefix:y})!==void 0)throw new a(`--direction may not be combined with --${y}-*`);return{direction:t,distance:o,from:k(e,{prefix:w})}}function En(e){const t=Sn(e.direction);return t===void 0?An(e):bn(e,t)}function An(e){if(e.distance!==void 0)throw new a("--distance is only valid with --direction");return{from:Fe(k(e,{prefix:w}),w),to:Fe(k(e,{prefix:y}),y)}}function On(e){const t=h(e.duration,"--duration"),o=h(e.timeout,"--timeout");return t===void 0&&o===void 0?void 0:{duration:t,timeout:o}}function Fe(e,t){if(e===void 0)throw new a(`A swipe needs --direction, or both endpoints: pass one of ${O({prefix:t})}`);return e}const In=V(),Pn=async({device:e,options:t})=>{const o=Ee(t);await e.tap(o,be(t))},Tn=async({device:e,options:t,positionals:[o]})=>{await e.type(o,$n(t))};function $n(e){const t=h(e.delay,"--delay");return t===void 0?void 0:{delay:t}}const Y=kt("appetize:cli"),kn={fail:()=>{},status:()=>{},step:()=>{},succeed:()=>{}};function Ln(e,t,o){return e==="interactive"?Dn(t,o):kn}function Dn(e,t){const o=Lt({handleSignals:!1,stream:e});let n=0;const i=()=>{o.stop()};t.addEventListener("abort",i,{once:!0});const r=()=>(process.off("exit",i),o.isSpinning);return{fail:()=>{r()&&o.stop()},status:s=>{if(!t.aborted){if(o.isSpinning){o.text=s;return}n=Date.now(),process.once("exit",i),o.start(s)}},step:s=>{e.write(`\u25B8 ${s}
|
|
5
|
+
`)},succeed:()=>{if(!r())return;const s=Math.round((Date.now()-n)/1e3);o.success(`${o.text} (${String(s)}s)`)}}}const Nn=new Set(["adb","api","apk","id","os","url"]),_n={bottom:"","bottom-left":"","bottom-mid":"","bottom-right":"",left:"","left-mid":"",mid:"","mid-mid":"",middle:" ",right:"","right-mid":"",top:"","top-left":"","top-mid":"","top-right":""},Ce=2,Rn=10;function Un(e){return e.split(/(?=[A-Z])/).map((t,o)=>{const n=t.toLowerCase();return Nn.has(n)?n.toUpperCase():o===0?n.charAt(0).toUpperCase()+n.slice(1):n}).join(" ")}function zn(e,t,o){return Q(e)?Ze(e,t,o):D(e,{fields:t,key:""},o)}function jn(e,t){const o=[];for(let n=0;n<e.length;n+=t)o.push(e.slice(n,n+t));return o.length===0?[e]:o}function Me(e){return new Dt({chars:_n,head:e,style:{border:[],head:[],"padding-left":0,"padding-right":Ce}})}function Be(e,t){return e.find(o=>o.key===t)}function qe(e,t){return t?.header??Un(e)}function J(e){return e!=null}function Q(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Ge(e){return["boolean","number","string"].includes(typeof e)}function Ke(e,t){const o=new Set(t),n=e.map(r=>r.key).filter(r=>o.has(r)),i=new Set(e.filter(r=>r.hidden).map(r=>r.key));return[...new Set([...n,...t])].filter(r=>!i.has(r))}function Ze(e,t,o){const n=new Map(Object.entries(e)),i=Ke(t,[...n.keys()]).filter(f=>J(n.get(f))),r=i.map(f=>Be(t,f)),s=i.map((f,v)=>qe(f,r[v])),p=Math.max(0,...s.map(f=>f.length))+Ce,c=o===void 0?void 0:o-p-1,l=Me();return l.push(...i.map((f,v)=>[s[v],D(n.get(f),r[v],c)])),Ve(l.toString())}function Fn(e,t){const o=e.map(s=>new Map(Object.entries(s))),n=Ke(t,o.flatMap(s=>[...s.keys()])),i=n.map(s=>Be(t,s)),r=Me(n.map((s,p)=>qe(s,i[p])));for(const s of o)r.push(n.map((p,c)=>{const l=s.get(p);return J(l)?D(l,i[c]):""}));return Ve(r.toString())}function D(e,t,o){if(t?.format)return N(t.format(e),o);if(Ge(e))return N(pe(e),o);if(Array.isArray(e)){const n=e.filter(J);return n.every(Ge)?N(pe(n),o):n.every(Q)?Fn(n,t?.fields??[]):n.map(i=>D(i,t,o)).join(`
|
|
6
|
+
`)}return Q(e)?Ze(e,t?.fields??[],o):N(String(e),o)}function Ve(e){return e.split(`
|
|
6
7
|
`).map(t=>t.trimEnd()).join(`
|
|
7
|
-
`)}function
|
|
8
|
-
`).flatMap(o=>
|
|
9
|
-
`)}function vn(e,t){const o=[];let n="";for(const i of e.split(" ").flatMap(s=>yn(s,t)))n===""?n=i:n.length+1+i.length<=t?n=`${n} ${i}`:(o.push(n),n=i);return o.push(n),o}async function Sn(e,t={}){const o=await ie(e,{signal:t.signal});return new it(o,{...t,onClose:()=>{o.close(),t.onClose?.()}})}function bn(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");return Sn(e.controlSocket,{baseUrl:e.baseUrl,signal:t})}async function En(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");const o=await ie(e.controlSocket,{signal:t}),{client:n}=st(o,{});return{close:()=>{o.close()},recordStart:async i=>await n.request("appetizeio.cli.record.start",i),recordStop:async()=>await n.request("appetizeio.cli.record.stop")}}function In(e){return async({options:t,positionals:o,progress:n,signal:i})=>{const s=t["session-id"],r=rt(typeof s=="string"?s:void 0);try{return await e({options:t,positionals:o,progress:n,record:r,signal:AbortSignal.any([i,te(r.pid)])})}catch(d){throw R.wrap(d,r.sessionId)}}}function On(e){return async({options:t,positionals:o,progress:n,record:i,signal:s})=>{const r=await bn(i,s);try{return await e({device:r,options:t,positionals:o,progress:n,signal:s})}finally{r.close()}}}function Pn(e){return async({options:t,positionals:o,progress:n,record:i,signal:s})=>{const r=await En(i,s);try{return await e({control:r,options:t,positionals:o,progress:n,signal:s})}finally{r.close()}}}const Tn=["session-id",...Ot,...kt,...zt,...Qt,...Zo,...Oo,...Jo,...rn],An=["json","quiet"];function kn(e){const t=qe("appetize").version(e).option("--json","Print the result as JSON").option("--quiet","Hide progress");return b(t,"session start <device>",o=>o.describe(["Start a device session and wait until it is ready","","Environment variables:","APPETIZE_API_TOKEN: Authorizes private APIs and targets; required with no target","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").option("--target","App, build or app group id to run").example("session start iphone15pro --target app_a1b2c3").example("session start iphone15pro").example("session start pixel7 --target app_a1b2c3 --device-os-version 14").example("session start iphone15pro --target app_a1b2c3 --open myapp://orders/42").example("session start iphone15pro --target app_a1b2c3 --session-id demo --force"),Lo,{fields:Ao}),J(t,"session stop",o=>o.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"),Uo),b(t,"build list",o=>o.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"),At,{fields:Tt}),b(t,"build upload <file>",o=>o.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(fe)})`).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"'),Nt,{fields:Lt}),b(t,"device list",o=>o.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"),Ct,{fields:Ft}),m(t,"press <button>",o=>o.describe(`Press a device button: ${de.join(", ")}; Android only: ${pe.join(", ")}`).example("press home").example("press volumeUp"),ao),m(t,"rotate <orientation>",o=>o.describe("Rotate the device to an orientation: portrait, landscapeLeft, landscapeRight or upsideDown").example("rotate portrait").example("rotate landscapeLeft"),fo),m(t,"open <url>",o=>o.describe("Open a URL or deep link on the device").example("open myapp://orders/42").example("open https://example.com"),io),m(t,"keychain clear",o=>o.describe("Clear the entire device keychain (iOS only)").example("keychain clear"),no),m(t,"screenshot [output]",o=>o.describe("Take a screenshot of the current device screen").example("screenshot").example("screenshot home-screen").example("screenshot ./shots/checkout.png"),ho),m(t,"tap",o=>K(o.describe(["Tap an element matched by a selector, or a screen position","",`Pick one of: ${I()}`])).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"),an),m(t,"swipe",o=>K(K(o.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: ${I({prefix:w})}`,` ${I({prefix:g})}`]).option("--direction",`Direction to travel: ${j.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"),Yo),m(t,"gesture <syntax>",o=>o.describe(["Play a single-pointer gesture",""," > press the pointer down"," / lift it"," {wait} pause 400 ms"," {wait=ms} pause ms"," {...} move the pointer:"," byText=Photo to the element with exactly that text"," byText=/photo/i to the element matching the regex",' byText="My photos" quote values with spaces'," byTestId=card to the element with that test id"," index=1 to the second match, from 0"," x=0 y=0 to a point in it; 0-1 spans it"," 0.5,0.6 to a screen position, each 0-1"," x=0.5 y=0.6 the same screen position"," duration=ms glide time while down; default 400","","Single-quote the gesture. A pointer still down at the end lifts.","Double taps need {wait=100} between taps; Android drops faster ones."]).option("--timeout","Milliseconds to wait for any elements").example("gesture '{byText=Photo}>/' # tap").example("gesture '{byText=Photo}>{wait=800}/' # long-press").example("gesture '{0.5,0.5}>/{wait=100}>/' # double tap").example("gesture '{byTestId=card}>{byTestId=drop-zone duration=600}/' # drag").example(`gesture '{byText="My photos" x=0 y=0}>{wait}/' # top-left corner`).example("gesture '{byText=/photo/i index=1}>{wait=800}/' # 2nd match").example("gesture '{byTestId=slider x=0}>{byTestId=slider x=1 duration=1000}/' # slide").example("gesture '{0.5,0.8}>{0.5,0.6}{0.8,0.6}{0.8,0.2}/' # path").example("gesture '{0.2,0.4}>{0.5,0.6 duration=300}/{0.2,0.7}>{0.8,0.7}/' # 2 strokes").example("gesture '{byTestId=card}>{wait=800}/' --timeout 5000 # wait for card"),qt),m(t,"inspect [output]",o=>me(o.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"),eo),m(t,"type <text>",o=>o.describe(`Type text; {...} presses a named key: ${pt.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"),cn),Me(t,"recording start <output>",o=>o.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"),po),Me(t,"recording stop",o=>o.describe("Stop recording the device video and close the file").example("recording stop"),lo),b(t,"skill install",o=>o.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"),Wo),t}function b(e,t,o,n,{fields:i}={}){const s=t.split(" ").filter(r=>!/^[<[]/.test(r)).join(" ");return o(e.command(t)).action(Ln({action:n,fields:i,name:s}))}function J(e,t,o,n){return b(e,t,i=>o(i).option("--session-id","Target session id; defaults to the sole active session"),In(n))}function m(e,t,o,n){return J(e,t,o,On(n))}function Me(e,t,o,n){return J(e,t,o,Pn(n))}function Be(e){return`${String(Date.now()-e)}ms`}function $n(e,t,o){if(e==="json")return JSON.stringify(t===void 0?{}:t,null,2);const n=t===void 0?"":gn(t,o,process.stdout.columns);return n===""?void 0:n}async function Dn({action:e,fields:t=[],name:o},n){const i=Date.now();X(`invoked: ${process.argv.slice(2).join(" ")}`);const s=at(n),{format:r,progress:d}=Wt(s.options,process.stdout,process.stderr),l=ln(d,process.stderr,s.signal);try{const c=$n(r,await e({...s,progress:l}),t);l.succeed(),c!==void 0&&process.stdout.write(`${c}
|
|
10
|
-
`),
|
|
11
|
-
`),
|
|
8
|
+
`)}function N(e,t){return t===void 0||t<Rn?e:e.split(`
|
|
9
|
+
`).flatMap(o=>Cn(o,t)).join(`
|
|
10
|
+
`)}function Cn(e,t){const o=[];let n="";for(const i of e.split(" ").flatMap(r=>jn(r,t)))n===""?n=i:n.length+1+i.length<=t?n=`${n} ${i}`:(o.push(n),n=i);return o.push(n),o}async function Mn(e,t={}){const o=await de(e,{signal:t.signal});return new ut(o,{...t,onClose:()=>{o.close(),t.onClose?.()}})}function Bn(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");return Mn(e.controlSocket,{baseUrl:e.baseUrl,signal:t})}async function qn(e,t){if(e.controlSocket===void 0)throw new Error("Session is not ready");const o=await de(e.controlSocket,{signal:t}),{client:n}=ft(o,{});return{close:()=>{o.close()},recordStart:async i=>await n.request("appetizeio.cli.record.start",i),recordStop:async()=>await n.request("appetizeio.cli.record.stop")}}function Gn(e){return async({options:t,positionals:o,progress:n,signal:i})=>{const r=t["session-id"],s=mt(typeof r=="string"?r:void 0);try{return await e({options:t,positionals:o,progress:n,record:s,signal:AbortSignal.any([i,ne(s.pid)])})}catch(p){throw U.wrap(p,s.sessionId)}}}function Kn(e){return async({options:t,positionals:o,progress:n,record:i,signal:r})=>{const s=await Bn(i,r);try{return await e({device:s,options:t,positionals:o,progress:n,signal:r})}finally{s.close()}}}function Zn(e){return async({options:t,positionals:o,progress:n,record:i,signal:r})=>{const s=await qn(i,r);try{return await e({control:s,options:t,positionals:o,progress:n,signal:r})}finally{s.close()}}}const Vn=["session-id",...Nt,...zt,...Zt,...lo,...fn,...Go,...yn,...In],Wn=["json","quiet"];function Xn(e){const t=He("appetize").version(e).option("--json","Print the result as JSON").option("--quiet","Hide progress");return b(t,"session start <device>",o=>o.describe(["Start a device session and wait until it is ready","","Environment variables:","APPETIZE_API_TOKEN: Authorizes private APIs and targets; required with no target","APPETIZE_ENDPOINT: Appetize URL used when --endpoint is omitted"]).option("--device-android-shell","Android only: adb shell command run at start; needs --target").option("--device-appearance",`Device appearance: ${se.join(" or ")}`).option("--device-font-size",`Device font size: ${re.join(", ")}`).option("--device-ios-keyboards","iOS only: keyboard layout (e.g., en_US@sw=QWERTY); repeatable").option("--device-locale","Device locale as a BCP 47 tag (e.g., fr-CA)").option("--device-location","Device location as <lat>,<lng>; use = if latitude is negative").option("--device-orientation",`Initial orientation: ${T.join(", ")}`).option("--device-os-version","Device OS version (e.g., 17 for iOS, 14 for Android)").option("--device-timezone","Device IANA timezone (e.g., America/Toronto)").option("--endpoint","Appetize URL (e.g., https://appetize.io); overrides APPETIZE_ENDPOINT").option("--force","Replace a session with the same id",!1).option("--no-device-android-soft-keyboard","Android only: hide the on-screen keyboard").option("--no-device-ios-autocorrect","iOS only: keep autocorrect off, its default").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").option("--target","App, build or app group id to run").example("session start iphone15pro --target app_a1b2c3").example("session start iphone15pro").example("session start pixel7 --target app_a1b2c3 --device-os-version 14").example("session start iphone15pro --target app_a1b2c3 --open myapp://orders/42").example("session start iphone15pro --device-orientation landscapeLeft --device-appearance dark").example("session start pixel7 --device-locale fr-CA --device-timezone America/Toronto").example("session start iphone15pro --target app_a1b2c3 --session-id demo --force"),Yo,{fields:Vo}),ee(t,"session stop",o=>o.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"),on),b(t,"build list",o=>o.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"),Ut,{fields:Rt}),b(t,"build upload <file>",o=>o.describe([`Upload a build (${M.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(ve)})`).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"'),Mt,{fields:Ct}),b(t,"device list",o=>o.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"),Wt,{fields:Vt}),m(t,"press <button>",o=>o.describe(`Press a device button: ${ge.join(", ")}; Android only: ${he.join(", ")}`).example("press home").example("press volumeUp"),vo),m(t,"rotate <orientation>",o=>o.describe("Rotate the device to an orientation: portrait, landscapeLeft, landscapeRight or upsideDown").example("rotate portrait").example("rotate landscapeLeft"),Ao),Yt in ae()&&m(t,"fold <hinge>",o=>o.describe(`Fold a foldable device: ${j.join(", ")}`).example("fold closed").example("fold fullyOpen"),Jt),m(t,"open <url>",o=>o.describe("Open a URL or deep link on the device").example("open myapp://orders/42").example("open https://example.com"),go),m(t,"keychain clear",o=>o.describe("Clear the entire device keychain (iOS only)").example("keychain clear"),ho),m(t,"screenshot [output]",o=>o.describe("Take a screenshot of the current device screen").example("screenshot").example("screenshot home-screen").example("screenshot ./shots/checkout.png"),Io),m(t,"tap",o=>Z(o.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"),Pn),m(t,"swipe",o=>Z(Z(o.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 --${y}-* point to form a path`,"",`Pick one per point: ${O({prefix:w})}`,` ${O({prefix:y})}`]).option("--direction",`Direction to travel: ${F.join(", ")}`).option("--distance","Travel as a fraction of the screen (0-1)"),{prefix:w}),{prefix:y}).option("--duration","Milliseconds the gesture travels for").option("--timeout",`Milliseconds to wait for any --${w}/--${y} 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"),vn),m(t,"gesture <syntax>",o=>o.describe(["Play a single-pointer gesture",""," > press the pointer down"," / lift it"," {wait} pause 400 ms"," {wait=ms} pause ms"," {...} move the pointer:"," byText=Photo to the element with exactly that text"," byText=/photo/i to the element matching the regex",' byText="My photos" quote values with spaces'," byTestId=card to the element with that test id"," index=1 to the second match, from 0"," x=0 y=0 to a point in it; 0-1 spans it"," 0.5,0.6 to a screen position, each 0-1"," x=0.5 y=0.6 the same screen position"," duration=ms glide time while down; default 400","","Single-quote the gesture. A pointer still down at the end lifts.","Double taps need {wait=100} between taps; Android drops faster ones."]).option("--timeout","Milliseconds to wait for any elements").example("gesture '{byText=Photo}>/' # tap").example("gesture '{byText=Photo}>{wait=800}/' # long-press").example("gesture '{0.5,0.5}>/{wait=100}>/' # double tap").example("gesture '{byTestId=card}>{byTestId=drop-zone duration=600}/' # drag").example(`gesture '{byText="My photos" x=0 y=0}>{wait}/' # top-left corner`).example("gesture '{byText=/photo/i index=1}>{wait=800}/' # 2nd match").example("gesture '{byTestId=slider x=0}>{byTestId=slider x=1 duration=1000}/' # slide").example("gesture '{0.5,0.8}>{0.5,0.6}{0.8,0.6}{0.8,0.2}/' # path").example("gesture '{0.2,0.4}>{0.5,0.6 duration=300}/{0.2,0.7}>{0.8,0.7}/' # 2 strokes").example("gesture '{byTestId=card}>{wait=800}/' --timeout 5000 # wait for card"),eo),m(t,"inspect [output]",o=>xe(o.describe("Capture the view hierarchy of the screen, or of every element a selector matches")).option("--timeout","Milliseconds to wait for a match").option("--pretty","Pretty-print the JSON",!1).example("inspect").example("inspect --select-text 'Log in'").example("inspect hierarchy.json --pretty"),uo),m(t,"type <text>",o=>o.describe(`Type text; {...} presses a named key: ${wt.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"),Tn),We(t,"recording start <output>",o=>o.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"),So),We(t,"recording stop",o=>o.describe("Stop recording the device video and close the file").example("recording stop"),bo),b(t,"skill install",o=>o.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"),mn),t}function b(e,t,o,n,{fields:i}={}){const r=t.split(" ").filter(s=>!/^[<[]/.test(s)).join(" ");return o(e.command(t)).action(Jn({action:n,fields:i,name:r}))}function ee(e,t,o,n){return b(e,t,i=>o(i).option("--session-id","Target session id; defaults to the sole active session"),Gn(n))}function m(e,t,o,n){return ee(e,t,o,Kn(n))}function We(e,t,o,n){return ee(e,t,o,Zn(n))}function Xe(e){return`${String(Date.now()-e)}ms`}function Hn(e,t,o){if(e==="json")return JSON.stringify(t===void 0?{}:t,null,2);const n=t===void 0?"":zn(t,o,process.stdout.columns);return n===""?void 0:n}async function Yn({action:e,fields:t=[],name:o},n){const i=Date.now();Y(`invoked: ${process.argv.slice(2).join(" ")}`);const r=ht(n),{format:s,progress:p}=io(r.options,process.stdout,process.stderr),c=Ln(p,process.stderr,r.signal);try{const l=Hn(s,await e({...r,progress:c}),t);c.succeed(),l!==void 0&&process.stdout.write(`${l}
|
|
11
|
+
`),Y(`ok in ${Xe(i)}`)}catch(l){c.fail(),process.stderr.write(`${gt(s,l,{command:o})}
|
|
12
|
+
`),Y(`failed after ${Xe(i)}:`,l),process.exitCode=1}}function Jn(e){return(...t)=>{Yn(e,t)}}Xn("0.24.0").parse(process.argv,{boolean:Wn,string:Vn});
|
package/daemon.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import{T as j,F as A,G as z,H as E,I as V,y as _,J as F,K as N,L as $,M as H,N as Q,u as J,O as G,P as K,Q as Z,R as B}from"./stream-CdilRAka.js";import T from"debug";import*as h from"zod/mini";import{createServer as R}from"node:net";import{pipeline as x}from"node:stream";import{rm as X}from"node:fs/promises";import{readFileSync as Y,createWriteStream as ee,existsSync as te,rmSync as ne}from"node:fs";import{createServer as oe}from"node:http";import{WebSocketServer as re}from"ws";import{pipeline as se}from"node:stream/promises";import{EncodedVideoPacketSource as ie,Output as ae,FilePathTarget as ce,Mp4OutputFormat as de,EncodedPacket as ue}from"mediabunny";import"json-rpc-2.0";import"node:path";import"node:os";import"stream";import"module";const le=T("appetize.io:transport:ws-ping"),pe=3e4;function fe(e){let t=!0;const n=()=>{t=!0};e.on("pong",n);const o=setInterval(()=>{if(!t){clearInterval(o),le("liveness ping went unanswered, terminating"),e.terminate();return}e.readyState===e.OPEN&&(t=!1,e.ping())},pe);return{destroy(){clearInterval(o),e.off("pong",n)},pong:n}}const v=T("appetize.io:trace:transport:websocket");class L extends j{constructor(t,{signal:n}={}){super(async r=>{this.#e.send(A(r))},{signal:n}),this.wsOrUrl=t,typeof t=="string"?(this.#e=new WebSocket(t),this.#e.binaryType="arraybuffer"):this.#e=t;const o=this.#e;if(o instanceof WebSocket?(o.addEventListener("open",this.#s),o.addEventListener("message",this.#r),o.addEventListener("error",this.#o),o.addEventListener("close",this.#n),this.#t.push(()=>{o.removeEventListener("open",this.#s),o.removeEventListener("message",this.#r),o.removeEventListener("error",this.#o),o.removeEventListener("close",this.#n)})):(o.addEventListener("open",this.#s),o.addEventListener("message",this.#r),o.addEventListener("error",this.#o),o.addEventListener("close",this.#n),this.#t.push(()=>{o.removeEventListener("open",this.#s),o.removeEventListener("message",this.#r),o.removeEventListener("error",this.#o),o.removeEventListener("close",this.#n)})),this.send=o.send.bind(o),"ping"in o){const{destroy:r,pong:s}=fe(o);this.#c=s,this.#t.push(r)}}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()}};#o=t=>{v("handleWebSocketError()",t),this.onerror?.(t)};#r=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[z].dispatchMessage(n)};#s=t=>{v("handleWebSocketOpen()",t),this.onopen?.(t)};#u(t){try{const n=E(new Uint8Array(t.data));return V(n)?n:null}catch{return null}}}function y(e){return typeof e=="object"&&e!==null}function P(e){try{const t=E(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 he(e,t={}){const n=t.baseUrl??(typeof e=="string"?new URL(e).origin:void 0),o=typeof e=="string"?new L(e,{signal:t.signal}):e,r=new _(o,{...t,baseUrl:n});return typeof e=="string"&&await we(o,r,t),r}function we(e,t,n){const o=F(n)?.onLegacyMessage;return new Promise((r,s)=>{e.onopen=()=>{r()},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=P(i);c&&o?.(c)}})}async function ge(e,{onStateChange:t,signal:n}){n?.throwIfAborted(),t?.({phase:"starting"}),await new Promise((o,r)=>{const s=()=>{i(),r(n?.reason)},i=()=>{e.onmessage=void 0,e.onclose=void 0,n?.removeEventListener("abort",s)};e.onmessage=c=>{const a=ve(P(c));a&&(t?.(a),a.phase==="ready"&&(i(),o()))},e.onclose=()=>{i(),r(new Error("The device closed before it was ready"))},n?.addEventListener("abort",s,{once:!0})})}function ve(e){if(!e)return;if(e.name==="ready")return{phase:"ready"};if(e.name!=="launchStatus")return;switch(y(e.payload)&&typeof e.payload.status=="string"?e.payload.status:void 0){case"downloadingApp":return{phase:"downloadingApp"};case"installingApp":return{phase:"installingApp"};case"launchingApp":return{phase:"launchingApp"};default:return}}function me(){return typeof process>"u"?void 0:process}function ye(e){return{deviceType:e.id,osVersion:e.osVersion}}function be(e){return e==="intercept"?"interceptOnDemand":e}class m extends Error{code;issues;constructor(t,n,o=[]){super(n),this.name="SessionRequestError",this.code=t,this.issues=o}}const b=e=>h.string({error:e}).check(h.minLength(1,e)),Se=h.object({apiToken:h.optional(b("expected an API token")),codec:h.optional(h.literal(["h264","jpeg"],{error:"expected h264 or jpeg"})),device:h.object({id:b("expected a device id"),osVersion:h.optional(b("expected an OS version"))},{error:"expected an object"}),endpoint:h.optional(h.url({error:"expected a URL"})),open:h.optional(h.url({error:"expected a URL"})),proxy:h.optional(h.string({error:"expected intercept or an http:// URL"}).check(h.refine(e=>e==="intercept"||e.startsWith("http://"),{error:"expected intercept or an http:// URL"}))),target:h.optional(b("expected an app, build or group id"))});function Ee(e){const t=Se.safeParse(e);if(t.success)return;const n=t.error.issues.map(o=>({message:o.message,path:o.path.join(".")}));throw new m("invalid",n.map(({message:o,path:r})=>`${r}: ${o}`).join(`
|
|
2
|
-
`)
|
|
3
|
-
`)}})}async function ut(e,t,{signal:n}={}){await q(async()=>await e.debug.createLogStream({signal:n}),t,{label:"log capture",signal:n})}async function lt(e,t,{signal:n}={}){await q(async()=>await e.debug.createProxyInterceptStream({signal:n}),t,{label:"network capture",signal:n})}const k=30;async function pt(e,t){await e.pipeTo(ht(t))}function ft(e){return{decoderConfig:{codec:"avc1.640028",codedHeight:e.height,codedWidth:e.width}}}function ht(e){let t;const n=async()=>{await t?.output.finalize()};return new WritableStream({abort:n,close:n,write:async o=>{o.data.byteLength!==0&&(t??=await wt(vt(o),e),gt(t,o),await mt(t,o))}})}async function wt(e,t){const n=new ie("avc"),o=new ae({format:new de({fastStart:"fragmented"}),target:new ce(t,{chunked:!1})});return o.addVideoTrack(n,{frameRate:k}),await o.start(),{frame:0,output:o,seen:{height:e.height,width:e.width},size:{height:e.height,width:e.width},source:n}}function gt(e,t){if(t.type!=="key")return;const{seen:n,size:o}=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(o.width)}x${String(o.height)}`))}function vt(e){if(e.type!=="key")throw new Error("Video stream started without a key chunk");return e}async function mt(e,t){const n=new ue(t.data,t.type,e.frame/k,1/k);e.frame+=1,await e.source.add(n,ft(e.size))}function yt(e,t,{signal:n}={}){let o,r;const s=async(a,{force:d=!1}={})=>{if(te(a)){if(!d)throw new Error(`A recording already exists at ${a}; pass --force to overwrite`);ne(a)}const p=await e.video.createStream({signal:n});return o={done:bt(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{o=void 0,r=void 0}},c=async()=>{if(!o)return;const{path:a}=o;await i(o),f(`closed the video recording: ${a}`)};return n?.addEventListener("abort",()=>{c()},{once:!0}),{start:async({force:a=!1,path:d})=>{const p=o?.path??r;if(p!==void 0)throw new Error(`Already recording to ${p}`);r=d;try{return await s(d,{force:a})}finally{r=void 0}},stop:async()=>{if(!o)throw new Error("No video recording in progress");const{path:a}=o;return await i(o),{path:a}}}}async function bt(e,t){try{await pt(e.readable,t)}catch(n){f(`video capture ended with an error: ${String(n)}`)}}async function St(){const{config:e,logs:t,store:n}=Ze();f(`recording to ${n.path}`);const{shutdown:o,signal:r}=at(n);try{const{device:s,transport:i}=await Ke(e,n,{onClose:o,signal:r}),c=yt(s,n,{signal:r});await Be(s,e,n,{signal:r}),await et(i,n,{signal:r});const a=await Ye(i,n,e.sessionId,{recorder:c,signal:r});f(`control socket listening at ${a}`),ut(s,t.device,{signal:r}),f(`recording device logs to ${t.device}`),t.network&&(lt(s,t.network,{signal:r}),f(`recording network events to ${t.network}`))}catch(s){if(r.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}}St();
|
|
1
|
+
import{T as _,L as A,M as F,N as E,O as z,F as V,P as N,t as v,Q as j,v as H,R as Q,V as J,W as $,X as G,Y as K,A as Z,Z as X,_ as Y,$ as B,a0 as ee}from"./stream-BowDfEJS.js";import T from"debug";import{createServer as R}from"node:net";import{pipeline as P}from"node:stream";import{rm as te}from"node:fs/promises";import{readFileSync as ne,createWriteStream as oe,existsSync as re,rmSync as se}from"node:fs";import{createServer as ie}from"node:http";import{WebSocketServer as ae}from"ws";import{pipeline as ce}from"node:stream/promises";import{EncodedVideoPacketSource as de,Output as ue,FilePathTarget as le,Mp4OutputFormat as fe,EncodedPacket as pe}from"mediabunny";import"json-rpc-2.0";import"zod/mini";import"node:path";import"node:os";import"stream";import"module";const he=T("appetize.io:transport:ws-ping"),we=3e4;function ge(e){let t=!0;const n=()=>{t=!0};e.on("pong",n);const o=setInterval(()=>{if(!t){clearInterval(o),he("liveness ping went unanswered, terminating"),e.terminate();return}e.readyState===e.OPEN&&(t=!1,e.ping())},we);return{destroy(){clearInterval(o),e.off("pong",n)},pong:n}}const w=T("appetize.io:trace:transport:websocket");class L extends _{constructor(t,{signal:n}={}){super(async r=>{this.#e.send(A(r))},{signal:n}),this.wsOrUrl=t,typeof t=="string"?(this.#e=new WebSocket(t),this.#e.binaryType="arraybuffer"):this.#e=t;const o=this.#e;if(o instanceof WebSocket?(o.addEventListener("open",this.#s),o.addEventListener("message",this.#r),o.addEventListener("error",this.#o),o.addEventListener("close",this.#n),this.#t.push(()=>{o.removeEventListener("open",this.#s),o.removeEventListener("message",this.#r),o.removeEventListener("error",this.#o),o.removeEventListener("close",this.#n)})):(o.addEventListener("open",this.#s),o.addEventListener("message",this.#r),o.addEventListener("error",this.#o),o.addEventListener("close",this.#n),this.#t.push(()=>{o.removeEventListener("open",this.#s),o.removeEventListener("message",this.#r),o.removeEventListener("error",this.#o),o.removeEventListener("close",this.#n)})),this.send=o.send.bind(o),"ping"in o){const{destroy:r,pong:s}=ge(o);this.#c=s,this.#t.push(r)}}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){w("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=>{w("handleWebSocketClose()",t);try{this.#d(t)}finally{this.close()}};#o=t=>{w("handleWebSocketError()",t),this.onerror?.(t)};#r=async t=>{w("handleWebSocketMessage()",t),this.#c?.();const n=this.#u(t);if(!n){w("handleWebSocketMessage() -> legacy"),this.onmessage?.(t);return}w("handleWebSocketMessage() -> jsonrpc",n),this[F].dispatchMessage(n)};#s=t=>{w("handleWebSocketOpen()",t),this.onopen?.(t)};#u(t){try{const n=E(new Uint8Array(t.data));return z(n)?n:null}catch{return null}}}function b(e){return typeof e=="object"&&e!==null}function I(e){try{const t=E(new Uint8Array(e.data));if(b(t)&&typeof t.name=="string")return{name:t.name,payload:"payload"in t?t.payload:void 0}}catch{}}async function ve(e,t={}){const n=t.baseUrl??(typeof e=="string"?new URL(e).origin:void 0),o=typeof e=="string"?new L(e,{signal:t.signal}):e,r=new V(o,{...t,baseUrl:n});return typeof e=="string"&&await me(o,r,t),r}function me(e,t,n){const o=N(n)?.onLegacyMessage;return new Promise((r,s)=>{e.onopen=()=>{r()},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=I(i);c&&o?.(c)}})}async function ye(e,{onStateChange:t,signal:n}){n?.throwIfAborted(),t?.({phase:"starting"}),await new Promise((o,r)=>{const s=()=>{i(),r(n?.reason)},i=()=>{e.onmessage=void 0,e.onclose=void 0,n?.removeEventListener("abort",s)};e.onmessage=c=>{const a=be(I(c));a&&(t?.(a),a.phase==="ready"&&(i(),o()))},e.onclose=()=>{i(),r(new Error("The device closed before it was ready"))},n?.addEventListener("abort",s,{once:!0})})}function be(e){if(!e)return;if(e.name==="ready")return{phase:"ready"};if(e.name!=="launchStatus")return;switch(b(e.payload)&&typeof e.payload.status=="string"?e.payload.status:void 0){case"downloadingApp":return{phase:"downloadingApp"};case"installingApp":return{phase:"installingApp"};case"launchingApp":return{phase:"launchingApp"};default:return}}function Se(){return typeof process>"u"?void 0:process}function Ee(e){return e==="intercept"?"interceptOnDemand":e}function Te(e,{onClose:t,onEvent:n,signal:o}={}){return new Promise((r,s)=>{const i=new WebSocket(e);i.binaryType="arraybuffer",i.onopen=()=>{W(i,{capabilities:{},t:"handshake"})},i.onmessage=c=>{Le(i,c,{onEvent:n,onHandshake:a=>{r({close:()=>{i.close()},id:a})}})},i.onclose=()=>{s(new v("disconnected",`The connection to ${e} closed`)),t?.()},o?.addEventListener("abort",()=>{s(o.reason),i.close()},{once:!0})})}function Le(e,t,n){const o=E(new Uint8Array(t.data));b(o)&&(o.t==="handshake"&&typeof o.id=="string"?n.onHandshake(o.id):o.name==="ping"?W(e,{name:"pong"}):typeof o.name=="string"&&n.onEvent?.(o.name,o.payload))}function W(e,t){e.send(A(t))}async function ke(e,t,{apiToken:n,onQueue:o,signal:r}={}){const s=j();Pe(s.reject,r);const i=await Te(Ie(e),{onClose:()=>{s.reject(Ae())},onEvent:(c,a)=>{$e(c,a,s,o)},signal:r});return Re(e,{apiToken:n,data:t,socketid:i.id},r).catch(s.reject),await s.promise.finally(()=>{i.close()})}function Ae(){return new v("disconnected","The webserver connection closed before a session was granted")}function U(e,t){return b(e)?e[t]:void 0}function $e(e,t,n,o){if(e==="queue"||e==="concurrentQueue"){const r=e==="queue"?Ue(t):Ce(t);r&&o?.(r)}else e==="newSession"?We(n,t):e==="userError"&&n.reject(new v("denied",De(t)))}function C(e,t){const n=U(e,t);return typeof n=="number"?n:void 0}function m(e,t){const n=U(e,t);return typeof n=="string"?n:void 0}async function Re(e,t,n){const o=await fetch(new URL("/api/session/request",e),{body:JSON.stringify(t),headers:{"content-type":"application/json"},method:"POST",signal:n});if(!o.ok)throw new v("denied",await Me(o))}function Pe(e,t){t?.addEventListener("abort",()=>{e(t.reason)},{once:!0})}function Ie(e){const t=new URL("/client",e);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.href}function We(e,t){const n=m(t,"path"),o=m(t,"sessionToken");n!==void 0&&o!==void 0?e.resolve({path:n,sessionToken:o}):e.reject(new v("denied","The granted session is missing its address"))}function Ue(e){const t=C(e,"position");return t===void 0?void 0:{position:t,reason:"capacity"}}function Ce(e){const t=C(e,"position");if(t!==void 0)return{limit:m(e,"name")??"Queue",position:t,reason:"concurrency"}}function De(e){return m(e,"message")??"The session request was denied"}async function Me(e){const t=`The webserver rejected the session request (${String(e.status)})`,n=await e.json().catch(()=>{}),o=m(n,"message");return o?`${t}: ${o}`:t}async function qe({apiToken:e=Oe(),codec:t="h264",device:n,endpoint:o="https://appetize.io",launch:r,onStateChange:s,proxy:i,signal:c,target:a}){c?.throwIfAborted(),H({apiToken:e,codec:t,device:n,endpoint:o,launch:r,proxy:i,target:a}),s?.({phase:"requesting"});const d=await ke(o,{codec:t,noVideo:!0,proxy:Ee(i),publicKey:a,...J(n),...Q(r)},{apiToken:e,onQueue:h=>{s?.({phase:"queued",queue:h})},signal:c});return{baseUrl:new URL(d.path).origin,url:xe(d)}}function Oe(){return Se()?.env.APPETIZE_API_TOKEN}function xe(e){const t=new URL("/client",e.path);return t.protocol=t.protocol==="http:"?"ws:":"wss:",t.searchParams.set("sessionToken",e.sessionToken),t.href}async function _e(e,{host:t="127.0.0.1",onError:n,port:o=0,signal:r}={}){r?.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 p=await e.debug.createAdbTunnel();if(l.destroyed){p.close();return}u.tunnel=p,p.closed.catch(S=>{n?.(S)}),Fe(l,p)}catch(p){l.destroyed||n?.(p),l.destroy()}},a=R(u=>{i();const l={socket:u};s=l,u.once("close",()=>{s===l&&(s=void 0)}),c(l)}),d=()=>{r?.removeEventListener("abort",d),i(),a.close()};r?.addEventListener("abort",d,{once:!0});const h=await ze(a,t,o);return{close:d,url:new URL(`adb://${h.address}:${String(h.port)}`)}}function Fe(e,t){P(t.readable,e,D),P(e,async n=>{const o=t.writable.getWriter();try{for await(const r of n)await o.write(r);await o.close()}catch(r){throw await o.abort(r),r}},D)}function D(){}function ze(e,t,n){return new Promise((o,r)=>{e.once("error",r),e.listen({host:t,port:n},()=>{e.off("error",r);const s=e.address();if(s===null||typeof s=="string"){r(new Error("The listener has no tcp address"));return}o(s)})})}async function Ve(e,{methods:t={},path:n,signal:o}){o?.throwIfAborted(),await te(n,{force:!0});const r=new Set,s=R(c=>{r.add(c),c.once("close",()=>{r.delete(c)}),$(new G(c),e,{leftEffects:()=>t})}),i=()=>{o?.removeEventListener("abort",i);for(const c of r)c.destroy();s.close()};return await Ne(s,n),o?.aborted&&(i(),o.throwIfAborted()),o?.addEventListener("abort",i,{once:!0}),{close:i,path:n}}function Ne(e,t){return new Promise((n,o)=>{e.once("error",o),e.listen(t,()=>{e.off("error",o),n()})})}const je=T("appetize:daemon");function f(e){je(e)}function He(e,{message:t="The operation timed out"}={}){const n=new AbortController,o=setTimeout(()=>{n.abort(new Error(t))},e),r=()=>{clearTimeout(o)};return{cancel:r,signal:n.signal,[Symbol.dispose]:r}}var Qe=(e,t)=>(t=Symbol[e])?t:Symbol.for("Symbol."+e),M=e=>{throw TypeError(e)},Je=(e,t,n)=>{if(t!=null){typeof t!="object"&&typeof t!="function"&&M("Object expected");var o,r;o===void 0&&(o=t[Qe("dispose")]),typeof o!="function"&&M("Object not disposable"),r&&(o=function(){try{r.call(this)}catch(s){return Promise.reject(s)}}),e.push([n,o,t])}return t},Ge=(e,t,n)=>{var o=typeof SuppressedError=="function"?SuppressedError:function(i,c,a,d){return d=Error(a),d.name="SuppressedError",d.error=i,d.suppressed=c,d},r=i=>t=n?new o(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=>(r(a),s()))}catch(a){r(a)}if(n)throw t};return s()};const Ke=1e4;async function Ze(e,t,{onClose:n,signal:o}={}){var r=[];try{const c=Je(r,He(Ke,{message:"Timed out waiting for session request state"})),a=AbortSignal.any([o,c.signal].filter(g=>!!g)),d=g=>{t.save({state:g}),f(g.phase),g.phase!=="requesting"&&c.cancel()},{featureFlags:h,sessionId:u,...l}=e,{baseUrl:p,url:S}=await qe({...l,onStateChange:d,signal:a});t.save({baseUrl:p});const y=new L(S,{signal:a});return await ye(y,{onStateChange:d,signal:a}),y.onclose=()=>{n?.("The device connection closed")},{device:await ve(y,{baseUrl:p,signal:a}),transport:y}}catch(c){var s=c,i=!0}finally{Ge(r,s,i)}}function Xe(){const e=K(),t=Z(e.sessionId,e.proxy),n=X({device:e.device,endpoint:e.endpoint,open:e.launch?.url,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 Ye(e,t,n,{signal:o}={}){try{if(!await Be(t))return;const r=await _e(e,{onError:s=>{f(`adb endpoint error: ${q(s)}`)},signal:o});n.save({adbSerial:r.url.host}),f(`adb endpoint listening at ${r.url.host}`)}catch(r){f(`adb endpoint unavailable: ${q(r)}`)}}async function Be(e){return await Y(e.device.id,{endpoint:e.endpoint})==="android"}function q(e){return e instanceof Error?e.message:String(e)}async function et(e,t,n,{recorder:o,signal:r}){const{path:s}=await Ve(e,{methods:{"appetizeio.cli.record.start":async i=>await o.start(i),"appetizeio.cli.record.stop":async()=>await o.stop()},path:B(n),signal:r});return t.save({controlSocket:s}),s}const O="127.0.0.1";async function tt(e,t,n={}){try{const o=await nt(e,n);return t.save({viewerUrl:o.url.href}),f(`web endpoint listening at ${o.url.href}`),o}catch(o){f(`web endpoint unavailable: ${at(o)}`);return}}async function nt(e,{port:t=0,signal:n,viewerHtml:o=ne(new URL("./viewer.html",import.meta.url),"utf8")}){n?.throwIfAborted();const r=ie((u,l)=>{rt(u,l,o)}),s=new Set,i=new ae({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 L(u);$(l,e),u.once("close",()=>{s.delete(u)})}),r.on("upgrade",(u,l,p)=>{st(i,u,l,p)});const a=()=>(c??=new Promise((u,l)=>{n?.removeEventListener("abort",d);for(const p of s)ot(p);i.close(),r.close(p=>{p?l(p):u()})}),c),d=()=>{a()};await it(r,t),n?.addEventListener("abort",d,{once:!0});const h=r.address();if(!h||typeof h=="string")throw await a(),new Error("Web address unavailable");return{close:a,url:new URL(`http://${O}:${String(h.port)}/`)}}function ot(e){e.close(1001)}function rt(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 st(e,t,n,o){if(t.url!=="/ws"){n.destroy();return}e.handleUpgrade(t,n,o,r=>{e.emit("connection",r,t)})}function it(e,t){return new Promise((n,o)=>{const r=s=>{o(s)};e.once("error",r),e.listen(t,O,()=>{e.removeListener("error",r),n()})})}function at(e){return e instanceof Error?e.message:String(e)}function ct(e){const t=new AbortController,n=dt(e,t),o=ee();return o.addEventListener("abort",()=>{const{reason:r}=o;n(r instanceof Error?r.message:String(r))},{once:!0}),{shutdown:n,signal:t.signal}}function dt(e,t){let n=!1;return o=>{n||(n=!0,e.save({endedAt:new Date().toISOString(),endedReason:o,state:{phase:"ended"}}),f(o),t.abort())}}async function x(e,t,{label:n,signal:o}){try{const r=await e();await ce(r.readable.pipeThrough(ut()),oe(t,{flags:"a"}))}catch(r){if(o?.aborted)return;const s=r instanceof Error?r.message:String(r);f(`${n} ended with an error: ${s}`)}}function ut(){return new TransformStream({transform:(e,t)=>{t.enqueue(`${JSON.stringify(e)}
|
|
2
|
+
`)}})}async function lt(e,t,{signal:n}={}){await x(async()=>await e.debug.createLogStream({signal:n}),t,{label:"log capture",signal:n})}async function ft(e,t,{signal:n}={}){await x(async()=>await e.debug.createProxyInterceptStream({signal:n}),t,{label:"network capture",signal:n})}const k=30;async function pt(e,t){await e.pipeTo(wt(t))}function ht(e){return{decoderConfig:{codec:"avc1.640028",codedHeight:e.height,codedWidth:e.width}}}function wt(e){let t;const n=async()=>{await t?.output.finalize()};return new WritableStream({abort:n,close:n,write:async o=>{o.data.byteLength!==0&&(t??=await gt(mt(o),e),vt(t,o),await yt(t,o))}})}async function gt(e,t){const n=new de("avc"),o=new ue({format:new fe({fastStart:"fragmented"}),target:new le(t,{chunked:!1})});return o.addVideoTrack(n,{frameRate:k}),await o.start(),{frame:0,output:o,seen:{height:e.height,width:e.width},size:{height:e.height,width:e.width},source:n}}function vt(e,t){if(t.type!=="key")return;const{seen:n,size:o}=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(o.width)}x${String(o.height)}`))}function mt(e){if(e.type!=="key")throw new Error("Video stream started without a key chunk");return e}async function yt(e,t){const n=new pe(t.data,t.type,e.frame/k,1/k);e.frame+=1,await e.source.add(n,ht(e.size))}function bt(e,t,{signal:n}={}){let o,r;const s=async(a,{force:d=!1}={})=>{if(re(a)){if(!d)throw new Error(`A recording already exists at ${a}; pass --force to overwrite`);se(a)}const h=await e.video.createStream({signal:n});return o={done:St(h,a),path:a,stream:h},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{o=void 0,r=void 0}},c=async()=>{if(!o)return;const{path:a}=o;await i(o),f(`closed the video recording: ${a}`)};return n?.addEventListener("abort",()=>{c()},{once:!0}),{start:async({force:a=!1,path:d})=>{const h=o?.path??r;if(h!==void 0)throw new Error(`Already recording to ${h}`);r=d;try{return await s(d,{force:a})}finally{r=void 0}},stop:async()=>{if(!o)throw new Error("No video recording in progress");const{path:a}=o;return await i(o),{path:a}}}}async function St(e,t){try{await pt(e.readable,t)}catch(n){f(`video capture ended with an error: ${String(n)}`)}}async function Et(){const{config:e,logs:t,store:n}=Xe();f(`recording to ${n.path}`);const{shutdown:o,signal:r}=ct(n);try{const{device:s,transport:i}=await Ze(e,n,{onClose:o,signal:r}),c=bt(s,n,{signal:r});await Ye(s,e,n,{signal:r}),await tt(i,n,{signal:r});const a=await et(i,n,e.sessionId,{recorder:c,signal:r});f(`control socket listening at ${a}`),lt(s,t.device,{signal:r}),f(`recording device logs to ${t.device}`),t.network&&(ft(s,t.network,{signal:r}),f(`recording network events to ${t.network}`))}catch(s){if(r.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}}Et();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appetize/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0",
|
|
4
4
|
"description": "Run, inspect and control iOS and Android apps on Appetize cloud devices",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"homepage": "https://appetize.io",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"cli-table3": "^0.6.5",
|
|
32
32
|
"debug": "^4.4.3",
|
|
33
|
-
"human-id": "^4.2.
|
|
33
|
+
"human-id": "^4.2.2",
|
|
34
34
|
"json-rpc-2.0": "^1.8.1",
|
|
35
|
-
"mediabunny": "^1.61.
|
|
35
|
+
"mediabunny": "^1.61.3",
|
|
36
36
|
"sade": "^1.8.1",
|
|
37
37
|
"ws": "^8.22.0",
|
|
38
38
|
"yocto-spinner": "^1.2.2",
|
package/skills/appetize/SKILL.md
CHANGED
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
or answering what is currently on screen — even if Appetize is not mentioned
|
|
11
11
|
by name.
|
|
12
12
|
metadata:
|
|
13
|
-
cli-version: '0.
|
|
13
|
+
cli-version: '0.24.0'
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
# Appetize
|
|
@@ -43,6 +43,7 @@ environment.
|
|
|
43
43
|
appetize session start iphone15pro --target app_a1b2c3 --json
|
|
44
44
|
appetize session start iphone15pro --json
|
|
45
45
|
appetize session start pixel7 --target app_a1b2c3 --device-os-version 14 --proxy --json
|
|
46
|
+
appetize session start iphone15pro --device-orientation landscapeLeft --device-appearance dark --json
|
|
46
47
|
```
|
|
47
48
|
|
|
48
49
|
This blocks until the device is ready; a background daemon then holds the
|
|
@@ -78,8 +79,8 @@ appetize inspect --select-test-id login-form --json
|
|
|
78
79
|
```
|
|
79
80
|
|
|
80
81
|
`inspect` writes the view hierarchy to `inspect.json` and prints its size. Scope
|
|
81
|
-
it with a selector
|
|
82
|
-
buries what you were looking for.
|
|
82
|
+
it with a selector, confirm `matches`, then read the file: an unscoped hierarchy
|
|
83
|
+
runs past 100 KB and buries what you were looking for.
|
|
83
84
|
|
|
84
85
|
The tree holds **only what is currently on screen**. Absence means "not visible
|
|
85
86
|
right now" — scrolled away or covered — not "does not exist".
|
|
@@ -29,6 +29,13 @@ Session directories live under `~/.appetize/cli/sessions/`, or
|
|
|
29
29
|
- `--proxy http://host:port` routes through your own proxy; other schemes are
|
|
30
30
|
rejected. Interception rewrites TLS, so an app that pins certificates may
|
|
31
31
|
fail under it.
|
|
32
|
+
- `--device-orientation`, `--device-locale`, `--device-timezone`,
|
|
33
|
+
`--device-location <lat>,<lng>` (`--device-location=-43.5,172.6` when the
|
|
34
|
+
latitude is negative),
|
|
35
|
+
`--device-appearance` and `--device-font-size` set the device up. iOS takes
|
|
36
|
+
`--device-ios-keyboards` (repeatable) and `--no-device-ios-autocorrect`;
|
|
37
|
+
Android takes `--no-device-android-soft-keyboard` and
|
|
38
|
+
`--device-android-shell`, which needs a target.
|
|
32
39
|
|
|
33
40
|
## session stop
|
|
34
41
|
|
|
@@ -62,8 +69,8 @@ double-tap window.
|
|
|
62
69
|
|
|
63
70
|
## inspect
|
|
64
71
|
|
|
65
|
-
Writes `{ platform,
|
|
66
|
-
`
|
|
72
|
+
Writes `{ platform, roots }`: the screen, or one root per match (counted in
|
|
73
|
+
`matches`) in `--select-index` order. `.json` is added if you omit an extension.
|
|
67
74
|
|
|
68
75
|
## type
|
|
69
76
|
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import We from"debug";import{JSONRPCServerAndClient as Ur,JSONRPCServer as Lr,JSONRPCClient as Pr,JSONRPC as Cr}from"json-rpc-2.0";import*as m from"zod/mini";import{writeFileSync as _r,renameSync as Nr,mkdirSync as Mt,readFileSync as Dr,rmSync as Br,readdirSync as zr}from"node:fs";import{sep as Fr,join as ne}from"node:path";import{homedir as Ut}from"node:os";import{Transform as jr}from"stream";import{createRequire as $r}from"module";import{connect as Vr}from"node:net";class Kr 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:Kr;return new r(t,e)}class Wr{#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 U=Symbol.for("@appetizeio/rpc:rpcMethods");class Jr extends Wr{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}))}}const Gr="@appetizeio/rpc:playwrightAdapter";class ve{closed=!1;[U];#e;constructor(e,{signal:r}={}){this[U]=new Jr(e),this.#e=r,this.#e?.addEventListener("abort",this.#t)}close(e={}){this.closed||(this.closed=!0,this.handleClose(e))}handleClose(e){this.#e?.removeEventListener("abort",this.#t),this[U].dispatchClose(e)}#t=()=>{this.close({code:1e3,reason:String(this.#e?.reason)})}}const Ce=Symbol.for(Gr);function Hr(t){window[Ce]&&console.warn("playwright exposed more than once, overwriting previous adapter");const e={send(r){t[U].send(r)},subscribe(r){return t[U].addEventListener("message",s=>{r(s.detail)})}};return window[Ce]=e,()=>{window[Ce]===e&&delete window[Ce]}}const Lt=We("appetize.io:error:rpc");function Je(t,e){const r=new Ur(new Lr({errorListener:Lt}),Zr(t),{errorListener:Lt}),s=t[U].addEventListener("message",({detail:i})=>{r.receiveAndSend(i)}),n=t[U].addEventListener("close",()=>{s(),r.rejectAllPendingRequests("Transport closed")},{once:!0});for(const i in e){const l=e[i];l&&r.addMethod(i,l.bind(e))}return{rpc:r,unsub:()=>{s(),n()}}}function Pt(t,e){const{rpc:r,unsub:s}=Je(t,e);return{client:r.client,unsub:s}}function _e(t,e){const r=t[U].addEventListener("message",({detail:n})=>{if("method"in n)try{e({method:n.method,param:n.params})}catch{}}),s=t[U].addEventListener("close",r,{once:!0});return()=>{r(),s()}}function Zr(t){return new Pr(t[U].send.bind(t[U]),()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)+1)}function Ge(t,e,{leftEffects:r=()=>({}),notifiesLeft:s,notifiesRight:n,rightEffects:i=()=>({})}={}){const l=Je(t,{}),y=Je(e,{}),p=[()=>{l.unsub(),y.unsub()}],A=()=>{for(const _ of p)try{_()}catch{}},E=y.rpc.client,R=l.rpc.client;p.push(t[U].addEventListener("close",()=>{A()},{once:!0}),e[U].addEventListener("close",()=>{A()},{once:!0}));const M=r(E,R);for(const _ in M){const P=M[_];P&&l.rpc.addMethod(_,P)}const N=i(R,E);for(const _ in N){const P=N[_];P&&y.rpc.addMethod(_,P)}return Ct(l.rpc,y.rpc,{notifies:s}),Ct(y.rpc,l.rpc,{notifies:n}),A}function Ct(t,e,{notifies:r}={}){t.applyServerMiddleware(async(s,n)=>{const i=r?.();if(i){const{id:y,...p}=n;i[U].send(p)}if(t.hasMethod(n.method))return await s(n);if(!n.id)return e.notify(n.method,n.params),null;const l=await e.request(n.method,n.params);return{id:n.id,jsonrpc:Cr,result:l}})}function ee(t,...e){t.notify(e[0],e[1],e[2])}const Yr=(...t)=>Pt(...t),Qr=["h264","jpeg"],He=["ArrowDown","ArrowLeft","ArrowRight","ArrowUp","Backspace","Enter","Tab"],_t=["landscapeLeft","landscapeRight","portrait","upsideDown"];function Ze(){const t=new ve(async r=>{e[U].dispatchMessage(r)}),e=new ve(async r=>{t[U].dispatchMessage(r)});return t[U].addEventListener("close",()=>{e.close()},{once:!0}),e[U].addEventListener("close",()=>{t.close()},{once:!0}),[t,e]}function Nt(t,e){const r=[],s=()=>{for(const n of r)n()};r.push(t[U].addEventListener("message",n=>e[U].send(n.detail))),r.push(t[U].addEventListener("close",s,{once:!0})),r.push(e[U].addEventListener("message",n=>t[U].send(n.detail))),r.push(e[U].addEventListener("close",s,{once:!0}))}function Ye(t){return!!t&&typeof t=="object"&&"jsonrpc"in t}const ie=We("appetize.io:trace:transport:iframe");class Dt extends ve{onmessage;#e;#t;constructor(e,{origin:r="*",signal:s}={}){super(async n=>{e.postMessage(n,this.#e)},{signal:s}),this.#t=e,this.#e=r,window.addEventListener("message",this.#r)}postMessage(e,r,s){ie("postMessage(legacy)",{message:e,targetOrigin:r,transfer:s}),this.#t.postMessage(e,r??this.#e,s)}handleClose(e){ie("close()"),window.removeEventListener("message",this.#r),super.handleClose(e)}#r=async e=>{if(ie("handleMessage()",e),e.source!==this.#t||(ie("handleMessage() -> source ok"),this.#e!=="*"&&e.origin!==this.#e))return;ie("handleMessage() -> origin ok");const r=e.data;if(!Ye(r)){ie("handleMessage() -> legacy"),this.onmessage?.(e);return}ie("handleMessage() -> jsonrpc"),this[U].dispatchMessage(r)}}function Ne(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}const Xr=2*1024*1024,Bt=We("appetize:client:acked-writable-stream");class es{unacked=0;#e;#t;#r;#s;#n=[];#o;constructor(e){this.#r=e,this.#o=e.window??Xr}abort(e){this.#i(e),this.#r.onAbort?.(e)}async close(){await this.#c(()=>this.unacked===0),this.#r.onClose?.()}fail(e){this.#i(e)&&this.#r.onError?.(e)}start(e){this.#e=e}write(e){const r=e.byteLength;return this.unacked+=r,this.#r.send(e).then(()=>{this.unacked-=r,this.#s&&(this.#s.acked+=r),this.#a()},s=>{this.fail(s)}),this.unacked>=this.#o&&!this.#s&&(this.#s={acked:0,at:performance.now()},Bt("window full: %d bytes unacked, writes stalled",this.unacked)),this.#c(()=>this.unacked<this.#o)}#a(){if(this.#s&&this.unacked<=this.#o/2){const e=performance.now()-this.#s.at;Bt("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)}#i(e){if(this.#t)return!1;this.#t={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={...Ne(),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;#t=!1;#r;#s;constructor(e){this.#r=e}cancel(e){this.#n(),this.#r?.(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.#t)return;this.#s??=Ne();const{promise:r}=this.#s;this.#e.enqueue(e),await r}start(e){this.#e=e}#n(){return this.#t?!1:(this.#t=!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=[];#t;#r;#s;#n;#o;#a="starting";constructor(e,{onClose:r,receiveBuffer:s,sendWindow:n,signal:i}){if(this.#o=e,this.#t=Ne(),this.closed=this.#t.promise,this.closed.catch(os),this.#s=new ns({highWaterMark:s,onCancel:()=>{this.#i(void 0)}}),this.readable=this.#s,this.#n=new ts({onAbort:()=>{this.#i(void 0)},onClose:()=>{this.#i(void 0)},onError:l=>{this.#i(l)},send:l=>e.request("appetizeio.device.server.debug.adbTunnel.data",{data:l}),window:n}),this.writable=this.#n,i){const l=()=>{this.#i(i.reason)};i.addEventListener("abort",l,{once:!0}),this.#e.push(()=>{i.removeEventListener("abort",l)})}this.#e.push(()=>{r(this)})}close=()=>{this.#i(void 0)};detach(){this.#i(void 0,{notifyServer:!1})}async handleData(e){this.#a==="open"&&await this.#s.push(e)}handleStopped({error:e}){this.#a==="open"&&this.#i(e?new Error(e):void 0)}async start(){try{await this.#o.request("appetizeio.device.server.debug.adbTunnel.start",void 0)}catch(e){throw this.#i(e),e}if(this.#a==="closed")throw this.#r?this.#r:new Error("The adb tunnel closed before it started");this.#a="open"}async[Symbol.asyncDispose](){this.close();try{await this.closed}catch{}}#i(e,{notifyServer:r=!0}={}){if(this.#a!=="closed"){this.#a="closed",this.#r=e,r&&ee(this.#o,"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.#t.reject(e):this.#t.resolve(void 0)}}}function os(){}class as{readable;ready;#e=[];#t=!1;#r;#s;#n;constructor({label:e,onClose:r,signal:s}){this.#s=e,this.ready=new Promise((i,l)=>{this.#n={reject:l,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.#r=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.#o()};fail(e){this.#o(e)}handleItem(e){this.#r.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()}}#o(e){this.#t||(this.#r.close(),this.#a(e))}#a(e){this.#t=!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.#r}#e;#t;#r=new Set;constructor(e){this.#e=e}close(){this.#t=void 0;for(const e of[...this.#r])e.close();this.#r.clear()}add(e){this.#r.add(e)}ensureStarted(){return this.#t||(this.#t=(async()=>{try{return await this.#e.start()}catch(e){throw this.#t=void 0,e}})()),this.#t}release(e){this.#r.delete(e),!(this.#r.size>0||!this.#t)&&(this.#t=void 0,this.#e.stop())}}class zt 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;#t;#r;#s;constructor(e){this.#s=e,this.#t=new zt({label:"Log stream",start:()=>e.request("appetizeio.device.server.debug.log.start"),stop:()=>{ee(e,"appetizeio.device.server.debug.log.stop",void 0)}}),this.#r=new zt({label:"Proxy intercept stream",start:()=>e.request("appetizeio.device.server.debug.proxyIntercept.start"),stop:()=>{ee(e,"appetizeio.device.server.debug.proxyIntercept.stop",void 0)}})}close(){this.#e?.detach(),this.#t.close(),this.#r.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.#t.createStream(e)}async createProxyInterceptStream(e){return await this.#r.createStream(e)}async handleAdbTunnelData({data:e}){await this.#e?.handleData(e)}handleAdbTunnelStopped(e){this.#e?.handleStopped(e)}handleLogEntry(e){this.#t.handleNotification(e)}handleProxyInterceptEvent(e){this.#r.handleNotification(e)}}function Qe(t){return{...fs(t),index:t.index,within:t.within?Qe(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}}const ds=400,Ft=["byTestId","byText","duration","index","x","y"],hs=new Set(Ft),ps=/^-?(?:\d+(?:\.\d*)?|\.\d+)$/u,ys=/^[a-z]$/u,Xe=/^\s$/u;class D extends SyntaxError{constructor(e,r,s){super(`${e} at position ${String(r)}`,{cause:s})}}class ws{#e=0;#t;constructor(e){this.#t=e}parse(){const e=[];for(;this.#e<this.#t.length;){const r=this.#i();if(Xe.test(r))this.#e++;else if(r===">"){if(this.#t.charAt(this.#e+1)===">")throw new D('Force press ">>" is not supported',this.#e);this.#e++,e.push({kind:"down"})}else if(r==="/")this.#e++,e.push({kind:"up"});else if(r==="{")e.push(this.#o());else{const[s]=this.#t.slice(this.#e,this.#e+2);throw new D(`Unexpected ${JSON.stringify(s)}; write selectors as {...}`,this.#e)}}return e}#r(){const e=this.#e,r=this.#c(s=>s!=="="&&!et(s));if(this.#i()!=="=")return{kind:"bare",name:r,offset:e};if(r.length===0)throw new D('Expected an attribute name before "="',e);return this.#e++,{kind:"attribute",name:r,offset:e,value:this.#a()}}#s(){const e=this.#e;this.#e++;let r="";for(;;){const s=this.#i();if(this.#e++,s==='"')return r;if(s==="\\"){const n=this.#i();if(n!=='"'&&n!=="\\"&&n!=="")throw new D('Only \\" and \\\\ escapes are supported',this.#e-1);this.#e++,r+=n}else{if(s==="")throw new D("Unclosed string",e);r+=s}}}#n(){const e=this.#e;this.#e++;let r="",s=!1;for(;;){const i=this.#i();if(i==="")throw new D("Unclosed regex",e);if(this.#e++,i==="/"&&!s)break;r+=i,i==="\\"?(r+=this.#i(),this.#e++):i==="["?s=!0:i==="]"&&(s=!1)}const n=this.#c(i=>ys.test(i));try{return new RegExp(r,n)}catch(i){throw new D(`Invalid regex /${r}/${n}`,e,i)}}#o(){const e=this.#e;this.#e++;let r;const s={offsets:{}};for(let n=0;;n++){this.#l();const i=this.#i();if(i==="")throw new D('Unclosed "{"',e);if(i==="}"){if(this.#e++,n===0)throw new D('Expected a selector or wait in "{}"',e);return r===void 0?bs(s,e):{duration:r,kind:"wait"}}const l=this.#r();if(n===0&&l.name==="wait")r=l.kind==="attribute"?$t(l.value,"wait"):ds;else{if(r!==void 0)throw new D('"wait" takes no other attributes',l.offset);gs(s,l,n)}}}#a(){const e=this.#e,r=this.#i();let s;if(r==='"')s={kind:"quoted",offset:e,text:this.#s()};else if(r==="/")s={kind:"regex",offset:e,regex:this.#n()};else{const n=this.#c(i=>!et(i));if(n.length===0)throw new D('Expected a value after "="',e);return{kind:"raw",offset:e,text:n}}if(!et(this.#i()))throw new D('Expected a space or "}" after the value',this.#e);return s}#i(){return this.#t.charAt(this.#e)}#c(e){const r=this.#e;for(;this.#e<this.#t.length&&e(this.#i());)this.#e++;return this.#t.slice(r,this.#e)}#l(){this.#c(e=>Xe.test(e))}}function jt(t){return new ws(t).parse()}function gs(t,e,r){const{name:s,offset:n}=e;if(e.kind==="attribute"){tt(t,s,n,e.value);return}if(!s.includes(","))throw new D(`Expected key=value, got ${JSON.stringify(s)}`,n);if(r>0)throw new D("Write the x,y position first",n);const[i,l,...y]=s.split(",");if(y.length>0)throw new D("Expected a screen position as x,y",n);const p=n+i.length+1;tt(t,"x",n,{kind:"raw",offset:n,text:i}),tt(t,"y",p,{kind:"raw",offset:p,text:l})}function ms(t){return hs.has(t)}function et(t){return t===""||t==="}"||Xe.test(t)}function tt(t,e,r,s){if(!ms(e))throw new D(`Unknown attribute ${JSON.stringify(e)}; expected one of ${Ft.join(", ")}`,r);if(t.offsets[e]!==void 0)throw new D(`Duplicate attribute ${JSON.stringify(e)}`,r);switch(t.offsets[e]=r,e){case"byTestId":case"byText":if(t.byText!==void 0||t.byTestId!==void 0)throw new D("Only one of byText and byTestId may be given",r);e==="byText"?t.byText=vs(s):t.byTestId=Ss(s);return;default:t[e]=$t(s,e)}}function bs(t,e){return{duration:t.duration,kind:"move",to:Es(t,e)}}function $t(t,e){if(t.kind!=="raw"||!ps.test(t.text))throw new D(`"${e}" takes a number`,t.offset);return Number(t.text)}function Es(t,e){const{byTestId:r,byText:s,index:n,offsets:i,x:l,y}=t;if(s!==void 0)return{by:"text",index:n,text:s,x:l,y};if(r!==void 0)return{by:"testId",index:n,testId:r,x:l,y};if(i.index!==void 0)throw new D('"index" needs byText or byTestId',i.index);if(l===void 0||y===void 0)throw new D("A screen position needs x and y",e);return{by:"position",x:l,y}}function Ss(t){if(t.kind==="regex")throw new D('"byTestId" takes a string',t.offset);return t.text}function vs(t){return t.kind==="regex"?t.regex:t.text}function Ae(t){return t.by==="position"?{by:"position",x:t.x,y:t.y}:{...Qe(t),x:t.x,y:t.y}}function As(t){return t.map(e=>e.kind==="move"?{duration:e.duration,kind:"move",to:Ae(e.to)}:e)}function xs(t){return{press:e=>{ee(t,"appetizeio.device.server.keyboard.press",{key:e})}}}function ks({onRequest:t,parent:e,proxyInlet:r,signal:s}){const n=new Dt(e,{signal:s}),i=[()=>{n.close()}];return Nt(n,r),t&&i.push(_e(n,l=>{t(l,{source:"parent"})})),{cleanup:()=>{for(const l of i)l()},notifies:()=>e===globalThis.window?void 0:n}}function Is({onRequest:t,proxyInlet:e}){const[r,s]=Ze(),i=[Hr(s),()=>{r.close()}];return Nt(r,e),t&&i.push(_e(r,l=>{t(l,{source:"parent"})})),{cleanup:()=>{for(const l of i)l()},notifies:()=>r}}function Os(t){return"to"in t?{from:Ae(t.from),kind:"path",to:Ae(t.to)}:{direction:t.direction,distance:t.distance,from:t.from?Ae(t.from):void 0,kind:"direction"}}function Ts(t){return{down:(e,r)=>{ee(t,"appetizeio.device.server.touch.down",{x:e,y:r})},move:(e,r)=>{ee(t,"appetizeio.device.server.touch.move",{x:e,y:r})},up:(e,r)=>{ee(t,"appetizeio.device.server.touch.up",{x:e,y:r})}}}const Rs=new Set(He),Ms=/^(?<key>[A-Za-z]+)>(?<repeat>\d+)\/$/u,Us=/(\{\{|\{[^{}]*\})/u;function Ls(t){const e=[];let r="";function s(){r.length>0&&(e.push({kind:"text",text:r}),r="")}for(const[n,i]of t.split(Us).entries())if(n%2===0){if(i.includes("{"))throw new Error(`Unclosed "{" in ${JSON.stringify(t)}`);r+=i}else i==="{{"?r+="{":(s(),e.push(Cs(i.slice(1,-1))));return s(),e}function Ps(t,e){if(!Rs.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: ${He.join(", ")}. Type characters as text, and escape a literal "{" as "{{".`)}function Cs(t){const e=Ms.exec(t)?.groups,r=e?.key??t;if(Ps(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 _s=Symbol("@appetizeio/client:v1Compat");function Vt(t){return t?.[_s]}class Ns{readable;ready;get dimensions(){return this.#s}#e=[];#t=!1;#r;#s;#n;#o;constructor({onClose:e,requestKeyFrame:r,signal:s}){this.#n=r,this.ready=new Promise((i,l)=>{this.#o={reject:l,resolve:i}});let n;if(this.readable=new ReadableStream({cancel:()=>{this.#i()},start:i=>{n=i}}),!n)throw new Error("ReadableStream controller was not initialized");if(this.#r=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.#a()};fail(e){this.#a(e)}handleChunk(e){if(this.#o){if(e.type!=="key")return;const{resolve:r}=this.#o;this.#o=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.#r.enqueue(e)}requestKeyFrame=()=>{this.#t||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()}}#a(e){this.#t||(this.#r.close(),this.#i(e))}#i(e){this.#t=!0;for(const r of this.#e)r();this.#o?.reject(e??new Error("Video stream closed before the first key chunk")),this.#o=void 0}}class Ds{#e=!1;#t;#r;#s=new Set;constructor(e){this.#t=e}close(){this.#r=void 0;for(const e of[...this.#s])e.close()}async createStream({signal:e}={}){e?.throwIfAborted();const r=new Ns({onClose:s=>{this.#o(s)},requestKeyFrame:()=>{this.#a()},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.#r?(this.#a(),this.#r):(this.#e=!0,this.#r=(async()=>{try{await this.#t.request("appetizeio.device.server.video.start")}catch(e){throw this.#r=void 0,e}})(),this.#r)}#o(e){this.#s.delete(e),!(this.#s.size>0||!this.#r)&&(this.#r=void 0,ee(this.#t,"appetizeio.device.server.video.stop",void 0))}#a(){this.#e||(this.#e=!0,ee(this.#t,"appetizeio.device.server.video.requestKeyFrame",void 0))}}class Bs{debug;keyboard;touch;video;#e;#t=[];#r;constructor(e,r={}){const s=Vt(r)??{},{baseUrl:n,onClose:i,signal:l}=r;this.#e=n?zs(n):void 0;const{onRequest:y,parent:p,proxyRequest:A}=s,E=Fs(y);l?.addEventListener("abort",this.close);const[R,M]=Ze(),[N,_]=Ze();this.#t.push(()=>{R.close(),M.close(),N.close(),_.close()}),this.#t.push(Ge(_,e,{leftEffects:A}));const P=p==="playwright"?Is({onRequest:E,proxyInlet:N}):p?ks({onRequest:E,parent:p,proxyInlet:N,signal:l}):void 0;P&&this.#t.push(P.cleanup),this.#t.push(Ge(M,N,{notifiesLeft:P?.notifies}));const{client:Z,unsub:Ee}=Yr(R,{"appetizeio.device.client.debug.adbTunnel.data":V=>Y.handleAdbTunnelData(V),"appetizeio.device.client.debug.adbTunnel.stopped":async V=>{Y.handleAdbTunnelStopped(V)},"appetizeio.device.client.debug.log.entry":async V=>{Y.handleLogEntry(V)},"appetizeio.device.client.debug.proxyIntercept.event":async V=>{Y.handleProxyInterceptEvent(V)},"appetizeio.device.client.video.chunk":async V=>{k.handleChunk(V)}});this.#r=Z,this.#t.push(Ee);const Y=new ls(this.#r);this.#t.push(()=>{Y.close()});const k=new Ds(this.#r);this.#t.push(()=>{k.close()}),this.debug=Y,this.keyboard=xs(this.#r),this.touch=Ts(this.#r),this.video=k,E&&(this.#t.push(_e(M,V=>{E(V,{source:"self"})})),e instanceof Dt&&this.#t.push(_e(e,V=>{E(V,{source:"iframe"})}))),this.#t.push(()=>i?.())}async clearKeychain(){await this.#r.request("appetizeio.device.server.clearKeychain")}close=()=>{for(const e of this.#t)try{e()}catch(r){console.error("Device cleanup error:",r)}};async fold(e){await this.#r.request("appetizeio.device.server.fold",{hinge:e})}async gesture(e,r){await this.#r.request("appetizeio.device.server.gesture",{options:r,steps:As(typeof e=="string"?jt(e):e)})}async inspect(e,r){return await this.#r.request("appetizeio.device.server.inspect",{options:r,selector:e?Qe(e):void 0})}async open(e){new URL(e),await this.#r.request("appetizeio.device.server.open",{url:e})}async press(e){await this.#r.request("appetizeio.device.server.press",{action:e})}async rotate(e){await this.#r.request("appetizeio.device.server.rotate",{orientation:e})}async screenshot(){const e=await this.#r.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.#r.request("appetizeio.device.server.swipe",{gesture:Os(e),options:r})}async tap(e,r){await this.#r.request("appetizeio.device.server.tap",{options:r,selector:Ae(e)})}async type(e,r){await this.#r.request("appetizeio.device.server.type",{options:r,steps:Ls(e)})}}function zs(t){const e=new URL(t);return e.protocol==="ws:"?e.protocol="http:":e.protocol==="wss:"&&(e.protocol="https:"),e.href}const Fs=t=>{if(t)return(e,r)=>{e.method.startsWith("appetizeio.device.server.")&&t(e,r)}};let rt;try{rt=new TextDecoder}catch{}let w,q,f=0;const st=[],js=105,$s=57342,Vs=57343,Kt=57337,qt=6,pe={};let xe=11281e4,te=1681e4,oe=st,ke=0,I={},B,Ie,Oe=0,ae=0,z,G,F=[],nt=[],W,K,Te,Wt={useRecords:!1,mapsAsObjects:!0},Re=!1,Jt=2;try{new Function("")}catch{Jt=1/0}class be{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[H(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(w)return er(()=>(ct(),this?this.decode(e,r):be.prototype.decode.call(Wt,e,r)));q=r>-1?r:e.length,f=0,ke=0,ae=0,Ie=null,oe=st,z=null,w=e;try{K=e.dataView||(e.dataView=new DataView(e.buffer,e.byteOffset,e.byteLength))}catch(s){throw w=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 be){if(I=this,W=this.sharedValues&&(this.pack?new Array(this.maxPrivatePackedValues||16).concat(this.sharedValues):this.sharedValues),this.structures)return B=this.structures,De();(!B||B.length>0)&&(B=[])}else I=Wt,(!B||B.length>0)&&(B=[]),W=null;return De()}decodeMultiple(e,r){let s,n=0;try{let i=e.length;Re=!0;let l=this?this.decode(e,i):ft.decode(e,i);if(r){if(r(l)===!1)return;for(;f<i;)if(n=f,r(De())===!1)return}else{for(s=[l];f<i;)n=f,s.push(De());return s}}catch(i){throw i.lastPosition=n,i.values=s,i}finally{Re=!1,ct()}}}function De(){try{let t=O();if(z){if(f>=z.postBundlePosition){let e=new Error("Unexpected bundle position");throw e.incomplete=!0,e}f=z.postBundlePosition,z=null}if(f==q)B=null,w=null,G&&(G=null);else if(f>q){let e=new Error("Unexpected end of CBOR data");throw e.incomplete=!0,e}else if(!Re)throw new Error("Data read, but end of buffer not reached");return t}catch(t){throw ct(),(t instanceof RangeError||t.message.startsWith("Unexpected end of buffer"))&&(t.incomplete=!0),t}}function ce(){let t=new Error("Unexpected end of CBOR data");return t.incomplete=!0,t}function O(){if(!(f<q))throw ce();let t=w[f++],e=t>>5;if(t=t&31,t>23)switch(t){case 24:if(f>=q)throw ce();t=w[f++];break;case 25:if(e==7)return Js();t=K.getUint16(f),f+=2;break;case 26:if(e==7){let r=K.getFloat32(f);if(I.useFloat32>2){let s=lt[(w[f]&127)<<1|w[f+1]>>7];return f+=4,(s*r+(r>0?.5:-.5)>>0)/s}return f+=4,r}if(t=K.getUint32(f),f+=4,e===1)return-1-t;break;case 27:if(e==7){let r=K.getFloat64(f);return f+=8,r}if(e>1){if(K.getUint32(f)>0)throw new Error("JavaScript does not support arrays, maps, or strings with length over 4294967295");t=K.getUint32(f+4)}else I.int64AsNumber?(t=K.getUint32(f)*4294967296,t+=K.getUint32(f+4)):t=K.getBigUint64(f);f+=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=O())!=pe;){if(n>=xe)throw new Error(`Array length exceeds ${xe}`);r[n++]=s}return e==4?r:e==3?r.join(""):Buffer.concat(r);case 5:let i;if(I.mapsAsObjects){let l={},y=0;if(I.keyMap)for(;(i=O())!=pe;){if(y++>=te)throw new Error(`Property count exceeds ${te}`);l[H(I.decodeKey(i))]=O()}else for(;(i=O())!=pe;){if(y++>=te)throw new Error(`Property count exceeds ${te}`);l[H(i)]=O()}return l}else{Te&&(I.mapsAsObjects=!0,Te=!1);let l=new Map;if(I.keyMap){let y=0;for(;(i=O())!=pe;){if(y++>=te)throw new Error(`Map size exceeds ${te}`);l.set(I.decodeKey(i),O())}}else{let y=0;for(;(i=O())!=pe;){if(y++>=te)throw new Error(`Map size exceeds ${te}`);l.set(i,O())}}return l}case 7:return pe;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 Ws(t);case 3:if(ae>=f)return Ie.slice(f-Oe,(f+=t)-Oe);if(ae==0&&q<140&&t<32){let n=t<16?Zt(t):qs(t);if(n!=null)return n}return Ht(t);case 4:if(t>=xe)throw new Error(`Array length exceeds ${xe}`);if(t>q-f)throw ce();let r=new Array(t);for(let n=0;n<t;n++)r[n]=O();return r;case 5:if(t>=te)throw new Error(`Map size exceeds ${xe}`);if(t>(q-f)/2)throw ce();if(I.mapsAsObjects){let n={};if(I.keyMap)for(let i=0;i<t;i++)n[H(I.decodeKey(O()))]=O();else for(let i=0;i<t;i++)n[H(O())]=O();return n}else{Te&&(I.mapsAsObjects=!0,Te=!1);let n=new Map;if(I.keyMap)for(let i=0;i<t;i++)n.set(I.decodeKey(O()),O());else for(let i=0;i<t;i++)n.set(O(),O());return n}case 6:if(t>=Kt){let n=B[t&8191];if(n)return n.read||(n.read=it(n)),n.read();if(t<65536){if(t==Vs){let i=we(),l=O(),y=O();ot(l,y);let p={};if(I.keyMap)for(let A=2;A<i;A++){let E=I.decodeKey(y[A-2]);p[H(E)]=O()}else for(let A=2;A<i;A++){let E=y[A-2];p[H(E)]=O()}return p}else if(t==$s){let i=we(),l=O();for(let y=2;y<i;y++)ot(l++,O());return O()}else if(t==Kt)return Xs();if(I.getShared&&(at(),n=B[t&8191],n))return n.read||(n.read=it(n)),n.read()}}let s=F[t];if(s)return s.handlesRead?s(O):s(O());{let n=O();for(let i=0;i<nt.length;i++){let l=nt[i](t,n);if(l!==void 0)return l}return new le(n,t)}case 7:switch(t){case 20:return!1;case 21:return!0;case 22:return null;case 23:return;default:let n=(W||fe())[t];if(n!==void 0)return n;throw new Error("Unknown token "+t)}default:throw isNaN(t)?ce():new Error("Unknown CBOR token "+t)}}const Gt=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;function it(t){if(!t)throw new Error("Structure is required in record definition");function e(){let r=w[f++];if(r=r&31,r>23)switch(r){case 24:r=w[f++];break;case 25:r=K.getUint16(f),f+=2;break;case 26:r=K.getUint32(f),f+=4;break;default:throw new Error("Expected array header, but got "+w[f-1])}let s=this.compiledReader;for(;s;){if(s.propertyCount===r)return s(O);s=s.next}if(this.slowReads++>=Jt){let i=this.length==r?this:this.slice(0,r);return s=I.keyMap?new Function("r","return {"+i.map(l=>I.decodeKey(l)).map(l=>Gt.test(l)?H(l)+":r()":"["+JSON.stringify(l)+"]:r()").join(",")+"}"):new Function("r","return {"+i.map(l=>Gt.test(l)?H(l)+":r()":"["+JSON.stringify(l)+"]:r()").join(",")+"}"),this.compiledReader&&(s.next=this.compiledReader),s.propertyCount=r,this.compiledReader=s,s(O)}let n={};if(I.keyMap)for(let i=0;i<r;i++)n[H(I.decodeKey(this[i]))]=O();else for(let i=0;i<r;i++)n[H(this[i])]=O();return n}return t.slowReads=0,e}function H(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 Ht=Be;function Ks(t){Ht=e();function e(r){return function(n){let i=oe[ke++];if(i==null){if(z)return Be(n);let y=t(f,q,n,w);if(typeof y=="string")i=y,oe=st;else if(oe=y,ke=1,ae=1,i=oe[0],i===void 0)throw new Error("Unexpected end of buffer")}let l=i.length;return l<=n?(f+=n,i):(Ie=i,Oe=f,ae=f+l,f+=n,i.slice(0,n))}}}function Be(t){let e;if(t<16&&(e=Zt(t)))return e;if(t>64&&rt)return rt.decode(w.subarray(f,f+=t));const r=f+t,s=[];for(e="";f<r;){const n=w[f++];if((n&128)===0)s.push(n);else if((n&224)===192)if(n<194||f>=r||(w[f]&192)!==128)s.push(65533);else{const i=w[f++]&63;s.push((n&31)<<6|i)}else if((n&240)===224){const i=f<r?w[f]:0;if(f>=r||(i&192)!==128||n===224&&i<160||n===237&&i>=160)s.push(65533);else if(f++,f>=r||(w[f]&192)!==128)s.push(65533);else{const l=w[f++]&63;s.push((n&31)<<12|(i&63)<<6|l)}}else if((n&248)===240){const i=f<r?w[f]:0;if(n>244||f>=r||(i&192)!==128||n===240&&i<144||n===244&&i>=144)s.push(65533);else if(f++,f>=r||(w[f]&192)!==128)s.push(65533);else{const l=w[f++]&63;if(f>=r||(w[f]&192)!==128)s.push(65533);else{const y=w[f++]&63;let p=(n&7)<<18|(i&63)<<12|l<<6|y;p-=65536,s.push(p>>>10&1023|55296),s.push(56320|p&1023)}}}else s.push(65533);s.length>=4096&&(e+=$.apply(String,s),s.length=0)}return s.length>0&&(e+=$.apply(String,s)),e}let $=String.fromCharCode;function qs(t){let e=f,r=new Array(t);for(let s=0;s<t;s++){const n=w[f++];if((n&128)>0){f=e;return}r[s]=n}return $.apply(String,r)}function Zt(t){if(t<4)if(t<2){if(t===0)return"";{let e=w[f++];if((e&128)>1){f-=1;return}return $(e)}}else{let e=w[f++],r=w[f++];if((e&128)>0||(r&128)>0){f-=2;return}if(t<3)return $(e,r);let s=w[f++];if((s&128)>0){f-=3;return}return $(e,r,s)}else{let e=w[f++],r=w[f++],s=w[f++],n=w[f++];if((e&128)>0||(r&128)>0||(s&128)>0||(n&128)>0){f-=4;return}if(t<6){if(t===4)return $(e,r,s,n);{let i=w[f++];if((i&128)>0){f-=5;return}return $(e,r,s,n,i)}}else if(t<8){let i=w[f++],l=w[f++];if((i&128)>0||(l&128)>0){f-=6;return}if(t<7)return $(e,r,s,n,i,l);let y=w[f++];if((y&128)>0){f-=7;return}return $(e,r,s,n,i,l,y)}else{let i=w[f++],l=w[f++],y=w[f++],p=w[f++];if((i&128)>0||(l&128)>0||(y&128)>0||(p&128)>0){f-=8;return}if(t<10){if(t===8)return $(e,r,s,n,i,l,y,p);{let A=w[f++];if((A&128)>0){f-=9;return}return $(e,r,s,n,i,l,y,p,A)}}else if(t<12){let A=w[f++],E=w[f++];if((A&128)>0||(E&128)>0){f-=10;return}if(t<11)return $(e,r,s,n,i,l,y,p,A,E);let R=w[f++];if((R&128)>0){f-=11;return}return $(e,r,s,n,i,l,y,p,A,E,R)}else{let A=w[f++],E=w[f++],R=w[f++],M=w[f++];if((A&128)>0||(E&128)>0||(R&128)>0||(M&128)>0){f-=12;return}if(t<14){if(t===12)return $(e,r,s,n,i,l,y,p,A,E,R,M);{let N=w[f++];if((N&128)>0){f-=13;return}return $(e,r,s,n,i,l,y,p,A,E,R,M,N)}}else{let N=w[f++],_=w[f++];if((N&128)>0||(_&128)>0){f-=14;return}if(t<15)return $(e,r,s,n,i,l,y,p,A,E,R,M,N,_);let P=w[f++];if((P&128)>0){f-=15;return}return $(e,r,s,n,i,l,y,p,A,E,R,M,N,_,P)}}}}}function Ws(t){return I.copyBuffers?Uint8Array.prototype.slice.call(w,f,f+=t):w.subarray(f,f+=t)}let Yt=new Float32Array(1),ze=new Uint8Array(Yt.buffer,0,4);function Js(){let t=w[f++],e=w[f++],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 ze[3]=t&128|(r>>1)+56,ze[2]=(t&7)<<5|e>>3,ze[1]=e<<5,ze[0]=0,Yt[0]}new Array(4096);class le{constructor(e,r){this.value=e,this.tag=r}}F[0]=t=>new Date(t),F[1]=t=>new Date(Math.round(t*1e3)),F[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},F[3]=t=>BigInt(-1)-F[2](t),F[4]=t=>+(t[1]+"e"+t[0]),F[5]=t=>t[1]*Math.exp(t[0]*Math.log(2));const ot=(t,e)=>{t=t-57344;let r=B[t];r&&r.isShared&&((B.restoreStructures||(B.restoreStructures=[]))[t]=r),B[t]=e,e.read=it(e)};F[js]=t=>{let e=t.length,r=t[1];ot(t[0],r);let s={};for(let n=2;n<e;n++){let i=r[n-2];s[H(i)]=t[n]}return s},F[14]=t=>z?z[0].slice(z.position0,z.position0+=t):new le(t,14),F[15]=t=>z?z[1].slice(z.position1,z.position1+=t):new le(t,15);let Gs={Error,RegExp};F[27]=t=>(Gs[t[0]]||Error)(t[1],t[2]);const Qt=t=>{if(w[f++]!=132){let r=new Error("Packed values structure must be followed by a 4 element array");throw w.length<f&&(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 W=W?e.concat(W.slice(e.length)):e,W.prefixes=t(),W.suffixes=t(),t()};Qt.handlesRead=!0,F[51]=Qt,F[qt]=t=>{if(!W)if(I.getShared)at();else return new le(t,qt);if(typeof t=="number")return W[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},F[28]=t=>{G||(G=new Map,G.id=0);let e=G.id++,r=f,s=w[f],n;s>>5==4?n=[]:n={};let i={target:n};G.set(e,i);let l=t();return i.used?(Object.getPrototypeOf(n)!==Object.getPrototypeOf(l)&&(f=r,n=l,G.set(e,{target:n}),l=t()),Object.assign(n,l)):(i.target=l,l)},F[28].handlesRead=!0,F[29]=t=>{let e=G.get(t);return e.used=!0,e.target},F[258]=t=>new Set(t),(F[259]=t=>(I.mapsAsObjects&&(I.mapsAsObjects=!1,Te=!0),t())).handlesRead=!0;function ye(t,e){return typeof t=="string"?t+e:t instanceof Array?t.concat(e):Object.assign({},t,e)}function fe(){if(!W)if(I.getShared)at();else throw new Error("No packed values available");return W}const Hs=1399353956;nt.push((t,e)=>{if(t>=225&&t<=255)return ye(fe().prefixes[t-224],e);if(t>=28704&&t<=32767)return ye(fe().prefixes[t-28672],e);if(t>=1879052288&&t<=2147483647)return ye(fe().prefixes[t-1879048192],e);if(t>=216&&t<=223)return ye(e,fe().suffixes[t-216]);if(t>=27647&&t<=28671)return ye(e,fe().suffixes[t-27639]);if(t>=1811940352&&t<=1879048191)return ye(e,fe().suffixes[t-1811939328]);if(t==Hs)return{packedValues:W,structures:B.slice(0),version:e};if(t==55799)return e});const Zs=new Uint8Array(new Uint16Array([1]).buffer)[0]==1,Xt=[Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?{name:"BigUint64Array"}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?{name:"BigInt64Array"}:BigInt64Array,Float32Array,Float64Array],Ys=[64,68,69,70,71,72,77,78,79,85,86];for(let t=0;t<Xt.length;t++)Qs(Xt[t],Ys[t]);function Qs(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;F[n?e:e-4]=s==1||n==Zs?l=>{if(!t)throw new Error("Could not find typed array for code "+e);return!I.copyBuffers&&(s===1||s===2&&!(l.byteOffset&1)||s===4&&!(l.byteOffset&3)||s===8&&!(l.byteOffset&7))?new t(l.buffer,l.byteOffset,l.byteLength>>i):new t(Uint8Array.prototype.slice.call(l,0).buffer)}:l=>{if(!t)throw new Error("Could not find typed array for code "+e);let y=new DataView(l.buffer,l.byteOffset,l.byteLength),p=l.length>>i,A=new t(p),E=y[r];for(let R=0;R<p;R++)A[R]=E.call(y,R<<i,n);return A}}}function Xs(){let t=we(),e=f+O();for(let s=2;s<t;s++){let n=we();f+=n}let r=f;return f=e,z=[Be(we()),Be(we())],z.position0=0,z.position1=0,z.postBundlePosition=f,f=r,O()}function we(){if(!(f<q))throw ce();let t=w[f++]&31;if(t>23)switch(t){case 24:if(f>=q)throw ce();t=w[f++];break;case 25:t=K.getUint16(f),f+=2;break;case 26:t=K.getUint32(f),f+=4;break}return t}function at(){if(I.getShared){let t=er(()=>(w=null,I.getShared()))||{},e=t.structures||[];I.sharedVersion=t.version,W=I.sharedValues=t.packedValues,B===!0?I.structures=B=e:B.splice.apply(B,[0,e.length].concat(e))}}function er(t){let e=q,r=f,s=ke,n=Oe,i=ae,l=Ie,y=oe,p=G,A=z,E=new Uint8Array(w.slice(0,q)),R=B,M=I,N=Re,_=t();return q=e,f=r,ke=s,Oe=n,ae=i,Ie=l,oe=y,G=p,z=A,w=E,Re=N,B=R,I=M,K=new DataView(w.buffer,w.byteOffset,w.byteLength),_}function ct(){w=null,G=null,B=null}const lt=new Array(147);for(let t=0;t<256;t++)lt[t]=+("1e"+Math.floor(45.15-t*.30103));let ft=new be({useRecords:!1});const en=ft.decode;ft.decodeMultiple;let Fe;try{Fe=new TextEncoder}catch{}let ut,tr;const je=typeof globalThis=="object"&&globalThis.Buffer,Me=typeof je<"u",dt=Me?je.allocUnsafeSlow:Uint8Array,rr=Me?je:Uint8Array,sr=256,nr=Me?4294967296:2144337920;let ht,c,L,o=0,re,j=null;const tn=61440,rn=/[\u0080-\uFFFF]/,J=Symbol("record-id");class ir extends be{constructor(e){super(e),this.offset=0;let r,s,n,i,l;e=e||{};let y=rr.prototype.utf8Write?function(a,g){return c.utf8Write(a,g,c.byteLength-g)}:Fe&&Fe.encodeInto?function(a,g){return Fe.encodeInto(a,c.subarray(g)).written}:!1,p=this,A=e.structures||e.saveStructures,E=e.maxSharedStructures;if(E==null&&(E=A?128:0),E>8190)throw new Error("Maximum maxSharedStructure is 8190");let R=e.sequential;R&&(E=0),this.structures||(this.structures=[]),this.saveStructures&&(this.saveShared=this.saveStructures);let M,N,_=e.sharedValues,P;if(_){P=Object.create(null);for(let a=0,g=_.length;a<g;a++)P[_[a]]=a}let Z=[],Ee=0,Y=0;this.mapEncode=function(a,g){return this._keyMap&&!this._mapped&&a.constructor.name==="Array"&&(a=a.map(d=>this.encodeKeys(d))),this.encode(a,g)},this.encode=function(a,g){if(c||(c=new dt(8192),L=new DataView(c.buffer,0,8192),o=0),re=c.length-10,re-o<2048?(c=new dt(c.length),L=new DataView(c.buffer,0,c.length),re=c.length-10,o=0):g===lr&&(o=o+7&2147483640),r=o,p.useSelfDescribedHeader&&(L.setUint32(o,3654940416),o+=3),l=p.structuredClone?new Map:null,p.bundleStrings&&typeof a!="string"?(j=[],j.size=1/0):j=null,s=p.structures,s){if(s.uninitialized){let h=p.getShared()||{};p.structures=s=h.structures||[],p.sharedVersion=h.version;let u=p.sharedValues=h.packedValues;if(u){P={};for(let b=0,S=u.length;b<S;b++)P[u[b]]=b}}let d=s.length;if(d>E&&!R&&(d=E),!s.transitions){s.transitions=Object.create(null);for(let h=0;h<d;h++){let u=s[h];if(!u)continue;let b,S=s.transitions;for(let v=0,x=u.length;v<x;v++){S[J]===void 0&&(S[J]=h);let T=u[v];b=S[T],b||(b=S[T]=Object.create(null)),S=b}S[J]=h|1048576}}R||(s.nextId=d)}if(n&&(n=!1),i=s||[],N=P,e.pack){let d=new Map;if(d.values=[],d.encoder=p,d.maxValues=e.maxPrivatePackedValues||(P?16:1/0),d.objectMap=P||!1,d.samplingPackedValues=M,$e(a,d),d.values.length>0){c[o++]=216,c[o++]=51,Q(4);let h=d.values;k(h),Q(0),Q(0),N=Object.create(P||null);for(let u=0,b=h.length;u<b;u++)N[h[u]]=u}}ht=g>try{if(ht)return;if(k(a),j&&cr(r,k),p.offset=o,l&&l.idsToInsert){o+=l.idsToInsert.length*2,o>re&&de(o),p.offset=o;let d=on(c.subarray(r,o),l.idsToInsert);return l=null,d}return g&lr?(c.start=r,c.end=o,c):c.subarray(r,o)}finally{if(s){if(Y<10&&Y++,s.length>E&&(s.length=E),Ee>1e4)s.transitions=null,Y=0,Ee=0,Z.length>0&&(Z=[]);else if(Z.length>0&&!R){for(let d=0,h=Z.length;d<h;d++)Z[d][J]=void 0;Z=[]}}if(n&&p.saveShared){p.structures.length>E&&(p.structures=p.structures.slice(0,E));let d=c.subarray(r,o);return p.updateSharedData()===!1?p.encode(a):d}g&cn&&(o=r)}},this.findCommonStringsToPack=()=>(M=new Map,P||(P=Object.create(null)),a=>{let g=a&&a.threshold||4,d=this.pack?a.maxPrivatePackedValues||16:0;_||(_=this.sharedValues=[]);for(let[h,u]of M)u.count>g&&(P[h]=d++,_.push(h),n=!0);for(;this.saveShared&&this.updateSharedData()===!1;);M=null});const k=a=>{o>re&&(c=de(o));var g=typeof a,d;if(g==="string"){if(N){let S=N[a];if(S>=0){S<16?c[o++]=S+224:(c[o++]=198,S&1?k(15-S>>1):k(S-16>>1));return}else if(M&&!e.pack){let v=M.get(a);v?v.count++:M.set(a,{count:1})}}let h=a.length;if(j&&h>=4&&h<1024){if((j.size+=h)>tn){let v,x=(j[0]?j[0].length*3+j[1].length:0)+10;o+x>re&&(c=de(o+x)),c[o++]=217,c[o++]=223,c[o++]=249,c[o++]=j.position?132:130,c[o++]=26,v=o-r,o+=4,j.position&&cr(r,k),j=["",""],j.size=0,j.position=v}let S=rn.test(a);j[S?0:1]+=a,c[o++]=S?206:207,k(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>re&&(c=de(o+b)),h<64||!y){let S,v,x,T=o+u;for(S=0;S<h;S++)v=a.charCodeAt(S),v<128?c[T++]=v:v<2048?(c[T++]=v>>6|192,c[T++]=v&63|128):(v&64512)===55296&&((x=a.charCodeAt(S+1))&64512)===56320?(v=65536+((v&1023)<<10)+(x&1023),S++,c[T++]=v>>18|240,c[T++]=v>>12&63|128,c[T++]=v>>6&63|128,c[T++]=v&63|128):(c[T++]=v>>12|224,c[T++]=v>>6&63|128,c[T++]=v&63|128);d=T-o-u}else d=y(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,L.setUint32(o,d),o+=4),o+=d}else if(g==="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,L.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,L.setUint16(o,~a),o+=2):(c[o++]=58,L.setUint32(o,~a),o+=4);else if(!this.alwaysUseFloat&&a<0&&a>=-4294967296&&Math.floor(a)===a)c[o++]=58,L.setUint32(o,-1-a),o+=4;else{let h;if((h=this.useFloat32)>0&&a<4294967296&&a>=-2147483648){c[o++]=250,L.setFloat32(o,a);let u;if(h<4||(u=a*lt[(c[o]&127)<<1|c[o+1]>>7])>>0===u){o+=4;return}else o--}c[o++]=251,L.setFloat64(o,a),o+=8}else if(g==="object")if(!a)c[o++]=246;else{if(l){let u=l.get(a);if(u){if(c[o++]=216,c[o++]=29,c[o++]=25,!u.references){let b=l.idsToInsert||(l.idsToInsert=[]);u.references=[],b.push(u)}u.references.push(o-r),o+=2;return}else l.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]])])),V(a);else if(h===Array){d=a.length,d<24?c[o++]=128|d:Q(d);for(let u=0;u<d;u++)k(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,L.setUint32(o,d),o+=4),p.keyMap)for(let[u,b]of a)k(p.encodeKey(u)),k(b);else for(let[u,b]of a)k(u),k(b);else{for(let u=0,b=ut.length;u<b;u++){let S=tr[u];if(a instanceof S){let v=ut[u],x=v.tag;x==null&&(x=v.getTag&&v.getTag.call(this,a)),x<24?c[o++]=192|x:x<256?(c[o++]=216,c[o++]=x):x<65536?(c[o++]=217,c[o++]=x>>8,c[o++]=x&255):x>-1&&(c[o++]=218,L.setUint32(o,x),o+=4),v.encode.call(this,a,k,de);return}}if(a[Symbol.iterator]){if(ht){let u=new Error("Iterable should be serialized as iterator");throw u.iteratorNotHandled=!0,u}c[o++]=159;for(let u of a)k(u);c[o++]=255;return}if(a[Symbol.asyncIterator]||pt(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 k(u)}V(a)}}else if(g==="boolean")c[o++]=a?245:244;else if(g==="bigint"){if(a<BigInt(1)<<BigInt(64)&&a>=0)c[o++]=27,L.setBigUint64(o,a);else if(a>-(BigInt(1)<<BigInt(64))&&a<0)c[o++]=59,L.setBigUint64(o,-a-BigInt(1));else if(this.largeBigIntToFloat)c[o++]=251,L.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);yt(new Uint8Array(h.reverse()),de);return}o+=8}else if(g==="undefined")c[o++]=247;else throw new Error("Unknown type: "+g)},V=this.useRecords===!1?this.variableMapSize?a=>{let g=Object.keys(a),d=Object.values(a),h=g.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,L.setUint32(o,h),o+=4),p.keyMap)for(let u=0;u<h;u++)k(p.encodeKey(g[u])),k(d[u]);else for(let u=0;u<h;u++)k(g[u]),k(d[u])}:a=>{c[o++]=185;let g=o-r;o+=2;let d=0;if(p.keyMap)for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(k(p.encodeKey(h)),k(a[h]),d++);else for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(k(h),k(a[h]),d++);c[g+++r]=d>>8,c[g+r]=d&255}:(a,g)=>{let d,h=i.transitions||(i.transitions=Object.create(null)),u=0,b=0,S,v;if(this.keyMap){v=Object.keys(a).map(T=>this.encodeKey(T)),b=v.length;for(let T=0;T<b;T++){let Rt=v[T];d=h[Rt],d||(d=h[Rt]=Object.create(null),u++),h=d}}else for(let T in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(T))&&(d=h[T],d||(h[J]&1048576&&(S=h[J]&65535),d=h[T]=Object.create(null),u++),h=d,b++);let x=h[J];if(x!==void 0)x&=65535,c[o++]=217,c[o++]=x>>8|224,c[o++]=x&255;else if(v||(v=h.__keys__||(h.__keys__=Object.keys(a))),S===void 0?(x=i.nextId++,x||(x=0,i.nextId=1),x>=sr&&(i.nextId=(x=E)+1)):x=S,i[x]=v,x<E){c[o++]=217,c[o++]=x>>8|224,c[o++]=x&255,h=i.transitions;for(let T=0;T<b;T++)(h[J]===void 0||h[J]&1048576)&&(h[J]=x),h=h[v[T]];h[J]=x|1048576,n=!0}else{if(h[J]=x,L.setUint32(o,3655335680),o+=3,u&&(Ee+=Y*u),Z.length>=sr-E&&(Z.shift()[J]=void 0),Z.push(h),Q(b+2),k(57344+x),k(v),g)return;for(let T in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(T))&&k(a[T]);return}if(b<24?c[o++]=128|b:Q(b),!g)for(let T in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(T))&&k(a[T])},de=a=>{let g;if(a>16777216){if(a-r>nr)throw new Error("Encoded buffer would be larger than maximum buffer size");g=Math.min(nr,Math.round(Math.max((a-r)*(a>67108864?1.25:2),4194304)/4096)*4096)}else g=(Math.max(a-r<<2,c.length-1)>>12)+1<<12;let d=new dt(g);return L=new DataView(d.buffer,0,g),c.copy?c.copy(d,0,r,a):d.set(c.slice(r,a)),o-=r,r=0,re=d.length-10,c=d};let se=100,It=1e3;this.encodeAsIterable=function(a,g){return Ot(a,g,he)},this.encodeAsAsyncIterable=function(a,g){return Ot(a,g,Tt)};function*he(a,g,d){let h=a.constructor;if(h===Object){let u=p.useRecords!==!1;u?V(a,!0):or(Object.keys(a).length,160);for(let b in a){let S=a[b];u||k(b),S&&typeof S=="object"?g[b]?yield*he(S,g[b]):yield*qe(S,g,b):k(S)}}else if(h===Array){let u=a.length;Q(u);for(let b=0;b<u;b++){let S=a[b];S&&(typeof S=="object"||o-r>se)?g.element?yield*he(S,g.element):yield*qe(S,g,"element"):k(S)}}else if(a[Symbol.iterator]&&!a.buffer){c[o++]=159;for(let u of a)u&&(typeof u=="object"||o-r>se)?g.element?yield*he(u,g.element):yield*qe(u,g,"element"):k(u);c[o++]=255}else pt(a)?(or(a.size,64),yield c.subarray(r,o),yield a,Se()):a[Symbol.asyncIterator]?(c[o++]=159,yield c.subarray(r,o),yield a,Se(),c[o++]=255):k(a);d&&o>r?yield c.subarray(r,o):o-r>se&&(yield c.subarray(r,o),Se())}function*qe(a,g,d){let h=o-r;try{k(a),o-r>se&&(yield c.subarray(r,o),Se())}catch(u){if(u.iteratorNotHandled)g[d]={},o=r+h,yield*he.call(this,a,g[d]);else throw u}}function Se(){se=It,p.encode(null,gt)}function Ot(a,g,d){return g&&g.chunkThreshold?se=It=g.chunkThreshold:se=100,a&&typeof a=="object"?(p.encode(null,gt),d(a,p.iterateProperties||(p.iterateProperties={}),!0)):[p.encode(a)]}async function*Tt(a,g){for(let d of he(a,g,!0)){let h=d.constructor;if(h===rr||h===Uint8Array)yield d;else if(pt(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)Se(),u?yield*Tt(u,g.async||(g.async={})):yield p.encode(u);else yield d}}}useBuffer(e){c=e,L=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 ar(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,l)=>this.structures[l]=i),n}}function or(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,L.setUint32(o,t),o+=4)}class ar{constructor(e,r,s){this.structures=e,this.packedValues=r,this.version=s}}function Q(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,L.setUint32(o,t),o+=4)}const sn=typeof Blob>"u"?function(){}:Blob;function pt(t){if(t instanceof sn)return!0;let e=t[Symbol.toStringTag];return e==="Blob"||e==="File"}function $e(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++)$e(t[s],e);else{let s=!e.encoder.useRecords;for(var r in t)t.hasOwnProperty(r)&&(s&&$e(r,e),$e(t[r],e))}break;case"function":console.log(t)}}const nn=new Uint8Array(new Uint16Array([1]).buffer)[0]==1;tr=[Date,Set,Error,RegExp,le,ArrayBuffer,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?function(){}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?function(){}:BigInt64Array,Float32Array,Float64Array,ar],ut=[{tag:1,encode(t,e){let r=t.getTime()/1e3;(this.useTimestamp32||t.getMilliseconds()===0)&&r>=0&&r<4294967296?(c[o++]=26,L.setUint32(o,r),o+=4):(c[o++]=251,L.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){yt(t,r)}},{getTag(t){if(t.constructor===Uint8Array&&(this.tagUint8Array||Me&&this.tagUint8Array!==!1))return 64},encode(t,e,r){yt(t,r)}},X(68,1),X(69,2),X(70,4),X(71,8),X(72,1),X(77,2),X(78,4),X(79,8),X(85,4),X(86,8),{encode(t,e){let r=t.packedValues||[],s=t.structures||[];if(r.values.length>0){c[o++]=216,c[o++]=51,Q(4);let n=r.values;e(n),Q(0),Q(0),packedObjectMap=Object.create(sharedPackedObjectMap||null);for(let i=0,l=n.length;i<l;i++)packedObjectMap[n[i]]=i}if(s){L.setUint32(o,3655335424),o+=3;let n=s.slice(0);n.unshift(57344),n.push(new le(t.version,1399353956)),e(n)}else e(new le(t.version,1399353956))}}];function X(t,e){return!nn&&e>1&&(t-=4),{tag:t,encode:function(s,n){let i=s.byteLength,l=s.byteOffset||0,y=s.buffer||s;n(Me?je.from(y,l,i):new Uint8Array(y,l,i))}}}function yt(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,L.setUint32(o,r),o+=4),o+r>=c.length&&e(o+r),c.set(t.buffer?t:new Uint8Array(t),o),o+=r}function on(t,e){let r,s=e.length*2,n=t.length-s;e.sort((i,l)=>i.offset>l.offset?1:-1);for(let i=0;i<e.length;i++){let l=e[i];l.id=i;for(let y of l.references)t[y++]=i>>8,t[y]=i&255}for(;r=e.pop();){let i=r.offset;t.copyWithin(i+s,i,n),s-=2;let l=i+s;t[l++]=216,t[l++]=28,n=i}return t}function cr(t,e){L.setUint32(j.position+t,o-j.position-t+1);let r=j;j=null,e(r[0]),e(r[1])}let wt=new ir({useRecords:!1});const an=wt.encode;wt.encodeAsIterable,wt.encodeAsAsyncIterable;const lr=512,cn=1024,gt=2048;class ln extends jr{constructor(e){e||(e={}),e.objectMode=!0,super(e),e.structures=[],this.decoder=e.decoder||new be(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 fn=process.env.CBOR_NATIVE_ACCELERATION_DISABLED!==void 0&&process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase()==="true";if(!fn){let t;try{typeof require=="function"?t=require("cbor-extract"):t=$r(import.meta.url)("cbor-extract"),t&&Ks(t.extractStrings)}catch{}}const fr=["light","dark"],ur=["xs","s","m","l","xl","xxl","xxxl"];function un(t){return{adbShellCommand:t.android?.shell,appearance:t.appearance,deviceType:t.id,disableVirtualKeyboard:t.android?.softKeyboard===!1?!0:void 0,fontSize:t.fontSize,iosAutocorrect:t.ios?.autocorrect,iosKeyboard:t.ios?.keyboards,language:t.locale,locale:t.locale,location:t.location&&[t.location.latitude,t.location.longitude],orientation:t.orientation,osVersion:t.osVersion,timezone:t.timezone}}const dn=["grant"];function hn(t){return{grantPermissions:t?.permissions==="grant"?!0:void 0,launchApp:t?.app,launchArgs:t?.ios?.args,launchUrl:t?.url,params:t?.params}}class dr extends Error{code;issues;constructor(e,r,s=[]){super(r),this.name="SessionRequestError",this.code=e,this.issues=s}}const C=t=>`expected ${t}`,ge=t=>m.string(C(t)).check(m.minLength(1,C(t))),Ue=t=>m.literal(t,C(`one of ${t.join(", ")}`)),me=t=>m.object(t,C("an object")),hr=()=>m.boolean(C("true or false")),pr=t=>m.number(C("a number")).check(m.gte(-t,C(`at least -${String(t)}`)),m.lte(t,C(`at most ${String(t)}`))),pn=/^\w{2}(?:_\w{2})?@sw(=[\w-]+)?$/,yn=/^[a-z0-9_.=:/ ()"-]+$/i,wn='letters, digits or _ . = : / ( ) " - and spaces',gn=/^[A-Za-z]{2,3}([-_][A-Za-z0-9]{2,8})*$/;function mn(t){if(!gn.test(t))return!1;try{return new Intl.Locale(t.replaceAll("_","-")),!0}catch{return!1}}function bn(t){try{return new Intl.DateTimeFormat(void 0,{timeZone:t}),!0}catch{return!1}}const mt={code:"needsTarget"},yr={code:"needsLaunchApp"},bt="has no effect without a target",wr="has no effect without a target, or with launch.app: false";function gr({launch:t}){return t?.app!==!1}const En=m.object({apiToken:m.optional(ge("an API token")),codec:m.optional(Ue(Qr)),device:me({android:m.optional(me({shell:m.optional(ge("a shell command")),softKeyboard:m.optional(hr())})),appearance:m.optional(Ue(fr)),fontSize:m.optional(Ue(ur)),id:ge("a device id"),ios:m.optional(me({autocorrect:m.optional(hr()),keyboards:m.optional(m.array(m.string(C("a keyboard layout")).check(m.regex(pn,C("a layout like en_US@sw=QWERTY"))),C("a list of keyboard layouts")))})),locale:m.optional(m.string(C("a BCP 47 tag, e.g. fr-CA")).check(m.refine(mn,{error:C("a BCP 47 tag, e.g. fr-CA")}))),location:m.optional(me({latitude:pr(90),longitude:pr(180)})),orientation:m.optional(Ue(_t)),osVersion:m.optional(ge("an OS version")),timezone:m.optional(m.string(C("an IANA timezone")).check(m.refine(bn,{error:C("an IANA timezone, e.g. America/Toronto")})))}),endpoint:m.optional(m.url(C("a URL"))),launch:m.optional(me({app:m.optional(m.union([ge("an app id"),m.literal(!1)],C("an app id or false"))),ios:m.optional(me({args:m.optional(m.array(m.string(C("an argument")).check(m.regex(yn,C(wn))),C("a list of arguments")).check(m.maxLength(20,C("at most 20 arguments"))))})),params:m.optional(m.custom(t=>typeof t=="object"&&t!==null&&!Array.isArray(t),C("an object"))),permissions:m.optional(Ue(dn)),url:m.optional(m.url(C("a URL")))})),proxy:m.optional(m.union([m.literal("intercept"),m.templateLiteral(["http://",m.string()])],C("intercept or an http:// URL"))),target:m.optional(ge("an app, build or group id"))}).check(m.refine(t=>t.device.android?.shell===void 0||t.target!==void 0,{error:bt,params:mt,path:["device","android","shell"]}),m.refine(t=>t.launch?.app===void 0||t.target!==void 0,{error:bt,params:mt,path:["launch","app"]}),m.refine(t=>t.launch?.permissions===void 0||t.target!==void 0,{error:bt,params:mt,path:["launch","permissions"]}),m.refine(t=>t.launch?.params===void 0||t.target!==void 0&&gr(t),{error:wr,params:yr,path:["launch","params"]}),m.refine(t=>t.launch?.ios?.args===void 0||t.target!==void 0&&gr(t),{error:wr,params:yr,path:["launch","ios","args"]}));function Sn(t){if(t.code==="custom"){const e=t.params?.code;if(e==="needsTarget"||e==="needsLaunchApp")return e}return"invalidValue"}function vn(t){const e=En.safeParse(t);if(e.success)return;const r=e.error.issues.map(s=>({code:Sn(s),message:s.message,path:s.path.join(".")}));throw new dr("invalid",r.map(({message:s,path:n})=>`${n}: ${s}`).join(`
|
|
2
|
+
`),r)}const An={dateStyle:"medium",timeStyle:"short"},xn=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;function Ve(t){return Array.isArray(t)?t.map(Ve).join(", "):typeof t=="boolean"?t?"yes":"no":typeof t=="number"?t.toLocaleString():kn(t)}function kn(t){if(xn.test(t)){const r=new Date(t);if(!Number.isNaN(r.getTime()))return r.toLocaleString(void 0,An)}const e=Ut();return t===e?"~":t.startsWith(`${e}${Fr}`)?`~${t.slice(e.length)}`:t}function mr(){return process.env.APPETIZE_CLI_HOME??ne(Ut(),".appetize","cli","sessions")}function Et(t){return ne(mr(),t)}class Ke 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 Pe extends Ke{sessionDir;constructor(e,{cause:r,details:s={},sessionId:n}){const i=Et(n);super(e,Object.assign({sessionDir:i},s),r),this.name="SessionError",this.sessionDir=i}static wrap(e,r){if(e instanceof Pe||e instanceof ue)return e;const s=e instanceof Error?e.message:String(e),n=e instanceof Ke?e.details:{};return new Pe(s,{cause:e,details:n,sessionId:r})}hints(){return[`Check the session logs for details: ${Ve(this.sessionDir)}`]}}class In extends Pe{constructor(e){super(`Session ${e} already exists`,{sessionId:e}),this.name="SessionAlreadyExistsError"}hints(){return[`Pass --force to replace it, deleting ${Ve(this.sessionDir)}`]}}class ue extends Ke{#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 On(t,e,r){const s=e instanceof Error?e.message:String(e);if(!(e instanceof Ke))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(`
|
|
3
|
+
`)}function St(t){try{return process.kill(t,0),!0}catch{return!1}}const Tn=["SIGINT","SIGTERM"],Rn=150;function Mn(t){const e=new AbortController,r=setInterval(()=>{St(t)||(clearInterval(r),e.abort(new Error("The device session exited")))},Rn);return r.unref(),e.signal}function br(){const t=new AbortController;for(const e of Tn)process.once(e,()=>{t.abort(new Error(`Received ${e}`))});return t.signal}function Un(t,e){return new Promise((r,s)=>{if(e.aborted){s(Er(e.reason));return}const n=()=>{clearTimeout(i),s(Er(e.reason))},i=setTimeout(()=>{e.removeEventListener("abort",n),r()},t);e.addEventListener("abort",n,{once:!0})})}function Er(t){return t instanceof Error?t:new Error(String(t))}function Ln(t,{defaultValue:e=!1}={}){return t===!e?!e:void 0}function Pn(t){const e=t.at(-1);return{options:Nn(e)?e:{},positionals:t.slice(0,-1).filter(r=>typeof r=="string"),signal:br()}}function Cn(t,e){const r=Sr(t,e);return r===void 0?void 0:vr(r,e)}function Sr(t,e){if(t===void 0)return;const r=typeof t=="boolean"?Number.NaN:Number(t);if(!Number.isFinite(r))throw new ue(`${e} expects a number, e.g., ${e}=-0.1`);return r}function vt(t,e){if(t!==void 0){if(typeof t!="string"||t==="")throw new ue(`${e} needs a value`);return t}}function vr(t,e){if(!(t>=0&&t<=1))throw new ue(`${e} expects a number between 0 and 1`);return t}function _n(t,e){const r=vt(t,e);if(r===void 0)throw new ue(`${e} needs a value`);return r}function Nn(t){return typeof t=="object"&&t!==null}const Dn="APPETIZEIO_FEATURE_FLAGS";function Bn(){const t={};for(const e of(process.env[Dn]??"").split(",")){const r=e.trim();r!==""&&(t[r]=!0)}return t}function zn(){const t=process.env.APPETIZE_API_TOKEN;if(!t)throw new Error("APPETIZE_API_TOKEN must be set");return t}function Ar(t){return vt(t.endpoint,"--endpoint")??Fn()}function Fn(){const t=process.env.APPETIZE_ENDPOINT?.trim();return t===""?void 0:t}const jn="https://appetize.io";async function xr(t,{authorize:e=!0,commandOptions:r={},query:s={},...n}={}){const i=new URL(t,Ar(r)??jn);for(const[p,A]of Object.entries(s))i.searchParams.set(p,A);const l=new Headers(n.headers);e&&l.set("x-api-key",zn());const y=await fetch(i,{...n,headers:l});if(!y.ok)throw new Error(`The webserver rejected the request (${String(y.status)})`);return await y.json()}const kr=["android","ios"];function Ir(t={},e){return xr("/api/v2/service/devices",{authorize:!1,commandOptions:t,signal:e})}function $n(t){return kr.includes(t)}async function Vn(t,e={}){return(await Ir(e)).find(s=>s.id===t)?.platform}function Kn(t){let e={...t,updatedAt:new Date().toISOString()};return{path:xt(e.sessionId),save:r=>(e={...e,...r,updatedAt:new Date().toISOString()},Qn(e),e)}}function Or(t){const e=t.state?.phase;return e==="ended"||e==="error"?!1:St(t.pid)}function qn(){const t=mr();return Mt(t,{recursive:!0}),zr(t).map(e=>At(e)).filter(e=>e!==void 0)}function Wn(t){if(t!==void 0){const r=At(t);if(!r)throw new Error(`No session found for id ${t}`);return r}const e=qn().filter(Or);if(e.length===0)throw new Error("No active sessions");if(e.length>1)throw new ue("There are multiple active sessions",["Specify --session-id to pick one"]);return e[0]}function At(t){try{return JSON.parse(Dr(xt(t),"utf8"))}catch{return}}function Jn(t){Br(Et(t),{force:!0,recursive:!0})}function Gn(t){return ne(Le(t),"control.sock")}function Tr(t){return ne(Le(t),"daemon.log")}function Hn(t){return ne(Le(t),"device.log.jsonl")}function Zn(t,e){return{daemon:Tr(t),device:Hn(t),...e==="intercept"?{network:Yn(t)}:void 0}}function Yn(t){return ne(Le(t),"network.jsonl")}function Le(t){const e=Et(t);return Mt(e,{recursive:!0}),e}function xt(t){return ne(Le(t),"state.json")}function Qn(t){const e=xt(t.sessionId),r=`${e}.tmp`;_r(r,`${JSON.stringify(t,null,2)}
|
|
4
|
+
`),Nr(r,e)}const kt="APPETIZE_DAEMON_CONFIG";function Xn(){const t=process.env[kt];if(!t)throw new Error(`${kt} is not set`);return JSON.parse(t)}function ei(t){return JSON.stringify(t)}function ti(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}const Rr={useRecords:!1},ri=new ir(Rr);class Mr extends ve{#e=[];#t;constructor(e,{signal:r}={}){super(async l=>{this.#t.write(ri.encode(l))},{signal:r}),this.#t=e;const s=new ln(Rr);s.on("data",l=>{Ye(l)&&this[U].dispatchMessage(l)}),s.on("error",()=>{this.close()});const n=l=>{s.write(l)},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 si(t,e){const{promise:r,reject:s,resolve:n}=ti(),i=Vr(t),l=e?.signal,y=()=>{i.off("connect",E),i.off("error",R),l?.removeEventListener("abort",A)},p=M=>{y(),i.destroy(),s(M)},A=()=>{p(new Error("The connection was aborted"))},E=()=>{y();const M=new Mr(i,e);M[U].addEventListener("close",()=>{i.destroy()},{once:!0}),n(M)},R=M=>{p(M)};return l?.aborted?(p(new Error("The connection was aborted")),r):(i.once("connect",E),i.once("error",R),l?.addEventListener("abort",A,{once:!0}),r)}export{Gn as $,Zn as A,Cn as B,Ve as C,kt as D,si as E,Bs as F,Pt as G,Wn as H,Pn as I,On as J,He as K,an as L,U as M,en as N,Ye as O,Vt as P,Ne as Q,hn as R,Pe as S,ve as T,ue as U,un as V,Ge as W,Mr as X,Xn as Y,Kn as Z,Vn as _,Sr as a,br as a0,Ir as b,kr as c,Un as d,jt as e,xr as f,_n as g,_t as h,$n as i,Tr as j,Or as k,St as l,Mn as m,Ln as n,ur as o,vt as p,fr as q,vr as r,ei as s,dr as t,Bn as u,vn as v,Ar as w,At as x,In as y,Jn as z};
|
package/stream-CdilRAka.js
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import je from"debug";import{JSONRPCServerAndClient as yr,JSONRPCServer as wr,JSONRPCClient as br,JSONRPC as gr}from"json-rpc-2.0";import{writeFileSync as mr,renameSync as Er,mkdirSync as At,readFileSync as Sr,rmSync as vr,readdirSync as Ar}from"node:fs";import{sep as xr,join as re}from"node:path";import{homedir as xt}from"node:os";import{Transform as kr}from"stream";import{createRequire as Ir}from"module";import{connect as Or}from"node:net";class Tr 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 Mr(t,e){const r="CloseEvent"in globalThis?globalThis.CloseEvent:Tr;return new r(t,e)}class Rr{#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 R=Symbol.for("@appetizeio/rpc:rpcMethods");class Ur extends Rr{send;constructor(e){super(),this.send=e}dispatchClose(e){return this.dispatchEvent(Mr("close",e))}dispatchMessage(e){return this.dispatchEvent(new CustomEvent("message",{detail:e}))}}const Pr="@appetizeio/rpc:playwrightAdapter";class ge{closed=!1;[R];#e;constructor(e,{signal:r}={}){this[R]=new Ur(e),this.#e=r,this.#e?.addEventListener("abort",this.#t)}close(e={}){this.closed||(this.closed=!0,this.handleClose(e))}handleClose(e){this.#e?.removeEventListener("abort",this.#t),this[R].dispatchClose(e)}#t=()=>{this.close({code:1e3,reason:String(this.#e?.reason)})}}const Me=Symbol.for(Pr);function Cr(t){window[Me]&&console.warn("playwright exposed more than once, overwriting previous adapter");const e={send(r){t[R].send(r)},subscribe(r){return t[R].addEventListener("message",s=>{r(s.detail)})}};return window[Me]=e,()=>{window[Me]===e&&delete window[Me]}}const kt=je("appetize.io:error:rpc");function $e(t,e){const r=new yr(new wr({errorListener:kt}),Lr(t),{errorListener:kt}),s=t[R].addEventListener("message",({detail:i})=>{r.receiveAndSend(i)}),n=t[R].addEventListener("close",()=>{s(),r.rejectAllPendingRequests("Transport closed")},{once:!0});for(const i in e){const l=e[i];l&&r.addMethod(i,l.bind(e))}return{rpc:r,unsub:()=>{s(),n()}}}function It(t,e){const{rpc:r,unsub:s}=$e(t,e);return{client:r.client,unsub:s}}function Re(t,e){const r=t[R].addEventListener("message",({detail:n})=>{if("method"in n)try{e({method:n.method,param:n.params})}catch{}}),s=t[R].addEventListener("close",r,{once:!0});return()=>{r(),s()}}function Lr(t){return new br(t[R].send.bind(t[R]),()=>Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)+1)}function Ve(t,e,{leftEffects:r=()=>({}),notifiesLeft:s,notifiesRight:n,rightEffects:i=()=>({})}={}){const l=$e(t,{}),y=$e(e,{}),p=[()=>{l.unsub(),y.unsub()}],v=()=>{for(const C of p)try{C()}catch{}},m=y.rpc.client,T=l.rpc.client;p.push(t[R].addEventListener("close",()=>{v()},{once:!0}),e[R].addEventListener("close",()=>{v()},{once:!0}));const M=r(m,T);for(const C in M){const P=M[C];P&&l.rpc.addMethod(C,P)}const L=i(T,m);for(const C in L){const P=L[C];P&&y.rpc.addMethod(C,P)}return Ot(l.rpc,y.rpc,{notifies:s}),Ot(y.rpc,l.rpc,{notifies:n}),v}function Ot(t,e,{notifies:r}={}){t.applyServerMiddleware(async(s,n)=>{const i=r?.();if(i){const{id:y,...p}=n;i[R].send(p)}if(t.hasMethod(n.method))return await s(n);if(!n.id)return e.notify(n.method,n.params),null;const l=await e.request(n.method,n.params);return{id:n.id,jsonrpc:gr,result:l}})}function Y(t,...e){t.notify(e[0],e[1],e[2])}const Nr=(...t)=>It(...t),Ke=["ArrowDown","ArrowLeft","ArrowRight","ArrowUp","Backspace","Enter","Tab"];function qe(){const t=new ge(async r=>{e[R].dispatchMessage(r)}),e=new ge(async r=>{t[R].dispatchMessage(r)});return t[R].addEventListener("close",()=>{e.close()},{once:!0}),e[R].addEventListener("close",()=>{t.close()},{once:!0}),[t,e]}function Tt(t,e){const r=[],s=()=>{for(const n of r)n()};r.push(t[R].addEventListener("message",n=>e[R].send(n.detail))),r.push(t[R].addEventListener("close",s,{once:!0})),r.push(e[R].addEventListener("message",n=>t[R].send(n.detail))),r.push(e[R].addEventListener("close",s,{once:!0}))}function Je(t){return!!t&&typeof t=="object"&&"jsonrpc"in t}const se=je("appetize.io:trace:transport:iframe");class Mt extends ge{onmessage;#e;#t;constructor(e,{origin:r="*",signal:s}={}){super(async n=>{e.postMessage(n,this.#e)},{signal:s}),this.#t=e,this.#e=r,window.addEventListener("message",this.#r)}postMessage(e,r,s){se("postMessage(legacy)",{message:e,targetOrigin:r,transfer:s}),this.#t.postMessage(e,r??this.#e,s)}handleClose(e){se("close()"),window.removeEventListener("message",this.#r),super.handleClose(e)}#r=async e=>{if(se("handleMessage()",e),e.source!==this.#t||(se("handleMessage() -> source ok"),this.#e!=="*"&&e.origin!==this.#e))return;se("handleMessage() -> origin ok");const r=e.data;if(!Je(r)){se("handleMessage() -> legacy"),this.onmessage?.(e);return}se("handleMessage() -> jsonrpc"),this[R].dispatchMessage(r)}}function Ue(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}const _r=2*1024*1024,Rt=je("appetize:client:acked-writable-stream");class Dr{unacked=0;#e;#t;#r;#s;#n=[];#o;constructor(e){this.#r=e,this.#o=e.window??_r}abort(e){this.#i(e),this.#r.onAbort?.(e)}async close(){await this.#c(()=>this.unacked===0),this.#r.onClose?.()}fail(e){this.#i(e)&&this.#r.onError?.(e)}start(e){this.#e=e}write(e){const r=e.byteLength;return this.unacked+=r,this.#r.send(e).then(()=>{this.unacked-=r,this.#s&&(this.#s.acked+=r),this.#a()},s=>{this.fail(s)}),this.unacked>=this.#o&&!this.#s&&(this.#s={acked:0,at:performance.now()},Rt("window full: %d bytes unacked, writes stalled",this.unacked)),this.#c(()=>this.unacked<this.#o)}#a(){if(this.#s&&this.unacked<=this.#o/2){const e=performance.now()-this.#s.at;Rt("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)}#i(e){if(this.#t)return!1;this.#t={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={...Ue(),until:e};return this.#n.push(r),r.promise}}class Br extends WritableStream{get unacked(){return this.#e.unacked}#e;constructor(e){const r=new Dr(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 Fr=1024*1024;class zr{#e;#t=!1;#r;#s;constructor(e){this.#r=e}cancel(e){this.#n(),this.#r?.(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.#t)return;this.#s??=Ue();const{promise:r}=this.#s;this.#e.enqueue(e),await r}start(e){this.#e=e}#n(){return this.#t?!1:(this.#t=!0,this.pull(),!0)}}class jr extends ReadableStream{#e;constructor({highWaterMark:e=Fr,onCancel:r}={}){const s=new zr(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 $r{closed;readable;writable;#e=[];#t;#r;#s;#n;#o;#a="starting";constructor(e,{onClose:r,receiveBuffer:s,sendWindow:n,signal:i}){if(this.#o=e,this.#t=Ue(),this.closed=this.#t.promise,this.closed.catch(Vr),this.#s=new jr({highWaterMark:s,onCancel:()=>{this.#i(void 0)}}),this.readable=this.#s,this.#n=new Br({onAbort:()=>{this.#i(void 0)},onClose:()=>{this.#i(void 0)},onError:l=>{this.#i(l)},send:l=>e.request("appetizeio.device.server.debug.adbTunnel.data",{data:l}),window:n}),this.writable=this.#n,i){const l=()=>{this.#i(i.reason)};i.addEventListener("abort",l,{once:!0}),this.#e.push(()=>{i.removeEventListener("abort",l)})}this.#e.push(()=>{r(this)})}close=()=>{this.#i(void 0)};detach(){this.#i(void 0,{notifyServer:!1})}async handleData(e){this.#a==="open"&&await this.#s.push(e)}handleStopped({error:e}){this.#a==="open"&&this.#i(e?new Error(e):void 0)}async start(){try{await this.#o.request("appetizeio.device.server.debug.adbTunnel.start",void 0)}catch(e){throw this.#i(e),e}if(this.#a==="closed")throw this.#r?this.#r:new Error("The adb tunnel closed before it started");this.#a="open"}async[Symbol.asyncDispose](){this.close();try{await this.closed}catch{}}#i(e,{notifyServer:r=!0}={}){if(this.#a!=="closed"){this.#a="closed",this.#r=e,r&&Y(this.#o,"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.#t.reject(e):this.#t.resolve(void 0)}}}function Vr(){}class Kr{readable;ready;#e=[];#t=!1;#r;#s;#n;constructor({label:e,onClose:r,signal:s}){this.#s=e,this.ready=new Promise((i,l)=>{this.#n={reject:l,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.#r=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.#o()};fail(e){this.#o(e)}handleItem(e){this.#r.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()}}#o(e){this.#t||(this.#r.close(),this.#a(e))}#a(e){this.#t=!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 qr{get subscribers(){return this.#r}#e;#t;#r=new Set;constructor(e){this.#e=e}close(){this.#t=void 0;for(const e of[...this.#r])e.close();this.#r.clear()}add(e){this.#r.add(e)}ensureStarted(){return this.#t||(this.#t=(async()=>{try{return await this.#e.start()}catch(e){throw this.#t=void 0,e}})()),this.#t}release(e){this.#r.delete(e),!(this.#r.size>0||!this.#t)&&(this.#t=void 0,this.#e.stop())}}class Ut extends qr{#e;constructor(e){super({start:e.start,stop:e.stop}),this.#e=e.label}async createStream({signal:e}={}){e?.throwIfAborted();const r=new Kr({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 Jr{#e;#t;#r;#s;constructor(e){this.#s=e,this.#t=new Ut({label:"Log stream",start:()=>e.request("appetizeio.device.server.debug.log.start"),stop:()=>{Y(e,"appetizeio.device.server.debug.log.stop",void 0)}}),this.#r=new Ut({label:"Proxy intercept stream",start:()=>e.request("appetizeio.device.server.debug.proxyIntercept.start"),stop:()=>{Y(e,"appetizeio.device.server.debug.proxyIntercept.stop",void 0)}})}close(){this.#e?.detach(),this.#t.close(),this.#r.close()}async createAdbTunnel({signal:e}={}){e?.throwIfAborted(),this.#e?.close();const r=new $r(this.#s,{onClose:()=>{this.#e=void 0},signal:e});return this.#e=r,await r.start(),r}async createLogStream(e){return await this.#t.createStream(e)}async createProxyInterceptStream(e){return await this.#r.createStream(e)}async handleAdbTunnelData({data:e}){await this.#e?.handleData(e)}handleAdbTunnelStopped(e){this.#e?.handleStopped(e)}handleLogEntry(e){this.#t.handleNotification(e)}handleProxyInterceptEvent(e){this.#r.handleNotification(e)}}function We(t){return{...Wr(t),index:t.index,within:t.within?We(t.within):void 0}}function Wr(t){switch(t.by){case"testId":return{by:"testId",testId:t.testId};case"text":return{by:"text",text:Gr(t.text)}}}function Gr(t){return typeof t=="string"?{kind:"exact",value:t}:{flags:t.flags,kind:"regex",source:t.source}}const Hr=400,Pt=["byTestId","byText","duration","index","x","y"],Zr=new Set(Pt),Xr=/^-?(?:\d+(?:\.\d*)?|\.\d+)$/u,Yr=/^[a-z]$/u,Ge=/^\s$/u;class N extends SyntaxError{constructor(e,r,s){super(`${e} at position ${String(r)}`,{cause:s})}}class Qr{#e=0;#t;constructor(e){this.#t=e}parse(){const e=[];for(;this.#e<this.#t.length;){const r=this.#i();if(Ge.test(r))this.#e++;else if(r===">"){if(this.#t.charAt(this.#e+1)===">")throw new N('Force press ">>" is not supported',this.#e);this.#e++,e.push({kind:"down"})}else if(r==="/")this.#e++,e.push({kind:"up"});else if(r==="{")e.push(this.#o());else{const[s]=this.#t.slice(this.#e,this.#e+2);throw new N(`Unexpected ${JSON.stringify(s)}; write selectors as {...}`,this.#e)}}return e}#r(){const e=this.#e,r=this.#c(s=>s!=="="&&!He(s));if(this.#i()!=="=")return{kind:"bare",name:r,offset:e};if(r.length===0)throw new N('Expected an attribute name before "="',e);return this.#e++,{kind:"attribute",name:r,offset:e,value:this.#a()}}#s(){const e=this.#e;this.#e++;let r="";for(;;){const s=this.#i();if(this.#e++,s==='"')return r;if(s==="\\"){const n=this.#i();if(n!=='"'&&n!=="\\"&&n!=="")throw new N('Only \\" and \\\\ escapes are supported',this.#e-1);this.#e++,r+=n}else{if(s==="")throw new N("Unclosed string",e);r+=s}}}#n(){const e=this.#e;this.#e++;let r="",s=!1;for(;;){const i=this.#i();if(i==="")throw new N("Unclosed regex",e);if(this.#e++,i==="/"&&!s)break;r+=i,i==="\\"?(r+=this.#i(),this.#e++):i==="["?s=!0:i==="]"&&(s=!1)}const n=this.#c(i=>Yr.test(i));try{return new RegExp(r,n)}catch(i){throw new N(`Invalid regex /${r}/${n}`,e,i)}}#o(){const e=this.#e;this.#e++;let r;const s={offsets:{}};for(let n=0;;n++){this.#l();const i=this.#i();if(i==="")throw new N('Unclosed "{"',e);if(i==="}"){if(this.#e++,n===0)throw new N('Expected a selector or wait in "{}"',e);return r===void 0?rs(s,e):{duration:r,kind:"wait"}}const l=this.#r();if(n===0&&l.name==="wait")r=l.kind==="attribute"?Lt(l.value,"wait"):Hr;else{if(r!==void 0)throw new N('"wait" takes no other attributes',l.offset);es(s,l,n)}}}#a(){const e=this.#e,r=this.#i();let s;if(r==='"')s={kind:"quoted",offset:e,text:this.#s()};else if(r==="/")s={kind:"regex",offset:e,regex:this.#n()};else{const n=this.#c(i=>!He(i));if(n.length===0)throw new N('Expected a value after "="',e);return{kind:"raw",offset:e,text:n}}if(!He(this.#i()))throw new N('Expected a space or "}" after the value',this.#e);return s}#i(){return this.#t.charAt(this.#e)}#c(e){const r=this.#e;for(;this.#e<this.#t.length&&e(this.#i());)this.#e++;return this.#t.slice(r,this.#e)}#l(){this.#c(e=>Ge.test(e))}}function Ct(t){return new Qr(t).parse()}function es(t,e,r){const{name:s,offset:n}=e;if(e.kind==="attribute"){Ze(t,s,n,e.value);return}if(!s.includes(","))throw new N(`Expected key=value, got ${JSON.stringify(s)}`,n);if(r>0)throw new N("Write the x,y position first",n);const[i,l,...y]=s.split(",");if(y.length>0)throw new N("Expected a screen position as x,y",n);const p=n+i.length+1;Ze(t,"x",n,{kind:"raw",offset:n,text:i}),Ze(t,"y",p,{kind:"raw",offset:p,text:l})}function ts(t){return Zr.has(t)}function He(t){return t===""||t==="}"||Ge.test(t)}function Ze(t,e,r,s){if(!ts(e))throw new N(`Unknown attribute ${JSON.stringify(e)}; expected one of ${Pt.join(", ")}`,r);if(t.offsets[e]!==void 0)throw new N(`Duplicate attribute ${JSON.stringify(e)}`,r);switch(t.offsets[e]=r,e){case"byTestId":case"byText":if(t.byText!==void 0||t.byTestId!==void 0)throw new N("Only one of byText and byTestId may be given",r);e==="byText"?t.byText=is(s):t.byTestId=ns(s);return;default:t[e]=Lt(s,e)}}function rs(t,e){return{duration:t.duration,kind:"move",to:ss(t,e)}}function Lt(t,e){if(t.kind!=="raw"||!Xr.test(t.text))throw new N(`"${e}" takes a number`,t.offset);return Number(t.text)}function ss(t,e){const{byTestId:r,byText:s,index:n,offsets:i,x:l,y}=t;if(s!==void 0)return{by:"text",index:n,text:s,x:l,y};if(r!==void 0)return{by:"testId",index:n,testId:r,x:l,y};if(i.index!==void 0)throw new N('"index" needs byText or byTestId',i.index);if(l===void 0||y===void 0)throw new N("A screen position needs x and y",e);return{by:"position",x:l,y}}function ns(t){if(t.kind==="regex")throw new N('"byTestId" takes a string',t.offset);return t.text}function is(t){return t.kind==="regex"?t.regex:t.text}function me(t){return t.by==="position"?{by:"position",x:t.x,y:t.y}:{...We(t),x:t.x,y:t.y}}function os(t){return t.map(e=>e.kind==="move"?{duration:e.duration,kind:"move",to:me(e.to)}:e)}function as(t){return{press:e=>{Y(t,"appetizeio.device.server.keyboard.press",{key:e})}}}function cs({onRequest:t,parent:e,proxyInlet:r,signal:s}){const n=new Mt(e,{signal:s}),i=[()=>{n.close()}];return Tt(n,r),t&&i.push(Re(n,l=>{t(l,{source:"parent"})})),{cleanup:()=>{for(const l of i)l()},notifies:()=>e===globalThis.window?void 0:n}}function ls({onRequest:t,proxyInlet:e}){const[r,s]=qe(),i=[Cr(s),()=>{r.close()}];return Tt(r,e),t&&i.push(Re(r,l=>{t(l,{source:"parent"})})),{cleanup:()=>{for(const l of i)l()},notifies:()=>r}}function fs(t){return"to"in t?{from:me(t.from),kind:"path",to:me(t.to)}:{direction:t.direction,distance:t.distance,from:t.from?me(t.from):void 0,kind:"direction"}}function us(t){return{down:(e,r)=>{Y(t,"appetizeio.device.server.touch.down",{x:e,y:r})},move:(e,r)=>{Y(t,"appetizeio.device.server.touch.move",{x:e,y:r})},up:(e,r)=>{Y(t,"appetizeio.device.server.touch.up",{x:e,y:r})}}}const ds=new Set(Ke),hs=/^(?<key>[A-Za-z]+)>(?<repeat>\d+)\/$/u,ps=/(\{\{|\{[^{}]*\})/u;function ys(t){const e=[];let r="";function s(){r.length>0&&(e.push({kind:"text",text:r}),r="")}for(const[n,i]of t.split(ps).entries())if(n%2===0){if(i.includes("{"))throw new Error(`Unclosed "{" in ${JSON.stringify(t)}`);r+=i}else i==="{{"?r+="{":(s(),e.push(bs(i.slice(1,-1))));return s(),e}function ws(t,e){if(!ds.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: ${Ke.join(", ")}. Type characters as text, and escape a literal "{" as "{{".`)}function bs(t){const e=hs.exec(t)?.groups,r=e?.key??t;if(ws(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 gs=Symbol("@appetizeio/client:v1Compat");function Nt(t){return t?.[gs]}class ms{readable;ready;get dimensions(){return this.#s}#e=[];#t=!1;#r;#s;#n;#o;constructor({onClose:e,requestKeyFrame:r,signal:s}){this.#n=r,this.ready=new Promise((i,l)=>{this.#o={reject:l,resolve:i}});let n;if(this.readable=new ReadableStream({cancel:()=>{this.#i()},start:i=>{n=i}}),!n)throw new Error("ReadableStream controller was not initialized");if(this.#r=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.#a()};fail(e){this.#a(e)}handleChunk(e){if(this.#o){if(e.type!=="key")return;const{resolve:r}=this.#o;this.#o=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.#r.enqueue(e)}requestKeyFrame=()=>{this.#t||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()}}#a(e){this.#t||(this.#r.close(),this.#i(e))}#i(e){this.#t=!0;for(const r of this.#e)r();this.#o?.reject(e??new Error("Video stream closed before the first key chunk")),this.#o=void 0}}class Es{#e=!1;#t;#r;#s=new Set;constructor(e){this.#t=e}close(){this.#r=void 0;for(const e of[...this.#s])e.close()}async createStream({signal:e}={}){e?.throwIfAborted();const r=new ms({onClose:s=>{this.#o(s)},requestKeyFrame:()=>{this.#a()},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.#r?(this.#a(),this.#r):(this.#e=!0,this.#r=(async()=>{try{await this.#t.request("appetizeio.device.server.video.start")}catch(e){throw this.#r=void 0,e}})(),this.#r)}#o(e){this.#s.delete(e),!(this.#s.size>0||!this.#r)&&(this.#r=void 0,Y(this.#t,"appetizeio.device.server.video.stop",void 0))}#a(){this.#e||(this.#e=!0,Y(this.#t,"appetizeio.device.server.video.requestKeyFrame",void 0))}}class Ss{debug;keyboard;touch;video;#e;#t=[];#r;constructor(e,r={}){const s=Nt(r)??{},{baseUrl:n,onClose:i,signal:l}=r;this.#e=n?vs(n):void 0;const{onRequest:y,parent:p,proxyRequest:v}=s,m=As(y);l?.addEventListener("abort",this.close);const[T,M]=qe(),[L,C]=qe();this.#t.push(()=>{T.close(),M.close(),L.close(),C.close()}),this.#t.push(Ve(C,e,{leftEffects:v}));const P=p==="playwright"?ls({onRequest:m,proxyInlet:L}):p?cs({onRequest:m,parent:p,proxyInlet:L,signal:l}):void 0;P&&this.#t.push(P.cleanup),this.#t.push(Ve(M,L,{notifiesLeft:P?.notifies}));const{client:G,unsub:we}=Nr(T,{"appetizeio.device.client.debug.adbTunnel.data":j=>H.handleAdbTunnelData(j),"appetizeio.device.client.debug.adbTunnel.stopped":async j=>{H.handleAdbTunnelStopped(j)},"appetizeio.device.client.debug.log.entry":async j=>{H.handleLogEntry(j)},"appetizeio.device.client.debug.proxyIntercept.event":async j=>{H.handleProxyInterceptEvent(j)},"appetizeio.device.client.video.chunk":async j=>{x.handleChunk(j)}});this.#r=G,this.#t.push(we);const H=new Jr(this.#r);this.#t.push(()=>{H.close()});const x=new Es(this.#r);this.#t.push(()=>{x.close()}),this.debug=H,this.keyboard=as(this.#r),this.touch=us(this.#r),this.video=x,m&&(this.#t.push(Re(M,j=>{m(j,{source:"self"})})),e instanceof Mt&&this.#t.push(Re(e,j=>{m(j,{source:"iframe"})}))),this.#t.push(()=>i?.())}async clearKeychain(){await this.#r.request("appetizeio.device.server.clearKeychain")}close=()=>{for(const e of this.#t)try{e()}catch(r){console.error("Device cleanup error:",r)}};async fold(e){await this.#r.request("appetizeio.device.server.fold",{hinge:e})}async gesture(e,r){await this.#r.request("appetizeio.device.server.gesture",{options:r,steps:os(typeof e=="string"?Ct(e):e)})}async inspect(e,r){return await this.#r.request("appetizeio.device.server.inspect",{options:r,selector:e?We(e):void 0})}async open(e){new URL(e),await this.#r.request("appetizeio.device.server.open",{url:e})}async press(e){await this.#r.request("appetizeio.device.server.press",{action:e})}async rotate(e){await this.#r.request("appetizeio.device.server.rotate",{orientation:e})}async screenshot(){const e=await this.#r.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.#r.request("appetizeio.device.server.swipe",{gesture:fs(e),options:r})}async tap(e,r){await this.#r.request("appetizeio.device.server.tap",{options:r,selector:me(e)})}async type(e,r){await this.#r.request("appetizeio.device.server.type",{options:r,steps:ys(e)})}}function vs(t){const e=new URL(t);return e.protocol==="ws:"?e.protocol="http:":e.protocol==="wss:"&&(e.protocol="https:"),e.href}const As=t=>{if(t)return(e,r)=>{e.method.startsWith("appetizeio.device.server.")&&t(e,r)}};let Xe;try{Xe=new TextDecoder}catch{}let w,V,f=0;const Ye=[],xs=105,ks=57342,Is=57343,_t=57337,Dt=6,de={};let Ee=11281e4,Q=1681e4,ne=Ye,Se=0,k={},_,ve,Ae=0,ie=0,D,J,B=[],Qe=[],K,$,xe,Bt={useRecords:!1,mapsAsObjects:!0},ke=!1,Ft=2;try{new Function("")}catch{Ft=1/0}class ye{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[W(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(w)return Jt(()=>(st(),this?this.decode(e,r):ye.prototype.decode.call(Bt,e,r)));V=r>-1?r:e.length,f=0,Se=0,ie=0,ve=null,ne=Ye,D=null,w=e;try{$=e.dataView||(e.dataView=new DataView(e.buffer,e.byteOffset,e.byteLength))}catch(s){throw w=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 ye){if(k=this,K=this.sharedValues&&(this.pack?new Array(this.maxPrivatePackedValues||16).concat(this.sharedValues):this.sharedValues),this.structures)return _=this.structures,Pe();(!_||_.length>0)&&(_=[])}else k=Bt,(!_||_.length>0)&&(_=[]),K=null;return Pe()}decodeMultiple(e,r){let s,n=0;try{let i=e.length;ke=!0;let l=this?this.decode(e,i):it.decode(e,i);if(r){if(r(l)===!1)return;for(;f<i;)if(n=f,r(Pe())===!1)return}else{for(s=[l];f<i;)n=f,s.push(Pe());return s}}catch(i){throw i.lastPosition=n,i.values=s,i}finally{ke=!1,st()}}}function Pe(){try{let t=I();if(D){if(f>=D.postBundlePosition){let e=new Error("Unexpected bundle position");throw e.incomplete=!0,e}f=D.postBundlePosition,D=null}if(f==V)_=null,w=null,J&&(J=null);else if(f>V){let e=new Error("Unexpected end of CBOR data");throw e.incomplete=!0,e}else if(!ke)throw new Error("Data read, but end of buffer not reached");return t}catch(t){throw st(),(t instanceof RangeError||t.message.startsWith("Unexpected end of buffer"))&&(t.incomplete=!0),t}}function oe(){let t=new Error("Unexpected end of CBOR data");return t.incomplete=!0,t}function I(){if(!(f<V))throw oe();let t=w[f++],e=t>>5;if(t=t&31,t>23)switch(t){case 24:if(f>=V)throw oe();t=w[f++];break;case 25:if(e==7)return Rs();t=$.getUint16(f),f+=2;break;case 26:if(e==7){let r=$.getFloat32(f);if(k.useFloat32>2){let s=nt[(w[f]&127)<<1|w[f+1]>>7];return f+=4,(s*r+(r>0?.5:-.5)>>0)/s}return f+=4,r}if(t=$.getUint32(f),f+=4,e===1)return-1-t;break;case 27:if(e==7){let r=$.getFloat64(f);return f+=8,r}if(e>1){if($.getUint32(f)>0)throw new Error("JavaScript does not support arrays, maps, or strings with length over 4294967295");t=$.getUint32(f+4)}else k.int64AsNumber?(t=$.getUint32(f)*4294967296,t+=$.getUint32(f+4)):t=$.getBigUint64(f);f+=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())!=de;){if(n>=Ee)throw new Error(`Array length exceeds ${Ee}`);r[n++]=s}return e==4?r:e==3?r.join(""):Buffer.concat(r);case 5:let i;if(k.mapsAsObjects){let l={},y=0;if(k.keyMap)for(;(i=I())!=de;){if(y++>=Q)throw new Error(`Property count exceeds ${Q}`);l[W(k.decodeKey(i))]=I()}else for(;(i=I())!=de;){if(y++>=Q)throw new Error(`Property count exceeds ${Q}`);l[W(i)]=I()}return l}else{xe&&(k.mapsAsObjects=!0,xe=!1);let l=new Map;if(k.keyMap){let y=0;for(;(i=I())!=de;){if(y++>=Q)throw new Error(`Map size exceeds ${Q}`);l.set(k.decodeKey(i),I())}}else{let y=0;for(;(i=I())!=de;){if(y++>=Q)throw new Error(`Map size exceeds ${Q}`);l.set(i,I())}}return l}case 7:return de;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 Ms(t);case 3:if(ie>=f)return ve.slice(f-Ae,(f+=t)-Ae);if(ie==0&&V<140&&t<32){let n=t<16?$t(t):Ts(t);if(n!=null)return n}return jt(t);case 4:if(t>=Ee)throw new Error(`Array length exceeds ${Ee}`);if(t>V-f)throw oe();let r=new Array(t);for(let n=0;n<t;n++)r[n]=I();return r;case 5:if(t>=Q)throw new Error(`Map size exceeds ${Ee}`);if(t>(V-f)/2)throw oe();if(k.mapsAsObjects){let n={};if(k.keyMap)for(let i=0;i<t;i++)n[W(k.decodeKey(I()))]=I();else for(let i=0;i<t;i++)n[W(I())]=I();return n}else{xe&&(k.mapsAsObjects=!0,xe=!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>=_t){let n=_[t&8191];if(n)return n.read||(n.read=et(n)),n.read();if(t<65536){if(t==Is){let i=pe(),l=I(),y=I();tt(l,y);let p={};if(k.keyMap)for(let v=2;v<i;v++){let m=k.decodeKey(y[v-2]);p[W(m)]=I()}else for(let v=2;v<i;v++){let m=y[v-2];p[W(m)]=I()}return p}else if(t==ks){let i=pe(),l=I();for(let y=2;y<i;y++)tt(l++,I());return I()}else if(t==_t)return _s();if(k.getShared&&(rt(),n=_[t&8191],n))return n.read||(n.read=et(n)),n.read()}}let s=B[t];if(s)return s.handlesRead?s(I):s(I());{let n=I();for(let i=0;i<Qe.length;i++){let l=Qe[i](t,n);if(l!==void 0)return l}return new ae(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||ce())[t];if(n!==void 0)return n;throw new Error("Unknown token "+t)}default:throw isNaN(t)?oe():new Error("Unknown CBOR token "+t)}}const zt=/^[a-zA-Z_$][a-zA-Z\d_$]*$/;function et(t){if(!t)throw new Error("Structure is required in record definition");function e(){let r=w[f++];if(r=r&31,r>23)switch(r){case 24:r=w[f++];break;case 25:r=$.getUint16(f),f+=2;break;case 26:r=$.getUint32(f),f+=4;break;default:throw new Error("Expected array header, but got "+w[f-1])}let s=this.compiledReader;for(;s;){if(s.propertyCount===r)return s(I);s=s.next}if(this.slowReads++>=Ft){let i=this.length==r?this:this.slice(0,r);return s=k.keyMap?new Function("r","return {"+i.map(l=>k.decodeKey(l)).map(l=>zt.test(l)?W(l)+":r()":"["+JSON.stringify(l)+"]:r()").join(",")+"}"):new Function("r","return {"+i.map(l=>zt.test(l)?W(l)+":r()":"["+JSON.stringify(l)+"]: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[W(k.decodeKey(this[i]))]=I();else for(let i=0;i<r;i++)n[W(this[i])]=I();return n}return t.slowReads=0,e}function W(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 jt=Ce;function Os(t){jt=e();function e(r){return function(n){let i=ne[Se++];if(i==null){if(D)return Ce(n);let y=t(f,V,n,w);if(typeof y=="string")i=y,ne=Ye;else if(ne=y,Se=1,ie=1,i=ne[0],i===void 0)throw new Error("Unexpected end of buffer")}let l=i.length;return l<=n?(f+=n,i):(ve=i,Ae=f,ie=f+l,f+=n,i.slice(0,n))}}}function Ce(t){let e;if(t<16&&(e=$t(t)))return e;if(t>64&&Xe)return Xe.decode(w.subarray(f,f+=t));const r=f+t,s=[];for(e="";f<r;){const n=w[f++];if((n&128)===0)s.push(n);else if((n&224)===192)if(n<194||f>=r||(w[f]&192)!==128)s.push(65533);else{const i=w[f++]&63;s.push((n&31)<<6|i)}else if((n&240)===224){const i=f<r?w[f]:0;if(f>=r||(i&192)!==128||n===224&&i<160||n===237&&i>=160)s.push(65533);else if(f++,f>=r||(w[f]&192)!==128)s.push(65533);else{const l=w[f++]&63;s.push((n&31)<<12|(i&63)<<6|l)}}else if((n&248)===240){const i=f<r?w[f]:0;if(n>244||f>=r||(i&192)!==128||n===240&&i<144||n===244&&i>=144)s.push(65533);else if(f++,f>=r||(w[f]&192)!==128)s.push(65533);else{const l=w[f++]&63;if(f>=r||(w[f]&192)!==128)s.push(65533);else{const y=w[f++]&63;let p=(n&7)<<18|(i&63)<<12|l<<6|y;p-=65536,s.push(p>>>10&1023|55296),s.push(56320|p&1023)}}}else s.push(65533);s.length>=4096&&(e+=z.apply(String,s),s.length=0)}return s.length>0&&(e+=z.apply(String,s)),e}let z=String.fromCharCode;function Ts(t){let e=f,r=new Array(t);for(let s=0;s<t;s++){const n=w[f++];if((n&128)>0){f=e;return}r[s]=n}return z.apply(String,r)}function $t(t){if(t<4)if(t<2){if(t===0)return"";{let e=w[f++];if((e&128)>1){f-=1;return}return z(e)}}else{let e=w[f++],r=w[f++];if((e&128)>0||(r&128)>0){f-=2;return}if(t<3)return z(e,r);let s=w[f++];if((s&128)>0){f-=3;return}return z(e,r,s)}else{let e=w[f++],r=w[f++],s=w[f++],n=w[f++];if((e&128)>0||(r&128)>0||(s&128)>0||(n&128)>0){f-=4;return}if(t<6){if(t===4)return z(e,r,s,n);{let i=w[f++];if((i&128)>0){f-=5;return}return z(e,r,s,n,i)}}else if(t<8){let i=w[f++],l=w[f++];if((i&128)>0||(l&128)>0){f-=6;return}if(t<7)return z(e,r,s,n,i,l);let y=w[f++];if((y&128)>0){f-=7;return}return z(e,r,s,n,i,l,y)}else{let i=w[f++],l=w[f++],y=w[f++],p=w[f++];if((i&128)>0||(l&128)>0||(y&128)>0||(p&128)>0){f-=8;return}if(t<10){if(t===8)return z(e,r,s,n,i,l,y,p);{let v=w[f++];if((v&128)>0){f-=9;return}return z(e,r,s,n,i,l,y,p,v)}}else if(t<12){let v=w[f++],m=w[f++];if((v&128)>0||(m&128)>0){f-=10;return}if(t<11)return z(e,r,s,n,i,l,y,p,v,m);let T=w[f++];if((T&128)>0){f-=11;return}return z(e,r,s,n,i,l,y,p,v,m,T)}else{let v=w[f++],m=w[f++],T=w[f++],M=w[f++];if((v&128)>0||(m&128)>0||(T&128)>0||(M&128)>0){f-=12;return}if(t<14){if(t===12)return z(e,r,s,n,i,l,y,p,v,m,T,M);{let L=w[f++];if((L&128)>0){f-=13;return}return z(e,r,s,n,i,l,y,p,v,m,T,M,L)}}else{let L=w[f++],C=w[f++];if((L&128)>0||(C&128)>0){f-=14;return}if(t<15)return z(e,r,s,n,i,l,y,p,v,m,T,M,L,C);let P=w[f++];if((P&128)>0){f-=15;return}return z(e,r,s,n,i,l,y,p,v,m,T,M,L,C,P)}}}}}function Ms(t){return k.copyBuffers?Uint8Array.prototype.slice.call(w,f,f+=t):w.subarray(f,f+=t)}let Vt=new Float32Array(1),Le=new Uint8Array(Vt.buffer,0,4);function Rs(){let t=w[f++],e=w[f++],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 Le[3]=t&128|(r>>1)+56,Le[2]=(t&7)<<5|e>>3,Le[1]=e<<5,Le[0]=0,Vt[0]}new Array(4096);class ae{constructor(e,r){this.value=e,this.tag=r}}B[0]=t=>new Date(t),B[1]=t=>new Date(Math.round(t*1e3)),B[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},B[3]=t=>BigInt(-1)-B[2](t),B[4]=t=>+(t[1]+"e"+t[0]),B[5]=t=>t[1]*Math.exp(t[0]*Math.log(2));const tt=(t,e)=>{t=t-57344;let r=_[t];r&&r.isShared&&((_.restoreStructures||(_.restoreStructures=[]))[t]=r),_[t]=e,e.read=et(e)};B[xs]=t=>{let e=t.length,r=t[1];tt(t[0],r);let s={};for(let n=2;n<e;n++){let i=r[n-2];s[W(i)]=t[n]}return s},B[14]=t=>D?D[0].slice(D.position0,D.position0+=t):new ae(t,14),B[15]=t=>D?D[1].slice(D.position1,D.position1+=t):new ae(t,15);let Us={Error,RegExp};B[27]=t=>(Us[t[0]]||Error)(t[1],t[2]);const Kt=t=>{if(w[f++]!=132){let r=new Error("Packed values structure must be followed by a 4 element array");throw w.length<f&&(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()};Kt.handlesRead=!0,B[51]=Kt,B[Dt]=t=>{if(!K)if(k.getShared)rt();else return new ae(t,Dt);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},B[28]=t=>{J||(J=new Map,J.id=0);let e=J.id++,r=f,s=w[f],n;s>>5==4?n=[]:n={};let i={target:n};J.set(e,i);let l=t();return i.used?(Object.getPrototypeOf(n)!==Object.getPrototypeOf(l)&&(f=r,n=l,J.set(e,{target:n}),l=t()),Object.assign(n,l)):(i.target=l,l)},B[28].handlesRead=!0,B[29]=t=>{let e=J.get(t);return e.used=!0,e.target},B[258]=t=>new Set(t),(B[259]=t=>(k.mapsAsObjects&&(k.mapsAsObjects=!1,xe=!0),t())).handlesRead=!0;function he(t,e){return typeof t=="string"?t+e:t instanceof Array?t.concat(e):Object.assign({},t,e)}function ce(){if(!K)if(k.getShared)rt();else throw new Error("No packed values available");return K}const Ps=1399353956;Qe.push((t,e)=>{if(t>=225&&t<=255)return he(ce().prefixes[t-224],e);if(t>=28704&&t<=32767)return he(ce().prefixes[t-28672],e);if(t>=1879052288&&t<=2147483647)return he(ce().prefixes[t-1879048192],e);if(t>=216&&t<=223)return he(e,ce().suffixes[t-216]);if(t>=27647&&t<=28671)return he(e,ce().suffixes[t-27639]);if(t>=1811940352&&t<=1879048191)return he(e,ce().suffixes[t-1811939328]);if(t==Ps)return{packedValues:K,structures:_.slice(0),version:e};if(t==55799)return e});const Cs=new Uint8Array(new Uint16Array([1]).buffer)[0]==1,qt=[Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?{name:"BigUint64Array"}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?{name:"BigInt64Array"}:BigInt64Array,Float32Array,Float64Array],Ls=[64,68,69,70,71,72,77,78,79,85,86];for(let t=0;t<qt.length;t++)Ns(qt[t],Ls[t]);function Ns(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;B[n?e:e-4]=s==1||n==Cs?l=>{if(!t)throw new Error("Could not find typed array for code "+e);return!k.copyBuffers&&(s===1||s===2&&!(l.byteOffset&1)||s===4&&!(l.byteOffset&3)||s===8&&!(l.byteOffset&7))?new t(l.buffer,l.byteOffset,l.byteLength>>i):new t(Uint8Array.prototype.slice.call(l,0).buffer)}:l=>{if(!t)throw new Error("Could not find typed array for code "+e);let y=new DataView(l.buffer,l.byteOffset,l.byteLength),p=l.length>>i,v=new t(p),m=y[r];for(let T=0;T<p;T++)v[T]=m.call(y,T<<i,n);return v}}}function _s(){let t=pe(),e=f+I();for(let s=2;s<t;s++){let n=pe();f+=n}let r=f;return f=e,D=[Ce(pe()),Ce(pe())],D.position0=0,D.position1=0,D.postBundlePosition=f,f=r,I()}function pe(){if(!(f<V))throw oe();let t=w[f++]&31;if(t>23)switch(t){case 24:if(f>=V)throw oe();t=w[f++];break;case 25:t=$.getUint16(f),f+=2;break;case 26:t=$.getUint32(f),f+=4;break}return t}function rt(){if(k.getShared){let t=Jt(()=>(w=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 Jt(t){let e=V,r=f,s=Se,n=Ae,i=ie,l=ve,y=ne,p=J,v=D,m=new Uint8Array(w.slice(0,V)),T=_,M=k,L=ke,C=t();return V=e,f=r,Se=s,Ae=n,ie=i,ve=l,ne=y,J=p,D=v,w=m,ke=L,_=T,k=M,$=new DataView(w.buffer,w.byteOffset,w.byteLength),C}function st(){w=null,J=null,_=null}const nt=new Array(147);for(let t=0;t<256;t++)nt[t]=+("1e"+Math.floor(45.15-t*.30103));let it=new ye({useRecords:!1});const Ds=it.decode;it.decodeMultiple;let Ne;try{Ne=new TextEncoder}catch{}let ot,Wt;const _e=typeof globalThis=="object"&&globalThis.Buffer,Ie=typeof _e<"u",at=Ie?_e.allocUnsafeSlow:Uint8Array,Gt=Ie?_e:Uint8Array,Ht=256,Zt=Ie?4294967296:2144337920;let ct,c,U,o=0,ee,F=null;const Bs=61440,Fs=/[\u0080-\uFFFF]/,q=Symbol("record-id");class Xt extends ye{constructor(e){super(e),this.offset=0;let r,s,n,i,l;e=e||{};let y=Gt.prototype.utf8Write?function(a,b){return c.utf8Write(a,b,c.byteLength-b)}:Ne&&Ne.encodeInto?function(a,b){return Ne.encodeInto(a,c.subarray(b)).written}:!1,p=this,v=e.structures||e.saveStructures,m=e.maxSharedStructures;if(m==null&&(m=v?128:0),m>8190)throw new Error("Maximum maxSharedStructure is 8190");let T=e.sequential;T&&(m=0),this.structures||(this.structures=[]),this.saveStructures&&(this.saveShared=this.saveStructures);let M,L,C=e.sharedValues,P;if(C){P=Object.create(null);for(let a=0,b=C.length;a<b;a++)P[C[a]]=a}let G=[],we=0,H=0;this.mapEncode=function(a,b){return this._keyMap&&!this._mapped&&a.constructor.name==="Array"&&(a=a.map(d=>this.encodeKeys(d))),this.encode(a,b)},this.encode=function(a,b){if(c||(c=new at(8192),U=new DataView(c.buffer,0,8192),o=0),ee=c.length-10,ee-o<2048?(c=new at(c.length),U=new DataView(c.buffer,0,c.length),ee=c.length-10,o=0):b===tr&&(o=o+7&2147483640),r=o,p.useSelfDescribedHeader&&(U.setUint32(o,3654940416),o+=3),l=p.structuredClone?new Map:null,p.bundleStrings&&typeof a!="string"?(F=[],F.size=1/0):F=null,s=p.structures,s){if(s.uninitialized){let h=p.getShared()||{};p.structures=s=h.structures||[],p.sharedVersion=h.version;let u=p.sharedValues=h.packedValues;if(u){P={};for(let g=0,E=u.length;g<E;g++)P[u[g]]=g}}let d=s.length;if(d>m&&!T&&(d=m),!s.transitions){s.transitions=Object.create(null);for(let h=0;h<d;h++){let u=s[h];if(!u)continue;let g,E=s.transitions;for(let S=0,A=u.length;S<A;S++){E[q]===void 0&&(E[q]=h);let O=u[S];g=E[O],g||(g=E[O]=Object.create(null)),E=g}E[q]=h|1048576}}T||(s.nextId=d)}if(n&&(n=!1),i=s||[],L=P,e.pack){let d=new Map;if(d.values=[],d.encoder=p,d.maxValues=e.maxPrivatePackedValues||(P?16:1/0),d.objectMap=P||!1,d.samplingPackedValues=M,De(a,d),d.values.length>0){c[o++]=216,c[o++]=51,Z(4);let h=d.values;x(h),Z(0),Z(0),L=Object.create(P||null);for(let u=0,g=h.length;u<g;u++)L[h[u]]=u}}ct=b&dt;try{if(ct)return;if(x(a),F&&er(r,x),p.offset=o,l&&l.idsToInsert){o+=l.idsToInsert.length*2,o>ee&&fe(o),p.offset=o;let d=$s(c.subarray(r,o),l.idsToInsert);return l=null,d}return b&tr?(c.start=r,c.end=o,c):c.subarray(r,o)}finally{if(s){if(H<10&&H++,s.length>m&&(s.length=m),we>1e4)s.transitions=null,H=0,we=0,G.length>0&&(G=[]);else if(G.length>0&&!T){for(let d=0,h=G.length;d<h;d++)G[d][q]=void 0;G=[]}}if(n&&p.saveShared){p.structures.length>m&&(p.structures=p.structures.slice(0,m));let d=c.subarray(r,o);return p.updateSharedData()===!1?p.encode(a):d}b&Ks&&(o=r)}},this.findCommonStringsToPack=()=>(M=new Map,P||(P=Object.create(null)),a=>{let b=a&&a.threshold||4,d=this.pack?a.maxPrivatePackedValues||16:0;C||(C=this.sharedValues=[]);for(let[h,u]of M)u.count>b&&(P[h]=d++,C.push(h),n=!0);for(;this.saveShared&&this.updateSharedData()===!1;);M=null});const x=a=>{o>ee&&(c=fe(o));var b=typeof a,d;if(b==="string"){if(L){let E=L[a];if(E>=0){E<16?c[o++]=E+224:(c[o++]=198,E&1?x(15-E>>1):x(E-16>>1));return}else if(M&&!e.pack){let S=M.get(a);S?S.count++:M.set(a,{count:1})}}let h=a.length;if(F&&h>=4&&h<1024){if((F.size+=h)>Bs){let S,A=(F[0]?F[0].length*3+F[1].length:0)+10;o+A>ee&&(c=fe(o+A)),c[o++]=217,c[o++]=223,c[o++]=249,c[o++]=F.position?132:130,c[o++]=26,S=o-r,o+=4,F.position&&er(r,x),F=["",""],F.size=0,F.position=S}let E=Fs.test(a);F[E?0:1]+=a,c[o++]=E?206:207,x(h);return}let u;h<32?u=1:h<256?u=2:h<65536?u=3:u=5;let g=h*3;if(o+g>ee&&(c=fe(o+g)),h<64||!y){let E,S,A,O=o+u;for(E=0;E<h;E++)S=a.charCodeAt(E),S<128?c[O++]=S:S<2048?(c[O++]=S>>6|192,c[O++]=S&63|128):(S&64512)===55296&&((A=a.charCodeAt(E+1))&64512)===56320?(S=65536+((S&1023)<<10)+(A&1023),E++,c[O++]=S>>18|240,c[O++]=S>>12&63|128,c[O++]=S>>6&63|128,c[O++]=S&63|128):(c[O++]=S>>12|224,c[O++]=S>>6&63|128,c[O++]=S&63|128);d=O-o-u}else d=y(a,o+u,g);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,U.setUint32(o,d),o+=4),o+=d}else if(b==="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,U.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,U.setUint16(o,~a),o+=2):(c[o++]=58,U.setUint32(o,~a),o+=4);else if(!this.alwaysUseFloat&&a<0&&a>=-4294967296&&Math.floor(a)===a)c[o++]=58,U.setUint32(o,-1-a),o+=4;else{let h;if((h=this.useFloat32)>0&&a<4294967296&&a>=-2147483648){c[o++]=250,U.setFloat32(o,a);let u;if(h<4||(u=a*nt[(c[o]&127)<<1|c[o+1]>>7])>>0===u){o+=4;return}else o--}c[o++]=251,U.setFloat64(o,a),o+=8}else if(b==="object")if(!a)c[o++]=246;else{if(l){let u=l.get(a);if(u){if(c[o++]=216,c[o++]=29,c[o++]=25,!u.references){let g=l.idsToInsert||(l.idsToInsert=[]);u.references=[],g.push(u)}u.references.push(o-r),o+=2;return}else l.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]])])),j(a);else if(h===Array){d=a.length,d<24?c[o++]=128|d:Z(d);for(let u=0;u<d;u++)x(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,U.setUint32(o,d),o+=4),p.keyMap)for(let[u,g]of a)x(p.encodeKey(u)),x(g);else for(let[u,g]of a)x(u),x(g);else{for(let u=0,g=ot.length;u<g;u++){let E=Wt[u];if(a instanceof E){let S=ot[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,U.setUint32(o,A),o+=4),S.encode.call(this,a,x,fe);return}}if(a[Symbol.iterator]){if(ct){let u=new Error("Iterable should be serialized as iterator");throw u.iteratorNotHandled=!0,u}c[o++]=159;for(let u of a)x(u);c[o++]=255;return}if(a[Symbol.asyncIterator]||lt(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 x(u)}j(a)}}else if(b==="boolean")c[o++]=a?245:244;else if(b==="bigint"){if(a<BigInt(1)<<BigInt(64)&&a>=0)c[o++]=27,U.setBigUint64(o,a);else if(a>-(BigInt(1)<<BigInt(64))&&a<0)c[o++]=59,U.setBigUint64(o,-a-BigInt(1));else if(this.largeBigIntToFloat)c[o++]=251,U.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);ft(new Uint8Array(h.reverse()),fe);return}o+=8}else if(b==="undefined")c[o++]=247;else throw new Error("Unknown type: "+b)},j=this.useRecords===!1?this.variableMapSize?a=>{let b=Object.keys(a),d=Object.values(a),h=b.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,U.setUint32(o,h),o+=4),p.keyMap)for(let u=0;u<h;u++)x(p.encodeKey(b[u])),x(d[u]);else for(let u=0;u<h;u++)x(b[u]),x(d[u])}:a=>{c[o++]=185;let b=o-r;o+=2;let d=0;if(p.keyMap)for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(x(p.encodeKey(h)),x(a[h]),d++);else for(let h in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(h))&&(x(h),x(a[h]),d++);c[b+++r]=d>>8,c[b+r]=d&255}:(a,b)=>{let d,h=i.transitions||(i.transitions=Object.create(null)),u=0,g=0,E,S;if(this.keyMap){S=Object.keys(a).map(O=>this.encodeKey(O)),g=S.length;for(let O=0;O<g;O++){let vt=S[O];d=h[vt],d||(d=h[vt]=Object.create(null),u++),h=d}}else for(let O in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(O))&&(d=h[O],d||(h[q]&1048576&&(E=h[q]&65535),d=h[O]=Object.create(null),u++),h=d,g++);let A=h[q];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>=Ht&&(i.nextId=(A=m)+1)):A=E,i[A]=S,A<m){c[o++]=217,c[o++]=A>>8|224,c[o++]=A&255,h=i.transitions;for(let O=0;O<g;O++)(h[q]===void 0||h[q]&1048576)&&(h[q]=A),h=h[S[O]];h[q]=A|1048576,n=!0}else{if(h[q]=A,U.setUint32(o,3655335680),o+=3,u&&(we+=H*u),G.length>=Ht-m&&(G.shift()[q]=void 0),G.push(h),Z(g+2),x(57344+A),x(S),b)return;for(let O in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(O))&&x(a[O]);return}if(g<24?c[o++]=128|g:Z(g),!b)for(let O in a)(typeof a.hasOwnProperty!="function"||a.hasOwnProperty(O))&&x(a[O])},fe=a=>{let b;if(a>16777216){if(a-r>Zt)throw new Error("Encoded buffer would be larger than maximum buffer size");b=Math.min(Zt,Math.round(Math.max((a-r)*(a>67108864?1.25:2),4194304)/4096)*4096)}else b=(Math.max(a-r<<2,c.length-1)>>12)+1<<12;let d=new at(b);return U=new DataView(d.buffer,0,b),c.copy?c.copy(d,0,r,a):d.set(c.slice(r,a)),o-=r,r=0,ee=d.length-10,c=d};let te=100,mt=1e3;this.encodeAsIterable=function(a,b){return Et(a,b,ue)},this.encodeAsAsyncIterable=function(a,b){return Et(a,b,St)};function*ue(a,b,d){let h=a.constructor;if(h===Object){let u=p.useRecords!==!1;u?j(a,!0):Yt(Object.keys(a).length,160);for(let g in a){let E=a[g];u||x(g),E&&typeof E=="object"?b[g]?yield*ue(E,b[g]):yield*ze(E,b,g):x(E)}}else if(h===Array){let u=a.length;Z(u);for(let g=0;g<u;g++){let E=a[g];E&&(typeof E=="object"||o-r>te)?b.element?yield*ue(E,b.element):yield*ze(E,b,"element"):x(E)}}else if(a[Symbol.iterator]&&!a.buffer){c[o++]=159;for(let u of a)u&&(typeof u=="object"||o-r>te)?b.element?yield*ue(u,b.element):yield*ze(u,b,"element"):x(u);c[o++]=255}else lt(a)?(Yt(a.size,64),yield c.subarray(r,o),yield a,be()):a[Symbol.asyncIterator]?(c[o++]=159,yield c.subarray(r,o),yield a,be(),c[o++]=255):x(a);d&&o>r?yield c.subarray(r,o):o-r>te&&(yield c.subarray(r,o),be())}function*ze(a,b,d){let h=o-r;try{x(a),o-r>te&&(yield c.subarray(r,o),be())}catch(u){if(u.iteratorNotHandled)b[d]={},o=r+h,yield*ue.call(this,a,b[d]);else throw u}}function be(){te=mt,p.encode(null,dt)}function Et(a,b,d){return b&&b.chunkThreshold?te=mt=b.chunkThreshold:te=100,a&&typeof a=="object"?(p.encode(null,dt),d(a,p.iterateProperties||(p.iterateProperties={}),!0)):[p.encode(a)]}async function*St(a,b){for(let d of ue(a,b,!0)){let h=d.constructor;if(h===Gt||h===Uint8Array)yield d;else if(lt(d)){let u=d.stream().getReader(),g;for(;!(g=await u.read()).done;)yield g.value}else if(d[Symbol.asyncIterator])for await(let u of d)be(),u?yield*St(u,b.async||(b.async={})):yield p.encode(u);else yield d}}}useBuffer(e){c=e,U=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 Qt(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,l)=>this.structures[l]=i),n}}function Yt(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,U.setUint32(o,t),o+=4)}class Qt{constructor(e,r,s){this.structures=e,this.packedValues=r,this.version=s}}function Z(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,U.setUint32(o,t),o+=4)}const zs=typeof Blob>"u"?function(){}:Blob;function lt(t){if(t instanceof zs)return!0;let e=t[Symbol.toStringTag];return e==="Blob"||e==="File"}function De(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++)De(t[s],e);else{let s=!e.encoder.useRecords;for(var r in t)t.hasOwnProperty(r)&&(s&&De(r,e),De(t[r],e))}break;case"function":console.log(t)}}const js=new Uint8Array(new Uint16Array([1]).buffer)[0]==1;Wt=[Date,Set,Error,RegExp,ae,ArrayBuffer,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,typeof BigUint64Array>"u"?function(){}:BigUint64Array,Int8Array,Int16Array,Int32Array,typeof BigInt64Array>"u"?function(){}:BigInt64Array,Float32Array,Float64Array,Qt],ot=[{tag:1,encode(t,e){let r=t.getTime()/1e3;(this.useTimestamp32||t.getMilliseconds()===0)&&r>=0&&r<4294967296?(c[o++]=26,U.setUint32(o,r),o+=4):(c[o++]=251,U.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){ft(t,r)}},{getTag(t){if(t.constructor===Uint8Array&&(this.tagUint8Array||Ie&&this.tagUint8Array!==!1))return 64},encode(t,e,r){ft(t,r)}},X(68,1),X(69,2),X(70,4),X(71,8),X(72,1),X(77,2),X(78,4),X(79,8),X(85,4),X(86,8),{encode(t,e){let r=t.packedValues||[],s=t.structures||[];if(r.values.length>0){c[o++]=216,c[o++]=51,Z(4);let n=r.values;e(n),Z(0),Z(0),packedObjectMap=Object.create(sharedPackedObjectMap||null);for(let i=0,l=n.length;i<l;i++)packedObjectMap[n[i]]=i}if(s){U.setUint32(o,3655335424),o+=3;let n=s.slice(0);n.unshift(57344),n.push(new ae(t.version,1399353956)),e(n)}else e(new ae(t.version,1399353956))}}];function X(t,e){return!js&&e>1&&(t-=4),{tag:t,encode:function(s,n){let i=s.byteLength,l=s.byteOffset||0,y=s.buffer||s;n(Ie?_e.from(y,l,i):new Uint8Array(y,l,i))}}}function ft(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,U.setUint32(o,r),o+=4),o+r>=c.length&&e(o+r),c.set(t.buffer?t:new Uint8Array(t),o),o+=r}function $s(t,e){let r,s=e.length*2,n=t.length-s;e.sort((i,l)=>i.offset>l.offset?1:-1);for(let i=0;i<e.length;i++){let l=e[i];l.id=i;for(let y of l.references)t[y++]=i>>8,t[y]=i&255}for(;r=e.pop();){let i=r.offset;t.copyWithin(i+s,i,n),s-=2;let l=i+s;t[l++]=216,t[l++]=28,n=i}return t}function er(t,e){U.setUint32(F.position+t,o-F.position-t+1);let r=F;F=null,e(r[0]),e(r[1])}let ut=new Xt({useRecords:!1});const Vs=ut.encode;ut.encodeAsIterable,ut.encodeAsAsyncIterable;const tr=512,Ks=1024,dt=2048;class qs extends kr{constructor(e){e||(e={}),e.objectMode=!0,super(e),e.structures=[],this.decoder=e.decoder||new ye(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 Js=process.env.CBOR_NATIVE_ACCELERATION_DISABLED!==void 0&&process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase()==="true";if(!Js){let t;try{typeof require=="function"?t=require("cbor-extract"):t=Ir(import.meta.url)("cbor-extract"),t&&Os(t.extractStrings)}catch{}}const Ws={dateStyle:"medium",timeStyle:"short"},Gs=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;function Be(t){return Array.isArray(t)?t.map(Be).join(", "):typeof t=="boolean"?t?"yes":"no":typeof t=="number"?t.toLocaleString():Hs(t)}function Hs(t){if(Gs.test(t)){const r=new Date(t);if(!Number.isNaN(r.getTime()))return r.toLocaleString(void 0,Ws)}const e=xt();return t===e?"~":t.startsWith(`${e}${xr}`)?`~${t.slice(e.length)}`:t}function rr(){return process.env.APPETIZE_CLI_HOME??re(xt(),".appetize","cli","sessions")}function ht(t){return re(rr(),t)}class Fe 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 Te extends Fe{sessionDir;constructor(e,{cause:r,details:s={},sessionId:n}){const i=ht(n);super(e,Object.assign({sessionDir:i},s),r),this.name="SessionError",this.sessionDir=i}static wrap(e,r){if(e instanceof Te||e instanceof le)return e;const s=e instanceof Error?e.message:String(e),n=e instanceof Fe?e.details:{};return new Te(s,{cause:e,details:n,sessionId:r})}hints(){return[`Check the session logs for details: ${Be(this.sessionDir)}`]}}class Zs extends Te{constructor(e){super(`Session ${e} already exists`,{sessionId:e}),this.name="SessionAlreadyExistsError"}hints(){return[`Pass --force to replace it, deleting ${Be(this.sessionDir)}`]}}class le extends Fe{#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 Xs(t,e,r){const s=e instanceof Error?e.message:String(e);if(!(e instanceof Fe))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 pt(t){try{return process.kill(t,0),!0}catch{return!1}}const Ys=["SIGINT","SIGTERM"],Qs=150;function en(t){const e=new AbortController,r=setInterval(()=>{pt(t)||(clearInterval(r),e.abort(new Error("The device session exited")))},Qs);return r.unref(),e.signal}function sr(){const t=new AbortController;for(const e of Ys)process.once(e,()=>{t.abort(new Error(`Received ${e}`))});return t.signal}function tn(t,e){return new Promise((r,s)=>{if(e.aborted){s(nr(e.reason));return}const n=()=>{clearTimeout(i),s(nr(e.reason))},i=setTimeout(()=>{e.removeEventListener("abort",n),r()},t);e.addEventListener("abort",n,{once:!0})})}function nr(t){return t instanceof Error?t:new Error(String(t))}function rn(t){const e=t.at(-1);return{options:on(e)?e:{},positionals:t.slice(0,-1).filter(r=>typeof r=="string"),signal:sr()}}function sn(t,e){const r=ir(t,e);return r===void 0?void 0:or(r,e)}function ir(t,e){if(t===void 0)return;const r=typeof t=="boolean"?Number.NaN:Number(t);if(!Number.isFinite(r))throw new le(`${e} expects a number, e.g., ${e}=-0.1`);return r}function yt(t,e){if(t!==void 0){if(typeof t!="string"||t==="")throw new le(`${e} needs a value`);return t}}function or(t,e){if(!(t>=0&&t<=1))throw new le(`${e} expects a number between 0 and 1`);return t}function nn(t,e){const r=yt(t,e);if(r===void 0)throw new le(`${e} needs a value`);return r}function on(t){return typeof t=="object"&&t!==null}const an="APPETIZEIO_FEATURE_FLAGS";function cn(){const t={};for(const e of(process.env[an]??"").split(",")){const r=e.trim();r!==""&&(t[r]=!0)}return t}function ln(){const t=process.env.APPETIZE_API_TOKEN;if(!t)throw new Error("APPETIZE_API_TOKEN must be set");return t}function ar(t){return yt(t.endpoint,"--endpoint")??fn()}function fn(){const t=process.env.APPETIZE_ENDPOINT?.trim();return t===""?void 0:t}const un="https://appetize.io";async function cr(t,{authorize:e=!0,commandOptions:r={},query:s={},...n}={}){const i=new URL(t,ar(r)??un);for(const[p,v]of Object.entries(s))i.searchParams.set(p,v);const l=new Headers(n.headers);e&&l.set("x-api-key",ln());const y=await fetch(i,{...n,headers:l});if(!y.ok)throw new Error(`The webserver rejected the request (${String(y.status)})`);return await y.json()}const lr=["android","ios"];function fr(t={},e){return cr("/api/v2/service/devices",{authorize:!1,commandOptions:t,signal:e})}function dn(t){return lr.includes(t)}async function hn(t,e={}){return(await fr(e)).find(s=>s.id===t)?.platform}function pn(t){let e={...t,updatedAt:new Date().toISOString()};return{path:bt(e.sessionId),save:r=>(e={...e,...r,updatedAt:new Date().toISOString()},vn(e),e)}}function ur(t){const e=t.state?.phase;return e==="ended"||e==="error"?!1:pt(t.pid)}function yn(){const t=rr();return At(t,{recursive:!0}),Ar(t).map(e=>wt(e)).filter(e=>e!==void 0)}function wn(t){if(t!==void 0){const r=wt(t);if(!r)throw new Error(`No session found for id ${t}`);return r}const e=yn().filter(ur);if(e.length===0)throw new Error("No active sessions");if(e.length>1)throw new le("There are multiple active sessions",["Specify --session-id to pick one"]);return e[0]}function wt(t){try{return JSON.parse(Sr(bt(t),"utf8"))}catch{return}}function bn(t){vr(ht(t),{force:!0,recursive:!0})}function gn(t){return re(Oe(t),"control.sock")}function dr(t){return re(Oe(t),"daemon.log")}function mn(t){return re(Oe(t),"device.log.jsonl")}function En(t,e){return{daemon:dr(t),device:mn(t),...e==="intercept"?{network:Sn(t)}:void 0}}function Sn(t){return re(Oe(t),"network.jsonl")}function Oe(t){const e=ht(t);return At(e,{recursive:!0}),e}function bt(t){return re(Oe(t),"state.json")}function vn(t){const e=bt(t.sessionId),r=`${e}.tmp`;mr(r,`${JSON.stringify(t,null,2)}
|
|
3
|
-
`),Er(r,e)}const gt="APPETIZE_DAEMON_CONFIG";function An(){const t=process.env[gt];if(!t)throw new Error(`${gt} is not set`);return JSON.parse(t)}function xn(t){return JSON.stringify(t)}function kn(){let t,e;return{promise:new Promise((s,n)=>{e=s,t=n}),reject:t,resolve:e}}const hr={useRecords:!1},In=new Xt(hr);class pr extends ge{#e=[];#t;constructor(e,{signal:r}={}){super(async l=>{this.#t.write(In.encode(l))},{signal:r}),this.#t=e;const s=new qs(hr);s.on("data",l=>{Je(l)&&this[R].dispatchMessage(l)}),s.on("error",()=>{this.close()});const n=l=>{s.write(l)},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 On(t,e){const{promise:r,reject:s,resolve:n}=kn(),i=Or(t),l=e?.signal,y=()=>{i.off("connect",m),i.off("error",T),l?.removeEventListener("abort",v)},p=M=>{y(),i.destroy(),s(M)},v=()=>{p(new Error("The connection was aborted"))},m=()=>{y();const M=new pr(i,e);M[R].addEventListener("close",()=>{i.destroy()},{once:!0}),n(M)},T=M=>{p(M)};return l?.aborted?(p(new Error("The connection was aborted")),r):(i.once("connect",m),i.once("error",T),l?.addEventListener("abort",v,{once:!0}),r)}export{wn as A,rn as B,Xs as C,gt as D,Ke as E,Vs as F,R as G,Ds as H,Je as I,Nt as J,Ue as K,Ve as L,pr as M,An as N,pn as O,hn as P,gn as Q,sr as R,Te as S,ge as T,le as U,ir as a,fr as b,lr as c,tn as d,Ct as e,cr as f,nn as g,dr as h,dn as i,ur as j,pt as k,en as l,cn as m,ar as n,wt as o,yt as p,Zs as q,or as r,xn as s,bn as t,En as u,sn as v,Be as w,On as x,Ss as y,It as z};
|