translate is working
This commit is contained in:
parent
974be37b3b
commit
f37e215c22
@ -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";
|
||||||
@ -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,7 +204,7 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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";
|
||||||
|
|||||||
@ -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) {
|
||||||
.then(data => {
|
const msg = responseData?.error?.message || response.statusText;
|
||||||
if (data && data.data && data.data.translations && data.data.translations.length > 0) {
|
throw new OperationError(`Google Translation API Error (${response.status}): ${msg}`);
|
||||||
return data.data.translations[0].translatedText;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.");
|
throw new OperationError("Error: Unexpected response format from Google Translation API.");
|
||||||
})
|
} catch (e) {
|
||||||
.catch(e => {
|
if (e.name === "OperationError") throw e;
|
||||||
if (e instanceof OperationError) throw e;
|
throw new OperationError(e.message || e.toString() +
|
||||||
throw new OperationError(e.toString() +
|
|
||||||
"\n\nThis error could be caused by a network issue or invalid authentication.");
|
"\n\nThis error could be caused by a network issue or invalid authentication.");
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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";
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
||||||
|
|||||||
@ -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";
|
||||||
|
|
||||||
|
|||||||
20
tests/operations/tests/GoogleTranslate.mjs
Normal file
20
tests/operations/tests/GoogleTranslate.mjs
Normal 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",
|
||||||
|
""
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user