Procházet zdrojové kódy

swap ENVIRONMENT_IS_* functions for Utils named exports

d98762625 před 6 roky
rodič
revize
1c24c05647
41 změnil soubory, kde provedl 117 přidání a 141 odebrání
  1. 1 4
      .eslintrc.json
  2. 0 9
      Gruntfile.js
  3. 2 1
      src/core/Chef.mjs
  4. 4 4
      src/core/Dish.mjs
  5. 30 19
      src/core/Utils.mjs
  6. 2 2
      src/core/dishTypes/DishFile.mjs
  7. 2 2
      src/core/dishTypes/DishListFile.mjs
  8. 2 2
      src/core/lib/Magic.mjs
  9. 2 1
      src/core/lib/PGP.mjs
  10. 2 1
      src/core/operations/Bcrypt.mjs
  11. 3 1
      src/core/operations/BcryptCompare.mjs
  12. 2 1
      src/core/operations/BlurImage.mjs
  13. 4 3
      src/core/operations/Bombe.mjs
  14. 2 1
      src/core/operations/ContainImage.mjs
  15. 2 1
      src/core/operations/CoverImage.mjs
  16. 2 1
      src/core/operations/CropImage.mjs
  17. 2 1
      src/core/operations/DitherImage.mjs
  18. 2 1
      src/core/operations/FlipImage.mjs
  19. 3 2
      src/core/operations/FromCharcode.mjs
  20. 4 2
      src/core/operations/FromHexdump.mjs
  21. 3 2
      src/core/operations/ImageBrightnessContrast.mjs
  22. 2 1
      src/core/operations/ImageFilter.mjs
  23. 4 3
      src/core/operations/ImageHueSaturationLightness.mjs
  24. 2 1
      src/core/operations/ImageOpacity.mjs
  25. 2 1
      src/core/operations/InvertImage.mjs
  26. 5 3
      src/core/operations/MultipleBombe.mjs
  27. 2 1
      src/core/operations/PseudoRandomNumberGenerator.mjs
  28. 2 1
      src/core/operations/Register.mjs
  29. 2 1
      src/core/operations/ResizeImage.mjs
  30. 2 1
      src/core/operations/RotateImage.mjs
  31. 2 2
      src/core/operations/ScanForEmbeddedFiles.mjs
  32. 2 1
      src/core/operations/Scrypt.mjs
  33. 4 3
      src/core/operations/ToCharcode.mjs
  34. 2 1
      src/core/operations/ToMessagePack.mjs
  35. 3 2
      src/core/operations/XORBruteForce.mjs
  36. 5 4
      src/core/operations/YARARules.mjs
  37. 0 11
      src/node/config/scripts/generateNodeIndex.mjs
  38. 0 11
      src/node/index.mjs
  39. 0 10
      tests/lib/utils.mjs
  40. 0 11
      tests/node/index.mjs
  41. 0 11
      tests/operations/index.mjs

+ 1 - 4
.eslintrc.json

@@ -106,9 +106,6 @@
 
         "COMPILE_TIME": false,
         "COMPILE_MSG": false,
-        "PKG_VERSION": false,
-        "ENVIRONMENT_IS_WORKER": false,
-        "ENVIRONMENT_IS_NODE": false,
-        "ENVIRONMENT_IS_WEB": false
+        "PKG_VERSION": false
     }
 }

+ 0 - 9
Gruntfile.js

@@ -86,15 +86,6 @@ module.exports = function (grunt) {
             COMPILE_TIME: JSON.stringify(compileTime),
             COMPILE_MSG: JSON.stringify(grunt.option("compile-msg") || grunt.option("msg") || ""),
             PKG_VERSION: JSON.stringify(pkg.version),
-            ENVIRONMENT_IS_WORKER: function() {
-                return typeof importScripts === "function";
-            },
-            ENVIRONMENT_IS_NODE: function() {
-                return typeof process === "object" && process.versions !== null && process.versions.node !== null && typeof require === "function";
-            },
-            ENVIRONMENT_IS_WEB: function() {
-                return typeof window === "object";
-            }
         },
         moduleEntryPoints = listEntryModules();
 

+ 2 - 1
src/core/Chef.mjs

@@ -7,6 +7,7 @@
 import Dish from "./Dish";
 import Recipe from "./Recipe";
 import log from "loglevel";
