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 • 3.5 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{St as t}from"./sdk-io.js";import"./diagnostics.js";import{f as n,p as r,u as i}from"./daemon-http.js";import{c as a}from"./transport.js";import o from"node:fs";import s from"node:path";import{Transform as c}from"node:stream";import{pipeline as l}from"node:stream/promises";const u=o.constants.O_WRONLY|o.constants.O_CREAT|o.constants.O_EXCL|(o.constants.O_NOFOLLOW??0);function d(e){let{destinationPath:t}=e;return e.isDirectory?{prepare:async()=>{await o.promises.mkdir(t,{recursive:!0})},cleanupOnError:async()=>{},materialize:(e,n)=>m(e,t,n)}:{prepare:async()=>{e.privateFile||await o.promises.mkdir(s.dirname(t),{recursive:!0})},cleanupOnError:()=>o.promises.rm(t,{force:!0}),materialize:async(n,r)=>{let{privateFile:i}=e;if(!i)return await l(n,o.createWriteStream(t),{signal:r}),t;let a=await o.promises.open(t,u,384).catch(e=>{throw n.destroy(),e}),s=f(i.maxBytes,e.artifactId);return await l(n,s,a.createWriteStream(),{signal:r}),t}}}function f(t,n){let r=0;return new c({transform(i,a,o){if(r+=i.length,r<=t){o(null,i);return}o(new e(`COMMAND_FAILED`,`The remote artifact is larger than ${t} bytes.`,{reason:`artifact_too_large`,artifactId:n,maxBytes:t}))}})}async function p(o){let s=await a(o.artifactUrl.protocol),c=d(o);return await c.prepare(),await new Promise((a,l)=>{let u=!1,d=!1,f,p=new AbortController,m=o.timeoutMs??9e4,h=(e,n)=>{if(!u){if(u=!0,clearTimeout(_),n){c.cleanupOnError().catch(e=>t({level:`warn`,phase:`remote_artifact_cleanup_failed`,data:{artifactId:o.artifactId,error:String(e)}})).finally(()=>l(n));return}a(e??o.destinationPath)}},g=s.request({protocol:o.artifactUrl.protocol,host:o.artifactUrl.hostname,port:o.artifactUrl.port,method:`GET`,path:o.artifactUrl.pathname+o.artifactUrl.search,headers:{...i(o.token),...r(o.requestScope?.tenantId),...n(o.requestScope?.leaseId)}},t=>{if(t.statusCode!==200){let n=``;t.setEncoding(`utf8`),t.on(`data`,e=>{let t=65536-n.length;t>0&&(n+=e.slice(0,t))});let r=()=>{h(void 0,new e(`COMMAND_FAILED`,`Failed to download remote artifact`,{artifactId:o.artifactId,statusCode:t.statusCode,requestId:o.requestScope?.requestId,body:n}))};t.on(`end`,r),t.on(`error`,r),t.on(`aborted`,r);return}d=!0,c.materialize(t,p.signal).then(e=>h(e),e=>h(void 0,f??(e instanceof Error?e:Error(String(e)))))}),_=setTimeout(()=>{f=new e(`COMMAND_FAILED`,`Remote artifact download timed out`,{artifactId:o.artifactId,requestId:o.requestScope?.requestId,timeoutMs:m}),p.abort(f),g.destroy(f),d||h(void 0,f)},m);g.on(`error`,t=>{if(!d){if(t instanceof e){h(void 0,f??t);return}h(void 0,new e(`COMMAND_FAILED`,`Failed to download remote artifact`,{artifactId:o.artifactId,requestId:o.requestScope?.requestId,timeoutMs:m},t instanceof Error?t:void 0))}}),g.end()})}async function m(t,n,r){let i=await o.promises.mkdtemp(s.join(n,`.agent-device-download-`)),a=s.join(i,`artifact.tar.gz`),c=s.join(i,`extracted`);try{await l(t,o.createWriteStream(a),{signal:r});let{extractArchiveSafely:i}=await import(`./archive.js`).then(e=>e.t);await i({archivePath:a,outputRoot:c,type:`tgz`,signal:r}),r.throwIfAborted();let[u,...d]=await o.promises.readdir(c);if(!u||d.length>0)throw new e(`COMMAND_FAILED`,`Downloaded directory artifact has an unexpected shape: expected exactly one top-level entry, found ${d.length+ +!!u}`);r.throwIfAborted();let f=s.join(n,u);return await o.promises.rename(s.join(c,u),f),f}finally{await o.promises.rm(i,{recursive:!0,force:!0})}}export{p as downloadRemoteArtifactFromUrl};