Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esp-web-flasher

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esp-web-flasher - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

3

dist/const.d.ts

@@ -47,2 +47,5 @@ export interface Logger {

export declare const ESP_CHECKSUM_MAGIC = 239;
export declare const ESP_FLASH_DEFL_BEGIN = 16;
export declare const ESP_FLASH_DEFL_DATA = 17;
export declare const ESP_FLASH_DEFL_END = 18;
export declare const ROM_INVALID_RECV_MSG = 5;

@@ -49,0 +52,0 @@ export declare const USB_RAM_BLOCK = 2048;

@@ -43,2 +43,5 @@ import { toByteArray } from "./util";

export const ESP_CHECKSUM_MAGIC = 0xef;
export const ESP_FLASH_DEFL_BEGIN = 0x10;
export const ESP_FLASH_DEFL_DATA = 0x11;
export const ESP_FLASH_DEFL_END = 0x12;
export const ROM_INVALID_RECV_MSG = 0x05;

@@ -45,0 +48,0 @@ export const USB_RAM_BLOCK = 0x800;

@@ -98,3 +98,3 @@ /// <reference types="w3c-web-serial" />

*/
flashData(binaryData: ArrayBuffer, updateProgress: (bytesWritten: number) => void, offset?: number): Promise<void>;
flashData(binaryData: ArrayBuffer, updateProgress: (bytesWritten: number, totalBytes: number) => void, offset?: number, compress?: boolean): Promise<void>;
/**

@@ -105,2 +105,3 @@ * @name flashBlock

flashBlock(data: number[], seq: number, timeout?: number): Promise<void>;
flashDeflBlock(data: number[], seq: number, timeout?: number): Promise<void>;
/**

@@ -112,3 +113,9 @@ * @name flashBegin

flashBegin(size?: number, offset?: number, encrypted?: boolean): Promise<number>;
/**
* @name flashDeflBegin
*
*/
flashDeflBegin(size?: number, compressedSize?: number, offset?: number, encrypted?: boolean): Promise<number>;
flashFinish(): Promise<void>;
flashDeflFinish(): Promise<void>;
/**

@@ -115,0 +122,0 @@ * @name getEraseSize

@@ -1,4 +0,5 @@

import { CHIP_FAMILY_ESP32, CHIP_FAMILY_ESP32S2, CHIP_FAMILY_ESP8266, MAX_TIMEOUT, DEFAULT_TIMEOUT, ERASE_REGION_TIMEOUT_PER_MB, ESP32S2_DATAREGVALUE, ESP32_DATAREGVALUE, ESP8266_DATAREGVALUE, ESP_CHANGE_BAUDRATE, ESP_CHECKSUM_MAGIC, ESP_FLASH_BEGIN, ESP_FLASH_DATA, ESP_FLASH_END, ESP_MEM_BEGIN, ESP_MEM_DATA, ESP_MEM_END, ESP_READ_REG, ESP_SPI_ATTACH, ESP_SPI_SET_PARAMS, ESP_SYNC, FLASH_SECTOR_SIZE, FLASH_WRITE_SIZE, STUB_FLASH_WRITE_SIZE, MEM_END_ROM_TIMEOUT, ROM_INVALID_RECV_MSG, SYNC_PACKET, SYNC_TIMEOUT, USB_RAM_BLOCK, ESP_ERASE_FLASH, CHIP_ERASE_TIMEOUT, timeoutPerMb, ESP_ROM_BAUD, } from "./const";
import { CHIP_FAMILY_ESP32, CHIP_FAMILY_ESP32S2, CHIP_FAMILY_ESP8266, MAX_TIMEOUT, DEFAULT_TIMEOUT, ERASE_REGION_TIMEOUT_PER_MB, ESP32S2_DATAREGVALUE, ESP32_DATAREGVALUE, ESP8266_DATAREGVALUE, ESP_CHANGE_BAUDRATE, ESP_CHECKSUM_MAGIC, ESP_FLASH_BEGIN, ESP_FLASH_DATA, ESP_FLASH_END, ESP_MEM_BEGIN, ESP_MEM_DATA, ESP_MEM_END, ESP_READ_REG, ESP_SPI_ATTACH, ESP_SPI_SET_PARAMS, ESP_SYNC, FLASH_SECTOR_SIZE, FLASH_WRITE_SIZE, STUB_FLASH_WRITE_SIZE, MEM_END_ROM_TIMEOUT, ROM_INVALID_RECV_MSG, SYNC_PACKET, SYNC_TIMEOUT, USB_RAM_BLOCK, ESP_ERASE_FLASH, CHIP_ERASE_TIMEOUT, timeoutPerMb, ESP_ROM_BAUD, ESP_FLASH_DEFL_BEGIN, ESP_FLASH_DEFL_DATA, ESP_FLASH_DEFL_END, } from "./const";
import { getStubCode } from "./stubs";
import { pack, sleep, slipEncode, toHex, unpack } from "./util";
import * as pako from "pako";
export class ESPLoader extends EventTarget {

@@ -487,32 +488,66 @@ constructor(port, logger, _parent) {

*/
async flashData(binaryData, updateProgress, offset = 0) {
let filesize = binaryData.byteLength;
this.logger.log("Writing data with filesize:" + filesize);
await this.flashBegin(filesize, offset);
async flashData(binaryData, updateProgress, offset = 0, compress = false) {
let uncompressedFilesize = binaryData.byteLength;
let compressedFilesize = 0;
let dataToFlash;
if (compress) {
dataToFlash = pako.deflate(new Uint8Array(binaryData), {
level: 9,
}).buffer;
compressedFilesize = dataToFlash.byteLength;
this.logger.log(`Writing data with filesize: ${uncompressedFilesize}. Compressed Size: ${compressedFilesize}`);
await this.flashDeflBegin(uncompressedFilesize, compressedFilesize, offset);
}
else {
this.logger.log(`Writing data with filesize: ${uncompressedFilesize}`);
dataToFlash = binaryData;
await this.flashBegin(uncompressedFilesize, offset);
}
let block = [];
let seq = 0;
let written = 0;
// let address = offset;
let position = 0;
let stamp = Date.now();
let flashWriteSize = this.getFlashWriteSize();
let filesize = compress ? compressedFilesize : uncompressedFilesize;
while (filesize - position > 0) {
/*logMsg(
"Writing at " + toHex(address + seq * flashWriteSize, 8) + "... (" + percentage + " %)"
);*/
if (this.debug) {
this.logger.log(`Writing at ${toHex(offset + seq * flashWriteSize, 8)} `);
}
if (filesize - position >= flashWriteSize) {
block = Array.from(new Uint8Array(binaryData, position, flashWriteSize));
block = Array.from(new Uint8Array(dataToFlash, position, flashWriteSize));
}
else {
// Pad the last block
block = Array.from(new Uint8Array(binaryData, position, filesize - position));
block = block.concat(new Array(flashWriteSize - block.length).fill(0xff));
// Pad the last block only if we are sending uncompressed data.
block = Array.from(new Uint8Array(dataToFlash, position, filesize - position));
if (!compress) {
block = block.concat(new Array(flashWriteSize - block.length).fill(0xff));
}
}
await this.flashBlock(block, seq, 2000);
if (compress) {
await this.flashDeflBlock(block, seq, 2000);
}
else {
await this.flashBlock(block, seq, 2000);
}
seq += 1;
written += block.length;
// If using compression we update the progress with the proportional size of the block taking into account the compression ratio.
// This way we report progress on the uncompressed size
written += compress
? Math.round((block.length * uncompressedFilesize) / compressedFilesize)
: block.length;
position += flashWriteSize;
updateProgress(written);
updateProgress(written, filesize);
}
this.logger.log("Took " + (Date.now() - stamp) + "ms to write " + filesize + " bytes");
// Only send flashF finish if running the stub because ir causes the ROM to exit and run user code
if (this.IS_STUB) {
await this.flashBegin(0, 0);
if (compress) {
await this.flashDeflFinish();
}
else {
await this.flashFinish();
}
}
}

@@ -526,2 +561,5 @@ /**

}
async flashDeflBlock(data, seq, timeout = 100) {
await this.checkCommand(ESP_FLASH_DEFL_DATA, pack("<IIII", data.length, seq, 0, 0).concat(data), this.checksum(data));
}
/**

@@ -579,2 +617,27 @@ * @name flashBegin

}
/**
* @name flashDeflBegin
*
*/
async flashDeflBegin(size = 0, compressedSize = 0, offset = 0, encrypted = false) {
// Start downloading compressed data to Flash (performs an erase)
// Returns number of blocks to write.
let flashWriteSize = this.getFlashWriteSize();
let numBlocks = Math.floor((compressedSize + flashWriteSize - 1) / flashWriteSize);
let eraseBlocks = Math.floor((size + flashWriteSize - 1) / flashWriteSize);
let writeSize = 0;
let timeout = 0;
let buffer;
if (this.IS_STUB) {
writeSize = size; // stub expects number of bytes here, manages erasing internally
timeout = DEFAULT_TIMEOUT;
}
else {
writeSize = eraseBlocks * flashWriteSize; // ROM expects rounded up to erase block size
timeout = timeoutPerMb(ERASE_REGION_TIMEOUT_PER_MB, writeSize); // ROM performs the erase up front
}
buffer = pack("<IIII", writeSize, numBlocks, flashWriteSize, offset);
await this.checkCommand(ESP_FLASH_DEFL_BEGIN, buffer, 0, timeout);
return numBlocks;
}
async flashFinish() {

@@ -584,2 +647,6 @@ let buffer = pack("<I", 1);

}
async flashDeflFinish() {
let buffer = pack("<I", 1);
await this.checkCommand(ESP_FLASH_DEFL_END, buffer);
}
/**

@@ -586,0 +653,0 @@ * @name getEraseSize

2

dist/web/index.js

@@ -1,1 +0,1 @@

const t=t=>{let e=[];for(let i of t)219==i?e=e.concat([219,221]):192==i?e=e.concat([219,220]):e.push(i);return e},e=t=>{let e=[];for(let i=0;i<t.length;i++){let a=t.charCodeAt(i);a<=255&&e.push(a)}return e},i=(t,...e)=>{let i=0;if(t.replace(/[<>]/,"").length!=e.length)throw new Error("Pack format to Argument count mismatch");let a=[],s=!0;const r=(t,e)=>{for(let i=0;i<e;i++)s?a.push(t>>8*i&255):a.push(t>>8*(e-i)&255)};for(let a=0;a<t.length;a++)if("<"==t[a])s=!0;else if(">"==t[a])s=!1;else if("B"==t[a])r(e[i],1),i++;else if("H"==t[a])r(e[i],2),i++;else{if("I"!=t[a])throw new Error(`Unhandled character "${t[a]}" in pack format`);r(e[i],4),i++}return a},a=(t,e=2)=>"0x"+t.toString(16).toUpperCase().padStart(e,"0"),s=t=>new Promise((e=>setTimeout(e,t))),r=e(" UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"),n=33382,h=50,o=12882,l=async t=>{let i;return t==h?i=await import("./esp32-9a0a29d9.js"):t==o?i=await import("./esp32s2-70924167.js"):33382==t&&(i=await import("./esp8266-695df45b.js")),{...i,text:e(atob(i.text)),data:e(atob(i.data))}};class c extends EventTarget{constructor(t,e,i){super(),this.port=t,this.logger=e,this._parent=i,this.chipName=null,this._efuses=new Array(4).fill(0),this._flashsize=4194304,this.debug=!1,this.IS_STUB=!1,this.connected=!0,this.stopReadLoop=!1}get _inputBuffer(){return this._parent?this._parent._inputBuffer:this.__inputBuffer}async initialize(){await this.hardReset(!0),this._parent||(this.__inputBuffer=[],this.readLoop()),await this.sync();let t,e=await this.readRegister(1610612856);if(353510656==e)this.chipFamily=h;else if(401408==e)this.chipFamily=33382;else{if(1280!=e)throw new Error("Unknown Chip.");this.chipFamily=o}33382==this.chipFamily?t=1072693328:(this.chipFamily==h||this.chipFamily==o)&&(t=1610719232);for(let e=0;e<4;e++)this._efuses[e]=await this.readRegister(t+4*e);this.chipFamily==h&&(this.chipName="ESP32"),this.chipFamily==o&&(this.chipName="ESP32-S2"),33382==this.chipFamily&&(16&this._efuses[0]||65536&this._efuses[2]?this.chipName="ESP8285":this.chipName="ESP8266EX")}async readLoop(){this.logger.debug("Starting read loop"),this._reader=this.port.readable.getReader();try{for(;!this.stopReadLoop;){const{value:t,done:e}=await this._reader.read();if(e){this._reader.releaseLock();break}t&&0!==t.length&&this._inputBuffer.push(...Array.from(t))}}catch(t){console.error("Read loop got disconnected"),this.connected=!1,this.dispatchEvent(new Event("disconnect"))}this.logger.debug("Finished read loop")}async hardReset(t=!1){this.logger.log("Try hard reset."),await this.port.setSignals({dataTerminalReady:!1,requestToSend:!0}),await this.port.setSignals({dataTerminalReady:t,requestToSend:!1}),await new Promise((t=>setTimeout(t,1e3)))}macAddr(){let t,e=new Array(6).fill(0),i=this._efuses[0],a=this._efuses[1],s=this._efuses[2],r=this._efuses[3];if(33382==this.chipFamily){if(0!=r)t=[r>>16&255,r>>8&255,255&r];else if(0==(a>>16&255))t=[24,254,52];else{if(1!=(a>>16&255))throw new Error("Couldnt determine OUI");t=[172,208,116]}e[0]=t[0],e[1]=t[1],e[2]=t[2],e[3]=a>>8&255,e[4]=255&a,e[5]=i>>24&255}else if(this.chipFamily==h)e[0]=s>>8&255,e[1]=255&s,e[2]=a>>24&255,e[3]=a>>16&255,e[4]=a>>8&255,e[5]=255&a;else{if(this.chipFamily!=o)throw new Error("Unknown chip family");e[0]=s>>8&255,e[1]=255&s,e[2]=a>>24&255,e[3]=a>>16&255,e[4]=a>>8&255,e[5]=255&a}return e}async readRegister(t){this.debug&&this.logger.debug("Reading Register",t);let e=i("I",t);return((t,e)=>{let i=0,a=[];for(let s of t)if("B"==s)a.push(255&e[i]),i+=1;else if("H"==s)a.push(255&e[i]|(255&e[i+1])<<8),i+=2;else{if("I"!=s)throw new Error(`Unhandled character "${s}" in unpack format`);a.push(255&e[i]|(255&e[i+1])<<8|(255&e[i+2])<<16|(255&e[i+3])<<24),i+=4}return a})("I",(await this.checkCommand(10,e))[0])[0]}async checkCommand(t,e,i=0,s=3e3){s=Math.min(s,12e5),await this.sendCommand(t,e,i);let[r,n]=await this.getResponse(t,s);if(null===n)throw new Error("Didn't get enough status bytes");let l=0;if(this.IS_STUB||33382==this.chipFamily?l=2:[h,o].includes(this.chipFamily)?l=4:[2,4].includes(n.length)&&(l=n.length),n.length<l)throw new Error("Didn't get enough status bytes");let c=n.slice(-l,n.length);if(n=n.slice(0,-l),this.debug&&(this.logger.debug("status",c),this.logger.debug("value",r),this.logger.debug("data",n)),1==c[0])throw 5==c[1]?new Error("Invalid (unsupported) command "+a(t)):new Error("Command failure error code "+a(c[1]));return[r,n]}async sendCommand(e,a,s=0){this._inputBuffer.length=0;let r=[192,0];r.push(e),r=r.concat(i("H",a.length)),r=r.concat(t(i("I",s))),r=r.concat(t(a)),r.push(192),this.debug&&this.logger.debug("Writing "+r.length+" byte"+(1==r.length?"":"s")+":",r),await this.writeToStream(r)}async getResponse(t,e=3e3){let i=[],a=0,r=!1,n=Date.now();for(;Date.now()-n<e;){if(this._inputBuffer.length>0){let t=this._inputBuffer.shift();219==t?r=!0:r?(221==t?i.push(220):220==t?i.push(192):i=i.concat([219,t]),r=!1):i.push(t)}else await s(10);if(i.length>0&&192!=i[0]&&i.shift(),i.length>1&&1!=i[1]&&i.shift(),i.length>2&&i[2]!=t&&i.shift(),i.length>4&&(a=i[3]+(i[4]<<8)),i.length==a+10)break}if(i.length!=a+10)return this.logger.log("Timed out after "+e+" milliseconds"),[null,null];this.debug&&this.logger.debug("Reading "+i.length+" byte"+(1==i.length?"":"s")+":",i);let h=i.slice(5,9),o=i.slice(9,-1);return this.debug&&this.logger.debug("value:",h,"data:",o),[h,o]}async readBuffer(t=3e3){let e=[],i=!1,a=Date.now();for(;Date.now()-a<t;){if(this._inputBuffer.length>0){let t=this._inputBuffer.shift();219==t?i=!0:i?(221==t?e.push(220):220==t?e.push(192):e=e.concat([219,t]),i=!1):e.push(t)}else await s(10);if(e.length>0&&192!=e[0]&&e.shift(),e.length>1&&192==e[e.length-1])break}if(e.length<2)return this.logger.log("Timed out after "+t+" milliseconds"),null;this.debug&&this.logger.debug("Reading "+e.length+" byte"+(1==e.length?"":"s")+":",e);let r=e.slice(1,-1);return this.debug&&this.logger.debug("data:",r),r}checksum(t,e=239){for(let i of t)e^=i;return e}async setBaudrate(t){if(33382==this.chipFamily)throw new Error("Changing baud rate is not supported on the ESP8266");this.logger.log("Attempting to change baud rate to "+t+"...");try{let e=i("<II",t,this.IS_STUB?115200:0);await this.checkCommand(15,e)}catch(e){throw console.error(e),new Error(`Unable to change the baud rate to ${t}: No response from set baud rate command.`)}this._parent?await this._parent.reconfigurePort(t):await this.reconfigurePort(t)}async reconfigurePort(t){var e,i;try{this.stopReadLoop=!0,await(null===(e=this._reader)||void 0===e?void 0:e.cancel()),null===(i=this._reader)||void 0===i||i.releaseLock(),await this.port.close(),await this.port.open({baudRate:t}),this.stopReadLoop=!1,this.readLoop(),this.logger.log(`Changed baud rate to ${t}`)}catch(e){throw console.error(e),new Error(`Unable to change the baud rate to ${t}: ${e}`)}}async sync(){for(let t=0;t<5;t++){if(await this._sync())return await s(100),!0;await s(100)}throw new Error("Couldn't sync to ESP. Try resetting.")}async _sync(){await this.sendCommand(8,r);for(let t=0;t<8;t++){let[t,e]=await this.getResponse(8,100);if(null!==e&&(e.length>1&&0==e[0]&&0==e[1]))return!0}return!1}getFlashWriteSize(){return this.IS_STUB?16384:1024}async flashData(t,e,i=0){let a=t.byteLength;this.logger.log("Writing data with filesize:"+a),await this.flashBegin(a,i);let s=[],r=0,n=0,h=0,o=Date.now(),l=this.getFlashWriteSize();for(;a-h>0;)a-h>=l?s=Array.from(new Uint8Array(t,h,l)):(s=Array.from(new Uint8Array(t,h,a-h)),s=s.concat(new Array(l-s.length).fill(255))),await this.flashBlock(s,r,2e3),r+=1,n+=s.length,h+=l,e(n);this.logger.log("Took "+(Date.now()-o)+"ms to write "+a+" bytes")}async flashBlock(t,e,a=100){await this.checkCommand(3,i("<IIII",t.length,e,0,0).concat(t),this.checksum(t),a)}async flashBegin(t=0,e=0,s=!1){let r,n,l=this.getFlashWriteSize();[h,o].includes(this.chipFamily)&&await this.checkCommand(13,new Array(8).fill(0)),this.chipFamily==h&&(n=i("<IIIIII",0,this._flashsize,65536,4096,256,65535),await this.checkCommand(11,n));let c,g=Math.floor((t+l-1)/l);r=33382==this.chipFamily?this.getEraseSize(e,t):t,c=this.IS_STUB?3e3:((t,e)=>{let i=Math.floor(t*(e/486));return i<3e3?3e3:i})(3e4,t);let d=Date.now();return n=i("<IIII",r,g,l,e),this.chipFamily==o&&(n=n.concat(i("<I",s?1:0))),this.logger.log("Erase size "+r+", blocks "+g+", block size "+l+", offset "+a(e,4)+", encrypted "+(s?"yes":"no")),await this.checkCommand(2,n,0,c),0==t||this.IS_STUB||this.logger.log("Took "+(Date.now()-d)+"ms to erase "+g+" bytes"),g}async flashFinish(){let t=i("<I",1);await this.checkCommand(4,t)}getEraseSize(t,e){let i=4096,a=Math.floor((e+i-1)/i),s=16-Math.floor(t/i)%16;return a<s&&(s=a),a<2*s?Math.floor((a+1)/2*i):(a-s)*i}async memBegin(t,e,a,s){return await this.checkCommand(5,i("<IIII",t,e,a,s))}async memBlock(t,e){return await this.checkCommand(7,i("<IIII",t.length,e,0,0).concat(t),this.checksum(t))}async memFinish(t=0){let e=this.IS_STUB?3e3:50,a=i("<II",0==t?1:0,t);return await this.checkCommand(6,a,0,e)}async runStub(){const t=await l(this.chipFamily);let e=2048;this.logger.log("Uploading stub...");for(let i of["text","data"])if(Object.keys(t).includes(i)){let a=t[i+"_start"],s=t[i].length,r=Math.floor((s+e-1)/e);await this.memBegin(s,r,e,a);for(let a of Array(r).keys()){let r=a*e,n=r+e;n>s&&(n=s),await this.memBlock(t[i].slice(r,n),a)}}this.logger.log("Running stub..."),await this.memFinish(t.entry);const i=await this.readBuffer(100),a=String.fromCharCode(...i);if("OHAI"!=a)throw new Error("Failed to start stub. Unexpected response: "+a);this.logger.log("Stub is now running...");return new g(this.port,this.logger,this)}async writeToStream(t){const e=this.port.writable.getWriter();await e.write(new Uint8Array(t));try{e.releaseLock()}catch(t){console.error("Ignoring release lock error",t)}}async disconnect(){this._parent?await this._parent.disconnect():(this._reader&&await this._reader.cancel(),await this.port.writable.getWriter().close(),await this.port.close(),this.connected=!1)}}class g extends c{constructor(){super(...arguments),this.IS_STUB=!0}async memBegin(t,e,i,s){let r=await l(this.chipFamily),n=s,h=s+t;console.log(n,h),console.log(r.data_start,r.data.length,r.text_start,r.text.length);for(let[t,e]of[[r.data_start,r.data_start+r.data.length],[r.text_start,r.text_start+r.text.length]])if(n<e&&h>t)throw new Error("Software loader is resident at "+a(t,8)+"-"+a(e,8)+". Can't load binary at overlapping address range "+a(n,8)+"-"+a(h,8)+". Try changing the binary loading address.")}async eraseFlash(){await this.checkCommand(208,[],0,6e5)}}const d=async t=>{const e=await navigator.serial.requestPort();return t.log("Connecting..."),await e.open({baudRate:115200}),t.log("Connected successfully."),new c(e,t)};export{h as CHIP_FAMILY_ESP32,o as CHIP_FAMILY_ESP32S2,n as CHIP_FAMILY_ESP8266,c as ESPLoader,d as connect};
const t=t=>{let e=[];for(let a of t)219==a?e=e.concat([219,221]):192==a?e=e.concat([219,220]):e.push(a);return e},e=t=>{let e=[];for(let a=0;a<t.length;a++){let i=t.charCodeAt(a);i<=255&&e.push(i)}return e},a=(t,...e)=>{let a=0;if(t.replace(/[<>]/,"").length!=e.length)throw new Error("Pack format to Argument count mismatch");let i=[],s=!0;const n=(t,e)=>{for(let a=0;a<e;a++)s?i.push(t>>8*a&255):i.push(t>>8*(e-a)&255)};for(let i=0;i<t.length;i++)if("<"==t[i])s=!0;else if(">"==t[i])s=!1;else if("B"==t[i])n(e[a],1),a++;else if("H"==t[i])n(e[a],2),a++;else{if("I"!=t[i])throw new Error(`Unhandled character "${t[i]}" in pack format`);n(e[a],4),a++}return i},i=(t,e=2)=>"0x"+t.toString(16).toUpperCase().padStart(e,"0"),s=t=>new Promise((e=>setTimeout(e,t))),n=e(" UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"),r=33382,o=50,l=12882,h=(t,e)=>{let a=Math.floor(t*(e/486));return a<3e3?3e3:a},d=async t=>{let a;return t==o?a=await import("./esp32-9a0a29d9.js"):t==l?a=await import("./esp32s2-70924167.js"):33382==t&&(a=await import("./esp8266-695df45b.js")),{...a,text:e(atob(a.text)),data:e(atob(a.data))}};function _(t){let e=t.length;for(;--e>=0;)t[e]=0}const c=new Uint8Array([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]),f=new Uint8Array([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]),u=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]),g=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),w=new Array(576);_(w);const m=new Array(60);_(m);const p=new Array(512);_(p);const b=new Array(256);_(b);const k=new Array(29);_(k);const y=new Array(30);function v(t,e,a,i,s){this.static_tree=t,this.extra_bits=e,this.extra_base=a,this.elems=i,this.max_length=s,this.has_stree=t&&t.length}let x,z,U;function E(t,e){this.dyn_tree=t,this.max_code=0,this.stat_desc=e}_(y);const A=t=>t<256?p[t]:p[256+(t>>>7)],S=(t,e)=>{t.pending_buf[t.pending++]=255&e,t.pending_buf[t.pending++]=e>>>8&255},R=(t,e,a)=>{t.bi_valid>16-a?(t.bi_buf|=e<<t.bi_valid&65535,S(t,t.bi_buf),t.bi_buf=e>>16-t.bi_valid,t.bi_valid+=a-16):(t.bi_buf|=e<<t.bi_valid&65535,t.bi_valid+=a)},I=(t,e,a)=>{R(t,a[2*e],a[2*e+1])},B=(t,e)=>{let a=0;do{a|=1&t,t>>>=1,a<<=1}while(--e>0);return a>>>1},F=(t,e,a)=>{const i=new Array(16);let s,n,r=0;for(s=1;s<=15;s++)i[s]=r=r+a[s-1]<<1;for(n=0;n<=e;n++){let e=t[2*n+1];0!==e&&(t[2*n]=B(i[e]++,e))}},Z=t=>{let e;for(e=0;e<286;e++)t.dyn_ltree[2*e]=0;for(e=0;e<30;e++)t.dyn_dtree[2*e]=0;for(e=0;e<19;e++)t.bl_tree[2*e]=0;t.dyn_ltree[512]=1,t.opt_len=t.static_len=0,t.last_lit=t.matches=0},T=t=>{t.bi_valid>8?S(t,t.bi_buf):t.bi_valid>0&&(t.pending_buf[t.pending++]=t.bi_buf),t.bi_buf=0,t.bi_valid=0},D=(t,e,a,i)=>{const s=2*e,n=2*a;return t[s]<t[n]||t[s]===t[n]&&i[e]<=i[a]},C=(t,e,a)=>{const i=t.heap[a];let s=a<<1;for(;s<=t.heap_len&&(s<t.heap_len&&D(e,t.heap[s+1],t.heap[s],t.depth)&&s++,!D(e,i,t.heap[s],t.depth));)t.heap[a]=t.heap[s],a=s,s<<=1;t.heap[a]=i},L=(t,e,a)=>{let i,s,n,r,o=0;if(0!==t.last_lit)do{i=t.pending_buf[t.d_buf+2*o]<<8|t.pending_buf[t.d_buf+2*o+1],s=t.pending_buf[t.l_buf+o],o++,0===i?I(t,s,e):(n=b[s],I(t,n+256+1,e),r=c[n],0!==r&&(s-=k[n],R(t,s,r)),i--,n=A(i),I(t,n,a),r=f[n],0!==r&&(i-=y[n],R(t,i,r)))}while(o<t.last_lit);I(t,256,e)},O=(t,e)=>{const a=e.dyn_tree,i=e.stat_desc.static_tree,s=e.stat_desc.has_stree,n=e.stat_desc.elems;let r,o,l,h=-1;for(t.heap_len=0,t.heap_max=573,r=0;r<n;r++)0!==a[2*r]?(t.heap[++t.heap_len]=h=r,t.depth[r]=0):a[2*r+1]=0;for(;t.heap_len<2;)l=t.heap[++t.heap_len]=h<2?++h:0,a[2*l]=1,t.depth[l]=0,t.opt_len--,s&&(t.static_len-=i[2*l+1]);for(e.max_code=h,r=t.heap_len>>1;r>=1;r--)C(t,a,r);l=n;do{r=t.heap[1],t.heap[1]=t.heap[t.heap_len--],C(t,a,1),o=t.heap[1],t.heap[--t.heap_max]=r,t.heap[--t.heap_max]=o,a[2*l]=a[2*r]+a[2*o],t.depth[l]=(t.depth[r]>=t.depth[o]?t.depth[r]:t.depth[o])+1,a[2*r+1]=a[2*o+1]=l,t.heap[1]=l++,C(t,a,1)}while(t.heap_len>=2);t.heap[--t.heap_max]=t.heap[1],((t,e)=>{const a=e.dyn_tree,i=e.max_code,s=e.stat_desc.static_tree,n=e.stat_desc.has_stree,r=e.stat_desc.extra_bits,o=e.stat_desc.extra_base,l=e.stat_desc.max_length;let h,d,_,c,f,u,g=0;for(c=0;c<=15;c++)t.bl_count[c]=0;for(a[2*t.heap[t.heap_max]+1]=0,h=t.heap_max+1;h<573;h++)d=t.heap[h],c=a[2*a[2*d+1]+1]+1,c>l&&(c=l,g++),a[2*d+1]=c,d>i||(t.bl_count[c]++,f=0,d>=o&&(f=r[d-o]),u=a[2*d],t.opt_len+=u*(c+f),n&&(t.static_len+=u*(s[2*d+1]+f)));if(0!==g){do{for(c=l-1;0===t.bl_count[c];)c--;t.bl_count[c]--,t.bl_count[c+1]+=2,t.bl_count[l]--,g-=2}while(g>0);for(c=l;0!==c;c--)for(d=t.bl_count[c];0!==d;)_=t.heap[--h],_>i||(a[2*_+1]!==c&&(t.opt_len+=(c-a[2*_+1])*a[2*_],a[2*_+1]=c),d--)}})(t,e),F(a,h,t.bl_count)},N=(t,e,a)=>{let i,s,n=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),e[2*(a+1)+1]=65535,i=0;i<=a;i++)s=r,r=e[2*(i+1)+1],++o<l&&s===r||(o<h?t.bl_tree[2*s]+=o:0!==s?(s!==n&&t.bl_tree[2*s]++,t.bl_tree[32]++):o<=10?t.bl_tree[34]++:t.bl_tree[36]++,o=0,n=s,0===r?(l=138,h=3):s===r?(l=6,h=3):(l=7,h=4))},M=(t,e,a)=>{let i,s,n=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),i=0;i<=a;i++)if(s=r,r=e[2*(i+1)+1],!(++o<l&&s===r)){if(o<h)do{I(t,s,t.bl_tree)}while(0!=--o);else 0!==s?(s!==n&&(I(t,s,t.bl_tree),o--),I(t,16,t.bl_tree),R(t,o-3,2)):o<=10?(I(t,17,t.bl_tree),R(t,o-3,3)):(I(t,18,t.bl_tree),R(t,o-11,7));o=0,n=s,0===r?(l=138,h=3):s===r?(l=6,h=3):(l=7,h=4)}};let H=!1;const P=(t,e,a,i)=>{R(t,0+(i?1:0),3),((t,e,a,i)=>{T(t),i&&(S(t,a),S(t,~a)),t.pending_buf.set(t.window.subarray(e,e+a),t.pending),t.pending+=a})(t,e,a,!0)};var j={_tr_init:t=>{H||((()=>{let t,e,a,i,s;const n=new Array(16);for(a=0,i=0;i<28;i++)for(k[i]=a,t=0;t<1<<c[i];t++)b[a++]=i;for(b[a-1]=i,s=0,i=0;i<16;i++)for(y[i]=s,t=0;t<1<<f[i];t++)p[s++]=i;for(s>>=7;i<30;i++)for(y[i]=s<<7,t=0;t<1<<f[i]-7;t++)p[256+s++]=i;for(e=0;e<=15;e++)n[e]=0;for(t=0;t<=143;)w[2*t+1]=8,t++,n[8]++;for(;t<=255;)w[2*t+1]=9,t++,n[9]++;for(;t<=279;)w[2*t+1]=7,t++,n[7]++;for(;t<=287;)w[2*t+1]=8,t++,n[8]++;for(F(w,287,n),t=0;t<30;t++)m[2*t+1]=5,m[2*t]=B(t,5);x=new v(w,c,257,286,15),z=new v(m,f,0,30,15),U=new v(new Array(0),u,0,19,7)})(),H=!0),t.l_desc=new E(t.dyn_ltree,x),t.d_desc=new E(t.dyn_dtree,z),t.bl_desc=new E(t.bl_tree,U),t.bi_buf=0,t.bi_valid=0,Z(t)},_tr_stored_block:P,_tr_flush_block:(t,e,a,i)=>{let s,n,r=0;t.level>0?(2===t.strm.data_type&&(t.strm.data_type=(t=>{let e,a=4093624447;for(e=0;e<=31;e++,a>>>=1)if(1&a&&0!==t.dyn_ltree[2*e])return 0;if(0!==t.dyn_ltree[18]||0!==t.dyn_ltree[20]||0!==t.dyn_ltree[26])return 1;for(e=32;e<256;e++)if(0!==t.dyn_ltree[2*e])return 1;return 0})(t)),O(t,t.l_desc),O(t,t.d_desc),r=(t=>{let e;for(N(t,t.dyn_ltree,t.l_desc.max_code),N(t,t.dyn_dtree,t.d_desc.max_code),O(t,t.bl_desc),e=18;e>=3&&0===t.bl_tree[2*g[e]+1];e--);return t.opt_len+=3*(e+1)+5+5+4,e})(t),s=t.opt_len+3+7>>>3,n=t.static_len+3+7>>>3,n<=s&&(s=n)):s=n=a+5,a+4<=s&&-1!==e?P(t,e,a,i):4===t.strategy||n===s?(R(t,2+(i?1:0),3),L(t,w,m)):(R(t,4+(i?1:0),3),((t,e,a,i)=>{let s;for(R(t,e-257,5),R(t,a-1,5),R(t,i-4,4),s=0;s<i;s++)R(t,t.bl_tree[2*g[s]+1],3);M(t,t.dyn_ltree,e-1),M(t,t.dyn_dtree,a-1)})(t,t.l_desc.max_code+1,t.d_desc.max_code+1,r+1),L(t,t.dyn_ltree,t.dyn_dtree)),Z(t),i&&T(t)},_tr_tally:(t,e,a)=>(t.pending_buf[t.d_buf+2*t.last_lit]=e>>>8&255,t.pending_buf[t.d_buf+2*t.last_lit+1]=255&e,t.pending_buf[t.l_buf+t.last_lit]=255&a,t.last_lit++,0===e?t.dyn_ltree[2*a]++:(t.matches++,e--,t.dyn_ltree[2*(b[a]+256+1)]++,t.dyn_dtree[2*A(e)]++),t.last_lit===t.lit_bufsize-1),_tr_align:t=>{R(t,2,3),I(t,256,w),(t=>{16===t.bi_valid?(S(t,t.bi_buf),t.bi_buf=0,t.bi_valid=0):t.bi_valid>=8&&(t.pending_buf[t.pending++]=255&t.bi_buf,t.bi_buf>>=8,t.bi_valid-=8)})(t)}};var K=(t,e,a,i)=>{let s=65535&t|0,n=t>>>16&65535|0,r=0;for(;0!==a;){r=a>2e3?2e3:a,a-=r;do{s=s+e[i++]|0,n=n+s|0}while(--r);s%=65521,n%=65521}return s|n<<16|0};const W=new Uint32Array((()=>{let t,e=[];for(var a=0;a<256;a++){t=a;for(var i=0;i<8;i++)t=1&t?3988292384^t>>>1:t>>>1;e[a]=t}return e})());var $=(t,e,a,i)=>{const s=W,n=i+a;t^=-1;for(let a=i;a<n;a++)t=t>>>8^s[255&(t^e[a])];return-1^t},Y={2:"need dictionary",1:"stream end",0:"","-1":"file error","-2":"stream error","-3":"data error","-4":"insufficient memory","-5":"buffer error","-6":"incompatible version"},G={Z_NO_FLUSH:0,Z_PARTIAL_FLUSH:1,Z_SYNC_FLUSH:2,Z_FULL_FLUSH:3,Z_FINISH:4,Z_BLOCK:5,Z_TREES:6,Z_OK:0,Z_STREAM_END:1,Z_NEED_DICT:2,Z_ERRNO:-1,Z_STREAM_ERROR:-2,Z_DATA_ERROR:-3,Z_MEM_ERROR:-4,Z_BUF_ERROR:-5,Z_NO_COMPRESSION:0,Z_BEST_SPEED:1,Z_BEST_COMPRESSION:9,Z_DEFAULT_COMPRESSION:-1,Z_FILTERED:1,Z_HUFFMAN_ONLY:2,Z_RLE:3,Z_FIXED:4,Z_DEFAULT_STRATEGY:0,Z_BINARY:0,Z_TEXT:1,Z_UNKNOWN:2,Z_DEFLATED:8};const{_tr_init:X,_tr_stored_block:q,_tr_flush_block:J,_tr_tally:Q,_tr_align:V}=j,{Z_NO_FLUSH:tt,Z_PARTIAL_FLUSH:et,Z_FULL_FLUSH:at,Z_FINISH:it,Z_BLOCK:st,Z_OK:nt,Z_STREAM_END:rt,Z_STREAM_ERROR:ot,Z_DATA_ERROR:lt,Z_BUF_ERROR:ht,Z_DEFAULT_COMPRESSION:dt,Z_FILTERED:_t,Z_HUFFMAN_ONLY:ct,Z_RLE:ft,Z_FIXED:ut,Z_DEFAULT_STRATEGY:gt,Z_UNKNOWN:wt,Z_DEFLATED:mt}=G,pt=(t,e)=>(t.msg=Y[e],e),bt=t=>(t<<1)-(t>4?9:0),kt=t=>{let e=t.length;for(;--e>=0;)t[e]=0};let yt=(t,e,a)=>(e<<t.hash_shift^a)&t.hash_mask;const vt=t=>{const e=t.state;let a=e.pending;a>t.avail_out&&(a=t.avail_out),0!==a&&(t.output.set(e.pending_buf.subarray(e.pending_out,e.pending_out+a),t.next_out),t.next_out+=a,e.pending_out+=a,t.total_out+=a,t.avail_out-=a,e.pending-=a,0===e.pending&&(e.pending_out=0))},xt=(t,e)=>{J(t,t.block_start>=0?t.block_start:-1,t.strstart-t.block_start,e),t.block_start=t.strstart,vt(t.strm)},zt=(t,e)=>{t.pending_buf[t.pending++]=e},Ut=(t,e)=>{t.pending_buf[t.pending++]=e>>>8&255,t.pending_buf[t.pending++]=255&e},Et=(t,e,a,i)=>{let s=t.avail_in;return s>i&&(s=i),0===s?0:(t.avail_in-=s,e.set(t.input.subarray(t.next_in,t.next_in+s),a),1===t.state.wrap?t.adler=K(t.adler,e,s,a):2===t.state.wrap&&(t.adler=$(t.adler,e,s,a)),t.next_in+=s,t.total_in+=s,s)},At=(t,e)=>{let a,i,s=t.max_chain_length,n=t.strstart,r=t.prev_length,o=t.nice_match;const l=t.strstart>t.w_size-262?t.strstart-(t.w_size-262):0,h=t.window,d=t.w_mask,_=t.prev,c=t.strstart+258;let f=h[n+r-1],u=h[n+r];t.prev_length>=t.good_match&&(s>>=2),o>t.lookahead&&(o=t.lookahead);do{if(a=e,h[a+r]===u&&h[a+r-1]===f&&h[a]===h[n]&&h[++a]===h[n+1]){n+=2,a++;do{}while(h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&h[++n]===h[++a]&&n<c);if(i=258-(c-n),n=c-258,i>r){if(t.match_start=e,r=i,i>=o)break;f=h[n+r-1],u=h[n+r]}}}while((e=_[e&d])>l&&0!=--s);return r<=t.lookahead?r:t.lookahead},St=t=>{const e=t.w_size;let a,i,s,n,r;do{if(n=t.window_size-t.lookahead-t.strstart,t.strstart>=e+(e-262)){t.window.set(t.window.subarray(e,e+e),0),t.match_start-=e,t.strstart-=e,t.block_start-=e,i=t.hash_size,a=i;do{s=t.head[--a],t.head[a]=s>=e?s-e:0}while(--i);i=e,a=i;do{s=t.prev[--a],t.prev[a]=s>=e?s-e:0}while(--i);n+=e}if(0===t.strm.avail_in)break;if(i=Et(t.strm,t.window,t.strstart+t.lookahead,n),t.lookahead+=i,t.lookahead+t.insert>=3)for(r=t.strstart-t.insert,t.ins_h=t.window[r],t.ins_h=yt(t,t.ins_h,t.window[r+1]);t.insert&&(t.ins_h=yt(t,t.ins_h,t.window[r+3-1]),t.prev[r&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=r,r++,t.insert--,!(t.lookahead+t.insert<3)););}while(t.lookahead<262&&0!==t.strm.avail_in)},Rt=(t,e)=>{let a,i;for(;;){if(t.lookahead<262){if(St(t),t.lookahead<262&&e===tt)return 1;if(0===t.lookahead)break}if(a=0,t.lookahead>=3&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),0!==a&&t.strstart-a<=t.w_size-262&&(t.match_length=At(t,a)),t.match_length>=3)if(i=Q(t,t.strstart-t.match_start,t.match_length-3),t.lookahead-=t.match_length,t.match_length<=t.max_lazy_match&&t.lookahead>=3){t.match_length--;do{t.strstart++,t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart}while(0!=--t.match_length);t.strstart++}else t.strstart+=t.match_length,t.match_length=0,t.ins_h=t.window[t.strstart],t.ins_h=yt(t,t.ins_h,t.window[t.strstart+1]);else i=Q(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++;if(i&&(xt(t,!1),0===t.strm.avail_out))return 1}return t.insert=t.strstart<2?t.strstart:2,e===it?(xt(t,!0),0===t.strm.avail_out?3:4):t.last_lit&&(xt(t,!1),0===t.strm.avail_out)?1:2},It=(t,e)=>{let a,i,s;for(;;){if(t.lookahead<262){if(St(t),t.lookahead<262&&e===tt)return 1;if(0===t.lookahead)break}if(a=0,t.lookahead>=3&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),t.prev_length=t.match_length,t.prev_match=t.match_start,t.match_length=2,0!==a&&t.prev_length<t.max_lazy_match&&t.strstart-a<=t.w_size-262&&(t.match_length=At(t,a),t.match_length<=5&&(t.strategy===_t||3===t.match_length&&t.strstart-t.match_start>4096)&&(t.match_length=2)),t.prev_length>=3&&t.match_length<=t.prev_length){s=t.strstart+t.lookahead-3,i=Q(t,t.strstart-1-t.prev_match,t.prev_length-3),t.lookahead-=t.prev_length-1,t.prev_length-=2;do{++t.strstart<=s&&(t.ins_h=yt(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart)}while(0!=--t.prev_length);if(t.match_available=0,t.match_length=2,t.strstart++,i&&(xt(t,!1),0===t.strm.avail_out))return 1}else if(t.match_available){if(i=Q(t,0,t.window[t.strstart-1]),i&&xt(t,!1),t.strstart++,t.lookahead--,0===t.strm.avail_out)return 1}else t.match_available=1,t.strstart++,t.lookahead--}return t.match_available&&(i=Q(t,0,t.window[t.strstart-1]),t.match_available=0),t.insert=t.strstart<2?t.strstart:2,e===it?(xt(t,!0),0===t.strm.avail_out?3:4):t.last_lit&&(xt(t,!1),0===t.strm.avail_out)?1:2};function Bt(t,e,a,i,s){this.good_length=t,this.max_lazy=e,this.nice_length=a,this.max_chain=i,this.func=s}const Ft=[new Bt(0,0,0,0,((t,e)=>{let a=65535;for(a>t.pending_buf_size-5&&(a=t.pending_buf_size-5);;){if(t.lookahead<=1){if(St(t),0===t.lookahead&&e===tt)return 1;if(0===t.lookahead)break}t.strstart+=t.lookahead,t.lookahead=0;const i=t.block_start+a;if((0===t.strstart||t.strstart>=i)&&(t.lookahead=t.strstart-i,t.strstart=i,xt(t,!1),0===t.strm.avail_out))return 1;if(t.strstart-t.block_start>=t.w_size-262&&(xt(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===it?(xt(t,!0),0===t.strm.avail_out?3:4):(t.strstart>t.block_start&&(xt(t,!1),t.strm.avail_out),1)})),new Bt(4,4,8,4,Rt),new Bt(4,5,16,8,Rt),new Bt(4,6,32,32,Rt),new Bt(4,4,16,16,It),new Bt(8,16,32,32,It),new Bt(8,16,128,128,It),new Bt(8,32,128,256,It),new Bt(32,128,258,1024,It),new Bt(32,258,258,4096,It)];function Zt(){this.strm=null,this.status=0,this.pending_buf=null,this.pending_buf_size=0,this.pending_out=0,this.pending=0,this.wrap=0,this.gzhead=null,this.gzindex=0,this.method=mt,this.last_flush=-1,this.w_size=0,this.w_bits=0,this.w_mask=0,this.window=null,this.window_size=0,this.prev=null,this.head=null,this.ins_h=0,this.hash_size=0,this.hash_bits=0,this.hash_mask=0,this.hash_shift=0,this.block_start=0,this.match_length=0,this.prev_match=0,this.match_available=0,this.strstart=0,this.match_start=0,this.lookahead=0,this.prev_length=0,this.max_chain_length=0,this.max_lazy_match=0,this.level=0,this.strategy=0,this.good_match=0,this.nice_match=0,this.dyn_ltree=new Uint16Array(1146),this.dyn_dtree=new Uint16Array(122),this.bl_tree=new Uint16Array(78),kt(this.dyn_ltree),kt(this.dyn_dtree),kt(this.bl_tree),this.l_desc=null,this.d_desc=null,this.bl_desc=null,this.bl_count=new Uint16Array(16),this.heap=new Uint16Array(573),kt(this.heap),this.heap_len=0,this.heap_max=0,this.depth=new Uint16Array(573),kt(this.depth),this.l_buf=0,this.lit_bufsize=0,this.last_lit=0,this.d_buf=0,this.opt_len=0,this.static_len=0,this.matches=0,this.insert=0,this.bi_buf=0,this.bi_valid=0}const Tt=t=>{if(!t||!t.state)return pt(t,ot);t.total_in=t.total_out=0,t.data_type=wt;const e=t.state;return e.pending=0,e.pending_out=0,e.wrap<0&&(e.wrap=-e.wrap),e.status=e.wrap?42:113,t.adler=2===e.wrap?0:1,e.last_flush=tt,X(e),nt},Dt=t=>{const e=Tt(t);var a;return e===nt&&((a=t.state).window_size=2*a.w_size,kt(a.head),a.max_lazy_match=Ft[a.level].max_lazy,a.good_match=Ft[a.level].good_length,a.nice_match=Ft[a.level].nice_length,a.max_chain_length=Ft[a.level].max_chain,a.strstart=0,a.block_start=0,a.lookahead=0,a.insert=0,a.match_length=a.prev_length=2,a.match_available=0,a.ins_h=0),e},Ct=(t,e,a,i,s,n)=>{if(!t)return ot;let r=1;if(e===dt&&(e=6),i<0?(r=0,i=-i):i>15&&(r=2,i-=16),s<1||s>9||a!==mt||i<8||i>15||e<0||e>9||n<0||n>ut)return pt(t,ot);8===i&&(i=9);const o=new Zt;return t.state=o,o.strm=t,o.wrap=r,o.gzhead=null,o.w_bits=i,o.w_size=1<<o.w_bits,o.w_mask=o.w_size-1,o.hash_bits=s+7,o.hash_size=1<<o.hash_bits,o.hash_mask=o.hash_size-1,o.hash_shift=~~((o.hash_bits+3-1)/3),o.window=new Uint8Array(2*o.w_size),o.head=new Uint16Array(o.hash_size),o.prev=new Uint16Array(o.w_size),o.lit_bufsize=1<<s+6,o.pending_buf_size=4*o.lit_bufsize,o.pending_buf=new Uint8Array(o.pending_buf_size),o.d_buf=1*o.lit_bufsize,o.l_buf=3*o.lit_bufsize,o.level=e,o.strategy=n,o.method=a,Dt(t)};var Lt={deflateInit:(t,e)=>Ct(t,e,mt,15,8,gt),deflateInit2:Ct,deflateReset:Dt,deflateResetKeep:Tt,deflateSetHeader:(t,e)=>t&&t.state?2!==t.state.wrap?ot:(t.state.gzhead=e,nt):ot,deflate:(t,e)=>{let a,i;if(!t||!t.state||e>st||e<0)return t?pt(t,ot):ot;const s=t.state;if(!t.output||!t.input&&0!==t.avail_in||666===s.status&&e!==it)return pt(t,0===t.avail_out?ht:ot);s.strm=t;const n=s.last_flush;if(s.last_flush=e,42===s.status)if(2===s.wrap)t.adler=0,zt(s,31),zt(s,139),zt(s,8),s.gzhead?(zt(s,(s.gzhead.text?1:0)+(s.gzhead.hcrc?2:0)+(s.gzhead.extra?4:0)+(s.gzhead.name?8:0)+(s.gzhead.comment?16:0)),zt(s,255&s.gzhead.time),zt(s,s.gzhead.time>>8&255),zt(s,s.gzhead.time>>16&255),zt(s,s.gzhead.time>>24&255),zt(s,9===s.level?2:s.strategy>=ct||s.level<2?4:0),zt(s,255&s.gzhead.os),s.gzhead.extra&&s.gzhead.extra.length&&(zt(s,255&s.gzhead.extra.length),zt(s,s.gzhead.extra.length>>8&255)),s.gzhead.hcrc&&(t.adler=$(t.adler,s.pending_buf,s.pending,0)),s.gzindex=0,s.status=69):(zt(s,0),zt(s,0),zt(s,0),zt(s,0),zt(s,0),zt(s,9===s.level?2:s.strategy>=ct||s.level<2?4:0),zt(s,3),s.status=113);else{let e=mt+(s.w_bits-8<<4)<<8,a=-1;a=s.strategy>=ct||s.level<2?0:s.level<6?1:6===s.level?2:3,e|=a<<6,0!==s.strstart&&(e|=32),e+=31-e%31,s.status=113,Ut(s,e),0!==s.strstart&&(Ut(s,t.adler>>>16),Ut(s,65535&t.adler)),t.adler=1}if(69===s.status)if(s.gzhead.extra){for(a=s.pending;s.gzindex<(65535&s.gzhead.extra.length)&&(s.pending!==s.pending_buf_size||(s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),vt(t),a=s.pending,s.pending!==s.pending_buf_size));)zt(s,255&s.gzhead.extra[s.gzindex]),s.gzindex++;s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),s.gzindex===s.gzhead.extra.length&&(s.gzindex=0,s.status=73)}else s.status=73;if(73===s.status)if(s.gzhead.name){a=s.pending;do{if(s.pending===s.pending_buf_size&&(s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),vt(t),a=s.pending,s.pending===s.pending_buf_size)){i=1;break}i=s.gzindex<s.gzhead.name.length?255&s.gzhead.name.charCodeAt(s.gzindex++):0,zt(s,i)}while(0!==i);s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),0===i&&(s.gzindex=0,s.status=91)}else s.status=91;if(91===s.status)if(s.gzhead.comment){a=s.pending;do{if(s.pending===s.pending_buf_size&&(s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),vt(t),a=s.pending,s.pending===s.pending_buf_size)){i=1;break}i=s.gzindex<s.gzhead.comment.length?255&s.gzhead.comment.charCodeAt(s.gzindex++):0,zt(s,i)}while(0!==i);s.gzhead.hcrc&&s.pending>a&&(t.adler=$(t.adler,s.pending_buf,s.pending-a,a)),0===i&&(s.status=103)}else s.status=103;if(103===s.status&&(s.gzhead.hcrc?(s.pending+2>s.pending_buf_size&&vt(t),s.pending+2<=s.pending_buf_size&&(zt(s,255&t.adler),zt(s,t.adler>>8&255),t.adler=0,s.status=113)):s.status=113),0!==s.pending){if(vt(t),0===t.avail_out)return s.last_flush=-1,nt}else if(0===t.avail_in&&bt(e)<=bt(n)&&e!==it)return pt(t,ht);if(666===s.status&&0!==t.avail_in)return pt(t,ht);if(0!==t.avail_in||0!==s.lookahead||e!==tt&&666!==s.status){let a=s.strategy===ct?((t,e)=>{let a;for(;;){if(0===t.lookahead&&(St(t),0===t.lookahead)){if(e===tt)return 1;break}if(t.match_length=0,a=Q(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++,a&&(xt(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===it?(xt(t,!0),0===t.strm.avail_out?3:4):t.last_lit&&(xt(t,!1),0===t.strm.avail_out)?1:2})(s,e):s.strategy===ft?((t,e)=>{let a,i,s,n;const r=t.window;for(;;){if(t.lookahead<=258){if(St(t),t.lookahead<=258&&e===tt)return 1;if(0===t.lookahead)break}if(t.match_length=0,t.lookahead>=3&&t.strstart>0&&(s=t.strstart-1,i=r[s],i===r[++s]&&i===r[++s]&&i===r[++s])){n=t.strstart+258;do{}while(i===r[++s]&&i===r[++s]&&i===r[++s]&&i===r[++s]&&i===r[++s]&&i===r[++s]&&i===r[++s]&&i===r[++s]&&s<n);t.match_length=258-(n-s),t.match_length>t.lookahead&&(t.match_length=t.lookahead)}if(t.match_length>=3?(a=Q(t,1,t.match_length-3),t.lookahead-=t.match_length,t.strstart+=t.match_length,t.match_length=0):(a=Q(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++),a&&(xt(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===it?(xt(t,!0),0===t.strm.avail_out?3:4):t.last_lit&&(xt(t,!1),0===t.strm.avail_out)?1:2})(s,e):Ft[s.level].func(s,e);if(3!==a&&4!==a||(s.status=666),1===a||3===a)return 0===t.avail_out&&(s.last_flush=-1),nt;if(2===a&&(e===et?V(s):e!==st&&(q(s,0,0,!1),e===at&&(kt(s.head),0===s.lookahead&&(s.strstart=0,s.block_start=0,s.insert=0))),vt(t),0===t.avail_out))return s.last_flush=-1,nt}return e!==it?nt:s.wrap<=0?rt:(2===s.wrap?(zt(s,255&t.adler),zt(s,t.adler>>8&255),zt(s,t.adler>>16&255),zt(s,t.adler>>24&255),zt(s,255&t.total_in),zt(s,t.total_in>>8&255),zt(s,t.total_in>>16&255),zt(s,t.total_in>>24&255)):(Ut(s,t.adler>>>16),Ut(s,65535&t.adler)),vt(t),s.wrap>0&&(s.wrap=-s.wrap),0!==s.pending?nt:rt)},deflateEnd:t=>{if(!t||!t.state)return ot;const e=t.state.status;return 42!==e&&69!==e&&73!==e&&91!==e&&103!==e&&113!==e&&666!==e?pt(t,ot):(t.state=null,113===e?pt(t,lt):nt)},deflateSetDictionary:(t,e)=>{let a=e.length;if(!t||!t.state)return ot;const i=t.state,s=i.wrap;if(2===s||1===s&&42!==i.status||i.lookahead)return ot;if(1===s&&(t.adler=K(t.adler,e,a,0)),i.wrap=0,a>=i.w_size){0===s&&(kt(i.head),i.strstart=0,i.block_start=0,i.insert=0);let t=new Uint8Array(i.w_size);t.set(e.subarray(a-i.w_size,a),0),e=t,a=i.w_size}const n=t.avail_in,r=t.next_in,o=t.input;for(t.avail_in=a,t.next_in=0,t.input=e,St(i);i.lookahead>=3;){let t=i.strstart,e=i.lookahead-2;do{i.ins_h=yt(i,i.ins_h,i.window[t+3-1]),i.prev[t&i.w_mask]=i.head[i.ins_h],i.head[i.ins_h]=t,t++}while(--e);i.strstart=t,i.lookahead=2,St(i)}return i.strstart+=i.lookahead,i.block_start=i.strstart,i.insert=i.lookahead,i.lookahead=0,i.match_length=i.prev_length=2,i.match_available=0,t.next_in=r,t.input=o,t.avail_in=n,i.wrap=s,nt},deflateInfo:"pako deflate (from Nodeca project)"};const Ot=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var Nt=function(t){const e=Array.prototype.slice.call(arguments,1);for(;e.length;){const a=e.shift();if(a){if("object"!=typeof a)throw new TypeError(a+"must be non-object");for(const e in a)Ot(a,e)&&(t[e]=a[e])}}return t},Mt=t=>{let e=0;for(let a=0,i=t.length;a<i;a++)e+=t[a].length;const a=new Uint8Array(e);for(let e=0,i=0,s=t.length;e<s;e++){let s=t[e];a.set(s,i),i+=s.length}return a};let Ht=!0;try{String.fromCharCode.apply(null,new Uint8Array(1))}catch(t){Ht=!1}const Pt=new Uint8Array(256);for(let t=0;t<256;t++)Pt[t]=t>=252?6:t>=248?5:t>=240?4:t>=224?3:t>=192?2:1;Pt[254]=Pt[254]=1;var jt=t=>{let e,a,i,s,n,r=t.length,o=0;for(s=0;s<r;s++)a=t.charCodeAt(s),55296==(64512&a)&&s+1<r&&(i=t.charCodeAt(s+1),56320==(64512&i)&&(a=65536+(a-55296<<10)+(i-56320),s++)),o+=a<128?1:a<2048?2:a<65536?3:4;for(e=new Uint8Array(o),n=0,s=0;n<o;s++)a=t.charCodeAt(s),55296==(64512&a)&&s+1<r&&(i=t.charCodeAt(s+1),56320==(64512&i)&&(a=65536+(a-55296<<10)+(i-56320),s++)),a<128?e[n++]=a:a<2048?(e[n++]=192|a>>>6,e[n++]=128|63&a):a<65536?(e[n++]=224|a>>>12,e[n++]=128|a>>>6&63,e[n++]=128|63&a):(e[n++]=240|a>>>18,e[n++]=128|a>>>12&63,e[n++]=128|a>>>6&63,e[n++]=128|63&a);return e},Kt=(t,e)=>{let a,i;const s=e||t.length,n=new Array(2*s);for(i=0,a=0;a<s;){let e=t[a++];if(e<128){n[i++]=e;continue}let r=Pt[e];if(r>4)n[i++]=65533,a+=r-1;else{for(e&=2===r?31:3===r?15:7;r>1&&a<s;)e=e<<6|63&t[a++],r--;r>1?n[i++]=65533:e<65536?n[i++]=e:(e-=65536,n[i++]=55296|e>>10&1023,n[i++]=56320|1023&e)}}return((t,e)=>{if(e<65534&&t.subarray&&Ht)return String.fromCharCode.apply(null,t.length===e?t:t.subarray(0,e));let a="";for(let i=0;i<e;i++)a+=String.fromCharCode(t[i]);return a})(n,i)},Wt=(t,e)=>{(e=e||t.length)>t.length&&(e=t.length);let a=e-1;for(;a>=0&&128==(192&t[a]);)a--;return a<0||0===a?e:a+Pt[t[a]]>e?a:e};var $t=function(){this.input=null,this.next_in=0,this.avail_in=0,this.total_in=0,this.output=null,this.next_out=0,this.avail_out=0,this.total_out=0,this.msg="",this.state=null,this.data_type=2,this.adler=0};const Yt=Object.prototype.toString,{Z_NO_FLUSH:Gt,Z_SYNC_FLUSH:Xt,Z_FULL_FLUSH:qt,Z_FINISH:Jt,Z_OK:Qt,Z_STREAM_END:Vt,Z_DEFAULT_COMPRESSION:te,Z_DEFAULT_STRATEGY:ee,Z_DEFLATED:ae}=G;function ie(t){this.options=Nt({level:te,method:ae,chunkSize:16384,windowBits:15,memLevel:8,strategy:ee},t||{});let e=this.options;e.raw&&e.windowBits>0?e.windowBits=-e.windowBits:e.gzip&&e.windowBits>0&&e.windowBits<16&&(e.windowBits+=16),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new $t,this.strm.avail_out=0;let a=Lt.deflateInit2(this.strm,e.level,e.method,e.windowBits,e.memLevel,e.strategy);if(a!==Qt)throw new Error(Y[a]);if(e.header&&Lt.deflateSetHeader(this.strm,e.header),e.dictionary){let t;if(t="string"==typeof e.dictionary?jt(e.dictionary):"[object ArrayBuffer]"===Yt.call(e.dictionary)?new Uint8Array(e.dictionary):e.dictionary,a=Lt.deflateSetDictionary(this.strm,t),a!==Qt)throw new Error(Y[a]);this._dict_set=!0}}function se(t,e){const a=new ie(e);if(a.push(t,!0),a.err)throw a.msg||Y[a.err];return a.result}ie.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize;let s,n;if(this.ended)return!1;for(n=e===~~e?e:!0===e?Jt:Gt,"string"==typeof t?a.input=jt(t):"[object ArrayBuffer]"===Yt.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;)if(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),(n===Xt||n===qt)&&a.avail_out<=6)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else{if(s=Lt.deflate(a,n),s===Vt)return a.next_out>0&&this.onData(a.output.subarray(0,a.next_out)),s=Lt.deflateEnd(this.strm),this.onEnd(s),this.ended=!0,s===Qt;if(0!==a.avail_out){if(n>0&&a.next_out>0)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else if(0===a.avail_in)break}else this.onData(a.output)}return!0},ie.prototype.onData=function(t){this.chunks.push(t)},ie.prototype.onEnd=function(t){t===Qt&&(this.result=Mt(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};var ne={Deflate:ie,deflate:se,deflateRaw:function(t,e){return(e=e||{}).raw=!0,se(t,e)},gzip:function(t,e){return(e=e||{}).gzip=!0,se(t,e)},constants:G};var re=function(t,e){let a,i,s,n,r,o,l,h,d,_,c,f,u,g,w,m,p,b,k,y,v,x,z,U;const E=t.state;a=t.next_in,z=t.input,i=a+(t.avail_in-5),s=t.next_out,U=t.output,n=s-(e-t.avail_out),r=s+(t.avail_out-257),o=E.dmax,l=E.wsize,h=E.whave,d=E.wnext,_=E.window,c=E.hold,f=E.bits,u=E.lencode,g=E.distcode,w=(1<<E.lenbits)-1,m=(1<<E.distbits)-1;t:do{f<15&&(c+=z[a++]<<f,f+=8,c+=z[a++]<<f,f+=8),p=u[c&w];e:for(;;){if(b=p>>>24,c>>>=b,f-=b,b=p>>>16&255,0===b)U[s++]=65535&p;else{if(!(16&b)){if(0==(64&b)){p=u[(65535&p)+(c&(1<<b)-1)];continue e}if(32&b){E.mode=12;break t}t.msg="invalid literal/length code",E.mode=30;break t}k=65535&p,b&=15,b&&(f<b&&(c+=z[a++]<<f,f+=8),k+=c&(1<<b)-1,c>>>=b,f-=b),f<15&&(c+=z[a++]<<f,f+=8,c+=z[a++]<<f,f+=8),p=g[c&m];a:for(;;){if(b=p>>>24,c>>>=b,f-=b,b=p>>>16&255,!(16&b)){if(0==(64&b)){p=g[(65535&p)+(c&(1<<b)-1)];continue a}t.msg="invalid distance code",E.mode=30;break t}if(y=65535&p,b&=15,f<b&&(c+=z[a++]<<f,f+=8,f<b&&(c+=z[a++]<<f,f+=8)),y+=c&(1<<b)-1,y>o){t.msg="invalid distance too far back",E.mode=30;break t}if(c>>>=b,f-=b,b=s-n,y>b){if(b=y-b,b>h&&E.sane){t.msg="invalid distance too far back",E.mode=30;break t}if(v=0,x=_,0===d){if(v+=l-b,b<k){k-=b;do{U[s++]=_[v++]}while(--b);v=s-y,x=U}}else if(d<b){if(v+=l+d-b,b-=d,b<k){k-=b;do{U[s++]=_[v++]}while(--b);if(v=0,d<k){b=d,k-=b;do{U[s++]=_[v++]}while(--b);v=s-y,x=U}}}else if(v+=d-b,b<k){k-=b;do{U[s++]=_[v++]}while(--b);v=s-y,x=U}for(;k>2;)U[s++]=x[v++],U[s++]=x[v++],U[s++]=x[v++],k-=3;k&&(U[s++]=x[v++],k>1&&(U[s++]=x[v++]))}else{v=s-y;do{U[s++]=U[v++],U[s++]=U[v++],U[s++]=U[v++],k-=3}while(k>2);k&&(U[s++]=U[v++],k>1&&(U[s++]=U[v++]))}break}}break}}while(a<i&&s<r);k=f>>3,a-=k,f-=k<<3,c&=(1<<f)-1,t.next_in=a,t.next_out=s,t.avail_in=a<i?i-a+5:5-(a-i),t.avail_out=s<r?r-s+257:257-(s-r),E.hold=c,E.bits=f};const oe=new Uint16Array([3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0]),le=new Uint8Array([16,16,16,16,16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,16,72,78]),he=new Uint16Array([1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0]),de=new Uint8Array([16,16,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,64,64]);var _e=(t,e,a,i,s,n,r,o)=>{const l=o.bits;let h,d,_,c,f,u,g=0,w=0,m=0,p=0,b=0,k=0,y=0,v=0,x=0,z=0,U=null,E=0;const A=new Uint16Array(16),S=new Uint16Array(16);let R,I,B,F=null,Z=0;for(g=0;g<=15;g++)A[g]=0;for(w=0;w<i;w++)A[e[a+w]]++;for(b=l,p=15;p>=1&&0===A[p];p--);if(b>p&&(b=p),0===p)return s[n++]=20971520,s[n++]=20971520,o.bits=1,0;for(m=1;m<p&&0===A[m];m++);for(b<m&&(b=m),v=1,g=1;g<=15;g++)if(v<<=1,v-=A[g],v<0)return-1;if(v>0&&(0===t||1!==p))return-1;for(S[1]=0,g=1;g<15;g++)S[g+1]=S[g]+A[g];for(w=0;w<i;w++)0!==e[a+w]&&(r[S[e[a+w]]++]=w);if(0===t?(U=F=r,u=19):1===t?(U=oe,E-=257,F=le,Z-=257,u=256):(U=he,F=de,u=-1),z=0,w=0,g=m,f=n,k=b,y=0,_=-1,x=1<<b,c=x-1,1===t&&x>852||2===t&&x>592)return 1;for(;;){R=g-y,r[w]<u?(I=0,B=r[w]):r[w]>u?(I=F[Z+r[w]],B=U[E+r[w]]):(I=96,B=0),h=1<<g-y,d=1<<k,m=d;do{d-=h,s[f+(z>>y)+d]=R<<24|I<<16|B|0}while(0!==d);for(h=1<<g-1;z&h;)h>>=1;if(0!==h?(z&=h-1,z+=h):z=0,w++,0==--A[g]){if(g===p)break;g=e[a+r[w]]}if(g>b&&(z&c)!==_){for(0===y&&(y=b),f+=m,k=g-y,v=1<<k;k+y<p&&(v-=A[k+y],!(v<=0));)k++,v<<=1;if(x+=1<<k,1===t&&x>852||2===t&&x>592)return 1;_=z&c,s[_]=b<<24|k<<16|f-n|0}}return 0!==z&&(s[f+z]=g-y<<24|64<<16|0),o.bits=b,0};const{Z_FINISH:ce,Z_BLOCK:fe,Z_TREES:ue,Z_OK:ge,Z_STREAM_END:we,Z_NEED_DICT:me,Z_STREAM_ERROR:pe,Z_DATA_ERROR:be,Z_MEM_ERROR:ke,Z_BUF_ERROR:ye,Z_DEFLATED:ve}=G,xe=t=>(t>>>24&255)+(t>>>8&65280)+((65280&t)<<8)+((255&t)<<24);function ze(){this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}const Ue=t=>{if(!t||!t.state)return pe;const e=t.state;return t.total_in=t.total_out=e.total=0,t.msg="",e.wrap&&(t.adler=1&e.wrap),e.mode=1,e.last=0,e.havedict=0,e.dmax=32768,e.head=null,e.hold=0,e.bits=0,e.lencode=e.lendyn=new Int32Array(852),e.distcode=e.distdyn=new Int32Array(592),e.sane=1,e.back=-1,ge},Ee=t=>{if(!t||!t.state)return pe;const e=t.state;return e.wsize=0,e.whave=0,e.wnext=0,Ue(t)},Ae=(t,e)=>{let a;if(!t||!t.state)return pe;const i=t.state;return e<0?(a=0,e=-e):(a=1+(e>>4),e<48&&(e&=15)),e&&(e<8||e>15)?pe:(null!==i.window&&i.wbits!==e&&(i.window=null),i.wrap=a,i.wbits=e,Ee(t))},Se=(t,e)=>{if(!t)return pe;const a=new ze;t.state=a,a.window=null;const i=Ae(t,e);return i!==ge&&(t.state=null),i};let Re,Ie,Be=!0;const Fe=t=>{if(Be){Re=new Int32Array(512),Ie=new Int32Array(32);let e=0;for(;e<144;)t.lens[e++]=8;for(;e<256;)t.lens[e++]=9;for(;e<280;)t.lens[e++]=7;for(;e<288;)t.lens[e++]=8;for(_e(1,t.lens,0,288,Re,0,t.work,{bits:9}),e=0;e<32;)t.lens[e++]=5;_e(2,t.lens,0,32,Ie,0,t.work,{bits:5}),Be=!1}t.lencode=Re,t.lenbits=9,t.distcode=Ie,t.distbits=5},Ze=(t,e,a,i)=>{let s;const n=t.state;return null===n.window&&(n.wsize=1<<n.wbits,n.wnext=0,n.whave=0,n.window=new Uint8Array(n.wsize)),i>=n.wsize?(n.window.set(e.subarray(a-n.wsize,a),0),n.wnext=0,n.whave=n.wsize):(s=n.wsize-n.wnext,s>i&&(s=i),n.window.set(e.subarray(a-i,a-i+s),n.wnext),(i-=s)?(n.window.set(e.subarray(a-i,a),0),n.wnext=i,n.whave=n.wsize):(n.wnext+=s,n.wnext===n.wsize&&(n.wnext=0),n.whave<n.wsize&&(n.whave+=s))),0};var Te={inflateReset:Ee,inflateReset2:Ae,inflateResetKeep:Ue,inflateInit:t=>Se(t,15),inflateInit2:Se,inflate:(t,e)=>{let a,i,s,n,r,o,l,h,d,_,c,f,u,g,w,m,p,b,k,y,v,x,z=0;const U=new Uint8Array(4);let E,A;const S=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);if(!t||!t.state||!t.output||!t.input&&0!==t.avail_in)return pe;a=t.state,12===a.mode&&(a.mode=13),r=t.next_out,s=t.output,l=t.avail_out,n=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,_=o,c=l,x=ge;t:for(;;)switch(a.mode){case 1:if(0===a.wrap){a.mode=13;break}for(;d<16;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(2&a.wrap&&35615===h){a.check=0,U[0]=255&h,U[1]=h>>>8&255,a.check=$(a.check,U,2,0),h=0,d=0,a.mode=2;break}if(a.flags=0,a.head&&(a.head.done=!1),!(1&a.wrap)||(((255&h)<<8)+(h>>8))%31){t.msg="incorrect header check",a.mode=30;break}if((15&h)!==ve){t.msg="unknown compression method",a.mode=30;break}if(h>>>=4,d-=4,v=8+(15&h),0===a.wbits)a.wbits=v;else if(v>a.wbits){t.msg="invalid window size",a.mode=30;break}a.dmax=1<<a.wbits,t.adler=a.check=1,a.mode=512&h?10:12,h=0,d=0;break;case 2:for(;d<16;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(a.flags=h,(255&a.flags)!==ve){t.msg="unknown compression method",a.mode=30;break}if(57344&a.flags){t.msg="unknown header flags set",a.mode=30;break}a.head&&(a.head.text=h>>8&1),512&a.flags&&(U[0]=255&h,U[1]=h>>>8&255,a.check=$(a.check,U,2,0)),h=0,d=0,a.mode=3;case 3:for(;d<32;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.head&&(a.head.time=h),512&a.flags&&(U[0]=255&h,U[1]=h>>>8&255,U[2]=h>>>16&255,U[3]=h>>>24&255,a.check=$(a.check,U,4,0)),h=0,d=0,a.mode=4;case 4:for(;d<16;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.head&&(a.head.xflags=255&h,a.head.os=h>>8),512&a.flags&&(U[0]=255&h,U[1]=h>>>8&255,a.check=$(a.check,U,2,0)),h=0,d=0,a.mode=5;case 5:if(1024&a.flags){for(;d<16;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.length=h,a.head&&(a.head.extra_len=h),512&a.flags&&(U[0]=255&h,U[1]=h>>>8&255,a.check=$(a.check,U,2,0)),h=0,d=0}else a.head&&(a.head.extra=null);a.mode=6;case 6:if(1024&a.flags&&(f=a.length,f>o&&(f=o),f&&(a.head&&(v=a.head.extra_len-a.length,a.head.extra||(a.head.extra=new Uint8Array(a.head.extra_len)),a.head.extra.set(i.subarray(n,n+f),v)),512&a.flags&&(a.check=$(a.check,i,f,n)),o-=f,n+=f,a.length-=f),a.length))break t;a.length=0,a.mode=7;case 7:if(2048&a.flags){if(0===o)break t;f=0;do{v=i[n+f++],a.head&&v&&a.length<65536&&(a.head.name+=String.fromCharCode(v))}while(v&&f<o);if(512&a.flags&&(a.check=$(a.check,i,f,n)),o-=f,n+=f,v)break t}else a.head&&(a.head.name=null);a.length=0,a.mode=8;case 8:if(4096&a.flags){if(0===o)break t;f=0;do{v=i[n+f++],a.head&&v&&a.length<65536&&(a.head.comment+=String.fromCharCode(v))}while(v&&f<o);if(512&a.flags&&(a.check=$(a.check,i,f,n)),o-=f,n+=f,v)break t}else a.head&&(a.head.comment=null);a.mode=9;case 9:if(512&a.flags){for(;d<16;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(h!==(65535&a.check)){t.msg="header crc mismatch",a.mode=30;break}h=0,d=0}a.head&&(a.head.hcrc=a.flags>>9&1,a.head.done=!0),t.adler=a.check=0,a.mode=12;break;case 10:for(;d<32;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}t.adler=a.check=xe(h),h=0,d=0,a.mode=11;case 11:if(0===a.havedict)return t.next_out=r,t.avail_out=l,t.next_in=n,t.avail_in=o,a.hold=h,a.bits=d,me;t.adler=a.check=1,a.mode=12;case 12:if(e===fe||e===ue)break t;case 13:if(a.last){h>>>=7&d,d-=7&d,a.mode=27;break}for(;d<3;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}switch(a.last=1&h,h>>>=1,d-=1,3&h){case 0:a.mode=14;break;case 1:if(Fe(a),a.mode=20,e===ue){h>>>=2,d-=2;break t}break;case 2:a.mode=17;break;case 3:t.msg="invalid block type",a.mode=30}h>>>=2,d-=2;break;case 14:for(h>>>=7&d,d-=7&d;d<32;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if((65535&h)!=(h>>>16^65535)){t.msg="invalid stored block lengths",a.mode=30;break}if(a.length=65535&h,h=0,d=0,a.mode=15,e===ue)break t;case 15:a.mode=16;case 16:if(f=a.length,f){if(f>o&&(f=o),f>l&&(f=l),0===f)break t;s.set(i.subarray(n,n+f),r),o-=f,n+=f,l-=f,r+=f,a.length-=f;break}a.mode=12;break;case 17:for(;d<14;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(a.nlen=257+(31&h),h>>>=5,d-=5,a.ndist=1+(31&h),h>>>=5,d-=5,a.ncode=4+(15&h),h>>>=4,d-=4,a.nlen>286||a.ndist>30){t.msg="too many length or distance symbols",a.mode=30;break}a.have=0,a.mode=18;case 18:for(;a.have<a.ncode;){for(;d<3;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.lens[S[a.have++]]=7&h,h>>>=3,d-=3}for(;a.have<19;)a.lens[S[a.have++]]=0;if(a.lencode=a.lendyn,a.lenbits=7,E={bits:a.lenbits},x=_e(0,a.lens,0,19,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid code lengths set",a.mode=30;break}a.have=0,a.mode=19;case 19:for(;a.have<a.nlen+a.ndist;){for(;z=a.lencode[h&(1<<a.lenbits)-1],w=z>>>24,m=z>>>16&255,p=65535&z,!(w<=d);){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(p<16)h>>>=w,d-=w,a.lens[a.have++]=p;else{if(16===p){for(A=w+2;d<A;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(h>>>=w,d-=w,0===a.have){t.msg="invalid bit length repeat",a.mode=30;break}v=a.lens[a.have-1],f=3+(3&h),h>>>=2,d-=2}else if(17===p){for(A=w+3;d<A;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}h>>>=w,d-=w,v=0,f=3+(7&h),h>>>=3,d-=3}else{for(A=w+7;d<A;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}h>>>=w,d-=w,v=0,f=11+(127&h),h>>>=7,d-=7}if(a.have+f>a.nlen+a.ndist){t.msg="invalid bit length repeat",a.mode=30;break}for(;f--;)a.lens[a.have++]=v}}if(30===a.mode)break;if(0===a.lens[256]){t.msg="invalid code -- missing end-of-block",a.mode=30;break}if(a.lenbits=9,E={bits:a.lenbits},x=_e(1,a.lens,0,a.nlen,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid literal/lengths set",a.mode=30;break}if(a.distbits=6,a.distcode=a.distdyn,E={bits:a.distbits},x=_e(2,a.lens,a.nlen,a.ndist,a.distcode,0,a.work,E),a.distbits=E.bits,x){t.msg="invalid distances set",a.mode=30;break}if(a.mode=20,e===ue)break t;case 20:a.mode=21;case 21:if(o>=6&&l>=258){t.next_out=r,t.avail_out=l,t.next_in=n,t.avail_in=o,a.hold=h,a.bits=d,re(t,c),r=t.next_out,s=t.output,l=t.avail_out,n=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,12===a.mode&&(a.back=-1);break}for(a.back=0;z=a.lencode[h&(1<<a.lenbits)-1],w=z>>>24,m=z>>>16&255,p=65535&z,!(w<=d);){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(m&&0==(240&m)){for(b=w,k=m,y=p;z=a.lencode[y+((h&(1<<b+k)-1)>>b)],w=z>>>24,m=z>>>16&255,p=65535&z,!(b+w<=d);){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}h>>>=b,d-=b,a.back+=b}if(h>>>=w,d-=w,a.back+=w,a.length=p,0===m){a.mode=26;break}if(32&m){a.back=-1,a.mode=12;break}if(64&m){t.msg="invalid literal/length code",a.mode=30;break}a.extra=15&m,a.mode=22;case 22:if(a.extra){for(A=a.extra;d<A;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.length+=h&(1<<a.extra)-1,h>>>=a.extra,d-=a.extra,a.back+=a.extra}a.was=a.length,a.mode=23;case 23:for(;z=a.distcode[h&(1<<a.distbits)-1],w=z>>>24,m=z>>>16&255,p=65535&z,!(w<=d);){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(0==(240&m)){for(b=w,k=m,y=p;z=a.distcode[y+((h&(1<<b+k)-1)>>b)],w=z>>>24,m=z>>>16&255,p=65535&z,!(b+w<=d);){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}h>>>=b,d-=b,a.back+=b}if(h>>>=w,d-=w,a.back+=w,64&m){t.msg="invalid distance code",a.mode=30;break}a.offset=p,a.extra=15&m,a.mode=24;case 24:if(a.extra){for(A=a.extra;d<A;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}a.offset+=h&(1<<a.extra)-1,h>>>=a.extra,d-=a.extra,a.back+=a.extra}if(a.offset>a.dmax){t.msg="invalid distance too far back",a.mode=30;break}a.mode=25;case 25:if(0===l)break t;if(f=c-l,a.offset>f){if(f=a.offset-f,f>a.whave&&a.sane){t.msg="invalid distance too far back",a.mode=30;break}f>a.wnext?(f-=a.wnext,u=a.wsize-f):u=a.wnext-f,f>a.length&&(f=a.length),g=a.window}else g=s,u=r-a.offset,f=a.length;f>l&&(f=l),l-=f,a.length-=f;do{s[r++]=g[u++]}while(--f);0===a.length&&(a.mode=21);break;case 26:if(0===l)break t;s[r++]=a.length,l--,a.mode=21;break;case 27:if(a.wrap){for(;d<32;){if(0===o)break t;o--,h|=i[n++]<<d,d+=8}if(c-=l,t.total_out+=c,a.total+=c,c&&(t.adler=a.check=a.flags?$(a.check,s,c,r-c):K(a.check,s,c,r-c)),c=l,(a.flags?h:xe(h))!==a.check){t.msg="incorrect data check",a.mode=30;break}h=0,d=0}a.mode=28;case 28:if(a.wrap&&a.flags){for(;d<32;){if(0===o)break t;o--,h+=i[n++]<<d,d+=8}if(h!==(4294967295&a.total)){t.msg="incorrect length check",a.mode=30;break}h=0,d=0}a.mode=29;case 29:x=we;break t;case 30:x=be;break t;case 31:return ke;case 32:default:return pe}return t.next_out=r,t.avail_out=l,t.next_in=n,t.avail_in=o,a.hold=h,a.bits=d,(a.wsize||c!==t.avail_out&&a.mode<30&&(a.mode<27||e!==ce))&&Ze(t,t.output,t.next_out,c-t.avail_out),_-=t.avail_in,c-=t.avail_out,t.total_in+=_,t.total_out+=c,a.total+=c,a.wrap&&c&&(t.adler=a.check=a.flags?$(a.check,s,c,t.next_out-c):K(a.check,s,c,t.next_out-c)),t.data_type=a.bits+(a.last?64:0)+(12===a.mode?128:0)+(20===a.mode||15===a.mode?256:0),(0===_&&0===c||e===ce)&&x===ge&&(x=ye),x},inflateEnd:t=>{if(!t||!t.state)return pe;let e=t.state;return e.window&&(e.window=null),t.state=null,ge},inflateGetHeader:(t,e)=>{if(!t||!t.state)return pe;const a=t.state;return 0==(2&a.wrap)?pe:(a.head=e,e.done=!1,ge)},inflateSetDictionary:(t,e)=>{const a=e.length;let i,s,n;return t&&t.state?(i=t.state,0!==i.wrap&&11!==i.mode?pe:11===i.mode&&(s=1,s=K(s,e,a,0),s!==i.check)?be:(n=Ze(t,e,a,a),n?(i.mode=31,ke):(i.havedict=1,ge))):pe},inflateInfo:"pako inflate (from Nodeca project)"};var De=function(){this.text=0,this.time=0,this.xflags=0,this.os=0,this.extra=null,this.extra_len=0,this.name="",this.comment="",this.hcrc=0,this.done=!1};const Ce=Object.prototype.toString,{Z_NO_FLUSH:Le,Z_FINISH:Oe,Z_OK:Ne,Z_STREAM_END:Me,Z_NEED_DICT:He,Z_STREAM_ERROR:Pe,Z_DATA_ERROR:je,Z_MEM_ERROR:Ke}=G;function We(t){this.options=Nt({chunkSize:65536,windowBits:15,to:""},t||{});const e=this.options;e.raw&&e.windowBits>=0&&e.windowBits<16&&(e.windowBits=-e.windowBits,0===e.windowBits&&(e.windowBits=-15)),!(e.windowBits>=0&&e.windowBits<16)||t&&t.windowBits||(e.windowBits+=32),e.windowBits>15&&e.windowBits<48&&0==(15&e.windowBits)&&(e.windowBits|=15),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new $t,this.strm.avail_out=0;let a=Te.inflateInit2(this.strm,e.windowBits);if(a!==Ne)throw new Error(Y[a]);if(this.header=new De,Te.inflateGetHeader(this.strm,this.header),e.dictionary&&("string"==typeof e.dictionary?e.dictionary=jt(e.dictionary):"[object ArrayBuffer]"===Ce.call(e.dictionary)&&(e.dictionary=new Uint8Array(e.dictionary)),e.raw&&(a=Te.inflateSetDictionary(this.strm,e.dictionary),a!==Ne)))throw new Error(Y[a])}We.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize,s=this.options.dictionary;let n,r,o;if(this.ended)return!1;for(r=e===~~e?e:!0===e?Oe:Le,"[object ArrayBuffer]"===Ce.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;){for(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),n=Te.inflate(a,r),n===He&&s&&(n=Te.inflateSetDictionary(a,s),n===Ne?n=Te.inflate(a,r):n===je&&(n=He));a.avail_in>0&&n===Me&&a.state.wrap>0&&0!==t[a.next_in];)Te.inflateReset(a),n=Te.inflate(a,r);switch(n){case Pe:case je:case He:case Ke:return this.onEnd(n),this.ended=!0,!1}if(o=a.avail_out,a.next_out&&(0===a.avail_out||n===Me))if("string"===this.options.to){let t=Wt(a.output,a.next_out),e=a.next_out-t,s=Kt(a.output,t);a.next_out=e,a.avail_out=i-e,e&&a.output.set(a.output.subarray(t,t+e),0),this.onData(s)}else this.onData(a.output.length===a.next_out?a.output:a.output.subarray(0,a.next_out));if(n!==Ne||0!==o){if(n===Me)return n=Te.inflateEnd(this.strm),this.onEnd(n),this.ended=!0,!0;if(0===a.avail_in)break}}return!0},We.prototype.onData=function(t){this.chunks.push(t)},We.prototype.onEnd=function(t){t===Ne&&("string"===this.options.to?this.result=this.chunks.join(""):this.result=Mt(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};const{Deflate:$e,deflate:Ye,deflateRaw:Ge,gzip:Xe}=ne;var qe=Ye;class Je extends EventTarget{constructor(t,e,a){super(),this.port=t,this.logger=e,this._parent=a,this.chipName=null,this._efuses=new Array(4).fill(0),this._flashsize=4194304,this.debug=!1,this.IS_STUB=!1,this.connected=!0,this.stopReadLoop=!1}get _inputBuffer(){return this._parent?this._parent._inputBuffer:this.__inputBuffer}async initialize(){await this.hardReset(!0),this._parent||(this.__inputBuffer=[],this.readLoop()),await this.sync();let t,e=await this.readRegister(1610612856);if(353510656==e)this.chipFamily=o;else if(401408==e)this.chipFamily=33382;else{if(1280!=e)throw new Error("Unknown Chip.");this.chipFamily=l}33382==this.chipFamily?t=1072693328:(this.chipFamily==o||this.chipFamily==l)&&(t=1610719232);for(let e=0;e<4;e++)this._efuses[e]=await this.readRegister(t+4*e);this.chipFamily==o&&(this.chipName="ESP32"),this.chipFamily==l&&(this.chipName="ESP32-S2"),33382==this.chipFamily&&(16&this._efuses[0]||65536&this._efuses[2]?this.chipName="ESP8285":this.chipName="ESP8266EX")}async readLoop(){this.logger.debug("Starting read loop"),this._reader=this.port.readable.getReader();try{for(;!this.stopReadLoop;){const{value:t,done:e}=await this._reader.read();if(e){this._reader.releaseLock();break}t&&0!==t.length&&this._inputBuffer.push(...Array.from(t))}}catch(t){console.error("Read loop got disconnected"),this.connected=!1,this.dispatchEvent(new Event("disconnect"))}this.logger.debug("Finished read loop")}async hardReset(t=!1){this.logger.log("Try hard reset."),await this.port.setSignals({dataTerminalReady:!1,requestToSend:!0}),await this.port.setSignals({dataTerminalReady:t,requestToSend:!1}),await new Promise((t=>setTimeout(t,1e3)))}macAddr(){let t,e=new Array(6).fill(0),a=this._efuses[0],i=this._efuses[1],s=this._efuses[2],n=this._efuses[3];if(33382==this.chipFamily){if(0!=n)t=[n>>16&255,n>>8&255,255&n];else if(0==(i>>16&255))t=[24,254,52];else{if(1!=(i>>16&255))throw new Error("Couldnt determine OUI");t=[172,208,116]}e[0]=t[0],e[1]=t[1],e[2]=t[2],e[3]=i>>8&255,e[4]=255&i,e[5]=a>>24&255}else if(this.chipFamily==o)e[0]=s>>8&255,e[1]=255&s,e[2]=i>>24&255,e[3]=i>>16&255,e[4]=i>>8&255,e[5]=255&i;else{if(this.chipFamily!=l)throw new Error("Unknown chip family");e[0]=s>>8&255,e[1]=255&s,e[2]=i>>24&255,e[3]=i>>16&255,e[4]=i>>8&255,e[5]=255&i}return e}async readRegister(t){this.debug&&this.logger.debug("Reading Register",t);let e=a("I",t);return((t,e)=>{let a=0,i=[];for(let s of t)if("B"==s)i.push(255&e[a]),a+=1;else if("H"==s)i.push(255&e[a]|(255&e[a+1])<<8),a+=2;else{if("I"!=s)throw new Error(`Unhandled character "${s}" in unpack format`);i.push(255&e[a]|(255&e[a+1])<<8|(255&e[a+2])<<16|(255&e[a+3])<<24),a+=4}return i})("I",(await this.checkCommand(10,e))[0])[0]}async checkCommand(t,e,a=0,s=3e3){s=Math.min(s,12e5),await this.sendCommand(t,e,a);let[n,r]=await this.getResponse(t,s);if(null===r)throw new Error("Didn't get enough status bytes");let h=0;if(this.IS_STUB||33382==this.chipFamily?h=2:[o,l].includes(this.chipFamily)?h=4:[2,4].includes(r.length)&&(h=r.length),r.length<h)throw new Error("Didn't get enough status bytes");let d=r.slice(-h,r.length);if(r=r.slice(0,-h),this.debug&&(this.logger.debug("status",d),this.logger.debug("value",n),this.logger.debug("data",r)),1==d[0])throw 5==d[1]?new Error("Invalid (unsupported) command "+i(t)):new Error("Command failure error code "+i(d[1]));return[n,r]}async sendCommand(e,i,s=0){this._inputBuffer.length=0;let n=[192,0];n.push(e),n=n.concat(a("H",i.length)),n=n.concat(t(a("I",s))),n=n.concat(t(i)),n.push(192),this.debug&&this.logger.debug("Writing "+n.length+" byte"+(1==n.length?"":"s")+":",n),await this.writeToStream(n)}async getResponse(t,e=3e3){let a=[],i=0,n=!1,r=Date.now();for(;Date.now()-r<e;){if(this._inputBuffer.length>0){let t=this._inputBuffer.shift();219==t?n=!0:n?(221==t?a.push(220):220==t?a.push(192):a=a.concat([219,t]),n=!1):a.push(t)}else await s(10);if(a.length>0&&192!=a[0]&&a.shift(),a.length>1&&1!=a[1]&&a.shift(),a.length>2&&a[2]!=t&&a.shift(),a.length>4&&(i=a[3]+(a[4]<<8)),a.length==i+10)break}if(a.length!=i+10)return this.logger.log("Timed out after "+e+" milliseconds"),[null,null];this.debug&&this.logger.debug("Reading "+a.length+" byte"+(1==a.length?"":"s")+":",a);let o=a.slice(5,9),l=a.slice(9,-1);return this.debug&&this.logger.debug("value:",o,"data:",l),[o,l]}async readBuffer(t=3e3){let e=[],a=!1,i=Date.now();for(;Date.now()-i<t;){if(this._inputBuffer.length>0){let t=this._inputBuffer.shift();219==t?a=!0:a?(221==t?e.push(220):220==t?e.push(192):e=e.concat([219,t]),a=!1):e.push(t)}else await s(10);if(e.length>0&&192!=e[0]&&e.shift(),e.length>1&&192==e[e.length-1])break}if(e.length<2)return this.logger.log("Timed out after "+t+" milliseconds"),null;this.debug&&this.logger.debug("Reading "+e.length+" byte"+(1==e.length?"":"s")+":",e);let n=e.slice(1,-1);return this.debug&&this.logger.debug("data:",n),n}checksum(t,e=239){for(let a of t)e^=a;return e}async setBaudrate(t){if(33382==this.chipFamily)throw new Error("Changing baud rate is not supported on the ESP8266");this.logger.log("Attempting to change baud rate to "+t+"...");try{let e=a("<II",t,this.IS_STUB?115200:0);await this.checkCommand(15,e)}catch(e){throw console.error(e),new Error(`Unable to change the baud rate to ${t}: No response from set baud rate command.`)}this._parent?await this._parent.reconfigurePort(t):await this.reconfigurePort(t)}async reconfigurePort(t){var e,a;try{this.stopReadLoop=!0,await(null===(e=this._reader)||void 0===e?void 0:e.cancel()),null===(a=this._reader)||void 0===a||a.releaseLock(),await this.port.close(),await this.port.open({baudRate:t}),this.stopReadLoop=!1,this.readLoop(),this.logger.log(`Changed baud rate to ${t}`)}catch(e){throw console.error(e),new Error(`Unable to change the baud rate to ${t}: ${e}`)}}async sync(){for(let t=0;t<5;t++){if(await this._sync())return await s(100),!0;await s(100)}throw new Error("Couldn't sync to ESP. Try resetting.")}async _sync(){await this.sendCommand(8,n);for(let t=0;t<8;t++){let[t,e]=await this.getResponse(8,100);if(null!==e&&(e.length>1&&0==e[0]&&0==e[1]))return!0}return!1}getFlashWriteSize(){return this.IS_STUB?16384:1024}async flashData(t,e,a=0,s=!1){let n,r=t.byteLength,o=0;s?(n=qe(new Uint8Array(t),{level:9}).buffer,o=n.byteLength,this.logger.log(`Writing data with filesize: ${r}. Compressed Size: ${o}`),await this.flashDeflBegin(r,o,a)):(this.logger.log(`Writing data with filesize: ${r}`),n=t,await this.flashBegin(r,a));let l=[],h=0,d=0,_=0,c=Date.now(),f=this.getFlashWriteSize(),u=s?o:r;for(;u-_>0;)this.debug&&this.logger.log(`Writing at ${i(a+h*f,8)} `),u-_>=f?l=Array.from(new Uint8Array(n,_,f)):(l=Array.from(new Uint8Array(n,_,u-_)),s||(l=l.concat(new Array(f-l.length).fill(255)))),s?await this.flashDeflBlock(l,h,2e3):await this.flashBlock(l,h,2e3),h+=1,d+=s?Math.round(l.length*r/o):l.length,_+=f,e(d,u);this.logger.log("Took "+(Date.now()-c)+"ms to write "+u+" bytes"),this.IS_STUB&&(await this.flashBegin(0,0),s?await this.flashDeflFinish():await this.flashFinish())}async flashBlock(t,e,i=100){await this.checkCommand(3,a("<IIII",t.length,e,0,0).concat(t),this.checksum(t),i)}async flashDeflBlock(t,e,i=100){await this.checkCommand(17,a("<IIII",t.length,e,0,0).concat(t),this.checksum(t))}async flashBegin(t=0,e=0,s=!1){let n,r,d=this.getFlashWriteSize();[o,l].includes(this.chipFamily)&&await this.checkCommand(13,new Array(8).fill(0)),this.chipFamily==o&&(r=a("<IIIIII",0,this._flashsize,65536,4096,256,65535),await this.checkCommand(11,r));let _,c=Math.floor((t+d-1)/d);n=33382==this.chipFamily?this.getEraseSize(e,t):t,_=this.IS_STUB?3e3:h(3e4,t);let f=Date.now();return r=a("<IIII",n,c,d,e),this.chipFamily==l&&(r=r.concat(a("<I",s?1:0))),this.logger.log("Erase size "+n+", blocks "+c+", block size "+d+", offset "+i(e,4)+", encrypted "+(s?"yes":"no")),await this.checkCommand(2,r,0,_),0==t||this.IS_STUB||this.logger.log("Took "+(Date.now()-f)+"ms to erase "+c+" bytes"),c}async flashDeflBegin(t=0,e=0,i=0,s=!1){let n,r=this.getFlashWriteSize(),o=Math.floor((e+r-1)/r),l=Math.floor((t+r-1)/r),d=0,_=0;return this.IS_STUB?(d=t,_=3e3):(d=l*r,_=h(3e4,d)),n=a("<IIII",d,o,r,i),await this.checkCommand(16,n,0,_),o}async flashFinish(){let t=a("<I",1);await this.checkCommand(4,t)}async flashDeflFinish(){let t=a("<I",1);await this.checkCommand(18,t)}getEraseSize(t,e){let a=4096,i=Math.floor((e+a-1)/a),s=16-Math.floor(t/a)%16;return i<s&&(s=i),i<2*s?Math.floor((i+1)/2*a):(i-s)*a}async memBegin(t,e,i,s){return await this.checkCommand(5,a("<IIII",t,e,i,s))}async memBlock(t,e){return await this.checkCommand(7,a("<IIII",t.length,e,0,0).concat(t),this.checksum(t))}async memFinish(t=0){let e=this.IS_STUB?3e3:50,i=a("<II",0==t?1:0,t);return await this.checkCommand(6,i,0,e)}async runStub(){const t=await d(this.chipFamily);let e=2048;this.logger.log("Uploading stub...");for(let a of["text","data"])if(Object.keys(t).includes(a)){let i=t[a+"_start"],s=t[a].length,n=Math.floor((s+e-1)/e);await this.memBegin(s,n,e,i);for(let i of Array(n).keys()){let n=i*e,r=n+e;r>s&&(r=s),await this.memBlock(t[a].slice(n,r),i)}}this.logger.log("Running stub..."),await this.memFinish(t.entry);const a=await this.readBuffer(100),i=String.fromCharCode(...a);if("OHAI"!=i)throw new Error("Failed to start stub. Unexpected response: "+i);this.logger.log("Stub is now running...");return new Qe(this.port,this.logger,this)}async writeToStream(t){const e=this.port.writable.getWriter();await e.write(new Uint8Array(t));try{e.releaseLock()}catch(t){console.error("Ignoring release lock error",t)}}async disconnect(){this._parent?await this._parent.disconnect():(this._reader&&await this._reader.cancel(),await this.port.writable.getWriter().close(),await this.port.close(),this.connected=!1)}}class Qe extends Je{constructor(){super(...arguments),this.IS_STUB=!0}async memBegin(t,e,a,s){let n=await d(this.chipFamily),r=s,o=s+t;console.log(r,o),console.log(n.data_start,n.data.length,n.text_start,n.text.length);for(let[t,e]of[[n.data_start,n.data_start+n.data.length],[n.text_start,n.text_start+n.text.length]])if(r<e&&o>t)throw new Error("Software loader is resident at "+i(t,8)+"-"+i(e,8)+". Can't load binary at overlapping address range "+i(r,8)+"-"+i(o,8)+". Try changing the binary loading address.")}async eraseFlash(){await this.checkCommand(208,[],0,6e5)}}const Ve=async t=>{const e=await navigator.serial.requestPort();return t.log("Connecting..."),await e.open({baudRate:115200}),t.log("Connected successfully."),new Je(e,t)};export{o as CHIP_FAMILY_ESP32,l as CHIP_FAMILY_ESP32S2,r as CHIP_FAMILY_ESP8266,Je as ESPLoader,Ve as connect};
{
"name": "esp-web-flasher",
"version": "2.1.1",
"version": "3.0.0",
"description": "Flash ESP devices using WebSerial",

@@ -16,2 +16,3 @@ "main": "dist/index.js",

"@rollup/plugin-typescript": "^8.2.1",
"@types/pako": "^1.0.1",
"@types/w3c-web-serial": "^1.0.1",

@@ -25,4 +26,5 @@ "prettier": "^2.3.0",

"dependencies": {
"pako": "^2.0.3",
"tslib": "^2.2.0"
}
}

@@ -13,4 +13,2 @@ # ESP Web Flasher

This is a fork of [Adafruit WebSerial ESPTool](https://github.com/adafruit/Adafruit_WebSerial_ESPTool). Their repository is a single-use website. This fork converts the code into a reusable package.
The changes of this fork are part of [an open pull request](https://github.com/adafruit/Adafruit_WebSerial_ESPTool/pull/10) upstream. If accepted in a way that it's usable for our projects, we can retire this project.
This is a fork of [Adafruit WebSerial ESPTool](https://github.com/adafruit/Adafruit_WebSerial_ESPTool). At the time fo the fork, their repository was a single-use website. This fork converted the code into a reusable package and published it to NPM.

@@ -61,2 +61,5 @@ import { toByteArray } from "./util";

export const ESP_CHECKSUM_MAGIC = 0xef;
export const ESP_FLASH_DEFL_BEGIN = 0x10;
export const ESP_FLASH_DEFL_DATA = 0x11;
export const ESP_FLASH_DEFL_END = 0x12;

@@ -63,0 +66,0 @@ export const ROM_INVALID_RECV_MSG = 0x05;

@@ -37,5 +37,9 @@ import {

ESP_ROM_BAUD,
ESP_FLASH_DEFL_BEGIN,
ESP_FLASH_DEFL_DATA,
ESP_FLASH_DEFL_END,
} from "./const";
import { getStubCode } from "./stubs";
import { pack, sleep, slipEncode, toHex, unpack } from "./util";
import * as pako from "pako";

@@ -558,12 +562,33 @@ export class ESPLoader extends EventTarget {

binaryData: ArrayBuffer,
updateProgress: (bytesWritten: number) => void,
offset = 0
updateProgress: (bytesWritten: number, totalBytes: number) => void,
offset = 0,
compress = false
) {
let filesize = binaryData.byteLength;
this.logger.log("Writing data with filesize:" + filesize);
await this.flashBegin(filesize, offset);
let uncompressedFilesize = binaryData.byteLength;
let compressedFilesize = 0;
let dataToFlash;
if (compress) {
dataToFlash = pako.deflate(new Uint8Array(binaryData), {
level: 9,
}).buffer;
compressedFilesize = dataToFlash.byteLength;
this.logger.log(
`Writing data with filesize: ${uncompressedFilesize}. Compressed Size: ${compressedFilesize}`
);
await this.flashDeflBegin(
uncompressedFilesize,
compressedFilesize,
offset
);
} else {
this.logger.log(`Writing data with filesize: ${uncompressedFilesize}`);
dataToFlash = binaryData;
await this.flashBegin(uncompressedFilesize, offset);
}
let block = [];
let seq = 0;
let written = 0;
// let address = offset;
let position = 0;

@@ -573,24 +598,38 @@ let stamp = Date.now();

let filesize = compress ? compressedFilesize : uncompressedFilesize;
while (filesize - position > 0) {
/*logMsg(
"Writing at " + toHex(address + seq * flashWriteSize, 8) + "... (" + percentage + " %)"
);*/
if (this.debug) {
this.logger.log(
`Writing at ${toHex(offset + seq * flashWriteSize, 8)} `
);
}
if (filesize - position >= flashWriteSize) {
block = Array.from(
new Uint8Array(binaryData, position, flashWriteSize)
new Uint8Array(dataToFlash, position, flashWriteSize)
);
} else {
// Pad the last block
// Pad the last block only if we are sending uncompressed data.
block = Array.from(
new Uint8Array(binaryData, position, filesize - position)
new Uint8Array(dataToFlash, position, filesize - position)
);
block = block.concat(
new Array(flashWriteSize - block.length).fill(0xff)
);
if (!compress) {
block = block.concat(
new Array(flashWriteSize - block.length).fill(0xff)
);
}
}
await this.flashBlock(block, seq, 2000);
if (compress) {
await this.flashDeflBlock(block, seq, 2000);
} else {
await this.flashBlock(block, seq, 2000);
}
seq += 1;
written += block.length;
// If using compression we update the progress with the proportional size of the block taking into account the compression ratio.
// This way we report progress on the uncompressed size
written += compress
? Math.round((block.length * uncompressedFilesize) / compressedFilesize)
: block.length;
position += flashWriteSize;
updateProgress(written);
updateProgress(written, filesize);
}

@@ -600,2 +639,12 @@ this.logger.log(

);
// Only send flashF finish if running the stub because ir causes the ROM to exit and run user code
if (this.IS_STUB) {
await this.flashBegin(0, 0);
if (compress) {
await this.flashDeflFinish();
} else {
await this.flashFinish();
}
}
}

