translate is working

This commit is contained in:
Andy L WSL 2026-02-28 10:22:16 +00:00
parent 974be37b3b
commit f37e215c22
7 changed files with 61 additions and 39 deletions

View File

@ -4,7 +4,7 @@
* @license Apache-2.0 * @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 OperationError from "./errors/OperationError.mjs";
import Operation from "./Operation.mjs"; import Operation from "./Operation.mjs";
import DishError from "./errors/DishError.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. * The Recipe controls a list of Operations and the Dish they operate on.
*/ */
class Recipe { class Recipe {
/** /**
* Recipe constructor * Recipe constructor
@ -176,7 +176,7 @@ class Recipe {
* @returns {number} * @returns {number}
* - The final progress through the recipe * - The final progress through the recipe
*/ */
async execute(dish, startFrom=0, forkState={}) { async execute(dish, startFrom = 0, forkState = {}) {
let op, input, output, let op, input, output,
numJumps = 0, numJumps = 0,
numRegisters = forkState.numRegisters || 0; numRegisters = forkState.numRegisters || 0;
@ -204,19 +204,19 @@ class Recipe {
log.debug(`Executing operation '${op.name}'`); log.debug(`Executing operation '${op.name}'`);
if (isWorkerEnvironment()) { 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); self.sendProgressMessage(i + 1, this.opList.length);
} }
if (op.flowControl) { if (op.flowControl) {
// Package up the current state // Package up the current state
let state = { let state = {
"progress": i, "progress": i,
"dish": dish, "dish": dish,
"opList": this.opList, "opList": this.opList,
"numJumps": numJumps, "numJumps": numJumps,
"numRegisters": numRegisters, "numRegisters": numRegisters,
"forkOffset": forkState.forkOffset || 0 "forkOffset": forkState.forkOffset || 0
}; };
state = await op.run(state); state = await op.run(state);
@ -339,7 +339,7 @@ class Recipe {
*/ */
lastOpPresented(progress) { lastOpPresented(progress) {
if (progress < 1) return false; 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;
} }
} }

View File

@ -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 Utils, { isWorkerEnvironment } from "../Utils.mjs";
import Recipe from "../Recipe.mjs"; import Recipe from "../Recipe.mjs";
import Dish from "../Dish.mjs"; import Dish from "../Dish.mjs";

View File

@ -59,7 +59,7 @@ class GoogleTranslate extends Operation {
* @param {Object[]} args * @param {Object[]} args
* @returns {string} * @returns {string}
*/ */
run(input, args) { async run(input, args) {
const [sourceLanguage, targetLanguage, authType, authString] = args; const [sourceLanguage, targetLanguage, authType, authString] = args;
if (input.length === 0) return ""; if (input.length === 0) return "";
@ -90,29 +90,31 @@ class GoogleTranslate extends Operation {
cache: "no-cache", cache: "no-cache",
}; };
return fetch(url, config) try {
.then(r => { const response = await fetch(url, config);
if (!r.ok) { let responseData;
return r.json().then(err => {
let msg = err?.error?.message || r.statusText; try {
throw new OperationError(`Google Translation API Error (${r.status}): ${msg}`); responseData = await response.json();
}).catch(() => { } catch (err) {
throw new OperationError(`Google Translation API Error: ${r.status} ${r.statusText}`); throw new OperationError("Error: Failed to parse response from Google Translation API.");
}); }
}
return r.json(); if (!response.ok) {
}) const msg = responseData?.error?.message || response.statusText;
.then(data => { throw new OperationError(`Google Translation API Error (${response.status}): ${msg}`);
if (data && data.data && data.data.translations && data.data.translations.length > 0) { }
return data.data.translations[0].translatedText;
} if (responseData && responseData.data && responseData.data.translations && responseData.data.translations.length > 0) {
throw new OperationError("Error: Unexpected response format from Google Translation API."); return responseData.data.translations[0].translatedText;
}) }
.catch(e => {
if (e instanceof OperationError) throw e; throw new OperationError("Error: Unexpected response format from Google Translation API.");
throw new OperationError(e.toString() + } catch (e) {
"\n\nThis error could be caused by a network issue or invalid authentication."); 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.");
}
} }
} }

View File

@ -10,7 +10,7 @@
import NodeDish from "./NodeDish.mjs"; import NodeDish from "./NodeDish.mjs";
import NodeRecipe from "./NodeRecipe.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 { sanitise, removeSubheadingsFromArray, sentenceToCamelCase } from "./apiUtils.mjs";
import ExcludedOperationError from "../core/errors/ExcludedOperationError.mjs"; import ExcludedOperationError from "../core/errors/ExcludedOperationError.mjs";

View File

@ -1,5 +1,5 @@
import sm from "sitemap"; 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. * Generates an XML sitemap for all CyberChef operations and a number of recipes.

View File

@ -1,6 +1,6 @@
import TestRegister from "../../lib/TestRegister.mjs"; import TestRegister from "../../lib/TestRegister.mjs";
import Categories from "../../../src/core/config/Categories.json" assert {type: "json"}; import Categories from "../../../src/core/config/Categories.json" with {type: "json"};
import OperationConfig from "../../../src/core/config/OperationConfig.json" assert {type: "json"}; import OperationConfig from "../../../src/core/config/OperationConfig.json" with {type: "json"};
import it from "../assertionHandler.mjs"; import it from "../assertionHandler.mjs";
import assert from "assert"; import assert from "assert";

View File

@ -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",
""
]
}
]
}
]);