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 • 1.08 kB
import{b as e}from"./daemon-http.js";import{Readable as t}from"node:stream";import{pipeline as n}from"node:stream/promises";import{TLSSocket as r}from"node:tls";async function i(e,r,i){if(r.method===`TRACE`){a(i,404);return}let c=o(r);if(!c){a(i,400);return}let l=await e.handle(s(c,r,i));i.statusCode=l.status;for(let[e,t]of l.headers)i.setHeader(e,t);if(!l.body){i.end();return}await n(t.fromWeb(l.body),i)}function a(e,t){e.statusCode=t,e.end()}function o(t){return e(t.url,t.rawHeaders,t.socket instanceof r)}function s(e,n,r){let i=n.method??`GET`,a=i!==`GET`&&i!==`HEAD`;return new Request(e,{method:i,headers:c(n),signal:l(n,r),...a?{body:t.toWeb(n),duplex:`half`}:{}})}function c(e){let t=new Headers;for(let[n,r]of Object.entries(e.headers))if(r!==void 0)for(let e of Array.isArray(r)?r:[r])t.append(n,e);return t}function l(e,t){let n=new AbortController,r=()=>{t.writableFinished||n.signal.aborted||n.abort(Error(`Proxy client disconnected before the response ended`))};return e.on(`aborted`,r),t.on(`close`,r),t.closed&&r(),n.signal}export{i as serveProxyRequest};