mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Refactor Dish _translate to handle sync and async depending on environment.
This commit is contained in:
parent
0a1ca18de5
commit
b48c16b4db
16 changed files with 492 additions and 223 deletions
|
@ -926,7 +926,11 @@ class Utils {
|
|||
* await Utils.readFile(new File(["hello"], "test"))
|
||||
*/
|
||||
static readFile(file) {
|
||||
if (Utils.isBrowser()) {
|
||||
|
||||
if (Utils.isNode()) {
|
||||
return Buffer.from(file).buffer;
|
||||
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
const data = new Uint8Array(file.size);
|
||||
|
@ -954,17 +958,12 @@ class Utils {
|
|||
|
||||
seek();
|
||||
});
|
||||
|
||||
} else if (Utils.isNode()) {
|
||||
return Buffer.from(file).buffer;
|
||||
}
|
||||
|
||||
throw new Error("Unkown environment!");
|
||||
}
|
||||
|
||||
/** */
|
||||
static readFileSync(file) {
|
||||
if (Utils.isBrowser()) {
|
||||
if (!Utils.isNode()) {
|
||||
throw new TypeError("Browser environment cannot support readFileSync");
|
||||
}
|
||||
|
||||
|
@ -1065,13 +1064,6 @@ class Utils {
|
|||
}[token];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if code is running in a browser environment
|
||||
*/
|
||||
static isBrowser() {
|
||||
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if code is running in a Node environment
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue