esptool-js icon indicating copy to clipboard operation
esptool-js copied to clipboard

use uint8array instead of string for write flash command

Open brianignacio5 opened this issue 2 months ago • 3 comments

Replace the old reader.readAsBinaryString to use reader.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 Uint8Array for 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:

  • Refactored all firmware image classes (ESP32FirmwareImage, ESP8266ROMFirmwareImage, ESP8266V2FirmwareImage, ESP32S2FirmwareImage, ESP32S3FirmwareImage, ESP32C3FirmwareImage) to accept and process Uint8Array instead of strings. The loadFromFile methods now handle both Uint8Array and string inputs, converting as needed. ([[1]](diffhunk://#diff-cb8f3ca2232e60e52ce55bcea56211c937fbb15d93f6f7b0c9834b0bde6607bbL43-R43), [[2]](diffhunk://#diff-cb8f3ca2232e60e52ce55bcea56211c937fbb15d93f6f7b0c9834b0bde6607bbL54-R56), [[3]](diffhunk://#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L10-R10), [[4]](diffhunk://#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L21-R22), [[5]](diffhunk://#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L51-R51), [[6]](diffhunk://#diff-fe7be74db67cb782f352691f2c1260d0f8847db03a0d9cf5b9197e5d5c67dbf4L62-R63), [[7]](diffhunk://#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL15-R20), [[8]](diffhunk://#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL24-R34), [[9]](diffhunk://#diff-3e3b9a4fb5a60a561e65244d8b4e06b18654430df28ae7acab669324a9694cdbL33-R48))
  • Updated the loadFirmwareImage function and related type annotations to support Uint8Array | string for image data, ensuring all image loading is done with binary-safe types. ([[1]](diffhunk://#diff-bc708f2006b74482b85e56e07e24d033a3916a4b9a10fd10b4cea2732f57e66fL20-R27), [[2]](diffhunk://#diff-bc708f2006b74482b85e56e07e24d033a3916a4b9a10fd10b4cea2732f57e66fR83-R94))

File reading and UI integration:

  • Changed file reading in the TypeScript example (index.ts) to use readAsArrayBuffer and convert the result to Uint8Array, updating all downstream references to use binary data. ([[1]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R72-R79), [[2]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718L423-R430))
  • Updated the MD5 hash calculation to work with binary data by converting Uint8Array to a Latin1 string before hashing. ([examples/typescript/src/index.tsR447-R460](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))

Firmware flashing logic improvements:

  • Refactored the core flashing logic in ESPLoader to work with Uint8Array throughout: image padding, compression, block writing, and SHA digest recalculation now operate on binary data, removing legacy string conversions and improving correctness. ([[1]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1342-R1353), [[2]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1366-R1368), [[3]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1383-R1391), [[4]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1409-R1464), [[5]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1476-R1485), [[6]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1485-R1494), [[7]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1498-R1509), [[8]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1520-R1531), [[9]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1529-R1546), [[10]](diffhunk://#diff-9c5587f99fb0aa42042f10b8d0d9e019be9130ded25135b21bddd818c86a3873L1552-R1565))

Error logging and UI feedback:

  • Added eslint-disable-next-line no-console comments and improved error logging for UI actions (connect, erase, program) to provide better feedback during failures. ([[1]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R114), [[2]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R126), [[3]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R151), [[4]](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))

Flash options and compatibility:

  • Updated flash options to include flashMode: "keep" and flashFreq: "keep" for compatibility and user feedback during flashing. ([examples/typescript/src/index.tsR447-R460](diffhunk://#diff-86a9903e156e927494bb34e6a8a5af426ba2201cec729680b9cd916609be5718R447-R460))

Testing

Use the examples/typescript to flash a set of binaries to your Espressif device.

Checklist

Before submitting a Pull Request, please ensure the following:

  • [ ] 🚨 This PR does not introduce breaking changes.
  • [ ] All CI checks (GH Actions) pass.
  • [ ] Documentation is updated as needed.
  • [ ] Tests are updated or added as necessary.
  • [ ] Code is well-commented, especially in complex areas.
  • [ ] Git history is clean — commits are squashed to the minimum necessary.

brianignacio5 avatar Dec 10 '25 04:12 brianignacio5