@@ -615,2 +664,9 @@

}
async flashDeflBlock(data: number[], seq: number, timeout = 100) {
await this.checkCommand(
ESP_FLASH_DEFL_DATA,
pack("<IIII", data.length, seq, 0, 0).concat(data),
this.checksum(data)
);
}

@@ -674,2 +730,38 @@ /**

/**
* @name flashDeflBegin
*
*/
async flashDeflBegin(
size = 0,
compressedSize = 0,
offset = 0,
encrypted = false
) {
// Start downloading compressed data to Flash (performs an erase)
// Returns number of blocks to write.
let flashWriteSize = this.getFlashWriteSize();
let numBlocks = Math.floor(
(compressedSize + flashWriteSize - 1) / flashWriteSize
);
let eraseBlocks = Math.floor((size + flashWriteSize - 1) / flashWriteSize);
let writeSize = 0;
let timeout = 0;
let buffer;
if (this.IS_STUB) {
writeSize = size; // stub expects number of bytes here, manages erasing internally
timeout = DEFAULT_TIMEOUT;
} else {
writeSize = eraseBlocks * flashWriteSize; // ROM expects rounded up to erase block size
timeout = timeoutPerMb(ERASE_REGION_TIMEOUT_PER_MB, writeSize); // ROM performs the erase up front
}
buffer = pack("<IIII", writeSize, numBlocks, flashWriteSize, offset);
await this.checkCommand(ESP_FLASH_DEFL_BEGIN, buffer, 0, timeout);
return numBlocks;
}
async flashFinish() {

@@ -680,2 +772,7 @@ let buffer = pack("<I", 1);

async flashDeflFinish() {
let buffer = pack("<I", 1);
await this.checkCommand(ESP_FLASH_DEFL_END, buffer);
}
/**

@@ -682,0 +779,0 @@ * @name getEraseSize

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc