use uint8array instead of string for write flash command #226
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Replace the old
reader.readAsBinaryStringto usereader.readAsArrayBuffer(file);instead allowing user to use UInt8Array for files instead of string.Fix #219
Fix #229
Description
This pull request refactors the firmware flashing workflow to consistently use
Uint8Arrayfor binary image data instead of strings. This change improves type safety, performance, and compatibility with modern web APIs. The update touches multiple areas, including file handling, firmware image classes, and the main flashing logic. It also adds better error logging and updates MD5 hash calculation for binary data.Binary image data handling and type safety:
ESP32FirmwareImage,ESP8266ROMFirmwareImage,ESP8266V2FirmwareImage,ESP32S2FirmwareImage,ESP32S3FirmwareImage,ESP32C3FirmwareImage) to accept and processUint8Arrayinstead of strings. TheloadFromFilemethods now handle bothUint8Arrayand string inputs, converting as needed. ([[1]](https://github.com/espressif/esptool-js/pull/226/files#diff-cb8f3ca2232e60e52ce55bcea56211c937fbb15d93f6f7b0c9834b0bde6607bbL43-R43),[[2]](https://github.com/espressif/esptool-js/pull/226/files#diff-cb8f3ca2232e60e52ce55bcea56211c937fbb15d93f6f7b0c9834b0bde6607bbL54-R56),[[3]](https://github.com/espressif/esptool-js/pull/226/files#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L10-R10),[[4]](https://github.com/espressif/esptool-js/pull/226/files#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L21-R22),[[5]](https://github.com/espressif/esptool-js/pull/226/files#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L51-R51),[[6]](https://github.com/espressif/esptool-js/pull/226/files#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L62-R63),[[7]](https://github.com/espressif/esptool-js/pull/226/files#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL15-R20),[[8]](https://github.com/espressif/esptool-js/pull/226/files#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL24-R34),[[9]](https://github.com/espressif/esptool-js/pull/226/files#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL33-R48))loadFirmwareImagefunction and related type annotations to supportUint8Array | stringfor image data, ensuring all image loading is done with binary-safe types. ([[1]](https://github.com/espressif/esptool-js/pull/226/files#diff-bc708f2006b74482b85e56e07e24d033a3916a4b9a10fd10b4cea2732f57e66fL20-R27),[[2]](https://github.com/espressif/esptool-js/pull/226/files#diff-bc708f2006b74482b85e56e07e24d033a3916a4b9a10fd10b4cea2732f57e66fR83-R94))File reading and UI integration:
index.ts) to usereadAsArrayBufferand convert the result toUint8Array, updating all downstream references to use binary data. ([[1]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R72-R79),[[2]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718L423-R430))Uint8Arrayto a Latin1 string before hashing. ([examples/typescript/src/index.tsR447-R460](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))Firmware flashing logic improvements:
ESPLoaderto work withUint8Arraythroughout: image padding, compression, block writing, and SHA digest recalculation now operate on binary data, removing legacy string conversions and improving correctness. ([[1]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1342-R1353),[[2]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1366-R1368),[[3]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1383-R1391),[[4]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1409-R1464),[[5]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1476-R1485),[[6]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1485-R1494),[[7]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1498-R1509),[[8]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1520-R1531),[[9]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1529-R1546),[[10]](https://github.com/espressif/esptool-js/pull/226/files#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1552-R1565))Error logging and UI feedback:
eslint-disable-next-line no-consolecomments and improved error logging for UI actions (connect, erase, program) to provide better feedback during failures. ([[1]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R114),[[2]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R126),[[3]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R151),[[4]](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))Flash options and compatibility:
flashMode: "keep"andflashFreq: "keep"for compatibility and user feedback during flashing. ([examples/typescript/src/index.tsR447-R460](https://github.com/espressif/esptool-js/pull/226/files#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))Testing
Use the
examples/typescriptto flash a set of binaries to your Espressif device.Checklist
Before submitting a Pull Request, please ensure the following: