UNPKG

esp-web-tools

Version:
2 lines (1 loc) • 4.16 kB
const e=e=>{const{baudRate:t,dataBits:i=8,stopBits:a=1,parity:r="none"}=e;if(!Number.isInteger(t)||t<=0)throw new RangeError(`invalid Baud Rate ${t}`);if(![5,6,7,8].includes(i))throw new RangeError(`invalid dataBits ${i}`);if(1!==a&&2!==a)throw new RangeError(`invalid stopBits ${a}`);if(!["none","even","odd"].includes(r))throw new RangeError(`invalid parity ${r}`);return{baudRate:t,dataBits:i,stopBits:a,parity:r}};class t{constructor(e,t){this._device=e,this._createDriver=t,this._readable=null,this._writable=null,this._signals={dataTerminalReady:!1,requestToSend:!1},this._break=!1}get _bufferSize(){var e,t;return null!==(t=null===(e=this._options)||void 0===e?void 0:e.bufferSize)&&void 0!==t?t:255}get readable(){if(this._readable||!this._device.opened||!this._endpoints)return this._readable;const e=new ReadableStream({pull:async t=>{var i;let a;null!==(i=this._pendingRead)&&void 0!==i||(this._pendingRead=this._read(t.desiredSize)),this._pendingRead.catch((()=>{}));try{a=await this._pendingRead}catch(i){return this._pendingRead=void 0,this._readable===e&&(this._readable=null),void t.error(i)}this._readable===e&&(this._pendingRead=void 0,t.enqueue(a))},cancel:()=>{this._readable===e&&(this._readable=null)}},new ByteLengthQueuingStrategy({highWaterMark:this._bufferSize}));return this._readable=e,e}get writable(){if(this._writable||!this._device.opened||!this._endpoints)return this._writable;const e=new WritableStream({write:async t=>{if(t.byteLength)try{const e=await this._device.transferOut(this._endpoints.out.endpointNumber,t);if("ok"!==e.status)throw new Error(`USB error: ${e.status}`)}catch(t){throw this._writable===e&&(this._writable=null),t}}},new ByteLengthQueuingStrategy({highWaterMark:this._bufferSize}));return this._writable=e,e}async _read(e){var t;const i=this._endpoints.in,a=this._driver,r=i.packetSize,n=Math.max(1,Math.ceil((null!=e?e:r)/r))*r;for(;;){const e=await this._device.transferIn(i.endpointNumber,n);if("ok"!==e.status)throw new Error(`USB error: ${e.status}`);if(!(null===(t=e.data)||void 0===t?void 0:t.byteLength))continue;let r=new Uint8Array(e.data.buffer,e.data.byteOffset,e.data.byteLength);if(a.processRead&&(r=a.processRead(r)),r.byteLength)return r}}async open(t){const i=e(t);this._options=t;try{await this._device.open(),null===this._device.configuration&&await this._device.selectConfiguration(1);const e=this._createDriver(this._device);this._driver=e,this._endpoints=await e.open(),await e.setLineConfig(i),this._signals={dataTerminalReady:!1,requestToSend:!1},this._break=!1,await e.setSignals(!1,!1)}catch(e){throw this._driver=void 0,this._endpoints=void 0,this._device.opened&&await this._device.close().catch((()=>{})),new Error(`Error setting up device: ${e}`)}}async close(){var e,t,i,a,r;if(await Promise.all([null===(e=this._readable)||void 0===e?void 0:e.cancel().catch((()=>{})),null===(t=this._writable)||void 0===t?void 0:t.abort().catch((()=>{}))]),this._readable=null,this._writable=null,this._pendingRead=void 0,this._device.opened){try{await(null===(i=this._driver)||void 0===i?void 0:i.setSignals(!1,!1)),await(null===(r=null===(a=this._driver)||void 0===a?void 0:a.close)||void 0===r?void 0:r.call(a))}catch{}this._driver=void 0,this._endpoints=void 0,await this._device.close()}}forget(){return this._device.forget()}getInfo(){return{usbVendorId:this._device.vendorId,usbProductId:this._device.productId}}async reconfigure(t){const i={...this._options,...t},a=e(i);this._options=i,await this._requireDriver().setLineConfig(a)}async setSignals(e){var t,i;const a=this._requireDriver();void 0===e.dataTerminalReady&&void 0===e.requestToSend||(this._signals={dataTerminalReady:null!==(t=e.dataTerminalReady)&&void 0!==t?t:this._signals.dataTerminalReady,requestToSend:null!==(i=e.requestToSend)&&void 0!==i?i:this._signals.requestToSend},await a.setSignals(this._signals.dataTerminalReady,this._signals.requestToSend)),void 0!==e.break&&a.setBreak&&(this._break=e.break,await a.setBreak(this._break))}_requireDriver(){if(!this._driver)throw new DOMException("The port is not open.","InvalidStateError");return this._driver}}export{t as UsbSerialPort,e as toLineConfig};