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 • 4.15 kB
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import{r as n,t as r}from"./png.js";import{t as i}from"./png-encode.js";import a from"node:zlib";const o=[137,80,78,71,13,10,26,10],s=[`IHDR`,`PLTE`,`IDAT`,`IEND`],c=/^[A-Za-z]{4}$/;function l(e){return!c.test(e)||e[0]<=`Z`&&!s.includes(e)}function u(e){return o.every((t,n)=>e[n]===t)}function d(e){if(e.length<33||!u(e))return null;let t=Buffer.from(e.buffer,e.byteOffset,e.byteLength);if(t.readUInt32BE(8)!==13||t.toString(`ascii`,12,16)!==`IHDR`)return null;let n=t.subarray(12,29);return t.readUInt32BE(29)===a.crc32(n)>>>0?{width:t.readUInt32BE(16),height:t.readUInt32BE(20),bitDepth:e[24],colorType:e[25],compressionMethod:e[26],filterMethod:e[27],interlace:e[28]}:null}function f(e){return e===2?3:e===6?4:null}function p(e){if(!u(e))return null;let t=Buffer.from(e.buffer,e.byteOffset,e.byteLength),n=[],r=8;for(;r+12<=e.length;){let i=t.readUInt32BE(r);if(i>2147483647||r+12+i>e.length)return null;let o=t.subarray(r+4,r+8+i);if(t.readUInt32BE(r+8+i)!==a.crc32(o)>>>0)return null;let s=t.toString(`ascii`,r+4,r+8);if(l(s))return null;if(n.push({type:s,data:t.subarray(r+8,r+8+i)}),s===`IEND`)return r+12+i===e.length?n:null;r+=12+i}return null}const m=[0,1,2,3,4];function h(e,t,n,r){switch(e){case 1:return t;case 2:return n;case 3:return t+n>>1;case 4:return g(t,n,r);default:return 0}}function g(e,t,n){let r=e+t-n,i=Math.abs(r-e),a=Math.abs(r-t),o=Math.abs(r-n);return i<=a&&i<=o?e:a<=o?t:n}function _(e,t){return e+t&255}function v(e){let t=d(e);return t===null||t.bitDepth!==8||t.compressionMethod!==0||t.filterMethod!==0||t.interlace!==0||f(t.colorType)===null?null:t}function y(e,t,n){let r=f(t.colorType);if(r===null)return null;let i=D(e);if(i===null)return null;let a=t.width*r,o=t.height*(a+1),s=E(i,o);if(s===null||s.length<o)return null;for(let e=0;e<n.y+n.height;e+=1)if(!b(s,e,a,r))return null;if(!C(s,n.y+n.height,t.height,a))return null;let c=r===4&&!w(s,n,a)?4:3;return{width:n.width,height:n.height,channels:c,pixels:T(s,n,a,r,c)}}function b(e,t,n,r){let i=t*(n+1)+1,a=e[i-1];return m.includes(a)?a===0?!0:t===0?x(e,i,n,r,a):S(e,i,n,r,a):!1}function x(e,t,n,r,i){let a=t+n;if(i===1||i===4){for(let n=t+r;n<a;n+=1)e[n]=_(e[n],e[n-r]);return!0}for(let n=t;n<a;n+=1){let a=n>=t+r?e[n-r]:0;e[n]=_(e[n],h(i,a,0,0))}return!0}function S(e,t,n,r,i){let a=t+n,o=t-(n+1);if(i===1){for(let n=t+r;n<a;n+=1)e[n]=_(e[n],e[n-r]);return!0}if(i===2){for(let n=t;n<a;n+=1)e[n]=_(e[n],e[o+(n-t)]);return!0}for(let n=t;n<a;n+=1){let a=n-t,s=n>=t+r?e[n-r]:0;e[n]=_(e[n],h(i,s,e[o+a],n>=t+r?e[o+a-r]:0))}return!0}function C(e,t,n,r){for(let i=t;i<n;i+=1)if(!m.includes(e[i*(r+1)]))return!1;return!0}function w(e,t,n){let r=255;for(let i=t.y;i<t.y+t.height&&r===255;i+=1){let a=i*(n+1)+4;for(let n=0;n<t.width;n+=1)r&=e[a+(t.x+n)*4]}return r===255}function T(e,t,n,r,i){let a=t.width*i,o=new Uint8Array(t.height*a);for(let s=0;s<t.height;s+=1){let c=(s+t.y)*(n+1)+1+t.x*r,l=s*a;if(r===i)o.set(e.subarray(c,c+t.width*r),l);else for(let n=0;n<t.width;n+=1){let t=c+n*r,a=l+n*i;o[a]=e[t],o[a+1]=e[t+1],o[a+2]=e[t+2]}}return o}function E(e,t){try{return a.inflateSync(e,{maxOutputLength:t})}catch{return null}}function D(e){let t=p(e);if(t===null)return null;let n=[];for(let e of t){if(e.type===`PLTE`||e.type===`tRNS`)return null;e.type===`IDAT`&&n.push(e.data)}return n.length===0?null:Buffer.concat(n)}var O=e({cropPngBytes:()=>k});function k(e,t,n){let r=v(e);if(r!==null&&!N(t,r.width,r.height)){M(t,r.width,r.height);let n=y(e,r,t);if(n!==null)return i(n.pixels,n.width,n.height,n.channels)}return A(e,t,n)}function A(e,t,i){let a=r(e,i);return M(t,a.width,a.height),N(t,a.width,a.height)?null:n.PNG.sync.write(j(a,t))}function j(e,t){let r=new n.PNG({width:t.width,height:t.height});for(let n=0;n<t.height;n+=1){let i=((n+t.y)*e.width+t.x)*4;e.data.copy(r.data,n*r.width*4,i,i+t.width*4)}return r}function M(e,n,r){if(e.x+e.width>n||e.y+e.height>r)throw new t(`INVALID_ARGS`,`Screenshot crop box ${e.width}x${e.height} at (${e.x}, ${e.y}) exceeds the ${n}x${r} image`)}function N(e,t,n){return e.x===0&&e.y===0&&e.width===t&&e.height===n}export{O as n,k as t};