diff --git a/src/core/Recipe.mjs b/src/core/Recipe.mjs index 7824d1e8..95c33d1c 100755 --- a/src/core/Recipe.mjs +++ b/src/core/Recipe.mjs @@ -4,7 +4,7 @@ * @license Apache-2.0 */ -import OperationConfig from "./config/OperationConfig.json" assert {type: "json"}; +import OperationConfig from "./config/OperationConfig.json" with {type: "json"}; import OperationError from "./errors/OperationError.mjs"; import Operation from "./Operation.mjs"; import DishError from "./errors/DishError.mjs"; @@ -17,7 +17,7 @@ let modules = null; /** * The Recipe controls a list of Operations and the Dish they operate on. */ -class Recipe { +class Recipe { /** * Recipe constructor @@ -176,7 +176,7 @@ class Recipe { * @returns {number} * - The final progress through the recipe */ - async execute(dish, startFrom=0, forkState={}) { + async execute(dish, startFrom = 0, forkState = {}) { let op, input, output, numJumps = 0, numRegisters = forkState.numRegisters || 0; @@ -204,19 +204,19 @@ class Recipe { log.debug(`Executing operation '${op.name}'`); if (isWorkerEnvironment()) { - self.sendStatusMessage(`Baking... (${i+1}/${this.opList.length})`); + self.sendStatusMessage(`Baking... (${i + 1}/${this.opList.length})`); self.sendProgressMessage(i + 1, this.opList.length); } if (op.flowControl) { // Package up the current state let state = { - "progress": i, - "dish": dish, - "opList": this.opList, - "numJumps": numJumps, + "progress": i, + "dish": dish, + "opList": this.opList, + "numJumps": numJumps, "numRegisters": numRegisters, - "forkOffset": forkState.forkOffset || 0 + "forkOffset": forkState.forkOffset || 0 }; state = await op.run(state); @@ -339,7 +339,7 @@ class Recipe { */ lastOpPresented(progress) { if (progress < 1) return false; - return this.opList[progress-1].presentType !== this.opList[progress-1].outputType; + return this.opList[progress - 1].presentType !== this.opList[progress - 1].outputType; } } diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index 14111ec7..d1a0f2d0 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -1,4 +1,4 @@ -import OperationConfig from "../config/OperationConfig.json" assert {type: "json"}; +import OperationConfig from "../config/OperationConfig.json" with {type: "json"}; import Utils, { isWorkerEnvironment } from "../Utils.mjs"; import Recipe from "../Recipe.mjs"; import Dish from "../Dish.mjs"; diff --git a/src/core/operations/GoogleTranslate.mjs b/src/core/operations/GoogleTranslate.mjs index 43ffb2e9..b088f755 100644 --- a/src/core/operations/GoogleTranslate.mjs +++ b/src/core/operations/GoogleTranslate.mjs @@ -59,7 +59,7 @@ class GoogleTranslate extends Operation { * @param {Object[]} args * @returns {string} */ - run(input, args) { + async run(input, args) { const [sourceLanguage, targetLanguage, authType, authString] = args; if (input.length === 0) return ""; @@ -90,29 +90,31 @@ class GoogleTranslate extends Operation { cache: "no-cache", }; - return fetch(url, config) - .then(r => { - if (!r.ok) { - return r.json().then(err => { - let msg = err?.error?.message || r.statusText; - throw new OperationError(`Google Translation API Error (${r.status}): ${msg}`); - }).catch(() => { - throw new OperationError(`Google Translation API Error: ${r.status} ${r.statusText}`); - }); - } - return r.json(); - }) - .then(data => { - if (data && data.data && data.data.translations && data.data.translations.length > 0) { - return data.data.translations[0].translatedText; - } - throw new OperationError("Error: Unexpected response format from Google Translation API."); - }) - .catch(e => { - if (e instanceof OperationError) throw e; - throw new OperationError(e.toString() + - "\n\nThis error could be caused by a network issue or invalid authentication."); - }); + try { + const response = await fetch(url, config); + let responseData; + + try { + responseData = await response.json(); + } catch (err) { + throw new OperationError("Error: Failed to parse response from Google Translation API."); + } + + if (!response.ok) { + const msg = responseData?.error?.message || response.statusText; + throw new OperationError(`Google Translation API Error (${response.status}): ${msg}`); + } + + if (responseData && responseData.data && responseData.data.translations && responseData.data.translations.length > 0) { + return responseData.data.translations[0].translatedText; + } + + throw new OperationError("Error: Unexpected response format from Google Translation API."); + } catch (e) { + if (e.name === "OperationError") throw e; + throw new OperationError(e.message || e.toString() + + "\n\nThis error could be caused by a network issue or invalid authentication."); + } } } diff --git a/src/node/api.mjs b/src/node/api.mjs index 88b3f834..b704a2bb 100644 --- a/src/node/api.mjs +++ b/src/node/api.mjs @@ -10,7 +10,7 @@ import NodeDish from "./NodeDish.mjs"; import NodeRecipe from "./NodeRecipe.mjs"; -import OperationConfig from "../core/config/OperationConfig.json" assert {type: "json"}; +import OperationConfig from "../core/config/OperationConfig.json" with {type: "json"}; import { sanitise, removeSubheadingsFromArray, sentenceToCamelCase } from "./apiUtils.mjs"; import ExcludedOperationError from "../core/errors/ExcludedOperationError.mjs"; diff --git a/src/web/static/sitemap.mjs b/src/web/static/sitemap.mjs index 4f8101d4..f2ad5e83 100644 --- a/src/web/static/sitemap.mjs +++ b/src/web/static/sitemap.mjs @@ -1,5 +1,5 @@ import sm from "sitemap"; -import OperationConfig from "../../core/config/OperationConfig.json" assert { type: "json" }; +import OperationConfig from "../../core/config/OperationConfig.json" with {type: "json"}; /** * Generates an XML sitemap for all CyberChef operations and a number of recipes. diff --git a/tests/node/tests/Categories.mjs b/tests/node/tests/Categories.mjs index e6f8bd72..f37b43ec 100644 --- a/tests/node/tests/Categories.mjs +++ b/tests/node/tests/Categories.mjs @@ -1,6 +1,6 @@ import TestRegister from "../../lib/TestRegister.mjs"; -import Categories from "../../../src/core/config/Categories.json" assert {type: "json"}; -import OperationConfig from "../../../src/core/config/OperationConfig.json" assert {type: "json"}; +import Categories from "../../../src/core/config/Categories.json" with {type: "json"}; +import OperationConfig from "../../../src/core/config/OperationConfig.json" with {type: "json"}; import it from "../assertionHandler.mjs"; import assert from "assert"; diff --git a/tests/operations/tests/GoogleTranslate.mjs b/tests/operations/tests/GoogleTranslate.mjs new file mode 100644 index 00000000..8d070e07 --- /dev/null +++ b/tests/operations/tests/GoogleTranslate.mjs @@ -0,0 +1,20 @@ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Google Translate: Missing Auth String", + input: "Hello world", + expectedError: "Error: Please provide a valid GCP Auth String (API Key or OAuth Token).", + recipeConfig: [ + { + "op": "Google Translate", + "args": [ + "en", + "es", + "API Key", + "" + ] + } + ] + } +]);