var I=Object.create;var c=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var n=(t,e)=>c(t,"name",{value:e,configurable:!0});var R=(t,e)=>()=>(t&&(e=t(t=0)),e);var k=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var v=(t,e,i,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of B(e))!T.call(t,o)&&o!==i&&c(t,o,{get:()=>e[o],enumerable:!(r=N(e,o))||r.enumerable});return t};var F=(t,e,i)=>(i=t!=null?I(P(t)):{},v(e||!t||!t.__esModule?c(i,"default",{value:t,enumerable:!0}):i,t));var l=R(()=>{"use strict"});var S=k((M,E)=>{"use strict";l();E.exports={}});l();var m=F(S());import s from"lodash";import{serial as _}from"web-serial-polyfill";l();import V from"lodash";async function g(t){let e;await new Promise(i=>{e=setTimeout(i,t)}),e!==void 0&&(clearTimeout(e),e=void 0)}n(g,"sleep");var b=[{usbVendorId:26728,usbProductId:34438},{usbVendorId:6421,usbProductId:21023}];function C(t){return s.toUpper(`000${t.toString(16)}`.slice(-4))}n(C,"u16ToHex");var d=class{static{n(this,"WebserialAdapter")}#e=!1;#t=!1;name="adapter";port;#r;#i;#o;#n;ultra;constructor(){let e=globalThis?.navigator??{};this.#o=globalThis?.TransformStream??m.TransformStream,this.#n=globalThis?.WritableStream??m.WritableStream,this.#i=e.serial??("usb"in e?_:null),this.#r=i=>{this.ultra?.emitter.emit("error",s.set(new Error(i.message),"originalError",i))}}#a(e,...i){this.ultra?.emitter.emit("debug","webserial",e,...i)}async install(e,i){let r=this.ultra=e.ultra,o=e.Buffer;s.isNil(r.$adapter)||await r.disconnect(new Error("adapter replaced"));let a={isSupported:n(()=>!s.isNil(this.#i),"isSupported")};return r.addHook("connect",async(x,f)=>{if(r.$adapter!==a)return await f();try{if(!a.isSupported())throw new Error("WebSerial not supported");if(this.port=await this.#i.requestPort({filters:b}),s.isNil(this.port))throw new Error("user canceled");let p=await this.port.getInfo();for(this.#a(`port selected, usbVendorId = 0x${C(p.usbVendorId)}, usbProductId = 0x${C(p.usbProductId)}`),this.#e=s.isMatch(p,b[1]),await this.port.open({baudRate:115200});s.isNil(this.port.readable)||s.isNil(this.port.writable);)await g(10);return this.#t=!0,this.port.addEventListener("disconnect",()=>{r.disconnect(new Error("Webserial disconnect"))}),this.#e?r.port={isOpen:n(()=>this.#t,"isOpen"),isDfu:n(()=>this.#e,"isDfu"),isSlip:n(()=>!0,"isSlip"),readable:this.port.readable.pipeThrough(new this.#o(new h(o))),writable:new this.#n({write:n(async D=>{let u=this.port?.writable.getWriter();if(s.isNil(u))throw new Error("Failed to getWriter(). Did you remember to use adapter plugin?");await u.write($(D,o)),u.releaseLock()},"write")})}:r.port=s.merge(this.port,{isOpen:n(()=>this.#t,"isOpen"),isDfu:n(()=>this.#e,"isDfu")}),await f()}catch(p){throw this.#r(p),p}}),r.addHook("disconnect",async(x,f)=>{if(r.$adapter!==a||s.isNil(this.port))return await f();await f().catch(this.#r),await this.port.close().catch(this.#r),this.#t=!1,this.#e=!1,delete this.port}),a}};((globalThis??{}).ChameleonUltraJS??{}).WebserialAdapter=d;var h=class{static{n(this,"SlipDecodeTransformer")}#e=[];#t;constructor(e){this.#t=e}transform(e,i){this.#t.isBuffer(e)||(e=this.#t.fromView(e)),this.#e.push(e);let r=this.#t.concat(this.#e.splice(0,this.#e.length));try{for(;r.length>0;){let o=r.indexOf(192);if(o<0)break;let a=A(r.subarray(0,o+1));a.length>0&&i.enqueue(a),r=r.subarray(o+1)}}finally{r.length>0&&this.#e.push(r)}}};function $(t,e){let i=t.length;for(let a of t)(a===192||a===219)&&i++;let r=e.alloc(i+1),o=0;for(let a of t)a===192?(r[o++]=219,r[o++]=220):a===219?(r[o++]=219,r[o++]=221):r[o++]=a;return r[o]=192,r}n($,"slipEncode");function A(t){let e=0;for(let i=0;i<t.length;i++)if(t[i]===219){if(++i>=t.length)break;t[i]===220?t[e++]=192:t[i]===221&&(t[e++]=219)}else{if(t[i]===192)break;t[e++]=t[i]}return t.slice(0,e)}n(A,"slipDecode");export{d as default,A as slipDecode,$ as slipEncode};
//# sourceMappingURL=WebserialAdapter.mjs.map