+import { isWorkerEnvironment } from "./Utils";
 
 /**
  * The main controller for CyberChef.
@@ -46,7 +47,7 @@ class Chef {
             notUTF8     = options && options.hasOwnProperty("treatAsUtf8") && !options.treatAsUtf8;
         let error = false;
 
-        if (containsFc && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
+        if (containsFc && isWorkerEnvironment()) self.setOption("attemptHighlight", false);
 
         // Clean up progress
         if (progress >= recipeConfig.length) {

+ 4 - 4
src/core/Dish.mjs

@@ -5,7 +5,7 @@
  * @license Apache-2.0
  */
 
-import Utils from "./Utils";
+import Utils, { isNodeEnvironment } from "./Utils";
 import DishError from "./errors/DishError";
 import BigNumber from "bignumber.js";
 import log from "loglevel";
@@ -140,7 +140,7 @@ class Dish {
         if (this.type !== type) {
 
             // Node environment => _translate is sync
-            if (Utils.isNode()) {
+            if (isNodeEnvironment()) {
                 this._translate(type, notUTF8);
                 return this.value;
 
@@ -349,7 +349,7 @@ class Dish {
         log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
 
         // Node environment => translate is sync
-        if (Utils.isNode()) {
+        if (isNodeEnvironment()) {
             this._toArrayBuffer();
             this.type = Dish.ARRAY_BUFFER;
             this._fromArrayBuffer(toType, notUTF8);
@@ -404,7 +404,7 @@ class Dish {
         };
 
         try {
-            return toByteArrayFuncs[Utils.isNode() && "node" || "browser"][this.type]();
+            return toByteArrayFuncs[isNodeEnvironment() && "node" || "browser"][this.type]();
         } catch (err) {
             throw new DishError(`Error translating from ${Dish.enumLookup(this.type)} to ArrayBuffer: ${err}`);
         }

+ 30 - 19
src/core/Utils.mjs

@@ -173,7 +173,7 @@ class Utils {
      * @returns {string}
      */
     static printable(str, preserveWs=false) {
-        if (ENVIRONMENT_IS_WEB() && window.app && !window.app.options.treatAsUtf8) {
+        if (isWebEnvironment() && window.app && !window.app.options.treatAsUtf8) {
             str = Utils.byteArrayToChars(Utils.strToByteArray(str));
         }
 
@@ -410,9 +410,9 @@ class Utils {
         const utf8Str = utf8.encode(str);
 
         if (str.length !== utf8Str.length) {
-            if (ENVIRONMENT_IS_WORKER()) {
+            if (isWorkerEnvironment()) {
                 self.setOption("attemptHighlight", false);
-            } else if (ENVIRONMENT_IS_WEB()) {
+            } else if (isWebEnvironment()) {
                 window.app.options.attemptHighlight = false;
             }
         }
@@ -465,9 +465,9 @@ class Utils {
         const utf8Str = utf8.encode(str);
 
         if (str.length !== utf8Str.length) {
-            if (ENVIRONMENT_IS_WORKER()) {
+            if (isWorkerEnvironment()) {
                 self.setOption("attemptHighlight", false);
-            } else if (ENVIRONMENT_IS_WEB()) {
+            } else if (isWebEnvironment()) {
                 window.app.options.attemptHighlight = false;
             }
         }
@@ -528,9 +528,9 @@ class Utils {
         try {
             const utf8Str = utf8.decode(str);
             if (str.length !== utf8Str.length) {
-                if (ENVIRONMENT_IS_WORKER()) {
+                if (isWorkerEnvironment()) {
                     self.setOption("attemptHighlight", false);
-                } else if (ENVIRONMENT_IS_WEB()) {
+                } else if (isWebEnvironment()) {
                     window.app.options.attemptHighlight = false;
                 }
             }
@@ -992,7 +992,7 @@ class Utils {
      */
     static readFile(file) {
 
-        if (Utils.isNode()) {
+        if (isNodeEnvironment()) {
             return Buffer.from(file).buffer;
 
         } else {
@@ -1036,7 +1036,7 @@ class Utils {
      * @throws {TypeError} thrown if the method is called from a browser environment
      */
     static readFileSync(file) {
-        if (!Utils.isNode()) {
+        if (!isNodeEnvironment()) {
             throw new TypeError("Browser environment cannot support readFileSync");
         }
 
@@ -1141,13 +1141,23 @@ class Utils {
         }[token];
     }
 
-    /**
-     * Check if code is running in a Node environment
-     */
-    static isNode() {
-        return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
-    }
+}
+
+/**
+ * Check whether the code is running in a Node.js environment
+ */
+export function isNodeEnvironment() {
+    return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
+}
+
+/** */
+export function isWebEnvironment() {
+    return typeof window === "object";
+}
 
+/** */
+export function isWorkerEnvironment() {
+    return typeof importScripts === "function";
 }
 
 export default Utils;
@@ -1267,12 +1277,13 @@ String.prototype.count = function(chr) {
  * @param {string} msg
  */
 export function sendStatusMessage(msg) {
-    if (ENVIRONMENT_IS_WORKER())
+    if (isWorkerEnvironment())
         self.sendStatusMessage(msg);
-    else if (ENVIRONMENT_IS_WEB())
+    else if (isWebEnvironment())
         app.alert(msg, 10000);
-    else if (ENVIRONMENT_IS_NODE())
-        log.debug(msg);
+    else if (isNodeEnvironment())
+        // eslint-disable-next-line no-console
+        console.debug(msg);
 }
 
 

+ 2 - 2
src/core/dishTypes/DishFile.mjs

@@ -5,7 +5,7 @@
  */
 
 import DishType from "./DishType";
-import Utils from "../Utils";
+import Utils, { isNodeEnvironment } from "../Utils";
 
 /**
  * Translation methods for file Dishes
@@ -18,7 +18,7 @@ class DishFile extends DishType {
      */
     static toArrayBuffer() {
         DishFile.checkForValue(this.value);
-        if (Utils.isNode()) {
+        if (isNodeEnvironment()) {
             this.value = Utils.readFileSync(this.value);
         } else {
             return new Promise((resolve, reject) => {

+ 2 - 2
src/core/dishTypes/DishListFile.mjs

@@ -5,7 +5,7 @@
  */
 
 import DishType from "./DishType";
-import Utils from "../Utils.mjs";
+import { isNodeEnvironment } from "../Utils.mjs";
 
 
 /**
@@ -19,7 +19,7 @@ class DishListFile extends DishType {
     static toArrayBuffer() {
         DishListFile.checkForValue(this.value);
 
-        if (Utils.isNode()) {
+        if (isNodeEnvironment()) {
             this.value = this.value.map(file => Uint8Array.from(file.data));
         }
         this.value = DishListFile.concatenateTypedArrays(...this.value).buffer;

+ 2 - 2
src/core/lib/Magic.mjs

@@ -1,5 +1,5 @@
 import OperationConfig from "../config/OperationConfig.json";
-import Utils from "../Utils";
+import Utils, { isWorkerEnvironment } from "../Utils";
 import Recipe from "../Recipe";
 import Dish from "../Dish";
 import {detectFileType} from "./FileType";
@@ -390,7 +390,7 @@ class Magic {
         const dish = new Dish();
         dish.set(input, Dish.ARRAY_BUFFER);
 
-        if (ENVIRONMENT_IS_WORKER()) self.loadRequiredModules(recipeConfig);
+        if (isWorkerEnvironment()) self.loadRequiredModules(recipeConfig);
 
         const recipe = new Recipe(recipeConfig);
         try {

+ 2 - 1
src/core/lib/PGP.mjs

@@ -11,6 +11,7 @@
  */
 
 import OperationError from "../errors/OperationError";
+import { isWorkerEnvironment } from "../Utils";
 import kbpgp from "kbpgp";
 import * as es6promisify from "es6-promisify";
 const promisify = es6promisify.default ? es6promisify.default.promisify : es6promisify.promisify;
@@ -45,7 +46,7 @@ export const ASP = kbpgp.ASP({
                 msg = `Stage: ${info.what}`;
         }
 
-        if (ENVIRONMENT_IS_WORKER())
+        if (isWorkerEnvironment())
             self.sendStatusMessage(msg);
     }
 });

+ 2 - 1
src/core/operations/Bcrypt.mjs

@@ -6,6 +6,7 @@
 
 import Operation from "../Operation";
 import bcrypt from "bcryptjs";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * Bcrypt operation
@@ -44,7 +45,7 @@ class Bcrypt extends Operation {
 
         return await bcrypt.hash(input, salt, null, p => {
             // Progress callback
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
         });
 

+ 3 - 1
src/core/operations/BcryptCompare.mjs

@@ -6,6 +6,8 @@
 
 import Operation from "../Operation";
 import bcrypt from "bcryptjs";
+import { isWorkerEnvironment } from "../Utils";
+
 
 /**
  * Bcrypt compare operation
@@ -43,7 +45,7 @@ class BcryptCompare extends Operation {
 
         const match = await bcrypt.compare(input, hash, null, p => {
             // Progress callback
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
         });
 

+ 2 - 1
src/core/operations/BlurImage.mjs

@@ -6,6 +6,7 @@
 
 import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
+import { isWorkerEnvironment } from "../Utils";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64";
 import jimp from "jimp";
@@ -67,7 +68,7 @@ class BlurImage extends Operation {
                     image.blur(blurAmount);
                     break;
                 case "Gaussian":
-                    if (ENVIRONMENT_IS_WORKER())
+                    if (isWorkerEnvironment())
                         self.sendStatusMessage("Gaussian blurring image. This may take a while...");
                     image.gaussian(blurAmount);
                     break;

+ 4 - 3
src/core/operations/Bombe.mjs

@@ -8,8 +8,9 @@
 
 import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
-import {BombeMachine} from "../lib/Bombe";
-import {ROTORS, ROTORS_FOURTH, REFLECTORS, Reflector} from "../lib/Enigma";
+import { isWorkerEnvironment } from "../Utils";
+import { BombeMachine } from "../lib/Bombe";
+import { ROTORS, ROTORS_FOURTH, REFLECTORS, Reflector } from "../lib/Enigma";
 
 /**
  * Bombe operation
@@ -139,7 +140,7 @@ class Bombe extends Operation {
         const ciphertext = input.slice(offset);
         const reflector = new Reflector(reflectorstr);
         let update;
-        if (ENVIRONMENT_IS_WORKER()) {
+        if (isWorkerEnvironment()) {
             update = this.updateStatus;
         } else {
             update = undefined;

+ 2 - 1
src/core/operations/ContainImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -112,7 +113,7 @@ class ContainImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Containing image...");
             image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
             const imageBuffer = await image.getBufferAsync(jimp.AUTO);

+ 2 - 1
src/core/operations/CoverImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -112,7 +113,7 @@ class CoverImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Covering image...");
             image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
             const imageBuffer = await image.getBufferAsync(jimp.AUTO);

+ 2 - 1
src/core/operations/CropImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -103,7 +104,7 @@ class CropImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Cropping image...");
             if (autocrop) {
                 image.autocrop({

+ 2 - 1
src/core/operations/DitherImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -48,7 +49,7 @@ class DitherImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Applying dither to image...");
             image.dither565();
             const imageBuffer = await image.getBufferAsync(jimp.AUTO);

+ 2 - 1
src/core/operations/FlipImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -55,7 +56,7 @@ class FlipImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Flipping image...");
             switch (flipAxis){
                 case "Horizontal":

+ 3 - 2
src/core/operations/FromCharcode.mjs

@@ -6,7 +6,8 @@
 
 import Operation from "../Operation";
 import Utils from "../Utils";
-import {DELIM_OPTIONS} from "../lib/Delim";
+import { DELIM_OPTIONS } from "../lib/Delim";
+import { isWorkerEnvironment } from "../Utils";
 import OperationError from "../errors/OperationError";
 
 /**
@@ -61,7 +62,7 @@ class FromCharcode extends Operation {
             return [];
         }
 
-        if (base !== 16 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
+        if (base !== 16 && isWorkerEnvironment()) self.setOption("attemptHighlight", false);
 
         // Split into groups of 2 if the whole string is concatenated and
         // too long to be a single character

+ 4 - 2
src/core/operations/FromHexdump.mjs

@@ -5,7 +5,9 @@
  */
 
 import Operation from "../Operation";
-import {fromHex} from "../lib/Hex";
+import { fromHex } from "../lib/Hex";
+import { isWorkerEnvironment } from "../Utils";
+
 
 /**
  * From Hexdump operation
@@ -55,7 +57,7 @@ class FromHexdump extends Operation {
         const w = (width - 13) / 4;
         // w should be the specified width of the hexdump and therefore a round number
         if (Math.floor(w) !== w || input.indexOf("\r") !== -1 || output.indexOf(13) !== -1) {
-            if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
+            if (isWorkerEnvironment()) self.setOption("attemptHighlight", false);
         }
         return output;
     }

+ 3 - 2
src/core/operations/ImageBrightnessContrast.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -65,12 +66,12 @@ class ImageBrightnessContrast extends Operation {
         }
         try {
             if (brightness !== 0) {
-                if (ENVIRONMENT_IS_WORKER())
+                if (isWorkerEnvironment())
                     self.sendStatusMessage("Changing image brightness...");
                 image.brightness(brightness / 100);
             }
             if (contrast !== 0) {
-                if (ENVIRONMENT_IS_WORKER())
+                if (isWorkerEnvironment())
                     self.sendStatusMessage("Changing image contrast...");
                 image.contrast(contrast / 100);
             }

+ 2 - 1
src/core/operations/ImageFilter.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -58,7 +59,7 @@ class ImageFilter extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
             if (filterType === "Greyscale") {
                 image.greyscale();

+ 4 - 3
src/core/operations/ImageHueSaturationLightness.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -73,7 +74,7 @@ class ImageHueSaturationLightness extends Operation {
         }
         try {
             if (hue !== 0) {
-                if (ENVIRONMENT_IS_WORKER())
+                if (isWorkerEnvironment())
                     self.sendStatusMessage("Changing image hue...");
                 image.colour([
                     {
@@ -83,7 +84,7 @@ class ImageHueSaturationLightness extends Operation {
                 ]);
             }
             if (saturation !== 0) {
-                if (ENVIRONMENT_IS_WORKER())
+                if (isWorkerEnvironment())
                     self.sendStatusMessage("Changing image saturation...");
                 image.colour([
                     {
@@ -93,7 +94,7 @@ class ImageHueSaturationLightness extends Operation {
                 ]);
             }
             if (lightness !== 0) {
-                if (ENVIRONMENT_IS_WORKER())
+                if (isWorkerEnvironment())
                     self.sendStatusMessage("Changing image lightness...");
                 image.colour([
                     {

+ 2 - 1
src/core/operations/ImageOpacity.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -57,7 +58,7 @@ class ImageOpacity extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Changing image opacity...");
             image.opacity(opacity / 100);
 

+ 2 - 1
src/core/operations/InvertImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -48,7 +49,7 @@ class InvertImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Inverting image...");
             image.invert();
             const imageBuffer = await image.getBufferAsync(jimp.AUTO);

+ 5 - 3
src/core/operations/MultipleBombe.mjs

@@ -9,8 +9,10 @@
 
 import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
-import {BombeMachine} from "../lib/Bombe";
-import {ROTORS, ROTORS_FOURTH, REFLECTORS, Reflector} from "../lib/Enigma";
+import { BombeMachine } from "../lib/Bombe";
+import { ROTORS, ROTORS_FOURTH, REFLECTORS, Reflector } from "../lib/Enigma";
+import { isWorkerEnvironment } from "../Utils";
+
 
 /**
  * Convenience method for flattening the preset ROTORS object into a newline-separated string.
@@ -222,7 +224,7 @@ class MultipleBombe extends Operation {
         crib = crib.replace(/[^A-Za-z]/g, "").toUpperCase();
         const ciphertext = input.slice(offset);
         let update;
-        if (ENVIRONMENT_IS_WORKER()) {
+        if (isWorkerEnvironment()) {
             update = this.updateStatus;
         } else {
             update = undefined;

+ 2 - 1
src/core/operations/PseudoRandomNumberGenerator.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import Utils from "../Utils";
 import forge from "node-forge/dist/forge.min.js";
 import BigNumber from "bignumber.js";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * Pseudo-Random Number Generator operation
@@ -50,7 +51,7 @@ class PseudoRandomNumberGenerator extends Operation {
 
         let bytes;
 
-        if (ENVIRONMENT_IS_WORKER() && self.crypto) {
+        if (isWorkerEnvironment() && self.crypto) {
             bytes = self.crypto.getRandomValues(new Uint8Array(numBytes));
             bytes = Utils.arrayBufferToStr(bytes.buffer);
         } else {

+ 2 - 1
src/core/operations/Register.mjs

@@ -7,6 +7,7 @@
 import Operation from "../Operation";
 import Dish from "../Dish";
 import XRegExp from "xregexp";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * Register operation
@@ -72,7 +73,7 @@ class Register extends Operation {
 
         if (!registers) return state;
 
-        if (ENVIRONMENT_IS_WORKER()) {
+        if (isWorkerEnvironment()) {
             self.setRegisters(state.forkOffset + state.progress, state.numRegisters, registers.slice(1));
         }
 

+ 2 - 1
src/core/operations/ResizeImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64.mjs";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -102,7 +103,7 @@ class ResizeImage extends Operation {
                 height = image.getHeight() * (height / 100);
             }
 
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Resizing image...");
             if (aspect) {
                 image.scaleToFit(width, height, resizeMap[resizeAlg]);

+ 2 - 1
src/core/operations/RotateImage.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import { isImage } from "../lib/FileType";
 import { toBase64 } from "../lib/Base64";
+import { isWorkerEnvironment } from "../Utils";
 import jimp from "jimp";
 
 /**
@@ -56,7 +57,7 @@ class RotateImage extends Operation {
             throw new OperationError(`Error loading image. (${err})`);
         }
         try {
-            if (ENVIRONMENT_IS_WORKER())
+            if (isWorkerEnvironment())
                 self.sendStatusMessage("Rotating image...");
             image.rotate(degrees);
             const imageBuffer = await image.getBufferAsync(jimp.AUTO);

+ 2 - 2
src/core/operations/ScanForEmbeddedFiles.mjs

@@ -6,8 +6,8 @@
 
 import Operation from "../Operation";
 import Utils from "../Utils";
-import {scanForFileTypes} from "../lib/FileType";
-import {FILE_SIGNATURES} from "../lib/FileSignatures";
+import { scanForFileTypes } from "../lib/FileType";
+import { FILE_SIGNATURES } from "../lib/FileSignatures";
 
 /**
  * Scan for Embedded Files operation

+ 2 - 1
src/core/operations/Scrypt.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import Utils from "../Utils";
 import OperationError from "../errors/OperationError";
 import scryptsy from "scryptsy";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * Scrypt operation
@@ -73,7 +74,7 @@ class Scrypt extends Operation {
                 input, salt, iterations, memFactor, parallelFactor, keyLength,
                 p => {
                     // Progress callback
-                    if (ENVIRONMENT_IS_WORKER())
+                    if (isWorkerEnvironment())
                         self.sendStatusMessage(`Progress: ${p.percent.toFixed(0)}%`);
                 }
             );

+ 4 - 3
src/core/operations/ToCharcode.mjs

@@ -6,8 +6,9 @@
 
 import Operation from "../Operation";
 import Utils from "../Utils";
-import {DELIM_OPTIONS} from "../lib/Delim";
+import { DELIM_OPTIONS } from "../lib/Delim";
 import OperationError from "../errors/OperationError";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * To Charcode operation
@@ -69,11 +70,11 @@ class ToCharcode extends Operation {
                 else if (ordinal < 4294967296) padding = 8;
                 else padding = 2;
 
-                if (padding > 2 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
+                if (padding > 2 && isWorkerEnvironment()) self.setOption("attemptHighlight", false);
 
                 output += Utils.hex(ordinal, padding) + delim;
             } else {
-                if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
+                if (isWorkerEnvironment()) self.setOption("attemptHighlight", false);
                 output += ordinal.toString(base) + delim;
             }
         }

+ 2 - 1
src/core/operations/ToMessagePack.mjs

@@ -7,6 +7,7 @@
 import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import notepack from "notepack.io";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * To MessagePack operation
@@ -35,7 +36,7 @@ class ToMessagePack extends Operation {
      */
     run(input, args) {
         try {
-            if (ENVIRONMENT_IS_WORKER()) {
+            if (isWorkerEnvironment()) {
                 return notepack.encode(input);
             } else {
                 const res = notepack.encode(input);

+ 3 - 2
src/core/operations/XORBruteForce.mjs

@@ -8,6 +8,7 @@ import Operation from "../Operation";
 import Utils from "../Utils";
 import { bitOp, xor } from "../lib/BitwiseOp";
 import { toHex } from "../lib/Hex";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * XOR Brute Force operation
@@ -94,7 +95,7 @@ class XORBruteForce extends Operation {
 
         input = input.slice(sampleOffset, sampleOffset + sampleLength);
 
-        if (ENVIRONMENT_IS_WORKER())
+        if (isWorkerEnvironment())
             self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
 
         /**
@@ -114,7 +115,7 @@ class XORBruteForce extends Operation {
         };
 
         for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
-            if (key % 10000 === 0 && ENVIRONMENT_IS_WORKER()) {
+            if (key % 10000 === 0 && isWorkerEnvironment()) {
                 self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
             }
 

+ 5 - 4
src/core/operations/YARARules.mjs

@@ -7,6 +7,7 @@
 import Operation from "../Operation";
 import OperationError from "../errors/OperationError";
 import Yara from "libyara-wasm";
+import { isWorkerEnvironment } from "../Utils";
 
 /**
  * YARA Rules operation
@@ -61,21 +62,21 @@ class YARARules extends Operation {
      * @returns {string}
      */
     run(input, args) {
-        if (ENVIRONMENT_IS_WORKER())
+        if (isWorkerEnvironment())
             self.sendStatusMessage("Instantiating YARA...");
         const [rules, showStrings, showLengths, showMeta, showCounts] = args;
         return new Promise((resolve, reject) => {
             Yara().then(yara => {
-                if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Converting data for YARA.");
+                if (isWorkerEnvironment()) self.sendStatusMessage("Converting data for YARA.");
                 let matchString = "";
 
                 const inpArr = new Uint8Array(input); // Turns out embind knows that JS uint8array <==> C++ std::string
 
-                if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Running YARA matching.");
+                if (isWorkerEnvironment()) self.sendStatusMessage("Running YARA matching.");
 
                 const resp = yara.run(inpArr, rules);
 
-                if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Processing data.");
+                if (isWorkerEnvironment()) self.sendStatusMessage("Processing data.");
 
                 if (resp.compileErrors.size() > 0) {
                     for (let i = 0; i < resp.compileErrors.size(); i++) {

+ 0 - 11
src/node/config/scripts/generateNodeIndex.mjs

@@ -55,17 +55,6 @@ code +=`
 
 global.File = File;
 
-// Define global environment functions
-global.ENVIRONMENT_IS_WORKER = function() {
-    return typeof importScripts === "function";
-};
-global.ENVIRONMENT_IS_NODE = function() {
-    return typeof process === "object" && typeof require === "function";
-};
-global.ENVIRONMENT_IS_WEB = function() {
-    return typeof window === "object";
-};
-
 /**
  * generateChef
  *

+ 0 - 11
src/node/index.mjs

@@ -321,17 +321,6 @@ import {
 
 global.File = File;
 
-// Define global environment functions
-global.ENVIRONMENT_IS_WORKER = function() {
-    return typeof importScripts === "function";
-};
-global.ENVIRONMENT_IS_NODE = function() {
-    return typeof process === "object" && typeof require === "function";
-};
-global.ENVIRONMENT_IS_WEB = function() {
-    return typeof window === "object";
-};
-
 /**
  * generateChef
  *

+ 0 - 10
tests/lib/utils.mjs

@@ -8,16 +8,6 @@
  * @license Apache-2.0
  */
 
-// Define global environment functions
-global.ENVIRONMENT_IS_WORKER = function() {
-    return typeof importScripts === "function";
-};
-global.ENVIRONMENT_IS_NODE = function() {
-    return typeof process === "object" && typeof require === "function";
-};
-global.ENVIRONMENT_IS_WEB = function() {
-    return typeof window === "object";
-};
 
 /**
  * Helper function to convert a status to an icon.

+ 0 - 11
tests/node/index.mjs

@@ -15,17 +15,6 @@ import {
     logTestReport,
 } from "../lib/utils";
 
-// Define global environment functions
-global.ENVIRONMENT_IS_WORKER = function() {
-    return typeof importScripts === "function";
-};
-global.ENVIRONMENT_IS_NODE = function() {
-    return typeof process === "object" && typeof require === "function";
-};
-global.ENVIRONMENT_IS_WEB = function() {
-    return typeof window === "object";
-};
-
 import TestRegister from "../lib/TestRegister";
 import "./tests/nodeApi";
 import "./tests/operations";

+ 0 - 11
tests/operations/index.mjs

@@ -16,17 +16,6 @@ import {
     logTestReport,
 } from "../lib/utils";
 
-// Define global environment functions
-global.ENVIRONMENT_IS_WORKER = function() {
-    return typeof importScripts === "function";
-};
-global.ENVIRONMENT_IS_NODE = function() {
-    return typeof process === "object" && typeof require === "function";
-};
-global.ENVIRONMENT_IS_WEB = function() {
-    return typeof window === "object";
-};
-
 import TestRegister from "../lib/TestRegister";
 import "./tests/BCD";
 import "./tests/BSON";