Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add js extensions use window atob #132

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"url": "https://github.com/espressif/esptool-js/issues"
},
"dependencies": {
"buffer": "^6.0.3",
"pako": "^2.1.0",
"tslib": "^2.4.1"
},
Expand Down
41 changes: 24 additions & 17 deletions src/esploader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ESPError } from "./error";
import { ESPError } from "./error.js";
import { Data, deflate, Inflate } from "pako";
import { Transport, SerialOptions } from "./webserial";
import { ROM } from "./targets/rom";
import { customReset, usbJTAGSerialReset } from "./reset";
import { Buffer } from "buffer/index";
import { Transport, SerialOptions } from "./webserial.js";
import { ROM } from "./targets/rom.js";
import { customReset, usbJTAGSerialReset } from "./reset.js";

/* global SerialPort */

Expand Down Expand Up @@ -120,39 +119,39 @@ type FlashReadCallback = ((packet: Uint8Array, progress: number, totalSize: numb
async function magic2Chip(magic: number): Promise<ROM | null> {
switch (magic) {
case 0x00f01d83: {
const { ESP32ROM } = await import("./targets/esp32");
const { ESP32ROM } = await import("./targets/esp32.js");
return new ESP32ROM();
}
case 0x6f51306f:
case 0x7c41a06f: {
const { ESP32C2ROM } = await import("./targets/esp32c2");
const { ESP32C2ROM } = await import("./targets/esp32c2.js");
return new ESP32C2ROM();
}
case 0x6921506f:
case 0x1b31506f:
case 0x4881606f:
case 0x4361606f: {
const { ESP32C3ROM } = await import("./targets/esp32c3");
const { ESP32C3ROM } = await import("./targets/esp32c3.js");
return new ESP32C3ROM();
}
case 0x2ce0806f: {
const { ESP32C6ROM } = await import("./targets/esp32c6");
const { ESP32C6ROM } = await import("./targets/esp32c6.js");
return new ESP32C6ROM();
}
case 0xd7b73e80: {
const { ESP32H2ROM } = await import("./targets/esp32h2");
const { ESP32H2ROM } = await import("./targets/esp32h2.js");
return new ESP32H2ROM();
}
case 0x09: {
const { ESP32S3ROM } = await import("./targets/esp32s3");
const { ESP32S3ROM } = await import("./targets/esp32s3.js");
return new ESP32S3ROM();
}
case 0x000007c6: {
const { ESP32S2ROM } = await import("./targets/esp32s2");
const { ESP32S2ROM } = await import("./targets/esp32s2.js");
return new ESP32S2ROM();
}
case 0xfff0c101: {
const { ESP8266ROM } = await import("./targets/esp8266");
const { ESP8266ROM } = await import("./targets/esp8266.js");
return new ESP8266ROM();
}
default:
Expand Down Expand Up @@ -1135,19 +1134,27 @@ export class ESPLoader {
return resp;
}

decodeBase64(romText: string) {
if (typeof window !== "undefined" && typeof window.atob === "function") {
return window.atob(romText);
} else {
return Buffer.from(romText, "base64").toString("binary");
}
}

/**
* Upload the flasher ROM bootloader (flasher stub) to the chip.
* @returns {ROM} The Chip ROM
*/
async runStub() {
async runStub(): Promise<ROM> {
this.info("Uploading stub...");
let decoded = Buffer.from(this.chip.ROM_TEXT, "base64").toString("binary");
let decoded = this.decodeBase64(this.chip.ROM_TEXT);
let chardata = decoded.split("").map(function (x) {
return x.charCodeAt(0);
});
const text = new Uint8Array(chardata);

decoded = Buffer.from(this.chip.ROM_DATA, "base64").toString("binary");
decoded = this.decodeBase64(this.chip.ROM_DATA);
chardata = decoded.split("").map(function (x) {
return x.charCodeAt(0);
});
Expand Down Expand Up @@ -1221,7 +1228,7 @@ export class ESPLoader {
/**
* Execute the main function of ESPLoader.
* @param {string} mode Reset mode to use
* @returns {ROM} chip ROM
* @returns {string} chip ROM
*/
async main(mode = "default_reset") {
await this.detectChip(mode);
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export { IEspLoaderTerminal, ESPLoader, FlashOptions, LoaderOptions } from "./esploader";
export { classicReset, customReset, hardReset, usbJTAGSerialReset, validateCustomResetStringSequence } from "./reset";
export { ROM } from "./targets/rom";
export { Transport, SerialOptions } from "./webserial";
export { IEspLoaderTerminal, ESPLoader, FlashOptions, LoaderOptions } from "./esploader.js";
export {
classicReset,
customReset,
hardReset,
usbJTAGSerialReset,
validateCustomResetStringSequence,
} from "./reset.js";
export { ROM } from "./targets/rom.js";
export { Transport, SerialOptions } from "./webserial.js";
2 changes: 1 addition & 1 deletion src/reset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transport } from "./webserial";
import { Transport } from "./webserial.js";

const DEFAULT_RESET_DELAY = 50;

Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32_STUB from "./stub_flasher/stub_flasher_32.json";

export class ESP32ROM extends ROM {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32c2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ESP32C3ROM } from "./esp32c3";
import { ESPLoader } from "../esploader.js";
import { ESP32C3ROM } from "./esp32c3.js";
import ESP32C2_STUB from "./stub_flasher/stub_flasher_32c2.json";

export class ESP32C2ROM extends ESP32C3ROM {
Expand Down Expand Up @@ -68,7 +68,7 @@
return desc;
}

public async getChipFeatures(loader: ESPLoader) {

Check warning on line 71 in src/targets/esp32c2.ts

View workflow job for this annotation

GitHub Actions / ci

'loader' is defined but never used
return ["Wi-Fi", "BLE"];
}

Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32c3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32C3_STUB from "./stub_flasher/stub_flasher_32c3.json";

export class ESP32C3ROM extends ROM {
Expand Down Expand Up @@ -67,11 +67,11 @@
return desc;
}

public async getChipFeatures(loader: ESPLoader) {

Check warning on line 70 in src/targets/esp32c3.ts

View workflow job for this annotation

GitHub Actions / ci

'loader' is defined but never used
return ["Wi-Fi"];
}

public async getCrystalFreq(loader: ESPLoader) {

Check warning on line 74 in src/targets/esp32c3.ts

View workflow job for this annotation

GitHub Actions / ci

'loader' is defined but never used
return 40;
}

Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32c6.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32C6_STUB from "./stub_flasher/stub_flasher_32c6.json";

export class ESP32C6ROM extends ROM {
Expand Down Expand Up @@ -67,11 +67,11 @@
return desc;
}

public async getChipFeatures(loader: ESPLoader) {

Check warning on line 70 in src/targets/esp32c6.ts

View workflow job for this annotation

GitHub Actions / ci

'loader' is defined but never used
return ["Wi-Fi"];
}

public async getCrystalFreq(loader: ESPLoader) {

Check warning on line 74 in src/targets/esp32c6.ts

View workflow job for this annotation

GitHub Actions / ci

'loader' is defined but never used
return 40;
}

Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32h2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32H2_STUB from "./stub_flasher/stub_flasher_32h2.json";

export class ESP32H2ROM extends ROM {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32s2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32S2_STUB from "./stub_flasher/stub_flasher_32s2.json";

export class ESP32S2ROM extends ROM {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp32s3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP32S3_STUB from "./stub_flasher/stub_flasher_32s3.json";

export class ESP32S3ROM extends ROM {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/esp8266.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ESPLoader } from "../esploader";
import { ROM } from "./rom";
import { ESPLoader } from "../esploader.js";
import { ROM } from "./rom.js";
import ESP8266_STUB from "./stub_flasher/stub_flasher_8266.json";

export class ESP8266ROM extends ROM {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/rom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ESPLoader } from "../esploader";
import { ESPLoader } from "../esploader.js";

/**
* Represents a chip ROM with basic registers field and abstract functions.
Expand Down
Loading