UNPKG

agent-device

Version:

Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.

1 lines • 2.88 kB
import{v as e}from"./sdk-contracts.js";import{n as t,r as n}from"./install-source-network-transport.js";import{t as r}from"./undici.js";import i from"node:net";var a=r();const o=8388608,s=3e4;async function c(t,n){try{return{response:await l(JSON.parse(t),n)}}catch(t){if(!(t instanceof e))throw t;return{error:{code:t.code,message:t.message,details:t.details}}}}async function l(e,t){return e.publicNetworkOnly?await d(e,t):await u(e)}async function u(e){if(typeof fetch!=`function`)throw Error(`global fetch is required for Maestro runScript http helpers`);return await b(await fetch(e.url,{...f(e),signal:AbortSignal.timeout(s)}),o)}async function d(t,n){let r=y(t.url),i=AbortSignal.timeout(s),c=new p(r.protocol===`https:`,i,n);try{return await b(await(0,a.fetch)(r,{...f(t),dispatcher:c,signal:i}),o)}catch(t){throw t instanceof TypeError&&t.cause instanceof e?t.cause:t}finally{await c.dispose()}}function f(e){return{method:e.method,headers:e.headers,...e.body===void 0?{}:{body:e.body}}}var p=class extends a.Dispatcher{#e=new Set;#t;#n;#r;constructor(e,t,n){super(),this.#t=e,this.#n=t,this.#r=n}dispatch(e,t){return this.dispatchApproved(e,t).catch(e=>{t.onError?.(e instanceof Error?e:Error(String(e)))}),!0}async dispose(){await Promise.all([...this.#e].map(async e=>await e.close()))}async dispatchApproved(r,i){let a=v(r);if(this.#t&&a.protocol!==`https:`)throw new e(`COMMAND_FAILED`,`Maestro runScript HTTP redirect downgraded HTTPS`);a.protocol===`https:`&&(this.#t=!0);let o=await n(a,this.#n),s=t(a,this.#r),c=s?h(s,a):m(o.address,o.family);this.#e.add(c),c.dispatch(s?g(r,a,o.address,o.family):r,i)}};function m(e,t){return new a.Agent({connect:{lookup:(n,r,i)=>{if(r.all){i(null,[{address:e,family:t}]);return}i(null,e,t)}}})}function h(e,t){let n=_(t.hostname);return new a.ProxyAgent({uri:e,proxyTunnel:!0,requestTls:t.protocol===`https:`&&i.isIP(n)===0?{servername:n}:void 0})}function g(e,t,n,r){let i=new URL(t);i.hostname=r===6?`[${_(n)}]`:n;let a=Array.isArray(e.headers)?[...e.headers,`host`,t.host]:{...e.headers,host:t.host};return{...e,origin:i.origin,headers:a}}function _(e){return e.startsWith(`[`)&&e.endsWith(`]`)?e.slice(1,-1):e}function v(t){if(!t.origin)throw new e(`INVALID_ARGS`,`Invalid Maestro runScript HTTP URL`);return new URL(t.path,t.origin)}function y(t){try{return new URL(t)}catch{throw new e(`INVALID_ARGS`,`Invalid Maestro runScript HTTP URL`)}}async function b(e,t){return{status:e.status,body:await x(e,t),headers:Object.fromEntries(e.headers.entries())}}async function x(t,n){let r=[],i=0,a=t.body?.getReader();if(!a)return``;for(;;){let{done:o,value:s}=await a.read();if(o)break;if(i+=s.byteLength,i>n)throw await a.cancel(),new e(`COMMAND_FAILED`,`Maestro runScript HTTP response exceeded ${n} bytes`,{limitBytes:n,status:t.status});r.push(s)}return Buffer.concat(r,i).toString(`utf8`)}export{s as n,c as r,o as t};